message
stringlengths
2
44.5k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
276
109k
cluster
float64
23
23
__index_level_0__
int64
552
217k
Provide a correct Python 3 solution for this coding contest problem. As shown in the following figure, there is a paper consisting of a grid structure where each cell is indicated by (x, y) coordinate system. We are going to put drops of ink on the paper. A drop comes in three different sizes: Large, Medium, and Small. From the point of fall, the ink sinks into surrounding cells as shown in the figure depending on its size. In the figure, a star denotes the point of fall and a circle denotes the surrounding cells. <image> Originally, the paper is white that means for each cell the value of density is 0. The value of density is increased by 1 when the ink sinks into the corresponding cells. For example, if we put a drop of Small ink at (1, 2) and a drop of Medium ink at (3, 2), the ink will sink as shown in the following figure (left side): <image> In the figure, density values of empty cells are 0. The ink sinking into out of the paper should be ignored as shown in the figure (top side). We can put several drops of ink at the same point. Your task is to write a program which reads a sequence of points of fall (x, y) with its size (Small = 1, Medium = 2, Large = 3), and prints the number of cells whose density value is 0. The program must also print the maximum value of density. You may assume that the paper always consists of 10 × 10, and 0 ≤ x < 10, 0 ≤ y < 10. Input x1,y1,s1 x2,y2,s2 : : (xi, yi) represents the position of the i-th drop and si denotes its size. The number of drops is less than or equal to 50. Output Print the number of cells whose density value is 0 in first line. Print the maximum value of density in the second line. Example Input 2,5,3 3,6,1 3,4,2 4,5,2 3,6,3 2,4,1 Output 77 5
instruction
0
36,750
23
73,500
"Correct Solution: ``` ar = [[0]*14 for _ in range(14)] while(True): try: x,y,s = map(int,input().split(",")) if s == 3: for x1,y1 in [[-2,0],[0,2],[2,0],[0,-2],[-1,-1],[0,-1],[1,-1],[-1,0],[0,0],[1,0],[-1,1],[0,1],[1,1]]: ar[y1+y+2][x1+x+2] += 1 elif s == 2: for x1,y1 in [[-1,-1],[0,-1],[1,-1],[-1,0],[0,0],[1,0],[-1,1],[0,1],[1,1]]: ar[y1+y+2][x1+x+2] += 1 elif s == 1: for x1,y1 in [[0,-1],[-1,0],[0,0],[1,0],[0,1]]: ar[y1+y+2][x1+x+2] += 1 except: break br = [ a[2:12] for a in ar[2:12] ] print(sum([b.count(0) for b in br])) print(max([max(b) for b in br])) ```
output
1
36,750
23
73,501
Provide a correct Python 3 solution for this coding contest problem. As shown in the following figure, there is a paper consisting of a grid structure where each cell is indicated by (x, y) coordinate system. We are going to put drops of ink on the paper. A drop comes in three different sizes: Large, Medium, and Small. From the point of fall, the ink sinks into surrounding cells as shown in the figure depending on its size. In the figure, a star denotes the point of fall and a circle denotes the surrounding cells. <image> Originally, the paper is white that means for each cell the value of density is 0. The value of density is increased by 1 when the ink sinks into the corresponding cells. For example, if we put a drop of Small ink at (1, 2) and a drop of Medium ink at (3, 2), the ink will sink as shown in the following figure (left side): <image> In the figure, density values of empty cells are 0. The ink sinking into out of the paper should be ignored as shown in the figure (top side). We can put several drops of ink at the same point. Your task is to write a program which reads a sequence of points of fall (x, y) with its size (Small = 1, Medium = 2, Large = 3), and prints the number of cells whose density value is 0. The program must also print the maximum value of density. You may assume that the paper always consists of 10 × 10, and 0 ≤ x < 10, 0 ≤ y < 10. Input x1,y1,s1 x2,y2,s2 : : (xi, yi) represents the position of the i-th drop and si denotes its size. The number of drops is less than or equal to 50. Output Print the number of cells whose density value is 0 in first line. Print the maximum value of density in the second line. Example Input 2,5,3 3,6,1 3,4,2 4,5,2 3,6,3 2,4,1 Output 77 5
instruction
0
36,751
23
73,502
"Correct Solution: ``` A=[[int(0) for i in range(10)]for j in range(10)] count=0 while 1: try: x,y,s = map(int, input().split(',')) if s==1: for i in range(x-1,x+2): if i>=0 and i<=9: A[i][y]=A[i][y]+1 for i in range(y-1,y+2): if i>=0 and i<=9: A[x][i]=A[x][i]+1 A[x][y]=A[x][y]-1 elif s==2: for i in range(x-1,x+2): for j in range(y-1,y+2): if i>=0 and i<=9 and j>=0 and j<=9: A[i][j]=A[i][j]+1 else: for i in range(x-2,x+3): if i>=0 and i<=9: A[i][y]=A[i][y]+1 for i in range(y-2,y+3): if i>=0 and i<=9: A[x][i]=A[x][i]+1 for i in range(x-1,x+2): if i>=0 and i<=9: A[i][y]=A[i][y]-1 for i in range(y-1,y+2): if i>=0 and i<=9: A[x][i]=A[x][i]-1 for i in range(x-1,x+2): for j in range(y-1,y+2): if i>=0 and i<=9 and j>=0 and j<=9: A[i][j]=A[i][j]+1 except: break num=0 for i in range(10): for j in range(10): if A[i][j]==0: count=count+1 if A[i][j]!=0: if num<A[i][j]: num=A[i][j] print(count) print(num) ```
output
1
36,751
23
73,503
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As shown in the following figure, there is a paper consisting of a grid structure where each cell is indicated by (x, y) coordinate system. We are going to put drops of ink on the paper. A drop comes in three different sizes: Large, Medium, and Small. From the point of fall, the ink sinks into surrounding cells as shown in the figure depending on its size. In the figure, a star denotes the point of fall and a circle denotes the surrounding cells. <image> Originally, the paper is white that means for each cell the value of density is 0. The value of density is increased by 1 when the ink sinks into the corresponding cells. For example, if we put a drop of Small ink at (1, 2) and a drop of Medium ink at (3, 2), the ink will sink as shown in the following figure (left side): <image> In the figure, density values of empty cells are 0. The ink sinking into out of the paper should be ignored as shown in the figure (top side). We can put several drops of ink at the same point. Your task is to write a program which reads a sequence of points of fall (x, y) with its size (Small = 1, Medium = 2, Large = 3), and prints the number of cells whose density value is 0. The program must also print the maximum value of density. You may assume that the paper always consists of 10 × 10, and 0 ≤ x < 10, 0 ≤ y < 10. Input x1,y1,s1 x2,y2,s2 : : (xi, yi) represents the position of the i-th drop and si denotes its size. The number of drops is less than or equal to 50. Output Print the number of cells whose density value is 0 in first line. Print the maximum value of density in the second line. Example Input 2,5,3 3,6,1 3,4,2 4,5,2 3,6,3 2,4,1 Output 77 5 Submitted Solution: ``` import sys a=[[0]*10for _ in[0]*10] p=[[(-1,0),(0,-1),(0,0),(0,1),(1,0)],[(-1,-1),(-1,1),(1,-1),(1,1)],[(-2,0),(0,-2),(0,2),(2,0)]] for e in sys.stdin: x,y,s=map(int,e.split(',')) for f in p[:s]: for s,t in f: if 0<=y+s<10 and 0<=x+t<10:a[y+s][x+t]+=1 print(sum(1 for r in a for c in r if c==0)) print(max(max(r)for r in a)) ```
instruction
0
36,752
23
73,504
Yes
output
1
36,752
23
73,505
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As shown in the following figure, there is a paper consisting of a grid structure where each cell is indicated by (x, y) coordinate system. We are going to put drops of ink on the paper. A drop comes in three different sizes: Large, Medium, and Small. From the point of fall, the ink sinks into surrounding cells as shown in the figure depending on its size. In the figure, a star denotes the point of fall and a circle denotes the surrounding cells. <image> Originally, the paper is white that means for each cell the value of density is 0. The value of density is increased by 1 when the ink sinks into the corresponding cells. For example, if we put a drop of Small ink at (1, 2) and a drop of Medium ink at (3, 2), the ink will sink as shown in the following figure (left side): <image> In the figure, density values of empty cells are 0. The ink sinking into out of the paper should be ignored as shown in the figure (top side). We can put several drops of ink at the same point. Your task is to write a program which reads a sequence of points of fall (x, y) with its size (Small = 1, Medium = 2, Large = 3), and prints the number of cells whose density value is 0. The program must also print the maximum value of density. You may assume that the paper always consists of 10 × 10, and 0 ≤ x < 10, 0 ≤ y < 10. Input x1,y1,s1 x2,y2,s2 : : (xi, yi) represents the position of the i-th drop and si denotes its size. The number of drops is less than or equal to 50. Output Print the number of cells whose density value is 0 in first line. Print the maximum value of density in the second line. Example Input 2,5,3 3,6,1 3,4,2 4,5,2 3,6,3 2,4,1 Output 77 5 Submitted Solution: ``` import sys def plot(x, y): global ban if 0 <= x < 10 and 0 <= y < 10: ban[y][x] += 1 def drop(x, y, s): for dx, dy in [(0, 0), (-1, 0), (1, 0), (0, -1), (0, 1)]: plot(x + dx, y + dy) if s == 1: return for dx, dy in [(-1, -1), (-1, 1), (1, -1), (1, 1)]: plot(x + dx, y + dy) if s == 2: return for dx, dy in [(-2, 0), (2, 0), (0, -2), (0, 2)]: plot(x + dx, y + dy) ban = [[0 for x in range(10)] for y in range(10)] for line in sys.stdin: x, y, size = map(int, line.split(',')) drop(x, y, size) print(sum(ban[y][x] == 0 for y in range(10) for x in range(10))) print(max(ban[y][x] for y in range(10) for x in range(10))) ```
instruction
0
36,753
23
73,506
Yes
output
1
36,753
23
73,507
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As shown in the following figure, there is a paper consisting of a grid structure where each cell is indicated by (x, y) coordinate system. We are going to put drops of ink on the paper. A drop comes in three different sizes: Large, Medium, and Small. From the point of fall, the ink sinks into surrounding cells as shown in the figure depending on its size. In the figure, a star denotes the point of fall and a circle denotes the surrounding cells. <image> Originally, the paper is white that means for each cell the value of density is 0. The value of density is increased by 1 when the ink sinks into the corresponding cells. For example, if we put a drop of Small ink at (1, 2) and a drop of Medium ink at (3, 2), the ink will sink as shown in the following figure (left side): <image> In the figure, density values of empty cells are 0. The ink sinking into out of the paper should be ignored as shown in the figure (top side). We can put several drops of ink at the same point. Your task is to write a program which reads a sequence of points of fall (x, y) with its size (Small = 1, Medium = 2, Large = 3), and prints the number of cells whose density value is 0. The program must also print the maximum value of density. You may assume that the paper always consists of 10 × 10, and 0 ≤ x < 10, 0 ≤ y < 10. Input x1,y1,s1 x2,y2,s2 : : (xi, yi) represents the position of the i-th drop and si denotes its size. The number of drops is less than or equal to 50. Output Print the number of cells whose density value is 0 in first line. Print the maximum value of density in the second line. Example Input 2,5,3 3,6,1 3,4,2 4,5,2 3,6,3 2,4,1 Output 77 5 Submitted Solution: ``` masu = [[0 for x in range(10)] for y in range(10)] syou = [[-1,0], [0,-1], [0,0], [0,1], [1,0]] tyuu = [[-1,-1], [-1,1], [1,-1], [1,1]] + syou dai = [[-2,0], [0,-2], [0,2], [2,0]] + tyuu def drop(x,y,s): if s == 1: l = syou elif s == 2: l = tyuu elif s == 3: l = dai for dx,dy in l: nx = x + dx ny = y + dy if nx in range(10) and ny in range(10): masu[ny][nx] += 1 while True: try: x,y,s = map(int, input().split(',')) drop(x,y,s) except EOFError: break max = cnt = 0 for y in range(10): for x in range(10): if masu[y][x] == 0: cnt += 1 if max < masu[y][x]: max = masu[y][x] print(cnt) print(max) ```
instruction
0
36,754
23
73,508
Yes
output
1
36,754
23
73,509
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As shown in the following figure, there is a paper consisting of a grid structure where each cell is indicated by (x, y) coordinate system. We are going to put drops of ink on the paper. A drop comes in three different sizes: Large, Medium, and Small. From the point of fall, the ink sinks into surrounding cells as shown in the figure depending on its size. In the figure, a star denotes the point of fall and a circle denotes the surrounding cells. <image> Originally, the paper is white that means for each cell the value of density is 0. The value of density is increased by 1 when the ink sinks into the corresponding cells. For example, if we put a drop of Small ink at (1, 2) and a drop of Medium ink at (3, 2), the ink will sink as shown in the following figure (left side): <image> In the figure, density values of empty cells are 0. The ink sinking into out of the paper should be ignored as shown in the figure (top side). We can put several drops of ink at the same point. Your task is to write a program which reads a sequence of points of fall (x, y) with its size (Small = 1, Medium = 2, Large = 3), and prints the number of cells whose density value is 0. The program must also print the maximum value of density. You may assume that the paper always consists of 10 × 10, and 0 ≤ x < 10, 0 ≤ y < 10. Input x1,y1,s1 x2,y2,s2 : : (xi, yi) represents the position of the i-th drop and si denotes its size. The number of drops is less than or equal to 50. Output Print the number of cells whose density value is 0 in first line. Print the maximum value of density in the second line. Example Input 2,5,3 3,6,1 3,4,2 4,5,2 3,6,3 2,4,1 Output 77 5 Submitted Solution: ``` t = [[0 for i in range(10)] for j in range(10)] case1 = [(0, 0), (0, -1), (1, 0), (0, 1), (-1, 0)] case2 = [(1, -1), (1, 1), (-1, 1), (-1, -1)] case3 = [(0, -2), (2, 0), (0, 2), (-2, 0)] while True: try: x, y, s = map(int, input().split(',')) except: break for c in [case1, case2, case3][:s]: for _x, _y in c: if y+_y < 0 or x+_x < 0: continue try: t[y+_y][x+_x] += 1 except IndexError: continue print(sum(1 for l in t for v in l if not v)) print(max(v for l in t for v in l)) ```
instruction
0
36,755
23
73,510
Yes
output
1
36,755
23
73,511
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As shown in the following figure, there is a paper consisting of a grid structure where each cell is indicated by (x, y) coordinate system. We are going to put drops of ink on the paper. A drop comes in three different sizes: Large, Medium, and Small. From the point of fall, the ink sinks into surrounding cells as shown in the figure depending on its size. In the figure, a star denotes the point of fall and a circle denotes the surrounding cells. <image> Originally, the paper is white that means for each cell the value of density is 0. The value of density is increased by 1 when the ink sinks into the corresponding cells. For example, if we put a drop of Small ink at (1, 2) and a drop of Medium ink at (3, 2), the ink will sink as shown in the following figure (left side): <image> In the figure, density values of empty cells are 0. The ink sinking into out of the paper should be ignored as shown in the figure (top side). We can put several drops of ink at the same point. Your task is to write a program which reads a sequence of points of fall (x, y) with its size (Small = 1, Medium = 2, Large = 3), and prints the number of cells whose density value is 0. The program must also print the maximum value of density. You may assume that the paper always consists of 10 × 10, and 0 ≤ x < 10, 0 ≤ y < 10. Input x1,y1,s1 x2,y2,s2 : : (xi, yi) represents the position of the i-th drop and si denotes its size. The number of drops is less than or equal to 50. Output Print the number of cells whose density value is 0 in first line. Print the maximum value of density in the second line. Example Input 2,5,3 3,6,1 3,4,2 4,5,2 3,6,3 2,4,1 Output 77 5 Submitted Solution: ``` t = [[0 for i in range(10)] for j in range(10)] case1 = [(0, 0), (0, -1), (1, 0), (0, 1), (-1, 0)] case2 = [(1, -1), (1, 1), (-1, 1), (-1, -1)] case3 = [(0, -2), (2, 0), (0, 2), (-2, 0)] while True: try: x, y, s = map(int, input().split(',')) except: break for c in [case1, case2, case3][:s]: for _x, _y in c: try: t[y+_y][x+_x] += 1 except IndexError: continue print(sum(1 for l in t for v in l if not v)) print(max(v for l in t for v in l)) ```
instruction
0
36,756
23
73,512
No
output
1
36,756
23
73,513
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As shown in the following figure, there is a paper consisting of a grid structure where each cell is indicated by (x, y) coordinate system. We are going to put drops of ink on the paper. A drop comes in three different sizes: Large, Medium, and Small. From the point of fall, the ink sinks into surrounding cells as shown in the figure depending on its size. In the figure, a star denotes the point of fall and a circle denotes the surrounding cells. <image> Originally, the paper is white that means for each cell the value of density is 0. The value of density is increased by 1 when the ink sinks into the corresponding cells. For example, if we put a drop of Small ink at (1, 2) and a drop of Medium ink at (3, 2), the ink will sink as shown in the following figure (left side): <image> In the figure, density values of empty cells are 0. The ink sinking into out of the paper should be ignored as shown in the figure (top side). We can put several drops of ink at the same point. Your task is to write a program which reads a sequence of points of fall (x, y) with its size (Small = 1, Medium = 2, Large = 3), and prints the number of cells whose density value is 0. The program must also print the maximum value of density. You may assume that the paper always consists of 10 × 10, and 0 ≤ x < 10, 0 ≤ y < 10. Input x1,y1,s1 x2,y2,s2 : : (xi, yi) represents the position of the i-th drop and si denotes its size. The number of drops is less than or equal to 50. Output Print the number of cells whose density value is 0 in first line. Print the maximum value of density in the second line. Example Input 2,5,3 3,6,1 3,4,2 4,5,2 3,6,3 2,4,1 Output 77 5 Submitted Solution: ``` import sys lines = [] for line in sys.stdin: lines.append(line.strip().split(',')) field = [[0 for i in range(10)] for j in range(10)] def drop(x, y, z): if z==1: for i in range(-1,2): for j in range(-1,2): if abs(i)+abs(j)<2 and 0<=y+j<=10 and 0<=x+i<=10: field[y+j][x+i] += 1 elif z==2: for i in range(-1,2): for j in range(-1,2): if 0<=y+j<=10 and 0<=x+i<=10: field[y+j][x+i] += 1 else: for i in range(-2,3): for j in range(-2,3): if abs(i)+abs(j)<3 and 0<=y+j<=10 and 0<=x+i<=10: field[y+j][x+i] += 1 drop(1,1,1) ```
instruction
0
36,757
23
73,514
No
output
1
36,757
23
73,515
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As shown in the following figure, there is a paper consisting of a grid structure where each cell is indicated by (x, y) coordinate system. We are going to put drops of ink on the paper. A drop comes in three different sizes: Large, Medium, and Small. From the point of fall, the ink sinks into surrounding cells as shown in the figure depending on its size. In the figure, a star denotes the point of fall and a circle denotes the surrounding cells. <image> Originally, the paper is white that means for each cell the value of density is 0. The value of density is increased by 1 when the ink sinks into the corresponding cells. For example, if we put a drop of Small ink at (1, 2) and a drop of Medium ink at (3, 2), the ink will sink as shown in the following figure (left side): <image> In the figure, density values of empty cells are 0. The ink sinking into out of the paper should be ignored as shown in the figure (top side). We can put several drops of ink at the same point. Your task is to write a program which reads a sequence of points of fall (x, y) with its size (Small = 1, Medium = 2, Large = 3), and prints the number of cells whose density value is 0. The program must also print the maximum value of density. You may assume that the paper always consists of 10 × 10, and 0 ≤ x < 10, 0 ≤ y < 10. Input x1,y1,s1 x2,y2,s2 : : (xi, yi) represents the position of the i-th drop and si denotes its size. The number of drops is less than or equal to 50. Output Print the number of cells whose density value is 0 in first line. Print the maximum value of density in the second line. Example Input 2,5,3 3,6,1 3,4,2 4,5,2 3,6,3 2,4,1 Output 77 5 Submitted Solution: ``` paper = {} counter = 0 i = 0 for i in range(10): for j in range(10): paper.update({(i,j):0}) while i < 50: try: x, y, s = list(map(int,input().split(","))) except: break if s == 3: paper[(x+2,y)] += 1 paper[(x-2,y)] += 1 paper[(x,y+2)] += 1 paper[(x,y-2)] += 1 if s >= 2: paper[(x+1,y+1)] += 1 paper[(x+1,y-1)] += 1 paper[(x-1,y+1)] += 1 paper[(x-1,y-1)] += 1 paper[(x,y)] += 1 paper[(x,y-1)] += 1 paper[(x,y+1)] += 1 paper[(x+1,y)] += 1 paper[(x-1,y)] += 1 i += 1 for i in range(10): for j in range(10): if paper[(i,j)] == 0: counter += 1 print(counter) print(max(paper.values())) ```
instruction
0
36,758
23
73,516
No
output
1
36,758
23
73,517
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As shown in the following figure, there is a paper consisting of a grid structure where each cell is indicated by (x, y) coordinate system. We are going to put drops of ink on the paper. A drop comes in three different sizes: Large, Medium, and Small. From the point of fall, the ink sinks into surrounding cells as shown in the figure depending on its size. In the figure, a star denotes the point of fall and a circle denotes the surrounding cells. <image> Originally, the paper is white that means for each cell the value of density is 0. The value of density is increased by 1 when the ink sinks into the corresponding cells. For example, if we put a drop of Small ink at (1, 2) and a drop of Medium ink at (3, 2), the ink will sink as shown in the following figure (left side): <image> In the figure, density values of empty cells are 0. The ink sinking into out of the paper should be ignored as shown in the figure (top side). We can put several drops of ink at the same point. Your task is to write a program which reads a sequence of points of fall (x, y) with its size (Small = 1, Medium = 2, Large = 3), and prints the number of cells whose density value is 0. The program must also print the maximum value of density. You may assume that the paper always consists of 10 × 10, and 0 ≤ x < 10, 0 ≤ y < 10. Input x1,y1,s1 x2,y2,s2 : : (xi, yi) represents the position of the i-th drop and si denotes its size. The number of drops is less than or equal to 50. Output Print the number of cells whose density value is 0 in first line. Print the maximum value of density in the second line. Example Input 2,5,3 3,6,1 3,4,2 4,5,2 3,6,3 2,4,1 Output 77 5 Submitted Solution: ``` paper = [0 for x in range(10) for y in range(10)] while True : try: x, y, ink = [int(_) for _ in input().split(',')] temp = (x - 1) + y * 10 spot = [temp] for _ in 1, 10 : if temp + _ > 0 : spot.append(temp + _) if temp - _ > 0 : spot.append(temp - _) if ink == 2 or ink == 3 : for _ in 9, 11 : if temp + _ > 0 : spot.append(temp + _) if temp - _ > 0 : spot.append(temp - _) if ink == 3 : for _ in 2, 20 : if temp + _ > 0 : spot.append(temp + _) if temp - _ > 0 : spot.append(temp - _) for _ in spot : paper[_] += 1 except : break none = 0 for _ in paper : if _ == 0 : none += 1 print(none, max(paper)) ```
instruction
0
36,759
23
73,518
No
output
1
36,759
23
73,519
Provide a correct Python 3 solution for this coding contest problem. The Smart Beaver from ABBYY loves puzzles. One of his favorite puzzles is the magic square. He has recently had an idea to automate the solution of this puzzle. The Beaver decided to offer this challenge to the ABBYY Cup contestants. The magic square is a matrix of size n × n. The elements of this matrix are integers. The sum of numbers in each row of the matrix is equal to some number s. The sum of numbers in each column of the matrix is also equal to s. In addition, the sum of the elements on the main diagonal is equal to s and the sum of elements on the secondary diagonal is equal to s. Examples of magic squares are given in the following figure: <image> Magic squares You are given a set of n2 integers ai. It is required to place these numbers into a square matrix of size n × n so that they form a magic square. Note that each number must occur in the matrix exactly the same number of times as it occurs in the original set. It is guaranteed that a solution exists! Input The first input line contains a single integer n. The next line contains n2 integers ai ( - 108 ≤ ai ≤ 108), separated by single spaces. The input limitations for getting 20 points are: * 1 ≤ n ≤ 3 The input limitations for getting 50 points are: * 1 ≤ n ≤ 4 * It is guaranteed that there are no more than 9 distinct numbers among ai. The input limitations for getting 100 points are: * 1 ≤ n ≤ 4 Output The first line of the output should contain a single integer s. In each of the following n lines print n integers, separated by spaces and describing the resulting magic square. In the resulting magic square the sums in the rows, columns and diagonals must be equal to s. If there are multiple solutions, you are allowed to print any of them. Examples Input 3 1 2 3 4 5 6 7 8 9 Output 15 2 7 6 9 5 1 4 3 8 Input 3 1 0 -1 0 2 -1 -2 0 1 Output 0 1 0 -1 -2 0 2 1 0 -1 Input 2 5 5 5 5 Output 10 5 5 5 5
instruction
0
37,095
23
74,190
"Correct Solution: ``` import sys, random def f(b): global a a = [[0] * n for o in range(n)] for i in range(n): for j in range(n): a[i][j] = b[i * n + j] rez = 0 for i in range(n): ns = 0 for j in range(n): ns += a[i][j] rez += abs(su - ns) for j in range(n): ns = 0 for i in range(n): ns += a[i][j] rez += abs(su - ns) ns = 0 for i in range(n): ns += a[i][i] rez += abs(su - ns) ns = 0 for i in range(n): ns += a[i][n - i - 1] rez += abs(su - ns) return rez input = sys.stdin.readline n = int(input()) d = list(map(int, input().split())) su = sum(d) // n p = f(d) while p: random.shuffle(d) p = f(d) for k in range(1000): i = random.randint(0, n*n-1) j = random.randint(0, n*n-1) while i == j: j = random.randint(0, n*n-1) if i > j: i, j = j, i d[i], d[j] = d[j], d[i] q = f(d) if q < p: p = q else: d[i], d[j] = d[j], d[i] p = f(d) print(su) for i in a: print(*i) ```
output
1
37,095
23
74,191
Provide tags and a correct Python 3 solution for this coding contest problem. Appleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1 × n. Your task is help Appleman with folding of such a sheet. Actually, you need to perform q queries. Each query will have one of the following types: 1. Fold the sheet of paper at position pi. After this query the leftmost part of the paper with dimensions 1 × pi must be above the rightmost part of the paper with dimensions 1 × ([current width of sheet] - pi). 2. Count what is the total width of the paper pieces, if we will make two described later cuts and consider only the pieces between the cuts. We will make one cut at distance li from the left border of the current sheet of paper and the other at distance ri from the left border of the current sheet of paper. Please look at the explanation of the first test example for better understanding of the problem. Input The first line contains two integers: n and q (1 ≤ n ≤ 105; 1 ≤ q ≤ 105) — the width of the paper and the number of queries. Each of the following q lines contains one of the described queries in the following format: * "1 pi" (1 ≤ pi < [current width of sheet]) — the first type query. * "2 li ri" (0 ≤ li < ri ≤ [current width of sheet]) — the second type query. Output For each query of the second type, output the answer. Examples Input 7 4 1 3 1 2 2 0 1 2 1 2 Output 4 3 Input 10 9 2 2 9 1 1 2 0 1 1 8 2 0 8 1 2 2 1 3 1 4 2 2 4 Output 7 2 10 4 5 Note The pictures below show the shapes of the paper during the queries of the first example: <image> After the first fold operation the sheet has width equal to 4, after the second one the width of the sheet equals to 2.
instruction
0
37,195
23
74,390
Tags: data structures, implementation Correct Solution: ``` from itertools import starmap def main(): n, q = map(int, input().split()) a = list(range(n + 1)) flipped = False start = 0 end = n for _ in range(q): cmd, *args = map(int, input().split()) if cmd == 1: p = args[0] if p > end-start-p: flipped = not flipped p = end-start-p if flipped: a[end-p:end-2*p:-1] = starmap( lambda a, b: a+n-b, zip(a[end-p:end-2*p:-1], a[end-p:end]) ) end -= p else: start += p a[start:start+p] = starmap( lambda a, b: a-b, zip(a[start:start+p], a[start:start-p:-1]) ) else: l, r = args if flipped: l, r = end-start-r, end-start-l print(a[start + r] - a[start + l]) if __name__ == '__main__': main() ```
output
1
37,195
23
74,391
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Appleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1 × n. Your task is help Appleman with folding of such a sheet. Actually, you need to perform q queries. Each query will have one of the following types: 1. Fold the sheet of paper at position pi. After this query the leftmost part of the paper with dimensions 1 × pi must be above the rightmost part of the paper with dimensions 1 × ([current width of sheet] - pi). 2. Count what is the total width of the paper pieces, if we will make two described later cuts and consider only the pieces between the cuts. We will make one cut at distance li from the left border of the current sheet of paper and the other at distance ri from the left border of the current sheet of paper. Please look at the explanation of the first test example for better understanding of the problem. Input The first line contains two integers: n and q (1 ≤ n ≤ 105; 1 ≤ q ≤ 105) — the width of the paper and the number of queries. Each of the following q lines contains one of the described queries in the following format: * "1 pi" (1 ≤ pi < [current width of sheet]) — the first type query. * "2 li ri" (0 ≤ li < ri ≤ [current width of sheet]) — the second type query. Output For each query of the second type, output the answer. Examples Input 7 4 1 3 1 2 2 0 1 2 1 2 Output 4 3 Input 10 9 2 2 9 1 1 2 0 1 1 8 2 0 8 1 2 2 1 3 1 4 2 2 4 Output 7 2 10 4 5 Note The pictures below show the shapes of the paper during the queries of the first example: <image> After the first fold operation the sheet has width equal to 4, after the second one the width of the sheet equals to 2. Submitted Solution: ``` from itertools import starmap def main(): n, q = map(int, input().split()) a = list(range(n + 1)) flipped = False start = 0 end = n for _ in range(q): cmd, *args = map(int, input().split()) if cmd == 1: p = args[0] if p > end-start-p: flipped = not flipped p = end-start-p if flipped: a[end-p:end-2*p:-1] = starmap( lambda a, b: a+n-b, zip(a[end-p:end-2*p:-1], a[end-p:end]) ) end -= p else: start += p a[start:start+p] = starmap( lambda a, b: a-b, zip(a[start:start+p], a[start:start-p:-1]) ) print(a, start, end, flipped) else: l, r = args if flipped: l, r = end - r, end - l print(a[start + r] - a[start + l]) if __name__ == '__main__': main() ```
instruction
0
37,196
23
74,392
No
output
1
37,196
23
74,393
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Many computer strategy games require building cities, recruiting army, conquering tribes, collecting resources. Sometimes it leads to interesting problems. Let's suppose that your task is to build a square city. The world map uses the Cartesian coordinates. The sides of the city should be parallel to coordinate axes. The map contains mines with valuable resources, located at some points with integer coordinates. The sizes of mines are relatively small, i.e. they can be treated as points. The city should be built in such a way that all the mines are inside or on the border of the city square. Building a city takes large amount of money depending on the size of the city, so you have to build the city with the minimum area. Given the positions of the mines find the minimum possible area of the city. Input The first line of the input contains number n — the number of mines on the map (2 ≤ n ≤ 1000). Each of the next n lines contains a pair of integers xi and yi — the coordinates of the corresponding mine ( - 109 ≤ xi, yi ≤ 109). All points are pairwise distinct. Output Print the minimum area of the city that can cover all the mines with valuable resources. Examples Input 2 0 0 2 2 Output 4 Input 2 0 0 0 3 Output 9 Submitted Solution: ``` n = int (input()) pointx,pointy=[],[] for i in range(n): x , y = map(int,input().split()) pointx.append(x) pointy.append(y) ans = max(max(pointx)-min(pointx),max(pointy)-min(pointy)) print(ans * ans) ```
instruction
0
37,205
23
74,410
Yes
output
1
37,205
23
74,411
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Many computer strategy games require building cities, recruiting army, conquering tribes, collecting resources. Sometimes it leads to interesting problems. Let's suppose that your task is to build a square city. The world map uses the Cartesian coordinates. The sides of the city should be parallel to coordinate axes. The map contains mines with valuable resources, located at some points with integer coordinates. The sizes of mines are relatively small, i.e. they can be treated as points. The city should be built in such a way that all the mines are inside or on the border of the city square. Building a city takes large amount of money depending on the size of the city, so you have to build the city with the minimum area. Given the positions of the mines find the minimum possible area of the city. Input The first line of the input contains number n — the number of mines on the map (2 ≤ n ≤ 1000). Each of the next n lines contains a pair of integers xi and yi — the coordinates of the corresponding mine ( - 109 ≤ xi, yi ≤ 109). All points are pairwise distinct. Output Print the minimum area of the city that can cover all the mines with valuable resources. Examples Input 2 0 0 2 2 Output 4 Input 2 0 0 0 3 Output 9 Submitted Solution: ``` n=int(input()) a=[] b=[] for i in range(n): x,y=list(map(int,input().split())) a.append(x) b.append(y) s=max(a)-min(a) t=max(b)-min(b) if s>t: print(s*s) else: print(t*t) ```
instruction
0
37,206
23
74,412
Yes
output
1
37,206
23
74,413
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Many computer strategy games require building cities, recruiting army, conquering tribes, collecting resources. Sometimes it leads to interesting problems. Let's suppose that your task is to build a square city. The world map uses the Cartesian coordinates. The sides of the city should be parallel to coordinate axes. The map contains mines with valuable resources, located at some points with integer coordinates. The sizes of mines are relatively small, i.e. they can be treated as points. The city should be built in such a way that all the mines are inside or on the border of the city square. Building a city takes large amount of money depending on the size of the city, so you have to build the city with the minimum area. Given the positions of the mines find the minimum possible area of the city. Input The first line of the input contains number n — the number of mines on the map (2 ≤ n ≤ 1000). Each of the next n lines contains a pair of integers xi and yi — the coordinates of the corresponding mine ( - 109 ≤ xi, yi ≤ 109). All points are pairwise distinct. Output Print the minimum area of the city that can cover all the mines with valuable resources. Examples Input 2 0 0 2 2 Output 4 Input 2 0 0 0 3 Output 9 Submitted Solution: ``` a1 = [] a2 = [] x = int(input()) for i in range(x): a, b = map(int, input().split(' ')) a1.append(a) a2.append(b) print(max(max(a1)-min(a1), max(a2)-min(a2))**2) ```
instruction
0
37,207
23
74,414
Yes
output
1
37,207
23
74,415
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Many computer strategy games require building cities, recruiting army, conquering tribes, collecting resources. Sometimes it leads to interesting problems. Let's suppose that your task is to build a square city. The world map uses the Cartesian coordinates. The sides of the city should be parallel to coordinate axes. The map contains mines with valuable resources, located at some points with integer coordinates. The sizes of mines are relatively small, i.e. they can be treated as points. The city should be built in such a way that all the mines are inside or on the border of the city square. Building a city takes large amount of money depending on the size of the city, so you have to build the city with the minimum area. Given the positions of the mines find the minimum possible area of the city. Input The first line of the input contains number n — the number of mines on the map (2 ≤ n ≤ 1000). Each of the next n lines contains a pair of integers xi and yi — the coordinates of the corresponding mine ( - 109 ≤ xi, yi ≤ 109). All points are pairwise distinct. Output Print the minimum area of the city that can cover all the mines with valuable resources. Examples Input 2 0 0 2 2 Output 4 Input 2 0 0 0 3 Output 9 Submitted Solution: ``` from sys import * inp = lambda : stdin.readline() m = 1e10 def main(): n = int(inp()) px,nx,py,ny = -m,m,-m,m for i in range(n): a,b = map(int,inp().split()) px,nx,py,ny = max(px,a),min(nx,a),max(py,b),min(ny,b) print(max(px-nx,py-ny)**2) if __name__ == "__main__": main() ```
instruction
0
37,208
23
74,416
Yes
output
1
37,208
23
74,417
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Many computer strategy games require building cities, recruiting army, conquering tribes, collecting resources. Sometimes it leads to interesting problems. Let's suppose that your task is to build a square city. The world map uses the Cartesian coordinates. The sides of the city should be parallel to coordinate axes. The map contains mines with valuable resources, located at some points with integer coordinates. The sizes of mines are relatively small, i.e. they can be treated as points. The city should be built in such a way that all the mines are inside or on the border of the city square. Building a city takes large amount of money depending on the size of the city, so you have to build the city with the minimum area. Given the positions of the mines find the minimum possible area of the city. Input The first line of the input contains number n — the number of mines on the map (2 ≤ n ≤ 1000). Each of the next n lines contains a pair of integers xi and yi — the coordinates of the corresponding mine ( - 109 ≤ xi, yi ≤ 109). All points are pairwise distinct. Output Print the minimum area of the city that can cover all the mines with valuable resources. Examples Input 2 0 0 2 2 Output 4 Input 2 0 0 0 3 Output 9 Submitted Solution: ``` a=int(input());ok=1 l,r,u,d=0,0,0,0 for _ in " "*a: x,y=map(int,input().split()) if ok:l=x;r=x;u=y;d=y;ok=0 l = min(l, y) r = max(y, r) u = max(u, x) d = min(d, x) print(max((r-l)**2,(u-d)**2)) ```
instruction
0
37,209
23
74,418
No
output
1
37,209
23
74,419
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Many computer strategy games require building cities, recruiting army, conquering tribes, collecting resources. Sometimes it leads to interesting problems. Let's suppose that your task is to build a square city. The world map uses the Cartesian coordinates. The sides of the city should be parallel to coordinate axes. The map contains mines with valuable resources, located at some points with integer coordinates. The sizes of mines are relatively small, i.e. they can be treated as points. The city should be built in such a way that all the mines are inside or on the border of the city square. Building a city takes large amount of money depending on the size of the city, so you have to build the city with the minimum area. Given the positions of the mines find the minimum possible area of the city. Input The first line of the input contains number n — the number of mines on the map (2 ≤ n ≤ 1000). Each of the next n lines contains a pair of integers xi and yi — the coordinates of the corresponding mine ( - 109 ≤ xi, yi ≤ 109). All points are pairwise distinct. Output Print the minimum area of the city that can cover all the mines with valuable resources. Examples Input 2 0 0 2 2 Output 4 Input 2 0 0 0 3 Output 9 Submitted Solution: ``` import time,math,bisect,sys from sys import stdin,stdout from collections import deque from fractions import Fraction from collections import Counter pi=3.14159265358979323846264338327950 def II(): # to take integer input return int(stdin.readline()) def IO(): # to take string input return stdin.readline() def IP(): # to take tuple as input return map(int,stdin.readline().split()) def L(): # to take list as input return list(map(int,stdin.readline().split())) def P(x): # to print integer,list,string etc.. return stdout.write(str(x)+"\n") def PI(x,y): # to print tuple separatedly return stdout.write(str(x)+" "+str(y)+"\n") def lcm(a,b): # to calculate lcm return (a*b)//gcd(a,b) def gcd(a,b): # to calculate gcd if a==0: return b elif b==0: return a if a>b: return gcd(a%b,b) else: return gcd(a,b%a) def readTree(): # to read tree v=int(input()) adj=[set() for i in range(v+1)] for i in range(v-1): u1,u2=In() adj[u1].add(u2) adj[u2].add(u1) return adj,v def bfs(adj,v): # a schema of bfs visited=[False]*(v+1) q=deque() while q: pass def sieve(): li=[True]*1000001 li[0],li[1]=False,False for i in range(2,len(li),1): if li[i]==True: for j in range(i*i,len(li),i): li[j]=False prime=[] for i in range(1000001): if li[i]==True: prime.append(i) return prime def setBit(n): count=0 while n!=0: n=n&(n-1) count+=1 return count ##################################################################################### def solve(): n=II() li=[] for i in range(n): li.append(L()) mx=-sys.maxsize for i in range(n): for j in range(n): dist=(abs(li[i][0]-li[j][0]))**2+(abs(li[i][1]-li[j][1]))**2 mx=max(dist,mx) if mx%2==0: print(mx//2) else: print(mx) solve() ```
instruction
0
37,210
23
74,420
No
output
1
37,210
23
74,421
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Many computer strategy games require building cities, recruiting army, conquering tribes, collecting resources. Sometimes it leads to interesting problems. Let's suppose that your task is to build a square city. The world map uses the Cartesian coordinates. The sides of the city should be parallel to coordinate axes. The map contains mines with valuable resources, located at some points with integer coordinates. The sizes of mines are relatively small, i.e. they can be treated as points. The city should be built in such a way that all the mines are inside or on the border of the city square. Building a city takes large amount of money depending on the size of the city, so you have to build the city with the minimum area. Given the positions of the mines find the minimum possible area of the city. Input The first line of the input contains number n — the number of mines on the map (2 ≤ n ≤ 1000). Each of the next n lines contains a pair of integers xi and yi — the coordinates of the corresponding mine ( - 109 ≤ xi, yi ≤ 109). All points are pairwise distinct. Output Print the minimum area of the city that can cover all the mines with valuable resources. Examples Input 2 0 0 2 2 Output 4 Input 2 0 0 0 3 Output 9 Submitted Solution: ``` def main(): count = int(input()) (x1, x2) = map(int, input().split()) (y1, y2) = (x1, x2) for i in range(count-1): x, y = map(int, input().split()) x1 = min(x1, x) x2 = max(x2, x) y1 = min(y1, y) y2 = max(x2, y) dx = abs(x1 - x2) dy = abs(y1 - y2) dm = max(dx, dy) return dm ** 2 print(main()) ```
instruction
0
37,211
23
74,422
No
output
1
37,211
23
74,423
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Many computer strategy games require building cities, recruiting army, conquering tribes, collecting resources. Sometimes it leads to interesting problems. Let's suppose that your task is to build a square city. The world map uses the Cartesian coordinates. The sides of the city should be parallel to coordinate axes. The map contains mines with valuable resources, located at some points with integer coordinates. The sizes of mines are relatively small, i.e. they can be treated as points. The city should be built in such a way that all the mines are inside or on the border of the city square. Building a city takes large amount of money depending on the size of the city, so you have to build the city with the minimum area. Given the positions of the mines find the minimum possible area of the city. Input The first line of the input contains number n — the number of mines on the map (2 ≤ n ≤ 1000). Each of the next n lines contains a pair of integers xi and yi — the coordinates of the corresponding mine ( - 109 ≤ xi, yi ≤ 109). All points are pairwise distinct. Output Print the minimum area of the city that can cover all the mines with valuable resources. Examples Input 2 0 0 2 2 Output 4 Input 2 0 0 0 3 Output 9 Submitted Solution: ``` def main(): count = int(input()) #print('Log:', __name__, 'input count') (x1, x2) = map(int, input().split()) (y1, y2) = (x1, x2) #print('Log:', __name__, 'input start coords') for i in range(count-1): x, y = map(int, input().split()) x1 = min(x1, x) x2 = max(x2, x) y1 = min(y1, y) y2 = max(y2, y) dx = abs(x1 - x2) dy = abs(y1 - y2) dm = max(dx, dy) return dm ** 2 """ testcount = int(input()) for ti in range(testcount): mout = main() res = int(input()) print(mout, res, mout == res) """ print(main()) ```
instruction
0
37,212
23
74,424
No
output
1
37,212
23
74,425
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Nuske has a grid with N rows and M columns of squares. The rows are numbered 1 through N from top to bottom, and the columns are numbered 1 through M from left to right. Each square in the grid is painted in either blue or white. If S_{i,j} is 1, the square at the i-th row and j-th column is blue; if S_{i,j} is 0, the square is white. For every pair of two blue square a and b, there is at most one path that starts from a, repeatedly proceeds to an adjacent (side by side) blue square and finally reaches b, without traversing the same square more than once. Phantom Thnook, Nuske's eternal rival, gives Q queries to Nuske. The i-th query consists of four integers x_{i,1}, y_{i,1}, x_{i,2} and y_{i,2} and asks him the following: when the rectangular region of the grid bounded by (and including) the x_{i,1}-th row, x_{i,2}-th row, y_{i,1}-th column and y_{i,2}-th column is cut out, how many connected components consisting of blue squares there are in the region? Process all the queries. Constraints * 1 ≤ N,M ≤ 2000 * 1 ≤ Q ≤ 200000 * S_{i,j} is either 0 or 1. * S_{i,j} satisfies the condition explained in the statement. * 1 ≤ x_{i,1} ≤ x_{i,2} ≤ N(1 ≤ i ≤ Q) * 1 ≤ y_{i,1} ≤ y_{i,2} ≤ M(1 ≤ i ≤ Q) Input The input is given from Standard Input in the following format: N M Q S_{1,1}..S_{1,M} : S_{N,1}..S_{N,M} x_{1,1} y_{i,1} x_{i,2} y_{i,2} : x_{Q,1} y_{Q,1} x_{Q,2} y_{Q,2} Output For each query, print the number of the connected components consisting of blue squares in the region. Examples Input 3 4 4 1101 0110 1101 1 1 3 4 1 1 3 1 2 2 3 4 1 2 2 4 Output 3 2 2 2 Input 5 5 6 11010 01110 10101 11101 01010 1 1 5 5 1 2 4 5 2 3 3 4 3 3 3 3 3 1 3 5 1 1 3 4 Output 3 2 1 1 3 2 Submitted Solution: ``` def get(a, b, c, d, dp): return dp[c][d]-dp[a][d]-dp[c][b]+dp[a][b] n, m, q = map(int, input().split()) s = [list(map(int, list(input()))) for _ in range(n)] vers = [[0]*(m+1) for i in range(n+1)] edges = [[0]*(m+1) for i in range(n+1)] tate = [[0]*(m+1) for i in range(n+1)] yoko = [[0]*(m+1) for i in range(n+1)] for i in range(n): for j in range(m): if s[i][j] == 0: continue vers[i+1][j+1] = 1 nx, ny = i+1, j if 0<=nx<n and 0<=ny<m and s[nx][ny] == 1: edges[i+1][j+1]+=1 edges[i+2][j+1]+=1 tate[i+1][j+1]+=1 nx, ny = i, j+1 if 0<=nx<n and 0<=ny<m and s[nx][ny] == 1: edges[i+1][j+1]+=1 edges[i+1][j+2]+=1 yoko[i+1][j+1]+=1 for i in range(1, n+1): for j in range(1, m+1): vers[i][j]+=vers[i][j-1] edges[i][j]+=edges[i][j-1] tate[i][j]+=tate[i][j-1] for j in range(1, m+1): for i in range(1, n+1): vers[i][j]+=vers[i-1][j] edges[i][j]+=edges[i-1][j] yoko[i][j]+=yoko[i-1][j] for _ in range(q): a, b, c, d = map(int, input().split()) a-=1 b-=1 vcnt = get(a, b, c, d, vers) ecnt = get(a, b, c, d, edges) ecnt-=(yoko[c][b]-yoko[a][b]) ecnt-=(tate[a][d]-tate[a][b]) ecnt-=(yoko[c][d]-yoko[a][d]) ecnt-=(tate[c][d]-tate[c][b]) ecnt//=2 print(vcnt-ecnt) ```
instruction
0
37,612
23
75,224
Yes
output
1
37,612
23
75,225
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Nuske has a grid with N rows and M columns of squares. The rows are numbered 1 through N from top to bottom, and the columns are numbered 1 through M from left to right. Each square in the grid is painted in either blue or white. If S_{i,j} is 1, the square at the i-th row and j-th column is blue; if S_{i,j} is 0, the square is white. For every pair of two blue square a and b, there is at most one path that starts from a, repeatedly proceeds to an adjacent (side by side) blue square and finally reaches b, without traversing the same square more than once. Phantom Thnook, Nuske's eternal rival, gives Q queries to Nuske. The i-th query consists of four integers x_{i,1}, y_{i,1}, x_{i,2} and y_{i,2} and asks him the following: when the rectangular region of the grid bounded by (and including) the x_{i,1}-th row, x_{i,2}-th row, y_{i,1}-th column and y_{i,2}-th column is cut out, how many connected components consisting of blue squares there are in the region? Process all the queries. Constraints * 1 ≤ N,M ≤ 2000 * 1 ≤ Q ≤ 200000 * S_{i,j} is either 0 or 1. * S_{i,j} satisfies the condition explained in the statement. * 1 ≤ x_{i,1} ≤ x_{i,2} ≤ N(1 ≤ i ≤ Q) * 1 ≤ y_{i,1} ≤ y_{i,2} ≤ M(1 ≤ i ≤ Q) Input The input is given from Standard Input in the following format: N M Q S_{1,1}..S_{1,M} : S_{N,1}..S_{N,M} x_{1,1} y_{i,1} x_{i,2} y_{i,2} : x_{Q,1} y_{Q,1} x_{Q,2} y_{Q,2} Output For each query, print the number of the connected components consisting of blue squares in the region. Examples Input 3 4 4 1101 0110 1101 1 1 3 4 1 1 3 1 2 2 3 4 1 2 2 4 Output 3 2 2 2 Input 5 5 6 11010 01110 10101 11101 01010 1 1 5 5 1 2 4 5 2 3 3 4 3 3 3 3 3 1 3 5 1 1 3 4 Output 3 2 1 1 3 2 Submitted Solution: ``` import sys input = sys.stdin.readline n, m, q = map(int, input().split()) S = [[0]*(m+1)] + [[0] + list(map(int, list(input().rstrip()))) for _ in range(n)] B = [[0]*(m+1) for _ in range(n+1)] U = [[0]*(m+1) for _ in range(n+1)] L = [[0]*(m+1) for _ in range(n+1)] for i in range(1, n+1): for j in range(1, m+1): B[i][j] = B[i-1][j] + B[i][j-1] - B[i-1][j-1] + S[i][j] U[i][j] = U[i-1][j] + U[i][j-1] - U[i-1][j-1] + U[i][j] L[i][j] = L[i-1][j] + L[i][j-1] - L[i-1][j-1] + L[i][j] if S[i][j]: U[i][j] += S[i-1][j] L[i][j] += S[i][j-1] def calc_b(x1, y1, x2, y2): return B[x2][y2] - B[x1-1][y2] - B[x2][y1-1] + B[x1-1][y1-1] def calc_u(x1, y1, x2, y2): return U[x2][y2] - U[x1][y2] - U[x2][y1-1] + U[x1][y1-1] def calc_l(x1, y1, x2, y2): return L[x2][y2] - L[x1-1][y2] - L[x2][y1] + L[x1-1][y1] for _ in range(q): x1, y1, x2, y2 = map(int, input().split()) res = calc_b(x1, y1, x2, y2) - (calc_u(x1, y1, x2, y2) + calc_l(x1, y1, x2, y2)) print(res) ```
instruction
0
37,613
23
75,226
Yes
output
1
37,613
23
75,227
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Nuske has a grid with N rows and M columns of squares. The rows are numbered 1 through N from top to bottom, and the columns are numbered 1 through M from left to right. Each square in the grid is painted in either blue or white. If S_{i,j} is 1, the square at the i-th row and j-th column is blue; if S_{i,j} is 0, the square is white. For every pair of two blue square a and b, there is at most one path that starts from a, repeatedly proceeds to an adjacent (side by side) blue square and finally reaches b, without traversing the same square more than once. Phantom Thnook, Nuske's eternal rival, gives Q queries to Nuske. The i-th query consists of four integers x_{i,1}, y_{i,1}, x_{i,2} and y_{i,2} and asks him the following: when the rectangular region of the grid bounded by (and including) the x_{i,1}-th row, x_{i,2}-th row, y_{i,1}-th column and y_{i,2}-th column is cut out, how many connected components consisting of blue squares there are in the region? Process all the queries. Constraints * 1 ≤ N,M ≤ 2000 * 1 ≤ Q ≤ 200000 * S_{i,j} is either 0 or 1. * S_{i,j} satisfies the condition explained in the statement. * 1 ≤ x_{i,1} ≤ x_{i,2} ≤ N(1 ≤ i ≤ Q) * 1 ≤ y_{i,1} ≤ y_{i,2} ≤ M(1 ≤ i ≤ Q) Input The input is given from Standard Input in the following format: N M Q S_{1,1}..S_{1,M} : S_{N,1}..S_{N,M} x_{1,1} y_{i,1} x_{i,2} y_{i,2} : x_{Q,1} y_{Q,1} x_{Q,2} y_{Q,2} Output For each query, print the number of the connected components consisting of blue squares in the region. Examples Input 3 4 4 1101 0110 1101 1 1 3 4 1 1 3 1 2 2 3 4 1 2 2 4 Output 3 2 2 2 Input 5 5 6 11010 01110 10101 11101 01010 1 1 5 5 1 2 4 5 2 3 3 4 3 3 3 3 3 1 3 5 1 1 3 4 Output 3 2 1 1 3 2 Submitted Solution: ``` n, m, q = map(int, input().split()) s = [input() for _ in range(n)] a = [[0] * (m + 1) for _ in range(n + 1)] b = [[0] * (m + 1) for _ in range(n + 1)] c = [[0] * (m + 1) for _ in range(n + 1)] for i in range(n): for j in range(m): a[i + 1][j + 1] = a[i + 1][j] if s[i][j] == '1': a[i + 1][j + 1] += 1 for i in range(m + 1): for j in range(n): a[j + 1][i] += a[j][i] for i in range(n): for j in range(1, m): b[i + 1][j + 1] = b[i + 1][j] if s[i][j] == s[i][j - 1] == '1': b[i + 1][j + 1] += 1 for i in range(m + 1): for j in range(n): b[j + 1][i] += b[j][i] for j in range(m): for i in range(1, n): c[i + 1][j + 1] = c[i][j + 1] if s[i][j] == s[i - 1][j] == '1': c[i + 1][j + 1] += 1 for j in range(n + 1): for i in range(m): c[j][i + 1] += c[j][i] for _ in range(q): x1, y1, x2, y2 = map(int, input().split()) x1 -= 1 y1 -= 1 print((a[x2][y2] - a[x1][y2] - a[x2][y1] + a[x1][y1]) - (b[x2][y2] - b[x1][y2] - b[x2][y1 + 1] + b[x1][y1 + 1]) - (c[x2][y2] - c[x1 + 1][y2] - c[x2][y1] + c[x1 + 1][y1])) ```
instruction
0
37,614
23
75,228
Yes
output
1
37,614
23
75,229
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Nuske has a grid with N rows and M columns of squares. The rows are numbered 1 through N from top to bottom, and the columns are numbered 1 through M from left to right. Each square in the grid is painted in either blue or white. If S_{i,j} is 1, the square at the i-th row and j-th column is blue; if S_{i,j} is 0, the square is white. For every pair of two blue square a and b, there is at most one path that starts from a, repeatedly proceeds to an adjacent (side by side) blue square and finally reaches b, without traversing the same square more than once. Phantom Thnook, Nuske's eternal rival, gives Q queries to Nuske. The i-th query consists of four integers x_{i,1}, y_{i,1}, x_{i,2} and y_{i,2} and asks him the following: when the rectangular region of the grid bounded by (and including) the x_{i,1}-th row, x_{i,2}-th row, y_{i,1}-th column and y_{i,2}-th column is cut out, how many connected components consisting of blue squares there are in the region? Process all the queries. Constraints * 1 ≤ N,M ≤ 2000 * 1 ≤ Q ≤ 200000 * S_{i,j} is either 0 or 1. * S_{i,j} satisfies the condition explained in the statement. * 1 ≤ x_{i,1} ≤ x_{i,2} ≤ N(1 ≤ i ≤ Q) * 1 ≤ y_{i,1} ≤ y_{i,2} ≤ M(1 ≤ i ≤ Q) Input The input is given from Standard Input in the following format: N M Q S_{1,1}..S_{1,M} : S_{N,1}..S_{N,M} x_{1,1} y_{i,1} x_{i,2} y_{i,2} : x_{Q,1} y_{Q,1} x_{Q,2} y_{Q,2} Output For each query, print the number of the connected components consisting of blue squares in the region. Examples Input 3 4 4 1101 0110 1101 1 1 3 4 1 1 3 1 2 2 3 4 1 2 2 4 Output 3 2 2 2 Input 5 5 6 11010 01110 10101 11101 01010 1 1 5 5 1 2 4 5 2 3 3 4 3 3 3 3 3 1 3 5 1 1 3 4 Output 3 2 1 1 3 2 Submitted Solution: ``` import sys input=sys.stdin.readline N,M,Q=map(int,input().split()) S=[] for i in range(N): S.append(input()) blue=[[0 for j in range(M+1)] for i in range(N+1)] for i in range(1,N+1): for j in range(1,M+1): blue[i][j]+=blue[i][j-1]+int(S[i-1][j-1]) for j in range(1,M+1): for i in range(1,N+1): blue[i][j]+=blue[i-1][j] yoko=[[0 for j in range(M+1)] for i in range(N+1)] for i in range(1,N+1): for j in range(2,M+1): yoko[i][j]+=int(S[i-1][j-1])*int(S[i-1][j-2])+yoko[i][j-1] for j in range(M+1): for i in range(1,N+1): yoko[i][j]+=yoko[i-1][j] tate=[[0 for i in range(M+1)] for j in range(N+1)] for j in range(1,M+1): for i in range(2,N+1): tate[i][j]+=tate[i-1][j]+int(S[i-2][j-1])*int(S[i-1][j-1]) for i in range(N+1): for j in range(1,M+1): tate[i][j]+=tate[i][j-1] for _ in range(Q): x1,y1,x2,y2=map(int,input().split()) res=(blue[x2][y2]-blue[x1-1][y2])-(blue[x2][y1-1]-blue[x1-1][y1-1]) res-=(tate[x2][y2]-tate[x2][y1-1])-(tate[x1][y2]-tate[x1][y1-1]) res-=(yoko[x2][y2]-yoko[x1-1][y2])-(yoko[x2][y1]-yoko[x1-1][y1]) print(res) ```
instruction
0
37,615
23
75,230
Yes
output
1
37,615
23
75,231
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Nuske has a grid with N rows and M columns of squares. The rows are numbered 1 through N from top to bottom, and the columns are numbered 1 through M from left to right. Each square in the grid is painted in either blue or white. If S_{i,j} is 1, the square at the i-th row and j-th column is blue; if S_{i,j} is 0, the square is white. For every pair of two blue square a and b, there is at most one path that starts from a, repeatedly proceeds to an adjacent (side by side) blue square and finally reaches b, without traversing the same square more than once. Phantom Thnook, Nuske's eternal rival, gives Q queries to Nuske. The i-th query consists of four integers x_{i,1}, y_{i,1}, x_{i,2} and y_{i,2} and asks him the following: when the rectangular region of the grid bounded by (and including) the x_{i,1}-th row, x_{i,2}-th row, y_{i,1}-th column and y_{i,2}-th column is cut out, how many connected components consisting of blue squares there are in the region? Process all the queries. Constraints * 1 ≤ N,M ≤ 2000 * 1 ≤ Q ≤ 200000 * S_{i,j} is either 0 or 1. * S_{i,j} satisfies the condition explained in the statement. * 1 ≤ x_{i,1} ≤ x_{i,2} ≤ N(1 ≤ i ≤ Q) * 1 ≤ y_{i,1} ≤ y_{i,2} ≤ M(1 ≤ i ≤ Q) Input The input is given from Standard Input in the following format: N M Q S_{1,1}..S_{1,M} : S_{N,1}..S_{N,M} x_{1,1} y_{i,1} x_{i,2} y_{i,2} : x_{Q,1} y_{Q,1} x_{Q,2} y_{Q,2} Output For each query, print the number of the connected components consisting of blue squares in the region. Examples Input 3 4 4 1101 0110 1101 1 1 3 4 1 1 3 1 2 2 3 4 1 2 2 4 Output 3 2 2 2 Input 5 5 6 11010 01110 10101 11101 01010 1 1 5 5 1 2 4 5 2 3 3 4 3 3 3 3 3 1 3 5 1 1 3 4 Output 3 2 1 1 3 2 Submitted Solution: ``` import os import sys import numpy as np if os.getenv("LOCAL"): sys.stdin = open("_in.txt", "r") sys.setrecursionlimit(10 ** 9) INF = float("inf") IINF = 10 ** 18 MOD = 10 ** 9 + 7 # MOD = 998244353 N, M, Q = list(map(int, sys.stdin.buffer.readline().split())) S = [list(sys.stdin.buffer.readline().decode().rstrip()) for _ in range(N)] XY = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(Q)] def count_v(x1, y1, x2, y2): return counts_cum[x2][y2] - counts_cum[x1 - 1][y2] - counts_cum[x2][y1 - 1] + counts_cum[x1 - 1][y1 - 1] def count_ev(x1, y1, x2, y2): return edges_v_cum[x2][y2] - edges_v_cum[x1][y2] - edges_v_cum[x2][y1 - 1] + edges_v_cum[x1][y1 - 1] def count_eh(x1, y1, x2, y2): return edges_h_cum[x2][y2] - edges_h_cum[x1 - 1][y2] - edges_h_cum[x2][y1] + edges_h_cum[x1 - 1][y1] S = np.array(S, dtype=int) == 1 vs = [] e1 = [] e2 = [] counts_cum = np.zeros((N + 1, M + 1), dtype=np.int32) counts_cum[1:, 1:] = S.cumsum(axis=0).cumsum(axis=1) counts_cum = counts_cum.tolist() for x1, y1, x2, y2 in XY: vs.append(count_v(x1, y1, x2, y2)) del counts_cum edges_v_cum = np.zeros((N + 1, M + 1), dtype=np.int16) edges_v_cum[2:, 1:] = (S[1:] & S[:-1]).cumsum(axis=0).cumsum(axis=1) edges_v_cum = edges_v_cum.tolist() for x1, y1, x2, y2 in XY: e1.append(count_ev(x1, y1, x2, y2)) del edges_v_cum edges_h_cum = np.zeros((N + 1, M + 1), dtype=np.int16) edges_h_cum[1:, 2:] = (S[:, 1:] & S[:, :-1]).cumsum(axis=0).cumsum(axis=1) edges_h_cum = edges_h_cum.tolist() for x1, y1, x2, y2 in XY: e2.append(count_eh(x1, y1, x2, y2)) del edges_h_cum del S ans = (np.array(vs) - np.array(e1) - np.array(e2)).astype(int) print(*ans, sep='\n') ```
instruction
0
37,616
23
75,232
No
output
1
37,616
23
75,233
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Nuske has a grid with N rows and M columns of squares. The rows are numbered 1 through N from top to bottom, and the columns are numbered 1 through M from left to right. Each square in the grid is painted in either blue or white. If S_{i,j} is 1, the square at the i-th row and j-th column is blue; if S_{i,j} is 0, the square is white. For every pair of two blue square a and b, there is at most one path that starts from a, repeatedly proceeds to an adjacent (side by side) blue square and finally reaches b, without traversing the same square more than once. Phantom Thnook, Nuske's eternal rival, gives Q queries to Nuske. The i-th query consists of four integers x_{i,1}, y_{i,1}, x_{i,2} and y_{i,2} and asks him the following: when the rectangular region of the grid bounded by (and including) the x_{i,1}-th row, x_{i,2}-th row, y_{i,1}-th column and y_{i,2}-th column is cut out, how many connected components consisting of blue squares there are in the region? Process all the queries. Constraints * 1 ≤ N,M ≤ 2000 * 1 ≤ Q ≤ 200000 * S_{i,j} is either 0 or 1. * S_{i,j} satisfies the condition explained in the statement. * 1 ≤ x_{i,1} ≤ x_{i,2} ≤ N(1 ≤ i ≤ Q) * 1 ≤ y_{i,1} ≤ y_{i,2} ≤ M(1 ≤ i ≤ Q) Input The input is given from Standard Input in the following format: N M Q S_{1,1}..S_{1,M} : S_{N,1}..S_{N,M} x_{1,1} y_{i,1} x_{i,2} y_{i,2} : x_{Q,1} y_{Q,1} x_{Q,2} y_{Q,2} Output For each query, print the number of the connected components consisting of blue squares in the region. Examples Input 3 4 4 1101 0110 1101 1 1 3 4 1 1 3 1 2 2 3 4 1 2 2 4 Output 3 2 2 2 Input 5 5 6 11010 01110 10101 11101 01010 1 1 5 5 1 2 4 5 2 3 3 4 3 3 3 3 3 1 3 5 1 1 3 4 Output 3 2 1 1 3 2 Submitted Solution: ``` class Ruiseki2D(): def __init__(self, matrix): self.h = len(matrix) self.w = len(matrix[0]) self.ruiseki = [[0] * (self.w + 1) for _ in range(self.h + 1)] for i in range(self.h): for j in range(self.w): self.ruiseki[i + 1][j + 1] = self.ruiseki[i + 1][j] + matrix[i][j] for i in range(self.h): for j in range(self.w): self.ruiseki[i + 1][j + 1] += self.ruiseki[i][j + 1] def get_sum(self, hl, hr, wl, wr): """[hl, hr), [wl, wr)""" return self.ruiseki[hr][wr] + self.ruiseki[hl][wl] - self.ruiseki[hr][wl] - self.ruiseki[hl][wr] n, m, q = map(int, input().split()) s = [list(input()) for i in range(n)] for i in range(n): for j in range(n): s[i][j] = int(s[i][j]) matrix = [[1 if s[i][j] == 1 else 0 for j in range(m)] for i in range(n)] ruiseki = Ruiseki2D(matrix) matrix = [[0]*m for i in range(n)] for i in range(n): for j in range(m - 1): if s[i][j] == 1 and s[i][j + 1] == 1: matrix[i][j] = 1 ruiseki_w = Ruiseki2D(matrix) matrix = [[0]*m for i in range(n)] for i in range(n - 1): for j in range(m): if s[i][j] == 1 and s[i + 1][j] == 1: matrix[i][j] = 1 ruiseki_h = Ruiseki2D(matrix) for i in range(q): hl, wl, hr, wr = list(map(int, input().split())) hl -= 1 wl -= 1 print(ruiseki.get_sum(hl, hr, wl, wr) - ruiseki_w.get_sum(hl, hr, wl, wr - 1) - ruiseki_h.get_sum(hl, hr - 1, wl, wr)) ```
instruction
0
37,617
23
75,234
No
output
1
37,617
23
75,235
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Nuske has a grid with N rows and M columns of squares. The rows are numbered 1 through N from top to bottom, and the columns are numbered 1 through M from left to right. Each square in the grid is painted in either blue or white. If S_{i,j} is 1, the square at the i-th row and j-th column is blue; if S_{i,j} is 0, the square is white. For every pair of two blue square a and b, there is at most one path that starts from a, repeatedly proceeds to an adjacent (side by side) blue square and finally reaches b, without traversing the same square more than once. Phantom Thnook, Nuske's eternal rival, gives Q queries to Nuske. The i-th query consists of four integers x_{i,1}, y_{i,1}, x_{i,2} and y_{i,2} and asks him the following: when the rectangular region of the grid bounded by (and including) the x_{i,1}-th row, x_{i,2}-th row, y_{i,1}-th column and y_{i,2}-th column is cut out, how many connected components consisting of blue squares there are in the region? Process all the queries. Constraints * 1 ≤ N,M ≤ 2000 * 1 ≤ Q ≤ 200000 * S_{i,j} is either 0 or 1. * S_{i,j} satisfies the condition explained in the statement. * 1 ≤ x_{i,1} ≤ x_{i,2} ≤ N(1 ≤ i ≤ Q) * 1 ≤ y_{i,1} ≤ y_{i,2} ≤ M(1 ≤ i ≤ Q) Input The input is given from Standard Input in the following format: N M Q S_{1,1}..S_{1,M} : S_{N,1}..S_{N,M} x_{1,1} y_{i,1} x_{i,2} y_{i,2} : x_{Q,1} y_{Q,1} x_{Q,2} y_{Q,2} Output For each query, print the number of the connected components consisting of blue squares in the region. Examples Input 3 4 4 1101 0110 1101 1 1 3 4 1 1 3 1 2 2 3 4 1 2 2 4 Output 3 2 2 2 Input 5 5 6 11010 01110 10101 11101 01010 1 1 5 5 1 2 4 5 2 3 3 4 3 3 3 3 3 1 3 5 1 1 3 4 Output 3 2 1 1 3 2 Submitted Solution: ``` def solve(): import sys input = sys.stdin.readline N, M, Q = map(int, input().split()) Sss = [input().rstrip() for _ in range(N)] numVertexss = [[0]*(M+1)] + [[0] + list(map(int, Ss)) for Ss in Sss] numEdgeVss = [[0]*(M+1) for _ in range(N+1)] numEdgeHss = [[0]*(M+1) for _ in range(N+1)] for i in range(1, N): for j in range(1, M+1): if numVertexss[i][j] and numVertexss[i+1][j]: numEdgeVss[i][j] = 1 for i in range(1, N+1): for j in range(1, M): if numVertexss[i][j] and numVertexss[i][j+1]: numEdgeHss[i][j] = 1 def getAccAss(Ass): for x in range(1, N+1): for y in range(M+1): Ass[x][y] += Ass[x-1][y] for x in range(N+1): for y in range(1, M+1): Ass[x][y] += Ass[x][y-1] def getRangeSum2D(accAss, xFr, xTo, yFr, yTo): return accAss[xTo+1][yTo+1] - accAss[xTo+1][yFr] - accAss[xFr][yTo+1] + accAss[xFr][yFr] getAccAss(numVertexss) getAccAss(numEdgeVss) getAccAss(numEdgeHss) anss = [] for _ in range(Q): x1, y1, x2, y2 = map(int, input().split()) x1, y1, x2, y2 = x1-1, y1-1, x2-1, y2-1 ans = getRangeSum2D(numVertexss, x1, x2, y1, y2) ans -= getRangeSum2D(numEdgeVss, x1, x2-1, y1, y2) ans -= getRangeSum2D(numEdgeHss, x1, x2, y1, y2-1) anss.append(ans) print('\n'.join(map(str, anss))) solve() ```
instruction
0
37,618
23
75,236
No
output
1
37,618
23
75,237
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Nuske has a grid with N rows and M columns of squares. The rows are numbered 1 through N from top to bottom, and the columns are numbered 1 through M from left to right. Each square in the grid is painted in either blue or white. If S_{i,j} is 1, the square at the i-th row and j-th column is blue; if S_{i,j} is 0, the square is white. For every pair of two blue square a and b, there is at most one path that starts from a, repeatedly proceeds to an adjacent (side by side) blue square and finally reaches b, without traversing the same square more than once. Phantom Thnook, Nuske's eternal rival, gives Q queries to Nuske. The i-th query consists of four integers x_{i,1}, y_{i,1}, x_{i,2} and y_{i,2} and asks him the following: when the rectangular region of the grid bounded by (and including) the x_{i,1}-th row, x_{i,2}-th row, y_{i,1}-th column and y_{i,2}-th column is cut out, how many connected components consisting of blue squares there are in the region? Process all the queries. Constraints * 1 ≤ N,M ≤ 2000 * 1 ≤ Q ≤ 200000 * S_{i,j} is either 0 or 1. * S_{i,j} satisfies the condition explained in the statement. * 1 ≤ x_{i,1} ≤ x_{i,2} ≤ N(1 ≤ i ≤ Q) * 1 ≤ y_{i,1} ≤ y_{i,2} ≤ M(1 ≤ i ≤ Q) Input The input is given from Standard Input in the following format: N M Q S_{1,1}..S_{1,M} : S_{N,1}..S_{N,M} x_{1,1} y_{i,1} x_{i,2} y_{i,2} : x_{Q,1} y_{Q,1} x_{Q,2} y_{Q,2} Output For each query, print the number of the connected components consisting of blue squares in the region. Examples Input 3 4 4 1101 0110 1101 1 1 3 4 1 1 3 1 2 2 3 4 1 2 2 4 Output 3 2 2 2 Input 5 5 6 11010 01110 10101 11101 01010 1 1 5 5 1 2 4 5 2 3 3 4 3 3 3 3 3 1 3 5 1 1 3 4 Output 3 2 1 1 3 2 Submitted Solution: ``` import numpy as np N, M, Q = map(int, input().split()) S = np.empty((N+1, M+1), dtype='int') for n in range(1, N+1): S[n, :] = [0] + list(map(int, list(input()))) # count nodes N_cum = np.cumsum(np.cumsum(S, axis=0), axis=1) # count edges(row) ER_cum = np.cumsum(np.cumsum(S[:N, :]*S[1:, :], axis=0), axis=1) # count edges(col) EC_cum = np.cumsum(np.cumsum(S[:, :M]*S[:, 1:], axis=0), axis=1) sol_list = [] for _ in range(Q): x1, y1, x2, y2 = map(int, input().split()) # nodes N_nodes = N_cum[x2, y2] - N_cum[x1-1, y2] - N_cum[x2, y1-1] + N_cum[x1-1, y1-1] # edges(row) N_edges1 = ER_cum[x2-1, y2] - ER_cum[x1-1, y2] - ER_cum[x2-1, y1-1] + ER_cum[x1-1, y1-1] # edges(col) N_edges2 = EC_cum[x2, y2-1] - EC_cum[x1-1, y2-1] - EC_cum[x2, y1-1] + EC_cum[x1-1, y1-1] sol_list.append(N_nodes - N_edges1 - N_edges2) for q in range(Q): print(sol_list[q]) ```
instruction
0
37,619
23
75,238
No
output
1
37,619
23
75,239
Provide a correct Python 3 solution for this coding contest problem. Let's solve the puzzle by programming. The numbers n x n are arranged in a grid pattern. Some of the numbers are circled and we will call them the starting point. The rules of the puzzle are as follows: * Draw one line that goes vertically and horizontally from each starting point (cannot be drawn diagonally). * Extend the line so that the sum of the numbers passed is the same as the starting number. * The line must not be branched. * Lines cannot pass through numbers already drawn (lines must not intersect). * A line cannot pass through more than one origin. As shown in the figure below, the goal of the puzzle is to use all the starting points and draw lines on all the numbers. <image> Your job is to create a puzzle-solving program. However, in this problem, it is only necessary to determine whether the given puzzle can be solved. Input The input consists of multiple datasets. The format of each dataset is as follows: n n x n numbers Indicates a puzzle given a number of n x n, the starting number is given as a negative number. When n is 0, it indicates the end of input. It can be assumed that n is 3 or more and 8 or less, numbers other than the starting point are 1 or more and 50 or less, and the starting point is -50 or more and -1 or less. You may also assume the following as the nature of the puzzle being entered: * Each row and column of a given puzzle has at least one starting point. * The number of starting points is about 20% to 40% of the total number of numbers (n x n). Output For each dataset, print "YES" if the puzzle can be solved, otherwise "NO" on one line. Example Input 3 -3 1 1 2 -4 1 2 1 -1 3 -4 1 1 1 1 -6 1 -5 3 4 -8 6 -2 1 2 -7 -2 1 1 -1 1 1 1 1 1 -5 6 2 2 3 -7 3 2 1 -10 1 1 3 2 2 6 5 2 -6 1 3 4 -23 2 2 5 3 3 -6 2 3 7 -7 2 3 2 -5 -13 6 2 2 3 -7 3 2 1 -10 1 1 3 2 2 6 5 2 -6 1 3 4 -23 2 2 5 3 3 -6 2 3 7 -7 2 3 2 -5 -12 0 Output YES NO NO YES NO
instruction
0
37,691
23
75,382
"Correct Solution: ``` board = [] used = [] v = [] n = 0 dx = [1, 0, -1, 0] dy = [0, 1, 0, -1] def set_v(): global v for y in range(0, n): for x in range(0, n): if board[y][x] < 0: v += [(x, y)] used[y][x] = 1 def solve(x, y, rest, s, k): if s == 0: if rest == 0: if k == len(v) - 1: return True return False if k < len(v) - 1: k1 = k + 1 xx, yy = v[k1] used[y][x] = 1 if solve(xx, yy, rest - 1, - board[yy][xx], k1): return True used[y][x] = 0 if s <= 0 or k >= len(v): return False if board[y][x] > 0: used[y][x] = 1 for d in range(0, 4): xx = x + dx[d] yy = y + dy[d] if 0 <= xx < n and 0 <= yy < n and used[yy][xx] == 0: if solve(xx, yy, rest - 1, s - board[yy][xx], k): return True if board[y][x] > 0: used[y][x] = 0 return False while True: board = [] v = [] line = input() n = int(line) if n == 0: break for _ in range(0, n): a = list(map(int, list(input().split()))) board.append(a) used = [ [0] * n for _ in range(0, n)] set_v() x, y = v[0] print("YES" if solve(x, y, n * n - 1, - board[y][x], 0) else "NO") ```
output
1
37,691
23
75,383
Provide a correct Python 3 solution for this coding contest problem. Let's solve the puzzle by programming. The numbers n x n are arranged in a grid pattern. Some of the numbers are circled and we will call them the starting point. The rules of the puzzle are as follows: * Draw one line that goes vertically and horizontally from each starting point (cannot be drawn diagonally). * Extend the line so that the sum of the numbers passed is the same as the starting number. * The line must not be branched. * Lines cannot pass through numbers already drawn (lines must not intersect). * A line cannot pass through more than one origin. As shown in the figure below, the goal of the puzzle is to use all the starting points and draw lines on all the numbers. <image> Your job is to create a puzzle-solving program. However, in this problem, it is only necessary to determine whether the given puzzle can be solved. Input The input consists of multiple datasets. The format of each dataset is as follows: n n x n numbers Indicates a puzzle given a number of n x n, the starting number is given as a negative number. When n is 0, it indicates the end of input. It can be assumed that n is 3 or more and 8 or less, numbers other than the starting point are 1 or more and 50 or less, and the starting point is -50 or more and -1 or less. You may also assume the following as the nature of the puzzle being entered: * Each row and column of a given puzzle has at least one starting point. * The number of starting points is about 20% to 40% of the total number of numbers (n x n). Output For each dataset, print "YES" if the puzzle can be solved, otherwise "NO" on one line. Example Input 3 -3 1 1 2 -4 1 2 1 -1 3 -4 1 1 1 1 -6 1 -5 3 4 -8 6 -2 1 2 -7 -2 1 1 -1 1 1 1 1 1 -5 6 2 2 3 -7 3 2 1 -10 1 1 3 2 2 6 5 2 -6 1 3 4 -23 2 2 5 3 3 -6 2 3 7 -7 2 3 2 -5 -13 6 2 2 3 -7 3 2 1 -10 1 1 3 2 2 6 5 2 -6 1 3 4 -23 2 2 5 3 3 -6 2 3 7 -7 2 3 2 -5 -12 0 Output YES NO NO YES NO
instruction
0
37,692
23
75,384
"Correct Solution: ``` while True: n = int(input()) if n == 0:break mp = [list(map(int, input().split())) for _ in range(n)] used = [[False] * n for _ in range(n)] inits = [] for y in range(n): for x in range(n): if mp[y][x] < 0: inits.append((x, y, mp[y][x])) used[y][x] = True vec = ((0, 1), (-1, 0), (0, -1), (1, 0)) def search(x, y, s, index, inits, end): if s == 0 and index == end: return True elif s == 0: x, y, s = inits[index] index += 1 ret = False for dx, dy in vec: nx, ny = x + dx, y + dy if 0 <= nx < n and 0 <= ny < n and not used[ny][nx] and mp[ny][nx] + s <= 0: used[ny][nx] = True ret = ret or search(nx, ny, s + mp[ny][nx], index, inits, end) used[ny][nx] = False return ret if sum([sum(lst) for lst in mp]) != 0: print("NO") else: if search(0, 0, 0, 0, inits, len(inits)): print("YES") else: print("NO") ```
output
1
37,692
23
75,385
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's solve the puzzle by programming. The numbers n x n are arranged in a grid pattern. Some of the numbers are circled and we will call them the starting point. The rules of the puzzle are as follows: * Draw one line that goes vertically and horizontally from each starting point (cannot be drawn diagonally). * Extend the line so that the sum of the numbers passed is the same as the starting number. * The line must not be branched. * Lines cannot pass through numbers already drawn (lines must not intersect). * A line cannot pass through more than one origin. As shown in the figure below, the goal of the puzzle is to use all the starting points and draw lines on all the numbers. <image> Your job is to create a puzzle-solving program. However, in this problem, it is only necessary to determine whether the given puzzle can be solved. Input The input consists of multiple datasets. The format of each dataset is as follows: n n x n numbers Indicates a puzzle given a number of n x n, the starting number is given as a negative number. When n is 0, it indicates the end of input. It can be assumed that n is 3 or more and 8 or less, numbers other than the starting point are 1 or more and 50 or less, and the starting point is -50 or more and -1 or less. You may also assume the following as the nature of the puzzle being entered: * Each row and column of a given puzzle has at least one starting point. * The number of starting points is about 20% to 40% of the total number of numbers (n x n). Output For each dataset, print "YES" if the puzzle can be solved, otherwise "NO" on one line. Example Input 3 -3 1 1 2 -4 1 2 1 -1 3 -4 1 1 1 1 -6 1 -5 3 4 -8 6 -2 1 2 -7 -2 1 1 -1 1 1 1 1 1 -5 6 2 2 3 -7 3 2 1 -10 1 1 3 2 2 6 5 2 -6 1 3 4 -23 2 2 5 3 3 -6 2 3 7 -7 2 3 2 -5 -13 6 2 2 3 -7 3 2 1 -10 1 1 3 2 2 6 5 2 -6 1 3 4 -23 2 2 5 3 3 -6 2 3 7 -7 2 3 2 -5 -12 0 Output YES NO NO YES NO Submitted Solution: ``` di=(-1,1,0,0) dj=(0,0,-1,1) def solve(a,ss,i,j,si,rest,vis): n=len(a) if i<0 or n<=i or j<0 or n<=j or vis[i][j]: return False if a[i][j]<0: if ss[si]!=(i,j): return False vis[i][j]=-1-si for k in range(4): ni,nj=i+di[k],j+dj[k] if solve(a,ss,ni,nj,si,rest,vis): return True vis[i][j]=0 else: if rest<a[i][j]: return False vis[i][j]=-1-si rest-=a[i][j] if rest==0: si+=1 if si==len(ss): return True ni,nj=ss[si] if solve(a,ss,ni,nj,si,-a[ni][nj],vis): return True else: for k in range(4): ni,nj=i+di[k],j+dj[k] if solve(a,ss,ni,nj,si,rest,vis): return True vis[i][j]=0 return False while 1: n=int(input()) if n==0: break a=[list(map(int,input().split())) for _ in range(n)] if sum(x for row in a for x in row): print("NO") continue ss=[(i,j) for i in range(n) for j in range(n) if a[i][j]<0] vis=[[0]*n for _ in range(n)] i,j=ss[0] if solve(a,ss,i,j,0,-a[i][j],vis): print("YES") else: print("NO") ```
instruction
0
37,693
23
75,386
No
output
1
37,693
23
75,387
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's solve the puzzle by programming. The numbers n x n are arranged in a grid pattern. Some of the numbers are circled and we will call them the starting point. The rules of the puzzle are as follows: * Draw one line that goes vertically and horizontally from each starting point (cannot be drawn diagonally). * Extend the line so that the sum of the numbers passed is the same as the starting number. * The line must not be branched. * Lines cannot pass through numbers already drawn (lines must not intersect). * A line cannot pass through more than one origin. As shown in the figure below, the goal of the puzzle is to use all the starting points and draw lines on all the numbers. <image> Your job is to create a puzzle-solving program. However, in this problem, it is only necessary to determine whether the given puzzle can be solved. Input The input consists of multiple datasets. The format of each dataset is as follows: n n x n numbers Indicates a puzzle given a number of n x n, the starting number is given as a negative number. When n is 0, it indicates the end of input. It can be assumed that n is 3 or more and 8 or less, numbers other than the starting point are 1 or more and 50 or less, and the starting point is -50 or more and -1 or less. You may also assume the following as the nature of the puzzle being entered: * Each row and column of a given puzzle has at least one starting point. * The number of starting points is about 20% to 40% of the total number of numbers (n x n). Output For each dataset, print "YES" if the puzzle can be solved, otherwise "NO" on one line. Example Input 3 -3 1 1 2 -4 1 2 1 -1 3 -4 1 1 1 1 -6 1 -5 3 4 -8 6 -2 1 2 -7 -2 1 1 -1 1 1 1 1 1 -5 6 2 2 3 -7 3 2 1 -10 1 1 3 2 2 6 5 2 -6 1 3 4 -23 2 2 5 3 3 -6 2 3 7 -7 2 3 2 -5 -13 6 2 2 3 -7 3 2 1 -10 1 1 3 2 2 6 5 2 -6 1 3 4 -23 2 2 5 3 3 -6 2 3 7 -7 2 3 2 -5 -12 0 Output YES NO NO YES NO Submitted Solution: ``` def dfs(a,i,j,ss,si,rest,vis): if i<0 or len(a)<=i or j<0 or len(a[i])<=j or vis[i][j]: return False if a[i][j]<0 and (i,j)==ss[si]: vis[i][j]=1 for di,dj in zip([-1,1,0,0],[0,0,-1,1]): if dfs(a,i+di,j+dj,ss,si,-a[i][j],vis): return True vis[i][j]=0 elif a[i][j]>0 and rest>=a[i][j]: rest-=a[i][j] vis[i][j]=1 if rest==0: if si+1==len(ss): return True ni,nj=ss[si+1] if dfs(a,ni,nj,ss,si+1,-a[i][j],vis): return True else: for di,dj in zip([-1,1,0,0],[0,0,-1,1]): if dfs(a,i+di,j+dj,ss,si,rest,vis): return True vis[i][j]=0 return False while 1: n=int(input()) if n==0: break a=[list(map(int,input().split())) for _ in range(n)] if sum(x for row in a for x in row): print("NO") continue ss=[(i,j) for i in range(n) for j in range(n) if a[i][j]<0] vis=[[0]*n for _ in range(n)] i,j=ss[0] print("YES" if dfs(a,i,j,ss,0,-a[i][j],vis) else "NO") ```
instruction
0
37,694
23
75,388
No
output
1
37,694
23
75,389
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's solve the puzzle by programming. The numbers n x n are arranged in a grid pattern. Some of the numbers are circled and we will call them the starting point. The rules of the puzzle are as follows: * Draw one line that goes vertically and horizontally from each starting point (cannot be drawn diagonally). * Extend the line so that the sum of the numbers passed is the same as the starting number. * The line must not be branched. * Lines cannot pass through numbers already drawn (lines must not intersect). * A line cannot pass through more than one origin. As shown in the figure below, the goal of the puzzle is to use all the starting points and draw lines on all the numbers. <image> Your job is to create a puzzle-solving program. However, in this problem, it is only necessary to determine whether the given puzzle can be solved. Input The input consists of multiple datasets. The format of each dataset is as follows: n n x n numbers Indicates a puzzle given a number of n x n, the starting number is given as a negative number. When n is 0, it indicates the end of input. It can be assumed that n is 3 or more and 8 or less, numbers other than the starting point are 1 or more and 50 or less, and the starting point is -50 or more and -1 or less. You may also assume the following as the nature of the puzzle being entered: * Each row and column of a given puzzle has at least one starting point. * The number of starting points is about 20% to 40% of the total number of numbers (n x n). Output For each dataset, print "YES" if the puzzle can be solved, otherwise "NO" on one line. Example Input 3 -3 1 1 2 -4 1 2 1 -1 3 -4 1 1 1 1 -6 1 -5 3 4 -8 6 -2 1 2 -7 -2 1 1 -1 1 1 1 1 1 -5 6 2 2 3 -7 3 2 1 -10 1 1 3 2 2 6 5 2 -6 1 3 4 -23 2 2 5 3 3 -6 2 3 7 -7 2 3 2 -5 -13 6 2 2 3 -7 3 2 1 -10 1 1 3 2 2 6 5 2 -6 1 3 4 -23 2 2 5 3 3 -6 2 3 7 -7 2 3 2 -5 -12 0 Output YES NO NO YES NO Submitted Solution: ``` def dfs(a,i,j,rest,vis): if i<0 or len(a)<=i or j<0 or len(a[i])<=j: return False if a[i][j]<0 or rest<a[i][j] or vis[i][j]: return False vis[i][j]=1 rest-=a[i][j] if rest==0: if start(a,vis): return True else: for di,dj in zip([-1,1,0,0],[0,0,-1,1]): if dfs(a,i+di,j+dj,rest,vis): return True vis[i][j]=0 return False def start(a,vis): for i in range(len(a)): for j in range(len(a[i])): if a[i][j]<0 and not vis[i][j]: vis[i][j]=1 for di,dj in zip([-1,1,0,0],[0,0,-1,1]): if dfs(a,i+di,j+dj,-a[i][j],vis): return True vis[i][j]=0 return False return True while 1: n=int(input()) if n==0: break a=[list(map(int,input().split())) for _ in range(n)] if sum(x for row in a for x in row): print("NO") continue vis=[[0]*n for _ in range(n)] print("YES" if start(a,vis) else "NO") ```
instruction
0
37,695
23
75,390
No
output
1
37,695
23
75,391
Provide a correct Python 3 solution for this coding contest problem. There is a chain consisting of multiple circles on a plane. The first (last) circle of the chain only intersects with the next (previous) circle, and each intermediate circle intersects only with the two neighboring circles. Your task is to find the shortest path that satisfies the following conditions. * The path connects the centers of the first circle and the last circle. * The path is confined in the chain, that is, all the points on the path are located within or on at least one of the circles. Figure E-1 shows an example of such a chain and the corresponding shortest path. <image> Figure E-1: An example chain and the corresponding shortest path Input The input consists of multiple datasets. Each dataset represents the shape of a chain in the following format. > n > x1 y1 r1 > x2 y2 r2 > ... > xn yn rn > The first line of a dataset contains an integer n (3 ≤ n ≤ 100) representing the number of the circles. Each of the following n lines contains three integers separated by a single space. (xi, yi) and ri represent the center position and the radius of the i-th circle Ci. You can assume that 0 ≤ xi ≤ 1000, 0 ≤ yi ≤ 1000, and 1 ≤ ri ≤ 25. You can assume that Ci and Ci+1 (1 ≤ i ≤ n−1) intersect at two separate points. When j ≥ i+2, Ci and Cj are apart and either of them does not contain the other. In addition, you can assume that any circle does not contain the center of any other circle. The end of the input is indicated by a line containing a zero. Figure E-1 corresponds to the first dataset of Sample Input below. Figure E-2 shows the shortest paths for the subsequent datasets of Sample Input. <image> Figure E-2: Example chains and the corresponding shortest paths Output For each dataset, output a single line containing the length of the shortest chain-confined path between the centers of the first circle and the last circle. The value should not have an error greater than 0.001. No extra characters should appear in the output. Sample Input 10 802 0 10 814 0 4 820 1 4 826 1 4 832 3 5 838 5 5 845 7 3 849 10 3 853 14 4 857 18 3 3 0 0 5 8 0 5 8 8 5 3 0 0 5 7 3 6 16 0 5 9 0 3 5 8 0 8 19 2 8 23 14 6 23 21 6 23 28 6 19 40 8 8 42 8 0 39 5 11 0 0 5 8 0 5 18 8 10 8 16 5 0 16 5 0 24 5 3 32 5 10 32 5 17 28 8 27 25 3 30 18 5 0 Output for the Sample Input 58.953437 11.414214 16.0 61.874812 63.195179 Example Input 10 802 0 10 814 0 4 820 1 4 826 1 4 832 3 5 838 5 5 845 7 3 849 10 3 853 14 4 857 18 3 3 0 0 5 8 0 5 8 8 5 3 0 0 5 7 3 6 16 0 5 9 0 3 5 8 0 8 19 2 8 23 14 6 23 21 6 23 28 6 19 40 8 8 42 8 0 39 5 11 0 0 5 8 0 5 18 8 10 8 16 5 0 16 5 0 24 5 3 32 5 10 32 5 17 28 8 27 25 3 30 18 5 0 Output 58.953437 11.414214 16.0 61.874812 63.195179
instruction
0
37,696
23
75,392
"Correct Solution: ``` from heapq import heappop, heappush def cross(a,b): return a[0]*b[1]-a[1]*b[0] def calc(r1,r2): a = ((r1[0]-r2[0])**2+(r1[1]-r2[1])**2)**0.5 b = r1[2] c = r2[2] cos = (a**2+b**2-c**2)/(2*a*b) s = b*cos t = (b**2-s**2)**0.5 e = ((r2[0]-r1[0])/a,(r2[1]-r1[1])/a) h = (-e[1],e[0]) return [[r1[0]+s*e[0]+t*h[0], r1[1]+s*e[1]+t*h[1]],[r1[0]+s*e[0]-t*h[0], r1[1]+s*e[1]-t*h[1]]] def check(a,b,s,t): v = (b[0]-a[0],b[1]-a[1]) v1 = (s[0]-a[0],s[1]-a[1]) v2 = (t[0]-a[0],t[1]-a[1]) if cross(v,v1)*cross(v,v2) > 0: return 0 v = (t[0]-s[0],t[1]-s[1]) v1 = (a[0]-s[0],a[1]-s[1]) v2 = (b[0]-s[0],b[1]-s[1]) if cross(v,v1)*cross(v,v2) > 0: return 0 return 1 def dijkstra(m,v): dist = [float("inf")]*m q = [(0,0)] dist[0] = 0 while q: dx,x = heappop(q) for y,d in v[x]: if dist[y] > dx+d: dist[y] = dx+d heappush(q,(dx+d,y)) return dist[m-1] def solve(n): r = [[int(x) for x in input().split()] for i in range(n)] p = [[r[0][0],r[0][1]]] for i in range(n-1): p += calc(r[i],r[i+1]) p.append([r[-1][0],r[-1][1]]) m = 2*n v = [[] for i in range(m)] gate = [] for i in range(1,n): gate.append([p[2*i-1],p[2*i]]) for i in range(m): x = p[i] for j in range(i+1,m): y = p[j] for g1,g2 in gate[(i-1)//2+1:(j-1)//2]: if not check(x,y,g1,g2): break else: d = ((x[0]-y[0])**2+(x[1]-y[1])**2)**0.5 v[i].append((j,d)) v[j].append((i,d)) print(dijkstra(m,v)) while 1: n = int(input()) if n == 0: break solve(n) ```
output
1
37,696
23
75,393
Provide a correct Python 3 solution for this coding contest problem. There is a chain consisting of multiple circles on a plane. The first (last) circle of the chain only intersects with the next (previous) circle, and each intermediate circle intersects only with the two neighboring circles. Your task is to find the shortest path that satisfies the following conditions. * The path connects the centers of the first circle and the last circle. * The path is confined in the chain, that is, all the points on the path are located within or on at least one of the circles. Figure E-1 shows an example of such a chain and the corresponding shortest path. <image> Figure E-1: An example chain and the corresponding shortest path Input The input consists of multiple datasets. Each dataset represents the shape of a chain in the following format. > n > x1 y1 r1 > x2 y2 r2 > ... > xn yn rn > The first line of a dataset contains an integer n (3 ≤ n ≤ 100) representing the number of the circles. Each of the following n lines contains three integers separated by a single space. (xi, yi) and ri represent the center position and the radius of the i-th circle Ci. You can assume that 0 ≤ xi ≤ 1000, 0 ≤ yi ≤ 1000, and 1 ≤ ri ≤ 25. You can assume that Ci and Ci+1 (1 ≤ i ≤ n−1) intersect at two separate points. When j ≥ i+2, Ci and Cj are apart and either of them does not contain the other. In addition, you can assume that any circle does not contain the center of any other circle. The end of the input is indicated by a line containing a zero. Figure E-1 corresponds to the first dataset of Sample Input below. Figure E-2 shows the shortest paths for the subsequent datasets of Sample Input. <image> Figure E-2: Example chains and the corresponding shortest paths Output For each dataset, output a single line containing the length of the shortest chain-confined path between the centers of the first circle and the last circle. The value should not have an error greater than 0.001. No extra characters should appear in the output. Sample Input 10 802 0 10 814 0 4 820 1 4 826 1 4 832 3 5 838 5 5 845 7 3 849 10 3 853 14 4 857 18 3 3 0 0 5 8 0 5 8 8 5 3 0 0 5 7 3 6 16 0 5 9 0 3 5 8 0 8 19 2 8 23 14 6 23 21 6 23 28 6 19 40 8 8 42 8 0 39 5 11 0 0 5 8 0 5 18 8 10 8 16 5 0 16 5 0 24 5 3 32 5 10 32 5 17 28 8 27 25 3 30 18 5 0 Output for the Sample Input 58.953437 11.414214 16.0 61.874812 63.195179 Example Input 10 802 0 10 814 0 4 820 1 4 826 1 4 832 3 5 838 5 5 845 7 3 849 10 3 853 14 4 857 18 3 3 0 0 5 8 0 5 8 8 5 3 0 0 5 7 3 6 16 0 5 9 0 3 5 8 0 8 19 2 8 23 14 6 23 21 6 23 28 6 19 40 8 8 42 8 0 39 5 11 0 0 5 8 0 5 18 8 10 8 16 5 0 16 5 0 24 5 3 32 5 10 32 5 17 28 8 27 25 3 30 18 5 0 Output 58.953437 11.414214 16.0 61.874812 63.195179
instruction
0
37,697
23
75,394
"Correct Solution: ``` import math class Vec: def __init__(self, x, y): self.x = x self.y = y def __mul__(self, other): return Vec(self.x * other, self.y * other) def length(self): return math.sqrt(self.x * self.x + self.y * self.y) def __str__(self): return "({},{})".format(self.x, self.y) def __sub__(self, other): return Vec(self.x - other.x, self.y - other.y) def __add__(self, other): return Vec(self.x + other.x, self.y + other.y) def __truediv__(self, other): return Vec(self.x / other, self.y / other) def turn90(self): return Vec(-self.y, self.x) def normalized(self): return self / self.length() def is_clock(self, other): return self.x * other.y - self.y * other.x < 0 class Circle: def __init__(self, x, y, r): self.r = r self.center = Vec(x, y) def intersection(self, other): d = (self.center - other.center).length() r1 = self.r r2 = other.r d1 = (d * d + r1 * r1 - r2 * r2) / 2 / d x = math.sqrt(r1 * r1 - d1 * d1) e1 = (other.center - self.center).normalized() e2 = e1.turn90() p1 = self.center + e1 * d1 + e2 * x p2 = self.center + e1 * d1 - e2 * x return p1, p2 def __str__(self): return "({},{})".format(self.center, self.r) def solve(ps: [Circle]): intersects = [] for i in range(len(ps) - 1): ci = ps[i] ci_next = ps[i + 1] intersection1, intersection2 = ci.intersection(ci_next) intersects.append((intersection1, intersection2)) res = [[ps[0].center, -1, 0]] # 候補座標とその深度と左右(0,1) dists = [[2 ** 32, 2 ** 32] for _ in range(len(intersects))] # 各交点の距離バッファ min_path_length = 2 ** 32 while len(res) != 0: res = sorted(res, key=lambda a: a[1], reverse=True) c, depth, lr = res.pop() current_path_length = dists[depth][lr] if depth != -1 else 0 if depth == len(intersects) - 1: last_path = ps[-1].center - c min_path_length = min(min_path_length, current_path_length + last_path.length()) continue l_limit_min, r_limit_min = [a - c for a in intersects[depth + 1]] # 左右の限界 l_limit, r_limit = l_limit_min, r_limit_min # 到達可能限界 l_limit_d, r_limit_d = depth + 1, depth + 1 flag = True for i in range(depth + 2, len(intersects)): l_limit2, r_limit2 = [a - c for a in intersects[i]] if l_limit_min.is_clock(l_limit2): # 限界更新 l_limit_min = l_limit2 if l_limit2.is_clock(r_limit_min): # 到達可能なら、こっちも更新 l_limit = l_limit2 l_limit_d = i if r_limit2.is_clock(r_limit_min): # 限界更新 r_limit_min = r_limit2 if l_limit_min.is_clock(r_limit2): # 到達可能なら、こっちも更新 r_limit = r_limit2 r_limit_d = i if r_limit_min.is_clock(l_limit_min): flag = False break last_path = ps[-1].center - c if flag and l_limit_min.is_clock(last_path) and last_path.is_clock(r_limit_min): # この点からゴールまで行ける min_path_length = min(min_path_length, current_path_length + last_path.length()) continue if dists[l_limit_d][0] > l_limit.length() + current_path_length: dists[l_limit_d][0] = l_limit.length() + current_path_length res.append([l_limit + c, l_limit_d, 0]) if dists[r_limit_d][1] > r_limit.length() + current_path_length: dists[r_limit_d][1] = r_limit.length() + current_path_length res.append([r_limit + c, r_limit_d, 1]) return min_path_length def main(): while True: n = int(input()) if n == 0: break ps = [Circle(*[int(a) for a in input().split()]) for _ in range(n)] print(solve(ps)) if __name__ == '__main__': main() ```
output
1
37,697
23
75,395
Provide a correct Python 3 solution for this coding contest problem. There is a chain consisting of multiple circles on a plane. The first (last) circle of the chain only intersects with the next (previous) circle, and each intermediate circle intersects only with the two neighboring circles. Your task is to find the shortest path that satisfies the following conditions. * The path connects the centers of the first circle and the last circle. * The path is confined in the chain, that is, all the points on the path are located within or on at least one of the circles. Figure E-1 shows an example of such a chain and the corresponding shortest path. <image> Figure E-1: An example chain and the corresponding shortest path Input The input consists of multiple datasets. Each dataset represents the shape of a chain in the following format. > n > x1 y1 r1 > x2 y2 r2 > ... > xn yn rn > The first line of a dataset contains an integer n (3 ≤ n ≤ 100) representing the number of the circles. Each of the following n lines contains three integers separated by a single space. (xi, yi) and ri represent the center position and the radius of the i-th circle Ci. You can assume that 0 ≤ xi ≤ 1000, 0 ≤ yi ≤ 1000, and 1 ≤ ri ≤ 25. You can assume that Ci and Ci+1 (1 ≤ i ≤ n−1) intersect at two separate points. When j ≥ i+2, Ci and Cj are apart and either of them does not contain the other. In addition, you can assume that any circle does not contain the center of any other circle. The end of the input is indicated by a line containing a zero. Figure E-1 corresponds to the first dataset of Sample Input below. Figure E-2 shows the shortest paths for the subsequent datasets of Sample Input. <image> Figure E-2: Example chains and the corresponding shortest paths Output For each dataset, output a single line containing the length of the shortest chain-confined path between the centers of the first circle and the last circle. The value should not have an error greater than 0.001. No extra characters should appear in the output. Sample Input 10 802 0 10 814 0 4 820 1 4 826 1 4 832 3 5 838 5 5 845 7 3 849 10 3 853 14 4 857 18 3 3 0 0 5 8 0 5 8 8 5 3 0 0 5 7 3 6 16 0 5 9 0 3 5 8 0 8 19 2 8 23 14 6 23 21 6 23 28 6 19 40 8 8 42 8 0 39 5 11 0 0 5 8 0 5 18 8 10 8 16 5 0 16 5 0 24 5 3 32 5 10 32 5 17 28 8 27 25 3 30 18 5 0 Output for the Sample Input 58.953437 11.414214 16.0 61.874812 63.195179 Example Input 10 802 0 10 814 0 4 820 1 4 826 1 4 832 3 5 838 5 5 845 7 3 849 10 3 853 14 4 857 18 3 3 0 0 5 8 0 5 8 8 5 3 0 0 5 7 3 6 16 0 5 9 0 3 5 8 0 8 19 2 8 23 14 6 23 21 6 23 28 6 19 40 8 8 42 8 0 39 5 11 0 0 5 8 0 5 18 8 10 8 16 5 0 16 5 0 24 5 3 32 5 10 32 5 17 28 8 27 25 3 30 18 5 0 Output 58.953437 11.414214 16.0 61.874812 63.195179
instruction
0
37,698
23
75,396
"Correct Solution: ``` def solve(): from math import acos from cmath import phase, rect, pi from sys import stdin file_input = stdin while True: n = int(file_input.readline()) if n == 0: break C = (map(int, file_input.readline().split()) for i in range(n)) P = [] x, y, r1 = next(C) c1 = x + y * 1j P.append(c1) # calculation of cross points of circles for x, y, r2 in C: c2 = x + y * 1j base = c2 - c1 d = abs(base) a = acos((r1 ** 2 + d ** 2 - r2 ** 2) / (2 * r1 * d)) t = phase(base) cp1 = c1 + rect(r1, t + a) cp2 = c1 + rect(r1, t - a) P.append(cp1) P.append(cp2) c1, r1 = c2, r2 # search path and calculation of its cost lim = 5000 dist = [lim] * (2 * n) dist[0] = 0 goal = c1 g_idx = 2 * n - 1 indices = ((i + (i % 2) + 1, i + (i % 2) + 2) for i in range(g_idx)) for tpl_idx, cp, d in zip(indices, P, dist): j, k = tpl_idx s1 = None s2 = None p_s1 = None p_s2 = None for l, cp1, cp2 in zip(range(j, g_idx, 2), P[j::2], P[k::2]): t_s1 = cp1 - cp t_s2 = cp2 - cp if s1 is None or phase(s1 / t_s1) >= 0: s1 = t_s1 if s2 is None or phase(s2 / t_s2) <= 0: s2 = t_s2 if phase(s1 / s2) < 0: break if p_s1 != s1: dist[l] = min(dist[l], d + abs(s1)) p_s1 = s1 if p_s2 != s2: dist[l+1] = min(dist[l+1], d + abs(s2)) p_s2 = s2 else: gs = goal - cp if (s1 is None and s2 is None) or \ phase(s1 / gs) >= 0 and phase(s2 / gs) <= 0: dist[g_idx] = min(dist[g_idx], d + abs(gs)) print(dist[g_idx]) solve() ```
output
1
37,698
23
75,397
Provide a correct Python 3 solution for this coding contest problem. There is a chain consisting of multiple circles on a plane. The first (last) circle of the chain only intersects with the next (previous) circle, and each intermediate circle intersects only with the two neighboring circles. Your task is to find the shortest path that satisfies the following conditions. * The path connects the centers of the first circle and the last circle. * The path is confined in the chain, that is, all the points on the path are located within or on at least one of the circles. Figure E-1 shows an example of such a chain and the corresponding shortest path. <image> Figure E-1: An example chain and the corresponding shortest path Input The input consists of multiple datasets. Each dataset represents the shape of a chain in the following format. > n > x1 y1 r1 > x2 y2 r2 > ... > xn yn rn > The first line of a dataset contains an integer n (3 ≤ n ≤ 100) representing the number of the circles. Each of the following n lines contains three integers separated by a single space. (xi, yi) and ri represent the center position and the radius of the i-th circle Ci. You can assume that 0 ≤ xi ≤ 1000, 0 ≤ yi ≤ 1000, and 1 ≤ ri ≤ 25. You can assume that Ci and Ci+1 (1 ≤ i ≤ n−1) intersect at two separate points. When j ≥ i+2, Ci and Cj are apart and either of them does not contain the other. In addition, you can assume that any circle does not contain the center of any other circle. The end of the input is indicated by a line containing a zero. Figure E-1 corresponds to the first dataset of Sample Input below. Figure E-2 shows the shortest paths for the subsequent datasets of Sample Input. <image> Figure E-2: Example chains and the corresponding shortest paths Output For each dataset, output a single line containing the length of the shortest chain-confined path between the centers of the first circle and the last circle. The value should not have an error greater than 0.001. No extra characters should appear in the output. Sample Input 10 802 0 10 814 0 4 820 1 4 826 1 4 832 3 5 838 5 5 845 7 3 849 10 3 853 14 4 857 18 3 3 0 0 5 8 0 5 8 8 5 3 0 0 5 7 3 6 16 0 5 9 0 3 5 8 0 8 19 2 8 23 14 6 23 21 6 23 28 6 19 40 8 8 42 8 0 39 5 11 0 0 5 8 0 5 18 8 10 8 16 5 0 16 5 0 24 5 3 32 5 10 32 5 17 28 8 27 25 3 30 18 5 0 Output for the Sample Input 58.953437 11.414214 16.0 61.874812 63.195179 Example Input 10 802 0 10 814 0 4 820 1 4 826 1 4 832 3 5 838 5 5 845 7 3 849 10 3 853 14 4 857 18 3 3 0 0 5 8 0 5 8 8 5 3 0 0 5 7 3 6 16 0 5 9 0 3 5 8 0 8 19 2 8 23 14 6 23 21 6 23 28 6 19 40 8 8 42 8 0 39 5 11 0 0 5 8 0 5 18 8 10 8 16 5 0 16 5 0 24 5 3 32 5 10 32 5 17 28 8 27 25 3 30 18 5 0 Output 58.953437 11.414214 16.0 61.874812 63.195179
instruction
0
37,699
23
75,398
"Correct Solution: ``` import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**13 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 [int(x) for x in sys.stdin.readline().split()] 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 _kosa(a1, a2, b1, b2): x1,y1 = a1 x2,y2 = a2 x3,y3 = b1 x4,y4 = b2 tc = (x1-x2)*(y3-y1)+(y1-y2)*(x1-x3) td = (x1-x2)*(y4-y1)+(y1-y2)*(x1-x4) return tc*td < 0 def kosa(a1, a2, b1, b2): return _kosa(a1,a2,b1,b2) and _kosa(b1,b2,a1,a2) def distance(x1, y1, x2, y2): return math.sqrt((x1-x2)**2 + (y1-y2)**2) def distance_p(a, b): return distance(a[0], a[1], b[0], b[1]) def polar(a, r): return (math.cos(r) * a, math.sin(r) * a) def intersection_c(ap, bp): ax,ay,ar = ap bx,by,br = bp d = distance(ax,ay,bx,by) if d > ar + br or d < abs(ar - br): return None ac = math.acos((ar**2 + d**2 - br**2) / (2 * ar * d)) t = math.atan2(by - ay, bx - ax) r1 = polar(ar, t + ac) r2 = polar(ar, t - ac) return [(ax+r1[0], ay+r1[1]), (ax+r2[0], ay+r2[1])] def main(): rr = [] def f(n): a = [LI() for _ in range(n)] inters = [] for i in range(n-1): inters.append(intersection_c(a[i], a[i+1])) dp = [[inf]*2 for _ in range(n-1)] s = a[0][:2] rt = a[-1][:2] rf = True for k in range(n-1): if not kosa(s,rt,inters[k][0],inters[k][1]): rf = False break if rf: return distance_p(s,rt) for i in range(n-1): for j in range(2): t = inters[i][j] f = True for k in range(i): if not kosa(s,t, inters[k][0], inters[k][1]): f = False break if f: dp[i][j] = distance_p(s,t) r = inf # print('dp', dp) for i in range(n-1): for j in range(2): s = inters[i][j] sd = dp[i][j] for k in range(i+1,n-1): for l in range(2): t = inters[k][l] f = True for m in range(i+1,k): if not kosa(s,t,inters[m][0], inters[m][1]): f = False break # print('s,t,f',s,t,f) if f: nd = sd + distance_p(s,t) if dp[k][l] > nd: dp[k][l] = nd t = a[-1][:2] f = True for k in range(i+1,n-1): if not kosa(s,t,inters[k][0], inters[k][1]): f = False break if f: nd = sd + distance_p(s,t) if r > nd: r = nd # print('i',i,inters[i]) # print('dp', dp) # print('r', r) return '{:0.5f}'.format(r) while 1: n = I() if n == 0: break rr.append(f(n)) return '\n'.join(map(str,rr)) print(main()) ```
output
1
37,699
23
75,399
Provide a correct Python 3 solution for this coding contest problem. B: Hokkaido University Hard Note Please note that the question settings are the same as question A, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 10 ^ 3 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4
instruction
0
37,712
23
75,424
"Correct Solution: ``` # -*- coding: utf-8 -*- import sys from operator import itemgetter # gcd from fractions import gcd # 切り上げ,切り捨て from math import ceil, floor # リストの真のコピー(変更が伝播しない) from copy import deepcopy # 累積和。list(accumulate(A))としてAの累積和 from itertools import accumulate # l = ['a', 'b', 'b', 'c', 'b', 'a', 'c', 'c', 'b', 'c', 'b', 'a'] # S = Counter(l) # カウンタークラスが作られる。S=Counter({'b': 5, 'c': 4, 'a': 3}) # print(S.most_common(2)) # [('b', 5), ('c', 4)] # print(S.keys()) # dict_keys(['a', 'b', 'c']) # print(S.values()) # dict_values([3, 5, 4]) # print(S.items()) # dict_items([('a', 3), ('b', 5), ('c', 4)]) from collections import Counter import math from functools import reduce sys.setrecursionlimit(200000) # input関係の定義 # local only # if not __debug__: # fin = open('in_1.txt', 'r') # sys.stdin = fin # local only input = sys.stdin.readline def ii(): return int(input()) def mi(): return map(int, input().rstrip().split()) def lmi(): return list(map(int, input().rstrip().split())) def li(): return list(input().rstrip()) def debug(*args, sep=" ", end="\n"): print("debug:", *args, file=sys.stderr, sep=sep, end=end) if not __debug__ else None # template H, W = mi() S = [li() for i in range(H)] ans = [] for i in range(H): for j in range(W): if S[i][j] == "B": ans.append((i, j)) ans.sort(key=lambda x: x[0] + x[1]) debug(ans[-1]) debug(ans[0]) tmp = abs(ans[-1][0] - ans[0][0]) + abs(ans[-1][1] - ans[0][1]) ans.sort(key=lambda x: x[0] + (W - x[1])) debug(ans[-1]) debug(ans[0]) tmp2 = abs(ans[-1][0] - ans[0][0]) + abs(ans[-1][1] - ans[0][1]) print(max(tmp,tmp2)) ```
output
1
37,712
23
75,425
Provide a correct Python 3 solution for this coding contest problem. B: Hokkaido University Hard Note Please note that the question settings are the same as question A, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 10 ^ 3 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4
instruction
0
37,713
23
75,426
"Correct Solution: ``` def inpl(): return list(map(int, input().split())) H, W = inpl() ans = 0 C = [input() for _ in range(H)] for i in range(2): L = [0]*H R = [0]*H Lk = [] Rk = [] for h in range(H): L[h] = C[h].find("B") R[h] = C[h].rfind("B") if L[h] >= 0: Lk.append(h) if R[h] >= 0: Rk.append(h) for lh in Lk: for rh in Rk: ans = max(ans, abs(L[lh] - R[rh]) + abs(lh - rh)) if i == 0: C = ["".join(c) for c in zip(*C)] H, W = W, H print(ans) ```
output
1
37,713
23
75,427
Provide a correct Python 3 solution for this coding contest problem. B: Hokkaido University Hard Note Please note that the question settings are the same as question A, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 10 ^ 3 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4
instruction
0
37,714
23
75,428
"Correct Solution: ``` # from sys import exit, stderr # from inspect import currentframe # def debug(*args): # names = {id(v):k for k,v in currentframe().f_back.f_locals.items()} # print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args), file=stderr) # return INF = 10e10 H, W = [int(n) for n in input().split()] c = [["" for __ in range(W)] for _ in range(H)] B = set() for i in range(H): c[i] = list(str(input())) for j in range(W): if c[i][j] == "B": B.add((i,j)) a = 0 b = INF c = 0 d = INF for e in B: a = max(e[0] + e[1],a) b = min(e[0] + e[1],b) c = max(e[0] - e[1],c) d = min(e[0] - e[1],d) print(max(a-b, c-d)) ```
output
1
37,714
23
75,429
Provide a correct Python 3 solution for this coding contest problem. B: Hokkaido University Hard Note Please note that the question settings are the same as question A, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 10 ^ 3 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4
instruction
0
37,715
23
75,430
"Correct Solution: ``` # from sys import exit H, W = [int(n) for n in input().split()] c = [[0 for _ in range(W)] for __ in range(H)] Bs = [] for i in range(H): c[i] = list(str(input())) for j in range(W): if c[i][j] == "B": Bs.append((i, j)) INF = 10e10 a, c = -INF, -INF b, d = INF, INF for e in Bs: a = max(e[0] - e[1], a) b = min(e[0] - e[1], b) c = max(e[0] + e[1], c) d = min(e[0] + e[1], d) print(max(a-b, c-d)) ```
output
1
37,715
23
75,431
Provide a correct Python 3 solution for this coding contest problem. B: Hokkaido University Hard Note Please note that the question settings are the same as question A, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 10 ^ 3 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4
instruction
0
37,716
23
75,432
"Correct Solution: ``` #!/usr/bin/python3 # -*- coding: utf-8 -*- H,W = map(int, input().split()) builds = [] for h in range(H): l = str(input()) for w in range(W): if l[w] == "B": builds.append([h,w]) # [min,max] lu = [H+W,0] ld = [H+W,0] for h,w in builds: lu = [min(lu[0],h+w),max(lu[1],h+w)] ld = [min(ld[0],H-h+w),max(ld[1],H-h+w)] print(max(lu[1]-lu[0],ld[1]-ld[0])) ```
output
1
37,716
23
75,433
Provide a correct Python 3 solution for this coding contest problem. B: Hokkaido University Hard Note Please note that the question settings are the same as question A, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 10 ^ 3 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4
instruction
0
37,717
23
75,434
"Correct Solution: ``` H, W = map(int,input().split()) s = [] for k in range(H): s.append(input()) m1 = float("inf") M1 = 0 m2 = float("inf") M2 = 0 for k in range(H): for l in range(W): if s[k][l] == "B": m1 = min(m1,k+l) M1 = max(M1,k+l) m2 = min(m2,W-1-l+k) M2 = max(M2,W-1-l+k) print(max(M1-m1,M2-m2)) ```
output
1
37,717
23
75,435
Provide a correct Python 3 solution for this coding contest problem. B: Hokkaido University Hard Note Please note that the question settings are the same as question A, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 10 ^ 3 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4
instruction
0
37,718
23
75,436
"Correct Solution: ``` H,W=map(int,input().split()) MAP=[list(input()) for i in range(H)] BLIST=[] for i in range(H): for j in range(W): if MAP[i][j]=="B": BLIST.append([i,j]) BLIST.sort(key=lambda x:x[0]+x[1]) ANS=abs(BLIST[0][0]-BLIST[-1][0])+abs(BLIST[0][1]-BLIST[-1][1]) BLIST.sort(key=lambda x:x[0]-x[1]) ANS=max(ANS,abs(BLIST[0][0]-BLIST[-1][0])+abs(BLIST[0][1]-BLIST[-1][1])) print(ANS) ```
output
1
37,718
23
75,437
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya loves playing with squares. Mum bought him a square 2n × 2n in size. Petya marked a cell inside the square and now he is solving the following task. The task is to draw a broken line that would go along the grid lines and that would cut the square into two equal parts. The cutting line should not have any common points with the marked cell and the resulting two parts should be equal up to rotation. Petya wants to determine whether it is possible to cut the square in the required manner given the sizes of the square side and the coordinates of the marked cell. Help him. Input The first line contains three space-separated integers 2n, x and y (2 ≤ 2n ≤ 100, 1 ≤ x, y ≤ 2n), representing the length of a square's side and the coordinates of the marked cell. It is guaranteed that 2n is even. The coordinates of the marked cell are represented by a pair of numbers x y, where x represents the number of the row and y represents the number of the column. The rows and columns are numbered by consecutive integers from 1 to 2n. The rows are numbered from top to bottom and the columns are numbered from the left to the right. Output If the square is possible to cut, print "YES", otherwise print "NO" (without the quotes). Examples Input 4 1 1 Output YES Input 2 2 2 Output NO Note A sample test from the statement and one of the possible ways of cutting the square are shown in the picture: <image>
instruction
0
37,794
23
75,588
Tags: implementation, math Correct Solution: ``` n,x,y=map(int,input().split()) a=[(n//2),(n//2)+1] print(['YES','NO'][x in a and y in a]) ```
output
1
37,794
23
75,589
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya loves playing with squares. Mum bought him a square 2n × 2n in size. Petya marked a cell inside the square and now he is solving the following task. The task is to draw a broken line that would go along the grid lines and that would cut the square into two equal parts. The cutting line should not have any common points with the marked cell and the resulting two parts should be equal up to rotation. Petya wants to determine whether it is possible to cut the square in the required manner given the sizes of the square side and the coordinates of the marked cell. Help him. Input The first line contains three space-separated integers 2n, x and y (2 ≤ 2n ≤ 100, 1 ≤ x, y ≤ 2n), representing the length of a square's side and the coordinates of the marked cell. It is guaranteed that 2n is even. The coordinates of the marked cell are represented by a pair of numbers x y, where x represents the number of the row and y represents the number of the column. The rows and columns are numbered by consecutive integers from 1 to 2n. The rows are numbered from top to bottom and the columns are numbered from the left to the right. Output If the square is possible to cut, print "YES", otherwise print "NO" (without the quotes). Examples Input 4 1 1 Output YES Input 2 2 2 Output NO Note A sample test from the statement and one of the possible ways of cutting the square are shown in the picture: <image>
instruction
0
37,795
23
75,590
Tags: implementation, math Correct Solution: ``` n,x,y=map(int,input().split()) n=n//2 if (n==x or n==x-1) and (n==y or n==y-1): print("NO") else: print("YES") ```
output
1
37,795
23
75,591
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya loves playing with squares. Mum bought him a square 2n × 2n in size. Petya marked a cell inside the square and now he is solving the following task. The task is to draw a broken line that would go along the grid lines and that would cut the square into two equal parts. The cutting line should not have any common points with the marked cell and the resulting two parts should be equal up to rotation. Petya wants to determine whether it is possible to cut the square in the required manner given the sizes of the square side and the coordinates of the marked cell. Help him. Input The first line contains three space-separated integers 2n, x and y (2 ≤ 2n ≤ 100, 1 ≤ x, y ≤ 2n), representing the length of a square's side and the coordinates of the marked cell. It is guaranteed that 2n is even. The coordinates of the marked cell are represented by a pair of numbers x y, where x represents the number of the row and y represents the number of the column. The rows and columns are numbered by consecutive integers from 1 to 2n. The rows are numbered from top to bottom and the columns are numbered from the left to the right. Output If the square is possible to cut, print "YES", otherwise print "NO" (without the quotes). Examples Input 4 1 1 Output YES Input 2 2 2 Output NO Note A sample test from the statement and one of the possible ways of cutting the square are shown in the picture: <image>
instruction
0
37,796
23
75,592
Tags: implementation, math Correct Solution: ``` a,b,c=map(int,input().split()) a=a/2 if (a==b or a+1==b) and (a==c or a+1==c) : print("NO") else: print("YES") ```
output
1
37,796
23
75,593
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya loves playing with squares. Mum bought him a square 2n × 2n in size. Petya marked a cell inside the square and now he is solving the following task. The task is to draw a broken line that would go along the grid lines and that would cut the square into two equal parts. The cutting line should not have any common points with the marked cell and the resulting two parts should be equal up to rotation. Petya wants to determine whether it is possible to cut the square in the required manner given the sizes of the square side and the coordinates of the marked cell. Help him. Input The first line contains three space-separated integers 2n, x and y (2 ≤ 2n ≤ 100, 1 ≤ x, y ≤ 2n), representing the length of a square's side and the coordinates of the marked cell. It is guaranteed that 2n is even. The coordinates of the marked cell are represented by a pair of numbers x y, where x represents the number of the row and y represents the number of the column. The rows and columns are numbered by consecutive integers from 1 to 2n. The rows are numbered from top to bottom and the columns are numbered from the left to the right. Output If the square is possible to cut, print "YES", otherwise print "NO" (without the quotes). Examples Input 4 1 1 Output YES Input 2 2 2 Output NO Note A sample test from the statement and one of the possible ways of cutting the square are shown in the picture: <image>
instruction
0
37,797
23
75,594
Tags: implementation, math Correct Solution: ``` n, x, y = map(int, input().split()) n = n // 2 print('NO' if x in [n, n + 1] and y in [n, n + 1] else 'YES') ```
output
1
37,797
23
75,595
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya loves playing with squares. Mum bought him a square 2n × 2n in size. Petya marked a cell inside the square and now he is solving the following task. The task is to draw a broken line that would go along the grid lines and that would cut the square into two equal parts. The cutting line should not have any common points with the marked cell and the resulting two parts should be equal up to rotation. Petya wants to determine whether it is possible to cut the square in the required manner given the sizes of the square side and the coordinates of the marked cell. Help him. Input The first line contains three space-separated integers 2n, x and y (2 ≤ 2n ≤ 100, 1 ≤ x, y ≤ 2n), representing the length of a square's side and the coordinates of the marked cell. It is guaranteed that 2n is even. The coordinates of the marked cell are represented by a pair of numbers x y, where x represents the number of the row and y represents the number of the column. The rows and columns are numbered by consecutive integers from 1 to 2n. The rows are numbered from top to bottom and the columns are numbered from the left to the right. Output If the square is possible to cut, print "YES", otherwise print "NO" (without the quotes). Examples Input 4 1 1 Output YES Input 2 2 2 Output NO Note A sample test from the statement and one of the possible ways of cutting the square are shown in the picture: <image>
instruction
0
37,798
23
75,596
Tags: implementation, math Correct Solution: ``` n, x, y = map(int, input().split()) n //= 2 print('NO' if n <= x <= n + 1 and n <= y <= n + 1 else 'YES') ```
output
1
37,798
23
75,597