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.
The city of Fishtopia can be imagined as a grid of 4 rows and an odd number of columns. It has two main villages; the first is located at the top-left cell (1,1), people who stay there love fishing at the Tuna pond at the bottom-right cell (4, n). The second village is located at (4, 1) and its people love the Salmon pond at (1, n).
The mayor of Fishtopia wants to place k hotels in the city, each one occupying one cell. To allow people to enter the city from anywhere, hotels should not be placed on the border cells.
A person can move from one cell to another if those cells are not occupied by hotels and share a side.
Can you help the mayor place the hotels in a way such that there are equal number of shortest paths from each village to its preferred pond?
Input
The first line of input contain two integers, n and k (3 β€ n β€ 99, 0 β€ k β€ 2Γ(n-2)), n is odd, the width of the city, and the number of hotels to be placed, respectively.
Output
Print "YES", if it is possible to place all the hotels in a way that satisfies the problem statement, otherwise print "NO".
If it is possible, print an extra 4 lines that describe the city, each line should have n characters, each of which is "#" if that cell has a hotel on it, or "." if not.
Examples
Input
7 2
Output
YES
.......
.#.....
.#.....
.......
Input
5 3
Output
YES
.....
.###.
.....
.....
Submitted Solution:
```
n,k=map(int,input().split())
if k>2*(n-1):
print("NO")
else :
print("YES")
print('.'*n)
x=(k+1)//2
print('#'*x+'.'*(n-x))
x=k//2
print("#"*x+'.'*(n-x))
print('.'*n)
``` | instruction | 0 | 87,285 | 15 | 174,570 |
No | output | 1 | 87,285 | 15 | 174,571 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The city of Fishtopia can be imagined as a grid of 4 rows and an odd number of columns. It has two main villages; the first is located at the top-left cell (1,1), people who stay there love fishing at the Tuna pond at the bottom-right cell (4, n). The second village is located at (4, 1) and its people love the Salmon pond at (1, n).
The mayor of Fishtopia wants to place k hotels in the city, each one occupying one cell. To allow people to enter the city from anywhere, hotels should not be placed on the border cells.
A person can move from one cell to another if those cells are not occupied by hotels and share a side.
Can you help the mayor place the hotels in a way such that there are equal number of shortest paths from each village to its preferred pond?
Input
The first line of input contain two integers, n and k (3 β€ n β€ 99, 0 β€ k β€ 2Γ(n-2)), n is odd, the width of the city, and the number of hotels to be placed, respectively.
Output
Print "YES", if it is possible to place all the hotels in a way that satisfies the problem statement, otherwise print "NO".
If it is possible, print an extra 4 lines that describe the city, each line should have n characters, each of which is "#" if that cell has a hotel on it, or "." if not.
Examples
Input
7 2
Output
YES
.......
.#.....
.#.....
.......
Input
5 3
Output
YES
.....
.###.
.....
.....
Submitted Solution:
```
n, k = map(int, input().split())
if(k % 2 != 0 and n - k != 2):
print("NO")
exit()
print("YES")
x = [["." for i in range(n)] for _ in range(4)]
if(k % 2 == 0):
curr = 1
while k > 0:
x[1][curr] = "#"
x[2][curr] = "#"
curr += 1
k -= 2
else:
x[1] = ["#" for i in range(n)]
x[1][0] = "."
x[1][n-1] ="."
for i in x:
print(''.join(i))
``` | instruction | 0 | 87,286 | 15 | 174,572 |
No | output | 1 | 87,286 | 15 | 174,573 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place.
You can perform zero or more operations of the following type:
* take two stones with indices i and j so that s_i β€ s_j, choose an integer d (0 β€ 2 β
d β€ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other.
You want to move the stones so that they are located at positions t_1, t_2, β¦, t_n. The order of the stones is not important β you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, β¦, t_n.
Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves.
Input
The first line contains a single integer n (1 β€ n β€ 3 β
10^5) β the number of stones.
The second line contains integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 10^9) β the initial positions of the stones.
The second line contains integers t_1, t_2, β¦, t_n (1 β€ t_i β€ 10^9) β the target positions of the stones.
Output
If it is impossible to move the stones this way, print "NO".
Otherwise, on the first line print "YES", on the second line print the number of operations m (0 β€ m β€ 5 β
n) required. You don't have to minimize the number of operations.
Then print m lines, each containing integers i, j, d (1 β€ i, j β€ n, s_i β€ s_j, 0 β€ 2 β
d β€ s_j - s_i), defining the operations.
One can show that if an answer exists, there is an answer requiring no more than 5 β
n operations.
Examples
Input
5
2 2 7 4 9
5 4 5 5 5
Output
YES
4
4 3 1
2 3 1
2 5 2
1 5 2
Input
3
1 5 10
3 5 7
Output
NO
Note
Consider the first example.
* After the first move the locations of stones is [2, 2, 6, 5, 9].
* After the second move the locations of stones is [2, 3, 5, 5, 9].
* After the third move the locations of stones is [2, 5, 5, 5, 7].
* After the last move the locations of stones is [4, 5, 5, 5, 5]. | instruction | 0 | 87,609 | 15 | 175,218 |
Tags: constructive algorithms, greedy, math, sortings, two pointers
Correct Solution:
```
n=int(input())
s=list(map(int,input().split()))
t=list(map(int,input().split()))
if sum(s)!=sum(t):
print("NO")
else:
s=[(s[i],i+1) for i in range(n)]
s.sort()
t.sort()
diff=[0]*n
for i in range(n):
if s[i][0]==t[i]:
diff[i]=0
elif s[i][0]<t[i]:
diff[i]=1
else:
diff[i]=-1
move=[abs(s[i][0]-t[i]) for i in range(n)]
sumi=sum(move)
indu=0
indd=0
out=[]
while sumi>0:
if indd<indu:
print("NO")
exit()
if diff[indu]==1 and move[indu]>0 and diff[indd]==-1 and move[indd]>0:
a=min(move[indu],move[indd])
move[indu]-=a
move[indd]-=a
out.append((s[indu][1],s[indd][1],a))
sumi-=2*a
elif diff[indu]==1 and move[indu]>0:
indd+=1
elif diff[indd]==-1 and move[indd]>0:
indu+=1
else:
indd+=1
indu+=1
print("YES")
print(len(out))
for guy in out:
print(guy[0],guy[1],guy[2])
``` | output | 1 | 87,609 | 15 | 175,219 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place.
You can perform zero or more operations of the following type:
* take two stones with indices i and j so that s_i β€ s_j, choose an integer d (0 β€ 2 β
d β€ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other.
You want to move the stones so that they are located at positions t_1, t_2, β¦, t_n. The order of the stones is not important β you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, β¦, t_n.
Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves.
Input
The first line contains a single integer n (1 β€ n β€ 3 β
10^5) β the number of stones.
The second line contains integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 10^9) β the initial positions of the stones.
The second line contains integers t_1, t_2, β¦, t_n (1 β€ t_i β€ 10^9) β the target positions of the stones.
Output
If it is impossible to move the stones this way, print "NO".
Otherwise, on the first line print "YES", on the second line print the number of operations m (0 β€ m β€ 5 β
n) required. You don't have to minimize the number of operations.
Then print m lines, each containing integers i, j, d (1 β€ i, j β€ n, s_i β€ s_j, 0 β€ 2 β
d β€ s_j - s_i), defining the operations.
One can show that if an answer exists, there is an answer requiring no more than 5 β
n operations.
Examples
Input
5
2 2 7 4 9
5 4 5 5 5
Output
YES
4
4 3 1
2 3 1
2 5 2
1 5 2
Input
3
1 5 10
3 5 7
Output
NO
Note
Consider the first example.
* After the first move the locations of stones is [2, 2, 6, 5, 9].
* After the second move the locations of stones is [2, 3, 5, 5, 9].
* After the third move the locations of stones is [2, 5, 5, 5, 7].
* After the last move the locations of stones is [4, 5, 5, 5, 5]. | instruction | 0 | 87,610 | 15 | 175,220 |
Tags: constructive algorithms, greedy, math, sortings, two pointers
Correct Solution:
```
from collections import namedtuple
Stone = namedtuple('Stone', ['s', 'i'])
def debug(*args, **kwargs):
import sys
#print(*args, *('{}={}'.format(k, v) for k, v in kwargs.items()), sep='; ', file=sys.stderr)
def solve(n, s, t):
#debug(s=s, t=t)
s = list(map(lambda s_i: Stone(s_i[1], s_i[0]), enumerate(s)))
t = t[:]
s.sort()
t.sort()
#debug(s=s, t=t)
diff = [s_.s - t_ for s_, t_ in zip(s, t)]
j = 0
while j < n and diff[j] <= 0:
j += 1
moves = []
for i in range(n):
if diff[i] == 0:
continue
if diff[i] > 0:
return None, None
while j < n and -diff[i] >= diff[j]:
#debug("about to gobble", i=i, j=j, moves=moves, diff=diff)
moves.append((s[i].i, s[j].i, diff[j]))
diff[i] += diff[j]
diff[j] = 0
while j < n and diff[j] <= 0:
j += 1
#debug(i=i, j=j, moves=moves, diff=diff)
if diff[i] != 0:
if j == n:
return None, None
moves.append((s[i].i, s[j].i, -diff[i]))
diff[j] -= -diff[i]
diff[i] = 0
#debug("gobbled", i=i, j=j, moves=moves, diff=diff)
return len(moves), moves
def check(n, s, t, m, moves):
s = s[:]
t = t[:]
for i, j, d in moves:
debug(i=i, j=j, d=d, s=s)
assert d > 0 and s[j] - s[i] >= 2*d
s[i] += d
s[j] -= d
debug(s=s, t=t)
s.sort()
t.sort()
assert s == t
def main():
n = int(input())
s = list(map(int, input().split()))
t = list(map(int, input().split()))
m, moves = solve(n, s, t)
if m is None:
print("NO")
return
#check(n, s, t, m, moves)
print("YES")
print(m)
for i, j, d in moves:
print(i + 1, j + 1, d)
if __name__ == "__main__":
main()
``` | output | 1 | 87,610 | 15 | 175,221 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place.
You can perform zero or more operations of the following type:
* take two stones with indices i and j so that s_i β€ s_j, choose an integer d (0 β€ 2 β
d β€ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other.
You want to move the stones so that they are located at positions t_1, t_2, β¦, t_n. The order of the stones is not important β you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, β¦, t_n.
Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves.
Input
The first line contains a single integer n (1 β€ n β€ 3 β
10^5) β the number of stones.
The second line contains integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 10^9) β the initial positions of the stones.
The second line contains integers t_1, t_2, β¦, t_n (1 β€ t_i β€ 10^9) β the target positions of the stones.
Output
If it is impossible to move the stones this way, print "NO".
Otherwise, on the first line print "YES", on the second line print the number of operations m (0 β€ m β€ 5 β
n) required. You don't have to minimize the number of operations.
Then print m lines, each containing integers i, j, d (1 β€ i, j β€ n, s_i β€ s_j, 0 β€ 2 β
d β€ s_j - s_i), defining the operations.
One can show that if an answer exists, there is an answer requiring no more than 5 β
n operations.
Examples
Input
5
2 2 7 4 9
5 4 5 5 5
Output
YES
4
4 3 1
2 3 1
2 5 2
1 5 2
Input
3
1 5 10
3 5 7
Output
NO
Note
Consider the first example.
* After the first move the locations of stones is [2, 2, 6, 5, 9].
* After the second move the locations of stones is [2, 3, 5, 5, 9].
* After the third move the locations of stones is [2, 5, 5, 5, 7].
* After the last move the locations of stones is [4, 5, 5, 5, 5]. | instruction | 0 | 87,611 | 15 | 175,222 |
Tags: constructive algorithms, greedy, math, sortings, two pointers
Correct Solution:
```
input()
s=[(int(x), i) for i, x in enumerate(input().split())]
t=[int(x) for x in input().split()]
s.sort()
t.sort()
def no():
print('NO')
raise SystemExit(0)
end=1
ans=[]
for si, ti in zip(s, t):
si_pos, i = si
if si_pos > ti:
no()
jump = ti - si_pos
while jump:
for end in range(end, len(s)):
if s[end][0] - t[end] > 0:
break
else:
no()
moved = s[end]
mov = moved[0] - t[end]
mov = min(mov, jump)
s[end] = (moved[0] - mov, moved[1])
jump -= mov
ans.append('{} {} {}'.format(i + 1, moved[1] + 1, mov))
assert len(ans) <= 5 * len(s)
if not ans:
print('YES\n0')
else:
print('YES\n{}\n{}'.format(len(ans), '\n'.join(ans)))
``` | output | 1 | 87,611 | 15 | 175,223 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place.
You can perform zero or more operations of the following type:
* take two stones with indices i and j so that s_i β€ s_j, choose an integer d (0 β€ 2 β
d β€ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other.
You want to move the stones so that they are located at positions t_1, t_2, β¦, t_n. The order of the stones is not important β you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, β¦, t_n.
Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves.
Input
The first line contains a single integer n (1 β€ n β€ 3 β
10^5) β the number of stones.
The second line contains integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 10^9) β the initial positions of the stones.
The second line contains integers t_1, t_2, β¦, t_n (1 β€ t_i β€ 10^9) β the target positions of the stones.
Output
If it is impossible to move the stones this way, print "NO".
Otherwise, on the first line print "YES", on the second line print the number of operations m (0 β€ m β€ 5 β
n) required. You don't have to minimize the number of operations.
Then print m lines, each containing integers i, j, d (1 β€ i, j β€ n, s_i β€ s_j, 0 β€ 2 β
d β€ s_j - s_i), defining the operations.
One can show that if an answer exists, there is an answer requiring no more than 5 β
n operations.
Examples
Input
5
2 2 7 4 9
5 4 5 5 5
Output
YES
4
4 3 1
2 3 1
2 5 2
1 5 2
Input
3
1 5 10
3 5 7
Output
NO
Note
Consider the first example.
* After the first move the locations of stones is [2, 2, 6, 5, 9].
* After the second move the locations of stones is [2, 3, 5, 5, 9].
* After the third move the locations of stones is [2, 5, 5, 5, 7].
* After the last move the locations of stones is [4, 5, 5, 5, 5]. | instruction | 0 | 87,612 | 15 | 175,224 |
Tags: constructive algorithms, greedy, math, sortings, two pointers
Correct Solution:
```
n = int(input())
s = sorted((v, i+1) for i, v in enumerate(map(int, input().split())))
t = sorted(map(int, input().split()))
r = []
q = []
err = False
for x, y in zip(s, t):
d = x[0] - y
if d < 0:
q.append([-d, x[1]])
else:
while d > 0:
if not q:
err = True
break
if q[-1][0] <= d:
z, i = q.pop()
d -= z
r.append((x[1], i, z))
else:
q[-1][0] -= d
r.append((x[1], q[-1][1], d))
break
if err: break
if err or q:
print("NO")
else:
print("YES")
print(len(r))
print("\n".join(f"{b} {a} {d}" for a, b, d in r))
``` | output | 1 | 87,612 | 15 | 175,225 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place.
You can perform zero or more operations of the following type:
* take two stones with indices i and j so that s_i β€ s_j, choose an integer d (0 β€ 2 β
d β€ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other.
You want to move the stones so that they are located at positions t_1, t_2, β¦, t_n. The order of the stones is not important β you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, β¦, t_n.
Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves.
Input
The first line contains a single integer n (1 β€ n β€ 3 β
10^5) β the number of stones.
The second line contains integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 10^9) β the initial positions of the stones.
The second line contains integers t_1, t_2, β¦, t_n (1 β€ t_i β€ 10^9) β the target positions of the stones.
Output
If it is impossible to move the stones this way, print "NO".
Otherwise, on the first line print "YES", on the second line print the number of operations m (0 β€ m β€ 5 β
n) required. You don't have to minimize the number of operations.
Then print m lines, each containing integers i, j, d (1 β€ i, j β€ n, s_i β€ s_j, 0 β€ 2 β
d β€ s_j - s_i), defining the operations.
One can show that if an answer exists, there is an answer requiring no more than 5 β
n operations.
Examples
Input
5
2 2 7 4 9
5 4 5 5 5
Output
YES
4
4 3 1
2 3 1
2 5 2
1 5 2
Input
3
1 5 10
3 5 7
Output
NO
Note
Consider the first example.
* After the first move the locations of stones is [2, 2, 6, 5, 9].
* After the second move the locations of stones is [2, 3, 5, 5, 9].
* After the third move the locations of stones is [2, 5, 5, 5, 7].
* After the last move the locations of stones is [4, 5, 5, 5, 5]. | instruction | 0 | 87,613 | 15 | 175,226 |
Tags: constructive algorithms, greedy, math, sortings, two pointers
Correct Solution:
```
import heapq
import io
import os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
s = list(map(int, input().split()))
t = list(map(int, input().split()))
s = sorted((x, i) for i, x in enumerate(s))
t.sort()
to_left = []
to_right = []
for (pos, i), target in zip(s, t):
if pos < target:
heapq.heappush(to_right, (pos, target, i))
elif pos > target:
heapq.heappush(to_left, (pos, target, i))
ops = []
while to_right and to_left:
pos1, target1, ind1 = heapq.heappop(to_right)
pos2, target2, ind2 = heapq.heappop(to_left)
if pos2 <= pos1:
print("NO")
exit()
d = min(target1-pos1, pos2-target2)
d = min(d, (pos2-pos1)//2)
ops.append((ind1, ind2, d))
pos1 += d
if pos1 != target1:
heapq.heappush(to_right, (pos1, target1, ind1))
pos2 -= d
if pos2 != target2:
heapq.heappush(to_left, (pos2, target2, ind2))
if to_right or to_left:
print("NO")
else:
print("YES")
print(len(ops))
for ind1, ind2, d in ops:
print(ind1+1, ind2+1, d)
``` | output | 1 | 87,613 | 15 | 175,227 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place.
You can perform zero or more operations of the following type:
* take two stones with indices i and j so that s_i β€ s_j, choose an integer d (0 β€ 2 β
d β€ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other.
You want to move the stones so that they are located at positions t_1, t_2, β¦, t_n. The order of the stones is not important β you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, β¦, t_n.
Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves.
Input
The first line contains a single integer n (1 β€ n β€ 3 β
10^5) β the number of stones.
The second line contains integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 10^9) β the initial positions of the stones.
The second line contains integers t_1, t_2, β¦, t_n (1 β€ t_i β€ 10^9) β the target positions of the stones.
Output
If it is impossible to move the stones this way, print "NO".
Otherwise, on the first line print "YES", on the second line print the number of operations m (0 β€ m β€ 5 β
n) required. You don't have to minimize the number of operations.
Then print m lines, each containing integers i, j, d (1 β€ i, j β€ n, s_i β€ s_j, 0 β€ 2 β
d β€ s_j - s_i), defining the operations.
One can show that if an answer exists, there is an answer requiring no more than 5 β
n operations.
Examples
Input
5
2 2 7 4 9
5 4 5 5 5
Output
YES
4
4 3 1
2 3 1
2 5 2
1 5 2
Input
3
1 5 10
3 5 7
Output
NO
Note
Consider the first example.
* After the first move the locations of stones is [2, 2, 6, 5, 9].
* After the second move the locations of stones is [2, 3, 5, 5, 9].
* After the third move the locations of stones is [2, 5, 5, 5, 7].
* After the last move the locations of stones is [4, 5, 5, 5, 5]. | instruction | 0 | 87,614 | 15 | 175,228 |
Tags: constructive algorithms, greedy, math, sortings, two pointers
Correct Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def main():
n = I()
a = LI()
b = sorted(LI())
sa = sum(a)
sb = sum(b)
if sa != sb:
return 'NO'
c = []
for i in range(1,n+1):
c.append([a[i-1], i])
c.sort(key=lambda x: x[0])
r = []
d = 0
for e in range(1,n):
while d < e:
ad = c[d][0]
sl = b[d] - ad
if sl < 0:
return 'NO'
if sl == 0:
d += 1
continue
ed = c[e][0]
sr = ed - b[e]
if sr <= 0:
break
sm = min(sl,sr)
c[d][0] += sm
c[e][0] -= sm
r.append('{} {} {}'.format(c[d][1], c[e][1], sm))
a[c[e][1]-1] -= sm
a[c[d][1]-1] += sm
e = n - 1
for d in range(n-2,-1,-1):
while d < e:
ad = c[d][0]
sl = b[d] - ad
if sl <= 0:
break
ed = c[e][0]
sr = ed - b[e]
if sr < 0:
return 'NO'
if sr == 0:
e -= 1
continue
sm = min(sl,sr)
c[d][0] += sm
c[e][0] -= sm
r.append('{} {} {}'.format(c[d][1], c[e][1], sm))
a[c[e][1]-1] -= sm
a[c[d][1]-1] += sm
d = 0
e = n - 1
while d < e:
ad = c[d][0]
sl = b[d] - ad
if sl < 0:
return 'NO'
if sl == 0:
d += 1
continue
ed = c[e][0]
sr = ed - b[e]
if sr < 0:
return 'NO'
if sr == 0:
e -= 1
continue
sm = min(sl,sr)
c[d][0] += sm
c[e][0] -= sm
r.append('{} {} {}'.format(c[d][1], c[e][1], sm))
a[c[e][1]-1] -= sm
a[c[d][1]-1] += sm
return 'YES\n{}\n{}'.format(len(r),'\n'.join(r))
print(main())
``` | output | 1 | 87,614 | 15 | 175,229 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place.
You can perform zero or more operations of the following type:
* take two stones with indices i and j so that s_i β€ s_j, choose an integer d (0 β€ 2 β
d β€ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other.
You want to move the stones so that they are located at positions t_1, t_2, β¦, t_n. The order of the stones is not important β you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, β¦, t_n.
Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves.
Input
The first line contains a single integer n (1 β€ n β€ 3 β
10^5) β the number of stones.
The second line contains integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 10^9) β the initial positions of the stones.
The second line contains integers t_1, t_2, β¦, t_n (1 β€ t_i β€ 10^9) β the target positions of the stones.
Output
If it is impossible to move the stones this way, print "NO".
Otherwise, on the first line print "YES", on the second line print the number of operations m (0 β€ m β€ 5 β
n) required. You don't have to minimize the number of operations.
Then print m lines, each containing integers i, j, d (1 β€ i, j β€ n, s_i β€ s_j, 0 β€ 2 β
d β€ s_j - s_i), defining the operations.
One can show that if an answer exists, there is an answer requiring no more than 5 β
n operations.
Examples
Input
5
2 2 7 4 9
5 4 5 5 5
Output
YES
4
4 3 1
2 3 1
2 5 2
1 5 2
Input
3
1 5 10
3 5 7
Output
NO
Note
Consider the first example.
* After the first move the locations of stones is [2, 2, 6, 5, 9].
* After the second move the locations of stones is [2, 3, 5, 5, 9].
* After the third move the locations of stones is [2, 5, 5, 5, 7].
* After the last move the locations of stones is [4, 5, 5, 5, 5]. | instruction | 0 | 87,615 | 15 | 175,230 |
Tags: constructive algorithms, greedy, math, sortings, two pointers
Correct Solution:
```
import sys
class cell:
def __init__(self, val, idx):
self.idx = idx
self.val = val
inp = [int(x) for x in sys.stdin.read().split()]
n = inp[0]
inp_idx = 1
s = [cell(inp[idx], idx) for idx in range(1, n + 1)]
t = [inp[idx] for idx in range(n + 1, n + n + 1)]
s.sort(key = lambda x: x.val)
t.sort()
sum = 0
for i in range(n):
sum += s[i].val - t[i]
if sum != 0:
print('NO')
else:
operation = []
beg = 0
end = 0
cnt = 0
while True:
while beg < n and s[beg].val == t[beg]: beg += 1
if beg == n:
break
while end <= beg or (end < n and s[end].val <= t[end]): end += 1
if end == n:
print('NO')
exit(0)
left = t[beg] - s[beg].val
if left < 0:
print('NO')
exit(0)
right = s[end].val - t[end]
d = min(left, right)
s[beg].val += d
s[end].val -= d
operation.append('%d %d %d' % (s[beg].idx, s[end].idx, d))
print('YES')
print(len(operation))
print('\n'.join(operation))
``` | output | 1 | 87,615 | 15 | 175,231 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place.
You can perform zero or more operations of the following type:
* take two stones with indices i and j so that s_i β€ s_j, choose an integer d (0 β€ 2 β
d β€ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other.
You want to move the stones so that they are located at positions t_1, t_2, β¦, t_n. The order of the stones is not important β you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, β¦, t_n.
Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves.
Input
The first line contains a single integer n (1 β€ n β€ 3 β
10^5) β the number of stones.
The second line contains integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 10^9) β the initial positions of the stones.
The second line contains integers t_1, t_2, β¦, t_n (1 β€ t_i β€ 10^9) β the target positions of the stones.
Output
If it is impossible to move the stones this way, print "NO".
Otherwise, on the first line print "YES", on the second line print the number of operations m (0 β€ m β€ 5 β
n) required. You don't have to minimize the number of operations.
Then print m lines, each containing integers i, j, d (1 β€ i, j β€ n, s_i β€ s_j, 0 β€ 2 β
d β€ s_j - s_i), defining the operations.
One can show that if an answer exists, there is an answer requiring no more than 5 β
n operations.
Examples
Input
5
2 2 7 4 9
5 4 5 5 5
Output
YES
4
4 3 1
2 3 1
2 5 2
1 5 2
Input
3
1 5 10
3 5 7
Output
NO
Note
Consider the first example.
* After the first move the locations of stones is [2, 2, 6, 5, 9].
* After the second move the locations of stones is [2, 3, 5, 5, 9].
* After the third move the locations of stones is [2, 5, 5, 5, 7].
* After the last move the locations of stones is [4, 5, 5, 5, 5]. | instruction | 0 | 87,616 | 15 | 175,232 |
Tags: constructive algorithms, greedy, math, sortings, two pointers
Correct Solution:
```
import sys
class cell:
def __init__(self, val, idx):
self.idx = idx
self.val = val
inp = [int(x) for x in sys.stdin.read().split()]
n = inp[0]
inp_idx = 1
s = [cell(inp[idx], idx) for idx in range(1, n + 1)]
t = [inp[idx] for idx in range(n + 1, n + n + 1)]
s.sort(key = lambda x: x.val)
t.sort()
sum = 0
for i in range(n):
sum += s[i].val - t[i]
if sum != 0:
print('NO')
else:
operation = []
beg = 0
end = 0
cnt = 0
while True:
while beg < n and s[beg].val == t[beg]: beg += 1
if beg == n:
break
while end <= beg or (end < n and s[end].val <= t[end]): end += 1
if end == n:
print('NO')
exit(0)
left = t[beg] - s[beg].val
if left < 0:
print('NO')
exit(0)
right = s[end].val - t[end]
d = min(left, right)
s[beg].val += d
s[end].val -= d
operation.append((s[beg].idx, s[end].idx, d))
print('YES')
print(len(operation))
for op in operation:
print(*op)
``` | output | 1 | 87,616 | 15 | 175,233 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place.
You can perform zero or more operations of the following type:
* take two stones with indices i and j so that s_i β€ s_j, choose an integer d (0 β€ 2 β
d β€ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other.
You want to move the stones so that they are located at positions t_1, t_2, β¦, t_n. The order of the stones is not important β you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, β¦, t_n.
Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves.
Input
The first line contains a single integer n (1 β€ n β€ 3 β
10^5) β the number of stones.
The second line contains integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 10^9) β the initial positions of the stones.
The second line contains integers t_1, t_2, β¦, t_n (1 β€ t_i β€ 10^9) β the target positions of the stones.
Output
If it is impossible to move the stones this way, print "NO".
Otherwise, on the first line print "YES", on the second line print the number of operations m (0 β€ m β€ 5 β
n) required. You don't have to minimize the number of operations.
Then print m lines, each containing integers i, j, d (1 β€ i, j β€ n, s_i β€ s_j, 0 β€ 2 β
d β€ s_j - s_i), defining the operations.
One can show that if an answer exists, there is an answer requiring no more than 5 β
n operations.
Examples
Input
5
2 2 7 4 9
5 4 5 5 5
Output
YES
4
4 3 1
2 3 1
2 5 2
1 5 2
Input
3
1 5 10
3 5 7
Output
NO
Note
Consider the first example.
* After the first move the locations of stones is [2, 2, 6, 5, 9].
* After the second move the locations of stones is [2, 3, 5, 5, 9].
* After the third move the locations of stones is [2, 5, 5, 5, 7].
* After the last move the locations of stones is [4, 5, 5, 5, 5].
Submitted Solution:
```
N = int(input())
X = [int(a) for a in input().split()]
Y = [int(a) for a in input().split()]
if sum(X) != sum(Y):
print("NO")
else:
X = sorted([[X[i], i+1] for i in range(N)])
Y = sorted([[Y[i], i+1] for i in range(N)])
a = 0
b = N-1
c = 0
d = N-1
ANS = []
while a < b:
if X[a][0] > Y[c][0] or X[b][0] < Y[d][0]:
print("NO")
break
m1 = Y[c][0]-X[a][0]
m2 = X[b][0]-Y[d][0]
if m1 == m2:
ANS.append((X[a][1], X[b][1], m1))
X[a][0] += m1
X[b][0] -= m1
a += 1
b -= 1
c += 1
d -= 1
elif m1 < m2:
ANS.append((X[a][1], X[b][1], m1))
X[a][0] += m1
X[b][0] -= m1
a += 1
c += 1
else:
ANS.append((X[a][1], X[b][1], m2))
X[a][0] += m2
X[b][0] -= m2
b -= 1
d -= 1
print("YES")
print("\n".join([" ".join(map(str, a)) for a in ANS]))
``` | instruction | 0 | 87,617 | 15 | 175,234 |
No | output | 1 | 87,617 | 15 | 175,235 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place.
You can perform zero or more operations of the following type:
* take two stones with indices i and j so that s_i β€ s_j, choose an integer d (0 β€ 2 β
d β€ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other.
You want to move the stones so that they are located at positions t_1, t_2, β¦, t_n. The order of the stones is not important β you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, β¦, t_n.
Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves.
Input
The first line contains a single integer n (1 β€ n β€ 3 β
10^5) β the number of stones.
The second line contains integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 10^9) β the initial positions of the stones.
The second line contains integers t_1, t_2, β¦, t_n (1 β€ t_i β€ 10^9) β the target positions of the stones.
Output
If it is impossible to move the stones this way, print "NO".
Otherwise, on the first line print "YES", on the second line print the number of operations m (0 β€ m β€ 5 β
n) required. You don't have to minimize the number of operations.
Then print m lines, each containing integers i, j, d (1 β€ i, j β€ n, s_i β€ s_j, 0 β€ 2 β
d β€ s_j - s_i), defining the operations.
One can show that if an answer exists, there is an answer requiring no more than 5 β
n operations.
Examples
Input
5
2 2 7 4 9
5 4 5 5 5
Output
YES
4
4 3 1
2 3 1
2 5 2
1 5 2
Input
3
1 5 10
3 5 7
Output
NO
Note
Consider the first example.
* After the first move the locations of stones is [2, 2, 6, 5, 9].
* After the second move the locations of stones is [2, 3, 5, 5, 9].
* After the third move the locations of stones is [2, 5, 5, 5, 7].
* After the last move the locations of stones is [4, 5, 5, 5, 5].
Submitted Solution:
```
n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
for i in range(n):
a[i] = [a[i], i]
b[i] = [b[i], i]
a.sort()
b.sort()
ans = []
l = 0
r = n - 1
while l < r:
d1 = b[l][0] - a[l][0]
d2 = a[r][0] - b[r][0]
if min(d1, d2) < 0:
print("NO")
exit(0)
ans.append([a[l][1] + 1, a[r][1] + 1, min(d1, d2)])
a[l][0] += min(d1, d2)
a[r][0] -= min(d1, d2)
if a[l][0] == b[l][0]:
l += 1
if a[r][0] == b[r][0]:
r -= 1
if l == r:
if a[l][0] != b[l][0]:
print("NO")
if n != 3:
print(1/ 0)
exit(0)
print("YES")
print(len(ans))
for key in ans:
print(key[0], key[1], key[2])
``` | instruction | 0 | 87,618 | 15 | 175,236 |
No | output | 1 | 87,618 | 15 | 175,237 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place.
You can perform zero or more operations of the following type:
* take two stones with indices i and j so that s_i β€ s_j, choose an integer d (0 β€ 2 β
d β€ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other.
You want to move the stones so that they are located at positions t_1, t_2, β¦, t_n. The order of the stones is not important β you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, β¦, t_n.
Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves.
Input
The first line contains a single integer n (1 β€ n β€ 3 β
10^5) β the number of stones.
The second line contains integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 10^9) β the initial positions of the stones.
The second line contains integers t_1, t_2, β¦, t_n (1 β€ t_i β€ 10^9) β the target positions of the stones.
Output
If it is impossible to move the stones this way, print "NO".
Otherwise, on the first line print "YES", on the second line print the number of operations m (0 β€ m β€ 5 β
n) required. You don't have to minimize the number of operations.
Then print m lines, each containing integers i, j, d (1 β€ i, j β€ n, s_i β€ s_j, 0 β€ 2 β
d β€ s_j - s_i), defining the operations.
One can show that if an answer exists, there is an answer requiring no more than 5 β
n operations.
Examples
Input
5
2 2 7 4 9
5 4 5 5 5
Output
YES
4
4 3 1
2 3 1
2 5 2
1 5 2
Input
3
1 5 10
3 5 7
Output
NO
Note
Consider the first example.
* After the first move the locations of stones is [2, 2, 6, 5, 9].
* After the second move the locations of stones is [2, 3, 5, 5, 9].
* After the third move the locations of stones is [2, 5, 5, 5, 7].
* After the last move the locations of stones is [4, 5, 5, 5, 5].
Submitted Solution:
```
from sys import stdout
n=int(input())
r=(list(map(int,input().split())))
s=(list(map(int,input().split())))
k=r[:]
c=[];a=[]
for i in range(n):
a.append([r[i],i])
a.sort()
s.sort()
for i in range(n):
c.append(a[i][0]-s[i])
if sum(c)!=0:
exit(print('NO'))
print('YES')
for i in range(n):
if c[i]>0:
j=i
break
i=0
ans=[]
if k[0]==69372829:
print(c)
while j<n and c[j]>0 and c[i]<0:
d=min(abs(c[i]),abs(c[j]))
c[i]+=d
c[j]-=d
ans.append([a[i][1]+1,a[j][1]+1,d])
if c[i]==0:
i+=1
if c[j]==0:
j+=1
#print(c)
print(len(ans))
for i in ans:
stdout.write(str(i[0])+' '+str(i[1])+' '+str(i[2])+'\n')
``` | instruction | 0 | 87,619 | 15 | 175,238 |
No | output | 1 | 87,619 | 15 | 175,239 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate s_i. There may be more than one stone in a single place.
You can perform zero or more operations of the following type:
* take two stones with indices i and j so that s_i β€ s_j, choose an integer d (0 β€ 2 β
d β€ s_j - s_i), and replace the coordinate s_i with (s_i + d) and replace coordinate s_j with (s_j - d). In other words, draw stones closer to each other.
You want to move the stones so that they are located at positions t_1, t_2, β¦, t_n. The order of the stones is not important β you just want for the multiset of the stones resulting positions to be the same as the multiset of t_1, t_2, β¦, t_n.
Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves.
Input
The first line contains a single integer n (1 β€ n β€ 3 β
10^5) β the number of stones.
The second line contains integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 10^9) β the initial positions of the stones.
The second line contains integers t_1, t_2, β¦, t_n (1 β€ t_i β€ 10^9) β the target positions of the stones.
Output
If it is impossible to move the stones this way, print "NO".
Otherwise, on the first line print "YES", on the second line print the number of operations m (0 β€ m β€ 5 β
n) required. You don't have to minimize the number of operations.
Then print m lines, each containing integers i, j, d (1 β€ i, j β€ n, s_i β€ s_j, 0 β€ 2 β
d β€ s_j - s_i), defining the operations.
One can show that if an answer exists, there is an answer requiring no more than 5 β
n operations.
Examples
Input
5
2 2 7 4 9
5 4 5 5 5
Output
YES
4
4 3 1
2 3 1
2 5 2
1 5 2
Input
3
1 5 10
3 5 7
Output
NO
Note
Consider the first example.
* After the first move the locations of stones is [2, 2, 6, 5, 9].
* After the second move the locations of stones is [2, 3, 5, 5, 9].
* After the third move the locations of stones is [2, 5, 5, 5, 7].
* After the last move the locations of stones is [4, 5, 5, 5, 5].
Submitted Solution:
```
n = int(input())
a = [ int(x) for x in input().strip().split() ]
b = [ int(x) for x in input().strip().split() ]
if sum(a)!=sum(b):
print('NO')
else:
a = [(a[i],i) for i in range(n)]
res = []
b.sort()
a.sort()
det = [a[i][0]-b[i] for i in range(n)]
stack = []
for i in range(n):
# print(stack)
if det[i]<0:
stack.append([det[i],i])
else:
d = det[i]
while d>0:
top = stack.pop()
if abs(top[0])<d:
d+=top[0]
res.append((top[1],i,abs(top[0])))
elif abs(top[0])==d:
d=0
res.append((top[1],i,abs(top[0])))
else:
top[0]+=d
stack.append(top)
res.append((top[1],i,d))
d=0
flag=0
# print(stack)
for s in stack:
if s[0]!=0:
flag=1
break
if len(stack)==0 or flag==0:
print('YES')
print(len(res))
for r in res:
print(r[0]+1, r[1]+1, r[2])
else:
print('NO')
# print(stack)
``` | instruction | 0 | 87,620 | 15 | 175,240 |
No | output | 1 | 87,620 | 15 | 175,241 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.
A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs.
They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf.
What is the maximum number of little pigs that may be eaten by the wolves?
Input
The first line contains integers n and m (1 β€ n, m β€ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf.
It is guaranteed that there will be at most one wolf adjacent to any little pig.
Output
Print a single number β the maximal number of little pigs that may be eaten by the wolves.
Examples
Input
2 3
PPW
W.P
Output
2
Input
3 3
P.W
.P.
W.P
Output
0
Note
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
<image> | instruction | 0 | 87,621 | 15 | 175,242 |
Tags: greedy, implementation
Correct Solution:
```
nk=input().split()
n=int(nk[0])
k=int(nk[1])
a=[]
for i in range(0,n):
a.append([(j) for j in input()])
d=0
for b in range (n):
for c in range (k):
if a[b][c]=="W":
s=0
if c+1!=k and a[b][c+1]=="P" and s==0:
d=d+1
a[b][c+1]="."
s=s+1
elif c-1>=0 and a[b][c-1]=="P" and s==0:
d=d+1
a[b][c - 1] = "."
s = s + 1
elif b-1>=0 and a[b-1][c]=="P"and s==0:
d=d+1
a[b - 1][c] ="."
s = s + 1
elif b+1!=n and a[b+1][c]=="P" and s==0:
d=d+1
a[b + 1][c] = "."
s = s + 1
print(d)
``` | output | 1 | 87,621 | 15 | 175,243 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.
A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs.
They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf.
What is the maximum number of little pigs that may be eaten by the wolves?
Input
The first line contains integers n and m (1 β€ n, m β€ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf.
It is guaranteed that there will be at most one wolf adjacent to any little pig.
Output
Print a single number β the maximal number of little pigs that may be eaten by the wolves.
Examples
Input
2 3
PPW
W.P
Output
2
Input
3 3
P.W
.P.
W.P
Output
0
Note
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
<image> | instruction | 0 | 87,622 | 15 | 175,244 |
Tags: greedy, implementation
Correct Solution:
```
import copy
def computeDanger(board):
danger = []
for i in range(len(board)):
for j in range(len(board[i])):
dangerList = []
if board[i][j] == "P":
if i > 0 and board[i-1][j] == "W":
dangerList.append((i-1, j))
if i < len(board)-1 and board[i+1][j] == "W":
dangerList.append((i+1, j))
if j > 0 and board[i][j-1] == "W":
dangerList.append((i, j-1))
if j < len(board[i])-1 and board[i][j+1] == "W":
dangerList.append((i, j+1))
if len(dangerList) > 0:
danger.append(dangerList)
return danger
def countEaten(d):
if len(d) == 0:
return 0
lastPig = d.pop()
maxEaten = 0
for wolf in lastPig:
newDanger = copy.deepcopy(d)
for i in range(len(newDanger)):
if wolf in newDanger[i]:
newDanger[i].remove(wolf)
while [] in newDanger:
newDanger.remove([])
eaten = 1+countEaten(newDanger)
maxEaten = max(eaten, maxEaten)
return maxEaten
n, m = [int(i) for i in input().split()]
board = []
for i in range(n):
row = [x for x in input()]
board.append(row)
print(countEaten(computeDanger(board)))
``` | output | 1 | 87,622 | 15 | 175,245 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.
A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs.
They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf.
What is the maximum number of little pigs that may be eaten by the wolves?
Input
The first line contains integers n and m (1 β€ n, m β€ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf.
It is guaranteed that there will be at most one wolf adjacent to any little pig.
Output
Print a single number β the maximal number of little pigs that may be eaten by the wolves.
Examples
Input
2 3
PPW
W.P
Output
2
Input
3 3
P.W
.P.
W.P
Output
0
Note
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
<image> | instruction | 0 | 87,623 | 15 | 175,246 |
Tags: greedy, implementation
Correct Solution:
```
n, m = [int(i) for i in input().split()]
x = []
for i in range(n):
x.append(input())
y = 0
for i in range(n):
for j in range(m):
if x[i][j] == "W":
pigs = sum([x[max(0,i-1)][j]=="P",x[min(n-1,i+1)][j]=="P",x[i][max(0,j-1)]=="P",x[i][min(m-1,j+1)]=="P"])
if pigs > 0:
y += 1
print(y)
``` | output | 1 | 87,623 | 15 | 175,247 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.
A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs.
They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf.
What is the maximum number of little pigs that may be eaten by the wolves?
Input
The first line contains integers n and m (1 β€ n, m β€ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf.
It is guaranteed that there will be at most one wolf adjacent to any little pig.
Output
Print a single number β the maximal number of little pigs that may be eaten by the wolves.
Examples
Input
2 3
PPW
W.P
Output
2
Input
3 3
P.W
.P.
W.P
Output
0
Note
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
<image> | instruction | 0 | 87,624 | 15 | 175,248 |
Tags: greedy, implementation
Correct Solution:
```
def main():
n, m = map(int, input().split())
W, P = [], []
for _ in range(n):
s = input()
W.append(s)
row = [False] * (m + 1)
for i, c in enumerate(s):
if c == 'P':
row[i] = True
P.append(row)
P.append([False] * (m + 1))
print(sum(any(P[i][j] for i, j in ((y - 1, x), (y + 1, x), (y, x - 1), (y, x + 1)))
for y, s in enumerate(W) for x, c in enumerate(s) if c == 'W'))
if __name__ == '__main__':
main()
``` | output | 1 | 87,624 | 15 | 175,249 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.
A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs.
They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf.
What is the maximum number of little pigs that may be eaten by the wolves?
Input
The first line contains integers n and m (1 β€ n, m β€ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf.
It is guaranteed that there will be at most one wolf adjacent to any little pig.
Output
Print a single number β the maximal number of little pigs that may be eaten by the wolves.
Examples
Input
2 3
PPW
W.P
Output
2
Input
3 3
P.W
.P.
W.P
Output
0
Note
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
<image> | instruction | 0 | 87,625 | 15 | 175,250 |
Tags: greedy, implementation
Correct Solution:
```
n,m = map(int,input().split())
mat=[]
for _ in range(n):
s=input()
l=[]
for i in s:
l.append(i)
mat.append(l)
flag = [[False for i in range(m)]for j in range(n)]
count = 0
# print(mat)
for i in range(n):
for j in range(m):
if(mat[i][j] == 'W'):
if(i-1>=0 and mat[i-1][j]=='P' and flag[i-1][j]==False):
flag[i-1][j] = True
count += 1
elif(i+1<n and mat[i+1][j]=='P' and flag[i+1][j]==False):
flag[i+1][j] = True
count += 1
elif(j-1>=0 and mat[i][j-1]=='P' and flag[i][j-1]==False):
flag[i][j-1] = True
count += 1
elif(j+1<m and mat[i][j+1]=='P' and flag[i][j+1]==False):
flag[i][j+1] = True
count += 1
print(count)
``` | output | 1 | 87,625 | 15 | 175,251 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.
A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs.
They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf.
What is the maximum number of little pigs that may be eaten by the wolves?
Input
The first line contains integers n and m (1 β€ n, m β€ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf.
It is guaranteed that there will be at most one wolf adjacent to any little pig.
Output
Print a single number β the maximal number of little pigs that may be eaten by the wolves.
Examples
Input
2 3
PPW
W.P
Output
2
Input
3 3
P.W
.P.
W.P
Output
0
Note
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
<image> | instruction | 0 | 87,626 | 15 | 175,252 |
Tags: greedy, implementation
Correct Solution:
```
"""
# SoluciΓ³n utilizando recursividad
def solve(grid):
n = len(grid)
m = len(grid[0])
s = [0]
# nWolves = sum([1 for i in range(n) for j in range(m) if grid[i][j] == "W"])
for i in range(n):
for j in range (m):
if grid[i][j] == 'W':
# Si es lobo, verificar los 4 casos posibles: arriba, izq, der, abajo
for p in range(-1,2):
for q in range(-1,2):
if (abs(p + q) == 1 and i + p < n and i + p >= 0 and j + q < m and j + q >= 0 and grid[i + p][j + q] == "P"):
grid[i][j] = "."
grid[i + p][j + q] = "."
s.append(1 + solve (grid.copy()))
grid[i][j] = "W"
grid[i + p][j + q] = "P"
return max(s)
"""
def isAdjacent (grid, i, j, cell):
for p in range(-1,2):
for q in range(-1,2):
if (abs(p + q) == 1 and i + p < n and i + p >= 0 and j + q < m and j + q >= 0 and grid[i + p][j + q] == cell):
return True
return False
def solve (grid):
n = len(grid)
m = len(grid[0])
nWolves = 0
nPigs = 0
for i in range (n):
for j in range (m):
if (grid[i][j] == "W" and isAdjacent(grid, i, j, "P")):
nWolves += 1
elif (grid[i][j] == "P" and isAdjacent(grid, i, j, "W")):
nPigs += 1
return min(nWolves, nPigs)
if __name__ == "__main__":
n, m = map (int, input ().split())
grid = []
for _ in range (n):
grid.append ([c for c in input()])
print (solve(grid))
``` | output | 1 | 87,626 | 15 | 175,253 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.
A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs.
They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf.
What is the maximum number of little pigs that may be eaten by the wolves?
Input
The first line contains integers n and m (1 β€ n, m β€ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf.
It is guaranteed that there will be at most one wolf adjacent to any little pig.
Output
Print a single number β the maximal number of little pigs that may be eaten by the wolves.
Examples
Input
2 3
PPW
W.P
Output
2
Input
3 3
P.W
.P.
W.P
Output
0
Note
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
<image> | instruction | 0 | 87,627 | 15 | 175,254 |
Tags: greedy, implementation
Correct Solution:
```
n, m = map(int, input().split())
a = [input() for i in range(n)]
ans = 0
for i in range(n):
for j in range(m):
if (a[i][j] == 'W'):
if (i - 1 >= 0 and a[i - 1][j] == 'P') or (i + 1 <= n - 1 and a[i + 1][j] == 'P') or (j - 1 >= 0 and a[i][j - 1] == 'P') or (j + 1 <= m - 1 and a[i][j + 1] == 'P'):
ans += 1
print (ans)
``` | output | 1 | 87,627 | 15 | 175,255 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.
A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs.
They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf.
What is the maximum number of little pigs that may be eaten by the wolves?
Input
The first line contains integers n and m (1 β€ n, m β€ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf.
It is guaranteed that there will be at most one wolf adjacent to any little pig.
Output
Print a single number β the maximal number of little pigs that may be eaten by the wolves.
Examples
Input
2 3
PPW
W.P
Output
2
Input
3 3
P.W
.P.
W.P
Output
0
Note
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
<image> | instruction | 0 | 87,628 | 15 | 175,256 |
Tags: greedy, implementation
Correct Solution:
```
import sys
from functools import reduce
from collections import Counter
import time
import datetime
import math
# def time_t():
# print("Current date and time: " , datetime.datetime.now())
# print("Current year: ", datetime.date.today().strftime("%Y"))
# print("Month of year: ", datetime.date.today().strftime("%B"))
# print("Week number of the year: ", datetime.date.today().strftime("%W"))
# print("Weekday of the week: ", datetime.date.today().strftime("%w"))
# print("Day of year: ", datetime.date.today().strftime("%j"))
# print("Day of the month : ", datetime.date.today().strftime("%d"))
# print("Day of week: ", datetime.date.today().strftime("%A"))
def ip(): return int(sys.stdin.readline())
# def sip(): return sys.stdin.readline()
def sip() : return input()
def mip(): return map(int,sys.stdin.readline().split())
def mips(): return map(str,sys.stdin.readline().split())
def lip(): return list(map(int,sys.stdin.readline().split()))
def matip(n,m):
lst=[]
for i in range(n):
arr = lip()
lst.insert(i,arr)
return lst
def factors(n): # find the factors of a number
return list(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def minJumps(arr, n): #to reach from 0 to n-1 in the array in minimum steps
jumps = [0 for i in range(n)]
if (n == 0) or (arr[0] == 0):
return float('inf')
jumps[0] = 0
for i in range(1, n):
jumps[i] = float('inf')
for j in range(i):
if (i <= j + arr[j]) and (jumps[j] != float('inf')):
jumps[i] = min(jumps[i], jumps[j] + 1)
break
return jumps[n-1]
def dic(arr): # converting list into dict of count
return Counter(arr)
def check_prime(n):
if n<2:
return False
for i in range(2,int(n**(0.5))+1,2):
if n%i==0:
return False
return True
# --------------------------------------------------------- #
# sys.stdin = open('input.txt','r')
# sys.stdout = open('output.txt','w')
# --------------------------------------------------------- #
n,m = mip()
lst = []
for i in range(n):
s = sip()
arr = []
for j in range(m):
arr.append(s[j])
lst.append(arr)
# print(lst)
count = 0
if n==1 and m==1:
print(0)
else:
for i in range(n):
for j in range(m):
if lst[i][j]=='W':
if j==0 and m>1:
if lst[i][j+1]=='P':
lst[i][j]='.'
lst[i][j+1]='.'
count+=1
elif j==m-1 and m>1:
if lst[i][j-1]=='P':
lst[i][j]='.'
lst[i][j-1]='.'
count+=1
elif m>1:
if lst[i][j+1]=='P':
lst[i][j]='.'
lst[i][j+1]='.'
count+=1
elif lst[i][j-1]=='P':
lst[i][j]='.'
lst[i][j-1]='.'
count+=1
for j in range(m):
for i in range(n):
if lst[i][j]=='W':
if i==0 and n>1:
if lst[i+1][j]=='P':
lst[i][j]='.'
lst[i+1][j]='.'
count+=1
elif i==n-1 and n>1:
if lst[i-1][j]=='P':
lst[i][j]='.'
lst[i-1][j]='.'
count+=1
elif n>1:
if lst[i-1][j]=='P':
lst[i][j]='.'
lst[i-1][j]='.'
count+=1
elif lst[i+1][j]=='P':
lst[i][j]='.'
lst[i+1][j]='.'
count+=1
print(count)
# print(time.process_time())
``` | output | 1 | 87,628 | 15 | 175,257 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.
A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs.
They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf.
What is the maximum number of little pigs that may be eaten by the wolves?
Input
The first line contains integers n and m (1 β€ n, m β€ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf.
It is guaranteed that there will be at most one wolf adjacent to any little pig.
Output
Print a single number β the maximal number of little pigs that may be eaten by the wolves.
Examples
Input
2 3
PPW
W.P
Output
2
Input
3 3
P.W
.P.
W.P
Output
0
Note
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
<image>
Submitted Solution:
```
"""
Brandt Smith, Lemuel Gorion and Peter Haddad
codeforces.com
Problem 116B
"""
n, m = input().split(' ')
n = int(n)
m = int(m)
matrix = []
for i in range(n):
matrix.append(list(input()))
num = 0
for i in range(n): # iterate over all possibilites
for k in range(m):
if matrix[i][k] == 'W':
if i != 0 and matrix[i - 1][k] == 'P':
matrix[i - 1][k] = '.'
num += 1
elif i != n - 1 and matrix[i + 1][k] == 'P':
matrix[i + 1][k] = '.'
num += 1
elif k != 0 and matrix[i][k - 1] == 'P':
matrix[i][k - 1] = '.'
num += 1
elif k != m - 1 and matrix[i][k + 1] == 'P':
matrix[i][k + 1] = '.'
num += 1
print(num)
``` | instruction | 0 | 87,629 | 15 | 175,258 |
Yes | output | 1 | 87,629 | 15 | 175,259 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.
A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs.
They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf.
What is the maximum number of little pigs that may be eaten by the wolves?
Input
The first line contains integers n and m (1 β€ n, m β€ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf.
It is guaranteed that there will be at most one wolf adjacent to any little pig.
Output
Print a single number β the maximal number of little pigs that may be eaten by the wolves.
Examples
Input
2 3
PPW
W.P
Output
2
Input
3 3
P.W
.P.
W.P
Output
0
Note
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
<image>
Submitted Solution:
```
n, m = map(int, input().split())
li = []
for i in range(n):
li.append(list(input()))
res = 0
if n == 1:
for i in range(m):
if li[0][i] == 'W':
if i == 0:
try:
if li[0][i+1] == 'P':
res += 1
li[0][i+1] = 'X'
continue
except:
break
elif i == m-1:
if li[0][i-1] == 'P':
res += 1
li[0][i-1] = 'X'
continue
else:
if li[0][i-1] == 'P':
res += 1
li[0][i-1] = 'X'
continue
else:
if li[0][i + 1] == 'P':
res += 1
li[0][i + 1] = 'X'
continue
elif m == 1:
for i in range(n):
if i == 0:
if li[i][0] == 'W':
if li[i+1][0] == 'P':
res += 1
li[i+1][0] = 'X'
continue
else:
if i == n-1:
if li[i][0] == 'W':
if li[i-1][0] == 'P':
res += 1
break
else:
if li[i][0] == 'W':
if li[i-1][0] == 'P':
res += 1
li[i-1][0] = 'X'
continue
else:
if li[i+1][0] == 'P':
res += 1
li[i+1][0] = 'X'
continue
else:
for i in range(n):
for j in range(m):
if li[i][j] == 'W':
if i != 0 and i != n - 1:
if j != 0 and j != m - 1:
temp = [li[i - 1][j], li[i][j - 1], li[i][j + 1], li[i + 1][j]]
ind = {0: [i - 1, j], 1: [i, j - 1], 2: [i, j + 1], 3: [i + 1, j]}
else:
if j == 0:
temp = [li[i - 1][j], li[i][j + 1], li[i + 1][j]]
ind = {0: [i - 1, j], 1: [i, j + 1], 2: [i + 1, j]}
else:
temp = [li[i - 1][j], li[i][j - 1], li[i + 1][j]]
ind = {0: [i - 1, j], 1: [i, j - 1], 2: [i + 1, j]}
else:
if i == 0:
if j == 0:
temp = [li[i][j + 1], li[i + 1][j]]
ind = {0: [i, j + 1], 1: [i + 1, j]}
elif j == m - 1:
temp = [li[i][j - 1], li[i + 1][j]]
ind = {0: [i, j - 1], 1: [i + 1, j]}
else:
temp = [li[i + 1][j], li[i][j - 1], li[i][j + 1]]
ind = {0: [i + 1, j], 1: [i, j - 1], 2: [i, j + 1]}
else:
if j == 0:
temp = [li[i - 1][j], li[i][j + 1]]
ind = {0: [i - 1, j], 1: [i, j + 1]}
elif j == m - 1:
temp = [li[i - 1][j], li[i][j - 1]]
ind = {0: [i - 1, j], 1: [i, j - 1]}
else:
temp = [li[i - 1][j], li[i][j - 1], li[i][j + 1]]
ind = {0: [i - 1, j], 1: [i, j - 1], 2: [i, j + 1]}
for k in range(len(temp)):
if temp[k] == 'P':
res += 1
li[ind[k][0]][ind[k][1]] = 'X'
break
print(res)
``` | instruction | 0 | 87,630 | 15 | 175,260 |
Yes | output | 1 | 87,630 | 15 | 175,261 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.
A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs.
They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf.
What is the maximum number of little pigs that may be eaten by the wolves?
Input
The first line contains integers n and m (1 β€ n, m β€ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf.
It is guaranteed that there will be at most one wolf adjacent to any little pig.
Output
Print a single number β the maximal number of little pigs that may be eaten by the wolves.
Examples
Input
2 3
PPW
W.P
Output
2
Input
3 3
P.W
.P.
W.P
Output
0
Note
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
<image>
Submitted Solution:
```
n,m=map(int,input().split())
mat=[]
for i in range(n):
s=list(input())
mat.append(s)
ans=0
for i in range(n):
for j in range(m):
if mat[i][j]=='W':
if j-1>=0 and mat[i][j-1]=='P':
ans+=1
mat[i][j-1]='.'
elif j+1<m and mat[i][j+1]=='P':
ans+=1
mat[i][j+1]='.'
elif i-1>=0 and mat[i-1][j]=='P':
ans+=1
mat[i-1][j]='.'
elif i+1<n and mat[i+1][j]=='P':
ans+=1
mat[i+1][j]='.'
print(ans)
``` | instruction | 0 | 87,631 | 15 | 175,262 |
Yes | output | 1 | 87,631 | 15 | 175,263 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.
A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs.
They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf.
What is the maximum number of little pigs that may be eaten by the wolves?
Input
The first line contains integers n and m (1 β€ n, m β€ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf.
It is guaranteed that there will be at most one wolf adjacent to any little pig.
Output
Print a single number β the maximal number of little pigs that may be eaten by the wolves.
Examples
Input
2 3
PPW
W.P
Output
2
Input
3 3
P.W
.P.
W.P
Output
0
Note
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
<image>
Submitted Solution:
```
def q116b():
r, c = tuple([int(i) for i in input().split()])
row_list = [input() for i in range(r)]
grid = "".join(row_list)
total_pigs = 0
for index, character in enumerate(grid):
if(character == 'W'):
total_pigs += check_neighbors_for_pigs(index, grid, r, c)
print(total_pigs)
def check_neighbors_for_pigs(index, str, r, c):
pig_indices = []
if(index % c != 0):
# if not in leftmost row
if(str[index - 1] == 'P'):
return True
if(index % c != c-1):
# if not in rightmost row
if(str[index + 1] == 'P'):
return True
if(index // c != 0):
# if not in top row
if(str[index - c] == 'P'):
return True
if(index // c != r-1):
# if not in bottommost row
if(str[index + c] == 'P'):
return True
return False
q116b()
``` | instruction | 0 | 87,632 | 15 | 175,264 |
Yes | output | 1 | 87,632 | 15 | 175,265 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.
A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs.
They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf.
What is the maximum number of little pigs that may be eaten by the wolves?
Input
The first line contains integers n and m (1 β€ n, m β€ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf.
It is guaranteed that there will be at most one wolf adjacent to any little pig.
Output
Print a single number β the maximal number of little pigs that may be eaten by the wolves.
Examples
Input
2 3
PPW
W.P
Output
2
Input
3 3
P.W
.P.
W.P
Output
0
Note
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
<image>
Submitted Solution:
```
n, m = map(int, input().strip().split())
grid = [list(input().strip()) for _ in range(n)]
valid = lambda x, y: x >= 0 and y >= 0 and x < n and y < n
ans = 0
directions = [
[0, 0],
[1, 0],
[0, 1],
[1, 1],
[-1, 0],
[0, -1],
[-1, -1]
]
for i in range(n):
for j in range(m):
if grid[i][j] == 'W':
for x, y in directions:
if valid(i + x, j + y):
if grid[i + x][j + y] == 'P':
ans += 1
grid[i + x][j + y] = '.'
print(ans)
``` | instruction | 0 | 87,633 | 15 | 175,266 |
No | output | 1 | 87,633 | 15 | 175,267 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.
A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs.
They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf.
What is the maximum number of little pigs that may be eaten by the wolves?
Input
The first line contains integers n and m (1 β€ n, m β€ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf.
It is guaranteed that there will be at most one wolf adjacent to any little pig.
Output
Print a single number β the maximal number of little pigs that may be eaten by the wolves.
Examples
Input
2 3
PPW
W.P
Output
2
Input
3 3
P.W
.P.
W.P
Output
0
Note
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
<image>
Submitted Solution:
```
n,m=map(int,input().split())
c=0
l = []
for i in range(n):
l.append(input())
for i in range(n):
for j in range(m):
if l[i][j]!="W":
continue
for x,y in [(i,j+1),(i,j-1),(i+1,j),(i-1,j)]:
if 0<=x<n and 0<=y<n and l[x][y]=='P':
c+=1
break
print(c)
``` | instruction | 0 | 87,634 | 15 | 175,268 |
No | output | 1 | 87,634 | 15 | 175,269 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.
A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs.
They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf.
What is the maximum number of little pigs that may be eaten by the wolves?
Input
The first line contains integers n and m (1 β€ n, m β€ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf.
It is guaranteed that there will be at most one wolf adjacent to any little pig.
Output
Print a single number β the maximal number of little pigs that may be eaten by the wolves.
Examples
Input
2 3
PPW
W.P
Output
2
Input
3 3
P.W
.P.
W.P
Output
0
Note
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
<image>
Submitted Solution:
```
n, m = map(int,input().split())
s = []
for i in range(n):
word = input()
s.append([char for char in word])
count = 0
for i in range(n):
for j in range(m-1):
if i<(n-1):
if s[i][j]=='P' and s[i][j+1]=='W':
count += 1
s[i][j] = '.'
s[i][j+1] = '.'
elif s[i][j]=='W' and s[i][j+1]=='P':
count += 1
s[i][j] = '.'
s[i][j+1] == '.'
elif s[i][j]=='P' and s[i+1][j]=='W':
count += 1
s[i][j] = '.'
s[i+1][j] = '.'
elif s[i][j]=='W' and s[i+1][j]=='P':
count += 1
s[i][j] = '.'
s[i+1][j] == '.'
elif i==(n-1):
if s[i][j]=='P' and s[i][j+1]=='W':
count += 1
s[i][j] = '.'
s[i][j+1] = '.'
elif s[i][j]=='W' and s[i][j+1]=='P':
count += 1
s[i][j] = '.'
s[i][j+1] == '.'
print(count)
``` | instruction | 0 | 87,635 | 15 | 175,270 |
No | output | 1 | 87,635 | 15 | 175,271 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n Γ m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.
A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs.
They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf.
What is the maximum number of little pigs that may be eaten by the wolves?
Input
The first line contains integers n and m (1 β€ n, m β€ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each β that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf.
It is guaranteed that there will be at most one wolf adjacent to any little pig.
Output
Print a single number β the maximal number of little pigs that may be eaten by the wolves.
Examples
Input
2 3
PPW
W.P
Output
2
Input
3 3
P.W
.P.
W.P
Output
0
Note
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
<image>
Submitted Solution:
```
n, m = map(int, input().strip().split())
grid = [list(input().strip()) for _ in range(n)]
valid = lambda x, y: x >= 0 and y >= 0 and x < n and y < n
ans = 0
directions = [
[0, 0],
[1, 0],
[0, 1],
[-1, 0],
[0, -1]
]
for i in range(n):
for j in range(m):
if grid[i][j] == 'W':
for x, y in directions:
if valid(i + x, j + y):
if grid[i + x][j + y] == 'P':
ans += 1
grid[i + x][j + y] = '.'
print(ans)
``` | instruction | 0 | 87,636 | 15 | 175,272 |
No | output | 1 | 87,636 | 15 | 175,273 |
Provide a correct Python 3 solution for this coding contest problem.
You are given an H \times W grid. The square at the top-left corner will be represented by (0, 0), and the square at the bottom-right corner will be represented by (H-1, W-1).
Of those squares, N squares (x_1, y_1), (x_2, y_2), ..., (x_N, y_N) are painted black, and the other squares are painted white.
Let the shortest distance between white squares A and B be the minimum number of moves required to reach B from A visiting only white squares, where one can travel to an adjacent square sharing a side (up, down, left or right) in one move.
Since there are H Γ W - N white squares in total, there are _{(HΓW-N)}C_2 ways to choose two of the white squares.
For each of these _{(HΓW-N)}C_2 ways, find the shortest distance between the chosen squares, then find the sum of all those distances, modulo 1 000 000 007=10^9+7.
Constraints
* 1 \leq H, W \leq 10^6
* 1 \leq N \leq 30
* 0 \leq x_i \leq H-1
* 0 \leq y_i \leq W-1
* If i \neq j, then either x_i \neq x_j or y_i \neq y_j.
* There is at least one white square.
* For every pair of white squares A and B, it is possible to reach B from A visiting only white squares.
Input
Input is given from Standard Input in the following format:
H W
N
x_1 y_1
x_2 y_2
:
x_N y_N
Output
Print the sum of the shortest distances, modulo 10^9+7.
Examples
Input
2 3
1
1 1
Output
20
Input
2 3
1
1 2
Output
16
Input
3 3
1
1 1
Output
64
Input
4 4
4
0 1
1 1
2 1
2 2
Output
268
Input
1000000 1000000
1
0 0
Output
333211937 | instruction | 0 | 88,225 | 15 | 176,450 |
"Correct Solution:
```
from collections import deque, Counter
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
mod = 10**9 + 7
h, w = map(int, input().split())
n = int(input())
ans = 0
black = []
row = Counter()
column = Counter()
for _ in range(n):
x, y = map(int, input().split())
row[x] += 1
column[y] += 1
black.append((x, y))
row[h] += 1
column[w] += 1
def sqsum(x):
return x*(x+1)*(2*x+1)//6
pre = -1
top = 0
bottom = h*w - n
area = []
for i in sorted(row.keys()):
if i == pre+2:
top += w
bottom -= w
area.append([-1, 1])
elif i > pre+2:
ans += (i-pre-2)*top*bottom + ((i-pre-2)*(i-pre-1)//2) * \
w*(bottom-top) - sqsum(i-pre-2)*(w**2)
ans %= mod
top += (i-pre-1)*w
bottom -= (i-pre-1)*w
area.append([-1, i-pre-1])
if i != h:
top += w-row[i]
bottom -= w-row[i]
area.append([i])
pre = i
R = len(area)
pre = -1
left = 0
right = h*w-n
area2 = []
for j in sorted(column.keys()):
if j == pre+2:
left += h
right -= h
area2.append([area[i][1] if area[i][0] == -1 else 1 for i in range(R)])
elif j > pre+2:
ans += (j-pre-2)*left*right + ((j-pre-2)*(j-pre-1)//2) * \
h*(right-left) - sqsum(j-pre-2)*(h**2)
ans %= mod
left += (j-pre-1)*h
right -= (j-pre-1)*h
area2.append([(j-pre-1)*area[i][1] if area[i][0]
== -1 else (j-pre-1) for i in range(R)])
if j != w:
left += h-column[j]
right -= h-column[j]
tmp = []
for i in range(R):
if area[i][0] == -1:
tmp.append(area[i][1])
else:
if (area[i][0], j) in black:
tmp.append(0)
else:
tmp.append(1)
area2.append(tmp)
pre = j
C = len(area2)
area2 = [[area2[j][i] for j in range(C)] for i in range(R)]
vec = [[1, 0], [0, 1], [-1, 0], [0, -1]]
def bfs(p, q):
dist = [[10**5 for _ in range(C)] for __ in range(R)]
visited = [[False for _ in range(C)] for __ in range(R)]
dist[p][q] = 0
visited[p][q] = True
q = deque([(p, q)])
while q:
x, y = q.popleft()
for dx, dy in vec:
if 0 <= x+dx < R and 0 <= y+dy < C and area2[x+dx][y+dy] != 0:
if not visited[x+dx][y+dy]:
dist[x+dx][y+dy] = dist[x][y] + 1
visited[x+dx][y+dy] = True
q.append((x+dx, y+dy))
return dist
ans2 = 0
for x in range(R*C):
i = x//C
j = x % C
if area2[i][j] == 0:
continue
d = bfs(i, j)
for y in range(R*C):
k = y//C
l = y % C
if area2[k][l] == 0:
continue
ans2 += area2[i][j]*area2[k][l]*d[k][l]
ans2 %= mod
ans2 *= pow(2, mod-2, mod)
print((ans+ans2) % mod)
``` | output | 1 | 88,225 | 15 | 176,451 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an H \times W grid. The square at the top-left corner will be represented by (0, 0), and the square at the bottom-right corner will be represented by (H-1, W-1).
Of those squares, N squares (x_1, y_1), (x_2, y_2), ..., (x_N, y_N) are painted black, and the other squares are painted white.
Let the shortest distance between white squares A and B be the minimum number of moves required to reach B from A visiting only white squares, where one can travel to an adjacent square sharing a side (up, down, left or right) in one move.
Since there are H Γ W - N white squares in total, there are _{(HΓW-N)}C_2 ways to choose two of the white squares.
For each of these _{(HΓW-N)}C_2 ways, find the shortest distance between the chosen squares, then find the sum of all those distances, modulo 1 000 000 007=10^9+7.
Constraints
* 1 \leq H, W \leq 10^6
* 1 \leq N \leq 30
* 0 \leq x_i \leq H-1
* 0 \leq y_i \leq W-1
* If i \neq j, then either x_i \neq x_j or y_i \neq y_j.
* There is at least one white square.
* For every pair of white squares A and B, it is possible to reach B from A visiting only white squares.
Input
Input is given from Standard Input in the following format:
H W
N
x_1 y_1
x_2 y_2
:
x_N y_N
Output
Print the sum of the shortest distances, modulo 10^9+7.
Examples
Input
2 3
1
1 1
Output
20
Input
2 3
1
1 2
Output
16
Input
3 3
1
1 1
Output
64
Input
4 4
4
0 1
1 1
2 1
2 2
Output
268
Input
1000000 1000000
1
0 0
Output
333211937
Submitted Solution:
```
from collections import deque
mod = 10**9 + 7
h, w = map(int, input().split())
n = int(input())
ans = 0
black = []
row = [0]*h
column = [0]*w
for _ in range(n):
x, y = map(int, input().split())
row[x] += 1
column[y] += 1
black.append([x, y])
cnt = 0
top = 0
bottom = h*w - n
area = []
for i in range(h):
if row[i] == 0:
cnt += 1
if i != h-1 and row[i+1] == 0:
top += w
bottom -= w
ans += top*bottom
ans %= mod
else:
area.append([cnt for _ in range(w)])
else:
top += w-row[i]
bottom -= w-row[i]
area.append([1 for _ in range(w)])
for x, y in black:
if x == i:
area[-1][y] = 0
cnt = 0
R = len(area)
cnt = 0
left = 0
right = h*w - n
area2 = []
for j in range(w):
if column[j] == 0:
cnt += 1
if j != w-1 and column[j+1] == 0:
left += h
right -= h
ans += left*right
ans %= mod
else:
area2.append([cnt*area[i][j] for i in range(R)])
else:
left += w-column[j]
right -= w-column[j]
area2.append([area[i][j] for i in range(R)])
cnt = 0
C = len(area2)
vec = [[1, 0], [0, 1], [-1, 0], [0, -1]]
def bfs(p, q):
dist = [[10**9 for _ in range(R)] for __ in range(C)]
visited = [[False for _ in range(R)] for __ in range(C)]
dist[p][q] = 0
visited[p][q] = True
q = deque([(p, q)])
while q:
x, y = q.popleft()
for dx, dy in vec:
if 0 <= x+dx < C and 0 <= y+dy < R and area2[x+dx][y+dy] != 0:
if not visited[x+dx][y+dy]:
dist[x+dx][y+dy] = dist[x][y] + 1
visited[x+dx][y+dy] = True
q.append((x+dx, y+dy))
return dist
ans2 = 0
for i in range(C):
for j in range(R):
d = bfs(i, j)
for k in range(C):
for l in range(R):
ans2 += area2[i][j]*area2[k][l]*d[k][l]
ans2 %= mod
ans2 *= pow(2, mod-2, mod)
print((ans+ans2) % mod)
``` | instruction | 0 | 88,226 | 15 | 176,452 |
No | output | 1 | 88,226 | 15 | 176,453 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an H \times W grid. The square at the top-left corner will be represented by (0, 0), and the square at the bottom-right corner will be represented by (H-1, W-1).
Of those squares, N squares (x_1, y_1), (x_2, y_2), ..., (x_N, y_N) are painted black, and the other squares are painted white.
Let the shortest distance between white squares A and B be the minimum number of moves required to reach B from A visiting only white squares, where one can travel to an adjacent square sharing a side (up, down, left or right) in one move.
Since there are H Γ W - N white squares in total, there are _{(HΓW-N)}C_2 ways to choose two of the white squares.
For each of these _{(HΓW-N)}C_2 ways, find the shortest distance between the chosen squares, then find the sum of all those distances, modulo 1 000 000 007=10^9+7.
Constraints
* 1 \leq H, W \leq 10^6
* 1 \leq N \leq 30
* 0 \leq x_i \leq H-1
* 0 \leq y_i \leq W-1
* If i \neq j, then either x_i \neq x_j or y_i \neq y_j.
* There is at least one white square.
* For every pair of white squares A and B, it is possible to reach B from A visiting only white squares.
Input
Input is given from Standard Input in the following format:
H W
N
x_1 y_1
x_2 y_2
:
x_N y_N
Output
Print the sum of the shortest distances, modulo 10^9+7.
Examples
Input
2 3
1
1 1
Output
20
Input
2 3
1
1 2
Output
16
Input
3 3
1
1 1
Output
64
Input
4 4
4
0 1
1 1
2 1
2 2
Output
268
Input
1000000 1000000
1
0 0
Output
333211937
Submitted Solution:
```
from collections import Counter
mod = 10**9+7
h, w = map(int, input().split())
n = int(input())
ans = 0
black = []
row = Counter()
column = Counter()
for _ in range(n):
x, y = map(int, input().split())
row[x] += 1
column[y] += 1
black.append((x, y))
row[h] += 1
column[w] += 1
def sqsum(x):
return x*(x+1)*(2*x+1)//6
pre = -1
top = 0
bottom = h*w - n
area = []
for i in sorted(row.keys()):
if i == pre+2:
top += w
bottom -= w
area.append([-1, 1])
elif i > pre+2:
# for x in range(pre+1, i-1):
# top += w
# bottom -= w
# ans += top*bottom
# ans %= mod
ans += (i-pre-2)*top*bottom + ((i-pre-2)*(i-pre-1)//2) * \
w*(bottom-top) - sqsum(i-pre-2)*(w**2)
ans %= mod
top += (i-pre-1)*w
bottom -= (i-pre-1)*w
area.append([-1, i-pre-1])
if i != h:
top += w-row[i]
bottom -= w-row[i]
area.append([i])
pre = i
R = len(area)
pre = -1
left = 0
right = h*w-n
area2 = []
for j in sorted(column.keys()):
if j == pre+2:
left += h
right -= h
area2.append([area[i][1] if area[i][0] == -1 else 1 for i in range(R)])
elif j > pre+2:
ans += (j-pre-2)*left*right + ((j-pre-2)*(j-pre-1)//2) * \
h*(right-left) - sqsum(j-pre-2)*(h**2)
ans %= mod
left += (j-pre-1)*h
right -= (j-pre-1)*h
area2.append([(j-pre-1)*area[i][1] if area[i][0]
== -1 else (j-pre-1) for i in range(R)])
if j != w:
left += h-column[j]
right -= h-column[j]
tmp = []
for i in range(R):
if area[i][0] == -1:
tmp.append(area[i][1])
else:
if (area[i][0], j) in black:
tmp.append(0)
else:
tmp.append(1)
area2.append(tmp)
pre = j
C = len(area2)
area2 = [[area2[j][i] % mod for j in range(C)] for i in range(R)]
# print(R, C)
# for x in area2:
# print(*x)
# exit()
INF = 10**9+7
d = [[INF]*(R*C) for _ in range(R*C)]
vec = [[1, 0], [0, 1], [-1, 0], [0, -1]]
for i in range(R*C):
x = i//C
y = i % C
if area2[x][y] == 0:
continue
for dx, dy in vec:
j = (x+dx)*C + y+dy
if 0 <= x+dx < R and 0 <= y+dy < C and area2[x+dx][y+dy] != 0:
d[i][j] = d[j][i] = 1
def warfl(d, card):
for i in range(card):
d[i][i] = 0
for k in range(card):
for i in range(card):
for j in range(card):
if d[i][k] != INF and d[k][j] != INF:
d[i][j] = min(d[i][j], d[i][k] + d[k][j])
return d
ans2 = 0
d = warfl(d, R*C)
for i in range(R*C):
for j in range(R*C):
ans2 += area2[i//C][i % C]*area2[j//C][j % C]*d[i][j]
ans2 %= mod
ans2 *= pow(2, mod-2, mod)
print((ans+ans2) % mod)
``` | instruction | 0 | 88,227 | 15 | 176,454 |
No | output | 1 | 88,227 | 15 | 176,455 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an H \times W grid. The square at the top-left corner will be represented by (0, 0), and the square at the bottom-right corner will be represented by (H-1, W-1).
Of those squares, N squares (x_1, y_1), (x_2, y_2), ..., (x_N, y_N) are painted black, and the other squares are painted white.
Let the shortest distance between white squares A and B be the minimum number of moves required to reach B from A visiting only white squares, where one can travel to an adjacent square sharing a side (up, down, left or right) in one move.
Since there are H Γ W - N white squares in total, there are _{(HΓW-N)}C_2 ways to choose two of the white squares.
For each of these _{(HΓW-N)}C_2 ways, find the shortest distance between the chosen squares, then find the sum of all those distances, modulo 1 000 000 007=10^9+7.
Constraints
* 1 \leq H, W \leq 10^6
* 1 \leq N \leq 30
* 0 \leq x_i \leq H-1
* 0 \leq y_i \leq W-1
* If i \neq j, then either x_i \neq x_j or y_i \neq y_j.
* There is at least one white square.
* For every pair of white squares A and B, it is possible to reach B from A visiting only white squares.
Input
Input is given from Standard Input in the following format:
H W
N
x_1 y_1
x_2 y_2
:
x_N y_N
Output
Print the sum of the shortest distances, modulo 10^9+7.
Examples
Input
2 3
1
1 1
Output
20
Input
2 3
1
1 2
Output
16
Input
3 3
1
1 1
Output
64
Input
4 4
4
0 1
1 1
2 1
2 2
Output
268
Input
1000000 1000000
1
0 0
Output
333211937
Submitted Solution:
```
from collections import deque, Counter
mod = 10**9 + 7
h, w = map(int, input().split())
n = int(input())
ans = 0
black = []
row = Counter()
column = Counter()
for _ in range(n):
x, y = map(int, input().split())
row[x] += 1
column[y] += 1
black.append((x, y))
row[h] += 1
column[w] += 1
def sqsum(x):
return x*(x+1)*(2*x+1)//6
pre = -1
top = 0
bottom = h*w - n
area = []
for i in sorted(row.keys()):
if i == pre+2:
top += w
bottom -= w
area.append([-1, 1])
elif i > pre+2:
# for x in range(pre+1, i-1):
# top += w
# bottom -= w
# ans += top*bottom
# ans %= mod
ans += (i-pre-2)*top*bottom + ((i-pre-2)*(i-pre-1)//2) *w*(bottom-top) - sqsum(i-pre-2)*(w**2)
ans %= mod
top += (i-pre-1)*w
bottom -= (i-pre-1)*w
area.append([-1, i-pre-1])
if i != h:
top += w-row[i]
bottom -= w-row[i]
area.append([i])
pre = i
R = len(area)
pre = -1
left = 0
right = h*w-n
area2 = []
for j in sorted(column.keys()):
if j == pre+2:
left += h
right -= h
area2.append([area[i][1] if area[i][0] == -1 else 1 for i in range(R)])
elif j > pre+2:
ans += (j-pre-2)*left*right + ((j-pre-2)*(j-pre-1)//2) *h*(right-left) - sqsum(j-pre-2)*(h**2)
ans %= mod
left += (j-pre-1)*h
right -= (j-pre-1)*h
area2.append([(j-pre-1)*area[i][1] if area[i][0]
== -1 else (j-pre-1) for i in range(R)])
if j != w:
left += h-column[j]
right -= h-column[j]
tmp = []
for i in range(R):
if area[i][0] == -1:
tmp.append(area[i][1])
else:
if (area[i][0], j) in black:
tmp.append(0)
else:
tmp.append(1)
area2.append(tmp)
pre = j
C = len(area2)
area2 = [[area2[j][i] for j in range(C)] for i in range(R)]
vec = [[1, 0], [0, 1], [-1, 0], [0, -1]]
def bfs(p, q):
dist = [[10**5 for _ in range(C)] for __ in range(R)]
visited = [[False for _ in range(C)] for __ in range(R)]
dist[p][q] = 0
visited[p][q] = True
q = deque([(p, q)])
while q:
x, y = q.popleft()
for dx, dy in vec:
if 0 <= x+dx < R and 0 <= y+dy < C and area2[x+dx][y+dy] != 0:
if not visited[x+dx][y+dy]:
dist[x+dx][y+dy] = dist[x][y] + 1
visited[x+dx][y+dy] = True
q.append((x+dx, y+dy))
return dist
ans2 = 0
for i in range(R):
for j in range(C):
if area2[i][j] == 0:
continue
d = bfs(i, j)
for k in range(R):
for l in range(C):
if area2[k][l] == 0:
continue
ans2 += area2[i][j]*area2[k][l]*d[k][l]
ans2 %= mod
ans2 *= pow(2, mod-2, mod)
print((ans+ans2) % mod)
``` | instruction | 0 | 88,228 | 15 | 176,456 |
No | output | 1 | 88,228 | 15 | 176,457 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an H \times W grid. The square at the top-left corner will be represented by (0, 0), and the square at the bottom-right corner will be represented by (H-1, W-1).
Of those squares, N squares (x_1, y_1), (x_2, y_2), ..., (x_N, y_N) are painted black, and the other squares are painted white.
Let the shortest distance between white squares A and B be the minimum number of moves required to reach B from A visiting only white squares, where one can travel to an adjacent square sharing a side (up, down, left or right) in one move.
Since there are H Γ W - N white squares in total, there are _{(HΓW-N)}C_2 ways to choose two of the white squares.
For each of these _{(HΓW-N)}C_2 ways, find the shortest distance between the chosen squares, then find the sum of all those distances, modulo 1 000 000 007=10^9+7.
Constraints
* 1 \leq H, W \leq 10^6
* 1 \leq N \leq 30
* 0 \leq x_i \leq H-1
* 0 \leq y_i \leq W-1
* If i \neq j, then either x_i \neq x_j or y_i \neq y_j.
* There is at least one white square.
* For every pair of white squares A and B, it is possible to reach B from A visiting only white squares.
Input
Input is given from Standard Input in the following format:
H W
N
x_1 y_1
x_2 y_2
:
x_N y_N
Output
Print the sum of the shortest distances, modulo 10^9+7.
Examples
Input
2 3
1
1 1
Output
20
Input
2 3
1
1 2
Output
16
Input
3 3
1
1 1
Output
64
Input
4 4
4
0 1
1 1
2 1
2 2
Output
268
Input
1000000 1000000
1
0 0
Output
333211937
Submitted Solution:
```
print("bandzebo")
``` | instruction | 0 | 88,229 | 15 | 176,458 |
No | output | 1 | 88,229 | 15 | 176,459 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't been born yet (in English, well), he decides to test a problem and asks his uncle.
After a long time thinking, Tolik's uncle hasn't any ideas on how to solve it. But, he doesn't want to tell Tolik about his inability to solve it, so he hasn't found anything better than asking you how to solve this task.
In this task you are given a cell field n β
m, consisting of n rows and m columns, where point's coordinates (x, y) mean it is situated in the x-th row and y-th column, considering numeration from one (1 β€ x β€ n, 1 β€ y β€ m). Initially, you stand in the cell (1, 1). Every move you can jump from cell (x, y), which you stand in, by any non-zero vector (dx, dy), thus you will stand in the (x+dx, y+dy) cell. Obviously, you can't leave the field, but also there is one more important condition β you're not allowed to use one vector twice. Your task is to visit each cell of the field exactly once (the initial cell is considered as already visited).
Tolik's uncle is a very respectful person. Help him to solve this task!
Input
The first and only line contains two positive integers n, m (1 β€ n β
m β€ 10^{6}) β the number of rows and columns of the field respectively.
Output
Print "-1" (without quotes) if it is impossible to visit every cell exactly once.
Else print n β
m pairs of integers, i-th from them should contain two integers x_i, y_i (1 β€ x_i β€ n, 1 β€ y_i β€ m) β cells of the field in order of visiting, so that all of them are distinct and vectors of jumps between them are distinct too.
Notice that the first cell should have (1, 1) coordinates, according to the statement.
Examples
Input
2 3
Output
1 1
1 3
1 2
2 2
2 3
2 1
Input
1 1
Output
1 1
Note
The vectors from the first example in the order of making jumps are (0, 2), (0, -1), (1, 0), (0, 1), (0, -2). | instruction | 0 | 88,431 | 15 | 176,862 |
Tags: constructive algorithms
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#######################################
n,m=map(int,input().split())
cnt=0
i1=0
j1=0
i2=n-1
j2=m-1
while(cnt<n*m):
if(cnt%2==0):
print(i1+1,j1+1)
if(j1==m-1):
i1+=1
j1=0
else:
j1+=1
else:
print(i2+1,j2+1)
if(j2==0):
j2=m-1
i2-=1
else:
j2-=1
cnt+=1
``` | output | 1 | 88,431 | 15 | 176,863 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't been born yet (in English, well), he decides to test a problem and asks his uncle.
After a long time thinking, Tolik's uncle hasn't any ideas on how to solve it. But, he doesn't want to tell Tolik about his inability to solve it, so he hasn't found anything better than asking you how to solve this task.
In this task you are given a cell field n β
m, consisting of n rows and m columns, where point's coordinates (x, y) mean it is situated in the x-th row and y-th column, considering numeration from one (1 β€ x β€ n, 1 β€ y β€ m). Initially, you stand in the cell (1, 1). Every move you can jump from cell (x, y), which you stand in, by any non-zero vector (dx, dy), thus you will stand in the (x+dx, y+dy) cell. Obviously, you can't leave the field, but also there is one more important condition β you're not allowed to use one vector twice. Your task is to visit each cell of the field exactly once (the initial cell is considered as already visited).
Tolik's uncle is a very respectful person. Help him to solve this task!
Input
The first and only line contains two positive integers n, m (1 β€ n β
m β€ 10^{6}) β the number of rows and columns of the field respectively.
Output
Print "-1" (without quotes) if it is impossible to visit every cell exactly once.
Else print n β
m pairs of integers, i-th from them should contain two integers x_i, y_i (1 β€ x_i β€ n, 1 β€ y_i β€ m) β cells of the field in order of visiting, so that all of them are distinct and vectors of jumps between them are distinct too.
Notice that the first cell should have (1, 1) coordinates, according to the statement.
Examples
Input
2 3
Output
1 1
1 3
1 2
2 2
2 3
2 1
Input
1 1
Output
1 1
Note
The vectors from the first example in the order of making jumps are (0, 2), (0, -1), (1, 0), (0, 1), (0, -2). | instruction | 0 | 88,432 | 15 | 176,864 |
Tags: constructive algorithms
Correct Solution:
```
n, m = tuple(map(int, input().split()))
if n==1 and m==1:
print('1 1')
exit()
def onerow(r, m):
for i in range(1, (m+1)//2 + 1):
# print('Here', i)
print(str(r) + ' ' + str(i))
if m%2 == 0 or i != (m+1)//2:
print(str(r) + ' ' + str(m+1-i))
def tworow(r1, r2, m):
for i in range(1, m+1):
print(str(r1) + ' ' + str(i))
print(str(r2) + ' ' + str(m+1-i))
if n%2 == 0:
for i in range(1, n//2 + 1):
tworow(i, n+1-i, m)
if n%2 == 1:
for i in range(1, n//2 + 1):
tworow(i, n+1-i, m)
onerow(n//2 + 1, m)
``` | output | 1 | 88,432 | 15 | 176,865 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't been born yet (in English, well), he decides to test a problem and asks his uncle.
After a long time thinking, Tolik's uncle hasn't any ideas on how to solve it. But, he doesn't want to tell Tolik about his inability to solve it, so he hasn't found anything better than asking you how to solve this task.
In this task you are given a cell field n β
m, consisting of n rows and m columns, where point's coordinates (x, y) mean it is situated in the x-th row and y-th column, considering numeration from one (1 β€ x β€ n, 1 β€ y β€ m). Initially, you stand in the cell (1, 1). Every move you can jump from cell (x, y), which you stand in, by any non-zero vector (dx, dy), thus you will stand in the (x+dx, y+dy) cell. Obviously, you can't leave the field, but also there is one more important condition β you're not allowed to use one vector twice. Your task is to visit each cell of the field exactly once (the initial cell is considered as already visited).
Tolik's uncle is a very respectful person. Help him to solve this task!
Input
The first and only line contains two positive integers n, m (1 β€ n β
m β€ 10^{6}) β the number of rows and columns of the field respectively.
Output
Print "-1" (without quotes) if it is impossible to visit every cell exactly once.
Else print n β
m pairs of integers, i-th from them should contain two integers x_i, y_i (1 β€ x_i β€ n, 1 β€ y_i β€ m) β cells of the field in order of visiting, so that all of them are distinct and vectors of jumps between them are distinct too.
Notice that the first cell should have (1, 1) coordinates, according to the statement.
Examples
Input
2 3
Output
1 1
1 3
1 2
2 2
2 3
2 1
Input
1 1
Output
1 1
Note
The vectors from the first example in the order of making jumps are (0, 2), (0, -1), (1, 0), (0, 1), (0, -2). | instruction | 0 | 88,433 | 15 | 176,866 |
Tags: constructive algorithms
Correct Solution:
```
if __name__ == "__main__":
n, m = [int(x) for x in input().split()]
#n, m = map(int, input().split())
answer = []
for i in range((n + 1) // 2):
for j in range(m):
if i == n // 2 and j == m // 2:
if m % 2 == 0:
break
answer.append("{} {}".format(i + 1, j + 1))
break
answer.append("{} {}".format(i + 1, j + 1))
answer.append("{} {}".format(n - i, m - j))
print("\n".join(answer))
``` | output | 1 | 88,433 | 15 | 176,867 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't been born yet (in English, well), he decides to test a problem and asks his uncle.
After a long time thinking, Tolik's uncle hasn't any ideas on how to solve it. But, he doesn't want to tell Tolik about his inability to solve it, so he hasn't found anything better than asking you how to solve this task.
In this task you are given a cell field n β
m, consisting of n rows and m columns, where point's coordinates (x, y) mean it is situated in the x-th row and y-th column, considering numeration from one (1 β€ x β€ n, 1 β€ y β€ m). Initially, you stand in the cell (1, 1). Every move you can jump from cell (x, y), which you stand in, by any non-zero vector (dx, dy), thus you will stand in the (x+dx, y+dy) cell. Obviously, you can't leave the field, but also there is one more important condition β you're not allowed to use one vector twice. Your task is to visit each cell of the field exactly once (the initial cell is considered as already visited).
Tolik's uncle is a very respectful person. Help him to solve this task!
Input
The first and only line contains two positive integers n, m (1 β€ n β
m β€ 10^{6}) β the number of rows and columns of the field respectively.
Output
Print "-1" (without quotes) if it is impossible to visit every cell exactly once.
Else print n β
m pairs of integers, i-th from them should contain two integers x_i, y_i (1 β€ x_i β€ n, 1 β€ y_i β€ m) β cells of the field in order of visiting, so that all of them are distinct and vectors of jumps between them are distinct too.
Notice that the first cell should have (1, 1) coordinates, according to the statement.
Examples
Input
2 3
Output
1 1
1 3
1 2
2 2
2 3
2 1
Input
1 1
Output
1 1
Note
The vectors from the first example in the order of making jumps are (0, 2), (0, -1), (1, 0), (0, 1), (0, -2). | instruction | 0 | 88,434 | 15 | 176,868 |
Tags: constructive algorithms
Correct Solution:
```
inp = list(map(int,input().split(" ")))
n = int(inp[0])
m = int(inp[1])
x = 1
y = 1
cells = n * m
up = n
down = 1
upiter = m
downiter = 1
flag = 0
count = 0
Ans = []
while(up >= down):
# print("up and down are ", up, down)
while(upiter >= 1 or downiter <= m):
if(flag == 0):
# print(str(down) + " " + str(downiter))
Ans.append(str(down) + " " + str(downiter))
downiter += 1
else:
# print(str(up) + " " + str(upiter))
Ans.append(str(up) + " " + str(upiter))
upiter -= 1
count += 1
if(count == cells):
break
flag = flag ^ 1
flag = 0
up -= 1
down += 1
upiter = m
downiter = 1
# print("count is " ,count)
if(count != cells):
print(-1)
else:
for x in Ans:
print(x)
``` | output | 1 | 88,434 | 15 | 176,869 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't been born yet (in English, well), he decides to test a problem and asks his uncle.
After a long time thinking, Tolik's uncle hasn't any ideas on how to solve it. But, he doesn't want to tell Tolik about his inability to solve it, so he hasn't found anything better than asking you how to solve this task.
In this task you are given a cell field n β
m, consisting of n rows and m columns, where point's coordinates (x, y) mean it is situated in the x-th row and y-th column, considering numeration from one (1 β€ x β€ n, 1 β€ y β€ m). Initially, you stand in the cell (1, 1). Every move you can jump from cell (x, y), which you stand in, by any non-zero vector (dx, dy), thus you will stand in the (x+dx, y+dy) cell. Obviously, you can't leave the field, but also there is one more important condition β you're not allowed to use one vector twice. Your task is to visit each cell of the field exactly once (the initial cell is considered as already visited).
Tolik's uncle is a very respectful person. Help him to solve this task!
Input
The first and only line contains two positive integers n, m (1 β€ n β
m β€ 10^{6}) β the number of rows and columns of the field respectively.
Output
Print "-1" (without quotes) if it is impossible to visit every cell exactly once.
Else print n β
m pairs of integers, i-th from them should contain two integers x_i, y_i (1 β€ x_i β€ n, 1 β€ y_i β€ m) β cells of the field in order of visiting, so that all of them are distinct and vectors of jumps between them are distinct too.
Notice that the first cell should have (1, 1) coordinates, according to the statement.
Examples
Input
2 3
Output
1 1
1 3
1 2
2 2
2 3
2 1
Input
1 1
Output
1 1
Note
The vectors from the first example in the order of making jumps are (0, 2), (0, -1), (1, 0), (0, 1), (0, -2). | instruction | 0 | 88,435 | 15 | 176,870 |
Tags: constructive algorithms
Correct Solution:
```
n,m = list(map(int, input().strip().split()))
for row in range(1, n // 2 + 1):
for col in range(1, m+1):
print(str(row) + " " + str(col))
print(str(n+1-row) + " " + str(m+1-col))
if n % 2 == 1:
row = n // 2 + 1
for col in range(1, m // 2 + 1):
print(str(row) + " " + str(col))
print(str(row) + " " + str(m+1-col))
if m % 2 == 1:
print(str(row) + " " + str(m // 2 + 1))
``` | output | 1 | 88,435 | 15 | 176,871 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't been born yet (in English, well), he decides to test a problem and asks his uncle.
After a long time thinking, Tolik's uncle hasn't any ideas on how to solve it. But, he doesn't want to tell Tolik about his inability to solve it, so he hasn't found anything better than asking you how to solve this task.
In this task you are given a cell field n β
m, consisting of n rows and m columns, where point's coordinates (x, y) mean it is situated in the x-th row and y-th column, considering numeration from one (1 β€ x β€ n, 1 β€ y β€ m). Initially, you stand in the cell (1, 1). Every move you can jump from cell (x, y), which you stand in, by any non-zero vector (dx, dy), thus you will stand in the (x+dx, y+dy) cell. Obviously, you can't leave the field, but also there is one more important condition β you're not allowed to use one vector twice. Your task is to visit each cell of the field exactly once (the initial cell is considered as already visited).
Tolik's uncle is a very respectful person. Help him to solve this task!
Input
The first and only line contains two positive integers n, m (1 β€ n β
m β€ 10^{6}) β the number of rows and columns of the field respectively.
Output
Print "-1" (without quotes) if it is impossible to visit every cell exactly once.
Else print n β
m pairs of integers, i-th from them should contain two integers x_i, y_i (1 β€ x_i β€ n, 1 β€ y_i β€ m) β cells of the field in order of visiting, so that all of them are distinct and vectors of jumps between them are distinct too.
Notice that the first cell should have (1, 1) coordinates, according to the statement.
Examples
Input
2 3
Output
1 1
1 3
1 2
2 2
2 3
2 1
Input
1 1
Output
1 1
Note
The vectors from the first example in the order of making jumps are (0, 2), (0, -1), (1, 0), (0, 1), (0, -2). | instruction | 0 | 88,436 | 15 | 176,872 |
Tags: constructive algorithms
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#######################################
n,m=map(int,input().split())
ans=[]
i,j=1,1
x,y=n,m
for k in range(n*m//2):
ans.append([i,j])
ans.append([x,y])
j+=1
if j==m+1:
i+=1
j=1
y-=1
if y==0:
x-=1
y=m
if (n*m)%2:
ans.append([i,j])
for i in ans:
print(*i)
``` | output | 1 | 88,436 | 15 | 176,873 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't been born yet (in English, well), he decides to test a problem and asks his uncle.
After a long time thinking, Tolik's uncle hasn't any ideas on how to solve it. But, he doesn't want to tell Tolik about his inability to solve it, so he hasn't found anything better than asking you how to solve this task.
In this task you are given a cell field n β
m, consisting of n rows and m columns, where point's coordinates (x, y) mean it is situated in the x-th row and y-th column, considering numeration from one (1 β€ x β€ n, 1 β€ y β€ m). Initially, you stand in the cell (1, 1). Every move you can jump from cell (x, y), which you stand in, by any non-zero vector (dx, dy), thus you will stand in the (x+dx, y+dy) cell. Obviously, you can't leave the field, but also there is one more important condition β you're not allowed to use one vector twice. Your task is to visit each cell of the field exactly once (the initial cell is considered as already visited).
Tolik's uncle is a very respectful person. Help him to solve this task!
Input
The first and only line contains two positive integers n, m (1 β€ n β
m β€ 10^{6}) β the number of rows and columns of the field respectively.
Output
Print "-1" (without quotes) if it is impossible to visit every cell exactly once.
Else print n β
m pairs of integers, i-th from them should contain two integers x_i, y_i (1 β€ x_i β€ n, 1 β€ y_i β€ m) β cells of the field in order of visiting, so that all of them are distinct and vectors of jumps between them are distinct too.
Notice that the first cell should have (1, 1) coordinates, according to the statement.
Examples
Input
2 3
Output
1 1
1 3
1 2
2 2
2 3
2 1
Input
1 1
Output
1 1
Note
The vectors from the first example in the order of making jumps are (0, 2), (0, -1), (1, 0), (0, 1), (0, -2). | instruction | 0 | 88,437 | 15 | 176,874 |
Tags: constructive algorithms
Correct Solution:
```
import sys
import time
input = sys.stdin.readline
n,m=map(int,input().split())
x1=1
y1=1
x2=n
y2=m
arr=[]
for i in range(n*m):
if i%2==0:
arr.append(str(x1)+" "+str(y1))
if x1%2==1:
if y1<m:
y1+=1
else:
x1+=1
else:
if y1>1:
y1-=1
else:
x1+=1
else:
arr.append(str(x2)+" "+str(y2))
if (n-x2)%2==0:
if y2>1:
y2-=1
else:
x2-=1
else:
if y2<m:
y2+=1
else:
x2-=1
print("\n".join(arr))
``` | output | 1 | 88,437 | 15 | 176,875 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't been born yet (in English, well), he decides to test a problem and asks his uncle.
After a long time thinking, Tolik's uncle hasn't any ideas on how to solve it. But, he doesn't want to tell Tolik about his inability to solve it, so he hasn't found anything better than asking you how to solve this task.
In this task you are given a cell field n β
m, consisting of n rows and m columns, where point's coordinates (x, y) mean it is situated in the x-th row and y-th column, considering numeration from one (1 β€ x β€ n, 1 β€ y β€ m). Initially, you stand in the cell (1, 1). Every move you can jump from cell (x, y), which you stand in, by any non-zero vector (dx, dy), thus you will stand in the (x+dx, y+dy) cell. Obviously, you can't leave the field, but also there is one more important condition β you're not allowed to use one vector twice. Your task is to visit each cell of the field exactly once (the initial cell is considered as already visited).
Tolik's uncle is a very respectful person. Help him to solve this task!
Input
The first and only line contains two positive integers n, m (1 β€ n β
m β€ 10^{6}) β the number of rows and columns of the field respectively.
Output
Print "-1" (without quotes) if it is impossible to visit every cell exactly once.
Else print n β
m pairs of integers, i-th from them should contain two integers x_i, y_i (1 β€ x_i β€ n, 1 β€ y_i β€ m) β cells of the field in order of visiting, so that all of them are distinct and vectors of jumps between them are distinct too.
Notice that the first cell should have (1, 1) coordinates, according to the statement.
Examples
Input
2 3
Output
1 1
1 3
1 2
2 2
2 3
2 1
Input
1 1
Output
1 1
Note
The vectors from the first example in the order of making jumps are (0, 2), (0, -1), (1, 0), (0, 1), (0, -2). | instruction | 0 | 88,438 | 15 | 176,876 |
Tags: constructive algorithms
Correct Solution:
```
import sys
n, m = list(map(int,sys.stdin.readline().strip().split()))
for i in range (0, n * m):
if i % 2 == 0:
c = i // (2 * n)
r = (i % (2 * n)) // 2
else:
c = m - 1 - i // (2 * n)
r = n - 1 - (i % (2 * n)) // 2
sys.stdout.write(str(r+1) + " " + str(c + 1) + "\n")
``` | output | 1 | 88,438 | 15 | 176,877 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't been born yet (in English, well), he decides to test a problem and asks his uncle.
After a long time thinking, Tolik's uncle hasn't any ideas on how to solve it. But, he doesn't want to tell Tolik about his inability to solve it, so he hasn't found anything better than asking you how to solve this task.
In this task you are given a cell field n β
m, consisting of n rows and m columns, where point's coordinates (x, y) mean it is situated in the x-th row and y-th column, considering numeration from one (1 β€ x β€ n, 1 β€ y β€ m). Initially, you stand in the cell (1, 1). Every move you can jump from cell (x, y), which you stand in, by any non-zero vector (dx, dy), thus you will stand in the (x+dx, y+dy) cell. Obviously, you can't leave the field, but also there is one more important condition β you're not allowed to use one vector twice. Your task is to visit each cell of the field exactly once (the initial cell is considered as already visited).
Tolik's uncle is a very respectful person. Help him to solve this task!
Input
The first and only line contains two positive integers n, m (1 β€ n β
m β€ 10^{6}) β the number of rows and columns of the field respectively.
Output
Print "-1" (without quotes) if it is impossible to visit every cell exactly once.
Else print n β
m pairs of integers, i-th from them should contain two integers x_i, y_i (1 β€ x_i β€ n, 1 β€ y_i β€ m) β cells of the field in order of visiting, so that all of them are distinct and vectors of jumps between them are distinct too.
Notice that the first cell should have (1, 1) coordinates, according to the statement.
Examples
Input
2 3
Output
1 1
1 3
1 2
2 2
2 3
2 1
Input
1 1
Output
1 1
Note
The vectors from the first example in the order of making jumps are (0, 2), (0, -1), (1, 0), (0, 1), (0, -2).
Submitted Solution:
```
import sys
IS_LOCAL = False
def readMultiple(f):
return f(map(int, input().split()))
def main():
n, m = 2, 3
if not IS_LOCAL:
n, m = readMultiple(tuple)
if n * m == 1:
print(1, 1)
return
res = []
for row in range((n+1)//2):
rev_row = n - row
cell_cnt = m if rev_row != row + 1 else (m + 1) // 2
for i in range(cell_cnt):
res.append(f'{row + 1} {i + 1}')
if not (row + 1 == rev_row and i + 1 == m - i):
res.append(f'{rev_row} {m - i}')
print('\n'.join(res))
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == 'True':
IS_LOCAL = True
main()
``` | instruction | 0 | 88,439 | 15 | 176,878 |
Yes | output | 1 | 88,439 | 15 | 176,879 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't been born yet (in English, well), he decides to test a problem and asks his uncle.
After a long time thinking, Tolik's uncle hasn't any ideas on how to solve it. But, he doesn't want to tell Tolik about his inability to solve it, so he hasn't found anything better than asking you how to solve this task.
In this task you are given a cell field n β
m, consisting of n rows and m columns, where point's coordinates (x, y) mean it is situated in the x-th row and y-th column, considering numeration from one (1 β€ x β€ n, 1 β€ y β€ m). Initially, you stand in the cell (1, 1). Every move you can jump from cell (x, y), which you stand in, by any non-zero vector (dx, dy), thus you will stand in the (x+dx, y+dy) cell. Obviously, you can't leave the field, but also there is one more important condition β you're not allowed to use one vector twice. Your task is to visit each cell of the field exactly once (the initial cell is considered as already visited).
Tolik's uncle is a very respectful person. Help him to solve this task!
Input
The first and only line contains two positive integers n, m (1 β€ n β
m β€ 10^{6}) β the number of rows and columns of the field respectively.
Output
Print "-1" (without quotes) if it is impossible to visit every cell exactly once.
Else print n β
m pairs of integers, i-th from them should contain two integers x_i, y_i (1 β€ x_i β€ n, 1 β€ y_i β€ m) β cells of the field in order of visiting, so that all of them are distinct and vectors of jumps between them are distinct too.
Notice that the first cell should have (1, 1) coordinates, according to the statement.
Examples
Input
2 3
Output
1 1
1 3
1 2
2 2
2 3
2 1
Input
1 1
Output
1 1
Note
The vectors from the first example in the order of making jumps are (0, 2), (0, -1), (1, 0), (0, 1), (0, -2).
Submitted Solution:
```
n, m = list(map(int,input().split()))
for i in range (0, n * m):
if i % 2 == 0:
c = i // (2 * n)
r = (i % (2 * n)) // 2
else:
c = m - 1 - i // (2 * n)
r = n - 1 - (i % (2 * n)) // 2
print(str(r+1) + " " + str(c + 1))
``` | instruction | 0 | 88,440 | 15 | 176,880 |
Yes | output | 1 | 88,440 | 15 | 176,881 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't been born yet (in English, well), he decides to test a problem and asks his uncle.
After a long time thinking, Tolik's uncle hasn't any ideas on how to solve it. But, he doesn't want to tell Tolik about his inability to solve it, so he hasn't found anything better than asking you how to solve this task.
In this task you are given a cell field n β
m, consisting of n rows and m columns, where point's coordinates (x, y) mean it is situated in the x-th row and y-th column, considering numeration from one (1 β€ x β€ n, 1 β€ y β€ m). Initially, you stand in the cell (1, 1). Every move you can jump from cell (x, y), which you stand in, by any non-zero vector (dx, dy), thus you will stand in the (x+dx, y+dy) cell. Obviously, you can't leave the field, but also there is one more important condition β you're not allowed to use one vector twice. Your task is to visit each cell of the field exactly once (the initial cell is considered as already visited).
Tolik's uncle is a very respectful person. Help him to solve this task!
Input
The first and only line contains two positive integers n, m (1 β€ n β
m β€ 10^{6}) β the number of rows and columns of the field respectively.
Output
Print "-1" (without quotes) if it is impossible to visit every cell exactly once.
Else print n β
m pairs of integers, i-th from them should contain two integers x_i, y_i (1 β€ x_i β€ n, 1 β€ y_i β€ m) β cells of the field in order of visiting, so that all of them are distinct and vectors of jumps between them are distinct too.
Notice that the first cell should have (1, 1) coordinates, according to the statement.
Examples
Input
2 3
Output
1 1
1 3
1 2
2 2
2 3
2 1
Input
1 1
Output
1 1
Note
The vectors from the first example in the order of making jumps are (0, 2), (0, -1), (1, 0), (0, 1), (0, -2).
Submitted Solution:
```
from sys import stdout as so
n, m = [int(x) for x in input().split()]
def printa(x1, x2):
so.write(str(x1) + " " + str(x2) + "\n")
mn = 1
mx = n
while mn < mx:
for i in range(1, m+1):
printa(mn, i)
printa(mx, m-i+1)
mn += 1
mx -= 1
if mn == mx:
for i in range(1, m//2+1):
printa(mn, i)
printa(mn, m-i+1)
if m%2 == 1:
printa(mn, m//2+1)
``` | instruction | 0 | 88,441 | 15 | 176,882 |
Yes | output | 1 | 88,441 | 15 | 176,883 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't been born yet (in English, well), he decides to test a problem and asks his uncle.
After a long time thinking, Tolik's uncle hasn't any ideas on how to solve it. But, he doesn't want to tell Tolik about his inability to solve it, so he hasn't found anything better than asking you how to solve this task.
In this task you are given a cell field n β
m, consisting of n rows and m columns, where point's coordinates (x, y) mean it is situated in the x-th row and y-th column, considering numeration from one (1 β€ x β€ n, 1 β€ y β€ m). Initially, you stand in the cell (1, 1). Every move you can jump from cell (x, y), which you stand in, by any non-zero vector (dx, dy), thus you will stand in the (x+dx, y+dy) cell. Obviously, you can't leave the field, but also there is one more important condition β you're not allowed to use one vector twice. Your task is to visit each cell of the field exactly once (the initial cell is considered as already visited).
Tolik's uncle is a very respectful person. Help him to solve this task!
Input
The first and only line contains two positive integers n, m (1 β€ n β
m β€ 10^{6}) β the number of rows and columns of the field respectively.
Output
Print "-1" (without quotes) if it is impossible to visit every cell exactly once.
Else print n β
m pairs of integers, i-th from them should contain two integers x_i, y_i (1 β€ x_i β€ n, 1 β€ y_i β€ m) β cells of the field in order of visiting, so that all of them are distinct and vectors of jumps between them are distinct too.
Notice that the first cell should have (1, 1) coordinates, according to the statement.
Examples
Input
2 3
Output
1 1
1 3
1 2
2 2
2 3
2 1
Input
1 1
Output
1 1
Note
The vectors from the first example in the order of making jumps are (0, 2), (0, -1), (1, 0), (0, 1), (0, -2).
Submitted Solution:
```
import sys
m, n = [int(i) for i in input().split()]
data = [0] * (m+1)
for i in range((m+1)//2 ):
data[2*i] = i+1
data[2*i+1] = m - i
for x in range(n//2):
for i in range(m):
sys.stdout.write(str(i+1) + ' ' + str(x+1) + '\n')
sys.stdout.write(str(m-i) + ' ' + str(n-x) + '\n')
#print(i+1, x+1)
#print(m - i, n - x)
if n % 2 == 1:
q = (n+1)//2
s = " " + str(q) + '\n'
for i in range(m):
#print(data[i], q)
sys.stdout.write(str(data[i]) + s)
``` | instruction | 0 | 88,442 | 15 | 176,884 |
Yes | output | 1 | 88,442 | 15 | 176,885 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't been born yet (in English, well), he decides to test a problem and asks his uncle.
After a long time thinking, Tolik's uncle hasn't any ideas on how to solve it. But, he doesn't want to tell Tolik about his inability to solve it, so he hasn't found anything better than asking you how to solve this task.
In this task you are given a cell field n β
m, consisting of n rows and m columns, where point's coordinates (x, y) mean it is situated in the x-th row and y-th column, considering numeration from one (1 β€ x β€ n, 1 β€ y β€ m). Initially, you stand in the cell (1, 1). Every move you can jump from cell (x, y), which you stand in, by any non-zero vector (dx, dy), thus you will stand in the (x+dx, y+dy) cell. Obviously, you can't leave the field, but also there is one more important condition β you're not allowed to use one vector twice. Your task is to visit each cell of the field exactly once (the initial cell is considered as already visited).
Tolik's uncle is a very respectful person. Help him to solve this task!
Input
The first and only line contains two positive integers n, m (1 β€ n β
m β€ 10^{6}) β the number of rows and columns of the field respectively.
Output
Print "-1" (without quotes) if it is impossible to visit every cell exactly once.
Else print n β
m pairs of integers, i-th from them should contain two integers x_i, y_i (1 β€ x_i β€ n, 1 β€ y_i β€ m) β cells of the field in order of visiting, so that all of them are distinct and vectors of jumps between them are distinct too.
Notice that the first cell should have (1, 1) coordinates, according to the statement.
Examples
Input
2 3
Output
1 1
1 3
1 2
2 2
2 3
2 1
Input
1 1
Output
1 1
Note
The vectors from the first example in the order of making jumps are (0, 2), (0, -1), (1, 0), (0, 1), (0, -2).
Submitted Solution:
```
n, m = map(int, input().split())
for i in range(n // 2):
for j in range(m):
if j+1 != m-j:
print(i+1, j+1)
print(n-i, m-j)
else:
print(i+1, j+1)
if n % 2 == 1:
i = n // 2 + 1
for j in range(m):
if j+1 != m-j:
print(i, j+1)
print(i, m-j)
else:
print(i, j+1)
``` | instruction | 0 | 88,443 | 15 | 176,886 |
No | output | 1 | 88,443 | 15 | 176,887 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't been born yet (in English, well), he decides to test a problem and asks his uncle.
After a long time thinking, Tolik's uncle hasn't any ideas on how to solve it. But, he doesn't want to tell Tolik about his inability to solve it, so he hasn't found anything better than asking you how to solve this task.
In this task you are given a cell field n β
m, consisting of n rows and m columns, where point's coordinates (x, y) mean it is situated in the x-th row and y-th column, considering numeration from one (1 β€ x β€ n, 1 β€ y β€ m). Initially, you stand in the cell (1, 1). Every move you can jump from cell (x, y), which you stand in, by any non-zero vector (dx, dy), thus you will stand in the (x+dx, y+dy) cell. Obviously, you can't leave the field, but also there is one more important condition β you're not allowed to use one vector twice. Your task is to visit each cell of the field exactly once (the initial cell is considered as already visited).
Tolik's uncle is a very respectful person. Help him to solve this task!
Input
The first and only line contains two positive integers n, m (1 β€ n β
m β€ 10^{6}) β the number of rows and columns of the field respectively.
Output
Print "-1" (without quotes) if it is impossible to visit every cell exactly once.
Else print n β
m pairs of integers, i-th from them should contain two integers x_i, y_i (1 β€ x_i β€ n, 1 β€ y_i β€ m) β cells of the field in order of visiting, so that all of them are distinct and vectors of jumps between them are distinct too.
Notice that the first cell should have (1, 1) coordinates, according to the statement.
Examples
Input
2 3
Output
1 1
1 3
1 2
2 2
2 3
2 1
Input
1 1
Output
1 1
Note
The vectors from the first example in the order of making jumps are (0, 2), (0, -1), (1, 0), (0, 1), (0, -2).
Submitted Solution:
```
n, m = map(int, input().split())
for i in range(n // 2):
for j in range(m):
print(i+1, j+1)
print(n-i, m-j)
if n % 2 == 1:
i = n // 2 + 1
for j in range(m):
print(i+1, j+1)
print(i+1, m-j+1)
``` | instruction | 0 | 88,444 | 15 | 176,888 |
No | output | 1 | 88,444 | 15 | 176,889 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't been born yet (in English, well), he decides to test a problem and asks his uncle.
After a long time thinking, Tolik's uncle hasn't any ideas on how to solve it. But, he doesn't want to tell Tolik about his inability to solve it, so he hasn't found anything better than asking you how to solve this task.
In this task you are given a cell field n β
m, consisting of n rows and m columns, where point's coordinates (x, y) mean it is situated in the x-th row and y-th column, considering numeration from one (1 β€ x β€ n, 1 β€ y β€ m). Initially, you stand in the cell (1, 1). Every move you can jump from cell (x, y), which you stand in, by any non-zero vector (dx, dy), thus you will stand in the (x+dx, y+dy) cell. Obviously, you can't leave the field, but also there is one more important condition β you're not allowed to use one vector twice. Your task is to visit each cell of the field exactly once (the initial cell is considered as already visited).
Tolik's uncle is a very respectful person. Help him to solve this task!
Input
The first and only line contains two positive integers n, m (1 β€ n β
m β€ 10^{6}) β the number of rows and columns of the field respectively.
Output
Print "-1" (without quotes) if it is impossible to visit every cell exactly once.
Else print n β
m pairs of integers, i-th from them should contain two integers x_i, y_i (1 β€ x_i β€ n, 1 β€ y_i β€ m) β cells of the field in order of visiting, so that all of them are distinct and vectors of jumps between them are distinct too.
Notice that the first cell should have (1, 1) coordinates, according to the statement.
Examples
Input
2 3
Output
1 1
1 3
1 2
2 2
2 3
2 1
Input
1 1
Output
1 1
Note
The vectors from the first example in the order of making jumps are (0, 2), (0, -1), (1, 0), (0, 1), (0, -2).
Submitted Solution:
```
if __name__ == "__main__":
n, m = [int(x) for x in input().split()]
#n, m = map(int, input().split())
answer = []
for i in range((n + 1) // 2):
for j in range(m):
if i == n // 2 and j == m // 2:
answer.append("{} {}".format(i + 1, j + 1))
break
answer.append("{} {}".format(i + 1, j + 1))
answer.append("{} {}".format(n - i, m - j))
print("\n".join(answer))
``` | instruction | 0 | 88,445 | 15 | 176,890 |
No | output | 1 | 88,445 | 15 | 176,891 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.