message stringlengths 2 19.9k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 322 108k | cluster float64 15 15 | __index_level_0__ int64 644 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nobuo-kun and Shizuo-kun are playing a game of competing for territories on a rectangular island. As shown in Fig. 1 below, the entire island is made up of square compartments divided in a grid pattern, and the profits and losses resulting from these are indicated by integers.
<image>
In this game, move one piece to determine the boundaries of the territory. At the beginning of the game, the piece is at the northwestern end of the island (Fig. β ). The trajectory of the piece when moving this piece to the southeastern end of the island becomes the boundary of the territory. The two players move the pieces alternately. Pieces can only be moved to the grid point next to the south or the grid point next to the east (Fig. 2). When the piece reaches the edge of the island, move it in the south or east direction. The game is over when the piece reaches the southeastern end.
The area on the northeast side of the boundary after the game is the area of ββthe first player, and the area on the southwest side is the area of ββthe second player (Fig. β’). The sum of the profits and losses generated from the parcels contained within each player's territory is that player's core. Both of them are quite accustomed to the game and move the pieces accurately so that the value obtained by subtracting the opponent's score from their own score is the largest in the end.
Create a program that calculates the outcome at the end of the game given the size of the island and the gains and losses that result from each parcel. The result is the absolute value of the difference between the scores of Nobuo-kun and Shizuo-kun.
Input
The input is given in the following format.
W H
s1,1 s1,2 ... s1, W
s2,1 s2,2 ... s2, W
::
sH, 1 sH, 2 ... sH, W
The first line gives the number of east-west and north-south sections contained in the island W, H (1 β€ W, H β€ 1000). The following row H is given the gains and losses si, j (-1000 β€ si, j β€ 1000) that arise from the compartments in row i and column j. However, the direction in which the value of i increases is south, and the direction in which the value of j increases is east.
Output
The absolute value of the difference between the scores of Nobuo-kun and Shizuo-kun is output in one line.
Examples
Input
2 1
-2 1
Output
1
Input
2 2
2 -3
3 -1
Output
3
Input
5 4
5 3 2 -5 2
2 -4 2 8 -4
2 3 -7 6 7
3 -4 10 -3 -3
Output
5
Submitted Solution:
```
w, h = map(int, input().split())
S = [list(map(int, input().split())) for i in range(h)]
SW = [[0]*w for i in range(h)]
SH = [[0]*w for i in range(h)]
for i in range(h):
cnt = 0
for j in range(w-1, -1, -1):
cnt += S[i][j]
SW[i][j] = cnt
for j in range(w):
cnt = 0
for i in range(h-1, -1, -1):
cnt += S[i][j]
SH[i][j] = cnt
memo = {}
def dfs(x, y):
if (x, y) in memo:
return memo[x, y]
if x == w or y == h:
return 0
if (x+y) % 2 == 0:
# first
res = max(dfs(x+1, y) - SH[y][x], dfs(x, y+1) + SW[y][x])
else:
# second
res = min(dfs(x+1, y) - SH[y][x], dfs(x, y+1) + SW[y][x])
memo[x, y] = res
return res
print(abs(dfs(0, 0)))
``` | instruction | 0 | 48,726 | 15 | 97,452 |
No | output | 1 | 48,726 | 15 | 97,453 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An elephant decided to visit his friend. It turned out that the elephant's house is located at point 0 and his friend's house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend's house.
Input
The first line of the input contains an integer x (1 β€ x β€ 1 000 000) β The coordinate of the friend's house.
Output
Print the minimum number of steps that elephant needs to make to get from point 0 to point x.
Examples
Input
5
Output
1
Input
12
Output
3
Note
In the first sample the elephant needs to make one step of length 5 to reach the point x.
In the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves. | instruction | 0 | 49,252 | 15 | 98,504 |
Tags: math
Correct Solution:
```
pos = int(input())
cnt = 0
while pos > 0:
if pos >= 5:
pos -= 5
else:
pos -= pos
cnt += 1
print(cnt)
``` | output | 1 | 49,252 | 15 | 98,505 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An elephant decided to visit his friend. It turned out that the elephant's house is located at point 0 and his friend's house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend's house.
Input
The first line of the input contains an integer x (1 β€ x β€ 1 000 000) β The coordinate of the friend's house.
Output
Print the minimum number of steps that elephant needs to make to get from point 0 to point x.
Examples
Input
5
Output
1
Input
12
Output
3
Note
In the first sample the elephant needs to make one step of length 5 to reach the point x.
In the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves. | instruction | 0 | 49,253 | 15 | 98,506 |
Tags: math
Correct Solution:
```
x = int(input())
if x % 5 == 0:
ans = x / 5
else:
ans = x / 5 + 1
print(int(ans))
``` | output | 1 | 49,253 | 15 | 98,507 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An elephant decided to visit his friend. It turned out that the elephant's house is located at point 0 and his friend's house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend's house.
Input
The first line of the input contains an integer x (1 β€ x β€ 1 000 000) β The coordinate of the friend's house.
Output
Print the minimum number of steps that elephant needs to make to get from point 0 to point x.
Examples
Input
5
Output
1
Input
12
Output
3
Note
In the first sample the elephant needs to make one step of length 5 to reach the point x.
In the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves. | instruction | 0 | 49,254 | 15 | 98,508 |
Tags: math
Correct Solution:
```
pl = int(input())
step = 0
kol = 0
while step != pl:
if step + 5 < pl:
step += 5
kol += 1
else:
if pl - step == 0:
print(kol)
else:
print(kol + 1)
break
``` | output | 1 | 49,254 | 15 | 98,509 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An elephant decided to visit his friend. It turned out that the elephant's house is located at point 0 and his friend's house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend's house.
Input
The first line of the input contains an integer x (1 β€ x β€ 1 000 000) β The coordinate of the friend's house.
Output
Print the minimum number of steps that elephant needs to make to get from point 0 to point x.
Examples
Input
5
Output
1
Input
12
Output
3
Note
In the first sample the elephant needs to make one step of length 5 to reach the point x.
In the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves. | instruction | 0 | 49,255 | 15 | 98,510 |
Tags: math
Correct Solution:
```
k=int(input())
kil=k//5
ost=k%5
kil=kil+ost//4
ost=ost%4
kil=kil+ost//3
ost=ost%3
kil=kil+ost//2
ost=ost%2
kil=kil+ost//1
ost=ost%1
print(kil)
``` | output | 1 | 49,255 | 15 | 98,511 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An elephant decided to visit his friend. It turned out that the elephant's house is located at point 0 and his friend's house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend's house.
Input
The first line of the input contains an integer x (1 β€ x β€ 1 000 000) β The coordinate of the friend's house.
Output
Print the minimum number of steps that elephant needs to make to get from point 0 to point x.
Examples
Input
5
Output
1
Input
12
Output
3
Note
In the first sample the elephant needs to make one step of length 5 to reach the point x.
In the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves. | instruction | 0 | 49,256 | 15 | 98,512 |
Tags: math
Correct Solution:
```
d=0
n=int(input())
while(n>0):
if(n>=5):
d=d+(n//5)
n=n%5
elif(n>=4):
d=d+(n//4)
n=n%4
elif(n>=3):
d=d+(n//3)
n=n%3
elif(n>=2):
d=d+(n//2)
n=n%2
else:
d=d+(n/1)
n=n%1
print(int(d))
``` | output | 1 | 49,256 | 15 | 98,513 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An elephant decided to visit his friend. It turned out that the elephant's house is located at point 0 and his friend's house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend's house.
Input
The first line of the input contains an integer x (1 β€ x β€ 1 000 000) β The coordinate of the friend's house.
Output
Print the minimum number of steps that elephant needs to make to get from point 0 to point x.
Examples
Input
5
Output
1
Input
12
Output
3
Note
In the first sample the elephant needs to make one step of length 5 to reach the point x.
In the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves. | instruction | 0 | 49,257 | 15 | 98,514 |
Tags: math
Correct Solution:
```
n=int(input())
t=n
c=0
while(t>0):
if(t%5>=0):
c=c+1
t=t-5
elif(t%4>=0):
c=c+1
t=t-4
elif(t%3>=0):
c=c+1
t=t-3
elif(t%2>=0):
c=c+1
t=t-2
elif(t%1>=0):
c=c+1
t=t-1
print(c)
``` | output | 1 | 49,257 | 15 | 98,515 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An elephant decided to visit his friend. It turned out that the elephant's house is located at point 0 and his friend's house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend's house.
Input
The first line of the input contains an integer x (1 β€ x β€ 1 000 000) β The coordinate of the friend's house.
Output
Print the minimum number of steps that elephant needs to make to get from point 0 to point x.
Examples
Input
5
Output
1
Input
12
Output
3
Note
In the first sample the elephant needs to make one step of length 5 to reach the point x.
In the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves. | instruction | 0 | 49,258 | 15 | 98,516 |
Tags: math
Correct Solution:
```
x = int(input())
print(x//5 + min(x%5, 1))
``` | output | 1 | 49,258 | 15 | 98,517 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An elephant decided to visit his friend. It turned out that the elephant's house is located at point 0 and his friend's house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend's house.
Input
The first line of the input contains an integer x (1 β€ x β€ 1 000 000) β The coordinate of the friend's house.
Output
Print the minimum number of steps that elephant needs to make to get from point 0 to point x.
Examples
Input
5
Output
1
Input
12
Output
3
Note
In the first sample the elephant needs to make one step of length 5 to reach the point x.
In the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves. | instruction | 0 | 49,259 | 15 | 98,518 |
Tags: math
Correct Solution:
```
x = int(input())
if x%5!=0:
x = (x//5)+1
else:
x = x//5
print(x)
``` | output | 1 | 49,259 | 15 | 98,519 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An elephant decided to visit his friend. It turned out that the elephant's house is located at point 0 and his friend's house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend's house.
Input
The first line of the input contains an integer x (1 β€ x β€ 1 000 000) β The coordinate of the friend's house.
Output
Print the minimum number of steps that elephant needs to make to get from point 0 to point x.
Examples
Input
5
Output
1
Input
12
Output
3
Note
In the first sample the elephant needs to make one step of length 5 to reach the point x.
In the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves.
Submitted Solution:
```
n = int(input())
k = n // 5
if n % 5 == 0:
print(k)
else:
print(k+1)
``` | instruction | 0 | 49,260 | 15 | 98,520 |
Yes | output | 1 | 49,260 | 15 | 98,521 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An elephant decided to visit his friend. It turned out that the elephant's house is located at point 0 and his friend's house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend's house.
Input
The first line of the input contains an integer x (1 β€ x β€ 1 000 000) β The coordinate of the friend's house.
Output
Print the minimum number of steps that elephant needs to make to get from point 0 to point x.
Examples
Input
5
Output
1
Input
12
Output
3
Note
In the first sample the elephant needs to make one step of length 5 to reach the point x.
In the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves.
Submitted Solution:
```
def main():
lst = [5, 4, 3, 2, 1]
pos = int(input())
steps, i, count = 0, 0, 0
while steps != pos:
if (steps + lst[i]) <= pos:
steps += lst[i]
count += 1
else:
i += 1
print(count)
main()
``` | instruction | 0 | 49,261 | 15 | 98,522 |
Yes | output | 1 | 49,261 | 15 | 98,523 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An elephant decided to visit his friend. It turned out that the elephant's house is located at point 0 and his friend's house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend's house.
Input
The first line of the input contains an integer x (1 β€ x β€ 1 000 000) β The coordinate of the friend's house.
Output
Print the minimum number of steps that elephant needs to make to get from point 0 to point x.
Examples
Input
5
Output
1
Input
12
Output
3
Note
In the first sample the elephant needs to make one step of length 5 to reach the point x.
In the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves.
Submitted Solution:
```
n = int(input())
i = 0
while n != 0:
if n-5 >= 0:
n -= 5
i+=1
elif n-4 >= 0:
n -= 4
i+=1
elif n - 3 >= 0:
n -= 3
i+=1
elif n - 2 >= 0:
n -= 2
i+=1
elif n == 1:
i += 1
n = 0
print(i)
``` | instruction | 0 | 49,262 | 15 | 98,524 |
Yes | output | 1 | 49,262 | 15 | 98,525 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An elephant decided to visit his friend. It turned out that the elephant's house is located at point 0 and his friend's house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend's house.
Input
The first line of the input contains an integer x (1 β€ x β€ 1 000 000) β The coordinate of the friend's house.
Output
Print the minimum number of steps that elephant needs to make to get from point 0 to point x.
Examples
Input
5
Output
1
Input
12
Output
3
Note
In the first sample the elephant needs to make one step of length 5 to reach the point x.
In the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves.
Submitted Solution:
```
x = int(input())
print((x+4)//5)
``` | instruction | 0 | 49,263 | 15 | 98,526 |
Yes | output | 1 | 49,263 | 15 | 98,527 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An elephant decided to visit his friend. It turned out that the elephant's house is located at point 0 and his friend's house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend's house.
Input
The first line of the input contains an integer x (1 β€ x β€ 1 000 000) β The coordinate of the friend's house.
Output
Print the minimum number of steps that elephant needs to make to get from point 0 to point x.
Examples
Input
5
Output
1
Input
12
Output
3
Note
In the first sample the elephant needs to make one step of length 5 to reach the point x.
In the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves.
Submitted Solution:
```
x=int(input())
cnt=0
if(x%5==0):
cnt+=1
elif(x%4==0):
cnt+=1
elif(x%3==0):
cnt+=1
elif(x%2==0):
cnt+=1
elif(x%1==0):
cnt+=1
print(cnt)
``` | instruction | 0 | 49,264 | 15 | 98,528 |
No | output | 1 | 49,264 | 15 | 98,529 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An elephant decided to visit his friend. It turned out that the elephant's house is located at point 0 and his friend's house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend's house.
Input
The first line of the input contains an integer x (1 β€ x β€ 1 000 000) β The coordinate of the friend's house.
Output
Print the minimum number of steps that elephant needs to make to get from point 0 to point x.
Examples
Input
5
Output
1
Input
12
Output
3
Note
In the first sample the elephant needs to make one step of length 5 to reach the point x.
In the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves.
Submitted Solution:
```
x=int(input())
print(x)
if x<5:
print(1)
else:
print(int(x/5)+1)
``` | instruction | 0 | 49,265 | 15 | 98,530 |
No | output | 1 | 49,265 | 15 | 98,531 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An elephant decided to visit his friend. It turned out that the elephant's house is located at point 0 and his friend's house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend's house.
Input
The first line of the input contains an integer x (1 β€ x β€ 1 000 000) β The coordinate of the friend's house.
Output
Print the minimum number of steps that elephant needs to make to get from point 0 to point x.
Examples
Input
5
Output
1
Input
12
Output
3
Note
In the first sample the elephant needs to make one step of length 5 to reach the point x.
In the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves.
Submitted Solution:
```
user_input = int(input())
list = [1,2,3,4,5]
count = 0
for i in list:
if user_input % i == 0:
count += user_input // i
break
else:
count += user_input // i
user_input = user_input % i
print(count)
``` | instruction | 0 | 49,266 | 15 | 98,532 |
No | output | 1 | 49,266 | 15 | 98,533 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An elephant decided to visit his friend. It turned out that the elephant's house is located at point 0 and his friend's house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend's house.
Input
The first line of the input contains an integer x (1 β€ x β€ 1 000 000) β The coordinate of the friend's house.
Output
Print the minimum number of steps that elephant needs to make to get from point 0 to point x.
Examples
Input
5
Output
1
Input
12
Output
3
Note
In the first sample the elephant needs to make one step of length 5 to reach the point x.
In the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves.
Submitted Solution:
```
n = int(input())
count = 0
for i in range(100):
if n>4:
n -= 5
count +=1
else:
break
if n<=4 and n>0:
count +=1
print(count)
``` | instruction | 0 | 49,267 | 15 | 98,534 |
No | output | 1 | 49,267 | 15 | 98,535 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Adieu l'ami.
Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around the abandoned Eikou Cram School building, Oshino's makeshift residence.
The space is represented by a rectangular grid of n Γ m cells, arranged into n rows and m columns. The c-th cell in the r-th row is denoted by (r, c).
Oshino places and removes barriers around rectangular areas of cells. Specifically, an action denoted by "1 r1 c1 r2 c2" means Oshino's placing barriers around a rectangle with two corners being (r1, c1) and (r2, c2) and sides parallel to squares sides. Similarly, "2 r1 c1 r2 c2" means Oshino's removing barriers around the rectangle. Oshino ensures that no barriers staying on the ground share any common points, nor do they intersect with boundaries of the n Γ m area.
Sometimes Koyomi tries to walk from one cell to another carefully without striding over barriers, in order to avoid damaging various items on the ground. "3 r1 c1 r2 c2" means that Koyomi tries to walk from (r1, c1) to (r2, c2) without crossing barriers.
And you're here to tell Koyomi the feasibility of each of his attempts.
Input
The first line of input contains three space-separated integers n, m and q (1 β€ n, m β€ 2 500, 1 β€ q β€ 100 000) β the number of rows and columns in the grid, and the total number of Oshino and Koyomi's actions, respectively.
The following q lines each describes an action, containing five space-separated integers t, r1, c1, r2, c2 (1 β€ t β€ 3, 1 β€ r1, r2 β€ n, 1 β€ c1, c2 β€ m) β the type and two coordinates of an action. Additionally, the following holds depending on the value of t:
* If t = 1: 2 β€ r1 β€ r2 β€ n - 1, 2 β€ c1 β€ c2 β€ m - 1;
* If t = 2: 2 β€ r1 β€ r2 β€ n - 1, 2 β€ c1 β€ c2 β€ m - 1, the specified group of barriers exist on the ground before the removal.
* If t = 3: no extra restrictions.
Output
For each of Koyomi's attempts (actions with t = 3), output one line β containing "Yes" (without quotes) if it's feasible, and "No" (without quotes) otherwise.
Examples
Input
5 6 5
1 2 2 4 5
1 3 3 3 3
3 4 4 1 1
2 2 2 4 5
3 1 1 4 4
Output
No
Yes
Input
2500 2500 8
1 549 1279 1263 2189
1 303 795 1888 2432
1 2227 622 2418 1161
3 771 2492 1335 1433
1 2017 2100 2408 2160
3 48 60 798 729
1 347 708 1868 792
3 1940 2080 377 1546
Output
No
Yes
No
Note
For the first example, the situations of Koyomi's actions are illustrated below.
<image>
Submitted Solution:
```
#!/usr/bin/env python3
import sys
import threading
sys.setrecursionlimit(10 ** 6)
threading.stack_size(2 ** 25)
class RegTree(object):
def __init__(self, r1, c1, r2, c2):
self.r1 = r1
self.r2 = r2
self.c1 = c1
self.c2 = c2
self.sreg = []
def include(self, x, y):
return self.r1 <= x and self.r2 >= x and self.c1 <= y and self.c2 >= y
def insert(self, r1, c1, r2, c2):
for child in self.sreg:
if child.include(r1, c1):
child.insert(r1, c1, r2, c2)
return self
self.sreg.append(RegTree(r1, c1, r2, c2))
return self
def delete(self, r1, c1, r2, c2, parent=None):
if self.r1 == r1 and self.r2 == r2 and self.c1 == c1 and self.c2 == c2:
assert(parent is not None)
parent.sreg += self.sreg
parent.sreg.remove(self)
return
else:
for child in self.sreg:
if child.include(r1, c1):
child.delete(r1, c1, r2, c2, self)
return
assert(False)
def query(self, x, y):
for child in self.sreg:
if child.include(x, y):
return child.query(x, y)
return self
def main():
n, m, q = map(int, input().split())
root = RegTree(1, 1, n, m)
for __ in range(q):
t, r1, c1, r2, c2 = map(int, input().split())
if t == 1:
root.insert(r1, c1, r2, c2)
elif t == 2:
root.delete(r1, c1, r2, c2)
else:
print("Yes" if root.query(r1, c1) == root.query(r2, c2) else "No")
if __name__ == '__main__':
threading.Thread(target=main).start()
``` | instruction | 0 | 49,353 | 15 | 98,706 |
No | output | 1 | 49,353 | 15 | 98,707 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Adieu l'ami.
Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around the abandoned Eikou Cram School building, Oshino's makeshift residence.
The space is represented by a rectangular grid of n Γ m cells, arranged into n rows and m columns. The c-th cell in the r-th row is denoted by (r, c).
Oshino places and removes barriers around rectangular areas of cells. Specifically, an action denoted by "1 r1 c1 r2 c2" means Oshino's placing barriers around a rectangle with two corners being (r1, c1) and (r2, c2) and sides parallel to squares sides. Similarly, "2 r1 c1 r2 c2" means Oshino's removing barriers around the rectangle. Oshino ensures that no barriers staying on the ground share any common points, nor do they intersect with boundaries of the n Γ m area.
Sometimes Koyomi tries to walk from one cell to another carefully without striding over barriers, in order to avoid damaging various items on the ground. "3 r1 c1 r2 c2" means that Koyomi tries to walk from (r1, c1) to (r2, c2) without crossing barriers.
And you're here to tell Koyomi the feasibility of each of his attempts.
Input
The first line of input contains three space-separated integers n, m and q (1 β€ n, m β€ 2 500, 1 β€ q β€ 100 000) β the number of rows and columns in the grid, and the total number of Oshino and Koyomi's actions, respectively.
The following q lines each describes an action, containing five space-separated integers t, r1, c1, r2, c2 (1 β€ t β€ 3, 1 β€ r1, r2 β€ n, 1 β€ c1, c2 β€ m) β the type and two coordinates of an action. Additionally, the following holds depending on the value of t:
* If t = 1: 2 β€ r1 β€ r2 β€ n - 1, 2 β€ c1 β€ c2 β€ m - 1;
* If t = 2: 2 β€ r1 β€ r2 β€ n - 1, 2 β€ c1 β€ c2 β€ m - 1, the specified group of barriers exist on the ground before the removal.
* If t = 3: no extra restrictions.
Output
For each of Koyomi's attempts (actions with t = 3), output one line β containing "Yes" (without quotes) if it's feasible, and "No" (without quotes) otherwise.
Examples
Input
5 6 5
1 2 2 4 5
1 3 3 3 3
3 4 4 1 1
2 2 2 4 5
3 1 1 4 4
Output
No
Yes
Input
2500 2500 8
1 549 1279 1263 2189
1 303 795 1888 2432
1 2227 622 2418 1161
3 771 2492 1335 1433
1 2017 2100 2408 2160
3 48 60 798 729
1 347 708 1868 792
3 1940 2080 377 1546
Output
No
Yes
No
Note
For the first example, the situations of Koyomi's actions are illustrated below.
<image>
Submitted Solution:
```
N, M, Q = map(int, input().split())
bars = []
for i in range(Q):
t, r1, c1, r2, c2 = map(int, input().split())
if t == 1:
bars.append((min(c1, c2), min(r1, r2), max(c1, c2), max(r1, r2)))
elif t == 2:
bars.remove((min(c1, c2), min(r1, r2), max(c1, c2), max(r1, r2)))
elif t == 3:
for x1, y1, x2, y2 in bars:
if (x1 <= c1 <= x2 and y1 <= r1 <= y2) or (x1 <= c2 <= x2 and y1 <= r2 <= y2):
print("No")
break
else:
print("Yes")
``` | instruction | 0 | 49,354 | 15 | 98,708 |
No | output | 1 | 49,354 | 15 | 98,709 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Adieu l'ami.
Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around the abandoned Eikou Cram School building, Oshino's makeshift residence.
The space is represented by a rectangular grid of n Γ m cells, arranged into n rows and m columns. The c-th cell in the r-th row is denoted by (r, c).
Oshino places and removes barriers around rectangular areas of cells. Specifically, an action denoted by "1 r1 c1 r2 c2" means Oshino's placing barriers around a rectangle with two corners being (r1, c1) and (r2, c2) and sides parallel to squares sides. Similarly, "2 r1 c1 r2 c2" means Oshino's removing barriers around the rectangle. Oshino ensures that no barriers staying on the ground share any common points, nor do they intersect with boundaries of the n Γ m area.
Sometimes Koyomi tries to walk from one cell to another carefully without striding over barriers, in order to avoid damaging various items on the ground. "3 r1 c1 r2 c2" means that Koyomi tries to walk from (r1, c1) to (r2, c2) without crossing barriers.
And you're here to tell Koyomi the feasibility of each of his attempts.
Input
The first line of input contains three space-separated integers n, m and q (1 β€ n, m β€ 2 500, 1 β€ q β€ 100 000) β the number of rows and columns in the grid, and the total number of Oshino and Koyomi's actions, respectively.
The following q lines each describes an action, containing five space-separated integers t, r1, c1, r2, c2 (1 β€ t β€ 3, 1 β€ r1, r2 β€ n, 1 β€ c1, c2 β€ m) β the type and two coordinates of an action. Additionally, the following holds depending on the value of t:
* If t = 1: 2 β€ r1 β€ r2 β€ n - 1, 2 β€ c1 β€ c2 β€ m - 1;
* If t = 2: 2 β€ r1 β€ r2 β€ n - 1, 2 β€ c1 β€ c2 β€ m - 1, the specified group of barriers exist on the ground before the removal.
* If t = 3: no extra restrictions.
Output
For each of Koyomi's attempts (actions with t = 3), output one line β containing "Yes" (without quotes) if it's feasible, and "No" (without quotes) otherwise.
Examples
Input
5 6 5
1 2 2 4 5
1 3 3 3 3
3 4 4 1 1
2 2 2 4 5
3 1 1 4 4
Output
No
Yes
Input
2500 2500 8
1 549 1279 1263 2189
1 303 795 1888 2432
1 2227 622 2418 1161
3 771 2492 1335 1433
1 2017 2100 2408 2160
3 48 60 798 729
1 347 708 1868 792
3 1940 2080 377 1546
Output
No
Yes
No
Note
For the first example, the situations of Koyomi's actions are illustrated below.
<image>
Submitted Solution:
```
class Area:
def __init__(self, x1, y1, x2, y2):
self.left = min(x1, x2)
self.top = min(y1, y2)
self.right = max(x1, x2)
self.bottom = max(y1, y2)
self.children = []
def __eq__(self, other):
return self.left == other.left and \
self.top == other.top and \
self.right == other.right and \
self.bottom == other.bottom
def contain(self, x, y):
return self.left <= x <= self.right and self.top <= y <= self.bottom
def contain_not_in_child(self, x, y):
if not self.contain(x, y):
return False
container = next((child for child in self.children if child.contain(x, y)), None)
return container is None
def container_for(self, x, y):
container = next((child for child in self.children if child.contain(x, y)), None)
if container is None:
return self
return container.container_for(x, y)
def add_children(self, children):
self.children += children
def insert_child(self, child):
container = next((x for x in self.children if x.contain(child.left, child.top)), None)
if container is None:
subchildren = (x for x in self.children if child.contain(x.left, x.right))
child.children = list(subchildren)
self.children = [x for x in self.children if x not in child.children]
self.children.append(child)
else:
container.insert_child(child)
def remove_child(self, child):
container = next((x for x in self.children if x.contain(child.left, child.top)), None)
if container is None:
return
if container != child:
container.remove_child(child)
return
self.children.remove(container)
self.add_children(container.children)
n, m, q = map(int, input().split())
mainArea = Area(1, 1, m, n)
for i in range(q):
action, x1, y1, x2, y2 = map(int, input().split())
if action == 1:
mainArea.insert_child(Area(x1, y1, x2, y2))
if action == 2:
mainArea.remove_child(Area(x1, y1, x2, y2))
if action == 3:
container = mainArea.container_for(x1, y1)
print("Yes" if container.contain_not_in_child(x2, y2) else "No")
``` | instruction | 0 | 49,355 | 15 | 98,710 |
No | output | 1 | 49,355 | 15 | 98,711 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Adieu l'ami.
Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around the abandoned Eikou Cram School building, Oshino's makeshift residence.
The space is represented by a rectangular grid of n Γ m cells, arranged into n rows and m columns. The c-th cell in the r-th row is denoted by (r, c).
Oshino places and removes barriers around rectangular areas of cells. Specifically, an action denoted by "1 r1 c1 r2 c2" means Oshino's placing barriers around a rectangle with two corners being (r1, c1) and (r2, c2) and sides parallel to squares sides. Similarly, "2 r1 c1 r2 c2" means Oshino's removing barriers around the rectangle. Oshino ensures that no barriers staying on the ground share any common points, nor do they intersect with boundaries of the n Γ m area.
Sometimes Koyomi tries to walk from one cell to another carefully without striding over barriers, in order to avoid damaging various items on the ground. "3 r1 c1 r2 c2" means that Koyomi tries to walk from (r1, c1) to (r2, c2) without crossing barriers.
And you're here to tell Koyomi the feasibility of each of his attempts.
Input
The first line of input contains three space-separated integers n, m and q (1 β€ n, m β€ 2 500, 1 β€ q β€ 100 000) β the number of rows and columns in the grid, and the total number of Oshino and Koyomi's actions, respectively.
The following q lines each describes an action, containing five space-separated integers t, r1, c1, r2, c2 (1 β€ t β€ 3, 1 β€ r1, r2 β€ n, 1 β€ c1, c2 β€ m) β the type and two coordinates of an action. Additionally, the following holds depending on the value of t:
* If t = 1: 2 β€ r1 β€ r2 β€ n - 1, 2 β€ c1 β€ c2 β€ m - 1;
* If t = 2: 2 β€ r1 β€ r2 β€ n - 1, 2 β€ c1 β€ c2 β€ m - 1, the specified group of barriers exist on the ground before the removal.
* If t = 3: no extra restrictions.
Output
For each of Koyomi's attempts (actions with t = 3), output one line β containing "Yes" (without quotes) if it's feasible, and "No" (without quotes) otherwise.
Examples
Input
5 6 5
1 2 2 4 5
1 3 3 3 3
3 4 4 1 1
2 2 2 4 5
3 1 1 4 4
Output
No
Yes
Input
2500 2500 8
1 549 1279 1263 2189
1 303 795 1888 2432
1 2227 622 2418 1161
3 771 2492 1335 1433
1 2017 2100 2408 2160
3 48 60 798 729
1 347 708 1868 792
3 1940 2080 377 1546
Output
No
Yes
No
Note
For the first example, the situations of Koyomi's actions are illustrated below.
<image>
Submitted Solution:
```
n, m, q = [int(x) for x in input().split()]
walls = set()
for _ in range(q):
t, r1, c1, r2, c2 = [int(x) for x in input().split()]
if t == 1:
walls.add((r1, c1, r2, c2))
elif t==2:
walls.remove((r1, c1, r2, c2))
else:
start_edge = r1==1 or c1==1 or r1==n or c1==m
goal_edge = r1==1 or c1==1 or r1==n or c1==m
if start_edge:
if goal_edge:
print("Yes")
else:
for wall in walls:
ri, ci, rf, cf = wall
if r2>=ri and r2<=rf and c2>=ci and c2<=cf:
print("No")
break
else:
print("Yes")
elif goal_edge:
for wall in walls:
ri, ci, rf, cf = wall
if r1>=ri and r1<=rf and c1>=ci and c1<=cf:
print("No")
break
else:
print("Yes")
else:
for wall in walls:
ri, ci, rf, cf = wall
start_enclosed = (r1>=ri and r1<=rf and c1>=ci and c1<=cf)
goal_enclosed = (r2>=ri and r2<=rf and c2>=ci and c2<=cf)
if (start_enclosed and not goal_enclosed) or (goal_enclosed and not start_enclosed):
print("No")
break
else:
print("Yes")
``` | instruction | 0 | 49,356 | 15 | 98,712 |
No | output | 1 | 49,356 | 15 | 98,713 |
Provide a correct Python 3 solution for this coding contest problem.
Tetris is a game in which falling blocks are lined up on the board and erased. Here, let's consider a game that arranges it a little.
The size of the board of this game is 5 frames wide, and it is high enough to accommodate all the blocks that appear. The falling blocks are straight and come in two types, landscape and portrait, with five lengths from 1 to 5.
An example is shown below. The figure from Step (a) to Step (e) shows how the blocks fall and disappear. From Step (a), proceed in order of Step (b) and Step (c).
When dropping a block, if somewhere in the block is caught in a block piled up like Step (a), the dropped block like Step (b) will stop at that place. Also, as a result of dropping a block, if all the frames in a horizontal line on the board are clogged with blocks, the blocks in that line will disappear as shown in Step (d). After this, the block above the disappeared line will move down one line as it is (Step (e)).
<image>
In one game, up to 1000 blocks will be dropped in order. For example, the lengths of the falling blocks are 4 frames horizontally, 3 frames horizontally, 2 frames vertically, and 3 frames vertically, and the falling locations are the 1st, 1st, 4th, and 5th frames from the left end. If there is, it will drop as shown in Step (a) to (g) in the figure below, and the last remaining block will be 2 frames.
<image>
Create a program that inputs the information of the blocks that fall in order and outputs the number of frames remaining when all the blocks fall.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
d1 p1 q1
d2 p2 q2
::
dn pn qn
The number of blocks n (1 β€ n β€ 1000) is given on the first line. The next n lines give the i-th block orientation di (1 or 2), the block length pi (1 β€ pi β€ 5), and the block position qi. Block orientation di is 1 for landscape orientation and 2 for portrait orientation. The block position qi is an integer from 1 to 5 from the left edge on the board, and in the case of a horizontal block, it is the position where the left edge frame falls.
The number of datasets does not exceed 20.
Output
The number of frames occupied by the last remaining block for each data set is output on one line.
Example
Input
4
1 4 1
1 3 1
2 2 4
2 3 5
1
1 5 1
7
2 2 2
1 4 1
2 1 3
1 4 1
1 1 1
2 5 5
1 4 2
0
Output
2
0
6 | instruction | 0 | 49,529 | 15 | 99,058 |
"Correct Solution:
```
def v_block():
block.insert(0, [' ']*5)
for l, line in enumerate(block):
if line[q-1:q+p-1] != [' ']*p:
break
else:
l += 1
block[l-1][q-1:q+p-1] = ['*']*p
return delete()
def h_block():
for _p in range(p):
block.insert(0, [' ']*5)
for l, line in enumerate(block):
if line[q-1] == '*':
break
else:
l += 1
for _l in range(l-1, l-p-1, -1):
block[_l][q-1] = '*'
return delete()
def delete():
b = len(block)
l = 0
while 0 < b:
if block[l] == [' ']*5 or block[l] == ['*']*5:
del block[l]
else:
l += 1
b -= 1
def count():
x = 0
for line in block:
for b in line:
if b == '*':
x += 1
else:
print(x)
while True:
n = int(input())
if n == 0:
break
block = [[' ']*5]
for i in range(n):
d, p, q = map(int, input().split())
if d == 1:
v_block()
else:
h_block()
else:
count()
``` | output | 1 | 49,529 | 15 | 99,059 |
Provide a correct Python 3 solution for this coding contest problem.
Tetris is a game in which falling blocks are lined up on the board and erased. Here, let's consider a game that arranges it a little.
The size of the board of this game is 5 frames wide, and it is high enough to accommodate all the blocks that appear. The falling blocks are straight and come in two types, landscape and portrait, with five lengths from 1 to 5.
An example is shown below. The figure from Step (a) to Step (e) shows how the blocks fall and disappear. From Step (a), proceed in order of Step (b) and Step (c).
When dropping a block, if somewhere in the block is caught in a block piled up like Step (a), the dropped block like Step (b) will stop at that place. Also, as a result of dropping a block, if all the frames in a horizontal line on the board are clogged with blocks, the blocks in that line will disappear as shown in Step (d). After this, the block above the disappeared line will move down one line as it is (Step (e)).
<image>
In one game, up to 1000 blocks will be dropped in order. For example, the lengths of the falling blocks are 4 frames horizontally, 3 frames horizontally, 2 frames vertically, and 3 frames vertically, and the falling locations are the 1st, 1st, 4th, and 5th frames from the left end. If there is, it will drop as shown in Step (a) to (g) in the figure below, and the last remaining block will be 2 frames.
<image>
Create a program that inputs the information of the blocks that fall in order and outputs the number of frames remaining when all the blocks fall.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
d1 p1 q1
d2 p2 q2
::
dn pn qn
The number of blocks n (1 β€ n β€ 1000) is given on the first line. The next n lines give the i-th block orientation di (1 or 2), the block length pi (1 β€ pi β€ 5), and the block position qi. Block orientation di is 1 for landscape orientation and 2 for portrait orientation. The block position qi is an integer from 1 to 5 from the left edge on the board, and in the case of a horizontal block, it is the position where the left edge frame falls.
The number of datasets does not exceed 20.
Output
The number of frames occupied by the last remaining block for each data set is output on one line.
Example
Input
4
1 4 1
1 3 1
2 2 4
2 3 5
1
1 5 1
7
2 2 2
1 4 1
2 1 3
1 4 1
1 1 1
2 5 5
1 4 2
0
Output
2
0
6 | instruction | 0 | 49,530 | 15 | 99,060 |
"Correct Solution:
```
while 1:
N = int(input())
if N == 0:
break
L = 5000
MP = [[0]*L for i in range(5)]
cs = [L]*5
us = []
U = [0]*L
for i in range(N):
d, p, q = map(int, input().split()); q -= 1
if d == 1:
y = L
for j in range(p):
y = min(y, cs[q+j])
y -= 1
for j in range(p):
MP[q+j][y] = 1
cs[q+j] = y
if all(MP[j][y] for j in range(5)):
us.append(y)
else:
y = cs[q] - p
for j in range(p):
MP[q][y+j] = 1
cs[q] = y
for j in range(p):
if all(MP[k][y+j] for k in range(5)):
us.append(y+j)
if us:
y0 = us[-1]
for y in us:
U[y] = 1
for j in range(5):
cur = y0
for k in range(y0, cs[j]-1, -1):
if U[k]:
continue
MP[j][cur] = MP[j][k]
cur -= 1
for k in range(cur, cs[j]-1, -1):
MP[j][k] = 0
while cur < L and MP[j][cur] == 0:
cur += 1
cs[j] = cur
for y in us:
U[y] = 0
us = []
ans = 0
for j in range(5):
for k in range(cs[j], L):
ans += MP[j][k]
print(ans)
``` | output | 1 | 49,530 | 15 | 99,061 |
Provide a correct Python 3 solution for this coding contest problem.
Tetris is a game in which falling blocks are lined up on the board and erased. Here, let's consider a game that arranges it a little.
The size of the board of this game is 5 frames wide, and it is high enough to accommodate all the blocks that appear. The falling blocks are straight and come in two types, landscape and portrait, with five lengths from 1 to 5.
An example is shown below. The figure from Step (a) to Step (e) shows how the blocks fall and disappear. From Step (a), proceed in order of Step (b) and Step (c).
When dropping a block, if somewhere in the block is caught in a block piled up like Step (a), the dropped block like Step (b) will stop at that place. Also, as a result of dropping a block, if all the frames in a horizontal line on the board are clogged with blocks, the blocks in that line will disappear as shown in Step (d). After this, the block above the disappeared line will move down one line as it is (Step (e)).
<image>
In one game, up to 1000 blocks will be dropped in order. For example, the lengths of the falling blocks are 4 frames horizontally, 3 frames horizontally, 2 frames vertically, and 3 frames vertically, and the falling locations are the 1st, 1st, 4th, and 5th frames from the left end. If there is, it will drop as shown in Step (a) to (g) in the figure below, and the last remaining block will be 2 frames.
<image>
Create a program that inputs the information of the blocks that fall in order and outputs the number of frames remaining when all the blocks fall.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
d1 p1 q1
d2 p2 q2
::
dn pn qn
The number of blocks n (1 β€ n β€ 1000) is given on the first line. The next n lines give the i-th block orientation di (1 or 2), the block length pi (1 β€ pi β€ 5), and the block position qi. Block orientation di is 1 for landscape orientation and 2 for portrait orientation. The block position qi is an integer from 1 to 5 from the left edge on the board, and in the case of a horizontal block, it is the position where the left edge frame falls.
The number of datasets does not exceed 20.
Output
The number of frames occupied by the last remaining block for each data set is output on one line.
Example
Input
4
1 4 1
1 3 1
2 2 4
2 3 5
1
1 5 1
7
2 2 2
1 4 1
2 1 3
1 4 1
1 1 1
2 5 5
1 4 2
0
Output
2
0
6 | instruction | 0 | 49,531 | 15 | 99,062 |
"Correct Solution:
```
def v_block():
block.insert(0, [' ']*5)
for l, line in enumerate(block):
if line[q-1:q+p-1] != [' ']*p:
break
else:
l += 1
block[l-1][q-1:q+p-1] = ['*']*p
return delete()
def h_block():
for _p in range(p):
block.insert(0, [' ']*5)
for l, line in enumerate(block):
if line[q-1] == '*':
break
else:
l += 1
for _l in range(l-1, l-p-1, -1):
block[_l][q-1] = '*'
return delete()
def delete():
l = 0
while True:
if block[l] == [' ']*5 or block[l] == ['*']*5:
del block[l]
else:
l += 1
if len(block) == l:
return
def count():
x = 0
for line in block:
for b in line:
if b == '*':
x += 1
else:
print(x)
while True:
n = int(input())
if n == 0:
break
block = [[' ']*5]
for i in range(n):
d, p, q = map(int, input().split())
if d == 1:
v_block()
else:
h_block()
else:
count()
``` | output | 1 | 49,531 | 15 | 99,063 |
Provide a correct Python 3 solution for this coding contest problem.
Tetris is a game in which falling blocks are lined up on the board and erased. Here, let's consider a game that arranges it a little.
The size of the board of this game is 5 frames wide, and it is high enough to accommodate all the blocks that appear. The falling blocks are straight and come in two types, landscape and portrait, with five lengths from 1 to 5.
An example is shown below. The figure from Step (a) to Step (e) shows how the blocks fall and disappear. From Step (a), proceed in order of Step (b) and Step (c).
When dropping a block, if somewhere in the block is caught in a block piled up like Step (a), the dropped block like Step (b) will stop at that place. Also, as a result of dropping a block, if all the frames in a horizontal line on the board are clogged with blocks, the blocks in that line will disappear as shown in Step (d). After this, the block above the disappeared line will move down one line as it is (Step (e)).
<image>
In one game, up to 1000 blocks will be dropped in order. For example, the lengths of the falling blocks are 4 frames horizontally, 3 frames horizontally, 2 frames vertically, and 3 frames vertically, and the falling locations are the 1st, 1st, 4th, and 5th frames from the left end. If there is, it will drop as shown in Step (a) to (g) in the figure below, and the last remaining block will be 2 frames.
<image>
Create a program that inputs the information of the blocks that fall in order and outputs the number of frames remaining when all the blocks fall.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
d1 p1 q1
d2 p2 q2
::
dn pn qn
The number of blocks n (1 β€ n β€ 1000) is given on the first line. The next n lines give the i-th block orientation di (1 or 2), the block length pi (1 β€ pi β€ 5), and the block position qi. Block orientation di is 1 for landscape orientation and 2 for portrait orientation. The block position qi is an integer from 1 to 5 from the left edge on the board, and in the case of a horizontal block, it is the position where the left edge frame falls.
The number of datasets does not exceed 20.
Output
The number of frames occupied by the last remaining block for each data set is output on one line.
Example
Input
4
1 4 1
1 3 1
2 2 4
2 3 5
1
1 5 1
7
2 2 2
1 4 1
2 1 3
1 4 1
1 1 1
2 5 5
1 4 2
0
Output
2
0
6 | instruction | 0 | 49,532 | 15 | 99,064 |
"Correct Solution:
```
end = [1,1,1,1,1]
while True:
n = int(input())
if n == 0:
break
mp = [[0] * 5 for _ in range(6000)]
height = [0] * 5
cnt = 0
for i in range(n):
d, p, q = map(int, input().split())
q -= 1
cnt += p
if d == 1:
pos = max(height[q:q + p])
mp[pos][q:q + p] = [1] * p
if mp[pos] == end:
cnt -= 5
mp.pop(pos)
mp.append([0] * 5)
for x in range(5):
for y in range(i * 5 - 1, -1, -1):
if mp[y][x] == 1:
height[x] = y + 1
break
else:
height[x] = 0
else:
height[q:q + p] = [pos + 1] * p
if d == 2:
pop_flag = False
pos = height[q]
for y in range(pos, pos + p):
mp[y][q] = 1
for y in range(pos + p - 1, pos - 1, -1):
if mp[y] == end:
cnt -= 5
mp.pop(y)
mp.append([0] * 5)
pop_flag = True
if pop_flag:
for x in range(5):
for y in range(i * 5 - 1, -1, -1):
if mp[y][x] == 1:
height[x] = y + 1
break
else:
height[x] = 0
else:
height[q] += p
print(cnt)
``` | output | 1 | 49,532 | 15 | 99,065 |
Provide a correct Python 3 solution for this coding contest problem.
Tetris is a game in which falling blocks are lined up on the board and erased. Here, let's consider a game that arranges it a little.
The size of the board of this game is 5 frames wide, and it is high enough to accommodate all the blocks that appear. The falling blocks are straight and come in two types, landscape and portrait, with five lengths from 1 to 5.
An example is shown below. The figure from Step (a) to Step (e) shows how the blocks fall and disappear. From Step (a), proceed in order of Step (b) and Step (c).
When dropping a block, if somewhere in the block is caught in a block piled up like Step (a), the dropped block like Step (b) will stop at that place. Also, as a result of dropping a block, if all the frames in a horizontal line on the board are clogged with blocks, the blocks in that line will disappear as shown in Step (d). After this, the block above the disappeared line will move down one line as it is (Step (e)).
<image>
In one game, up to 1000 blocks will be dropped in order. For example, the lengths of the falling blocks are 4 frames horizontally, 3 frames horizontally, 2 frames vertically, and 3 frames vertically, and the falling locations are the 1st, 1st, 4th, and 5th frames from the left end. If there is, it will drop as shown in Step (a) to (g) in the figure below, and the last remaining block will be 2 frames.
<image>
Create a program that inputs the information of the blocks that fall in order and outputs the number of frames remaining when all the blocks fall.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
d1 p1 q1
d2 p2 q2
::
dn pn qn
The number of blocks n (1 β€ n β€ 1000) is given on the first line. The next n lines give the i-th block orientation di (1 or 2), the block length pi (1 β€ pi β€ 5), and the block position qi. Block orientation di is 1 for landscape orientation and 2 for portrait orientation. The block position qi is an integer from 1 to 5 from the left edge on the board, and in the case of a horizontal block, it is the position where the left edge frame falls.
The number of datasets does not exceed 20.
Output
The number of frames occupied by the last remaining block for each data set is output on one line.
Example
Input
4
1 4 1
1 3 1
2 2 4
2 3 5
1
1 5 1
7
2 2 2
1 4 1
2 1 3
1 4 1
1 1 1
2 5 5
1 4 2
0
Output
2
0
6 | instruction | 0 | 49,533 | 15 | 99,066 |
"Correct Solution:
```
while True:
N = int(input())
if N == 0:
break
table = [[0 for i in range(5)] for j in range(6000)]
blocks = [0 for i in range(5)]
for l in range(N):
d,p,q = [int(i) for i in input().split()]
q -= 1
if d == 1:
maxtop = 0
for i in range(q, q+p):
cnt = 0
t = 0
if blocks[i] > 0:
for j in range(5000):
cnt += table[j][i]
if cnt == blocks[i]:
t = j+1
break
maxtop = max(maxtop, t)
for i in range(q, q+p):
table[maxtop][i] = 1
blocks[i] += 1
if table[maxtop][0]*table[maxtop][1]*table[maxtop][2]*table[maxtop][3]*table[maxtop][4] == 1:
del table[maxtop]
for i in range(5):
blocks[i] -= 1
else:
maxtop = 0
cnt = 0
if blocks[q] > 0:
t = 0
for i in range(5000):
cnt += table[i][q]
if cnt == blocks[q]:
t = i + 1
break
maxtop = t
for i in range(p):
table[maxtop + i][q] = 1
blocks[q] += p
for i in range(p-1, -1, -1):
k = maxtop + i
if table[k][0]*table[k][1]*table[k][2]*table[k][3]*table[k][4] == 1:
del table[k]
for j in range(5):
blocks[j] -= 1
ans = 0
# for i in range(9,-1,-1):
# for j in range(5):
# print(table[i][j], end = "")
# print("")
for i in range(5000):
for j in range(5):
ans += table[i][j]
print(ans)
``` | output | 1 | 49,533 | 15 | 99,067 |
Provide a correct Python 3 solution for this coding contest problem.
Tetris is a game in which falling blocks are lined up on the board and erased. Here, let's consider a game that arranges it a little.
The size of the board of this game is 5 frames wide, and it is high enough to accommodate all the blocks that appear. The falling blocks are straight and come in two types, landscape and portrait, with five lengths from 1 to 5.
An example is shown below. The figure from Step (a) to Step (e) shows how the blocks fall and disappear. From Step (a), proceed in order of Step (b) and Step (c).
When dropping a block, if somewhere in the block is caught in a block piled up like Step (a), the dropped block like Step (b) will stop at that place. Also, as a result of dropping a block, if all the frames in a horizontal line on the board are clogged with blocks, the blocks in that line will disappear as shown in Step (d). After this, the block above the disappeared line will move down one line as it is (Step (e)).
<image>
In one game, up to 1000 blocks will be dropped in order. For example, the lengths of the falling blocks are 4 frames horizontally, 3 frames horizontally, 2 frames vertically, and 3 frames vertically, and the falling locations are the 1st, 1st, 4th, and 5th frames from the left end. If there is, it will drop as shown in Step (a) to (g) in the figure below, and the last remaining block will be 2 frames.
<image>
Create a program that inputs the information of the blocks that fall in order and outputs the number of frames remaining when all the blocks fall.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
d1 p1 q1
d2 p2 q2
::
dn pn qn
The number of blocks n (1 β€ n β€ 1000) is given on the first line. The next n lines give the i-th block orientation di (1 or 2), the block length pi (1 β€ pi β€ 5), and the block position qi. Block orientation di is 1 for landscape orientation and 2 for portrait orientation. The block position qi is an integer from 1 to 5 from the left edge on the board, and in the case of a horizontal block, it is the position where the left edge frame falls.
The number of datasets does not exceed 20.
Output
The number of frames occupied by the last remaining block for each data set is output on one line.
Example
Input
4
1 4 1
1 3 1
2 2 4
2 3 5
1
1 5 1
7
2 2 2
1 4 1
2 1 3
1 4 1
1 1 1
2 5 5
1 4 2
0
Output
2
0
6 | instruction | 0 | 49,534 | 15 | 99,068 |
"Correct Solution:
```
def solve():
from bisect import bisect_left
from sys import stdin
f_i = stdin
while True:
n = int(f_i.readline())
if n == 0:
break
b = [[0] for i in range(5)]
for i in range(n):
d, p, q = map(int, f_i.readline().split())
q -= 1
if d == 1:
if p == 5:
continue
h = 0
for c in b[q:q+p]:
if c[-1] > h:
h = c[-1]
h += 1
for c in b[:q] + b[q+p:]:
if h not in c:
for c in b[q:q+p]:
c.append(h)
break
else:
for c in b[:q] + b[q+p:]:
idx = bisect_left(c, h)
c.pop(idx)
for j in range(idx, len(c)):
c[j] -= 1
else:
h = b[q][-1] + 1
b[q] += list(range(h, h + p))
diff = 0
for x in range(h, h + p):
x += diff
for c in b[:q] + b[q+1:]:
if x not in c:
break
else:
for c in b:
idx = bisect_left(c, x)
c.pop(idx)
for j in range(idx, len(c)):
c[j] -= 1
diff -= 1
print(sum(map(len, b)) - 5)
solve()
``` | output | 1 | 49,534 | 15 | 99,069 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers b and w. You have a chessboard of size 10^9 Γ 10^9 with the top left cell at (1; 1), the cell (1; 1) is painted white.
Your task is to find a connected component on this chessboard that contains exactly b black cells and exactly w white cells. Two cells are called connected if they share a side (i.e. for the cell (x, y) there are at most four connected cells: (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)). A set of cells is called a connected component if for every pair of cells C_1 and C_2 from this set, there exists a sequence of cells c_1, c_2, ..., c_k such that c_1 = C_1, c_k = C_2, all c_i from 1 to k are belong to this set of cells and for every i β [1, k - 1], cells c_i and c_{i + 1} are connected.
Obviously, it can be impossible to find such component. In this case print "NO". Otherwise, print "YES" and any suitable connected component.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 10^5) β the number of queries. Then q queries follow.
The only line of the query contains two integers b and w (1 β€ b, w β€ 10^5) β the number of black cells required and the number of white cells required.
It is guaranteed that the sum of numbers of cells does not exceed 2 β
10^5 (β w + β b β€ 2 β
10^5).
Output
For each query, print the answer to it.
If it is impossible to find the required component, print "NO" on the first line.
Otherwise, print "YES" on the first line. In the next b + w lines print coordinates of cells of your component in any order. There should be exactly b black cells and w white cells in your answer. The printed component should be connected.
If there are several answers, you can print any. All coordinates in the answer should be in the range [1; 10^9].
Example
Input
3
1 1
1 4
2 5
Output
YES
2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5 | instruction | 0 | 49,719 | 15 | 99,438 |
Tags: constructive algorithms, implementation
Correct Solution:
```
def col(x, y):
return (x + y) % 2 == 0
t = int(input())
for z in range(t):
w, b = map(int, input().split())
if b < w:
sx = 3
sy = 2
else:
sx = 2
sy = 2
x = sx
y = sy
if max(b, w) > min(b, w) * 2 + 1 + min(b, w):
print('NO')
else:
print('YES')
mn = min(w, b)
mx = max(w, b)
for i in range(mn):
c = col(x, y)
if c:
b -= 1
else:
w -= 1
print(x, y)
y += 1
c = col(x, y)
if c:
b -= 1
else:
w -= 1
print(x, y)
y += 1
#print(w, b, '???')
if not w and not b:
continue
c = col(x, y)
if c:
b -= 1
else:
w -= 1
print(x, y)
mx = max(b, w)
x = sx
y = sy + 1
#print(w, b, '!!!')
while mx > 0:
if mx > 1:
print(x + 1, y)
print(x - 1, y)
y += 2
mx -= 2
else:
print(x + 1, y)
mx -= 1
``` | output | 1 | 49,719 | 15 | 99,439 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers b and w. You have a chessboard of size 10^9 Γ 10^9 with the top left cell at (1; 1), the cell (1; 1) is painted white.
Your task is to find a connected component on this chessboard that contains exactly b black cells and exactly w white cells. Two cells are called connected if they share a side (i.e. for the cell (x, y) there are at most four connected cells: (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)). A set of cells is called a connected component if for every pair of cells C_1 and C_2 from this set, there exists a sequence of cells c_1, c_2, ..., c_k such that c_1 = C_1, c_k = C_2, all c_i from 1 to k are belong to this set of cells and for every i β [1, k - 1], cells c_i and c_{i + 1} are connected.
Obviously, it can be impossible to find such component. In this case print "NO". Otherwise, print "YES" and any suitable connected component.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 10^5) β the number of queries. Then q queries follow.
The only line of the query contains two integers b and w (1 β€ b, w β€ 10^5) β the number of black cells required and the number of white cells required.
It is guaranteed that the sum of numbers of cells does not exceed 2 β
10^5 (β w + β b β€ 2 β
10^5).
Output
For each query, print the answer to it.
If it is impossible to find the required component, print "NO" on the first line.
Otherwise, print "YES" on the first line. In the next b + w lines print coordinates of cells of your component in any order. There should be exactly b black cells and w white cells in your answer. The printed component should be connected.
If there are several answers, you can print any. All coordinates in the answer should be in the range [1; 10^9].
Example
Input
3
1 1
1 4
2 5
Output
YES
2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5 | instruction | 0 | 49,720 | 15 | 99,440 |
Tags: constructive algorithms, implementation
Correct Solution:
```
for _ in range(int(input())):
b, w = map(int, input().split())
if b <= w:
if w > 3 * b + 1:
print('NO')
else:
print('YES')
x = 2
y = 3
for i in range(b):
print(x + 2 * i, y)
for j in range(min(w, b+1)):
print(x - 1 + 2 * j, y)
if 2 * b + 1 >= w > b + 1:
for j in range(min(w - b-1, b)):
print(x + 2 * j, y - 1)
if 3 * b + 1 >= w > 2 * b + 1:
for j in range(b):
print(x + 2 * j, y - 1)
for j in range(w - 2*b - 1):
print(x + 2 * j, y + 1)
else:
if b > 3 * w + 1:
print('NO')
else:
print('YES')
x = 2
y = 2
for i in range(w):
print(x + 2 * i, y)
for j in range(min(b, w + 1)):
print(x - 1 + 2 * j, y)
if 2 * w + 1 >= b > w + 1:
for j in range(min(b - w - 1, w)):
print(x + 2 * j, y - 1)
if 3 * w + 1 >= b > 2 * w + 1:
for j in range(w):
print(x + 2 * j, y - 1)
for j in range(b - 2 * w - 1):
print(x + 2 * j, y + 1)
``` | output | 1 | 49,720 | 15 | 99,441 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers b and w. You have a chessboard of size 10^9 Γ 10^9 with the top left cell at (1; 1), the cell (1; 1) is painted white.
Your task is to find a connected component on this chessboard that contains exactly b black cells and exactly w white cells. Two cells are called connected if they share a side (i.e. for the cell (x, y) there are at most four connected cells: (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)). A set of cells is called a connected component if for every pair of cells C_1 and C_2 from this set, there exists a sequence of cells c_1, c_2, ..., c_k such that c_1 = C_1, c_k = C_2, all c_i from 1 to k are belong to this set of cells and for every i β [1, k - 1], cells c_i and c_{i + 1} are connected.
Obviously, it can be impossible to find such component. In this case print "NO". Otherwise, print "YES" and any suitable connected component.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 10^5) β the number of queries. Then q queries follow.
The only line of the query contains two integers b and w (1 β€ b, w β€ 10^5) β the number of black cells required and the number of white cells required.
It is guaranteed that the sum of numbers of cells does not exceed 2 β
10^5 (β w + β b β€ 2 β
10^5).
Output
For each query, print the answer to it.
If it is impossible to find the required component, print "NO" on the first line.
Otherwise, print "YES" on the first line. In the next b + w lines print coordinates of cells of your component in any order. There should be exactly b black cells and w white cells in your answer. The printed component should be connected.
If there are several answers, you can print any. All coordinates in the answer should be in the range [1; 10^9].
Example
Input
3
1 1
1 4
2 5
Output
YES
2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5 | instruction | 0 | 49,721 | 15 | 99,442 |
Tags: constructive algorithms, implementation
Correct Solution:
```
import sys
def main():
# a, b = [int(s) for s in sys.stdin.readline().strip().split()]
N = int(sys.stdin.readline().strip())
for _ in range(N):
(b, w) = [int(x) for x in sys.stdin.readline().strip().split()]
x = 2
y = 2
if b < w:
b, w = w, b
x += 1
if b > w * 3 + 1:
print('NO')
continue
print('YES')
for i in range(w):
print(x + i * 2, y)
print(x + 1 + i * 2, y)
b -= w
up = min(b, w)
for i in range(up):
print(x + i * 2, y - 1)
b -= up
down = min(b, w)
for i in range(down):
print(x + i * 2, y + 1)
b -= down
if b:
assert(b == 1)
print(x - 1, y)
main()
``` | output | 1 | 49,721 | 15 | 99,443 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers b and w. You have a chessboard of size 10^9 Γ 10^9 with the top left cell at (1; 1), the cell (1; 1) is painted white.
Your task is to find a connected component on this chessboard that contains exactly b black cells and exactly w white cells. Two cells are called connected if they share a side (i.e. for the cell (x, y) there are at most four connected cells: (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)). A set of cells is called a connected component if for every pair of cells C_1 and C_2 from this set, there exists a sequence of cells c_1, c_2, ..., c_k such that c_1 = C_1, c_k = C_2, all c_i from 1 to k are belong to this set of cells and for every i β [1, k - 1], cells c_i and c_{i + 1} are connected.
Obviously, it can be impossible to find such component. In this case print "NO". Otherwise, print "YES" and any suitable connected component.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 10^5) β the number of queries. Then q queries follow.
The only line of the query contains two integers b and w (1 β€ b, w β€ 10^5) β the number of black cells required and the number of white cells required.
It is guaranteed that the sum of numbers of cells does not exceed 2 β
10^5 (β w + β b β€ 2 β
10^5).
Output
For each query, print the answer to it.
If it is impossible to find the required component, print "NO" on the first line.
Otherwise, print "YES" on the first line. In the next b + w lines print coordinates of cells of your component in any order. There should be exactly b black cells and w white cells in your answer. The printed component should be connected.
If there are several answers, you can print any. All coordinates in the answer should be in the range [1; 10^9].
Example
Input
3
1 1
1 4
2 5
Output
YES
2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5 | instruction | 0 | 49,722 | 15 | 99,444 |
Tags: constructive algorithms, implementation
Correct Solution:
```
import sys
input = lambda: sys.stdin.readline().strip()
t = int(input())
for i in range(t):
b, w = map(int, input().split())
if w==b:
print("YES")
for i in range(1, 2*b+1):
print(2, i)
elif 3*b+1>=w>b:
print("YES")
for i in range(3, 2*b+2):
print(2, i)
target = w-b+1
for i in range(1, b+1):
if target!=0:
print(1, 2*i+1)
target-=1
else: break
for i in range(1, b+1):
if target!=0:
print(3, 2*i+1)
target-=1
else: break
if target!=0:
print(2, 2)
target-=1
if target!=0:
print(2, 2*b+2)
target-=1
elif 3*w+1>=b>w:
print("YES")
for i in range(2, 2*w+1):
print(2, i)
target = b-w+1
for i in range(1, w+1):
if target!=0:
print(1, 2*i)
target-=1
else: break
for i in range(1, w+1):
if target!=0:
print(3, 2*i)
target-=1
else: break
if target!=0:
print(2, 1)
target-=1
if target!=0:
print(2, 2*w+1)
target-=1
else: print("NO")
``` | output | 1 | 49,722 | 15 | 99,445 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers b and w. You have a chessboard of size 10^9 Γ 10^9 with the top left cell at (1; 1), the cell (1; 1) is painted white.
Your task is to find a connected component on this chessboard that contains exactly b black cells and exactly w white cells. Two cells are called connected if they share a side (i.e. for the cell (x, y) there are at most four connected cells: (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)). A set of cells is called a connected component if for every pair of cells C_1 and C_2 from this set, there exists a sequence of cells c_1, c_2, ..., c_k such that c_1 = C_1, c_k = C_2, all c_i from 1 to k are belong to this set of cells and for every i β [1, k - 1], cells c_i and c_{i + 1} are connected.
Obviously, it can be impossible to find such component. In this case print "NO". Otherwise, print "YES" and any suitable connected component.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 10^5) β the number of queries. Then q queries follow.
The only line of the query contains two integers b and w (1 β€ b, w β€ 10^5) β the number of black cells required and the number of white cells required.
It is guaranteed that the sum of numbers of cells does not exceed 2 β
10^5 (β w + β b β€ 2 β
10^5).
Output
For each query, print the answer to it.
If it is impossible to find the required component, print "NO" on the first line.
Otherwise, print "YES" on the first line. In the next b + w lines print coordinates of cells of your component in any order. There should be exactly b black cells and w white cells in your answer. The printed component should be connected.
If there are several answers, you can print any. All coordinates in the answer should be in the range [1; 10^9].
Example
Input
3
1 1
1 4
2 5
Output
YES
2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5 | instruction | 0 | 49,723 | 15 | 99,446 |
Tags: constructive algorithms, implementation
Correct Solution:
```
from sys import stdin, stdout, exit
from collections import defaultdict
q = int(input())
for it in range(q):
b,w = map(int, stdin.readline().split())
ans = []
if b < w:
for i in range(b):
ans.append((2+2*i, 3))
for i in range(b+1):
if w > 0:
ans.append((1+2*i, 3))
w -= 1
for i in range(b):
if w > 0:
ans.append((2+2*i, 2))
w -= 1
if w > 0:
ans.append((2+2*i, 4))
w -= 1
if w > 0:
stdout.write("NO\n")
continue
else:
for i in range(w):
ans.append((2+2*i, 2))
for i in range(w+1):
if b > 0:
ans.append((1+2*i, 2))
b -= 1
for i in range(w):
if b > 0:
ans.append((2+2*i, 1))
b -= 1
if b > 0:
ans.append((2+2*i, 3))
b -= 1
if b > 0:
stdout.write("NO\n")
continue
stdout.write("YES\n")
for x, y in ans:
stdout.write(str(x) + " " +str(y) + "\n")
``` | output | 1 | 49,723 | 15 | 99,447 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers b and w. You have a chessboard of size 10^9 Γ 10^9 with the top left cell at (1; 1), the cell (1; 1) is painted white.
Your task is to find a connected component on this chessboard that contains exactly b black cells and exactly w white cells. Two cells are called connected if they share a side (i.e. for the cell (x, y) there are at most four connected cells: (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)). A set of cells is called a connected component if for every pair of cells C_1 and C_2 from this set, there exists a sequence of cells c_1, c_2, ..., c_k such that c_1 = C_1, c_k = C_2, all c_i from 1 to k are belong to this set of cells and for every i β [1, k - 1], cells c_i and c_{i + 1} are connected.
Obviously, it can be impossible to find such component. In this case print "NO". Otherwise, print "YES" and any suitable connected component.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 10^5) β the number of queries. Then q queries follow.
The only line of the query contains two integers b and w (1 β€ b, w β€ 10^5) β the number of black cells required and the number of white cells required.
It is guaranteed that the sum of numbers of cells does not exceed 2 β
10^5 (β w + β b β€ 2 β
10^5).
Output
For each query, print the answer to it.
If it is impossible to find the required component, print "NO" on the first line.
Otherwise, print "YES" on the first line. In the next b + w lines print coordinates of cells of your component in any order. There should be exactly b black cells and w white cells in your answer. The printed component should be connected.
If there are several answers, you can print any. All coordinates in the answer should be in the range [1; 10^9].
Example
Input
3
1 1
1 4
2 5
Output
YES
2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5 | instruction | 0 | 49,724 | 15 | 99,448 |
Tags: constructive algorithms, implementation
Correct Solution:
```
import sys
input = sys.stdin.readline
for _ in range(int(input())):
b, w = map(int, input().split())
if min(b, w) * 3 + 1 < max(b, w):
print('NO')
continue
ans = []
x, y = 2, 2
if b < w:
x = 3
while b and w:
ans.append((x, y))
ans.append((x, y-1))
y += 2
b -= 1
w -= 1
y -= 2
rem = max(b, w)
if rem:
ans.append((x, y+1))
rem -= 1
while rem:
ans.append((x-1, y))
rem -= 1
if rem:
ans.append((x+1, y))
rem -= 1
y -= 2
print('YES')
for l in ans:
print(*l)
``` | output | 1 | 49,724 | 15 | 99,449 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers b and w. You have a chessboard of size 10^9 Γ 10^9 with the top left cell at (1; 1), the cell (1; 1) is painted white.
Your task is to find a connected component on this chessboard that contains exactly b black cells and exactly w white cells. Two cells are called connected if they share a side (i.e. for the cell (x, y) there are at most four connected cells: (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)). A set of cells is called a connected component if for every pair of cells C_1 and C_2 from this set, there exists a sequence of cells c_1, c_2, ..., c_k such that c_1 = C_1, c_k = C_2, all c_i from 1 to k are belong to this set of cells and for every i β [1, k - 1], cells c_i and c_{i + 1} are connected.
Obviously, it can be impossible to find such component. In this case print "NO". Otherwise, print "YES" and any suitable connected component.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 10^5) β the number of queries. Then q queries follow.
The only line of the query contains two integers b and w (1 β€ b, w β€ 10^5) β the number of black cells required and the number of white cells required.
It is guaranteed that the sum of numbers of cells does not exceed 2 β
10^5 (β w + β b β€ 2 β
10^5).
Output
For each query, print the answer to it.
If it is impossible to find the required component, print "NO" on the first line.
Otherwise, print "YES" on the first line. In the next b + w lines print coordinates of cells of your component in any order. There should be exactly b black cells and w white cells in your answer. The printed component should be connected.
If there are several answers, you can print any. All coordinates in the answer should be in the range [1; 10^9].
Example
Input
3
1 1
1 4
2 5
Output
YES
2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5 | instruction | 0 | 49,725 | 15 | 99,450 |
Tags: constructive algorithms, implementation
Correct Solution:
```
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
from sys import stdin, stdout
import collections
Q = int(input())
ans = [0]*Q
#s = input()
#N = len(s)
#arr = [int(x) for x in stdin.readline().split()]
for i in range(Q):
B,W = [int(x) for x in stdin.readline().split()]
mini = (B-1)//3 + 1*((B-1)%3!=0)
maxi = 3*B+1
#print(mini,maxi)
if W<mini or W>maxi:
print('NO')
else:
print('YES')
b = B
w = W
if b<w:
for i in range(b):
print(2,2*i+3)
tmp = w
for i in range(b+1):
if tmp>0:
print(2,2*i+2)
tmp -= 1
else:
break
for i in range(b):
if tmp>0:
print(1,2*i+3)
tmp -= 1
else:
break
if tmp>0:
print(3,2*i+3)
tmp -= 1
else:
break
elif b==w:
for i in range(b):
print(2,2*i+1)
print(2,2*i+2)
elif b>w:
for i in range(w):
print(2,2*i+2)
tmp = b
for i in range(w+1):
if tmp>0:
print(2,2*i+1)
tmp -= 1
else:
break
for i in range(w):
if tmp>0:
print(1,2*i+2)
tmp -= 1
else:
break
if tmp>0:
print(3,2*i+2)
tmp -= 1
else:
break
``` | output | 1 | 49,725 | 15 | 99,451 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers b and w. You have a chessboard of size 10^9 Γ 10^9 with the top left cell at (1; 1), the cell (1; 1) is painted white.
Your task is to find a connected component on this chessboard that contains exactly b black cells and exactly w white cells. Two cells are called connected if they share a side (i.e. for the cell (x, y) there are at most four connected cells: (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)). A set of cells is called a connected component if for every pair of cells C_1 and C_2 from this set, there exists a sequence of cells c_1, c_2, ..., c_k such that c_1 = C_1, c_k = C_2, all c_i from 1 to k are belong to this set of cells and for every i β [1, k - 1], cells c_i and c_{i + 1} are connected.
Obviously, it can be impossible to find such component. In this case print "NO". Otherwise, print "YES" and any suitable connected component.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 10^5) β the number of queries. Then q queries follow.
The only line of the query contains two integers b and w (1 β€ b, w β€ 10^5) β the number of black cells required and the number of white cells required.
It is guaranteed that the sum of numbers of cells does not exceed 2 β
10^5 (β w + β b β€ 2 β
10^5).
Output
For each query, print the answer to it.
If it is impossible to find the required component, print "NO" on the first line.
Otherwise, print "YES" on the first line. In the next b + w lines print coordinates of cells of your component in any order. There should be exactly b black cells and w white cells in your answer. The printed component should be connected.
If there are several answers, you can print any. All coordinates in the answer should be in the range [1; 10^9].
Example
Input
3
1 1
1 4
2 5
Output
YES
2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5 | instruction | 0 | 49,726 | 15 | 99,452 |
Tags: constructive algorithms, implementation
Correct Solution:
```
''' CODED WITH LOVE BY SATYAM KUMAR '''
from sys import stdin, stdout
import cProfile, math
from collections import Counter,defaultdict,deque
from bisect import bisect_left,bisect,bisect_right
import itertools
from copy import deepcopy
from fractions import Fraction
import sys, threading
import operator as op
from functools import reduce
sys.setrecursionlimit(10**6) # max depth of recursion
threading.stack_size(2**27) # new thread will get stack of such size
fac_warmup = False
printHeap = str()
memory_constrained = False
P = 10**9+7
import sys
class merge_find:
def __init__(self,n):
self.parent = list(range(n))
self.size = [1]*n
self.num_sets = n
self.lista = [[_] for _ in range(n)]
def find(self,a):
to_update = []
while a != self.parent[a]:
to_update.append(a)
a = self.parent[a]
for b in to_update:
self.parent[b] = a
return self.parent[a]
def merge(self,a,b):
a = self.find(a)
b = self.find(b)
if a==b:
return
if self.size[a]<self.size[b]:
a,b = b,a
self.num_sets -= 1
self.parent[b] = a
self.size[a] += self.size[b]
self.lista[a] += self.lista[b]
def set_size(self, a):
return self.size[self.find(a)]
def __len__(self):
return self.num_sets
def display(string_to_print):
stdout.write(str(string_to_print) + "\n")
def primeFactors(n): #n**0.5 complex
factors = dict()
for i in range(2,math.ceil(math.sqrt(n))+1):
while n % i== 0:
if i in factors:
factors[i]+=1
else: factors[i]=1
n = n // i
if n>2:
factors[n]=1
return (factors)
def all_factors(n):
return set(reduce(list.__add__,
([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))
def fibonacci_modP(n,MOD):
if n<2: return 1
#print (n,MOD)
return (cached_fn(fibonacci_modP, (n+1)//2, MOD)*cached_fn(fibonacci_modP, n//2, MOD) + cached_fn(fibonacci_modP, (n-1) // 2, MOD)*cached_fn(fibonacci_modP, (n-2) // 2, MOD)) % MOD
def factorial_modP_Wilson(n , p):
if (p <= n):
return 0
res = (p - 1)
for i in range (n + 1, p):
res = (res * cached_fn(InverseEuler,i, p)) % p
return res
def binary(n,digits = 20):
b = bin(n)[2:]
b = '0'*(digits-len(b))+b
return b
def isprime(n):
"""Returns True if n is prime."""
if n < 4:
return True
if n % 2 == 0:
return False
if n % 3 == 0:
return False
i = 5
w = 2
while i * i <= n:
if n % i == 0:
return False
i += w
w = 6 - w
return True
def generate_primes(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * 2, n+1, p):
prime[i] = False
p += 1
return prime
factorial_modP = []
def warm_up_fac(MOD):
global factorial_modP,fac_warmup
if fac_warmup: return
factorial_modP= [1 for _ in range(fac_warmup_size+1)]
for i in range(2,fac_warmup_size):
factorial_modP[i]= (factorial_modP[i-1]*i) % MOD
fac_warmup = True
def InverseEuler(n,MOD):
return pow(n,MOD-2,MOD)
def nCr(n, r, MOD):
global fac_warmup,factorial_modP
if not fac_warmup:
warm_up_fac(MOD)
fac_warmup = True
return (factorial_modP[n]*((pow(factorial_modP[r], MOD-2, MOD) * pow(factorial_modP[n-r], MOD-2, MOD)) % MOD)) % MOD
def test_print(*args):
if testingMode:
print(args)
def display_list(list1, sep=" "):
stdout.write(sep.join(map(str, list1)) + "\n")
def display_2D_list(li):
for i in li:
print(i)
def prefix_sum(li):
sm = 0
res = []
for i in li:
sm+=i
res.append(sm)
return res
def get_int():
return int(stdin.readline().strip())
def get_tuple():
return map(int, stdin.readline().split())
def get_list():
return list(map(int, stdin.readline().split()))
import heapq,itertools
pq = [] # list of entries arranged in a heap
entry_finder = {} # mapping of tasks to entries
REMOVED = '<removed-task>'
def add_task(task, priority=0):
'Add a new task or update the priority of an existing task'
if task in entry_finder:
remove_task(task)
count = next(counter)
entry = [priority, count, task]
entry_finder[task] = entry
heapq.heappush(pq, entry)
def remove_task(task):
'Mark an existing task as REMOVED. Raise KeyError if not found.'
entry = entry_finder.pop(task)
entry[-1] = REMOVED
def pop_task():
'Remove and return the lowest priority task. Raise KeyError if empty.'
while pq:
priority, count, task = heapq.heappop(pq)
if task is not REMOVED:
del entry_finder[task]
return task
raise KeyError('pop from an empty priority queue')
memory = dict()
def clear_cache():
global memory
memory = dict()
def cached_fn(fn, *args):
global memory
if args in memory:
return memory[args]
else:
result = fn(*args)
memory[args] = result
return result
def ncr (n,r):
return math.factorial(n)/(math.factorial(n-r)*math.factorial(r))
def binary_serach(i,li):
#print("Search for ",i)
fn = lambda x: li[x]-x//i
x = -1
b = len(li)
while b>=1:
#print(b,x)
while b+x<len(li) and fn(b+x)>0: #Change this condition 2 to whatever you like
x+=b
b=b//2
return x
# -------------------------------------------------------------- MAIN PROGRAM
TestCases = True
testingMode = False
fac_warmup_size = 10**5+100
optimiseForReccursion = False #Can not be used clubbed with TestCases # WHen using recursive functions, use Python 3
from math import factorial
def main():
b, w = get_tuple()
maxi,mini = max(b,w), min(b,w)
points = []
if maxi<=mini*3 + 1:
print("YES")
if w>b:
z = 0
else:
z = 1
for i in range(2*mini):
points.append([2+i+z,2])
rem = maxi-mini
if rem!=0:
points.append([3+z+i,2])
rem-=1
for j in range(3+z, 3+i+z+1, 2):
if rem>0:
points.append([j,1])
rem-=1
if rem>0:
points.append([j,3])
rem-=1
for i in points:
display_list(i)
else:
print("NO")
# --------------------------------------------------------------------- END=
if TestCases:
for i in range(get_int()):
cProfile.run('main()') if testingMode else main()
else: (cProfile.run('main()') if testingMode else main()) if not optimiseForReccursion else threading.Thread(target=main).start()
``` | output | 1 | 49,726 | 15 | 99,453 |
Provide tags and a correct Python 2 solution for this coding contest problem.
You are given two integers b and w. You have a chessboard of size 10^9 Γ 10^9 with the top left cell at (1; 1), the cell (1; 1) is painted white.
Your task is to find a connected component on this chessboard that contains exactly b black cells and exactly w white cells. Two cells are called connected if they share a side (i.e. for the cell (x, y) there are at most four connected cells: (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)). A set of cells is called a connected component if for every pair of cells C_1 and C_2 from this set, there exists a sequence of cells c_1, c_2, ..., c_k such that c_1 = C_1, c_k = C_2, all c_i from 1 to k are belong to this set of cells and for every i β [1, k - 1], cells c_i and c_{i + 1} are connected.
Obviously, it can be impossible to find such component. In this case print "NO". Otherwise, print "YES" and any suitable connected component.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 10^5) β the number of queries. Then q queries follow.
The only line of the query contains two integers b and w (1 β€ b, w β€ 10^5) β the number of black cells required and the number of white cells required.
It is guaranteed that the sum of numbers of cells does not exceed 2 β
10^5 (β w + β b β€ 2 β
10^5).
Output
For each query, print the answer to it.
If it is impossible to find the required component, print "NO" on the first line.
Otherwise, print "YES" on the first line. In the next b + w lines print coordinates of cells of your component in any order. There should be exactly b black cells and w white cells in your answer. The printed component should be connected.
If there are several answers, you can print any. All coordinates in the answer should be in the range [1; 10^9].
Example
Input
3
1 1
1 4
2 5
Output
YES
2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5 | instruction | 0 | 49,727 | 15 | 99,454 |
Tags: constructive algorithms, implementation
Correct Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
def in_arr():
return map(int,raw_input().split())
def pr_num(n):
stdout.write(str(n)+'\n')
def pr_arr(arr):
for i in arr:
stdout.write(str(i)+' ')
stdout.write('\n')
range = xrange # not for python 3.0+
for t in range(int(raw_input())):
b,w=in_arr()
d=defaultdict(Counter)
ans=[]
f=0
f1=0
if b<w:
"""
mx=2*b
for i in range(1,2*b,2):
ans.append((2,i))
i=1
j=1
while w:
if j>mx:
i+=1
if i>3:
f=1
break
j=2-j%2
w-=1
ans.append((i,j))
j+=2
"""
f1=1
b,w=w,b
if 1:
mx=(2*w)+1
for i in range(2,(2*w)+1,2):
if f1:
ans.append((3,i))
if i!=(2*w):
ans.append((3,i+1))
d[3][i+1]=1
b-=1
if b<0:
f=1
b=0
break
else:
ans.append((2,i))
if i!=2*w:
ans.append((2,i+1))
b-=1
d[2][i+1]=1
if b<0:
f=1
b=0
break
i=1
j=2
while b:
if j>mx:
i+=1
if i>3:
f=1
break
j=(i%2)+1
pos=i
if f1:
pos+=1
if d[pos][j]:
#print 'a',i,j
j+=2
continue
b-=1
if f1:
ans.append((i+1,j))
else:
ans.append((i,j))
j+=2
if f:
pr('NO\n')
else:
pr('YES\n')
for i in ans:
pr_arr(i)
``` | output | 1 | 49,727 | 15 | 99,455 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two integers b and w. You have a chessboard of size 10^9 Γ 10^9 with the top left cell at (1; 1), the cell (1; 1) is painted white.
Your task is to find a connected component on this chessboard that contains exactly b black cells and exactly w white cells. Two cells are called connected if they share a side (i.e. for the cell (x, y) there are at most four connected cells: (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)). A set of cells is called a connected component if for every pair of cells C_1 and C_2 from this set, there exists a sequence of cells c_1, c_2, ..., c_k such that c_1 = C_1, c_k = C_2, all c_i from 1 to k are belong to this set of cells and for every i β [1, k - 1], cells c_i and c_{i + 1} are connected.
Obviously, it can be impossible to find such component. In this case print "NO". Otherwise, print "YES" and any suitable connected component.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 10^5) β the number of queries. Then q queries follow.
The only line of the query contains two integers b and w (1 β€ b, w β€ 10^5) β the number of black cells required and the number of white cells required.
It is guaranteed that the sum of numbers of cells does not exceed 2 β
10^5 (β w + β b β€ 2 β
10^5).
Output
For each query, print the answer to it.
If it is impossible to find the required component, print "NO" on the first line.
Otherwise, print "YES" on the first line. In the next b + w lines print coordinates of cells of your component in any order. There should be exactly b black cells and w white cells in your answer. The printed component should be connected.
If there are several answers, you can print any. All coordinates in the answer should be in the range [1; 10^9].
Example
Input
3
1 1
1 4
2 5
Output
YES
2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5
Submitted Solution:
```
t = int(input())
for i in range(t):
b,w = map(int,input().split())
if max(b,w)>min(b,w)*3 + 1:
print('NO')
else:
print('YES')
if b>w:
x = 2
y = 2
mini = w
maxi = b
else:
x = 3
y = 2
mini = b
maxi = w
for i in range(mini):
print(x+2*i,y)
print(x+2*i + 1,y)
z = min(mini,maxi-mini)
for i in range(z):
print(x+2*i,y-1)
z1 = min(maxi-(z+mini),mini)
for i in range(min(maxi-(z+mini),mini)):
print(x+2*i,y+1)
if maxi == 3*mini+1:
print(x-1,y)
``` | instruction | 0 | 49,728 | 15 | 99,456 |
Yes | output | 1 | 49,728 | 15 | 99,457 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two integers b and w. You have a chessboard of size 10^9 Γ 10^9 with the top left cell at (1; 1), the cell (1; 1) is painted white.
Your task is to find a connected component on this chessboard that contains exactly b black cells and exactly w white cells. Two cells are called connected if they share a side (i.e. for the cell (x, y) there are at most four connected cells: (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)). A set of cells is called a connected component if for every pair of cells C_1 and C_2 from this set, there exists a sequence of cells c_1, c_2, ..., c_k such that c_1 = C_1, c_k = C_2, all c_i from 1 to k are belong to this set of cells and for every i β [1, k - 1], cells c_i and c_{i + 1} are connected.
Obviously, it can be impossible to find such component. In this case print "NO". Otherwise, print "YES" and any suitable connected component.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 10^5) β the number of queries. Then q queries follow.
The only line of the query contains two integers b and w (1 β€ b, w β€ 10^5) β the number of black cells required and the number of white cells required.
It is guaranteed that the sum of numbers of cells does not exceed 2 β
10^5 (β w + β b β€ 2 β
10^5).
Output
For each query, print the answer to it.
If it is impossible to find the required component, print "NO" on the first line.
Otherwise, print "YES" on the first line. In the next b + w lines print coordinates of cells of your component in any order. There should be exactly b black cells and w white cells in your answer. The printed component should be connected.
If there are several answers, you can print any. All coordinates in the answer should be in the range [1; 10^9].
Example
Input
3
1 1
1 4
2 5
Output
YES
2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5
Submitted Solution:
```
# !/usr/bin/env python3
# encoding: UTF-8
# Modified: <15/Oct/2019 01:02:21 AM>
# βͺ H4WK3yEδΉ‘
# Mohd. Farhan Tahir
# Indian Institute Of Information Technology (IIIT), Gwalior
import sys
import os
from io import IOBase, BytesIO
def main():
for tc in range(int(input())):
b, w = get_ints()
x, y = 3, 2
if b > w:
f = 1 # black
b, w = w, b
x += 1
if w > 3 * b + 1:
print("NO")
continue
ans = [(x - 1, y), (x, y + 1), (x, y - 1)]
right = [(x + 1, y)]
ans1 = [(x, y)]
p = x + 2
for i in range(b - 1):
ans1.append((p, y))
right.append((p + 1, y))
ans.append((p, y - 1))
ans.append((p, y + 1))
p += 2
print("YES")
for i in ans1:
print(*i)
for i in right:
print(*i)
for i in range(w - len(right)):
print(*ans[i])
# print(len(ans), w)
BUFSIZE = 8192
class FastIO(BytesIO):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.writable = "x" in file.mode or "w" in file.mode
self.write = super(FastIO, self).write if self.writable else None
def _fill(self):
s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
return s
def read(self):
while self._fill():
pass
return super(FastIO, self).read()
def readline(self):
while self.newlines == 0:
s = self._fill()
self.newlines = s.count(b"\n") + (not s)
self.newlines -= 1
return super(FastIO, self).readline()
def flush(self):
if self.writable:
os.write(self._fd, self.getvalue())
self.truncate(0), self.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
py2 = round(0.5)
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
if py2 == 1:
self.write = self.buffer.write
self.read = self.buffer.read
self.readline = self.buffer.readline
else:
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")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
def get_array():
return list(map(int, sys.stdin.readline().split()))
def get_ints():
return map(int, sys.stdin.readline().split())
def input():
return sys.stdin.readline().strip()
if __name__ == "__main__":
main()
``` | instruction | 0 | 49,729 | 15 | 99,458 |
Yes | output | 1 | 49,729 | 15 | 99,459 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two integers b and w. You have a chessboard of size 10^9 Γ 10^9 with the top left cell at (1; 1), the cell (1; 1) is painted white.
Your task is to find a connected component on this chessboard that contains exactly b black cells and exactly w white cells. Two cells are called connected if they share a side (i.e. for the cell (x, y) there are at most four connected cells: (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)). A set of cells is called a connected component if for every pair of cells C_1 and C_2 from this set, there exists a sequence of cells c_1, c_2, ..., c_k such that c_1 = C_1, c_k = C_2, all c_i from 1 to k are belong to this set of cells and for every i β [1, k - 1], cells c_i and c_{i + 1} are connected.
Obviously, it can be impossible to find such component. In this case print "NO". Otherwise, print "YES" and any suitable connected component.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 10^5) β the number of queries. Then q queries follow.
The only line of the query contains two integers b and w (1 β€ b, w β€ 10^5) β the number of black cells required and the number of white cells required.
It is guaranteed that the sum of numbers of cells does not exceed 2 β
10^5 (β w + β b β€ 2 β
10^5).
Output
For each query, print the answer to it.
If it is impossible to find the required component, print "NO" on the first line.
Otherwise, print "YES" on the first line. In the next b + w lines print coordinates of cells of your component in any order. There should be exactly b black cells and w white cells in your answer. The printed component should be connected.
If there are several answers, you can print any. All coordinates in the answer should be in the range [1; 10^9].
Example
Input
3
1 1
1 4
2 5
Output
YES
2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5
Submitted Solution:
```
import sys
input = sys.stdin.readline
Q = int(input())
for _ in range(Q):
A, B = map(int, input().split())
if A > 3*B+1 or B > 3*A+1:
print("NO")
elif A == B:
print("YES")
for i in range(A):
print(1, i+1)
print(2, i+1)
elif A > B:
print("YES")
for i in range(B*2+1):
print(2, i+1)
c = A-B-1
a = 0
while c:
a += 2
print(1, a)
c -= 1
if c == 0: break
print(3, a)
c -= 1
elif B > A:
print("YES")
for i in range(A*2+1):
print(3, i+1)
c = B-A-1
a = 0
while c:
a += 2
print(2, a)
c -= 1
if c == 0: break
print(4, a)
c -= 1
``` | instruction | 0 | 49,730 | 15 | 99,460 |
Yes | output | 1 | 49,730 | 15 | 99,461 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two integers b and w. You have a chessboard of size 10^9 Γ 10^9 with the top left cell at (1; 1), the cell (1; 1) is painted white.
Your task is to find a connected component on this chessboard that contains exactly b black cells and exactly w white cells. Two cells are called connected if they share a side (i.e. for the cell (x, y) there are at most four connected cells: (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)). A set of cells is called a connected component if for every pair of cells C_1 and C_2 from this set, there exists a sequence of cells c_1, c_2, ..., c_k such that c_1 = C_1, c_k = C_2, all c_i from 1 to k are belong to this set of cells and for every i β [1, k - 1], cells c_i and c_{i + 1} are connected.
Obviously, it can be impossible to find such component. In this case print "NO". Otherwise, print "YES" and any suitable connected component.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 10^5) β the number of queries. Then q queries follow.
The only line of the query contains two integers b and w (1 β€ b, w β€ 10^5) β the number of black cells required and the number of white cells required.
It is guaranteed that the sum of numbers of cells does not exceed 2 β
10^5 (β w + β b β€ 2 β
10^5).
Output
For each query, print the answer to it.
If it is impossible to find the required component, print "NO" on the first line.
Otherwise, print "YES" on the first line. In the next b + w lines print coordinates of cells of your component in any order. There should be exactly b black cells and w white cells in your answer. The printed component should be connected.
If there are several answers, you can print any. All coordinates in the answer should be in the range [1; 10^9].
Example
Input
3
1 1
1 4
2 5
Output
YES
2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5
Submitted Solution:
```
q = int(input())
for _ in range(q):
b,w = map(int,input().split())
if b == w:
print("YES")
for i in range(b + w):
print("1 " + str(i + 1))
elif b > w:
if b > 3 * w + 1:
print("NO")
else:
print("YES")
b -= (w + 1)
for i in range(w * 2 + 1):
print("2 " + str(i + 1))
cnt = 2
while b > 0:
print("1 " + str(cnt))
b -= 1
if b == 0:
break
print("3 " + str(cnt))
b -= 1
cnt += 2
elif w > b:
if w > 3 * b + 1:
print("NO")
else:
print("YES")
w -= (b + 1)
for i in range(b * 2 + 1):
print("3 " + str(i + 1))
cnt = 2
while w > 0:
print("2 " + str(cnt))
w -= 1
if w == 0:
break
print("4 " + str(cnt))
w -= 1
cnt += 2
``` | instruction | 0 | 49,731 | 15 | 99,462 |
Yes | output | 1 | 49,731 | 15 | 99,463 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two integers b and w. You have a chessboard of size 10^9 Γ 10^9 with the top left cell at (1; 1), the cell (1; 1) is painted white.
Your task is to find a connected component on this chessboard that contains exactly b black cells and exactly w white cells. Two cells are called connected if they share a side (i.e. for the cell (x, y) there are at most four connected cells: (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)). A set of cells is called a connected component if for every pair of cells C_1 and C_2 from this set, there exists a sequence of cells c_1, c_2, ..., c_k such that c_1 = C_1, c_k = C_2, all c_i from 1 to k are belong to this set of cells and for every i β [1, k - 1], cells c_i and c_{i + 1} are connected.
Obviously, it can be impossible to find such component. In this case print "NO". Otherwise, print "YES" and any suitable connected component.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 10^5) β the number of queries. Then q queries follow.
The only line of the query contains two integers b and w (1 β€ b, w β€ 10^5) β the number of black cells required and the number of white cells required.
It is guaranteed that the sum of numbers of cells does not exceed 2 β
10^5 (β w + β b β€ 2 β
10^5).
Output
For each query, print the answer to it.
If it is impossible to find the required component, print "NO" on the first line.
Otherwise, print "YES" on the first line. In the next b + w lines print coordinates of cells of your component in any order. There should be exactly b black cells and w white cells in your answer. The printed component should be connected.
If there are several answers, you can print any. All coordinates in the answer should be in the range [1; 10^9].
Example
Input
3
1 1
1 4
2 5
Output
YES
2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5
Submitted Solution:
```
q = int(input())
ans = [[] for i in range(q)]
for a in range(q):
b, w = map(int, input().split())
coords = [[] for i in range(b+w)]
if b > (3 * w + 1):
print('NO')
continue
if w > (3 * b + 1):
print('NO')
continue
if b == 4 and w == 1:
coords[0].append(2)
coords[1].append(2)
coords[2].append(1)
coords[3].append(3)
coords[4].append(2)
coords[0].append(1)
coords[1].append(3)
coords[2].append(2)
coords[3].append(2)
coords[4].append(2)
if w == 4 and b == 1:
coords[0].append(3)
coords[1].append(3)
coords[2].append(2)
coords[3].append(4)
coords[4].append(3)
coords[0].append(1)
coords[1].append(3)
coords[2].append(2)
coords[3].append(2)
coords[4].append(2)
else:
if w > b:
for i in range(b):
coords[i].append(2)
coords[i].append(i * 2 + 3)
for i in range(b+1):
coords[i+b].append(2)
coords[i+b].append(i * 2 + 2)
if (w - (b + 1)) <= b * 2:
for i in range(w-b-1):
coords[i+b+b+1].append(1)
coords[i+b+b+1].append(i * 2 + 3)
else:
if (w-b-1) % 2 == 0:
for i in range((w-b-1)/2):
coords[i+b+b+1].append(1)
coords[i+b+b+1].append(i * 2 + 3)
for i in range((w-b-1)/2):
coords[i+b+b+1+(w-b-1)/2].append(3)
coords[i+b+b+1+(w-b-1)/2].append(i * 2 + 3)
else:
for i in range((w-b-1)//2):
coords[i+b+b+1].append(1)
coords[i+b+b+1].append(i * 2 + 3)
for i in range((w-b-1)//2+1):
coords[i+b+b+1+(w-b-1)//2].append(3)
coords[i+b+b+1+(w-b-1)//2].append(i * 2 + 3)
if b > w:
for i in range(w):
coords[i].append(2)
coords[i].append(i * 2 + 3)
for i in range(w+1):
coords[i+w].append(2)
coords[i+w].append(i * 2 + 2)
if (b - (w + 1)) <= w * 2:
for i in range(b-w-1):
coords[i+2*w+1].append(1)
coords[i+2*w+1].append(i * 2 + 3)
else:
if (b-w-1) % 2 == 0:
for i in range((b-w-1)/2):
coords[i+2*w+1].append(1)
coords[i+2*w+1].append(i * 2 + 3)
for i in range((b-w-1)/2):
coords[i+2*w+1+(b-w-1)/2].append(3)
coords[i+2*w+1+(b-w-1)/2].append(i * 2 + 3)
else:
for i in range((b-w-1)//2):
coords[i+2*w+1].append(1)
coords[i+2*w+1].append(i * 2 + 3)
for i in range((b-w-1)//2+1):
coords[i+2*w+1+(b-w-1)//2].append(3)
coords[i+2*w+1+(b-w-1)//2].append(i * 2 + 3)
if b == w:
for i in range(b):
coords[i].append(2)
coords[i].append(i * 2 + 1)
for i in range(w):
coords[i+b].append(2)
coords[i+b].append(2 * i + 2)
print('YES')
for i in range(w+b):
print(*coords[i])
``` | instruction | 0 | 49,732 | 15 | 99,464 |
No | output | 1 | 49,732 | 15 | 99,465 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two integers b and w. You have a chessboard of size 10^9 Γ 10^9 with the top left cell at (1; 1), the cell (1; 1) is painted white.
Your task is to find a connected component on this chessboard that contains exactly b black cells and exactly w white cells. Two cells are called connected if they share a side (i.e. for the cell (x, y) there are at most four connected cells: (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)). A set of cells is called a connected component if for every pair of cells C_1 and C_2 from this set, there exists a sequence of cells c_1, c_2, ..., c_k such that c_1 = C_1, c_k = C_2, all c_i from 1 to k are belong to this set of cells and for every i β [1, k - 1], cells c_i and c_{i + 1} are connected.
Obviously, it can be impossible to find such component. In this case print "NO". Otherwise, print "YES" and any suitable connected component.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 10^5) β the number of queries. Then q queries follow.
The only line of the query contains two integers b and w (1 β€ b, w β€ 10^5) β the number of black cells required and the number of white cells required.
It is guaranteed that the sum of numbers of cells does not exceed 2 β
10^5 (β w + β b β€ 2 β
10^5).
Output
For each query, print the answer to it.
If it is impossible to find the required component, print "NO" on the first line.
Otherwise, print "YES" on the first line. In the next b + w lines print coordinates of cells of your component in any order. There should be exactly b black cells and w white cells in your answer. The printed component should be connected.
If there are several answers, you can print any. All coordinates in the answer should be in the range [1; 10^9].
Example
Input
3
1 1
1 4
2 5
Output
YES
2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5
Submitted Solution:
```
q = int(input())
for i in range(q):
b, w = map(int, input().split())
count_b = 0
count_w = 1
if b >= w:
mw = {(1001, 1001)}
not_used_w = {(1001, 1001)}
not_used_b = set()
mb = set()
while True:
if not not_used_w:
if count_w == w:
print('NO1')
exit(0)
x, y = not_used_b.pop()
if x > 1 and (x - 1, y) not in mw:
mw.add((x - 1, y))
not_used_w.add((x - 1, y))
count_w += 1
elif (x + 1, y) not in mw:
mw.add((x + 1, y))
not_used_w.add((x + 1, y))
count_w += 1
elif y > 1 and (x, y - 1) not in mw:
mw.add((x, y - 1))
not_used_w.add((x, y + 1))
count_w += 1
elif (x, y + 1) not in mw:
mw.add((x, y + 1))
not_used_w.add((x, y + 1))
count_w += 1
continue
x, y = not_used_w.pop()
if x > 1 and (x - 1, y) not in mb:
mb.add((x - 1, y))
not_used_b.add((x - 1, y))
count_b += 1
if count_b == b:
break
if (x + 1, y) not in mb:
mb.add((x + 1, y))
not_used_b.add((x + 1, y))
count_b += 1
if count_b == b:
break
if y > 1 and (x, y - 1) not in mb:
mb.add((x, y - 1))
not_used_b.add((x, y + 1))
count_b += 1
if count_b == b:
break
if (x, y + 1) not in mb:
mb.add((x, y + 1))
not_used_b.add((x, y + 1))
count_b += 1
if count_b == b:
break
while count_w != w:
x, y = not_used_b.pop()
if x > 1 and (x - 1, y) not in mw:
mw.add((x - 1, y))
not_used_w.add((x - 1, y))
count_w += 1
if count_w == w:
break
if (x + 1, y) not in mw:
mw.add((x + 1, y))
not_used_w.add((x + 1, y))
count_w += 1
if count_w == w:
break
if y > 1 and (x, y - 1) not in mw:
mw.add((x, y - 1))
not_used_w.add((x, y + 1))
count_w += 1
if count_w == w:
break
elif (x, y + 1) not in mw:
mw.add((x, y + 1))
not_used_w.add((x, y + 1))
count_w += 1
if count_w == w:
break
else:
not_used_w = set()
not_used_b = {(1000, 1000)}
mb = {(1000, 1000)}
mw = set()
while True:
if not not_used_b:
if count_b == b:
print('NO2')
exit(0)
x, y = not_used_w.pop()
if x > 1 and (x - 1, y) not in mb:
mb.add((x - 1, y))
not_used_b.add((x - 1, y))
count_b += 1
elif (x + 1, y) not in mb:
mb.add((x + 1, y))
not_used_b.add((x + 1, y))
count_b += 1
elif y > 1 and (x, y - 1) not in mb:
mb.add((x, y - 1))
not_used_b.add((x, y + 1))
count_b += 1
elif (x, y + 1) not in mb:
mb.add((x, y + 1))
not_used_b.add((x, y + 1))
count_b += 1
continue
x, y = not_used_b.pop()
if x > 1 and (x - 1, y) not in mw:
mw.add((x - 1, y))
not_used_w.add((x - 1, y))
count_w += 1
if count_w == w:
break
if (x + 1, y) not in mw:
mw.add((x + 1, y))
not_used_w.add((x + 1, y))
count_w += 1
if count_w == w:
break
if y > 1 and (x, y - 1) not in mw:
mw.add((x, y - 1))
not_used_w.add((x, y + 1))
count_w += 1
if count_w == w:
break
if (x, y + 1) not in mw:
mw.add((x, y + 1))
not_used_w.add((x, y + 1))
count_w += 1
if count_w == w:
break
while True:
x, y = not_used_w.pop()
if x > 1 and (x - 1, y) not in mb:
mb.add((x - 1, y))
not_used_b.add((x - 1, y))
count_b += 1
if count_b == b:
break
if (x + 1, y) not in mb:
mb.add((x + 1, y))
not_used_b.add((x + 1, y))
count_b += 1
if count_b == b:
break
if y > 1 and (x, y - 1) not in mb:
mb.add((x, y - 1))
not_used_b.add((x, y + 1))
count_b += 1
if count_b == b:
break
elif (x, y + 1) not in mb:
mb.add((x, y + 1))
not_used_b.add((x, y + 1))
count_b += 1
if count_b == b:
break
print('YES')
for el in mb:
print(*el)
for el in mw:
print(*el)
``` | instruction | 0 | 49,733 | 15 | 99,466 |
No | output | 1 | 49,733 | 15 | 99,467 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two integers b and w. You have a chessboard of size 10^9 Γ 10^9 with the top left cell at (1; 1), the cell (1; 1) is painted white.
Your task is to find a connected component on this chessboard that contains exactly b black cells and exactly w white cells. Two cells are called connected if they share a side (i.e. for the cell (x, y) there are at most four connected cells: (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)). A set of cells is called a connected component if for every pair of cells C_1 and C_2 from this set, there exists a sequence of cells c_1, c_2, ..., c_k such that c_1 = C_1, c_k = C_2, all c_i from 1 to k are belong to this set of cells and for every i β [1, k - 1], cells c_i and c_{i + 1} are connected.
Obviously, it can be impossible to find such component. In this case print "NO". Otherwise, print "YES" and any suitable connected component.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 10^5) β the number of queries. Then q queries follow.
The only line of the query contains two integers b and w (1 β€ b, w β€ 10^5) β the number of black cells required and the number of white cells required.
It is guaranteed that the sum of numbers of cells does not exceed 2 β
10^5 (β w + β b β€ 2 β
10^5).
Output
For each query, print the answer to it.
If it is impossible to find the required component, print "NO" on the first line.
Otherwise, print "YES" on the first line. In the next b + w lines print coordinates of cells of your component in any order. There should be exactly b black cells and w white cells in your answer. The printed component should be connected.
If there are several answers, you can print any. All coordinates in the answer should be in the range [1; 10^9].
Example
Input
3
1 1
1 4
2 5
Output
YES
2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5
Submitted Solution:
```
for q in range (int(input())):
b, w = map(int, input().split())
if b <= (3*w)+1 and w <= (3*b)+1:
print("YES")
if w >= b:
for k in range(b):
print(3,2*k+1)
print(3,2*k+2)
extra = w - b
if extra > 0:
print(3,2*b+1)
extra -= 1
pos = 1
while extra > 0:
print(2,2*pos)
extra -= 1
if extra > 0:
print(4,2*pos)
extra -= 1
pos += 1
else:
for k in range(b):
print(2,2*k+1)
print(2,2*k+2)
extra = w - b
if extra > 0:
print(2,2*b+1)
extra -= 1
pos = 1
while extra > 0:
print(1,2*pos)
extra -= 1
if extra > 0:
print(3,2*pos)
extra -= 1
pos += 1
else:
print("NO")
``` | instruction | 0 | 49,734 | 15 | 99,468 |
No | output | 1 | 49,734 | 15 | 99,469 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two integers b and w. You have a chessboard of size 10^9 Γ 10^9 with the top left cell at (1; 1), the cell (1; 1) is painted white.
Your task is to find a connected component on this chessboard that contains exactly b black cells and exactly w white cells. Two cells are called connected if they share a side (i.e. for the cell (x, y) there are at most four connected cells: (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)). A set of cells is called a connected component if for every pair of cells C_1 and C_2 from this set, there exists a sequence of cells c_1, c_2, ..., c_k such that c_1 = C_1, c_k = C_2, all c_i from 1 to k are belong to this set of cells and for every i β [1, k - 1], cells c_i and c_{i + 1} are connected.
Obviously, it can be impossible to find such component. In this case print "NO". Otherwise, print "YES" and any suitable connected component.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 10^5) β the number of queries. Then q queries follow.
The only line of the query contains two integers b and w (1 β€ b, w β€ 10^5) β the number of black cells required and the number of white cells required.
It is guaranteed that the sum of numbers of cells does not exceed 2 β
10^5 (β w + β b β€ 2 β
10^5).
Output
For each query, print the answer to it.
If it is impossible to find the required component, print "NO" on the first line.
Otherwise, print "YES" on the first line. In the next b + w lines print coordinates of cells of your component in any order. There should be exactly b black cells and w white cells in your answer. The printed component should be connected.
If there are several answers, you can print any. All coordinates in the answer should be in the range [1; 10^9].
Example
Input
3
1 1
1 4
2 5
Output
YES
2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5
Submitted Solution:
```
def col(x, y):
return (x + y) % 2 == 1
t = int(input())
for z in range(t):
w, b = map(int, input().split())
if b > w:
sx = 3
sy = 2
else:
sx = 2
sy = 2
x = sx
y = sy
if max(b, w) > min(b, w) * 2 + 1 + min(b, w):
print('NO')
else:
print('YES')
mn = min(w, b)
mx = max(w, b)
for i in range(mn):
c = col(x, y)
if c:
b -= 1
else:
w -= 1
print(x, y)
y += 1
c = col(x, y)
if c:
b -= 1
else:
w -= 1
print(x, y)
y += 1
#print(w, b, '???')
if not w and not b:
continue
c = col(x, y)
if c:
b -= 1
else:
w -= 1
print(x, y)
mx = max(b, w)
x = sx
y = sy + 1
#print(w, b, '!!!')
while mx > 0:
if mx > 1:
print(x + 1, y)
print(x - 1, y)
y += 2
mx -= 2
else:
print(x + 1, y)
mx -= 1
``` | instruction | 0 | 49,735 | 15 | 99,470 |
No | output | 1 | 49,735 | 15 | 99,471 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are playing the following game. There are n points on a plane. They are the vertices of a regular n-polygon. Points are labeled with integer numbers from 1 to n. Each pair of distinct points is connected by a diagonal, which is colored in one of 26 colors. Points are denoted by lowercase English letters. There are three stones positioned on three distinct vertices. All stones are the same. With one move you can move the stone to another free vertex along some diagonal. The color of this diagonal must be the same as the color of the diagonal, connecting another two stones.
Your goal is to move stones in such way that the only vertices occupied by stones are 1, 2 and 3. You must achieve such position using minimal number of moves. Write a program which plays this game in an optimal way.
Input
In the first line there is one integer n (3 β€ n β€ 70) β the number of points. In the second line there are three space-separated integer from 1 to n β numbers of vertices, where stones are initially located.
Each of the following n lines contains n symbols β the matrix denoting the colors of the diagonals. Colors are denoted by lowercase English letters. The symbol j of line i denotes the color of diagonal between points i and j. Matrix is symmetric, so j-th symbol of i-th line is equal to i-th symbol of j-th line. Main diagonal is filled with '*' symbols because there is no diagonal, connecting point to itself.
Output
If there is no way to put stones on vertices 1, 2 and 3, print -1 on a single line. Otherwise, on the first line print minimal required number of moves and in the next lines print the description of each move, one move per line. To describe a move print two integers. The point from which to remove the stone, and the point to which move the stone. If there are several optimal solutions, print any of them.
Examples
Input
4
2 3 4
*aba
a*ab
ba*b
abb*
Output
1
4 1
Input
4
2 3 4
*abc
a*ab
ba*b
cbb*
Output
-1
Note
In the first example we can move stone from point 4 to point 1 because this points are connected by the diagonal of color 'a' and the diagonal connection point 2 and 3, where the other stones are located, are connected by the diagonal of the same color. After that stones will be on the points 1, 2 and 3. | instruction | 0 | 49,995 | 15 | 99,990 |
Tags: dp, graphs, implementation, shortest paths
Correct Solution:
```
from collections import deque
__author__ = 'asmn'
n = int(input())
end = tuple(sorted(map(lambda x: int(x) - 1, input().split())))
st = (0, 1, 2)
mat = [input() for i in range(n)]
v = set([st])
path = {}
dist = {st: 0}
queue = deque([st])
while end not in v and len(queue) > 0:
p = queue.popleft()
for x in range(-2, 1):
p1, p2, p3 = p[x], p[x + 1], p[x + 2]
for i in range(n):
if i not in (p1, p2, p3) and mat[i][p3] == mat[p1][p2]:
np = tuple(sorted((p1, p2, i)))
if np not in v:
v.add(np)
queue.append(np)
path[np] = p
dist[np] = dist[p] + 1
def pathinfo(fr, to):
return str((set(fr) - set(to)).pop() + 1) + ' ' + str((set(to) - set(fr)).pop() + 1)
if end not in dist:
print(-1)
exit()
print(dist[end])
while end in path:
print(pathinfo(end, path[end]))
end = path[end]
``` | output | 1 | 49,995 | 15 | 99,991 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, n stones will fall and Liss will escape from the stones. The stones are numbered from 1 to n in order.
The stones always fall to the center of Liss's interval. When Liss occupies the interval [k - d, k + d] and a stone falls to k, she will escape to the left or to the right. If she escapes to the left, her new interval will be [k - d, k]. If she escapes to the right, her new interval will be [k, k + d].
You are given a string s of length n. If the i-th character of s is "l" or "r", when the i-th stone falls Liss will escape to the left or to the right, respectively. Find the sequence of stones' numbers from left to right after all the n stones falls.
Input
The input consists of only one line. The only line contains the string s (1 β€ |s| β€ 106). Each character in s will be either "l" or "r".
Output
Output n lines β on the i-th line you should print the i-th stone's number from the left.
Examples
Input
llrlr
Output
3
5
4
2
1
Input
rrlll
Output
1
2
5
4
3
Input
lrlrr
Output
2
4
5
3
1
Note
In the first example, the positions of stones 1, 2, 3, 4, 5 will be <image>, respectively. So you should print the sequence: 3, 5, 4, 2, 1. | instruction | 0 | 50,776 | 15 | 101,552 |
Tags: constructive algorithms, data structures, implementation, two pointers
Correct Solution:
```
s=input()
n=len(s)
a=1
ans=[0]*n
for i in range(n):
if s[i]=='l':
ans[n-1]=i+1
n-=1
else:
ans[a-1]=i+1
a+=1
for i in ans:
print(i)
``` | output | 1 | 50,776 | 15 | 101,553 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, n stones will fall and Liss will escape from the stones. The stones are numbered from 1 to n in order.
The stones always fall to the center of Liss's interval. When Liss occupies the interval [k - d, k + d] and a stone falls to k, she will escape to the left or to the right. If she escapes to the left, her new interval will be [k - d, k]. If she escapes to the right, her new interval will be [k, k + d].
You are given a string s of length n. If the i-th character of s is "l" or "r", when the i-th stone falls Liss will escape to the left or to the right, respectively. Find the sequence of stones' numbers from left to right after all the n stones falls.
Input
The input consists of only one line. The only line contains the string s (1 β€ |s| β€ 106). Each character in s will be either "l" or "r".
Output
Output n lines β on the i-th line you should print the i-th stone's number from the left.
Examples
Input
llrlr
Output
3
5
4
2
1
Input
rrlll
Output
1
2
5
4
3
Input
lrlrr
Output
2
4
5
3
1
Note
In the first example, the positions of stones 1, 2, 3, 4, 5 will be <image>, respectively. So you should print the sequence: 3, 5, 4, 2, 1. | instruction | 0 | 50,777 | 15 | 101,554 |
Tags: constructive algorithms, data structures, implementation, two pointers
Correct Solution:
```
s = str(input())
n = len(s)
l1, l2 = list(), list()
for i in range(n):
if s[i] == 'l':
l1.append(i+1)
else:
l2.append(i+1)
for x in l2:
print(x)
for i in range(len(l1)-1, -1, -1):
print(l1[i])
``` | output | 1 | 50,777 | 15 | 101,555 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, n stones will fall and Liss will escape from the stones. The stones are numbered from 1 to n in order.
The stones always fall to the center of Liss's interval. When Liss occupies the interval [k - d, k + d] and a stone falls to k, she will escape to the left or to the right. If she escapes to the left, her new interval will be [k - d, k]. If she escapes to the right, her new interval will be [k, k + d].
You are given a string s of length n. If the i-th character of s is "l" or "r", when the i-th stone falls Liss will escape to the left or to the right, respectively. Find the sequence of stones' numbers from left to right after all the n stones falls.
Input
The input consists of only one line. The only line contains the string s (1 β€ |s| β€ 106). Each character in s will be either "l" or "r".
Output
Output n lines β on the i-th line you should print the i-th stone's number from the left.
Examples
Input
llrlr
Output
3
5
4
2
1
Input
rrlll
Output
1
2
5
4
3
Input
lrlrr
Output
2
4
5
3
1
Note
In the first example, the positions of stones 1, 2, 3, 4, 5 will be <image>, respectively. So you should print the sequence: 3, 5, 4, 2, 1. | instruction | 0 | 50,778 | 15 | 101,556 |
Tags: constructive algorithms, data structures, implementation, two pointers
Correct Solution:
```
s=input()
l,r=[],[]
n=len(s)
for i in range(n):
if s[i]=='l':
l.append(i+1)
else:
r.append(i+1)
l=sorted(l,reverse=True)
r.extend(l)
for i in r:
print(i)
``` | output | 1 | 50,778 | 15 | 101,557 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, n stones will fall and Liss will escape from the stones. The stones are numbered from 1 to n in order.
The stones always fall to the center of Liss's interval. When Liss occupies the interval [k - d, k + d] and a stone falls to k, she will escape to the left or to the right. If she escapes to the left, her new interval will be [k - d, k]. If she escapes to the right, her new interval will be [k, k + d].
You are given a string s of length n. If the i-th character of s is "l" or "r", when the i-th stone falls Liss will escape to the left or to the right, respectively. Find the sequence of stones' numbers from left to right after all the n stones falls.
Input
The input consists of only one line. The only line contains the string s (1 β€ |s| β€ 106). Each character in s will be either "l" or "r".
Output
Output n lines β on the i-th line you should print the i-th stone's number from the left.
Examples
Input
llrlr
Output
3
5
4
2
1
Input
rrlll
Output
1
2
5
4
3
Input
lrlrr
Output
2
4
5
3
1
Note
In the first example, the positions of stones 1, 2, 3, 4, 5 will be <image>, respectively. So you should print the sequence: 3, 5, 4, 2, 1. | instruction | 0 | 50,779 | 15 | 101,558 |
Tags: constructive algorithms, data structures, implementation, two pointers
Correct Solution:
```
s=input()
l,r=[],[]
for x in range(len(s)):
if s[x]=='r':
r.append(x+1)
else:
l.append(x+1)
for el in r:
print(el)
for el in l[::-1]:
print(el)
``` | output | 1 | 50,779 | 15 | 101,559 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, n stones will fall and Liss will escape from the stones. The stones are numbered from 1 to n in order.
The stones always fall to the center of Liss's interval. When Liss occupies the interval [k - d, k + d] and a stone falls to k, she will escape to the left or to the right. If she escapes to the left, her new interval will be [k - d, k]. If she escapes to the right, her new interval will be [k, k + d].
You are given a string s of length n. If the i-th character of s is "l" or "r", when the i-th stone falls Liss will escape to the left or to the right, respectively. Find the sequence of stones' numbers from left to right after all the n stones falls.
Input
The input consists of only one line. The only line contains the string s (1 β€ |s| β€ 106). Each character in s will be either "l" or "r".
Output
Output n lines β on the i-th line you should print the i-th stone's number from the left.
Examples
Input
llrlr
Output
3
5
4
2
1
Input
rrlll
Output
1
2
5
4
3
Input
lrlrr
Output
2
4
5
3
1
Note
In the first example, the positions of stones 1, 2, 3, 4, 5 will be <image>, respectively. So you should print the sequence: 3, 5, 4, 2, 1. | instruction | 0 | 50,780 | 15 | 101,560 |
Tags: constructive algorithms, data structures, implementation, two pointers
Correct Solution:
```
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
s = input()
n = len(s)
ans = [0]*(n + 1)
l = n
r = 1
stone = 1
for i in s:
if i == 'l':
ans[l] = stone
l -= 1
if i == 'r':
ans[r] = stone
r += 1
stone += 1
# print(ans)
for i in ans[1:]:
print(i)
``` | output | 1 | 50,780 | 15 | 101,561 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.