message stringlengths 2 45.8k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 254 108k | cluster float64 3 3 | __index_level_0__ int64 508 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You a captain of a ship. Initially you are standing in a point (x_1, y_1) (obviously, all positions in the sea can be described by cartesian plane) and you want to travel to a point (x_2, y_2).
You know the weather forecast β the string s of length n, consisting only of letters U, D, L and R. The letter corresponds to a direction of wind. Moreover, the forecast is periodic, e.g. the first day wind blows to the side s_1, the second day β s_2, the n-th day β s_n and (n+1)-th day β s_1 again and so on.
Ship coordinates change the following way:
* if wind blows the direction U, then the ship moves from (x, y) to (x, y + 1);
* if wind blows the direction D, then the ship moves from (x, y) to (x, y - 1);
* if wind blows the direction L, then the ship moves from (x, y) to (x - 1, y);
* if wind blows the direction R, then the ship moves from (x, y) to (x + 1, y).
The ship can also either go one of the four directions or stay in place each day. If it goes then it's exactly 1 unit of distance. Transpositions of the ship and the wind add up. If the ship stays in place, then only the direction of wind counts. For example, if wind blows the direction U and the ship moves the direction L, then from point (x, y) it will move to the point (x - 1, y + 1), and if it goes the direction U, then it will move to the point (x, y + 2).
You task is to determine the minimal number of days required for the ship to reach the point (x_2, y_2).
Input
The first line contains two integers x_1, y_1 (0 β€ x_1, y_1 β€ 10^9) β the initial coordinates of the ship.
The second line contains two integers x_2, y_2 (0 β€ x_2, y_2 β€ 10^9) β the coordinates of the destination point.
It is guaranteed that the initial coordinates and destination point coordinates are different.
The third line contains a single integer n (1 β€ n β€ 10^5) β the length of the string s.
The fourth line contains the string s itself, consisting only of letters U, D, L and R.
Output
The only line should contain the minimal number of days required for the ship to reach the point (x_2, y_2).
If it's impossible then print "-1".
Examples
Input
0 0
4 6
3
UUU
Output
5
Input
0 3
0 0
3
UDD
Output
3
Input
0 0
0 1
1
L
Output
-1
Note
In the first example the ship should perform the following sequence of moves: "RRRRU". Then its coordinates will change accordingly: (0, 0) β (1, 1) β (2, 2) β (3, 3) β (4, 4) β (4, 6).
In the second example the ship should perform the following sequence of moves: "DD" (the third day it should stay in place). Then its coordinates will change accordingly: (0, 3) β (0, 3) β (0, 1) β (0, 0).
In the third example the ship can never reach the point (0, 1).
Submitted Solution:
```
x1, y1 = [int(_) for _ in input().split()]
x2, y2 = [int(_) for _ in input().split()]
length = int(input())
string = input()
wind = 0
h = [0, 0]
cur_wind = []
for i in string:
if i == 'U':
cur_wind.append((0, 1))
h[1] += 1
elif i == 'D':
cur_wind.append((0, -1))
h[1] -= 1
elif i == 'R':
cur_wind.append((1, 0))
h[0] += 1
else:
cur_wind.append((-1, 0))
h[0] -= 1
w = (x2 - x1, y2 - y1)
def min_days(s, f):
return abs(s[0] - f[0]) + abs(s[1] - f[1])
cur_pos = [x1, y1]
day = 1
ans = -1
bad = False
start_diff = min_days(cur_pos, (x2, y2))
cycle_diff = min_days((cur_pos[0] + h[0], cur_pos[1] + h[1]), (x2, y2)) - len(string)
if start_diff == cycle_diff:
bad = True
if bad:
print(-1)
else:
while True:
if day > 30:
break
if wind == len(cur_wind):
wind = 0
cur_pos[0] += cur_wind[wind][0]
cur_pos[1] += cur_wind[wind][1]
days = min_days(cur_pos, (x2, y2))
if days <= day:
ans = day
break
day += 1
wind += 1
print(ans)
``` | instruction | 0 | 10,862 | 3 | 21,724 |
No | output | 1 | 10,862 | 3 | 21,725 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You, the mighty Blackout, are standing in the upper-left (0,0) corner of NxM matrix. You must move either right or down each second.
There are K transformers jumping around the matrix in the following way. Each transformer starts jumping from position (x,y), at time t, and jumps to the next position each second. The x-axes grows downwards, and y-axes grows to the right. The order of jumping positions is defined as {(x,y),(x+d,y-d),(x+d,y),(x,y+d)}, and is periodic. Before time t transformer is not in the matrix.
You want to arrive to the bottom-right corner (N-1,M-1), while slaying transformers and losing the least possible amount of energy. When you meet the transformer (or more of them) in the matrix field, you must kill them all, and you lose the sum of the energy amounts required to kill each transformer.
After the transformer is killed, he of course stops jumping, falls into the abyss and leaves the matrix world. Output minimum possible amount of energy wasted.
Input
In the first line, integers N,M (1 β€ N, M β€ 500), representing size of the matrix, and K (0 β€ K β€ 5*10^5) , the number of jumping transformers.
In next K lines, for each transformer, numbers x, y, d (d β₯ 1), t (0 β€ t β€ N+M-2), and e (0 β€ e β€ 10^9), representing starting coordinates of transformer, jumping positions distance in pattern described above, time when transformer starts jumping, and energy required to kill it.
It is guaranteed that all 4 of jumping points of the transformers are within matrix coordinates
Output
Print single integer, the minimum possible amount of energy wasted, for Blackout to arrive at bottom-right corner.
Example
Input
3 3 5
0 1 1 0 7
1 1 1 0 10
1 1 1 1 2
1 1 1 2 2
0 1 1 2 3
Output
9
Note
If Blackout takes the path from (0, 0) to (2, 0), and then from (2, 0) to (2, 2) he will need to kill the first and third transformer for a total energy cost of 9. There exists no path with less energy value.
Submitted Solution:
```
fst_line = input()
fst_line = fst_line.split(' ')
n, m, k = [int(var) for var in fst_line]
enemies = []
for _ in range(k):
tmp_line = input()
tmp_line = tmp_line.split(' ')
enemies.append([int(var) for var in tmp_line]) # x y d t e
best_score = 99999999
def calculate_position(x, y, d, t_e, t):
if t >= t_e:
step = (t - t_e) % 4
if step == 0: return x, y
elif step == 1: return x + d, y - d
elif step == 2: return x + d, y
else: return x, y + d
def simulate(n, m, x, y, t, score, living_enemies):
global best_score
if score >= best_score: pass
if n - 1 == x and m - 1 == y: best_score = score
else:
for enemy_index in living_enemies[::-1]:
x_e, y_e, d_e, t_e, e_e = enemies[enemy_index]
if t_e > t: continue
tmp_x, tmp_y = calculate_position(x_e, y_e, d_e, t_e, t)
if x == tmp_x and y == tmp_y:
score += e_e
living_enemies.remove(enemy_index)
if x + 1 < n:
simulate(n, m, x + 1, y, t + 1, score, living_enemies.copy())
if y + 1 < m:
simulate(n, m, x, y + 1, t + 1, score, living_enemies.copy())
simulate(n, m, 0, 0, 0, 0, [enemy_index for enemy_index in range(len(enemies))])
print(best_score)
``` | instruction | 0 | 10,871 | 3 | 21,742 |
No | output | 1 | 10,871 | 3 | 21,743 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You, the mighty Blackout, are standing in the upper-left (0,0) corner of NxM matrix. You must move either right or down each second.
There are K transformers jumping around the matrix in the following way. Each transformer starts jumping from position (x,y), at time t, and jumps to the next position each second. The x-axes grows downwards, and y-axes grows to the right. The order of jumping positions is defined as {(x,y),(x+d,y-d),(x+d,y),(x,y+d)}, and is periodic. Before time t transformer is not in the matrix.
You want to arrive to the bottom-right corner (N-1,M-1), while slaying transformers and losing the least possible amount of energy. When you meet the transformer (or more of them) in the matrix field, you must kill them all, and you lose the sum of the energy amounts required to kill each transformer.
After the transformer is killed, he of course stops jumping, falls into the abyss and leaves the matrix world. Output minimum possible amount of energy wasted.
Input
In the first line, integers N,M (1 β€ N, M β€ 500), representing size of the matrix, and K (0 β€ K β€ 5*10^5) , the number of jumping transformers.
In next K lines, for each transformer, numbers x, y, d (d β₯ 1), t (0 β€ t β€ N+M-2), and e (0 β€ e β€ 10^9), representing starting coordinates of transformer, jumping positions distance in pattern described above, time when transformer starts jumping, and energy required to kill it.
It is guaranteed that all 4 of jumping points of the transformers are within matrix coordinates
Output
Print single integer, the minimum possible amount of energy wasted, for Blackout to arrive at bottom-right corner.
Example
Input
3 3 5
0 1 1 0 7
1 1 1 0 10
1 1 1 1 2
1 1 1 2 2
0 1 1 2 3
Output
9
Note
If Blackout takes the path from (0, 0) to (2, 0), and then from (2, 0) to (2, 2) he will need to kill the first and third transformer for a total energy cost of 9. There exists no path with less energy value.
Submitted Solution:
```
# -*- coding: utf-8 -*-+
"""
Created on Sun Jan 12 10:20:32 2020
@author: Rodro
"""
inp = str(input()).split()
n = int(inp[0])
m = int(inp[1])
k = int(inp[2])
oo = 1e20
mcost = [ [[0, 0] for __ in range(n)] for _ in range(m)]#[i][j][k] donde k define de el costo de venir de la izk(k = 0) o de la derecha(k = 1)
mmove = [[[oo, oo] for __ in range(n)] for _ in range(m)]#[i][j][k] donde k indica moverse a la derecha(k = 0) o hacia abajo(k = 1)
mpend = [[[[], []] for __ in range(n)] for _ in range(m)]#[i][j][k] se guarda posible posiciones de un trabsformador si es posible qye haya
#sido asesinado previamente. y por tanto no haga falta matarlo k = 1 implica que se vino de arriba,
#k = 0 de la izq.
for _ in range(k):
inp = str(input()).split()
x, y, d, t, e = int(inp[0]), int(inp[1]), int(inp[2]), int(inp[3]), int(inp[4])
z = x + y
p0 = z >= t and 0 == (z - t)%4
p1 = z >= t + 1 and 0 == (z - t - 1)%4
p2 = z + d >= t + 2 and 0 == (z + d - t - 2)%4
p3 = z + d >= t + 3 and 0 == (z + d - t - 3)%4
if p0:
mcost[y][x][0] += e
mcost[y][x][1] += e
if p1:
mcost[y - d][x + d][0] += e
mcost[y - d][x + d][1] += e
if p2:
if p0:
mpend[y][x + d][0].append((y - d, e))
else: mcost[y][x + d][0] += e
if p1:
mpend[y][x + d][1].append((x, e))
else: mcost[y][x + d][1] += e
if p3:
if p0:
mpend[y + d][x][1].append((x, e))
else: mcost[y + d][x][1] += e
mcost[y + d][x][0] += e
for i in range(m):
for j in range(n):
sorted(mpend[i][j][0])
sorted(mpend[i][j][1])
mmove[0][0][0] = mmove[0][0][1] = 0
for i in range(m):
for j in range(n):
cost = 0
for k in range(j + 1, n):
while(len(mpend[i][k][0]) > 0) and mpend[i][k][0][-1][0] < j:
mcost[i][k][0] += mpend[i][k][0][-1][1]
mpend[i][k][0].pop()
cost += mcost[i][k][0]
mmove[i][k][0] = min(mmove[i][k][0], mmove[i][j][1] + cost)
cost = 0
for k in range(i + 1, m):
while(len(mpend[k][j][1]) > 0 and mpend[k][j][1][-1][0] < i):
mcost[k][j][1] += mpend[k][j][1][-1][1]
mpend[k][j][1].pop()
cost += mcost[k][j][1]
mmove[k][j][1] = min(mmove[k][j][1], mmove[i][j][0] + cost)
print("Respuesta", min(mmove[m - 1][n - 1][0], mmove[m - 1][n - 1][1]))
#print(mmove)
#print("##############")
#print(mcost)
``` | instruction | 0 | 10,872 | 3 | 21,744 |
No | output | 1 | 10,872 | 3 | 21,745 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You, the mighty Blackout, are standing in the upper-left (0,0) corner of NxM matrix. You must move either right or down each second.
There are K transformers jumping around the matrix in the following way. Each transformer starts jumping from position (x,y), at time t, and jumps to the next position each second. The x-axes grows downwards, and y-axes grows to the right. The order of jumping positions is defined as {(x,y),(x+d,y-d),(x+d,y),(x,y+d)}, and is periodic. Before time t transformer is not in the matrix.
You want to arrive to the bottom-right corner (N-1,M-1), while slaying transformers and losing the least possible amount of energy. When you meet the transformer (or more of them) in the matrix field, you must kill them all, and you lose the sum of the energy amounts required to kill each transformer.
After the transformer is killed, he of course stops jumping, falls into the abyss and leaves the matrix world. Output minimum possible amount of energy wasted.
Input
In the first line, integers N,M (1 β€ N, M β€ 500), representing size of the matrix, and K (0 β€ K β€ 5*10^5) , the number of jumping transformers.
In next K lines, for each transformer, numbers x, y, d (d β₯ 1), t (0 β€ t β€ N+M-2), and e (0 β€ e β€ 10^9), representing starting coordinates of transformer, jumping positions distance in pattern described above, time when transformer starts jumping, and energy required to kill it.
It is guaranteed that all 4 of jumping points of the transformers are within matrix coordinates
Output
Print single integer, the minimum possible amount of energy wasted, for Blackout to arrive at bottom-right corner.
Example
Input
3 3 5
0 1 1 0 7
1 1 1 0 10
1 1 1 1 2
1 1 1 2 2
0 1 1 2 3
Output
9
Note
If Blackout takes the path from (0, 0) to (2, 0), and then from (2, 0) to (2, 2) he will need to kill the first and third transformer for a total energy cost of 9. There exists no path with less energy value.
Submitted Solution:
```
fst_line = input()
fst_line = fst_line.split(' ')
n, m, k = [int(var) for var in fst_line]
enemies = []
for _ in range(k):
tmp_line = input()
tmp_line = tmp_line.split(' ')
enemies.append([int(var) for var in tmp_line]) # x y d t e
best_score = 99999999
def calculate_position(x, y, d, t_e, t):
if t >= t_e:
step = (t - t_e) % 4
if step == 0: return x, y
elif step == 1: return x + d, y - d
elif step == 2: return x + d, y
else: return x, y + d
def simulate(n, m, x, y, t, score, living_enemies):
global best_score
if score >= best_score: pass
if n - 1 == x and m - 1 == y: best_score = score
else:
for enemy_index in living_enemies[::-1]:
x_e, y_e, d_e, t_e, e_e = enemies[enemy_index]
if t_e > t: continue
tmp_x, tmp_y = calculate_position(x_e, y_e, d_e, t_e, t)
if x == tmp_x and y == tmp_y:
score += e_e
living_enemies.remove(enemy_index)
if x + 1 < n:
simulate(n, m, x + 1, y, t + 1, score, living_enemies.copy())
if y + 1 < m:
simulate(n, m, x, y + 1, t + 1, score, living_enemies.copy())
simulate(n, m, 0, 0, 0, 0, [enemy_index for enemy_index in range(len(enemies))])
print(best_score-1)
``` | instruction | 0 | 10,873 | 3 | 21,746 |
No | output | 1 | 10,873 | 3 | 21,747 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You, the mighty Blackout, are standing in the upper-left (0,0) corner of NxM matrix. You must move either right or down each second.
There are K transformers jumping around the matrix in the following way. Each transformer starts jumping from position (x,y), at time t, and jumps to the next position each second. The x-axes grows downwards, and y-axes grows to the right. The order of jumping positions is defined as {(x,y),(x+d,y-d),(x+d,y),(x,y+d)}, and is periodic. Before time t transformer is not in the matrix.
You want to arrive to the bottom-right corner (N-1,M-1), while slaying transformers and losing the least possible amount of energy. When you meet the transformer (or more of them) in the matrix field, you must kill them all, and you lose the sum of the energy amounts required to kill each transformer.
After the transformer is killed, he of course stops jumping, falls into the abyss and leaves the matrix world. Output minimum possible amount of energy wasted.
Input
In the first line, integers N,M (1 β€ N, M β€ 500), representing size of the matrix, and K (0 β€ K β€ 5*10^5) , the number of jumping transformers.
In next K lines, for each transformer, numbers x, y, d (d β₯ 1), t (0 β€ t β€ N+M-2), and e (0 β€ e β€ 10^9), representing starting coordinates of transformer, jumping positions distance in pattern described above, time when transformer starts jumping, and energy required to kill it.
It is guaranteed that all 4 of jumping points of the transformers are within matrix coordinates
Output
Print single integer, the minimum possible amount of energy wasted, for Blackout to arrive at bottom-right corner.
Example
Input
3 3 5
0 1 1 0 7
1 1 1 0 10
1 1 1 1 2
1 1 1 2 2
0 1 1 2 3
Output
9
Note
If Blackout takes the path from (0, 0) to (2, 0), and then from (2, 0) to (2, 2) he will need to kill the first and third transformer for a total energy cost of 9. There exists no path with less energy value.
Submitted Solution:
```
fst_line = input()
fst_line = fst_line.split(' ')
n, m, k = [int(var) for var in fst_line]
enemies = []
for _ in range(k):
tmp_line = input()
tmp_line = tmp_line.split(' ')
enemies.append([int(var) for var in tmp_line]) # x y d t e
enemies_indices = [index for index in range(len(enemies))]
best_score = 99999999
def calculate_position(x, y, d, t_e, t):
if t >= t_e:
step = (t - t_e) % 4
if step == 0: return x, y
elif step == 1: return x + d, y - d
elif step == 2: return x + d, y
else: return x, y + d
def simulate(n, m, x, y, t, score, living_enemies):
global best_score
if score >= best_score: pass
if n - 1 == x and m - 1 == y and score < best_score: best_score = score
else:
for enemy_index in living_enemies[::-1]:
x_e, y_e, d_e, t_e, e_e = enemies[enemy_index]
if t_e > t: continue
tmp_x, tmp_y = calculate_position(x_e, y_e, d_e, t_e, t)
if x == tmp_x and y == tmp_y:
score += e_e
living_enemies.remove(enemy_index)
if x + 1 < n:
simulate(n, m, x + 1, y, t + 1, score, living_enemies.copy())
if y + 1 < m:
simulate(n, m, x, y + 1, t + 1, score, living_enemies.copy())
simulate(n, m, 0, 0, 0, 0, enemies_indices)
print(best_score)
``` | instruction | 0 | 10,874 | 3 | 21,748 |
No | output | 1 | 10,874 | 3 | 21,749 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem you have to implement an algorithm to defragment your hard disk. The hard disk consists of a sequence of clusters, numbered by integers from 1 to n. The disk has m recorded files, the i-th file occupies clusters with numbers ai, 1, ai, 2, ..., ai, ni. These clusters are not necessarily located consecutively on the disk, but the order in which they are given corresponds to their sequence in the file (cluster ai, 1 contains the first fragment of the i-th file, cluster ai, 2 has the second fragment, etc.). Also the disc must have one or several clusters which are free from files.
You are permitted to perform operations of copying the contents of cluster number i to cluster number j (i and j must be different). Moreover, if the cluster number j used to keep some information, it is lost forever. Clusters are not cleaned, but after the defragmentation is complete, some of them are simply declared unusable (although they may possibly still contain some fragments of files).
Your task is to use a sequence of copy operations to ensure that each file occupies a contiguous area of memory. Each file should occupy a consecutive cluster section, the files must follow one after another from the beginning of the hard disk. After defragmentation all free (unused) clusters should be at the end of the hard disk. After defragmenting files can be placed in an arbitrary order. Clusters of each file should go consecutively from first to last. See explanatory examples in the notes.
Print the sequence of operations leading to the disk defragmentation. Note that you do not have to minimize the number of operations, but it should not exceed 2n.
Input
The first line contains two integers n and m (1 β€ n, m β€ 200) β the number of clusters and the number of files, correspondingly. Next m lines contain descriptions of the files. The first number in the line is ni (ni β₯ 1), the number of clusters occupied by the i-th file. Then follow ni numbers ai, 1, ai, 2, ..., ai, ni (1 β€ ai, j β€ n). It is guaranteed that each cluster number occurs not more than once and <image>, that is, there exists at least one unused cluster. Numbers on each line are separated by spaces.
Output
In the first line print a single integer k (0 β€ k β€ 2n) β the number of operations needed to defragment the disk. Next k lines should contain the operations' descriptions as "i j" (copy the contents of the cluster number i to the cluster number j).
Examples
Input
7 2
2 1 2
3 3 4 5
Output
0
Input
7 2
2 1 3
3 2 4 5
Output
3
2 6
3 2
6 3
Note
Let's say that a disk consists of 8 clusters and contains two files. The first file occupies two clusters and the second file occupies three clusters. Let's look at examples of correct and incorrect positions of files after defragmentation.
<image>
Example 2: each file must occupy a contiguous area of memory.
Example 3: the order of files to each other is not important, at first the second file can be written, and then β the first one.
Example 4: violating the order of file fragments to each other is not allowed.
Example 5: unused clusters should be located at the end, and in this example the unused clusters are 3, 7, 8.
Submitted Solution:
```
"""
Brandt Smith, Peter Haddad and Lemuel Gorion
codeforces.com
Problem 180A
"""
c, f = input().split(' ')
hd = [0] * int(c)
total_count = 0
moves = []
for i in range(int(f)):
file = input().split()
for x in range(1,len(file)):
hd[int(file[x]) - 1] = [i + 1,x]
while True:
count = 0
zeros = []
for i in range(len(hd)):
if hd[i] == 0:
zeros.append(i)
for i in range(len(hd) - 1):
if hd[i] == 0 and hd[i + 1] == 0:
continue
elif hd[i] != 0 and hd[i + 1] == 0:
continue
elif hd[i] == 0 and hd[i + 1] != 0:
if hd[i - 1] != 0 and i - 1 != -1 and (hd[i - 1][0] > hd[i + 1][0] or hd[i - 1][1] > hd[i + 1][1]):
moves.append([i, i + 1])
moves.append([i + 2, i])
temp = hd[i - 1]
hd[i - 1] = hd[i + 1]
hd[i] = temp
hd[i + 1] = 0
count += 2
else:
if hd[i - 1] == 0:
moves.append([i + 2, i])
temp = hd[i + 1]
hd[i + 1] = hd[i - 1]
hd[i - 1] = temp
else:
moves.append([i + 2, i + 1])
temp = hd[i + 1]
hd[i + 1] = hd[i]
hd[i] = temp
count += 1
elif hd[i][0] > hd[i + 1][0]:
t = zeros.pop()
moves.append([i + 1, t])
moves.append([i + 2, i + 1])
moves.append([t, i + 2])
zeros.append(t)
temp = hd[i + 1]
hd[i + 1] = hd[i]
hd[i] = temp
count += 3
elif hd[i][0] == hd[i + 1][0] and hd[i][1] > hd[i + 1][1]:
t = zeros.pop()
moves.append([i + 1, t])
moves.append([i + 2, i + 1])
moves.append([t, i + 2])
zeros.append(t)
temp = hd[i + 1]
hd[i + 1] = hd[i]
hd[i] = temp
count += 3
total_count += count
if count == 0:
break
print(total_count)
if total_count > 0:
for move in moves:
print(move[0], move[1])
``` | instruction | 0 | 11,033 | 3 | 22,066 |
No | output | 1 | 11,033 | 3 | 22,067 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem you have to implement an algorithm to defragment your hard disk. The hard disk consists of a sequence of clusters, numbered by integers from 1 to n. The disk has m recorded files, the i-th file occupies clusters with numbers ai, 1, ai, 2, ..., ai, ni. These clusters are not necessarily located consecutively on the disk, but the order in which they are given corresponds to their sequence in the file (cluster ai, 1 contains the first fragment of the i-th file, cluster ai, 2 has the second fragment, etc.). Also the disc must have one or several clusters which are free from files.
You are permitted to perform operations of copying the contents of cluster number i to cluster number j (i and j must be different). Moreover, if the cluster number j used to keep some information, it is lost forever. Clusters are not cleaned, but after the defragmentation is complete, some of them are simply declared unusable (although they may possibly still contain some fragments of files).
Your task is to use a sequence of copy operations to ensure that each file occupies a contiguous area of memory. Each file should occupy a consecutive cluster section, the files must follow one after another from the beginning of the hard disk. After defragmentation all free (unused) clusters should be at the end of the hard disk. After defragmenting files can be placed in an arbitrary order. Clusters of each file should go consecutively from first to last. See explanatory examples in the notes.
Print the sequence of operations leading to the disk defragmentation. Note that you do not have to minimize the number of operations, but it should not exceed 2n.
Input
The first line contains two integers n and m (1 β€ n, m β€ 200) β the number of clusters and the number of files, correspondingly. Next m lines contain descriptions of the files. The first number in the line is ni (ni β₯ 1), the number of clusters occupied by the i-th file. Then follow ni numbers ai, 1, ai, 2, ..., ai, ni (1 β€ ai, j β€ n). It is guaranteed that each cluster number occurs not more than once and <image>, that is, there exists at least one unused cluster. Numbers on each line are separated by spaces.
Output
In the first line print a single integer k (0 β€ k β€ 2n) β the number of operations needed to defragment the disk. Next k lines should contain the operations' descriptions as "i j" (copy the contents of the cluster number i to the cluster number j).
Examples
Input
7 2
2 1 2
3 3 4 5
Output
0
Input
7 2
2 1 3
3 2 4 5
Output
3
2 6
3 2
6 3
Note
Let's say that a disk consists of 8 clusters and contains two files. The first file occupies two clusters and the second file occupies three clusters. Let's look at examples of correct and incorrect positions of files after defragmentation.
<image>
Example 2: each file must occupy a contiguous area of memory.
Example 3: the order of files to each other is not important, at first the second file can be written, and then β the first one.
Example 4: violating the order of file fragments to each other is not allowed.
Example 5: unused clusters should be located at the end, and in this example the unused clusters are 3, 7, 8.
Submitted Solution:
```
"""
Brandt Smith, Peter Haddad and Lemuel Gorion
codeforces.com
Problem 180A
"""
c, f = input().split(' ')
hd = [0] * int(c)
total_count = 0
moves = []
for i in range(int(f)):
file = input().split()
for x in range(1,len(file)):
hd[int(file[x]) - 1] = [i + 1,x]
while True:
count = 0
zeros = []
for i in range(len(hd)):
if hd[i] == 0:
zeros.append(i)
for i in range(len(hd) - 1):
if hd[i] == 0 and hd[i + 1] == 0:
continue
elif hd[i] != 0 and hd[i + 1] == 0:
continue
elif hd[i] == 0 and hd[i + 1] != 0:
if hd[i - 1] != 0 and (hd[i - 1][0] > hd[i + 1][0] or hd[i - 1][1] > hd[i + 1][1]):
moves.append([i, i + 1])
moves.append([i + 2, i])
temp = hd[i - 1]
hd[i - 1] = hd[i + 1]
hd[i] = temp
hd[i + 1] = 0
count += 2
else:
moves.append([i + 2, i + 1])
temp = hd[i + 1]
hd[i + 1] = hd[i]
hd[i] = temp
count += 1
elif hd[i][0] > hd[i + 1][0]:
t = zeros.pop()
moves.append([i + 1, t])
moves.append([i + 2, i + 1])
moves.append([t, i + 2])
temp = hd[i + 1]
hd[i + 1] = hd[i]
hd[i] = temp
count += 3
elif hd[i][0] == hd[i + 1][0] and hd[i][1] > hd[i + 1][1]:
t = zeros.pop()
moves.append([i + 1, t])
moves.append([i + 2, i + 1])
moves.append([t, i + 2])
temp = hd[i + 1]
hd[i + 1] = hd[i]
hd[i] = temp
count += 3
total_count += count
if count == 0:
break
print(total_count)
if total_count > 0:
for move in moves:
print(move[0], move[1])
``` | instruction | 0 | 11,034 | 3 | 22,068 |
No | output | 1 | 11,034 | 3 | 22,069 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem you have to implement an algorithm to defragment your hard disk. The hard disk consists of a sequence of clusters, numbered by integers from 1 to n. The disk has m recorded files, the i-th file occupies clusters with numbers ai, 1, ai, 2, ..., ai, ni. These clusters are not necessarily located consecutively on the disk, but the order in which they are given corresponds to their sequence in the file (cluster ai, 1 contains the first fragment of the i-th file, cluster ai, 2 has the second fragment, etc.). Also the disc must have one or several clusters which are free from files.
You are permitted to perform operations of copying the contents of cluster number i to cluster number j (i and j must be different). Moreover, if the cluster number j used to keep some information, it is lost forever. Clusters are not cleaned, but after the defragmentation is complete, some of them are simply declared unusable (although they may possibly still contain some fragments of files).
Your task is to use a sequence of copy operations to ensure that each file occupies a contiguous area of memory. Each file should occupy a consecutive cluster section, the files must follow one after another from the beginning of the hard disk. After defragmentation all free (unused) clusters should be at the end of the hard disk. After defragmenting files can be placed in an arbitrary order. Clusters of each file should go consecutively from first to last. See explanatory examples in the notes.
Print the sequence of operations leading to the disk defragmentation. Note that you do not have to minimize the number of operations, but it should not exceed 2n.
Input
The first line contains two integers n and m (1 β€ n, m β€ 200) β the number of clusters and the number of files, correspondingly. Next m lines contain descriptions of the files. The first number in the line is ni (ni β₯ 1), the number of clusters occupied by the i-th file. Then follow ni numbers ai, 1, ai, 2, ..., ai, ni (1 β€ ai, j β€ n). It is guaranteed that each cluster number occurs not more than once and <image>, that is, there exists at least one unused cluster. Numbers on each line are separated by spaces.
Output
In the first line print a single integer k (0 β€ k β€ 2n) β the number of operations needed to defragment the disk. Next k lines should contain the operations' descriptions as "i j" (copy the contents of the cluster number i to the cluster number j).
Examples
Input
7 2
2 1 2
3 3 4 5
Output
0
Input
7 2
2 1 3
3 2 4 5
Output
3
2 6
3 2
6 3
Note
Let's say that a disk consists of 8 clusters and contains two files. The first file occupies two clusters and the second file occupies three clusters. Let's look at examples of correct and incorrect positions of files after defragmentation.
<image>
Example 2: each file must occupy a contiguous area of memory.
Example 3: the order of files to each other is not important, at first the second file can be written, and then β the first one.
Example 4: violating the order of file fragments to each other is not allowed.
Example 5: unused clusters should be located at the end, and in this example the unused clusters are 3, 7, 8.
Submitted Solution:
```
"""
Brandt Smith, Peter Haddad and Lemuel Gorion
codeforces.com
Problem 180A
"""
c, f = input().split(' ')
hd = [0] * int(c)
total_count = 0
zeros = []
moves = []
for i in range(int(f)):
file = input().split()
for x in range(1,len(file)):
hd[int(file[x]) - 1] = [i + 1,x]
for x in range(len(hd)):
if hd[x] == 0:
zeros.append(x)
while True:
count = 0
for i in range(len(hd) - 1):
if hd[i] == 0 and hd[i + 1] == 0:
continue
elif hd[i] != 0 and hd[i + 1] == 0:
continue
elif hd[i] == 0 and hd[i + 1] != 0:
moves.append([i + 2, i + 1])
temp = hd[i + 1]
hd[i + 1] = hd[i]
hd[i] = temp
count += 1
elif hd[i][0] > hd[i + 1][0]:
t = zeros.pop()
moves.append([i + 1, t])
moves.append([i + 2, i + 1])
moves.append([t, i + 2])
zeros.append(t)
temp = hd[i + 1]
hd[i + 1] = hd[i]
hd[i] = temp
count += 3
elif hd[i][0] == hd[i + 1][0] and hd[i][1] > hd[i + 1][1]:
t = zeros.pop()
moves.append([i + 1, t])
moves.append([i + 2, i + 1])
moves.append([t, i + 2])
zeros.append(t)
temp = hd[i + 1]
hd[i + 1] = hd[i]
hd[i] = temp
count += 3
total_count += count
if count == 0:
break
print(total_count)
if total_count > 0:
for move in moves:
print(move[0], move[1])
``` | instruction | 0 | 11,035 | 3 | 22,070 |
No | output | 1 | 11,035 | 3 | 22,071 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem you have to implement an algorithm to defragment your hard disk. The hard disk consists of a sequence of clusters, numbered by integers from 1 to n. The disk has m recorded files, the i-th file occupies clusters with numbers ai, 1, ai, 2, ..., ai, ni. These clusters are not necessarily located consecutively on the disk, but the order in which they are given corresponds to their sequence in the file (cluster ai, 1 contains the first fragment of the i-th file, cluster ai, 2 has the second fragment, etc.). Also the disc must have one or several clusters which are free from files.
You are permitted to perform operations of copying the contents of cluster number i to cluster number j (i and j must be different). Moreover, if the cluster number j used to keep some information, it is lost forever. Clusters are not cleaned, but after the defragmentation is complete, some of them are simply declared unusable (although they may possibly still contain some fragments of files).
Your task is to use a sequence of copy operations to ensure that each file occupies a contiguous area of memory. Each file should occupy a consecutive cluster section, the files must follow one after another from the beginning of the hard disk. After defragmentation all free (unused) clusters should be at the end of the hard disk. After defragmenting files can be placed in an arbitrary order. Clusters of each file should go consecutively from first to last. See explanatory examples in the notes.
Print the sequence of operations leading to the disk defragmentation. Note that you do not have to minimize the number of operations, but it should not exceed 2n.
Input
The first line contains two integers n and m (1 β€ n, m β€ 200) β the number of clusters and the number of files, correspondingly. Next m lines contain descriptions of the files. The first number in the line is ni (ni β₯ 1), the number of clusters occupied by the i-th file. Then follow ni numbers ai, 1, ai, 2, ..., ai, ni (1 β€ ai, j β€ n). It is guaranteed that each cluster number occurs not more than once and <image>, that is, there exists at least one unused cluster. Numbers on each line are separated by spaces.
Output
In the first line print a single integer k (0 β€ k β€ 2n) β the number of operations needed to defragment the disk. Next k lines should contain the operations' descriptions as "i j" (copy the contents of the cluster number i to the cluster number j).
Examples
Input
7 2
2 1 2
3 3 4 5
Output
0
Input
7 2
2 1 3
3 2 4 5
Output
3
2 6
3 2
6 3
Note
Let's say that a disk consists of 8 clusters and contains two files. The first file occupies two clusters and the second file occupies three clusters. Let's look at examples of correct and incorrect positions of files after defragmentation.
<image>
Example 2: each file must occupy a contiguous area of memory.
Example 3: the order of files to each other is not important, at first the second file can be written, and then β the first one.
Example 4: violating the order of file fragments to each other is not allowed.
Example 5: unused clusters should be located at the end, and in this example the unused clusters are 3, 7, 8.
Submitted Solution:
```
"""
Brandt Smith, Peter Haddad and Lemuel Gorion
codeforces.com
Problem 180A
"""
c, f = input().split(' ')
hd = [0] * int(c)
total_count = 0
moves = []
for i in range(int(f)):
file = input().split()
for x in range(1,len(file)):
hd[int(file[x]) - 1] = [i + 1,x]
while True:
count = 0
zeros = []
for i in range(len(hd)):
if hd[i] == 0:
zeros.append(i)
print(hd,zeros)
for i in range(len(hd) - 1):
if hd[i] == 0 and hd[i + 1] == 0:
continue
elif hd[i] != 0 and hd[i + 1] == 0:
continue
elif hd[i] == 0 and hd[i + 1] != 0:
if hd[i - 1] != 0 and i - 1 != -1 and (hd[i - 1][0] > hd[i + 1][0] or hd[i - 1][1] > hd[i + 1][1]):
moves.append([i, i + 1])
moves.append([i + 2, i])
temp = hd[i - 1]
hd[i - 1] = hd[i + 1]
hd[i] = temp
hd[i + 1] = 0
count += 2
else:
if hd[i - 1] == 0:
moves.append([i + 2, i])
temp = hd[i + 1]
hd[i + 1] = hd[i - 1]
hd[i - 1] = temp
else:
moves.append([i + 2, i + 1])
temp = hd[i + 1]
hd[i + 1] = hd[i]
hd[i] = temp
count += 1
elif hd[i][0] > hd[i + 1][0]:
t = zeros.pop()
moves.append([i + 1, t])
moves.append([i + 2, i + 1])
moves.append([t, i + 2])
temp = hd[i + 1]
hd[i + 1] = hd[i]
hd[i] = temp
count += 3
elif hd[i][0] == hd[i + 1][0] and hd[i][1] > hd[i + 1][1]:
t = zeros.pop()
moves.append([i + 1, t])
moves.append([i + 2, i + 1])
moves.append([t, i + 2])
temp = hd[i + 1]
hd[i + 1] = hd[i]
hd[i] = temp
count += 3
total_count += count
if count == 0:
break
print(total_count)
if total_count > 0:
for move in moves:
print(move[0], move[1])
``` | instruction | 0 | 11,036 | 3 | 22,072 |
No | output | 1 | 11,036 | 3 | 22,073 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are k sensors located in the rectangular room of size n Γ m meters. The i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle.
Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes.
At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of <image> meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start.
When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.
For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors.
Input
The first line of the input contains three integers n, m and k (2 β€ n, m β€ 100 000, 1 β€ k β€ 100 000) β lengths of the room's walls and the number of sensors.
Each of the following k lines contains two integers xi and yi (1 β€ xi β€ n - 1, 1 β€ yi β€ m - 1) β coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.
Output
Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen.
Examples
Input
3 3 4
1 1
1 2
2 1
2 2
Output
1
-1
-1
2
Input
3 4 6
1 1
2 1
1 2
2 2
1 3
2 3
Output
1
-1
-1
2
5
-1
Input
7 4 5
1 3
2 2
5 1
5 3
4 3
Output
13
2
9
5
-1
Note
In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds.
<image>
In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1).
<image> | instruction | 0 | 11,259 | 3 | 22,518 |
Tags: greedy, hashing, implementation, math, number theory, sortings
Correct Solution:
```
n, m, k = map(int,input().split())
dm, dp = {}, {}
vis = {}
sensors = []
border = set()
for el in [(0, m), (n, 0), (0, 0), (n, m)]:
border.add(el)
for _ in range(k):
x, y = map(int, input().split())
if not (x - y) in dm:
dm[x - y] = []
dm[x - y].append((x, y))
if not (x + y) in dp:
dp[x + y] = []
dp[x + y].append((x, y))
vis[(x, y)] = -1
sensors.append((x,y))
x, y = 0, 0
time = 0
move = (1,1)
while True:
if move == (1,1):
v = min(n - x, m - y)
nxt = (x + v, y + v)
if nxt[0] == n:
move = (-1, 1)
else:
move = (1, -1)
if (x - y) in dm:
for sensor in dm[x - y]:
if vis[sensor] == -1:
#print('doing', sensor)
vis[sensor] = time + sensor[0] - x
time += v
elif move == (-1,-1):
v = min(x, y)
nxt = (x - v, y - v)
if nxt[0] == 0:
move = (1, -1)
else:
move = (-1, 1)
if (x - y) in dm:
for sensor in dm[x - y]:
if vis[sensor] == -1:
#print('doing', sensor)
vis[sensor] = time + x - sensor[0]
time += v
elif move == (-1,1):
v = min(x, m - y)
nxt = (x - v, y + v)
if nxt[0] == 0:
move = (1, 1)
else:
move = (-1, -1)
if (x + y) in dp:
for sensor in dp[x + y]:
if vis[sensor] == -1:
#print('doing', sensor)
vis[sensor] = time + x - sensor[0]
time += v
else:
v = min(n - x, y)
nxt = (x + v, y - v)
if nxt[0] == n:
move = (-1, -1)
else:
move = (1, 1)
if (x + y) in dp:
for sensor in dp[x + y]:
if vis[sensor] == -1:
#print('doing', sensor)
vis[sensor] = time + sensor[0] - x
time += v
if nxt in border:
break
else:
border.add(nxt)
x, y = nxt
#print('bum', x, y)
for i in range(k):
#print(sensors[i])
print(vis[sensors[i]])
# Made By Mostafa_Khaled
``` | output | 1 | 11,259 | 3 | 22,519 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are k sensors located in the rectangular room of size n Γ m meters. The i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle.
Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes.
At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of <image> meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start.
When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.
For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors.
Input
The first line of the input contains three integers n, m and k (2 β€ n, m β€ 100 000, 1 β€ k β€ 100 000) β lengths of the room's walls and the number of sensors.
Each of the following k lines contains two integers xi and yi (1 β€ xi β€ n - 1, 1 β€ yi β€ m - 1) β coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.
Output
Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen.
Examples
Input
3 3 4
1 1
1 2
2 1
2 2
Output
1
-1
-1
2
Input
3 4 6
1 1
2 1
1 2
2 2
1 3
2 3
Output
1
-1
-1
2
5
-1
Input
7 4 5
1 3
2 2
5 1
5 3
4 3
Output
13
2
9
5
-1
Note
In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds.
<image>
In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1).
<image> | instruction | 0 | 11,260 | 3 | 22,520 |
Tags: greedy, hashing, implementation, math, number theory, sortings
Correct Solution:
```
f = lambda: map(int, input().split())
n, m, k = f()
w, h = 2 * n, 2 * m
s = [[] for i in range(w + h)]
p = [-1] * k
for i in range(k):
x, y = f()
if x - y & 1: continue
for a in (x, w - x):
for b in (y, h - y):
s[b - a].append((a, i))
a = b = t = 0
while 1:
for x, i in s[b - a]:
if p[i] < 0: p[i] = t + x - a
d = min(w - a, h - b)
t += d
a = (a + d) % w
b = (b + d) % h
if a % n == b % m: break
for q in p: print(q)
``` | output | 1 | 11,260 | 3 | 22,521 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are k sensors located in the rectangular room of size n Γ m meters. The i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle.
Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes.
At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of <image> meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start.
When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.
For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors.
Input
The first line of the input contains three integers n, m and k (2 β€ n, m β€ 100 000, 1 β€ k β€ 100 000) β lengths of the room's walls and the number of sensors.
Each of the following k lines contains two integers xi and yi (1 β€ xi β€ n - 1, 1 β€ yi β€ m - 1) β coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.
Output
Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen.
Examples
Input
3 3 4
1 1
1 2
2 1
2 2
Output
1
-1
-1
2
Input
3 4 6
1 1
2 1
1 2
2 2
1 3
2 3
Output
1
-1
-1
2
5
-1
Input
7 4 5
1 3
2 2
5 1
5 3
4 3
Output
13
2
9
5
-1
Note
In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds.
<image>
In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1).
<image> | instruction | 0 | 11,261 | 3 | 22,522 |
Tags: greedy, hashing, implementation, math, number theory, sortings
Correct Solution:
```
__author__ = 'Think'
n, m, k=[int(i) for i in input().split()]
n=2*n
m=2*m
a, b=sorted((m, n))
tracker=[b]
while a>0:
b=(b%a)
tracker.append(a)
a, b=(b, a)
g=b
prod=(m*n)//g
if m!=n:
if len(tracker)>=3:
pair=(1, -(tracker[-3]//tracker[-2]))
for i in range(len(tracker)-4, -1, -1):
new=-(tracker[i]//tracker[i+1])
pair=(pair[1], pair[0]+pair[1]*new)
if sorted((m, n))[0]==n:
pair=(pair[1], pair[0])
a, b=pair
else:
if m>n:
a=1
b=0
else:
a=0
b=1
for i in range(k):
x, y=[int(i) for i in input().split()]
if (x-y)%g != 0 and (x+y)%g != 0:
print(-1)
continue
else:
shortlist=[]
for nx in [x, -x]:
if ((nx-y)%g) == 0:
new=(nx+a*n*((y-nx)//g))%prod
shortlist.append(new)
shortlist.append(prod-new)
if len(shortlist)>0:
print(min(shortlist))
else:
print(-1)
else:
for i in range(k):
x, y=[int(i) for i in input().split()]
if x!=y:
print(-1)
else:
print(x)
``` | output | 1 | 11,261 | 3 | 22,523 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are k sensors located in the rectangular room of size n Γ m meters. The i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle.
Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes.
At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of <image> meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start.
When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.
For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors.
Input
The first line of the input contains three integers n, m and k (2 β€ n, m β€ 100 000, 1 β€ k β€ 100 000) β lengths of the room's walls and the number of sensors.
Each of the following k lines contains two integers xi and yi (1 β€ xi β€ n - 1, 1 β€ yi β€ m - 1) β coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.
Output
Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen.
Examples
Input
3 3 4
1 1
1 2
2 1
2 2
Output
1
-1
-1
2
Input
3 4 6
1 1
2 1
1 2
2 2
1 3
2 3
Output
1
-1
-1
2
5
-1
Input
7 4 5
1 3
2 2
5 1
5 3
4 3
Output
13
2
9
5
-1
Note
In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds.
<image>
In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1).
<image> | instruction | 0 | 11,262 | 3 | 22,524 |
Tags: greedy, hashing, implementation, math, number theory, sortings
Correct Solution:
```
# [https://codeforces.com/contest/724/submission/21299971]
(n, m, k) = map(int, input().split())
n = 2*n
m = 2*m
a = min(m, n)
b = max(m, n)
tracker = [b]
while a>0:
b = b % a
tracker.append(a)
(a, b) = (b, a)
g = b
prod=(m*n)//g
if m!=n:
if len(tracker)>=3:
pair=(1, -(tracker[-3]//tracker[-2]))
for i in range(len(tracker)-4, -1, -1):
new=-(tracker[i]//tracker[i+1])
pair=(pair[1], pair[0]+pair[1]*new)
if min(m, n) == n:
pair=(pair[1], pair[0])
(a, b) = pair
else:
if m>n:
a = 1
b = 0
else:
a = 0
b = 1
for i in range(k):
(x, y) = [int(i) for i in input().split()]
if (x-y)%g != 0 and (x+y)%g != 0:
print(-1)
continue
else:
shortlist = []
for nx in (x, -x):
if ((nx-y)%g) == 0:
new=(nx+a*n*((y-nx)//g))%prod
shortlist.append(new)
shortlist.append(prod-new)
if len(shortlist)>0:
print(min(shortlist))
else:
print(-1)
else:
for i in range(k):
(x, y) = [int(i) for i in input().split()]
if x!=y:
print(-1)
else:
print(x)
``` | output | 1 | 11,262 | 3 | 22,525 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are k sensors located in the rectangular room of size n Γ m meters. The i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle.
Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes.
At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of <image> meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start.
When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.
For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors.
Input
The first line of the input contains three integers n, m and k (2 β€ n, m β€ 100 000, 1 β€ k β€ 100 000) β lengths of the room's walls and the number of sensors.
Each of the following k lines contains two integers xi and yi (1 β€ xi β€ n - 1, 1 β€ yi β€ m - 1) β coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.
Output
Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen.
Examples
Input
3 3 4
1 1
1 2
2 1
2 2
Output
1
-1
-1
2
Input
3 4 6
1 1
2 1
1 2
2 2
1 3
2 3
Output
1
-1
-1
2
5
-1
Input
7 4 5
1 3
2 2
5 1
5 3
4 3
Output
13
2
9
5
-1
Note
In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds.
<image>
In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1).
<image> | instruction | 0 | 11,263 | 3 | 22,526 |
Tags: greedy, hashing, implementation, math, number theory, sortings
Correct Solution:
```
import sys
from fractions import gcd
import math
def euclid_algorithm(a, b):
t1, t2 = abs(a), abs(b)
#saving equalities:
#t1 == x1 * a + y1 * b,
#t2 == x2 * a + y2 * b.
x1, y1, x2, y2 = int(math.copysign(1, a)), 0, 0, int(math.copysign(1, b))
if t1 < t2:
t1, t2 = t2, t1
x1, y1, x2, y2 = x2, y2, x1, y1
while t2 > 0:
k = int(t1 // t2)
t1, t2 = t2, t1 % t2
#t1 - k * t2 == (x1 - k * x2) * a + (y1 - k * y2) * b
x1, y1, x2, y2 = x2, y2, x1 - k * x2, y1 - k * y2
return t1, x1, y1
def opposite_element(x, p):
gcd, k, l = euclid_algorithm(x, p)
if gcd != 1:
return -1
return k % p
n, m, k = [int(x) for x in input().split()]
g = gcd(n, m)
end = n * m // g
n1, m1 = n//g, m//g
l1 = opposite_element(n1, m1)
def solve(x, y):
if x%(2*g) != y%(2*g):
return float('inf')
x1, y1 = x//(2*g), y//(2*g)
t = x1%n1 + n1*((y1-x1%n1)*l1%m1)
return x%(2*g) + t*2*g
def check(x,y):
res = min(solve(x,y), solve(-x,y), solve(x,-y), solve(-x,-y))
return -1 if res >= end else res
for line in sys.stdin:
x, y = [int(x) for x in line.split()]
sys.stdout.write(str(check(x,y)) + '\n')
``` | output | 1 | 11,263 | 3 | 22,527 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are k sensors located in the rectangular room of size n Γ m meters. The i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle.
Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes.
At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of <image> meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start.
When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.
For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors.
Input
The first line of the input contains three integers n, m and k (2 β€ n, m β€ 100 000, 1 β€ k β€ 100 000) β lengths of the room's walls and the number of sensors.
Each of the following k lines contains two integers xi and yi (1 β€ xi β€ n - 1, 1 β€ yi β€ m - 1) β coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.
Output
Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen.
Examples
Input
3 3 4
1 1
1 2
2 1
2 2
Output
1
-1
-1
2
Input
3 4 6
1 1
2 1
1 2
2 2
1 3
2 3
Output
1
-1
-1
2
5
-1
Input
7 4 5
1 3
2 2
5 1
5 3
4 3
Output
13
2
9
5
-1
Note
In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds.
<image>
In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1).
<image> | instruction | 0 | 11,264 | 3 | 22,528 |
Tags: greedy, hashing, implementation, math, number theory, sortings
Correct Solution:
```
"""
NTC here
"""
import sys
inp = sys.stdin.readline
def input(): return inp().strip()
# flush= sys.stdout.flush
# import threading
# sys.setrecursionlimit(10**6)
# threading.stack_size(2**26)
def iin(): return int(input())
def lin(): return list(map(int, input().split()))
# range = xrange
# input = raw_input
def main():
T = 1
def fn(ch, x, y):
return x*ch[0] + y*ch[1] + ch[2]==0
def fnx(ch, y):
return (-ch[1]*y-ch[2])/ch[0]
def fny(ch, x):
return (-ch[0]*x-ch[2])/ch[1]
def int_val(x):
return int(x*10000)==int(x)*10000
while T:
T-=1
n, m, k = lin()
h = m
w = n
end = {(0, h), (0, 0), (w, h), (w, 0)}
sensors = [lin() for _ in range(k)]
# we will track ray
# as line as set of ax+by+c=0
lines = {(1, -1, 0):[ (0, 0), 0]}
ch = [1, -1, 0]
ch1 = 1
ch2 = 0
st = [0, 0]
while 1:
# print('EQ-', ch)
# print(st)
dn = 0
# y = h
y1 = h
x1 = fnx(ch, y1)
# print("D",[x1, y1])
if int_val(x1) and 0<=int(x1)<=w and [int(x1), int(y1)]!=st:
x1 = int(x1)
if x1 == w:
break
dn = 1
# y = 0
if dn==0:
y1 = 0
x1 = fnx(ch, 0)
# print("A",[x1, y1])
if int_val(x1) and 0<=int(x1)<=w and [int(x1), int(y1)] != st:
x1 = int(x1)
if x1 == 0:
break
dn = 1
if dn==0:
# x = 0
x1 = 0
y1 = fny(ch, x1)
# print("B",[x1, y1])
if int_val(y1) and 0<=int(y1)<=h and [int(x1), int(y1)] != st:
y1 = int(y1)
if y1 == 0:
break
dn = 1
if dn==0:
# x = w
x1 = w
y1 = fny(ch, x1)
# print("C",[x1, y1])
if int_val(y1) and 0<=int(y1)<=h and [int(x1), int(y1)] != st:
y1 = int(y1)
if y1 == h:
break
dn = 1
if dn:
# print(x1, y1)
ch2 += abs(st[0]-x1)
ch1 = -1 if ch1==1 else 1
ch = [ch1, -1, -ch1*x1+y1]
if tuple(ch) in lines:continue
lines[tuple(ch)] = [[x1, y1], ch2]
if (x1, y1) in end:break
st = [x1, y1]
else:
break
# print(lines)
for i, j in sensors:
ch1, ch2 = (1, -1, -i+j), (-1, -1, i+j)
# print((i, j), ch1, ch2)
ans = -1
if ch1 in lines:
p, c1 = lines[ch1]
ans = abs(p[0]-i)+c1
if ch2 in lines:
p, c1 = lines[ch2]
ans = abs(p[0]-i)+c1 if ans==-1 else min(abs(p[0]-i)+c1, ans)
print(ans)
main()
# threading.Thread(target=main).start()
``` | output | 1 | 11,264 | 3 | 22,529 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are k sensors located in the rectangular room of size n Γ m meters. The i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle.
Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes.
At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of <image> meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start.
When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.
For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors.
Input
The first line of the input contains three integers n, m and k (2 β€ n, m β€ 100 000, 1 β€ k β€ 100 000) β lengths of the room's walls and the number of sensors.
Each of the following k lines contains two integers xi and yi (1 β€ xi β€ n - 1, 1 β€ yi β€ m - 1) β coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.
Output
Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen.
Examples
Input
3 3 4
1 1
1 2
2 1
2 2
Output
1
-1
-1
2
Input
3 4 6
1 1
2 1
1 2
2 2
1 3
2 3
Output
1
-1
-1
2
5
-1
Input
7 4 5
1 3
2 2
5 1
5 3
4 3
Output
13
2
9
5
-1
Note
In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds.
<image>
In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1).
<image> | instruction | 0 | 11,265 | 3 | 22,530 |
Tags: greedy, hashing, implementation, math, number theory, sortings
Correct Solution:
```
n, m, k = map(int,input().split())
dm, dp = {}, {}
vis = {}
sensors = []
border = set()
for el in [(0, m), (n, 0), (0, 0), (n, m)]:
border.add(el)
for _ in range(k):
x, y = map(int, input().split())
if not (x - y) in dm:
dm[x - y] = []
dm[x - y].append((x, y))
if not (x + y) in dp:
dp[x + y] = []
dp[x + y].append((x, y))
vis[(x, y)] = -1
sensors.append((x,y))
x, y = 0, 0
time = 0
move = (1,1)
while True:
if move == (1,1):
v = min(n - x, m - y)
nxt = (x + v, y + v)
if nxt[0] == n:
move = (-1, 1)
else:
move = (1, -1)
if (x - y) in dm:
for sensor in dm[x - y]:
if vis[sensor] == -1:
#print('doing', sensor)
vis[sensor] = time + sensor[0] - x
time += v
elif move == (-1,-1):
v = min(x, y)
nxt = (x - v, y - v)
if nxt[0] == 0:
move = (1, -1)
else:
move = (-1, 1)
if (x - y) in dm:
for sensor in dm[x - y]:
if vis[sensor] == -1:
#print('doing', sensor)
vis[sensor] = time + x - sensor[0]
time += v
elif move == (-1,1):
v = min(x, m - y)
nxt = (x - v, y + v)
if nxt[0] == 0:
move = (1, 1)
else:
move = (-1, -1)
if (x + y) in dp:
for sensor in dp[x + y]:
if vis[sensor] == -1:
#print('doing', sensor)
vis[sensor] = time + x - sensor[0]
time += v
else:
v = min(n - x, y)
nxt = (x + v, y - v)
if nxt[0] == n:
move = (-1, -1)
else:
move = (1, 1)
if (x + y) in dp:
for sensor in dp[x + y]:
if vis[sensor] == -1:
#print('doing', sensor)
vis[sensor] = time + sensor[0] - x
time += v
if nxt in border:
break
else:
border.add(nxt)
x, y = nxt
#print('bum', x, y)
for i in range(k):
#print(sensors[i])
print(vis[sensors[i]])
``` | output | 1 | 11,265 | 3 | 22,531 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are k sensors located in the rectangular room of size n Γ m meters. The i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle.
Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes.
At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of <image> meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start.
When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.
For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors.
Input
The first line of the input contains three integers n, m and k (2 β€ n, m β€ 100 000, 1 β€ k β€ 100 000) β lengths of the room's walls and the number of sensors.
Each of the following k lines contains two integers xi and yi (1 β€ xi β€ n - 1, 1 β€ yi β€ m - 1) β coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.
Output
Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen.
Examples
Input
3 3 4
1 1
1 2
2 1
2 2
Output
1
-1
-1
2
Input
3 4 6
1 1
2 1
1 2
2 2
1 3
2 3
Output
1
-1
-1
2
5
-1
Input
7 4 5
1 3
2 2
5 1
5 3
4 3
Output
13
2
9
5
-1
Note
In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds.
<image>
In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1).
<image> | instruction | 0 | 11,266 | 3 | 22,532 |
Tags: greedy, hashing, implementation, math, number theory, sortings
Correct Solution:
```
from sys import *
f = lambda: map(int, stdin.readline().split())
n, m, k = f()
w, h = 2 * n, 2 * m
inf = 1e11
s = [inf] * (w + h)
a = b = t = 0
while 1:
if s[b - a] == inf: s[b - a] = t - a
d = min(w - a, h - b)
t += d
a = (a + d) % w
b = (b + d) % h
if a % n == b % m: break
for i in range(k):
x, y = f()
q = min(s[b - a] + a for a in (x, w - x) for b in (y, h - y))
print(q if q < inf else -1)
``` | output | 1 | 11,266 | 3 | 22,533 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are k sensors located in the rectangular room of size n Γ m meters. The i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle.
Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes.
At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of <image> meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start.
When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.
For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors.
Input
The first line of the input contains three integers n, m and k (2 β€ n, m β€ 100 000, 1 β€ k β€ 100 000) β lengths of the room's walls and the number of sensors.
Each of the following k lines contains two integers xi and yi (1 β€ xi β€ n - 1, 1 β€ yi β€ m - 1) β coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.
Output
Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen.
Examples
Input
3 3 4
1 1
1 2
2 1
2 2
Output
1
-1
-1
2
Input
3 4 6
1 1
2 1
1 2
2 2
1 3
2 3
Output
1
-1
-1
2
5
-1
Input
7 4 5
1 3
2 2
5 1
5 3
4 3
Output
13
2
9
5
-1
Note
In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds.
<image>
In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1).
<image>
Submitted Solution:
```
def main():
nx, my, k = list(map(int, input().strip().split()))
my *= 2
nx *= 2
diags = [[] for i in range(nx + my)]
answers = [-1] * k
for i in range(k):
x,y = list(map(int, input().strip().split()))
def add(x, y, i):
diag_index = nx + (y - x)
diags[diag_index].append( (x,y,i) )
add(x, y, i)
add(x, my - y, i)
add(nx - x, y, i)
add(nx - x, my - y, i)
cur_t = 0
cur_x = 0
cur_y = 0
while True:
diag_index = nx + (cur_y - cur_x)
for x,y,i in diags[diag_index]:
if answers[i] == -1:
t = cur_t + (x - cur_x)
assert(x - cur_x == y - cur_y)
answers[i] = t
diff_x = nx - cur_x
diff_y = my - cur_y
diff = min(diff_x, diff_y)
cur_t += diff
cur_x = (cur_x + diff) % nx
cur_y = (cur_y + diff) % my
if (cur_x % (nx // 2)) + (cur_y % (my // 2)) == 0:
break
for a in answers:
print(a)
main()
``` | instruction | 0 | 11,267 | 3 | 22,534 |
Yes | output | 1 | 11,267 | 3 | 22,535 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are k sensors located in the rectangular room of size n Γ m meters. The i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle.
Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes.
At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of <image> meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start.
When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.
For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors.
Input
The first line of the input contains three integers n, m and k (2 β€ n, m β€ 100 000, 1 β€ k β€ 100 000) β lengths of the room's walls and the number of sensors.
Each of the following k lines contains two integers xi and yi (1 β€ xi β€ n - 1, 1 β€ yi β€ m - 1) β coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.
Output
Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen.
Examples
Input
3 3 4
1 1
1 2
2 1
2 2
Output
1
-1
-1
2
Input
3 4 6
1 1
2 1
1 2
2 2
1 3
2 3
Output
1
-1
-1
2
5
-1
Input
7 4 5
1 3
2 2
5 1
5 3
4 3
Output
13
2
9
5
-1
Note
In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds.
<image>
In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1).
<image>
Submitted Solution:
```
import sys
input = sys.stdin.readline
from collections import defaultdict
def inside(n, m, x, y):
return 0 <= x <= n and 0 <= y <= m
def corner(n, m, x, y):
return x in [0, n] and y in [0, m]
def next(n, m, x, y, t, a, b):
for cx in [0, n]:
cy = a * cx + b
if inside(n, m, cx, cy) and (cx, cy) != (x, y):
nx, ny = cx, cy
for cy in [0, m]:
cx = (cy - b) // a
if inside(n, m, cx, cy) and (cx, cy) != (x, y):
nx, ny = cx, cy
nt = t + abs(nx - x)
na = -a
nb = ny - na * nx
if corner(n, m, nx, ny):
return None
return nx, ny, nt, na, nb
n, m, k = map(int, input().split())
d = defaultdict(list)
for i in range(k):
x, y = map(int, input().split())
for a in [-1, 1]:
b = y - a * x
d[(a, b)].append((x, y, i))
ans = [-1] * k
ray = (0, 0, 0, 1, 0) #x, y, t, a, b
visit = set()
while ray:
x, y, t, a, b = ray
if (a, b) in visit:
break
visit.add((a, b))
for sensor in d[(a, b)]:
sx, sy, i = sensor
if ans[i] == -1:
ans[i] = t + abs(x - sx)
ray = next(n, m, x, y, t, a, b)
for x in ans:
print(x)
``` | instruction | 0 | 11,268 | 3 | 22,536 |
Yes | output | 1 | 11,268 | 3 | 22,537 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are k sensors located in the rectangular room of size n Γ m meters. The i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle.
Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes.
At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of <image> meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start.
When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.
For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors.
Input
The first line of the input contains three integers n, m and k (2 β€ n, m β€ 100 000, 1 β€ k β€ 100 000) β lengths of the room's walls and the number of sensors.
Each of the following k lines contains two integers xi and yi (1 β€ xi β€ n - 1, 1 β€ yi β€ m - 1) β coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.
Output
Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen.
Examples
Input
3 3 4
1 1
1 2
2 1
2 2
Output
1
-1
-1
2
Input
3 4 6
1 1
2 1
1 2
2 2
1 3
2 3
Output
1
-1
-1
2
5
-1
Input
7 4 5
1 3
2 2
5 1
5 3
4 3
Output
13
2
9
5
-1
Note
In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds.
<image>
In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1).
<image>
Submitted Solution:
```
import sys
from fractions import gcd
import math
def euclid_algorithm(a, b):
t1, t2 = abs(a), abs(b)
#saving equalities:
#t1 == x1 * a + y1 * b,
#t2 == x2 * a + y2 * b.
x1, y1, x2, y2 = int(math.copysign(1, a)), 0, 0, int(math.copysign(1, b))
if t1 < t2:
t1, t2 = t2, t1
x1, y1, x2, y2 = x2, y2, x1, y1
while t2 > 0:
k = int(t1 // t2)
t1, t2 = t2, t1 % t2
#t1 - k * t2 == (x1 - k * x2) * a + (y1 - k * y2) * b
x1, y1, x2, y2 = x2, y2, x1 - k * x2, y1 - k * y2
return t1, x1, y1
def opposite_element(x, p):
gcd, k, l = euclid_algorithm(x, p)
if gcd != 1:
return -1
return k % p
n, m, k = [int(x) for x in input().split()]
g = gcd(n, m)
end = n * m // g
n1, m1 = n//g, m//g
l1 = opposite_element(n, m)
def solve(x, y):
if x%(2*g) != y%(2*g):
return float('inf')
x1, y1 = x//(2*g), y//(2*g)
t = x1%n1 + n1*((y1-x1%n1)*l1%m1)
return x%(2*g) + t*2*g
def check(x,y):
res = min(solve(x,y), solve(-x,y), solve(x,-y), solve(-x,-y))
return -1 if res == float('inf') else res
for line in sys.stdin:
x, y = [int(x) for x in line.split()]
sys.stdout.write(str(check(x,y)) + '\n')
``` | instruction | 0 | 11,269 | 3 | 22,538 |
No | output | 1 | 11,269 | 3 | 22,539 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are k sensors located in the rectangular room of size n Γ m meters. The i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle.
Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes.
At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of <image> meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start.
When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.
For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors.
Input
The first line of the input contains three integers n, m and k (2 β€ n, m β€ 100 000, 1 β€ k β€ 100 000) β lengths of the room's walls and the number of sensors.
Each of the following k lines contains two integers xi and yi (1 β€ xi β€ n - 1, 1 β€ yi β€ m - 1) β coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.
Output
Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen.
Examples
Input
3 3 4
1 1
1 2
2 1
2 2
Output
1
-1
-1
2
Input
3 4 6
1 1
2 1
1 2
2 2
1 3
2 3
Output
1
-1
-1
2
5
-1
Input
7 4 5
1 3
2 2
5 1
5 3
4 3
Output
13
2
9
5
-1
Note
In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds.
<image>
In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1).
<image>
Submitted Solution:
```
__author__ = 'Think'
n, m, k=[int(i) for i in input().split()]
n=2*n
m=2*m
a, b=sorted((m, n))
tracker=[b]
while a>0:
b=(b%a)
tracker.append(a)
a, b=(b, a)
g=b
prod=(m*n)//g
if len(tracker)>=3:
pair=(1, -(tracker[-3]//tracker[-2]))
for i in range(len(tracker)-4, -1, -1):
new=-(tracker[i]//tracker[i+1])
pair=(pair[1], pair[0]+pair[1]*new)
if sorted((m, n))[0]==n:
pair=(pair[1], pair[0])
a, b=pair
else:
a=1
b=0
for i in range(k):
x, y=[int(i) for i in input().split()]
if (x-y)%g != 0:
print(-1)
continue
else:
shortlist=[]
for nx in [x, -x]:
for ny in [y, -y]:
shortlist.append((nx+a*n*((ny-nx)//g))%prod)
print(min(shortlist))
``` | instruction | 0 | 11,270 | 3 | 22,540 |
No | output | 1 | 11,270 | 3 | 22,541 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are k sensors located in the rectangular room of size n Γ m meters. The i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle.
Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes.
At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of <image> meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start.
When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.
For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors.
Input
The first line of the input contains three integers n, m and k (2 β€ n, m β€ 100 000, 1 β€ k β€ 100 000) β lengths of the room's walls and the number of sensors.
Each of the following k lines contains two integers xi and yi (1 β€ xi β€ n - 1, 1 β€ yi β€ m - 1) β coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.
Output
Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen.
Examples
Input
3 3 4
1 1
1 2
2 1
2 2
Output
1
-1
-1
2
Input
3 4 6
1 1
2 1
1 2
2 2
1 3
2 3
Output
1
-1
-1
2
5
-1
Input
7 4 5
1 3
2 2
5 1
5 3
4 3
Output
13
2
9
5
-1
Note
In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds.
<image>
In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1).
<image>
Submitted Solution:
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def sum():
global x,y,ux,uy
x+=ux
y+=uy
def dvig():
global x,y,ux,uy,tmp
if (x == n and y == m) or (x == 0 and y == 0) or (x == 0 and y == m) or (x == n and y == 0):
tmp = False
ux,uy=0,0
elif x==n or x==0:
ux=-ux
elif y==m or y==0:
uy=-uy
sum()
n,m,k=map(int,input().split())
d={}
dd={}
for i in range(k):
xd=list(map(int,input().split()))
d[i]=xd
f={}
for i in range(k):
f[d[i][0],d[i][1]]=-1
t,x,y,ux,uy=1,1,1,1,1
tmp=True
while tmp==True:
if [x,y] in d.values() and f[x,y]==-1:
f[x,y]=t
dvig()
t+=1
if tmp==False:
break
print(f)
for i in range(k):
x,y=d[i][0],d[i][1]
print(f[x,y])
``` | instruction | 0 | 11,271 | 3 | 22,542 |
No | output | 1 | 11,271 | 3 | 22,543 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are k sensors located in the rectangular room of size n Γ m meters. The i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle.
Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes.
At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of <image> meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start.
When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.
For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors.
Input
The first line of the input contains three integers n, m and k (2 β€ n, m β€ 100 000, 1 β€ k β€ 100 000) β lengths of the room's walls and the number of sensors.
Each of the following k lines contains two integers xi and yi (1 β€ xi β€ n - 1, 1 β€ yi β€ m - 1) β coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.
Output
Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen.
Examples
Input
3 3 4
1 1
1 2
2 1
2 2
Output
1
-1
-1
2
Input
3 4 6
1 1
2 1
1 2
2 2
1 3
2 3
Output
1
-1
-1
2
5
-1
Input
7 4 5
1 3
2 2
5 1
5 3
4 3
Output
13
2
9
5
-1
Note
In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds.
<image>
In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1).
<image>
Submitted Solution:
```
print("Ray Tracing program")
print("first is width, second is hight ,third number of point")
s=input() #take firs line of input
s=s.split()
m,n,k=int(s[0])+1,int(s[1])+1,int(s[2]) #m=width , n=hight , k=inputs
arr=[]
# initialize the width and hight of the array
for i in range(n):
arr.append([0 for j in range(m)])
#taking inputs save it in array named point
point=[] #this array will hold the time the ray taked to reach point
for i in range(k):
s=input()
s=s.split()
point.append([int(s[0]),int(s[1]),-1])
#mark points in the array
for i in range(len(point)):
arr[point[i][1]][point[i][0]]=-1
r,c,x,y,count=1,1,0,0,0
while not(y==n-1 and x==m-1 or y==0 and x==m-1 or y==n-1 and x==0):
#test if the point collision with a wall ?
if (y==n-1 or y==0):
if count != 0:
r*=-1
#test if the point collision with a wall ?
if (x==m-1 or x==0) :
if count != 0:
c*=-1
x+=c
y+=r
count+=1 #time
#check if ray pass through a point ?
if arr[y][x]==-1:
arr[y][x]=0
for entry in point:
if x==entry[0] and y==entry[1]:
entry[2]=count
#output
for entry in point:
print(entry[2])
``` | instruction | 0 | 11,272 | 3 | 22,544 |
No | output | 1 | 11,272 | 3 | 22,545 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Igor the analyst fell asleep on the work and had a strange dream. In the dream his desk was crowded with computer mice, so he bought a mousetrap to catch them.
The desk can be considered as an infinite plane, then the mousetrap is a rectangle which sides are parallel to the axes, and which opposite sides are located in points (x1, y1) and (x2, y2).
Igor wants to catch all mice. Igor has analysed their behavior and discovered that each mouse is moving along a straight line with constant speed, the speed of the i-th mouse is equal to (vix, viy), that means that the x coordinate of the mouse increases by vix units per second, while the y coordinates increases by viy units. The mousetrap is open initially so that the mice are able to move freely on the desk. Igor can close the mousetrap at any moment catching all the mice that are strictly inside the mousetrap.
Igor works a lot, so he is busy in the dream as well, and he asks you to write a program that by given mousetrap's coordinates, the initial coordinates of the mice and their speeds determines the earliest time moment in which he is able to catch all the mice. Please note that Igor can close the mousetrap only once.
Input
The first line contains single integer n (1 β€ n β€ 100 000) β the number of computer mice on the desk.
The second line contains four integers x1, y1, x2 and y2 (0 β€ x1 β€ x2 β€ 100 000), (0 β€ y1 β€ y2 β€ 100 000) β the coordinates of the opposite corners of the mousetrap.
The next n lines contain the information about mice.
The i-th of these lines contains four integers rix, riy, vix and viy, (0 β€ rix, riy β€ 100 000, - 100 000 β€ vix, viy β€ 100 000), where (rix, riy) is the initial position of the mouse, and (vix, viy) is its speed.
Output
In the only line print minimum possible non-negative number t such that if Igor closes the mousetrap at t seconds from the beginning, then all the mice are strictly inside the mousetrap. If there is no such t, print -1.
Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if <image>.
Examples
Input
4
7 7 9 8
3 5 7 5
7 5 2 4
3 3 7 8
6 6 3 2
Output
0.57142857142857139685
Input
4
7 7 9 8
0 3 -5 4
5 0 5 4
9 9 -1 -6
10 5 -7 -10
Output
-1
Note
Here is a picture of the first sample
Points A, B, C, D - start mice positions, segments are their paths.
<image>
Then, at first time when all mice will be in rectangle it will be looks like this:
<image>
Here is a picture of the second sample
<image>
Points A, D, B will never enter rectangle. | instruction | 0 | 11,298 | 3 | 22,596 |
Tags: geometry, implementation, math, sortings
Correct Solution:
```
rd = lambda: map(int, input().split())
n = int(input())
x1, y1, x2, y2 = rd()
l = []
r = []
m = []
for i in range(n):
t = []
rx, ry, vx, vy = rd()
m.append([rx, ry, vx, vy])
if x1 <= rx <= x2 and y1 <= ry <= y2:
t.append(0)
if vx == 0 and vy == 0:
t.append(0x3f3f3f3f3f3f3f3f)
if vx:
t1 = (x1 - rx) / vx
if t1 >= 0:
if y1 <= ry + t1 * vy <= y2:
t.append(t1)
t1 = (x2 - rx) / vx
if t1 >= 0:
if y1 <= ry + t1 * vy <= y2:
t.append(t1)
if vy:
t1 = (y1 - ry) / vy
if t1 >= 0:
if x1 <= rx + t1 * vx <= x2:
t.append(t1)
t1 = (y2 - ry) / vy
if t1 >= 0:
if x1 <= rx + t1 * vx <= x2:
t.append(t1)
if len(t) < 2:
print(-1)
exit()
t.sort()
l.append(t[0])
r.append(t[-1])
l.sort()
r.sort()
if l[-1] > r[0]:
print(-1)
else:
p = (l[-1] + r[0]) / 2
if not all(x1 < rx + p * vx < x2 and y1 < ry + p * vy < y2 for rx, ry, vx, vy in m):
print(-1)
else:
print(l[-1])
``` | output | 1 | 11,298 | 3 | 22,597 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Igor the analyst fell asleep on the work and had a strange dream. In the dream his desk was crowded with computer mice, so he bought a mousetrap to catch them.
The desk can be considered as an infinite plane, then the mousetrap is a rectangle which sides are parallel to the axes, and which opposite sides are located in points (x1, y1) and (x2, y2).
Igor wants to catch all mice. Igor has analysed their behavior and discovered that each mouse is moving along a straight line with constant speed, the speed of the i-th mouse is equal to (vix, viy), that means that the x coordinate of the mouse increases by vix units per second, while the y coordinates increases by viy units. The mousetrap is open initially so that the mice are able to move freely on the desk. Igor can close the mousetrap at any moment catching all the mice that are strictly inside the mousetrap.
Igor works a lot, so he is busy in the dream as well, and he asks you to write a program that by given mousetrap's coordinates, the initial coordinates of the mice and their speeds determines the earliest time moment in which he is able to catch all the mice. Please note that Igor can close the mousetrap only once.
Input
The first line contains single integer n (1 β€ n β€ 100 000) β the number of computer mice on the desk.
The second line contains four integers x1, y1, x2 and y2 (0 β€ x1 β€ x2 β€ 100 000), (0 β€ y1 β€ y2 β€ 100 000) β the coordinates of the opposite corners of the mousetrap.
The next n lines contain the information about mice.
The i-th of these lines contains four integers rix, riy, vix and viy, (0 β€ rix, riy β€ 100 000, - 100 000 β€ vix, viy β€ 100 000), where (rix, riy) is the initial position of the mouse, and (vix, viy) is its speed.
Output
In the only line print minimum possible non-negative number t such that if Igor closes the mousetrap at t seconds from the beginning, then all the mice are strictly inside the mousetrap. If there is no such t, print -1.
Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if <image>.
Examples
Input
4
7 7 9 8
3 5 7 5
7 5 2 4
3 3 7 8
6 6 3 2
Output
0.57142857142857139685
Input
4
7 7 9 8
0 3 -5 4
5 0 5 4
9 9 -1 -6
10 5 -7 -10
Output
-1
Note
Here is a picture of the first sample
Points A, B, C, D - start mice positions, segments are their paths.
<image>
Then, at first time when all mice will be in rectangle it will be looks like this:
<image>
Here is a picture of the second sample
<image>
Points A, D, B will never enter rectangle. | instruction | 0 | 11,299 | 3 | 22,598 |
Tags: geometry, implementation, math, sortings
Correct Solution:
```
rd = lambda: map(int, input().split())
n = int(input())
x1, y1, x2, y2 = rd()
l = []
r = []
m = []
for i in range(n):
t = []
rx, ry, vx, vy = rd()
m.append([rx, ry, vx, vy])
if x1 <= rx <= x2 and y1 <= ry <= y2:
t.append(0)
if vx == 0 and vy == 0:
t.append(0x3f3f3f3f3f3f3f3f)
if vx:
t1 = (x1 - rx) / vx
if t1 >= 0:
if y1 <= ry + t1 * vy <= y2:
t.append(t1)
t1 = (x2 - rx) / vx
if t1 >= 0:
if y1 <= ry + t1 * vy <= y2:
t.append(t1)
if vy:
t1 = (y1 - ry) / vy
if t1 >= 0:
if x1 <= rx + t1 * vx <= x2:
t.append(t1)
t1 = (y2 - ry) / vy
if t1 >= 0:
if x1 <= rx + t1 * vx <= x2:
t.append(t1)
if len(t) < 2:
print(-1)
exit()
t.sort()
l.append(t[0])
r.append(t[-1])
l.sort()
r.sort()
if l[-1] > r[0]:
print(-1)
elif l[-1] == r[0]:
if not all(x1 < rx + r[0] * vx < x2 and y1 < ry + r[0] * vy < y2 for rx, ry, vx, vy in m):
print(-1)
else:
p = (l[-1] + r[0]) / 2
if not all(x1 < rx + p * vx < x2 and y1 < ry + p * vy < y2 for rx, ry, vx, vy in m):
print(-1)
else:
print(l[-1])
``` | output | 1 | 11,299 | 3 | 22,599 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Igor the analyst fell asleep on the work and had a strange dream. In the dream his desk was crowded with computer mice, so he bought a mousetrap to catch them.
The desk can be considered as an infinite plane, then the mousetrap is a rectangle which sides are parallel to the axes, and which opposite sides are located in points (x1, y1) and (x2, y2).
Igor wants to catch all mice. Igor has analysed their behavior and discovered that each mouse is moving along a straight line with constant speed, the speed of the i-th mouse is equal to (vix, viy), that means that the x coordinate of the mouse increases by vix units per second, while the y coordinates increases by viy units. The mousetrap is open initially so that the mice are able to move freely on the desk. Igor can close the mousetrap at any moment catching all the mice that are strictly inside the mousetrap.
Igor works a lot, so he is busy in the dream as well, and he asks you to write a program that by given mousetrap's coordinates, the initial coordinates of the mice and their speeds determines the earliest time moment in which he is able to catch all the mice. Please note that Igor can close the mousetrap only once.
Input
The first line contains single integer n (1 β€ n β€ 100 000) β the number of computer mice on the desk.
The second line contains four integers x1, y1, x2 and y2 (0 β€ x1 β€ x2 β€ 100 000), (0 β€ y1 β€ y2 β€ 100 000) β the coordinates of the opposite corners of the mousetrap.
The next n lines contain the information about mice.
The i-th of these lines contains four integers rix, riy, vix and viy, (0 β€ rix, riy β€ 100 000, - 100 000 β€ vix, viy β€ 100 000), where (rix, riy) is the initial position of the mouse, and (vix, viy) is its speed.
Output
In the only line print minimum possible non-negative number t such that if Igor closes the mousetrap at t seconds from the beginning, then all the mice are strictly inside the mousetrap. If there is no such t, print -1.
Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if <image>.
Examples
Input
4
7 7 9 8
3 5 7 5
7 5 2 4
3 3 7 8
6 6 3 2
Output
0.57142857142857139685
Input
4
7 7 9 8
0 3 -5 4
5 0 5 4
9 9 -1 -6
10 5 -7 -10
Output
-1
Note
Here is a picture of the first sample
Points A, B, C, D - start mice positions, segments are their paths.
<image>
Then, at first time when all mice will be in rectangle it will be looks like this:
<image>
Here is a picture of the second sample
<image>
Points A, D, B will never enter rectangle. | instruction | 0 | 11,300 | 3 | 22,600 |
Tags: geometry, implementation, math, sortings
Correct Solution:
```
n=int(input())
x1,y1,x2,y2=[int(x) for x in input().split()]
st=[0]
et=[1e10]
ok=1
if x1==x2 or y1==y2:
ok=0
for i in range(n):
a,b,u,w=[int(x) for x in input().split()]
for x,v,s,e in ((a,u,x1,x2),(b,w,y1,y2)):
if v==0:
if not(s<x<e):
ok=0
else:
t1=(s-x)/v
t2=(e-x)/v
st+=[min(t1,t2)]
et+=[max(t1,t2)]
#print(st,et)
if max(st)<min(et) and min(et)>0 and ok:
print(max(max(st),0))
else:
print(-1)
``` | output | 1 | 11,300 | 3 | 22,601 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Igor the analyst fell asleep on the work and had a strange dream. In the dream his desk was crowded with computer mice, so he bought a mousetrap to catch them.
The desk can be considered as an infinite plane, then the mousetrap is a rectangle which sides are parallel to the axes, and which opposite sides are located in points (x1, y1) and (x2, y2).
Igor wants to catch all mice. Igor has analysed their behavior and discovered that each mouse is moving along a straight line with constant speed, the speed of the i-th mouse is equal to (vix, viy), that means that the x coordinate of the mouse increases by vix units per second, while the y coordinates increases by viy units. The mousetrap is open initially so that the mice are able to move freely on the desk. Igor can close the mousetrap at any moment catching all the mice that are strictly inside the mousetrap.
Igor works a lot, so he is busy in the dream as well, and he asks you to write a program that by given mousetrap's coordinates, the initial coordinates of the mice and their speeds determines the earliest time moment in which he is able to catch all the mice. Please note that Igor can close the mousetrap only once.
Input
The first line contains single integer n (1 β€ n β€ 100 000) β the number of computer mice on the desk.
The second line contains four integers x1, y1, x2 and y2 (0 β€ x1 β€ x2 β€ 100 000), (0 β€ y1 β€ y2 β€ 100 000) β the coordinates of the opposite corners of the mousetrap.
The next n lines contain the information about mice.
The i-th of these lines contains four integers rix, riy, vix and viy, (0 β€ rix, riy β€ 100 000, - 100 000 β€ vix, viy β€ 100 000), where (rix, riy) is the initial position of the mouse, and (vix, viy) is its speed.
Output
In the only line print minimum possible non-negative number t such that if Igor closes the mousetrap at t seconds from the beginning, then all the mice are strictly inside the mousetrap. If there is no such t, print -1.
Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if <image>.
Examples
Input
4
7 7 9 8
3 5 7 5
7 5 2 4
3 3 7 8
6 6 3 2
Output
0.57142857142857139685
Input
4
7 7 9 8
0 3 -5 4
5 0 5 4
9 9 -1 -6
10 5 -7 -10
Output
-1
Note
Here is a picture of the first sample
Points A, B, C, D - start mice positions, segments are their paths.
<image>
Then, at first time when all mice will be in rectangle it will be looks like this:
<image>
Here is a picture of the second sample
<image>
Points A, D, B will never enter rectangle. | instruction | 0 | 11,301 | 3 | 22,602 |
Tags: geometry, implementation, math, sortings
Correct Solution:
```
import math
n = int(input())
x1, y1, x2, y2 = map(int, input().split())
t1 = 0
t2 = math.inf
yes = True
for i in range(n):
x, y, vx, vy = map(int, input().split())
if vx == 0:
if x <= x1 or x >= x2:
yes = False
break
else:
tt1 = (x1-x)/vx
tt2 = (x2-x)/vx
tt1, tt2 = min(tt1, tt2), max(tt1, tt2)
t1 = max(t1, tt1)
t2 = min(t2, tt2)
if vy == 0:
if y <= y1 or y >= y2:
yes = False
break
else:
tt1 = (y1-y)/vy
tt2 = (y2-y)/vy
tt1, tt2 = min(tt1, tt2), max(tt1, tt2)
t1 = max(t1, tt1)
t2 = min(t2, tt2)
if yes and t1 < t2:
print(t1)
else:
print(-1)
``` | output | 1 | 11,301 | 3 | 22,603 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Igor the analyst fell asleep on the work and had a strange dream. In the dream his desk was crowded with computer mice, so he bought a mousetrap to catch them.
The desk can be considered as an infinite plane, then the mousetrap is a rectangle which sides are parallel to the axes, and which opposite sides are located in points (x1, y1) and (x2, y2).
Igor wants to catch all mice. Igor has analysed their behavior and discovered that each mouse is moving along a straight line with constant speed, the speed of the i-th mouse is equal to (vix, viy), that means that the x coordinate of the mouse increases by vix units per second, while the y coordinates increases by viy units. The mousetrap is open initially so that the mice are able to move freely on the desk. Igor can close the mousetrap at any moment catching all the mice that are strictly inside the mousetrap.
Igor works a lot, so he is busy in the dream as well, and he asks you to write a program that by given mousetrap's coordinates, the initial coordinates of the mice and their speeds determines the earliest time moment in which he is able to catch all the mice. Please note that Igor can close the mousetrap only once.
Input
The first line contains single integer n (1 β€ n β€ 100 000) β the number of computer mice on the desk.
The second line contains four integers x1, y1, x2 and y2 (0 β€ x1 β€ x2 β€ 100 000), (0 β€ y1 β€ y2 β€ 100 000) β the coordinates of the opposite corners of the mousetrap.
The next n lines contain the information about mice.
The i-th of these lines contains four integers rix, riy, vix and viy, (0 β€ rix, riy β€ 100 000, - 100 000 β€ vix, viy β€ 100 000), where (rix, riy) is the initial position of the mouse, and (vix, viy) is its speed.
Output
In the only line print minimum possible non-negative number t such that if Igor closes the mousetrap at t seconds from the beginning, then all the mice are strictly inside the mousetrap. If there is no such t, print -1.
Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if <image>.
Examples
Input
4
7 7 9 8
3 5 7 5
7 5 2 4
3 3 7 8
6 6 3 2
Output
0.57142857142857139685
Input
4
7 7 9 8
0 3 -5 4
5 0 5 4
9 9 -1 -6
10 5 -7 -10
Output
-1
Note
Here is a picture of the first sample
Points A, B, C, D - start mice positions, segments are their paths.
<image>
Then, at first time when all mice will be in rectangle it will be looks like this:
<image>
Here is a picture of the second sample
<image>
Points A, D, B will never enter rectangle. | instruction | 0 | 11,302 | 3 | 22,604 |
Tags: geometry, implementation, math, sortings
Correct Solution:
```
# In the name of Allah
def main() :
n = int(input())
x1, y1, x2, y2 = map(int, input().split())
st = [0]
ed = [1e11]
if x1 == x2 or y1 == y2 :
print(-1)
exit()
for i in range(n) :
a, b, c, d = map(int, input().split())
for x, v, s, e in ((a, c, x1, x2), (b, d, y1, y2)) :
if v == 0 :
if not(s < x < e) :
print(-1)
exit()
else :
t1 = (s - x) / v;
t2 = (e - x) / v;
st += [min(t1, t2)]
ed += [max(t1, t2)]
if max(st) < min(ed) and min(ed) > 0 :
print(max(max(st), 0))
else :
print(-1)
return 0
main()
``` | output | 1 | 11,302 | 3 | 22,605 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Igor the analyst fell asleep on the work and had a strange dream. In the dream his desk was crowded with computer mice, so he bought a mousetrap to catch them.
The desk can be considered as an infinite plane, then the mousetrap is a rectangle which sides are parallel to the axes, and which opposite sides are located in points (x1, y1) and (x2, y2).
Igor wants to catch all mice. Igor has analysed their behavior and discovered that each mouse is moving along a straight line with constant speed, the speed of the i-th mouse is equal to (vix, viy), that means that the x coordinate of the mouse increases by vix units per second, while the y coordinates increases by viy units. The mousetrap is open initially so that the mice are able to move freely on the desk. Igor can close the mousetrap at any moment catching all the mice that are strictly inside the mousetrap.
Igor works a lot, so he is busy in the dream as well, and he asks you to write a program that by given mousetrap's coordinates, the initial coordinates of the mice and their speeds determines the earliest time moment in which he is able to catch all the mice. Please note that Igor can close the mousetrap only once.
Input
The first line contains single integer n (1 β€ n β€ 100 000) β the number of computer mice on the desk.
The second line contains four integers x1, y1, x2 and y2 (0 β€ x1 β€ x2 β€ 100 000), (0 β€ y1 β€ y2 β€ 100 000) β the coordinates of the opposite corners of the mousetrap.
The next n lines contain the information about mice.
The i-th of these lines contains four integers rix, riy, vix and viy, (0 β€ rix, riy β€ 100 000, - 100 000 β€ vix, viy β€ 100 000), where (rix, riy) is the initial position of the mouse, and (vix, viy) is its speed.
Output
In the only line print minimum possible non-negative number t such that if Igor closes the mousetrap at t seconds from the beginning, then all the mice are strictly inside the mousetrap. If there is no such t, print -1.
Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if <image>.
Examples
Input
4
7 7 9 8
3 5 7 5
7 5 2 4
3 3 7 8
6 6 3 2
Output
0.57142857142857139685
Input
4
7 7 9 8
0 3 -5 4
5 0 5 4
9 9 -1 -6
10 5 -7 -10
Output
-1
Note
Here is a picture of the first sample
Points A, B, C, D - start mice positions, segments are their paths.
<image>
Then, at first time when all mice will be in rectangle it will be looks like this:
<image>
Here is a picture of the second sample
<image>
Points A, D, B will never enter rectangle. | instruction | 0 | 11,303 | 3 | 22,606 |
Tags: geometry, implementation, math, sortings
Correct Solution:
```
N = int( input() )
x1, y1, x2, y2 = map( int, input().split() )
maxl, minr = 0.0, 1e30
for i in range( N ):
rx, ry, vx, vy = map( int, input().split() )
c = [ 0.0 for i in range( 4 ) ] # b, t, l, r
if vx == 0:
if not ( x1 < rx and rx < x2 ):
exit( print( -1 ) )
else:
c[ 2 ] = 0.0
c[ 3 ] = 0.0
else:
c[ 2 ] = ( x1 - rx ) / vx
c[ 3 ] = ( x2 - rx ) / vx
if vy == 0:
if not ( y1 < ry and ry < y2 ):
exit( print( -1 ) )
else:
c[ 0 ] = 0.0
c[ 1 ] = 0.0
else:
c[ 0 ] = ( y1 - ry ) / vy
c[ 1 ] = ( y2 - ry ) / vy
for j in range( 2 ):
xx = rx + c[ j ] * vx
if not ( x1 <= xx and xx <= x2 ):
c[ j ] = -1.0
for j in range( 2, 4, 1 ):
yy = ry + c[ j ] * vy
if not ( y1 <= yy and yy <= y2 ):
c[ j ] = -1.0
ll, rr = 1e30, 0.0
for j in range( 4 ):
if c[ j ] < 0.0: continue
ll = min( ll, c[ j ] )
rr = max( rr, c[ j ] )
if x1 < rx and rx < x2 and y1 < ry and ry < y2:
if vx == 0 and vy == 0: continue # mugen
minr = min( minr, rr )
continue
if ll == 1e30 or ll == rr:
exit( print( -1 ) )
maxl = max( maxl, ll )
minr = min( minr, rr )
if minr <= maxl:
print( -1 )
else:
print( "%.8f" % maxl )
``` | output | 1 | 11,303 | 3 | 22,607 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Igor the analyst fell asleep on the work and had a strange dream. In the dream his desk was crowded with computer mice, so he bought a mousetrap to catch them.
The desk can be considered as an infinite plane, then the mousetrap is a rectangle which sides are parallel to the axes, and which opposite sides are located in points (x1, y1) and (x2, y2).
Igor wants to catch all mice. Igor has analysed their behavior and discovered that each mouse is moving along a straight line with constant speed, the speed of the i-th mouse is equal to (vix, viy), that means that the x coordinate of the mouse increases by vix units per second, while the y coordinates increases by viy units. The mousetrap is open initially so that the mice are able to move freely on the desk. Igor can close the mousetrap at any moment catching all the mice that are strictly inside the mousetrap.
Igor works a lot, so he is busy in the dream as well, and he asks you to write a program that by given mousetrap's coordinates, the initial coordinates of the mice and their speeds determines the earliest time moment in which he is able to catch all the mice. Please note that Igor can close the mousetrap only once.
Input
The first line contains single integer n (1 β€ n β€ 100 000) β the number of computer mice on the desk.
The second line contains four integers x1, y1, x2 and y2 (0 β€ x1 β€ x2 β€ 100 000), (0 β€ y1 β€ y2 β€ 100 000) β the coordinates of the opposite corners of the mousetrap.
The next n lines contain the information about mice.
The i-th of these lines contains four integers rix, riy, vix and viy, (0 β€ rix, riy β€ 100 000, - 100 000 β€ vix, viy β€ 100 000), where (rix, riy) is the initial position of the mouse, and (vix, viy) is its speed.
Output
In the only line print minimum possible non-negative number t such that if Igor closes the mousetrap at t seconds from the beginning, then all the mice are strictly inside the mousetrap. If there is no such t, print -1.
Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if <image>.
Examples
Input
4
7 7 9 8
3 5 7 5
7 5 2 4
3 3 7 8
6 6 3 2
Output
0.57142857142857139685
Input
4
7 7 9 8
0 3 -5 4
5 0 5 4
9 9 -1 -6
10 5 -7 -10
Output
-1
Note
Here is a picture of the first sample
Points A, B, C, D - start mice positions, segments are their paths.
<image>
Then, at first time when all mice will be in rectangle it will be looks like this:
<image>
Here is a picture of the second sample
<image>
Points A, D, B will never enter rectangle. | instruction | 0 | 11,304 | 3 | 22,608 |
Tags: geometry, implementation, math, sortings
Correct Solution:
```
import sys
lines = sys.stdin.read().split('\n')[1:][:-1]
line_1 = lines.pop(0)
x_1, y_1, x_2, y_2 = map(int, line_1.split())
def terminate_with_fail():
print('-1', end='')
sys.exit()
def move(x, y, dx, dy, t):
return (x + t * dx,
y + t * dy)
def enter_line(k, dk, k_1, k_2):
if k_1 < k < k_2:
return 0
elif dk == 0:
terminate_with_fail()
move_time = min((k_1 - k) / dk, (k_2 - k) / dk)
if move_time < 0 or k_1 == k_2:
terminate_with_fail()
return move_time
def exit_line(k, dk, k_1, k_2):
if dk == 0:
return float('inf')
else:
return max((k_1 - k) / dk, (k_2 - k) / dk)
earliest = 0
latest = float('inf')
for line in lines:
x, y, dx, dy = map(int, line.split())
move_time_x = enter_line(x, dx, x_1, x_2)
(x, y) = move(x, y, dx, dy, move_time_x)
move_time_y = enter_line(y, dy, y_1, y_2)
(x, y) = move(x, y, dx, dy, move_time_y)
start_time = move_time_x + move_time_y
move_time_x_end = exit_line(x, dx, x_1, x_2)
move_time_y_end = exit_line(y, dy, y_1, y_2)
end_time = start_time + min(move_time_x_end, move_time_y_end)
earliest = max(earliest, start_time)
latest = min(latest, end_time)
if earliest >= latest:
terminate_with_fail()
print(str(earliest), end='')
``` | output | 1 | 11,304 | 3 | 22,609 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Igor the analyst fell asleep on the work and had a strange dream. In the dream his desk was crowded with computer mice, so he bought a mousetrap to catch them.
The desk can be considered as an infinite plane, then the mousetrap is a rectangle which sides are parallel to the axes, and which opposite sides are located in points (x1, y1) and (x2, y2).
Igor wants to catch all mice. Igor has analysed their behavior and discovered that each mouse is moving along a straight line with constant speed, the speed of the i-th mouse is equal to (vix, viy), that means that the x coordinate of the mouse increases by vix units per second, while the y coordinates increases by viy units. The mousetrap is open initially so that the mice are able to move freely on the desk. Igor can close the mousetrap at any moment catching all the mice that are strictly inside the mousetrap.
Igor works a lot, so he is busy in the dream as well, and he asks you to write a program that by given mousetrap's coordinates, the initial coordinates of the mice and their speeds determines the earliest time moment in which he is able to catch all the mice. Please note that Igor can close the mousetrap only once.
Input
The first line contains single integer n (1 β€ n β€ 100 000) β the number of computer mice on the desk.
The second line contains four integers x1, y1, x2 and y2 (0 β€ x1 β€ x2 β€ 100 000), (0 β€ y1 β€ y2 β€ 100 000) β the coordinates of the opposite corners of the mousetrap.
The next n lines contain the information about mice.
The i-th of these lines contains four integers rix, riy, vix and viy, (0 β€ rix, riy β€ 100 000, - 100 000 β€ vix, viy β€ 100 000), where (rix, riy) is the initial position of the mouse, and (vix, viy) is its speed.
Output
In the only line print minimum possible non-negative number t such that if Igor closes the mousetrap at t seconds from the beginning, then all the mice are strictly inside the mousetrap. If there is no such t, print -1.
Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if <image>.
Examples
Input
4
7 7 9 8
3 5 7 5
7 5 2 4
3 3 7 8
6 6 3 2
Output
0.57142857142857139685
Input
4
7 7 9 8
0 3 -5 4
5 0 5 4
9 9 -1 -6
10 5 -7 -10
Output
-1
Note
Here is a picture of the first sample
Points A, B, C, D - start mice positions, segments are their paths.
<image>
Then, at first time when all mice will be in rectangle it will be looks like this:
<image>
Here is a picture of the second sample
<image>
Points A, D, B will never enter rectangle. | instruction | 0 | 11,305 | 3 | 22,610 |
Tags: geometry, implementation, math, sortings
Correct Solution:
```
#!/usr/bin/env python3
from sys import stdin,stdout
from decimal import *
def ri():
return map(int, stdin.readline().split())
#lines = stdin.readlines()
def findt(rx,ry,vx,vy):
if vx == 0:
if rx > x1 and rx < x2:
t1x = Decimal(0)
t2x = Decimal(10**6)
else:
return 2, 1
else:
t1x = Decimal(x1-rx)/Decimal(vx)
t2x = Decimal(x2-rx)/Decimal(vx)
if vy == 0:
if ry > y1 and ry < y2:
t1y = Decimal(0)
t2y = Decimal(10**6)
else:
return 2, 1
else:
t1y = Decimal(y1-ry)/Decimal(vy)
t2y = Decimal(y2-ry)/Decimal(vy)
if t1x > t2x:
t1x, t2x = t2x, t1x
if t1y > t2y:
t1y, t2y = t2y, t1y
return max(t1x, t1y), min(t2x, t2y)
n = int(input())
x1, y1, x2, y2 = ri()
if x1 > x2:
x1, x2 = x2, x1
if y1 > y2:
y1, y2 = y2, y1
mintt = 0
maxtt = 10**20
for i in range(n):
rx, ry, vx, vy = ri()
mint, maxt = findt(rx, ry, vx, vy)
if mint >= maxt:
print(-1)
exit()
if maxt <= 0:
print(-1)
exit()
mintt = max(mint, mintt)
maxtt = min(maxt, maxtt)
if mintt >= maxtt:
print(-1)
exit()
print(mintt)
``` | output | 1 | 11,305 | 3 | 22,611 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Igor the analyst fell asleep on the work and had a strange dream. In the dream his desk was crowded with computer mice, so he bought a mousetrap to catch them.
The desk can be considered as an infinite plane, then the mousetrap is a rectangle which sides are parallel to the axes, and which opposite sides are located in points (x1, y1) and (x2, y2).
Igor wants to catch all mice. Igor has analysed their behavior and discovered that each mouse is moving along a straight line with constant speed, the speed of the i-th mouse is equal to (vix, viy), that means that the x coordinate of the mouse increases by vix units per second, while the y coordinates increases by viy units. The mousetrap is open initially so that the mice are able to move freely on the desk. Igor can close the mousetrap at any moment catching all the mice that are strictly inside the mousetrap.
Igor works a lot, so he is busy in the dream as well, and he asks you to write a program that by given mousetrap's coordinates, the initial coordinates of the mice and their speeds determines the earliest time moment in which he is able to catch all the mice. Please note that Igor can close the mousetrap only once.
Input
The first line contains single integer n (1 β€ n β€ 100 000) β the number of computer mice on the desk.
The second line contains four integers x1, y1, x2 and y2 (0 β€ x1 β€ x2 β€ 100 000), (0 β€ y1 β€ y2 β€ 100 000) β the coordinates of the opposite corners of the mousetrap.
The next n lines contain the information about mice.
The i-th of these lines contains four integers rix, riy, vix and viy, (0 β€ rix, riy β€ 100 000, - 100 000 β€ vix, viy β€ 100 000), where (rix, riy) is the initial position of the mouse, and (vix, viy) is its speed.
Output
In the only line print minimum possible non-negative number t such that if Igor closes the mousetrap at t seconds from the beginning, then all the mice are strictly inside the mousetrap. If there is no such t, print -1.
Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if <image>.
Examples
Input
4
7 7 9 8
3 5 7 5
7 5 2 4
3 3 7 8
6 6 3 2
Output
0.57142857142857139685
Input
4
7 7 9 8
0 3 -5 4
5 0 5 4
9 9 -1 -6
10 5 -7 -10
Output
-1
Note
Here is a picture of the first sample
Points A, B, C, D - start mice positions, segments are their paths.
<image>
Then, at first time when all mice will be in rectangle it will be looks like this:
<image>
Here is a picture of the second sample
<image>
Points A, D, B will never enter rectangle.
Submitted Solution:
```
n = int(input())
x1, y1, x2, y2 = map(int, input().split())
lx, rx, ly, ry = [], [], [], []
pos = True
for i in range(n):
x, y, vx, vy = map(int, input().split())
if x1 < x < x2:
lx.append(0)
if vx == 0:
rx.append(float('inf'))
else:
gl, gr = 0, 10 ** 5 + 1
while gr - gl > 10 ** -6:
m = (gl + gr) / 2
if x1 < x + m * vx < x2:
gl = m
else:
gr = m
rx.append(gr)
elif x >= x2:
if vx >= 0:
pos = False
break
else:
lx.append((x2 - x - 10 ** -6) / vx)
rx.append((x1 - x + 10 ** -6) / vx)
else:
if vx <= 0:
pos = False
break
else:
lx.append((x1 - x + 10 ** -6) / vx)
rx.append((x2 - x - 10 ** - 6) / vx)
if y1 < y < y2:
ly.append(0)
if vy == 0:
ry.append(float('inf'))
else:
gl, gr = 0, 10 ** 5 + 1
while gr - gl > 10 ** -6:
m = (gl + gr) / 2
if y1 < y + m * vy < y2:
gl = m
else:
gr = m
ry.append(gr)
elif y >= y2:
if vy >= 0:
pos = False
break
else:
ly.append((y2 - y - 10 ** -6) / vy)
ry.append((y1 - y + 10 ** -6) / vy)
else:
if vy <= 0:
pos = False
break
else:
ly.append((y1 - y + 10 ** -6) / vy)
ry.append((y2 - y - 10 ** - 6) / vy)
if not pos:
print(-1)
else:
l = max(max(ly), max(lx))
r = min(min(ry), min(rx))
if r <= l:
print(-1)
else:
print(l)
``` | instruction | 0 | 11,306 | 3 | 22,612 |
Yes | output | 1 | 11,306 | 3 | 22,613 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Igor the analyst fell asleep on the work and had a strange dream. In the dream his desk was crowded with computer mice, so he bought a mousetrap to catch them.
The desk can be considered as an infinite plane, then the mousetrap is a rectangle which sides are parallel to the axes, and which opposite sides are located in points (x1, y1) and (x2, y2).
Igor wants to catch all mice. Igor has analysed their behavior and discovered that each mouse is moving along a straight line with constant speed, the speed of the i-th mouse is equal to (vix, viy), that means that the x coordinate of the mouse increases by vix units per second, while the y coordinates increases by viy units. The mousetrap is open initially so that the mice are able to move freely on the desk. Igor can close the mousetrap at any moment catching all the mice that are strictly inside the mousetrap.
Igor works a lot, so he is busy in the dream as well, and he asks you to write a program that by given mousetrap's coordinates, the initial coordinates of the mice and their speeds determines the earliest time moment in which he is able to catch all the mice. Please note that Igor can close the mousetrap only once.
Input
The first line contains single integer n (1 β€ n β€ 100 000) β the number of computer mice on the desk.
The second line contains four integers x1, y1, x2 and y2 (0 β€ x1 β€ x2 β€ 100 000), (0 β€ y1 β€ y2 β€ 100 000) β the coordinates of the opposite corners of the mousetrap.
The next n lines contain the information about mice.
The i-th of these lines contains four integers rix, riy, vix and viy, (0 β€ rix, riy β€ 100 000, - 100 000 β€ vix, viy β€ 100 000), where (rix, riy) is the initial position of the mouse, and (vix, viy) is its speed.
Output
In the only line print minimum possible non-negative number t such that if Igor closes the mousetrap at t seconds from the beginning, then all the mice are strictly inside the mousetrap. If there is no such t, print -1.
Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if <image>.
Examples
Input
4
7 7 9 8
3 5 7 5
7 5 2 4
3 3 7 8
6 6 3 2
Output
0.57142857142857139685
Input
4
7 7 9 8
0 3 -5 4
5 0 5 4
9 9 -1 -6
10 5 -7 -10
Output
-1
Note
Here is a picture of the first sample
Points A, B, C, D - start mice positions, segments are their paths.
<image>
Then, at first time when all mice will be in rectangle it will be looks like this:
<image>
Here is a picture of the second sample
<image>
Points A, D, B will never enter rectangle.
Submitted Solution:
```
n = int(input())
x1, y1, x2, y2 = list(map(int, input().split()))
span = [0, 10000000]
for _ in range(n):
m = list(map(int, input().split()))
if m[2] == 0:
if m[0] <= min(x1, x2) or m[0] >= max(x1, x2):
span = []
break
else:
x_range = [-100000000, 100000000]
else:
x_range = sorted([(x1-m[0])/m[2], (x2-m[0])/m[2]])
if m[3] == 0:
if m[1] <= min(y1, y2) or m[1] >= max(y1, y2):
span = []
break
else:
y_range = [-100000000, 100000000]
else:
y_range = sorted([(y1-m[1])/m[3], (y2-m[1])/m[3]])
if x_range[1] < 0 or y_range[1] < 0:
span = []
break
r = [max(x_range[0], y_range[0]), min(x_range[1], y_range[1])]
span = [max(r[0], span[0]), min(r[1], span[1])]
#print(span)
if len(span) == 0 or span[1] <= span[0]:
print('-1')
else:
print('%.8f' % (span[0]+0.00000001))
``` | instruction | 0 | 11,307 | 3 | 22,614 |
Yes | output | 1 | 11,307 | 3 | 22,615 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Igor the analyst fell asleep on the work and had a strange dream. In the dream his desk was crowded with computer mice, so he bought a mousetrap to catch them.
The desk can be considered as an infinite plane, then the mousetrap is a rectangle which sides are parallel to the axes, and which opposite sides are located in points (x1, y1) and (x2, y2).
Igor wants to catch all mice. Igor has analysed their behavior and discovered that each mouse is moving along a straight line with constant speed, the speed of the i-th mouse is equal to (vix, viy), that means that the x coordinate of the mouse increases by vix units per second, while the y coordinates increases by viy units. The mousetrap is open initially so that the mice are able to move freely on the desk. Igor can close the mousetrap at any moment catching all the mice that are strictly inside the mousetrap.
Igor works a lot, so he is busy in the dream as well, and he asks you to write a program that by given mousetrap's coordinates, the initial coordinates of the mice and their speeds determines the earliest time moment in which he is able to catch all the mice. Please note that Igor can close the mousetrap only once.
Input
The first line contains single integer n (1 β€ n β€ 100 000) β the number of computer mice on the desk.
The second line contains four integers x1, y1, x2 and y2 (0 β€ x1 β€ x2 β€ 100 000), (0 β€ y1 β€ y2 β€ 100 000) β the coordinates of the opposite corners of the mousetrap.
The next n lines contain the information about mice.
The i-th of these lines contains four integers rix, riy, vix and viy, (0 β€ rix, riy β€ 100 000, - 100 000 β€ vix, viy β€ 100 000), where (rix, riy) is the initial position of the mouse, and (vix, viy) is its speed.
Output
In the only line print minimum possible non-negative number t such that if Igor closes the mousetrap at t seconds from the beginning, then all the mice are strictly inside the mousetrap. If there is no such t, print -1.
Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if <image>.
Examples
Input
4
7 7 9 8
3 5 7 5
7 5 2 4
3 3 7 8
6 6 3 2
Output
0.57142857142857139685
Input
4
7 7 9 8
0 3 -5 4
5 0 5 4
9 9 -1 -6
10 5 -7 -10
Output
-1
Note
Here is a picture of the first sample
Points A, B, C, D - start mice positions, segments are their paths.
<image>
Then, at first time when all mice will be in rectangle it will be looks like this:
<image>
Here is a picture of the second sample
<image>
Points A, D, B will never enter rectangle.
Submitted Solution:
```
# t*v+a = b
# t=(b-a)/v
import sys
#sys.stdin=open("data.txt")
input=sys.stdin.readline
mintime=(0,1)
maxtime=(10**9,1)
useless=0
n=int(input())
#include <math.h>
# int 'y1' redeclared as different kind of symbol
x1,y1,x2,y2=map(int,input().split())
for _ in range(n):
rx,ry,vx,vy=map(int,input().split())
# x time
if vx==0:
if not x1<rx<x2:
useless=1
break
else:
t1=(x1-rx,vx)
t2=(x2-rx,vx)
if vx<0:
t1=(-t1[0],-t1[1])
t2=(-t2[0],-t2[1])
# t11/t12 > t21/t22
if t1[0]*t2[1]>t1[1]*t2[0]:
t1,t2=t2,t1
if mintime[0]*t1[1]<mintime[1]*t1[0]:
mintime=t1
if maxtime[0]*t2[1]>maxtime[1]*t2[0]:
maxtime=t2
# y time
if vy==0:
if not y1<ry<y2:
useless=1
break
else:
t1=(y1-ry,vy)
t2=(y2-ry,vy)
if vy<0:
t1=(-t1[0],-t1[1])
t2=(-t2[0],-t2[1])
# t11/t12 > t21/t22
if t1[0]*t2[1]>t1[1]*t2[0]:
t1,t2=t2,t1
if mintime[0]*t1[1]<mintime[1]*t1[0]:
mintime=t1
if maxtime[0]*t2[1]>maxtime[1]*t2[0]:
maxtime=t2
if useless or mintime[0]*maxtime[1]>=maxtime[0]*mintime[1]:
print(-1)
else:
print(mintime[0]/float(mintime[1]))
``` | instruction | 0 | 11,308 | 3 | 22,616 |
Yes | output | 1 | 11,308 | 3 | 22,617 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Igor the analyst fell asleep on the work and had a strange dream. In the dream his desk was crowded with computer mice, so he bought a mousetrap to catch them.
The desk can be considered as an infinite plane, then the mousetrap is a rectangle which sides are parallel to the axes, and which opposite sides are located in points (x1, y1) and (x2, y2).
Igor wants to catch all mice. Igor has analysed their behavior and discovered that each mouse is moving along a straight line with constant speed, the speed of the i-th mouse is equal to (vix, viy), that means that the x coordinate of the mouse increases by vix units per second, while the y coordinates increases by viy units. The mousetrap is open initially so that the mice are able to move freely on the desk. Igor can close the mousetrap at any moment catching all the mice that are strictly inside the mousetrap.
Igor works a lot, so he is busy in the dream as well, and he asks you to write a program that by given mousetrap's coordinates, the initial coordinates of the mice and their speeds determines the earliest time moment in which he is able to catch all the mice. Please note that Igor can close the mousetrap only once.
Input
The first line contains single integer n (1 β€ n β€ 100 000) β the number of computer mice on the desk.
The second line contains four integers x1, y1, x2 and y2 (0 β€ x1 β€ x2 β€ 100 000), (0 β€ y1 β€ y2 β€ 100 000) β the coordinates of the opposite corners of the mousetrap.
The next n lines contain the information about mice.
The i-th of these lines contains four integers rix, riy, vix and viy, (0 β€ rix, riy β€ 100 000, - 100 000 β€ vix, viy β€ 100 000), where (rix, riy) is the initial position of the mouse, and (vix, viy) is its speed.
Output
In the only line print minimum possible non-negative number t such that if Igor closes the mousetrap at t seconds from the beginning, then all the mice are strictly inside the mousetrap. If there is no such t, print -1.
Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if <image>.
Examples
Input
4
7 7 9 8
3 5 7 5
7 5 2 4
3 3 7 8
6 6 3 2
Output
0.57142857142857139685
Input
4
7 7 9 8
0 3 -5 4
5 0 5 4
9 9 -1 -6
10 5 -7 -10
Output
-1
Note
Here is a picture of the first sample
Points A, B, C, D - start mice positions, segments are their paths.
<image>
Then, at first time when all mice will be in rectangle it will be looks like this:
<image>
Here is a picture of the second sample
<image>
Points A, D, B will never enter rectangle.
Submitted Solution:
```
import math
n = int(input())
x1, y1, x2, y2 = map(int, input().split())
t1 = 0
t2 = math.inf
yes = True
for i in range(n):
x, y, vx, vy = map(int, input().split())
if vx == 0:
if x <= x1 or x >= x2:
yes = False
break
else:
tt1 = (x1-x)/vx
tt2 = (x2-x)/vx
tt1, tt2 = min(tt1, tt2), max(tt1, tt2)
t1 = max(t1, tt1)
t2 = min(t2, tt2)
if vy == 0:
if y <= y1 or y >= y2:
yes = False
break
else:
tt1 = (y1-y)/vy
tt2 = (y2-y)/vy
tt1, tt2 = min(tt1, tt2), max(tt1, tt2)
t1 = max(t1, tt1)
t2 = min(t2, tt2)
if yes and t1 < t2:
print(t1)
else:
print(-1)
# Made By Mostafa_Khaled
``` | instruction | 0 | 11,309 | 3 | 22,618 |
Yes | output | 1 | 11,309 | 3 | 22,619 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Igor the analyst fell asleep on the work and had a strange dream. In the dream his desk was crowded with computer mice, so he bought a mousetrap to catch them.
The desk can be considered as an infinite plane, then the mousetrap is a rectangle which sides are parallel to the axes, and which opposite sides are located in points (x1, y1) and (x2, y2).
Igor wants to catch all mice. Igor has analysed their behavior and discovered that each mouse is moving along a straight line with constant speed, the speed of the i-th mouse is equal to (vix, viy), that means that the x coordinate of the mouse increases by vix units per second, while the y coordinates increases by viy units. The mousetrap is open initially so that the mice are able to move freely on the desk. Igor can close the mousetrap at any moment catching all the mice that are strictly inside the mousetrap.
Igor works a lot, so he is busy in the dream as well, and he asks you to write a program that by given mousetrap's coordinates, the initial coordinates of the mice and their speeds determines the earliest time moment in which he is able to catch all the mice. Please note that Igor can close the mousetrap only once.
Input
The first line contains single integer n (1 β€ n β€ 100 000) β the number of computer mice on the desk.
The second line contains four integers x1, y1, x2 and y2 (0 β€ x1 β€ x2 β€ 100 000), (0 β€ y1 β€ y2 β€ 100 000) β the coordinates of the opposite corners of the mousetrap.
The next n lines contain the information about mice.
The i-th of these lines contains four integers rix, riy, vix and viy, (0 β€ rix, riy β€ 100 000, - 100 000 β€ vix, viy β€ 100 000), where (rix, riy) is the initial position of the mouse, and (vix, viy) is its speed.
Output
In the only line print minimum possible non-negative number t such that if Igor closes the mousetrap at t seconds from the beginning, then all the mice are strictly inside the mousetrap. If there is no such t, print -1.
Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if <image>.
Examples
Input
4
7 7 9 8
3 5 7 5
7 5 2 4
3 3 7 8
6 6 3 2
Output
0.57142857142857139685
Input
4
7 7 9 8
0 3 -5 4
5 0 5 4
9 9 -1 -6
10 5 -7 -10
Output
-1
Note
Here is a picture of the first sample
Points A, B, C, D - start mice positions, segments are their paths.
<image>
Then, at first time when all mice will be in rectangle it will be looks like this:
<image>
Here is a picture of the second sample
<image>
Points A, D, B will never enter rectangle.
Submitted Solution:
```
import sys
n = int(input())
x1, y1, x2, y2 = tuple(map(int, input().split()))
mouse = []
for i in range(n):
mouse.append(list(map(int, input().split())))
def inside(a):
frames = []
if a[2] != 0:
g1 = (x1 - a[0])/a[2]
ay1 = a[3]*g1 + a[1]
if g1 >= 0 and y1 <= ay1 <= y2: frames.append(g1)
g2 = (x2 - a[0])/a[2]
ay2 = a[3]*g2 + a[1]
if g2 >= 0 and y1 <= ay2 <= y2: frames.append(g2)
if a[3] != 0:
g1 = (y1 - a[1])/a[3]
ax1 = a[2]*g1 + a[0]
if g1 >= 0 and x1 <= ax1 <= x2: frames.append(g1)
g2 = (y2 - a[1])/a[3]
ax2 = a[2]*g2 + a[0]
if g2 >= 0 and x1 <= ax2 <= x2: frames.append(g2)
if len(frames) == 0: print(-1); exit()
return sorted(frames)
min_out = sys.maxsize
max_in = 0
for i in range(n):
f = inside(mouse[i])
if f[0] > max_in: max_in = f[0]
if len(f) > 1 and f[1] < min_out: min_out = f[1]
if (max_in > min_out): print(-1)
else: print(max_in)
``` | instruction | 0 | 11,310 | 3 | 22,620 |
No | output | 1 | 11,310 | 3 | 22,621 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Igor the analyst fell asleep on the work and had a strange dream. In the dream his desk was crowded with computer mice, so he bought a mousetrap to catch them.
The desk can be considered as an infinite plane, then the mousetrap is a rectangle which sides are parallel to the axes, and which opposite sides are located in points (x1, y1) and (x2, y2).
Igor wants to catch all mice. Igor has analysed their behavior and discovered that each mouse is moving along a straight line with constant speed, the speed of the i-th mouse is equal to (vix, viy), that means that the x coordinate of the mouse increases by vix units per second, while the y coordinates increases by viy units. The mousetrap is open initially so that the mice are able to move freely on the desk. Igor can close the mousetrap at any moment catching all the mice that are strictly inside the mousetrap.
Igor works a lot, so he is busy in the dream as well, and he asks you to write a program that by given mousetrap's coordinates, the initial coordinates of the mice and their speeds determines the earliest time moment in which he is able to catch all the mice. Please note that Igor can close the mousetrap only once.
Input
The first line contains single integer n (1 β€ n β€ 100 000) β the number of computer mice on the desk.
The second line contains four integers x1, y1, x2 and y2 (0 β€ x1 β€ x2 β€ 100 000), (0 β€ y1 β€ y2 β€ 100 000) β the coordinates of the opposite corners of the mousetrap.
The next n lines contain the information about mice.
The i-th of these lines contains four integers rix, riy, vix and viy, (0 β€ rix, riy β€ 100 000, - 100 000 β€ vix, viy β€ 100 000), where (rix, riy) is the initial position of the mouse, and (vix, viy) is its speed.
Output
In the only line print minimum possible non-negative number t such that if Igor closes the mousetrap at t seconds from the beginning, then all the mice are strictly inside the mousetrap. If there is no such t, print -1.
Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if <image>.
Examples
Input
4
7 7 9 8
3 5 7 5
7 5 2 4
3 3 7 8
6 6 3 2
Output
0.57142857142857139685
Input
4
7 7 9 8
0 3 -5 4
5 0 5 4
9 9 -1 -6
10 5 -7 -10
Output
-1
Note
Here is a picture of the first sample
Points A, B, C, D - start mice positions, segments are their paths.
<image>
Then, at first time when all mice will be in rectangle it will be looks like this:
<image>
Here is a picture of the second sample
<image>
Points A, D, B will never enter rectangle.
Submitted Solution:
```
rd = lambda: map(int, input().split())
n = int(input())
x1, y1, x2, y2 = rd()
l = []
r = []
for i in range(n):
t = []
rx, ry, vx, vy = rd()
if x1 <= rx <= x2 and y1 <= ry <= y2:
t.append(0)
if vx:
t1 = (x1 - rx) / vx
if t1 >= 0:
if y1 <= ry + t1 * vy <= y2:
t.append(t1)
t1 = (x2 - rx) / vx
if t1 >= 0:
if y1 <= ry + t1 * vy <= y2:
t.append(t1)
if vy:
t1 = (y1 - ry) / vy
if t1 >= 0:
if x1 <= rx + t1 * vx <= x2:
t.append(t1)
t1 = (y2 - ry) / vy
if t1 >= 0:
if x1 <= rx + t1 * vx <= x2:
t.append(t1)
if vx == 0 and vy == 0:
t.append(0x3f3f3f3f3f3f3f3f)
if len(t) < 1:
print(-1)
exit()
t.sort()
l.append(t[0])
r.append(t[-1])
l.sort()
r.sort()
if l[-1] > r[0]:
print(-1)
else:
print(l[-1])
``` | instruction | 0 | 11,311 | 3 | 22,622 |
No | output | 1 | 11,311 | 3 | 22,623 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Igor the analyst fell asleep on the work and had a strange dream. In the dream his desk was crowded with computer mice, so he bought a mousetrap to catch them.
The desk can be considered as an infinite plane, then the mousetrap is a rectangle which sides are parallel to the axes, and which opposite sides are located in points (x1, y1) and (x2, y2).
Igor wants to catch all mice. Igor has analysed their behavior and discovered that each mouse is moving along a straight line with constant speed, the speed of the i-th mouse is equal to (vix, viy), that means that the x coordinate of the mouse increases by vix units per second, while the y coordinates increases by viy units. The mousetrap is open initially so that the mice are able to move freely on the desk. Igor can close the mousetrap at any moment catching all the mice that are strictly inside the mousetrap.
Igor works a lot, so he is busy in the dream as well, and he asks you to write a program that by given mousetrap's coordinates, the initial coordinates of the mice and their speeds determines the earliest time moment in which he is able to catch all the mice. Please note that Igor can close the mousetrap only once.
Input
The first line contains single integer n (1 β€ n β€ 100 000) β the number of computer mice on the desk.
The second line contains four integers x1, y1, x2 and y2 (0 β€ x1 β€ x2 β€ 100 000), (0 β€ y1 β€ y2 β€ 100 000) β the coordinates of the opposite corners of the mousetrap.
The next n lines contain the information about mice.
The i-th of these lines contains four integers rix, riy, vix and viy, (0 β€ rix, riy β€ 100 000, - 100 000 β€ vix, viy β€ 100 000), where (rix, riy) is the initial position of the mouse, and (vix, viy) is its speed.
Output
In the only line print minimum possible non-negative number t such that if Igor closes the mousetrap at t seconds from the beginning, then all the mice are strictly inside the mousetrap. If there is no such t, print -1.
Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if <image>.
Examples
Input
4
7 7 9 8
3 5 7 5
7 5 2 4
3 3 7 8
6 6 3 2
Output
0.57142857142857139685
Input
4
7 7 9 8
0 3 -5 4
5 0 5 4
9 9 -1 -6
10 5 -7 -10
Output
-1
Note
Here is a picture of the first sample
Points A, B, C, D - start mice positions, segments are their paths.
<image>
Then, at first time when all mice will be in rectangle it will be looks like this:
<image>
Here is a picture of the second sample
<image>
Points A, D, B will never enter rectangle.
Submitted Solution:
```
import sys
lines = sys.stdin.read().split('\n')[1:][:-1]
line_1 = lines.pop(0)
x_1, y_1, x_2, y_2 = map(int, line_1.split())
earliest = float('-inf')
latest = float('inf')
def move(x, y, dx, dy, t):
return (x + t * dx,
y + t * dy)
for line in lines:
x, y, dx, dy = map(int, line.split())
print((x, y, dx, dy))
start_time = 0
is_valid = True
if x_1 > x:
move_time = (x_1 - x) / dx
start_time += move_time
(x, y) = move(x, y, dx, dy, move_time)
is_valid = is_valid and move_time >= 0
if y_1 > y:
move_time = (y_1 - y) / dy
start_time += move_time
(x, y) = move(x, y, dx, dy, move_time)
is_valid = is_valid and move_time >= 0
if x > x_2:
move_time = (x_2 - x) / dx
start_time += move_time
(x, y) = move(x, y, dx, dy, move_time)
is_valid = is_valid and move_time >= 0
if y > y_2:
move_time = (y_2 - y) / dy
start_time += move_time
(x, y) = move(x, y, dx, dy, move_time)
is_valid = is_valid and move_time >= 0
if not is_valid:
print('-1', end='')
sys.exit()
move_time_x2 = (x_2 - x) / dx
move_time_y2 = (y_2 - y) / dy
end_time = start_time + max(move_time_x2, move_time_y2)
earliest = max(earliest, start_time)
latest = min(latest, end_time)
if earliest > latest:
print('-1', end='')
else:
print(str(earliest), end='')
``` | instruction | 0 | 11,312 | 3 | 22,624 |
No | output | 1 | 11,312 | 3 | 22,625 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Igor the analyst fell asleep on the work and had a strange dream. In the dream his desk was crowded with computer mice, so he bought a mousetrap to catch them.
The desk can be considered as an infinite plane, then the mousetrap is a rectangle which sides are parallel to the axes, and which opposite sides are located in points (x1, y1) and (x2, y2).
Igor wants to catch all mice. Igor has analysed their behavior and discovered that each mouse is moving along a straight line with constant speed, the speed of the i-th mouse is equal to (vix, viy), that means that the x coordinate of the mouse increases by vix units per second, while the y coordinates increases by viy units. The mousetrap is open initially so that the mice are able to move freely on the desk. Igor can close the mousetrap at any moment catching all the mice that are strictly inside the mousetrap.
Igor works a lot, so he is busy in the dream as well, and he asks you to write a program that by given mousetrap's coordinates, the initial coordinates of the mice and their speeds determines the earliest time moment in which he is able to catch all the mice. Please note that Igor can close the mousetrap only once.
Input
The first line contains single integer n (1 β€ n β€ 100 000) β the number of computer mice on the desk.
The second line contains four integers x1, y1, x2 and y2 (0 β€ x1 β€ x2 β€ 100 000), (0 β€ y1 β€ y2 β€ 100 000) β the coordinates of the opposite corners of the mousetrap.
The next n lines contain the information about mice.
The i-th of these lines contains four integers rix, riy, vix and viy, (0 β€ rix, riy β€ 100 000, - 100 000 β€ vix, viy β€ 100 000), where (rix, riy) is the initial position of the mouse, and (vix, viy) is its speed.
Output
In the only line print minimum possible non-negative number t such that if Igor closes the mousetrap at t seconds from the beginning, then all the mice are strictly inside the mousetrap. If there is no such t, print -1.
Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if <image>.
Examples
Input
4
7 7 9 8
3 5 7 5
7 5 2 4
3 3 7 8
6 6 3 2
Output
0.57142857142857139685
Input
4
7 7 9 8
0 3 -5 4
5 0 5 4
9 9 -1 -6
10 5 -7 -10
Output
-1
Note
Here is a picture of the first sample
Points A, B, C, D - start mice positions, segments are their paths.
<image>
Then, at first time when all mice will be in rectangle it will be looks like this:
<image>
Here is a picture of the second sample
<image>
Points A, D, B will never enter rectangle.
Submitted Solution:
```
def f(x, dx, s, t):
if dx < 0:
s, t = t, s
if dx == 0:
return (0, 2e9) if s < x and x < t else (2e9, 0)
low = (s-x) / dx + 1e-9
up = (t-x) / dx - 1e-9
return (low, up)
n = int(input())
x1, y1, x2, y2 = map(int, input().split())
l, r = 0.0, 1e9
for i in range(n):
x, y, dx, dy = map(int, input().split())
t = f(x, dx, x1, x2)
l = max(t[0], l)
r = min(t[1], r)
t = f(y, dy, y1, y2)
l = max(t[0], l)
r = min(t[1], r)
print("%f" % l)#(-1 if l > r else l))
``` | instruction | 0 | 11,313 | 3 | 22,626 |
No | output | 1 | 11,313 | 3 | 22,627 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Rebel fleet is on the run. It consists of m ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independently come up with the coordinate to which that ship will jump. In the obsolete navigation system used by the Rebels, this coordinate is given as the value of an arithmetic expression of the form <image>.
To plan the future of the resistance movement, Princess Heidi needs to know, for each ship, how many ships are going to end up at the same coordinate after the jump. You are her only hope!
Input
The first line of the input contains a single integer m (1 β€ m β€ 200 000) β the number of ships. The next m lines describe one jump coordinate each, given as an arithmetic expression. An expression has the form (a+b)/c. Namely, it consists of: an opening parenthesis (, a positive integer a of up to two decimal digits, a plus sign +, a positive integer b of up to two decimal digits, a closing parenthesis ), a slash /, and a positive integer c of up to two decimal digits.
Output
Print a single line consisting of m space-separated integers. The i-th integer should be equal to the number of ships whose coordinate is equal to that of the i-th ship (including the i-th ship itself).
Example
Input
4
(99+98)/97
(26+4)/10
(12+33)/15
(5+1)/7
Output
1 2 2 1
Note
In the sample testcase, the second and the third ship will both end up at the coordinate 3.
Note that this problem has only two versions β easy and hard. | instruction | 0 | 11,366 | 3 | 22,732 |
Tags: expression parsing, math
Correct Solution:
```
m=int(input())
ans=[]
f={}
for _ in range(m):
n=input()
a=n.split('(')
b=a[1].split(')')
ad,bd=b[0].split('+')
cd=b[1].split('/')[1]
ans.append((int(ad)+int(bd))/int(cd))
f[_]=(int(ad)+int(bd))/int(cd)
freq = {}
for item in ans:
if (item in freq):
freq[item] += 1
else:
freq[item] = 1
for i in range(m):
print(freq[f[i]],end=" ")
``` | output | 1 | 11,366 | 3 | 22,733 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Rebel fleet is on the run. It consists of m ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independently come up with the coordinate to which that ship will jump. In the obsolete navigation system used by the Rebels, this coordinate is given as the value of an arithmetic expression of the form <image>.
To plan the future of the resistance movement, Princess Heidi needs to know, for each ship, how many ships are going to end up at the same coordinate after the jump. You are her only hope!
Input
The first line of the input contains a single integer m (1 β€ m β€ 200 000) β the number of ships. The next m lines describe one jump coordinate each, given as an arithmetic expression. An expression has the form (a+b)/c. Namely, it consists of: an opening parenthesis (, a positive integer a of up to two decimal digits, a plus sign +, a positive integer b of up to two decimal digits, a closing parenthesis ), a slash /, and a positive integer c of up to two decimal digits.
Output
Print a single line consisting of m space-separated integers. The i-th integer should be equal to the number of ships whose coordinate is equal to that of the i-th ship (including the i-th ship itself).
Example
Input
4
(99+98)/97
(26+4)/10
(12+33)/15
(5+1)/7
Output
1 2 2 1
Note
In the sample testcase, the second and the third ship will both end up at the coordinate 3.
Note that this problem has only two versions β easy and hard. | instruction | 0 | 11,367 | 3 | 22,734 |
Tags: expression parsing, math
Correct Solution:
```
import collections
def compute_coorinate(data):
return eval(data)
if __name__ == "__main__":
m = int(input())
coordinates = []
for i in range(m):
coordinates.append(compute_coorinate(input()))
numbers = collections.Counter(coordinates)
print(" ".join(str(numbers[x]) for x in coordinates))
``` | output | 1 | 11,367 | 3 | 22,735 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Rebel fleet is on the run. It consists of m ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independently come up with the coordinate to which that ship will jump. In the obsolete navigation system used by the Rebels, this coordinate is given as the value of an arithmetic expression of the form <image>.
To plan the future of the resistance movement, Princess Heidi needs to know, for each ship, how many ships are going to end up at the same coordinate after the jump. You are her only hope!
Input
The first line of the input contains a single integer m (1 β€ m β€ 200 000) β the number of ships. The next m lines describe one jump coordinate each, given as an arithmetic expression. An expression has the form (a+b)/c. Namely, it consists of: an opening parenthesis (, a positive integer a of up to two decimal digits, a plus sign +, a positive integer b of up to two decimal digits, a closing parenthesis ), a slash /, and a positive integer c of up to two decimal digits.
Output
Print a single line consisting of m space-separated integers. The i-th integer should be equal to the number of ships whose coordinate is equal to that of the i-th ship (including the i-th ship itself).
Example
Input
4
(99+98)/97
(26+4)/10
(12+33)/15
(5+1)/7
Output
1 2 2 1
Note
In the sample testcase, the second and the third ship will both end up at the coordinate 3.
Note that this problem has only two versions β easy and hard. | instruction | 0 | 11,368 | 3 | 22,736 |
Tags: expression parsing, math
Correct Solution:
```
from collections import defaultdict
d = defaultdict(lambda : 0)
a = []
for _ in range(int(input())):
s = input()
a.append(s)
d[eval(s)] += 1
for e in a:
print(d[eval(e)], end = ' ')
``` | output | 1 | 11,368 | 3 | 22,737 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Rebel fleet is on the run. It consists of m ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independently come up with the coordinate to which that ship will jump. In the obsolete navigation system used by the Rebels, this coordinate is given as the value of an arithmetic expression of the form <image>.
To plan the future of the resistance movement, Princess Heidi needs to know, for each ship, how many ships are going to end up at the same coordinate after the jump. You are her only hope!
Input
The first line of the input contains a single integer m (1 β€ m β€ 200 000) β the number of ships. The next m lines describe one jump coordinate each, given as an arithmetic expression. An expression has the form (a+b)/c. Namely, it consists of: an opening parenthesis (, a positive integer a of up to two decimal digits, a plus sign +, a positive integer b of up to two decimal digits, a closing parenthesis ), a slash /, and a positive integer c of up to two decimal digits.
Output
Print a single line consisting of m space-separated integers. The i-th integer should be equal to the number of ships whose coordinate is equal to that of the i-th ship (including the i-th ship itself).
Example
Input
4
(99+98)/97
(26+4)/10
(12+33)/15
(5+1)/7
Output
1 2 2 1
Note
In the sample testcase, the second and the third ship will both end up at the coordinate 3.
Note that this problem has only two versions β easy and hard. | instruction | 0 | 11,369 | 3 | 22,738 |
Tags: expression parsing, math
Correct Solution:
```
from collections import defaultdict
m = int(input())
value = {}
count = defaultdict(int)
for i in range(m):
s = input()
ans = 0
z = ""
n = len(s)
for j in range(1,n):
if s[j]=='+':
ans = int(z)
z = ""
continue
elif s[j]==')':
ans +=int(z)
z = ""
j+=2
while j<n:
z+=s[j]
j+=1
ans = ans/int(z)
count[ans]+=1
value[i] = ans
else:
z = z+s[j]
for i in range(m):
print(count[value[i]],end=" ")
print()
``` | output | 1 | 11,369 | 3 | 22,739 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Rebel fleet is on the run. It consists of m ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independently come up with the coordinate to which that ship will jump. In the obsolete navigation system used by the Rebels, this coordinate is given as the value of an arithmetic expression of the form <image>.
To plan the future of the resistance movement, Princess Heidi needs to know, for each ship, how many ships are going to end up at the same coordinate after the jump. You are her only hope!
Input
The first line of the input contains a single integer m (1 β€ m β€ 200 000) β the number of ships. The next m lines describe one jump coordinate each, given as an arithmetic expression. An expression has the form (a+b)/c. Namely, it consists of: an opening parenthesis (, a positive integer a of up to two decimal digits, a plus sign +, a positive integer b of up to two decimal digits, a closing parenthesis ), a slash /, and a positive integer c of up to two decimal digits.
Output
Print a single line consisting of m space-separated integers. The i-th integer should be equal to the number of ships whose coordinate is equal to that of the i-th ship (including the i-th ship itself).
Example
Input
4
(99+98)/97
(26+4)/10
(12+33)/15
(5+1)/7
Output
1 2 2 1
Note
In the sample testcase, the second and the third ship will both end up at the coordinate 3.
Note that this problem has only two versions β easy and hard. | instruction | 0 | 11,370 | 3 | 22,740 |
Tags: expression parsing, math
Correct Solution:
```
arr = []
d = {}
for _ in range(int(input())):
s = input()
a,b,c = tuple(map(int, s.replace("(","").replace(")","").replace("/",".").replace("+",".").split(".")))
x = (a+b)/c
arr.append(x)
if x not in d:
d[x] = 0
d[x] += 1
for i in arr:
print(d[i], end = " ")
``` | output | 1 | 11,370 | 3 | 22,741 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Rebel fleet is on the run. It consists of m ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independently come up with the coordinate to which that ship will jump. In the obsolete navigation system used by the Rebels, this coordinate is given as the value of an arithmetic expression of the form <image>.
To plan the future of the resistance movement, Princess Heidi needs to know, for each ship, how many ships are going to end up at the same coordinate after the jump. You are her only hope!
Input
The first line of the input contains a single integer m (1 β€ m β€ 200 000) β the number of ships. The next m lines describe one jump coordinate each, given as an arithmetic expression. An expression has the form (a+b)/c. Namely, it consists of: an opening parenthesis (, a positive integer a of up to two decimal digits, a plus sign +, a positive integer b of up to two decimal digits, a closing parenthesis ), a slash /, and a positive integer c of up to two decimal digits.
Output
Print a single line consisting of m space-separated integers. The i-th integer should be equal to the number of ships whose coordinate is equal to that of the i-th ship (including the i-th ship itself).
Example
Input
4
(99+98)/97
(26+4)/10
(12+33)/15
(5+1)/7
Output
1 2 2 1
Note
In the sample testcase, the second and the third ship will both end up at the coordinate 3.
Note that this problem has only two versions β easy and hard. | instruction | 0 | 11,371 | 3 | 22,742 |
Tags: expression parsing, math
Correct Solution:
```
from collections import Counter
def getList(l, m):
r = []
cnt = Counter(l)
for i in l:
r.extend([cnt[i]])
return r
m = int(input())
res = []
for i in range(m):
a, b = [i for i in input().split("/")]
a = a.split("+")
a = [a[0].replace("(", ""), a[len(a) - 1].replace(")", "")]
a = list(map(lambda x: int(x), a)); b = float(b)
res.extend([sum(a)/b])
output = getList(res, m) + ["\n"]
for i in output:
print(str(i) + " ", end = "")
``` | output | 1 | 11,371 | 3 | 22,743 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Rebel fleet is on the run. It consists of m ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independently come up with the coordinate to which that ship will jump. In the obsolete navigation system used by the Rebels, this coordinate is given as the value of an arithmetic expression of the form <image>.
To plan the future of the resistance movement, Princess Heidi needs to know, for each ship, how many ships are going to end up at the same coordinate after the jump. You are her only hope!
Input
The first line of the input contains a single integer m (1 β€ m β€ 200 000) β the number of ships. The next m lines describe one jump coordinate each, given as an arithmetic expression. An expression has the form (a+b)/c. Namely, it consists of: an opening parenthesis (, a positive integer a of up to two decimal digits, a plus sign +, a positive integer b of up to two decimal digits, a closing parenthesis ), a slash /, and a positive integer c of up to two decimal digits.
Output
Print a single line consisting of m space-separated integers. The i-th integer should be equal to the number of ships whose coordinate is equal to that of the i-th ship (including the i-th ship itself).
Example
Input
4
(99+98)/97
(26+4)/10
(12+33)/15
(5+1)/7
Output
1 2 2 1
Note
In the sample testcase, the second and the third ship will both end up at the coordinate 3.
Note that this problem has only two versions β easy and hard. | instruction | 0 | 11,372 | 3 | 22,744 |
Tags: expression parsing, math
Correct Solution:
```
from collections import defaultdict
from sys import stdin
input = stdin.readline
dct = defaultdict(int)
n = int(input())
lst = [0] * n
for i in range(n):
t = input().strip()
a, b, c = map(int, (t[1:t.index('+')], t[t.index('+') + 1:t.index(')')], t[t.index('/') + 1:]))
x = (a + b) / c
lst[i] = x
dct[x] += 1
for i in lst:
print(dct[i], end=' ')
``` | output | 1 | 11,372 | 3 | 22,745 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Rebel fleet is on the run. It consists of m ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independently come up with the coordinate to which that ship will jump. In the obsolete navigation system used by the Rebels, this coordinate is given as the value of an arithmetic expression of the form <image>.
To plan the future of the resistance movement, Princess Heidi needs to know, for each ship, how many ships are going to end up at the same coordinate after the jump. You are her only hope!
Input
The first line of the input contains a single integer m (1 β€ m β€ 200 000) β the number of ships. The next m lines describe one jump coordinate each, given as an arithmetic expression. An expression has the form (a+b)/c. Namely, it consists of: an opening parenthesis (, a positive integer a of up to two decimal digits, a plus sign +, a positive integer b of up to two decimal digits, a closing parenthesis ), a slash /, and a positive integer c of up to two decimal digits.
Output
Print a single line consisting of m space-separated integers. The i-th integer should be equal to the number of ships whose coordinate is equal to that of the i-th ship (including the i-th ship itself).
Example
Input
4
(99+98)/97
(26+4)/10
(12+33)/15
(5+1)/7
Output
1 2 2 1
Note
In the sample testcase, the second and the third ship will both end up at the coordinate 3.
Note that this problem has only two versions β easy and hard. | instruction | 0 | 11,373 | 3 | 22,746 |
Tags: expression parsing, math
Correct Solution:
```
n = int(input())
cl = []
dic = {}
res = [0]*n
for i in range(n):
x = eval(input())
if x not in dic.keys():
dic.update({x:[]})
dic[x].append(i)
else:
dic[x].append(i)
for x in dic.values():
k = len(x)
for y in x:
res[y]=k
result = ''
for x in res:
result+=str(x)+' '
print(result)
``` | output | 1 | 11,373 | 3 | 22,747 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Rebel fleet is on the run. It consists of m ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independently come up with the coordinate to which that ship will jump. In the obsolete navigation system used by the Rebels, this coordinate is given as the value of an arithmetic expression of the form <image>.
To plan the future of the resistance movement, Princess Heidi needs to know, for each ship, how many ships are going to end up at the same coordinate after the jump. You are her only hope!
Input
The first line of the input contains a single integer m (1 β€ m β€ 200 000) β the number of ships. The next m lines describe one jump coordinate each, given as an arithmetic expression. An expression has the form (a+b)/c. Namely, it consists of: an opening parenthesis (, a positive integer a of up to two decimal digits, a plus sign +, a positive integer b of up to two decimal digits, a closing parenthesis ), a slash /, and a positive integer c of up to two decimal digits.
Output
Print a single line consisting of m space-separated integers. The i-th integer should be equal to the number of ships whose coordinate is equal to that of the i-th ship (including the i-th ship itself).
Example
Input
4
(99+98)/97
(26+4)/10
(12+33)/15
(5+1)/7
Output
1 2 2 1
Note
In the sample testcase, the second and the third ship will both end up at the coordinate 3.
Note that this problem has only two versions β easy and hard.
Submitted Solution:
```
n = int(input())
l=[]
dic={}
while(n>0):
s=input()
coord= eval(s)
l.append(coord)
dic[coord]=dic.get(coord,0) + 1
n-=1
new_list = [ dic[coord] for coord in l ]
print(*new_list,sep =" ")
``` | instruction | 0 | 11,374 | 3 | 22,748 |
Yes | output | 1 | 11,374 | 3 | 22,749 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Rebel fleet is on the run. It consists of m ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independently come up with the coordinate to which that ship will jump. In the obsolete navigation system used by the Rebels, this coordinate is given as the value of an arithmetic expression of the form <image>.
To plan the future of the resistance movement, Princess Heidi needs to know, for each ship, how many ships are going to end up at the same coordinate after the jump. You are her only hope!
Input
The first line of the input contains a single integer m (1 β€ m β€ 200 000) β the number of ships. The next m lines describe one jump coordinate each, given as an arithmetic expression. An expression has the form (a+b)/c. Namely, it consists of: an opening parenthesis (, a positive integer a of up to two decimal digits, a plus sign +, a positive integer b of up to two decimal digits, a closing parenthesis ), a slash /, and a positive integer c of up to two decimal digits.
Output
Print a single line consisting of m space-separated integers. The i-th integer should be equal to the number of ships whose coordinate is equal to that of the i-th ship (including the i-th ship itself).
Example
Input
4
(99+98)/97
(26+4)/10
(12+33)/15
(5+1)/7
Output
1 2 2 1
Note
In the sample testcase, the second and the third ship will both end up at the coordinate 3.
Note that this problem has only two versions β easy and hard.
Submitted Solution:
```
"""
Satwik_Tiwari ;) .
4th july , 2020 - Saturday
"""
#===============================================================================================
#importing some useful libraries.
from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
import bisect
from heapq import *
from math import *
from collections import deque
from collections import Counter as counter # Counter(list) return a dict with {key: count}
from itertools import combinations as comb # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)]
from itertools import permutations as permutate
from bisect import bisect_left as bl
#If the element is already present in the list,
# the left most position where element has to be inserted is returned.
from bisect import bisect_right as br
from bisect import bisect
#If the element is already present in the list,
# the right most position where element has to be inserted is returned
#==============================================================================================
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
# inp = lambda: sys.stdin.readline().rstrip("\r\n")
#===============================================================================================
#some shortcuts
mod = 1000000007
def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input
def out(var): sys.stdout.write(str(var)) #for fast output, always take string
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
# def graph(vertex): return [[] for i in range(0,vertex+1)]
def zerolist(n): return [0]*n
def nextline(): out("\n") #as stdout.write always print sring.
def testcase(t):
for p in range(t):
solve()
def printlist(a) :
for p in range(0,len(a)):
out(str(a[p]) + ' ')
def lcm(a,b): return (a*b)//gcd(a,b)
def power(a,b):
ans = 1
while(b>0):
if(b%2==1):
ans*=a
a*=a
b//=2
return ans
def ncr(n,r): return factorial(n)//(factorial(r)*factorial(max(n-r,1)))
def isPrime(n) : # Check Prime Number or not
if (n <= 1) : return False
if (n <= 3) : return True
if (n % 2 == 0 or n % 3 == 0) : return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
#===============================================================================================
# code here ;))
def solve():
m = int(inp())
d = {}
arr = [0]*(m)
for i in range(0,m):
s = list(inp())
plus = s.index('+')
a = s[1:plus]
b = s[plus+1:s.index(')')]
c = s[s.index('/')+1:]
a = ''.join(a)
b = ''.join(b)
c = ''.join(c)
# print(a,b,c)
co = (int(a)+int(b))/int(c)
if(d.get(co) == None):
d[co] = [i]
else:
d[co].append(i)
arr[i] = co
# print(arr)
# print(d)
ans = []
for i in range(m):
ans.append(len(d[arr[i]]))
print(' '.join(str(ans[i]) for i in range(len(ans))))
testcase(1)
# testcase(int(inp()))
``` | instruction | 0 | 11,375 | 3 | 22,750 |
Yes | output | 1 | 11,375 | 3 | 22,751 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Rebel fleet is on the run. It consists of m ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independently come up with the coordinate to which that ship will jump. In the obsolete navigation system used by the Rebels, this coordinate is given as the value of an arithmetic expression of the form <image>.
To plan the future of the resistance movement, Princess Heidi needs to know, for each ship, how many ships are going to end up at the same coordinate after the jump. You are her only hope!
Input
The first line of the input contains a single integer m (1 β€ m β€ 200 000) β the number of ships. The next m lines describe one jump coordinate each, given as an arithmetic expression. An expression has the form (a+b)/c. Namely, it consists of: an opening parenthesis (, a positive integer a of up to two decimal digits, a plus sign +, a positive integer b of up to two decimal digits, a closing parenthesis ), a slash /, and a positive integer c of up to two decimal digits.
Output
Print a single line consisting of m space-separated integers. The i-th integer should be equal to the number of ships whose coordinate is equal to that of the i-th ship (including the i-th ship itself).
Example
Input
4
(99+98)/97
(26+4)/10
(12+33)/15
(5+1)/7
Output
1 2 2 1
Note
In the sample testcase, the second and the third ship will both end up at the coordinate 3.
Note that this problem has only two versions β easy and hard.
Submitted Solution:
```
d = {}
l =[]
for i in range(int(input())):
a,b,c = map(int,input().replace("(","").replace(")/"," ").replace("+"," ").split())
ans = (a+b)/c
l.append(ans)
if ans in d: d[ans]+=1
else: d[ans]=1
for i in l:
print(d[i],end=" ")
``` | instruction | 0 | 11,376 | 3 | 22,752 |
Yes | output | 1 | 11,376 | 3 | 22,753 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.