Tsuyotsuyo Engineers will also try to do what is called competitive programming.
Implement a function that returns 0 if the input is a negative value and around that if the input is a positive value.
N = int(input())
def ReLU(x):
if x > 0:
return x
else:
return 0
print(ReLU(N))
Bounce back to the x-axis to play billiards. I miss it because it is a common problem in taking an examination. A bullet is shot at the point where the target location is moved to the x-axis target. The answer is the intersection with the x-axis at that time. I felt that it would be good to calculate the internal division point considering the similarity.
sx, sy, gx, gy = map(int, input().split())
print(float((gy * sx + sy * gx) / (sy + gy)))
If you have N cities and you visit all cities once, how many routes will the time be exactly K?
Since N is at most 8, there are about 8! Of combinations of routes. There seems to be no problem with the full search.
Exporting all combinations is easy with python using itertools
0 1 10 100
1 0 20 200
10 20 0 300
100 200 300 0
To receive a matrix like this with input, you can convert each column to an array and turn it by row.
code.py
N, K = map(int, input().split())
matrix = [input().split() for _ in range(N)]
print(*matrix, sep='\n')
output.py
['0', '1', '10', '100']
['1', '0', '20', '200']
['10', '20', '0', '300']
['100', '200', '300', '0']
The answer is like this. I feel like I can write better
import itertools
N, K = map(int, input().split())
matrix = [list(map(int, input().split())) for _ in range(N)]
ans =0
#Since it starts from 1 and changes to 1, insert the city 1 after finding the permutation combination without 1 (index is 0).
for v in itertools.permutations(range(1, N)):
total_time = 0
keiro = list(v)
keiro.append(0)
keiro.insert(0, 0)
for i in range(len(keiro) -1):
total_time += matrix[keiro[i]][keiro[i+1]]
if total_time == K:
ans += 1
print(ans)
Prepare an array for the length of time, and add the usage time to the usage time zone while looping for people. Out if there is time to exceed W.
I implemented it thinking that, but TLE
max_time = 2 * 10 ** 5
t = [0 for _ in range(max_time)]
n, w = map(int, input().split())
ans = 'Yes'
for i in range(n):
start, end, usage = map(int, input().split())
for j in range(start, end): #This is OK because end is not included
t[j] += usage
if t[j] > w:
ans = 'No'
break
print(ans)
I overlooked the constraints ... N and T were on the order of 2 * 10 ** 5
Since it is only the start and end timings that need to be judged, I was able to go to the point where I should look at the elements at that timing for each person, but I can not think of it after that,
Looking at the explanation, it seems that there is something called "potato method"
https://imoz.jp/algorithms/imos_method.html
With this I was able to solve it in time!
max_time = 2 * 10 ** 5 + 1
t = [0 for _ in range(max_time)]
n, w = map(int, input().split())
ans = 'Yes'
for i in range(n):
start, end, usage = map(int, input().split())
#Use the potato method. Record the timing of entry and exit
t[start] += usage
t[end] -= usage
for i in range(1, len(t)):
t[i] += t[i-1]
if max(t) > w:
print('No')
else:
print(ans)
Recommended Posts