message stringlengths 2 28.7k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 21 109k | cluster float64 7 7 | __index_level_0__ int64 42 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Overlooking the captivating blend of myriads of vernal hues, Arkady the painter lays out a long, long canvas.
Arkady has a sufficiently large amount of paint of three colours: cyan, magenta, and yellow. On the one-dimensional canvas split into n consecutive segments, each segment needs to be painted in one of the colours.
Arkady has already painted some (possibly none or all) segments and passes the paintbrush to you. You are to determine whether there are at least two ways of colouring all the unpainted segments so that no two adjacent segments are of the same colour. Two ways are considered different if and only if a segment is painted in different colours in them.
Input
The first line contains a single positive integer n (1 ≤ n ≤ 100) — the length of the canvas.
The second line contains a string s of n characters, the i-th of which is either 'C' (denoting a segment painted in cyan), 'M' (denoting one painted in magenta), 'Y' (one painted in yellow), or '?' (an unpainted one).
Output
If there are at least two different ways of painting, output "Yes"; otherwise output "No" (both without quotes).
You can print each character in any case (upper or lower).
Examples
Input
5
CY??Y
Output
Yes
Input
5
C?C?Y
Output
Yes
Input
5
?CYC?
Output
Yes
Input
5
C??MM
Output
No
Input
3
MMY
Output
No
Note
For the first example, there are exactly two different ways of colouring: CYCMY and CYMCY.
For the second example, there are also exactly two different ways of colouring: CMCMY and CYCMY.
For the third example, there are four ways of colouring: MCYCM, MCYCY, YCYCM, and YCYCY.
For the fourth example, no matter how the unpainted segments are coloured, the existing magenta segments will prevent the painting from satisfying the requirements. The similar is true for the fifth example.
Submitted Solution:
```
n = int(input())
s = input()
flag = 0
if(n==1):
if(s[0]=='?'):
flag = 1
else:
flag = 0
else:
for i in range(n-1):
if(i==0):
if(s[i]!='?'):
if(s[i]==s[i+1]):
flag = 0
break
else:
flag = 1
else:
if(s[i]!='?'):
if(s[i]==s[i+1]):
flag = 0
break
else:
if(s[i-1]==s[i+1] and s[i-1]!='?'):
flag = 1
elif(s[i-1]=='?' or s[i+1]=='?'):
flag = 1
if(flag == 1):
print("Yes")
else:
print("No")
``` | instruction | 0 | 37,474 | 7 | 74,948 |
No | output | 1 | 37,474 | 7 | 74,949 |
Provide a correct Python 3 solution for this coding contest problem.
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 | instruction | 0 | 37,604 | 7 | 75,208 |
"Correct Solution:
```
import sys
from itertools import accumulate
readline = sys.stdin.readline
def accumulate2d(X):
N = len(X)
M = len(X[0])
for i in range(1, N):
for j in range(1, M):
X[i][j] += X[i][j-1]
for j in range(1, M):
for i in range(1, N):
X[i][j] += X[i-1][j]
return X
N, M, Q = map(int, readline().split())
G = [[0]*(M+2)] + [[0] + list(map(int, readline().strip())) + [0] for _ in range(N)] + [[0]*(M+2)]
U = [[0]*(M+2) for _ in range(N+2)]
L = [[0]*(M+2) for _ in range(N+2)]
for i in range(1, N+1):
for j in range(1, M+1):
if G[i][j]:
if G[i-1][j]:
U[i][j] += 1
if G[i][j-1]:
L[i][j] += 1
G = accumulate2d(G)
L = accumulate2d(L)
U = accumulate2d(U)
Ans = [None]*Q
for qu in range(Q):
x1, y1, x2, y2 = map(int, readline().split())
u = U[x1][y1-1] + U[x2][y2] - U[x1][y2] - U[x2][y1-1]
l = L[x1-1][y1] + L[x2][y2] - L[x1-1][y2] - L[x2][y1]
g = G[x1-1][y1-1] + G[x2][y2] - G[x1-1][y2] - G[x2][y1-1]
Ans[qu] = g - u - l
print('\n'.join(map(str, Ans)))
``` | output | 1 | 37,604 | 7 | 75,209 |
Provide a correct Python 3 solution for this coding contest problem.
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 | instruction | 0 | 37,605 | 7 | 75,210 |
"Correct Solution:
```
def main():
import itertools
import os
import sys
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
def cumsum(it):
"""
累積和
:param collections.Iterable it:
"""
cs = 0
ret = [0]
for v in it:
cs += v
ret.append(cs)
return ret
def cumsum_mat(mat):
h = len(mat)
w = len(mat[0])
tmp = []
for row in mat:
tmp.append(cumsum(row))
del mat
ret = [[0] * (w + 1) for _ in range(h + 1)]
for c in range(w + 1):
col = [row[c] for row in tmp]
for r, a in enumerate(cumsum(col)):
ret[r][c] = a
return ret
N, M, Q = list(map(int, sys.stdin.buffer.readline().split()))
S = [list(map(int, 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]
vs = []
e1 = []
e2 = []
counts_cum = cumsum_mat(S)
for x1, y1, x2, y2 in XY:
vs.append(count_v(x1, y1, x2, y2))
del counts_cum
edges_v = [[0] * M for _ in range(N)]
for h, w in itertools.product(range(1, N), range(M)):
edges_v[h][w] = S[h][w] & S[h - 1][w]
edges_v_cum = cumsum_mat(edges_v)
for x1, y1, x2, y2 in XY:
e1.append(count_ev(x1, y1, x2, y2))
del edges_v
del edges_v_cum
edges_h = [[0] * M for _ in range(N)]
for h, w in itertools.product(range(N), range(1, M)):
edges_h[h][w] = S[h][w] & S[h][w - 1]
edges_h_cum = cumsum_mat(edges_h)
for x1, y1, x2, y2 in XY:
e2.append(count_eh(x1, y1, x2, y2))
del edges_h
del edges_h_cum
ans = [v - e1 - e2 for v, e1, e2 in zip(vs, e1, e2)]
print(*ans, sep='\n')
main()
``` | output | 1 | 37,605 | 7 | 75,211 |
Provide a correct Python 3 solution for this coding contest problem.
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 | instruction | 0 | 37,606 | 7 | 75,212 |
"Correct Solution:
```
N,M,Q=map(int,input().split())
G=[input() for i in range(N)]
node=[[0]*(M+1)]
edge_x=[[0]*M]
edge_y=[[0]*(M+1)]
for i in range(1,N+1):
n,e_x,e_y=[0],[0],[0]
for j in range(1,M+1):
if G[i-1][j-1]=="1":
n.append(n[-1]+node[i-1][j]-node[i-1][j-1]+1)
else:
n.append(n[-1]+node[i-1][j]-node[i-1][j-1])
if j<M:
if G[i-1][j-1]=="1" and G[i-1][j]=="1":
e_x.append(e_x[-1]+edge_x[i-1][j]-edge_x[i-1][j-1]+1)
else:
e_x.append(e_x[-1]+edge_x[i-1][j]-edge_x[i-1][j-1])
if i<N:
if G[i-1][j-1]=="1" and G[i][j-1]=="1":
e_y.append(e_y[-1]+edge_y[i-1][j]-edge_y[i-1][j-1]+1)
else:
e_y.append(e_y[-1]+edge_y[i-1][j]-edge_y[i-1][j-1])
node.append(n)
edge_x.append(e_x)
if i<N:
edge_y.append(e_y)
for q in range(Q):
x1,y1,x2,y2=map(int,input().split())
n=node[x2][y2]-node[x1-1][y2]-node[x2][y1-1]+node[x1-1][y1-1]
e_y=edge_y[x2-1][y2]-edge_y[x1-1][y2]-edge_y[x2-1][y1-1]+edge_y[x1-1][y1-1]
e_x=edge_x[x2][y2-1]-edge_x[x1-1][y2-1]-edge_x[x2][y1-1]+edge_x[x1-1][y1-1]
e=e_x+e_y
print(n-e)
``` | output | 1 | 37,606 | 7 | 75,213 |
Provide a correct Python 3 solution for this coding contest problem.
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 | instruction | 0 | 37,607 | 7 | 75,214 |
"Correct Solution:
```
import sys
input = sys.stdin.readline
N, M, Q = map(int, input().split())
S = [input().rstrip() for _ in range(N)]
cum = [[0] * (M + 1) for _ in range(N + 1)]
for i in range(N):
for j in range(M):
cum[i+1][j+1] = int(S[i][j]) + cum[i][j+1] + cum[i+1][j] - cum[i][j]
cumh = [[0] * M for _ in range(N + 1)]
for i in range(N):
for j in range(M-1):
cumh[i+1][j+1] = cumh[i][j+1] + cumh[i+1][j] - cumh[i][j]
if S[i][j] == S[i][j+1] == '1':
cumh[i+1][j+1] += 1
cumv = [[0] * (M + 1) for _ in range(N)]
for i in range(N-1):
for j in range(M):
cumv[i+1][j+1] = cumv[i][j+1] + cumv[i+1][j] - cumv[i][j]
if S[i][j] == S[i+1][j] == '1':
cumv[i+1][j+1] += 1
for _ in range(Q):
x1, y1, x2, y2 = map(lambda x: int(x) - 1, input().split())
a = cum[x2+1][y2+1] - cum[x2+1][y1] - cum[x1][y2+1] + cum[x1][y1]
b = cumh[x2+1][y2] - cumh[x1][y2] - cumh[x2+1][y1] + cumh[x1][y1]
c = cumv[x2][y2+1] - cumv[x2][y1] - cumv[x1][y2+1] + cumv[x1][y1]
print(a - b - c)
``` | output | 1 | 37,607 | 7 | 75,215 |
Provide a correct Python 3 solution for this coding contest problem.
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 | instruction | 0 | 37,608 | 7 | 75,216 |
"Correct Solution:
```
import sys
n, m, q = map(int, input().split())
dp1 = [[0] * (m + 1) for _ in range(n + 1)]
dpv = [[0] * (m + 1) for _ in range(n + 1)]
dph = [[0] * (m + 1) for _ in range(n + 1)]
prev_s = '0' * m
for i in range(n):
s = input()
for j in range(m):
is1 = s[j] == '1'
additional = 0
if is1:
dp1[i + 1][j + 1] = 1
if i > 0 and prev_s[j] == '1':
dpv[i + 1][j + 1] = 1
if j > 0 and s[j - 1] == '1':
dph[i + 1][j + 1] = 1
prev_s = s
for i in range(1, n + 1):
for j in range(1, m + 1):
dp1[i][j] += dp1[i][j - 1]
dpv[i][j] += dpv[i][j - 1]
dph[i][j] += dph[i][j - 1]
for j in range(1, m + 1):
for i in range(1, n + 1):
dp1[i][j] += dp1[i - 1][j]
dpv[i][j] += dpv[i - 1][j]
dph[i][j] += dph[i - 1][j]
mp = map(int, sys.stdin.read().split())
buf = []
for x1, y1, x2, y2 in zip(mp, mp, mp, mp):
cnt1 = dp1[x2][y2] - dp1[x1 - 1][y2] - dp1[x2][y1 - 1] + dp1[x1 - 1][y1 - 1]
ver1 = dpv[x2][y2] - dpv[x1][y2] - dpv[x2][y1 - 1] + dpv[x1][y1 - 1]
hor1 = dph[x2][y2] - dph[x1 - 1][y2] - dph[x2][y1] + dph[x1 - 1][y1]
buf.append(cnt1 - ver1 - hor1)
print('\n'.join(map(str, buf)))
``` | output | 1 | 37,608 | 7 | 75,217 |
Provide a correct Python 3 solution for this coding contest problem.
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 | instruction | 0 | 37,609 | 7 | 75,218 |
"Correct Solution:
```
N,M,Q = map(int,input().split())
src = [input() for i in range(N)]
qs = [tuple(map(int,input().split())) for i in range(Q)]
blue_cum = [[0 for j in range(M+1)] for i in range(N+1)]
for i in range(N):
for j in range(M):
blue_cum[i+1][j+1] += blue_cum[i+1][j] + int(src[i][j])
for i in range(N):
for j in range(M+1):
blue_cum[i+1][j] += blue_cum[i][j]
h_edge = [[0 for j in range(M)] for i in range(N+1)]
for i in range(N):
for j in range(M-1):
h_edge[i+1][j+1] += h_edge[i+1][j] + int(src[i][j:j+2]=='11')
for i in range(N):
for j in range(M):
h_edge[i+1][j] += h_edge[i][j]
v_edge = [[0 for j in range(M+1)] for i in range(N)]
for i in range(N-1):
for j in range(M):
v_edge[i+1][j+1] += v_edge[i+1][j] + int(src[i][j]=='1' and src[i+1][j]=='1')
for i in range(N-1):
for j in range(M+1):
v_edge[i+1][j] += v_edge[i][j]
ans = []
for y1,x1,y2,x2 in qs:
y1 -= 1
x1 -= 1
blue = blue_cum[y1][x1] - blue_cum[y1][x2] - blue_cum[y2][x1] + blue_cum[y2][x2]
edge = h_edge[y1][x1] - h_edge[y1][x2-1] - h_edge[y2][x1] + h_edge[y2][x2-1]
edge+= v_edge[y1][x1] - v_edge[y1][x2] - v_edge[y2-1][x1] + v_edge[y2-1][x2]
ans.append(blue - edge)
print(*ans, sep='\n')
``` | output | 1 | 37,609 | 7 | 75,219 |
Provide a correct Python 3 solution for this coding contest problem.
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 | instruction | 0 | 37,610 | 7 | 75,220 |
"Correct Solution:
```
import sys
from itertools import accumulate
def solve():
n, m, q = map(int, input().split())
s = [[int(j) for j in input()] for i in range(n)]
# 青マスの2次元累積和
sxy = [list(accumulate(s[i])) for i in range(n)]
for j in range(m):
for i in range(n - 1):
sxy[i + 1][j] += sxy[i][j]
# 枝の2次元累積和(2n * 2mで管理)
exy = [[0]*(2*m) for i in range(2*n)]
for i in range(2*n - 1):
if i % 2 == 0:
for j in range(2*m - 1):
exy[i][j + 1] += exy[i][j]
if j % 2 == 0 and j < 2*m - 2:
exy[i][j + 1] += (s[i//2][j//2] == s[i//2][j//2 + 1] == 1)
else:
for j in range(2*m - 1):
if j % 2 == 0:
exy[i][j] += (s[i//2][j//2] == s[i//2 + 1][j//2] == 1)
exy[i][j + 1] += exy[i][j]
for j in range(2*m):
for i in range(2*n - 1):
exy[i + 1][j] += exy[i][j]
'''
for i in range(2*n):
print(exy[i], file=sys.stderr)
'''
for qi in range(q):
x1, y1, x2, y2 = map(lambda x: x - 1, map(int, sys.stdin.readline().split()))
nv = sxy[x2][y2]
if x1 - 1 >= 0:
nv -= sxy[x1 - 1][y2]
if y1 - 1 >= 0:
nv -= sxy[x2][y1 - 1]
if x1 - 1 >= 0 and y1 - 1 >= 0:
nv += sxy[x1 - 1][y1 - 1]
ne = exy[2*x2][2*y2]
if 2*x1 - 1 >= 0:
ne -= exy[2*x1 - 1][2*y2]
if 2*y1 - 1 >= 0:
ne -= exy[2*x2][2*y1 - 1]
if 2*x1 - 1 >= 0 and 2*y1 - 1 >= 0:
ne += exy[2*x1 - 1][2*y1 - 1]
ans = nv - ne
print(ans)
if __name__ == '__main__':
solve()
``` | output | 1 | 37,610 | 7 | 75,221 |
Provide a correct Python 3 solution for this coding contest problem.
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 | instruction | 0 | 37,611 | 7 | 75,222 |
"Correct Solution:
```
import sys
input = sys.stdin.readline
N, M, Q = map(int, input().split())
S = [list(map(int, list(input())[: -1])) for _ in range(N)]
e = [[0] * M for _ in range(N + 1)]
for i in range(1, N + 1):
for j in range(M - 1): e[i][j + 1] = e[i][j] + ((S[i - 1][j] == 1) and (S[i - 1][j + 1] == 1))
for i in range(1, N):
for j in range(M): e[i + 1][j] += e[i][j]
cs = [[0] * (M + 1) for _ in range(N + 1)]
for i in range(1, N + 1):
for j in range(M): cs[i][j + 1] = cs[i][j] + (S[i - 1][j] == 1)
for i in range(1, N):
for j in range(M + 1): cs[i + 1][j] += cs[i][j]
T = [[0] * N for _ in range(M)]
for i in range(N):
for j in range(M): T[j][i] = S[i][j]
N, M = M, N
ee = [[0] * M for _ in range(N + 1)]
for i in range(1, N + 1):
for j in range(M - 1): ee[i][j + 1] = ee[i][j] + ((T[i - 1][j] == 1) and (T[i - 1][j + 1] == 1))
for i in range(1, N):
for j in range(M): ee[i + 1][j] += ee[i][j]
#print(cs)
for i in range(Q):
u, l, d, r = map(lambda x: int(x) - 1, input().split())
rese = e[d + 1][r] - e[d + 1][l] - e[u][r] + e[u][l]
resee = ee[r + 1][d] - ee[r + 1][u] - ee[l][d] + ee[l][u]
res = cs[d + 1][r + 1] - cs[d + 1][l] - cs[u][r + 1] + cs[u][l]
print(res - rese - resee)
``` | output | 1 | 37,611 | 7 | 75,223 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vova has won n trophies in different competitions. Each trophy is either golden or silver. The trophies are arranged in a row.
The beauty of the arrangement is the length of the longest subsegment consisting of golden trophies. Vova wants to swap two trophies (not necessarily adjacent ones) to make the arrangement as beautiful as possible — that means, to maximize the length of the longest such subsegment.
Help Vova! Tell him the maximum possible beauty of the arrangement if he is allowed to do at most one swap.
Input
The first line contains one integer n (2 ≤ n ≤ 10^5) — the number of trophies.
The second line contains n characters, each of them is either G or S. If the i-th character is G, then the i-th trophy is a golden one, otherwise it's a silver trophy.
Output
Print the maximum possible length of a subsegment of golden trophies, if Vova is allowed to do at most one swap.
Examples
Input
10
GGGSGGGSGG
Output
7
Input
4
GGGG
Output
4
Input
3
SSS
Output
0
Note
In the first example Vova has to swap trophies with indices 4 and 10. Thus he will obtain the sequence "GGGGGGGSGS", the length of the longest subsegment of golden trophies is 7.
In the second example Vova can make no swaps at all. The length of the longest subsegment of golden trophies in the sequence is 4.
In the third example Vova cannot do anything to make the length of the longest subsegment of golden trophies in the sequence greater than 0.
Submitted Solution:
```
# Problem: Vasya and Book
# Link: https://codeforces.com/problemset/problem/1082/A
import sys
import unittest
from math import ceil as up
MAX_TROPHIES = 100000
#Different possible combination:
# GGGSGGGSGG
def B1082(colors):
curr = 0
prev = 0
is_swapped = False
first_G = False
swap_count = 0
prev_trophy = 'G'
tG = colors.count('G')
tS = colors.count('S')
if tG == 0: return 0
if tS == 0: return tG
for c in colors:
# ignore all S at the start because they do not impact the results.
if c == 'S' and not first_G:
continue
# We have already reached the best. Lets exit.
if curr == tG: break
if c == 'G':
curr += 1
first_G = True
elif c == 'S' and not is_swapped:
swap_count = curr
# Is G available to swap?
if curr == tG: break
# Swap and increment G count
curr += 1
is_swapped = True
else:
prev = max(curr, prev)
if prev_trophy == 'S':
swap_count = 0
curr = 1
else:
swap_count = curr - swap_count - 1
curr = 1 + swap_count
prev_trophy = c
return(max(curr, prev))
def B1082_wrapper(colors):
print('{}'.format(B1082(colors)))
def sanity_check(trophies, colors):
if trophies < 2 or trophies > MAX_TROPHIES:
print('[error] Number of trophies entered is invalid.')
exit(-1)
if (colors.count('G') + colors.count('S')) != trophies:
print('[error] Number of entered G and S are different than trophies.')
exit(-1)
def B1082_stdio():
''' This function read input from standard input. '''
trophies = int(sys.stdin.readline(), 10)
colors = sys.stdin.readline()
sanity_check(trophies, colors)
B1082_wrapper(colors)
def B1082_file(filename):
''' This funciton read input from a file. '''
f = open(filename, "r")
b_1082 = f.readlines()
try:
trophies = int(b_1082[0], 10)
sanity_check(trophies, b_1082[1])
except ValueError:
print('[error] Entered value is not an integer {}.'.format(given_tests[0]))
exit(0)
B1082_wrapper(b_1082[1])
if __name__ == '__main__':
B1082_stdio()
``` | instruction | 0 | 37,777 | 7 | 75,554 |
Yes | output | 1 | 37,777 | 7 | 75,555 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three multisets of pairs of colored sticks:
* R pairs of red sticks, the first pair has length r_1, the second pair has length r_2, ..., the R-th pair has length r_R;
* G pairs of green sticks, the first pair has length g_1, the second pair has length g_2, ..., the G-th pair has length g_G;
* B pairs of blue sticks, the first pair has length b_1, the second pair has length b_2, ..., the B-th pair has length b_B;
You are constructing rectangles from these pairs of sticks with the following process:
1. take a pair of sticks of one color;
2. take a pair of sticks of another color different from the first one;
3. add the area of the resulting rectangle to the total area.
Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.
Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.
What is the maximum area you can achieve?
Input
The first line contains three integers R, G, B (1 ≤ R, G, B ≤ 200) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks.
The second line contains R integers r_1, r_2, ..., r_R (1 ≤ r_i ≤ 2000) — the lengths of sticks in each pair of red sticks.
The third line contains G integers g_1, g_2, ..., g_G (1 ≤ g_i ≤ 2000) — the lengths of sticks in each pair of green sticks.
The fourth line contains B integers b_1, b_2, ..., b_B (1 ≤ b_i ≤ 2000) — the lengths of sticks in each pair of blue sticks.
Output
Print the maximum possible total area of the constructed rectangles.
Examples
Input
1 1 1
3
5
4
Output
20
Input
2 1 3
9 5
1
2 8 5
Output
99
Input
10 1 1
11 7 20 15 19 14 2 4 13 14
8
11
Output
372
Note
In the first example you can construct one of these rectangles: red and green with sides 3 and 5, red and blue with sides 3 and 4 and green and blue with sides 5 and 4. The best area of them is 4 × 5 = 20.
In the second example the best rectangles are: red/blue 9 × 8, red/blue 5 × 5, green/blue 2 × 1. So the total area is 72 + 25 + 2 = 99.
In the third example the best rectangles are: red/green 19 × 8 and red/blue 20 × 11. The total area is 152 + 220 = 372. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color. | instruction | 0 | 37,893 | 7 | 75,786 |
Tags: dp, greedy, sortings
Correct Solution:
```
import sys
input = sys.stdin.readline
def inInt():
return int(input())
def inStr():
return input().strip("\n")
def inIList():
return (list(map(int, input().split())))
def inSList():
return (input().split())
####################################################
def solve(r, g, b, dp):
for x in range(len(r) - 1, -1, -1):
for y in range(len(g) - 1, -1, -1):
for z in range(len(b) - 1, -1, -1):
dp[x][y][z] = max(
dp[x + 1][y + 1][z] + r[x] * g[y],
dp[x][y + 1][z + 1] + g[y] * b[z],
dp[x + 1][y][z + 1] + r[x] * b[z]
)
return dp[0][0][0]
nums = inIList()
r = sorted(inIList(), reverse=True)
g = sorted(inIList(), reverse=True)
b = sorted(inIList(), reverse=True)
dp = [[[0 for z in range(len(b) + 1)] for y in range(len(g) + 1)] for x in range(len(r) + 1)]
for x in range(len(r) - 1, -1 , -1):
for y in range(len(g) - 1, -1, -1):
dp[x][y][len(b)] = dp[x + 1][y + 1][len(b)] + r[x] * g[y]
for y in range(len(g) - 1, -1, -1):
for z in range(len(b) - 1, -1, -1):
dp[len(r)][y][z] = dp[len(r)][y + 1][z + 1] + g[y] * b[z]
for x in range(len(r) - 1, -1, -1):
for z in range(len(b) - 1, -1, -1):
dp[x][len(g)][z] = dp[x + 1][len(g)][z + 1] + r[x] * b[z]
print(solve(r, g, b, dp))
``` | output | 1 | 37,893 | 7 | 75,787 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three multisets of pairs of colored sticks:
* R pairs of red sticks, the first pair has length r_1, the second pair has length r_2, ..., the R-th pair has length r_R;
* G pairs of green sticks, the first pair has length g_1, the second pair has length g_2, ..., the G-th pair has length g_G;
* B pairs of blue sticks, the first pair has length b_1, the second pair has length b_2, ..., the B-th pair has length b_B;
You are constructing rectangles from these pairs of sticks with the following process:
1. take a pair of sticks of one color;
2. take a pair of sticks of another color different from the first one;
3. add the area of the resulting rectangle to the total area.
Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.
Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.
What is the maximum area you can achieve?
Input
The first line contains three integers R, G, B (1 ≤ R, G, B ≤ 200) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks.
The second line contains R integers r_1, r_2, ..., r_R (1 ≤ r_i ≤ 2000) — the lengths of sticks in each pair of red sticks.
The third line contains G integers g_1, g_2, ..., g_G (1 ≤ g_i ≤ 2000) — the lengths of sticks in each pair of green sticks.
The fourth line contains B integers b_1, b_2, ..., b_B (1 ≤ b_i ≤ 2000) — the lengths of sticks in each pair of blue sticks.
Output
Print the maximum possible total area of the constructed rectangles.
Examples
Input
1 1 1
3
5
4
Output
20
Input
2 1 3
9 5
1
2 8 5
Output
99
Input
10 1 1
11 7 20 15 19 14 2 4 13 14
8
11
Output
372
Note
In the first example you can construct one of these rectangles: red and green with sides 3 and 5, red and blue with sides 3 and 4 and green and blue with sides 5 and 4. The best area of them is 4 × 5 = 20.
In the second example the best rectangles are: red/blue 9 × 8, red/blue 5 × 5, green/blue 2 × 1. So the total area is 72 + 25 + 2 = 99.
In the third example the best rectangles are: red/green 19 × 8 and red/blue 20 × 11. The total area is 152 + 220 = 372. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color. | instruction | 0 | 37,894 | 7 | 75,788 |
Tags: dp, greedy, sortings
Correct Solution:
```
import sys
import bisect
#t=int(input())
t=1
for _ in range(t):
r,g,b=map(int,input().split())
#l=list(map(int,input().split()))
#l2=list(map(int,input().split()))
#n=int(input())
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
l3=list(map(int,input().split()))
l1.sort(reverse=True)
l2.sort(reverse=True)
l3.sort(reverse=True)
#l1.insert(0,0)
#l2.insert(0,0)
#l3.insert(0,0)
dp=[[[0 for k in range(b+1)] for j in range(g+1)]for i in range(r+1)]
#print(dp)
#dp[0][0][0]=max(l1[0]*l2[0],l2[0]*l3[0],l3[0]*l1[0])
for i in range(r+1):
for j in range(g+1):
for k in range(b+1):
val1,val2,val3=0,0,0
if i-1>=0 and j-1>=0:
val1=l1[i-1]*l2[j-1]
if j-1>=0 and k-1>=0:
val2=l2[j-1]*l3[k-1]
if k-1>=0 and i-1>=0:
val3=l3[k-1]*l1[i-1]
if val1!=0:
dp[i][j][k]=max(dp[i][j][k],dp[i-1][j-1][k]+val1)
if val2!=0:
dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k-1]+val2)
if val3!=0:
dp[i][j][k]=max(dp[i][j][k],dp[i-1][j][k-1]+val3)
maxi=0
for i in range(r+1):
for j in range(g+1):
for k in range(b+1):
maxi=max(maxi,dp[i][j][k])
print(maxi)
``` | output | 1 | 37,894 | 7 | 75,789 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three multisets of pairs of colored sticks:
* R pairs of red sticks, the first pair has length r_1, the second pair has length r_2, ..., the R-th pair has length r_R;
* G pairs of green sticks, the first pair has length g_1, the second pair has length g_2, ..., the G-th pair has length g_G;
* B pairs of blue sticks, the first pair has length b_1, the second pair has length b_2, ..., the B-th pair has length b_B;
You are constructing rectangles from these pairs of sticks with the following process:
1. take a pair of sticks of one color;
2. take a pair of sticks of another color different from the first one;
3. add the area of the resulting rectangle to the total area.
Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.
Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.
What is the maximum area you can achieve?
Input
The first line contains three integers R, G, B (1 ≤ R, G, B ≤ 200) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks.
The second line contains R integers r_1, r_2, ..., r_R (1 ≤ r_i ≤ 2000) — the lengths of sticks in each pair of red sticks.
The third line contains G integers g_1, g_2, ..., g_G (1 ≤ g_i ≤ 2000) — the lengths of sticks in each pair of green sticks.
The fourth line contains B integers b_1, b_2, ..., b_B (1 ≤ b_i ≤ 2000) — the lengths of sticks in each pair of blue sticks.
Output
Print the maximum possible total area of the constructed rectangles.
Examples
Input
1 1 1
3
5
4
Output
20
Input
2 1 3
9 5
1
2 8 5
Output
99
Input
10 1 1
11 7 20 15 19 14 2 4 13 14
8
11
Output
372
Note
In the first example you can construct one of these rectangles: red and green with sides 3 and 5, red and blue with sides 3 and 4 and green and blue with sides 5 and 4. The best area of them is 4 × 5 = 20.
In the second example the best rectangles are: red/blue 9 × 8, red/blue 5 × 5, green/blue 2 × 1. So the total area is 72 + 25 + 2 = 99.
In the third example the best rectangles are: red/green 19 × 8 and red/blue 20 × 11. The total area is 152 + 220 = 372. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color. | instruction | 0 | 37,895 | 7 | 75,790 |
Tags: dp, greedy, sortings
Correct Solution:
```
def helper(i,j,k,A,B,C,a,b,c,d):
if i>=a and j>=b or i>=a and k>=c or j>=b and k>=c:
return 0
if d[i][j][k]!=-1:
return d[i][j][k]
ans=0
x=0
y=0
z=0
if i<a and j<b:
x=A[i]*B[j]+helper(i+1,j+1,k,A,B,C,a,b,c,d)
if i<a and k<c:
y=A[i]*C[k]+helper(i+1,j,k+1,A,B,C,a,b,c,d)
if j<b and k<c:
z=B[j]*C[k]+helper(i,j+1,k+1,A,B,C,a,b,c,d)
d[i][j][k] =max(x,y,z)
return d[i][j][k]
def answer(a,b,c,A,B,C):
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
d=[[[-1 for i in range(202)] for j in range(202)] for k in range(202)]
return helper(0,0,0,A,B,C,a,b,c,d)
a,b,c=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
print(answer(a,b,c,A,B,C))
``` | output | 1 | 37,895 | 7 | 75,791 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three multisets of pairs of colored sticks:
* R pairs of red sticks, the first pair has length r_1, the second pair has length r_2, ..., the R-th pair has length r_R;
* G pairs of green sticks, the first pair has length g_1, the second pair has length g_2, ..., the G-th pair has length g_G;
* B pairs of blue sticks, the first pair has length b_1, the second pair has length b_2, ..., the B-th pair has length b_B;
You are constructing rectangles from these pairs of sticks with the following process:
1. take a pair of sticks of one color;
2. take a pair of sticks of another color different from the first one;
3. add the area of the resulting rectangle to the total area.
Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.
Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.
What is the maximum area you can achieve?
Input
The first line contains three integers R, G, B (1 ≤ R, G, B ≤ 200) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks.
The second line contains R integers r_1, r_2, ..., r_R (1 ≤ r_i ≤ 2000) — the lengths of sticks in each pair of red sticks.
The third line contains G integers g_1, g_2, ..., g_G (1 ≤ g_i ≤ 2000) — the lengths of sticks in each pair of green sticks.
The fourth line contains B integers b_1, b_2, ..., b_B (1 ≤ b_i ≤ 2000) — the lengths of sticks in each pair of blue sticks.
Output
Print the maximum possible total area of the constructed rectangles.
Examples
Input
1 1 1
3
5
4
Output
20
Input
2 1 3
9 5
1
2 8 5
Output
99
Input
10 1 1
11 7 20 15 19 14 2 4 13 14
8
11
Output
372
Note
In the first example you can construct one of these rectangles: red and green with sides 3 and 5, red and blue with sides 3 and 4 and green and blue with sides 5 and 4. The best area of them is 4 × 5 = 20.
In the second example the best rectangles are: red/blue 9 × 8, red/blue 5 × 5, green/blue 2 × 1. So the total area is 72 + 25 + 2 = 99.
In the third example the best rectangles are: red/green 19 × 8 and red/blue 20 × 11. The total area is 152 + 220 = 372. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color. | instruction | 0 | 37,896 | 7 | 75,792 |
Tags: dp, greedy, sortings
Correct Solution:
```
import sys
input=sys.stdin.buffer.readline
nr,ng,nb=[int(x) for x in input().split()]
r=[int(x) for x in input().split()]
g=[int(x) for x in input().split()]
b=[int(x) for x in input().split()]
r.sort()
g.sort()
b.sort()
memo=[[[-1 for _ in range(nb+1)] for __ in range(ng+1)] for ___ in range(nr+1)]
memo[0][0][0]=0 #starting point when i==-1,j==-1,k==-1
for i in range(nr):
memo[i+1][0][0]=0
for j in range(ng):
memo[0][j+1][0]=0
for k in range(nb):
memo[0][0][k+1]=0
def dp(i,j,k): #dp(i,j,k) is the max value including r[i],g[j],b[k]
if i<-1 or j<-1 or k<-1:
return -float('inf')
if memo[i+1][j+1][k+1]==-1: #offset by 1 because i,j,k can be -1
memo[i+1][j+1][k+1]=max(dp(i,j-1,k-1)+g[j]*b[k],
dp(i-1,j-1,k)+r[i]*g[j],
dp(i-1,j,k-1)+r[i]*b[k]
)
return memo[i+1][j+1][k+1]
#for i in range(max(nr,ng,nb)):
# dp(min(i,nr-1),min(i,ng-1),min(i,nb-1))
print(dp(nr-1,ng-1,nb-1))
``` | output | 1 | 37,896 | 7 | 75,793 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three multisets of pairs of colored sticks:
* R pairs of red sticks, the first pair has length r_1, the second pair has length r_2, ..., the R-th pair has length r_R;
* G pairs of green sticks, the first pair has length g_1, the second pair has length g_2, ..., the G-th pair has length g_G;
* B pairs of blue sticks, the first pair has length b_1, the second pair has length b_2, ..., the B-th pair has length b_B;
You are constructing rectangles from these pairs of sticks with the following process:
1. take a pair of sticks of one color;
2. take a pair of sticks of another color different from the first one;
3. add the area of the resulting rectangle to the total area.
Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.
Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.
What is the maximum area you can achieve?
Input
The first line contains three integers R, G, B (1 ≤ R, G, B ≤ 200) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks.
The second line contains R integers r_1, r_2, ..., r_R (1 ≤ r_i ≤ 2000) — the lengths of sticks in each pair of red sticks.
The third line contains G integers g_1, g_2, ..., g_G (1 ≤ g_i ≤ 2000) — the lengths of sticks in each pair of green sticks.
The fourth line contains B integers b_1, b_2, ..., b_B (1 ≤ b_i ≤ 2000) — the lengths of sticks in each pair of blue sticks.
Output
Print the maximum possible total area of the constructed rectangles.
Examples
Input
1 1 1
3
5
4
Output
20
Input
2 1 3
9 5
1
2 8 5
Output
99
Input
10 1 1
11 7 20 15 19 14 2 4 13 14
8
11
Output
372
Note
In the first example you can construct one of these rectangles: red and green with sides 3 and 5, red and blue with sides 3 and 4 and green and blue with sides 5 and 4. The best area of them is 4 × 5 = 20.
In the second example the best rectangles are: red/blue 9 × 8, red/blue 5 × 5, green/blue 2 × 1. So the total area is 72 + 25 + 2 = 99.
In the third example the best rectangles are: red/green 19 × 8 and red/blue 20 × 11. The total area is 152 + 220 = 372. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color. | instruction | 0 | 37,897 | 7 | 75,794 |
Tags: dp, greedy, sortings
Correct Solution:
```
from collections import defaultdict
def main():
R, G, B = map(int, input().split())
red = list(map(int, input().split()))
green = list(map(int, input().split()))
blue = list(map(int, input().split()))
red.sort(reverse=True)
green.sort(reverse=True)
blue.sort(reverse=True)
dp = [[[-2*10**9]*(B+3) for i in range(G+3)] for j in range(R+3)]
dp[0][0][0] = 0
ans = 0
for i in range(R+1):
for j in range(G+1):
for k in range(B+1):
if i == 0 and j == 0 and k == 0: continue
dp[i][j][k] = max(dp[i-1][j-1][k]+red[i-1]*green[j-1], dp[i]
[j-1][k-1]+green[j-1]*blue[k-1], dp[i-1][j][k-1]+red[i-1]*blue[k-1])
ans = max(ans, dp[i][j][k])
print(ans)
return
if __name__ == "__main__":
main()
``` | output | 1 | 37,897 | 7 | 75,795 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three multisets of pairs of colored sticks:
* R pairs of red sticks, the first pair has length r_1, the second pair has length r_2, ..., the R-th pair has length r_R;
* G pairs of green sticks, the first pair has length g_1, the second pair has length g_2, ..., the G-th pair has length g_G;
* B pairs of blue sticks, the first pair has length b_1, the second pair has length b_2, ..., the B-th pair has length b_B;
You are constructing rectangles from these pairs of sticks with the following process:
1. take a pair of sticks of one color;
2. take a pair of sticks of another color different from the first one;
3. add the area of the resulting rectangle to the total area.
Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.
Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.
What is the maximum area you can achieve?
Input
The first line contains three integers R, G, B (1 ≤ R, G, B ≤ 200) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks.
The second line contains R integers r_1, r_2, ..., r_R (1 ≤ r_i ≤ 2000) — the lengths of sticks in each pair of red sticks.
The third line contains G integers g_1, g_2, ..., g_G (1 ≤ g_i ≤ 2000) — the lengths of sticks in each pair of green sticks.
The fourth line contains B integers b_1, b_2, ..., b_B (1 ≤ b_i ≤ 2000) — the lengths of sticks in each pair of blue sticks.
Output
Print the maximum possible total area of the constructed rectangles.
Examples
Input
1 1 1
3
5
4
Output
20
Input
2 1 3
9 5
1
2 8 5
Output
99
Input
10 1 1
11 7 20 15 19 14 2 4 13 14
8
11
Output
372
Note
In the first example you can construct one of these rectangles: red and green with sides 3 and 5, red and blue with sides 3 and 4 and green and blue with sides 5 and 4. The best area of them is 4 × 5 = 20.
In the second example the best rectangles are: red/blue 9 × 8, red/blue 5 × 5, green/blue 2 × 1. So the total area is 72 + 25 + 2 = 99.
In the third example the best rectangles are: red/green 19 × 8 and red/blue 20 × 11. The total area is 152 + 220 = 372. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color. | instruction | 0 | 37,898 | 7 | 75,796 |
Tags: dp, greedy, sortings
Correct Solution:
```
r,g,b=list(map(int,input().split()))
x=list(map(int,input().split()))
y=list(map(int,input().split()))
z=list(map(int,input().split()))
x=sorted(x,reverse=True)
y=sorted(y,reverse=True)
z=sorted(z,reverse=True)
x.append(0)
y.append(0)
z.append(0)
def solution(red,green,blue):
if dp[red][green][blue]==0:
a1=x[red]*y[green]
a2=y[green]*z[blue]
a3=x[red]*z[blue]
if r!=red and g!=green:
a1+=solution(red+1,green+1,blue)
if g!=green and b!=blue:
a2+=solution(red,green+1,blue+1)
if r!=red and b!=blue:
a3+=solution(red+1,green,blue+1)
dp[red][green][blue]=max(a1,a2,a3)
return dp[red][green][blue]
dp=[[[0 for l in range(b+1)] for j in range(g+1)] for k in range(r+1)]
print(solution(0,0,0))
``` | output | 1 | 37,898 | 7 | 75,797 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three multisets of pairs of colored sticks:
* R pairs of red sticks, the first pair has length r_1, the second pair has length r_2, ..., the R-th pair has length r_R;
* G pairs of green sticks, the first pair has length g_1, the second pair has length g_2, ..., the G-th pair has length g_G;
* B pairs of blue sticks, the first pair has length b_1, the second pair has length b_2, ..., the B-th pair has length b_B;
You are constructing rectangles from these pairs of sticks with the following process:
1. take a pair of sticks of one color;
2. take a pair of sticks of another color different from the first one;
3. add the area of the resulting rectangle to the total area.
Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.
Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.
What is the maximum area you can achieve?
Input
The first line contains three integers R, G, B (1 ≤ R, G, B ≤ 200) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks.
The second line contains R integers r_1, r_2, ..., r_R (1 ≤ r_i ≤ 2000) — the lengths of sticks in each pair of red sticks.
The third line contains G integers g_1, g_2, ..., g_G (1 ≤ g_i ≤ 2000) — the lengths of sticks in each pair of green sticks.
The fourth line contains B integers b_1, b_2, ..., b_B (1 ≤ b_i ≤ 2000) — the lengths of sticks in each pair of blue sticks.
Output
Print the maximum possible total area of the constructed rectangles.
Examples
Input
1 1 1
3
5
4
Output
20
Input
2 1 3
9 5
1
2 8 5
Output
99
Input
10 1 1
11 7 20 15 19 14 2 4 13 14
8
11
Output
372
Note
In the first example you can construct one of these rectangles: red and green with sides 3 and 5, red and blue with sides 3 and 4 and green and blue with sides 5 and 4. The best area of them is 4 × 5 = 20.
In the second example the best rectangles are: red/blue 9 × 8, red/blue 5 × 5, green/blue 2 × 1. So the total area is 72 + 25 + 2 = 99.
In the third example the best rectangles are: red/green 19 × 8 and red/blue 20 × 11. The total area is 152 + 220 = 372. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color. | instruction | 0 | 37,899 | 7 | 75,798 |
Tags: dp, greedy, sortings
Correct Solution:
```
from collections import defaultdict as dd
import math
import sys
import heapq
import copy
input=sys.stdin.readline
def nn():
return int(input())
def li():
return list(input())
def mi():
return map(int, input().split())
def lm():
return list(map(int, input().split()))
def solve():
r,g,b = mi()
rs = lm()
gs = lm()
bs = lm()
rs.sort()
gs.sort()
bs.sort()
ans = [[[0 for i in range(b+1)] for i in range(g+1)] for i in range(r+1)]
for i in range(1,r+1):
for j in range(1,g+1):
ans[i][j][0]= ans[i-1][j-1][0]+rs[i-1]*gs[j-1]
for i in range(r+1):
for j in range(g+1):
for k in range(1,b+1):
new_len = bs[k-1]
if i==0:
i_len = 0
else:
i_len = ans[i-1][j][k-1] + rs[i-1]*new_len
if j==0:
j_len = 0
else:
j_len = ans[i][j-1][k-1] + gs[j-1]*new_len
if i>0 and j>0:
i_j_len = ans[i-1][j-1][k]+rs[i-1]*gs[j-1]
else:
i_j_len = 0
ans[i][j][k] = max(i_len,
j_len,
ans[i][j][k-1],
i_j_len)
#print(ans)
print(ans[r][g][b])
solve()
``` | output | 1 | 37,899 | 7 | 75,799 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three multisets of pairs of colored sticks:
* R pairs of red sticks, the first pair has length r_1, the second pair has length r_2, ..., the R-th pair has length r_R;
* G pairs of green sticks, the first pair has length g_1, the second pair has length g_2, ..., the G-th pair has length g_G;
* B pairs of blue sticks, the first pair has length b_1, the second pair has length b_2, ..., the B-th pair has length b_B;
You are constructing rectangles from these pairs of sticks with the following process:
1. take a pair of sticks of one color;
2. take a pair of sticks of another color different from the first one;
3. add the area of the resulting rectangle to the total area.
Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.
Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.
What is the maximum area you can achieve?
Input
The first line contains three integers R, G, B (1 ≤ R, G, B ≤ 200) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks.
The second line contains R integers r_1, r_2, ..., r_R (1 ≤ r_i ≤ 2000) — the lengths of sticks in each pair of red sticks.
The third line contains G integers g_1, g_2, ..., g_G (1 ≤ g_i ≤ 2000) — the lengths of sticks in each pair of green sticks.
The fourth line contains B integers b_1, b_2, ..., b_B (1 ≤ b_i ≤ 2000) — the lengths of sticks in each pair of blue sticks.
Output
Print the maximum possible total area of the constructed rectangles.
Examples
Input
1 1 1
3
5
4
Output
20
Input
2 1 3
9 5
1
2 8 5
Output
99
Input
10 1 1
11 7 20 15 19 14 2 4 13 14
8
11
Output
372
Note
In the first example you can construct one of these rectangles: red and green with sides 3 and 5, red and blue with sides 3 and 4 and green and blue with sides 5 and 4. The best area of them is 4 × 5 = 20.
In the second example the best rectangles are: red/blue 9 × 8, red/blue 5 × 5, green/blue 2 × 1. So the total area is 72 + 25 + 2 = 99.
In the third example the best rectangles are: red/green 19 × 8 and red/blue 20 × 11. The total area is 152 + 220 = 372. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color. | instruction | 0 | 37,900 | 7 | 75,800 |
Tags: dp, greedy, sortings
Correct Solution:
```
#!/usr/bin/env python3
import io
import os
import sys
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def prdbg(*args, **kwargs):
print(*args, **kwargs)
pass
def get_str():
return input().decode().strip()
def rint():
return map(int, input().split())
def oint():
return int(input())
def valid(i1,i2,i3):
if (i1+i2+i3)%2 or (i1 < 0 or i2 < 0 or i3 < 0) or i3 > i1 + i2\
or i2 > i1 + i3 or i1 > i2 + i3:
return False
return True
def dfs(i1,i2,i3):
#if not valid(i1, i2, i3):
if (i1 + i2 + i3) % 2 or (i1 < 0 or i2 < 0 or i3 < 0) or i3 > i1 + i2 \
or i2 > i1 + i3 or i1 > i2 + i3:
return -2
if dp[i1][i2][i3] != -1:
return dp[i1][i2][i3]
ret1 = dfs(i1-1, i2-1, i3)
if ret1 >= 0 :
ret1 += a1[i1]*a2[i2]
ret2 = dfs(i1-1, i2, i3-1)
if ret2 >= 0:
ret2 += a1[i1]*a3[i3]
ret3 = dfs(i1, i2-1, i3-1)
if ret3 >= 0:
ret3 += a2[i2]*a3[i3]
ret = max(ret1, ret2, ret3)
dp[i1][i2][i3] = ret
return ret
n1, n2, n3 = rint()
a1, a2, a3 = list(rint()), list(rint()), list(rint())
a1.sort(reverse=True)
a2.sort(reverse=True)
a3.sort(reverse=True)
a1 = [0] + a1
a2 = [0] + a2
a3 = [0] + a3
n1 += 1
n2 += 1
n3 += 1
dp = [[[-1 for i3 in range(n3)] for i2 in range(n2)] for i1 in range(n1)]
dp[1][1][0] = a1[1]*a2[1]
dp[1][0][1] = a1[1]*a3[1]
dp[0][1][1] = a2[1]*a3[1]
dp[0][0][0] = -2
for i1 in range(n1):
for i2 in range(n2):
for i3 in range(n3):
dfs(i1, i2, i3)
ans = -1
for i1 in range(n1):
for i2 in range(n2):
for i3 in range(n3):
ans = max(ans, dp[i1][i2][i3])
#print(a1,a2,a3)
print(ans)
``` | output | 1 | 37,900 | 7 | 75,801 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two polar bears Menshykov and Uslada from the St.Petersburg zoo and elephant Horace from the Kiev zoo got six sticks to play with and assess the animals' creativity. Menshykov, Uslada and Horace decided to make either an elephant or a bear from those sticks. They can make an animal from sticks in the following way:
* Four sticks represent the animal's legs, these sticks should have the same length.
* Two remaining sticks represent the animal's head and body. The bear's head stick must be shorter than the body stick. The elephant, however, has a long trunk, so his head stick must be as long as the body stick. Note that there are no limits on the relations between the leg sticks and the head and body sticks.
Your task is to find out which animal can be made from the given stick set. The zoo keeper wants the sticks back after the game, so they must never be broken, even bears understand it.
Input
The single line contains six space-separated integers li (1 ≤ li ≤ 9) — the lengths of the six sticks. It is guaranteed that the input is such that you cannot make both animals from the sticks.
Output
If you can make a bear from the given set, print string "Bear" (without the quotes). If you can make an elephant, print string "Elephant" (wıthout the quotes). If you can make neither a bear nor an elephant, print string "Alien" (without the quotes).
Examples
Input
4 2 5 4 4 4
Output
Bear
Input
4 4 5 4 4 5
Output
Elephant
Input
1 2 3 4 5 6
Output
Alien
Note
If you're out of creative ideas, see instructions below which show how to make a bear and an elephant in the first two samples. The stick of length 2 is in red, the sticks of length 4 are in green, the sticks of length 5 are in blue.
<image> | instruction | 0 | 38,051 | 7 | 76,102 |
Tags: implementation
Correct Solution:
```
l = [0] * 10
for i in input().split():
l[int(i)] += 1
l.sort()
if l[-1] >= 4:
if l[-1] == 6 or l[-2] == 2:
print("Elephant")
else:
print("Bear")
else:
print("Alien")
``` | output | 1 | 38,051 | 7 | 76,103 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two polar bears Menshykov and Uslada from the St.Petersburg zoo and elephant Horace from the Kiev zoo got six sticks to play with and assess the animals' creativity. Menshykov, Uslada and Horace decided to make either an elephant or a bear from those sticks. They can make an animal from sticks in the following way:
* Four sticks represent the animal's legs, these sticks should have the same length.
* Two remaining sticks represent the animal's head and body. The bear's head stick must be shorter than the body stick. The elephant, however, has a long trunk, so his head stick must be as long as the body stick. Note that there are no limits on the relations between the leg sticks and the head and body sticks.
Your task is to find out which animal can be made from the given stick set. The zoo keeper wants the sticks back after the game, so they must never be broken, even bears understand it.
Input
The single line contains six space-separated integers li (1 ≤ li ≤ 9) — the lengths of the six sticks. It is guaranteed that the input is such that you cannot make both animals from the sticks.
Output
If you can make a bear from the given set, print string "Bear" (without the quotes). If you can make an elephant, print string "Elephant" (wıthout the quotes). If you can make neither a bear nor an elephant, print string "Alien" (without the quotes).
Examples
Input
4 2 5 4 4 4
Output
Bear
Input
4 4 5 4 4 5
Output
Elephant
Input
1 2 3 4 5 6
Output
Alien
Note
If you're out of creative ideas, see instructions below which show how to make a bear and an elephant in the first two samples. The stick of length 2 is in red, the sticks of length 4 are in green, the sticks of length 5 are in blue.
<image> | instruction | 0 | 38,054 | 7 | 76,108 |
Tags: implementation
Correct Solution:
```
s = sorted(int(x) for x in input().split())
for i in range(3):
if s[i] == s[i + 3]:
s[i:i + 4] = []
print('Elephant' if s[0] == s[1] else 'Bear')
exit()
print('Alien')
``` | output | 1 | 38,054 | 7 | 76,109 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 3000, 1 ≤ k ≤ min(n, 10)) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows:
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows:
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample. | instruction | 0 | 38,127 | 7 | 76,254 |
Tags: two pointers
Correct Solution:
```
#they are all inclusive
R, C, N, K = map(int, input().split())
v = []
for i in range(N):
v.append(tuple(map(int, input().split())))
ans = 0
for sr in range(1, R+1):
for sc in range(1, C+1):
for er in range(sr, R+1):
for ec in range(sc, C+1):
cnt = 0
for x, y in v:
if sr <= x <= er and sc <= y <= ec:
cnt += 1
if cnt >= K:
ans += 1
print(ans)
``` | output | 1 | 38,127 | 7 | 76,255 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 3000, 1 ≤ k ≤ min(n, 10)) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows:
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows:
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample. | instruction | 0 | 38,128 | 7 | 76,256 |
Tags: two pointers
Correct Solution:
```
f = lambda: map(int, input().split())
r, c, n, k = f()
t = [[0] * (c + 1) for i in range(r + 1)]
for j in range(n):
y, x = f()
t[y][x] = 1
for y in range(r):
p = t[y + 1]
for x in range(c): p[x + 1] += p[x]
t[y + 1] = [a + b for a, b in zip(p, t[y])]
s = (c * c + c) * (r * r + r) >> 2
for y in range(r):
for x in range(c):
for a in range(1, c - x + 1):
for b in range(1, r - y + 1):
if t[y + b][x + a] + t[y][x] - t[y + b][x] - t[y][x + a] >= k: break
s -= 1
print(s)
``` | output | 1 | 38,128 | 7 | 76,257 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 3000, 1 ≤ k ≤ min(n, 10)) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows:
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows:
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample. | instruction | 0 | 38,129 | 7 | 76,258 |
Tags: two pointers
Correct Solution:
```
r,c,n,k = map(int,input().split())
xy = [list(map(int,input().split())) for i in range(n)]
field = [[0]*c for i in range(r)]
for i in range(n):
field[xy[i][0]-1][xy[i][1]-1] = 1
num = 0
for i in range(r):
for j in range(c):
for i2 in range(i,r):
for j2 in range(j,c):
num2 = 0
for i3 in range(i,i2+1):
for j3 in range(j,j2+1):
if field[i3][j3] == 1:
num2 += 1
if num2 >= k:
num += 1
print(num)
``` | output | 1 | 38,129 | 7 | 76,259 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 3000, 1 ≤ k ≤ min(n, 10)) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows:
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows:
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample. | instruction | 0 | 38,130 | 7 | 76,260 |
Tags: two pointers
Correct Solution:
```
r, c, n, k = list(map(int, input().split()))
A = [2] * n
for i in range(n):
A[i] = list(map(int, input().split()))
answer = 0
for i1 in range(1, r + 1):
for j1 in range(1, c + 1):
for i2 in range(i1, r + 1):
for j2 in range(j1, c + 1):
t = 0
for i in range(n):
if i1 <= A[i][0] <= i2 and j1 <= A[i][1] <= j2:
t += 1
answer += (t >= k)
print(answer)
``` | output | 1 | 38,130 | 7 | 76,261 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 3000, 1 ≤ k ≤ min(n, 10)) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows:
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows:
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample. | instruction | 0 | 38,131 | 7 | 76,262 |
Tags: two pointers
Correct Solution:
```
r, c, n, k = list(map(int, input().split()))
alt = []
count = 0
for i in range(n):
a, b = list(map(int, input().split()))
alt.append([a - 1, b - 1])
for x1 in range(r):
for x2 in range(x1 + 1):
for y1 in range(c):
for y2 in range(y1 + 1):
d = 0
for i in range(n):
if (alt[i][0] <= x1 and alt[i][0] >= x2 and alt[i][1] <= y1 and alt[i][1] >= y2):
d += 1
if d >= k:
count += 1
print(count)
``` | output | 1 | 38,131 | 7 | 76,263 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 3000, 1 ≤ k ≤ min(n, 10)) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows:
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows:
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample. | instruction | 0 | 38,132 | 7 | 76,264 |
Tags: two pointers
Correct Solution:
```
r, c, n, k = map(int, input().split())
g = set([tuple(map(int, input().split())) for _ in range(n)])
ret = 0
for i in range(r):
for j in range(c):
for l in range(1, r-i+1):
for w in range(1, c-j+1):
count = 0
for a in range(i, i+l):
for b in range(j, j+w):
if (a+1, b+1) in g:
count += 1
if count >= k:
ret += 1
print(ret)
``` | output | 1 | 38,132 | 7 | 76,265 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 3000, 1 ≤ k ≤ min(n, 10)) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows:
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows:
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample. | instruction | 0 | 38,133 | 7 | 76,266 |
Tags: two pointers
Correct Solution:
```
import collections
import math
r, c, n, k = map(int, input().split())
T = []
ans = 0
for i in range(n):
x, y = map(int, input().split())
T.append((x, y))
for ri in range (1, r + 1):
for rj in range(ri, r + 1):
for ci in range(1, c + 1):
for cj in range(ci, c + 1):
t = 0
for i in range(len(T)):
if ri <= T[i][0] <= rj and ci <= T[i][1] <= cj:
t += 1
if t >= k:
ans += 1
print(ans)
``` | output | 1 | 38,133 | 7 | 76,267 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 3000, 1 ≤ k ≤ min(n, 10)) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows:
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows:
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample. | instruction | 0 | 38,134 | 7 | 76,268 |
Tags: two pointers
Correct Solution:
```
r,c,n,k = map(int, input().split())
G = [[0]*c for i in range(0,r)]
for i in range(0, n):
a,b = map(int, input().split())
a-=1
b-=1
G[a][b] = True
ans = 0
for x in range(0,r):
for y in range(0,c):
for y1 in range(y + 1, c + 1):
for x1 in range(x + 1, r + 1):
num = 0;
for cl in range(y, y1):
for rw in range(x, x1):
if G[rw][cl]:
num += 1
if num >= k:
ans+=1
print(ans)
``` | output | 1 | 38,134 | 7 | 76,269 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 3000, 1 ≤ k ≤ min(n, 10)) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows:
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows:
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample.
Submitted Solution:
```
r, c, n, k = [int(i) for i in input().split()]
a = [[0] * r for i in range(c)]
for i in range(n):
x, y = [int(i) for i in input().split()]
a[y - 1][x - 1] = 1
ct1 = 0
for i in range(c):
for j in range(r):
for s in range(i + 1, c + 1):
for t in range(j + 1, r + 1):
ct = 0
for u in range(i, s):
for v in range(j, t):
if a[u][v] == 1:
ct += 1
if ct >= k:
ct1 += 1
print(ct1)
``` | instruction | 0 | 38,135 | 7 | 76,270 |
Yes | output | 1 | 38,135 | 7 | 76,271 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 3000, 1 ≤ k ≤ min(n, 10)) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows:
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows:
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample.
Submitted Solution:
```
r, c, n, k = map(int, input().split())
A = [[0] * c for i in range(r)]
for i in range(n):
a, b = map(int, input().split())
A[a - 1][b - 1] = 1
f = 0
g = 0
ans = 0
for i in range(r):
for j in range(c):
for i2 in range(r - i):
for j2 in range(c - j):
cnt = 0
for i3 in range(i, i + i2 + 1):
for j3 in range(j, j + j2 + 1):
#print(i3, j3)
cnt += int(A[i3][j3] == 1)
if cnt >= k:
ans += 1
print(ans)
``` | instruction | 0 | 38,136 | 7 | 76,272 |
Yes | output | 1 | 38,136 | 7 | 76,273 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 3000, 1 ≤ k ≤ min(n, 10)) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows:
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows:
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample.
Submitted Solution:
```
b, a, n, k = map(int, input().split())
data = [[0 for i in range(a)]for j in range(b)]
for i in range(n):
xi, yi = map(int, input().split())
xi -= 1
yi -= 1
data[xi][yi] = 1
answer = 0
for i in range(b):
for j in range(a):
for x in range(i, b):
for y in range(j, a):
counter = 0
for x1 in range(min(i, x), max(i, x) + 1):
for y1 in range(min(j, y), max(j, y) + 1):
counter += data[x1][y1]
if counter >= k:
answer += 1
print(answer)
``` | instruction | 0 | 38,137 | 7 | 76,274 |
Yes | output | 1 | 38,137 | 7 | 76,275 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 3000, 1 ≤ k ≤ min(n, 10)) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows:
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows:
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample.
Submitted Solution:
```
r, c, n, k = map(int, input().split())
a = [tuple(map(int, input().split())) for _ in range(n)]
ans = 0
for x1 in range(1, r + 1):
for x2 in range(x1, r + 1):
for y1 in range(1, c + 1):
for y2 in range(y1, c + 1):
if len([1 for x, y in a if x1 <= x <= x2 and y1 <= y <= y2]) >= k:
ans += 1
print(ans)
``` | instruction | 0 | 38,138 | 7 | 76,276 |
Yes | output | 1 | 38,138 | 7 | 76,277 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 3000, 1 ≤ k ≤ min(n, 10)) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows:
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows:
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample.
Submitted Solution:
```
#!/usr/bin/python3
tmp = input().split(" ");
r = int(tmp[0])
c = int(tmp[1])
n = int(tmp[2])
k = int(tmp[3])
tab = [[0]]
for j in range(1,c+1):
tab[0].append(0)
for i in range(1,r+1):
tab.append([0])
for j in range(1,c+1):
tab[i].append(0)
for i in range(0,n):
tmp = input().split(" ")
tab[ int(tmp[0]) ][ int(tmp[1]) ] = 1
pre = tab
for i in range(1,r+1):
for j in range(1,c+1):
pre[i][j] = pre[i-1][j] + pre[i][j-1] - pre[i-1][j-1] + tab[i][j]
wynik = 0
for i in range(1,r+1):
for j in range(1,c+1):
for ii in range(i,r+1):
for jj in range(j,c+1):
if tab[ii][jj] - tab[i-1][jj] - tab[ii][j-1] - tab[i-1][j-1] >= k:
wynik = wynik + 1
print ( wynik )
``` | instruction | 0 | 38,139 | 7 | 76,278 |
No | output | 1 | 38,139 | 7 | 76,279 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 3000, 1 ≤ k ≤ min(n, 10)) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows:
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows:
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample.
Submitted Solution:
```
r, c, n, k = map(int, input().split(' '))
p = []
def findIn(z, x, y, s):
global n
ret = 0
for i in range(n):
if z <= p[0][0] and y >= p[0][0] and x <= p[0][1] and s >= p[0][1]:
ret = ret + 1
return ret
for i in range(n):
x, y = map(int, input().split(' '))
p.append((x, y))
ans = 0
for z in range(1, c + 1):
for x in range(1, r + 1):
for y in range(z, c + 1):
for s in range(x, r + 1):
if findIn(z, x, y, s) >= k:
ans = ans + 1
print (ans);
``` | instruction | 0 | 38,140 | 7 | 76,280 |
No | output | 1 | 38,140 | 7 | 76,281 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 3000, 1 ≤ k ≤ min(n, 10)) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows:
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows:
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample.
Submitted Solution:
```
#!/usr/bin/env python3
r, c, n, k = [int(x) for x in input().split()]
min_x, min_y, max_x, max_y = r, c, 1, 1
first = True
for i in range(0, n):
x, y = [int(x) for x in input().split()]
if first:
min_x = x
min_y = y
max_x = x
max_y = y
else:
min_x = min(x, min_x)
min_y = min(y, min_y)
max_x = max(x, max_x)
max_y = max(y, max_y)
print(min_x * min_y * (r - max_x + 1) * (c - max_y + 1))
``` | instruction | 0 | 38,141 | 7 | 76,282 |
No | output | 1 | 38,141 | 7 | 76,283 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 3000, 1 ≤ k ≤ min(n, 10)) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows:
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows:
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample.
Submitted Solution:
```
r,c,n,k = map(int,input().split())
t=[]
for s in range(n):
a,b=(map(int,input().split()))
t.append([a-1,b-1])
import math
g= r*c-n
if k==1:
print(r*c)
else:
v=math.ceil(g/k)
if v==1:
print(v)
else:print(v*2)
``` | instruction | 0 | 38,142 | 7 | 76,284 |
No | output | 1 | 38,142 | 7 | 76,285 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with H rows and W columns, where each square is painted black or white.
You are given H strings S_1, S_2, ..., S_H, each of length W. If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is `#`; if that square is painted white, the j-th character in the string S_i is `.`.
Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:
* There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...
Constraints
* 1 \leq H, W \leq 400
* |S_i| = W (1 \leq i \leq H)
* For each i (1 \leq i \leq H), the string S_i consists of characters `#` and `.`.
Input
Input is given from Standard Input in the following format:
H W
S_1
S_2
:
S_H
Output
Print the answer.
Examples
Input
3 3
.#.
..#
#..
Output
10
Input
3 3
.#.
..#
..
Output
10
Input
2 4
....
....
Output
0
Input
4 3
...
Output
6 | instruction | 0 | 38,392 | 7 | 76,784 |
"Correct Solution:
```
from collections import deque
H, W = map(int, input().split())
seen = [[False]*W for _ in range(H)]
dx,dy = [1, 0, -1, 0], [0, 1, 0, -1]
c,S = 0,[]
for _ in range(H):
S.append(input())
for i in range(H):
for j in range(W):
if (S[i][j]=='#') and (not seen[i][j]):
d = deque()
d.append((i, j))
seen[i][j],b,w = True,1,0
while d:
px,py = d.popleft()
for x,y in zip(dx,dy):
nx, ny = px+x, py+y
if nx < 0 or ny < 0 or nx >= H or ny >= W or seen[nx][ny]:
continue
if (S[px][py]=='#' and S[nx][ny] == '.') or (S[px][py]=='.' and S[nx][ny] == '#'):
d.append((nx, ny))
seen[nx][ny] = 1
if S[px][py] == '#':
w += 1
else:
b += 1
c += b*w
print(c)
``` | output | 1 | 38,392 | 7 | 76,785 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with H rows and W columns, where each square is painted black or white.
You are given H strings S_1, S_2, ..., S_H, each of length W. If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is `#`; if that square is painted white, the j-th character in the string S_i is `.`.
Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:
* There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...
Constraints
* 1 \leq H, W \leq 400
* |S_i| = W (1 \leq i \leq H)
* For each i (1 \leq i \leq H), the string S_i consists of characters `#` and `.`.
Input
Input is given from Standard Input in the following format:
H W
S_1
S_2
:
S_H
Output
Print the answer.
Examples
Input
3 3
.#.
..#
#..
Output
10
Input
3 3
.#.
..#
..
Output
10
Input
2 4
....
....
Output
0
Input
4 3
...
Output
6 | instruction | 0 | 38,393 | 7 | 76,786 |
"Correct Solution:
```
from collections import deque
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
def dfs(x, y):
stack = deque()
stack.append((x, y))
num_w = 0
num_b = 0
while stack:
cx, cy = stack.popleft()
cc = S[cy][cx]
for i, j in zip(dx, dy):
nx = i + cx
ny = j + cy
if (0 <= nx < w and 0 <= ny < h) and not visited[ny][nx] and S[ny][nx] != cc:
visited[ny][nx] = True
stack.append((nx, ny))
if cc == ".":
num_w += 1
else:
num_b += 1
else:
return num_w, num_b
h, w = map(int, input().split())
S = [list(input()) for _ in range(h)]
visited = [[False] * w for _ in range(h)]
ans = 0
for i in range(w):
for j in range(h):
if not visited[j][i]:
num_w, num_b = dfs(i, j)
ans += num_b * num_w
print(ans)
``` | output | 1 | 38,393 | 7 | 76,787 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with H rows and W columns, where each square is painted black or white.
You are given H strings S_1, S_2, ..., S_H, each of length W. If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is `#`; if that square is painted white, the j-th character in the string S_i is `.`.
Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:
* There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...
Constraints
* 1 \leq H, W \leq 400
* |S_i| = W (1 \leq i \leq H)
* For each i (1 \leq i \leq H), the string S_i consists of characters `#` and `.`.
Input
Input is given from Standard Input in the following format:
H W
S_1
S_2
:
S_H
Output
Print the answer.
Examples
Input
3 3
.#.
..#
#..
Output
10
Input
3 3
.#.
..#
..
Output
10
Input
2 4
....
....
Output
0
Input
4 3
...
Output
6 | instruction | 0 | 38,394 | 7 | 76,788 |
"Correct Solution:
```
from collections import deque
H, W = [int(_) for _ in input().split()]
S = [input() for _ in range(H)]
move = ((1, 0), (-1, 0), (0, 1), (0, -1))
visited = [[False] * W for _ in range(H)]
def bfs(i, j):
b, w = 0, 0
visited[i][j] = True
que = deque([(i, j)])
while que:
ci, cj = que.popleft()
if S[ci][cj] == '#':
b += 1
else:
w += 1
for di, dj in move:
ni, nj = ci + di, cj + dj
if not (0 <= ni < H and 0 <= nj < W) or visited[ni][nj]:
continue
if S[ci][cj] != S[ni][nj]:
visited[ni][nj] = True
que.append((ni, nj))
return b * w
result = 0
for i in range(H):
for j in range(W):
if visited[i][j]:
continue
result += bfs(i, j)
print(result)
``` | output | 1 | 38,394 | 7 | 76,789 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with H rows and W columns, where each square is painted black or white.
You are given H strings S_1, S_2, ..., S_H, each of length W. If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is `#`; if that square is painted white, the j-th character in the string S_i is `.`.
Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:
* There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...
Constraints
* 1 \leq H, W \leq 400
* |S_i| = W (1 \leq i \leq H)
* For each i (1 \leq i \leq H), the string S_i consists of characters `#` and `.`.
Input
Input is given from Standard Input in the following format:
H W
S_1
S_2
:
S_H
Output
Print the answer.
Examples
Input
3 3
.#.
..#
#..
Output
10
Input
3 3
.#.
..#
..
Output
10
Input
2 4
....
....
Output
0
Input
4 3
...
Output
6 | instruction | 0 | 38,395 | 7 | 76,790 |
"Correct Solution:
```
import itertools
h, w = map(int, input().split())
s = [input() for _ in range(h)]
diffs = [(0, 1), (0, -1), (1, 0), (-1, 0)]
t = [[0] * w for _ in range(h)]
group = 1
for i, j in itertools.product(range(h), range(w)):
if t[i][j] != 0:
continue
stack = [(i, j)]
while stack:
x, y = stack.pop()
t[x][y] = group
for dx, dy in diffs:
nx = x + dx
ny = y + dy
if 0 <= nx < h and 0 <= ny < w and t[nx][ny] == 0 and s[x][y] != s[nx][ny]:
stack.append((nx, ny))
group += 1
d = [[0] * 2 for _ in range(group)]
for i, j in itertools.product(range(h), range(w)):
d[t[i][j]][0 if s[i][j] == '.' else 1] += 1
ans = sum(d[i][0] * d[i][1] for i in range(len(d)))
print(ans)
``` | output | 1 | 38,395 | 7 | 76,791 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with H rows and W columns, where each square is painted black or white.
You are given H strings S_1, S_2, ..., S_H, each of length W. If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is `#`; if that square is painted white, the j-th character in the string S_i is `.`.
Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:
* There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...
Constraints
* 1 \leq H, W \leq 400
* |S_i| = W (1 \leq i \leq H)
* For each i (1 \leq i \leq H), the string S_i consists of characters `#` and `.`.
Input
Input is given from Standard Input in the following format:
H W
S_1
S_2
:
S_H
Output
Print the answer.
Examples
Input
3 3
.#.
..#
#..
Output
10
Input
3 3
.#.
..#
..
Output
10
Input
2 4
....
....
Output
0
Input
4 3
...
Output
6 | instruction | 0 | 38,396 | 7 | 76,792 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(10**6)
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
dx = (0, 0, 1, -1)
dy = (1, -1, 0, 0)
seen = [[False] * w for _ in range(h)]
numb = 0
numw = 0
def dfs(x, y):
global numb, numw
seen[x][y] = True
if s[x][y] == '#':
numb += 1
else:
numw += 1
for di in range(4):
nx = x + dx[di]
ny = y + dy[di]
if (nx < 0 or nx >= h or ny < 0 or ny >= w):
continue
if s[x][y] == s[nx][ny]:
continue
if seen[nx][ny]:
continue
dfs(nx, ny)
ans = 0
for x in range(h):
for y in range(w):
if seen[x][y]:
continue
if s[x][y] == '.':
continue
numb = 0
numw = 0
dfs(x, y)
ans += numb * numw
print(ans)
``` | output | 1 | 38,396 | 7 | 76,793 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with H rows and W columns, where each square is painted black or white.
You are given H strings S_1, S_2, ..., S_H, each of length W. If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is `#`; if that square is painted white, the j-th character in the string S_i is `.`.
Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:
* There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...
Constraints
* 1 \leq H, W \leq 400
* |S_i| = W (1 \leq i \leq H)
* For each i (1 \leq i \leq H), the string S_i consists of characters `#` and `.`.
Input
Input is given from Standard Input in the following format:
H W
S_1
S_2
:
S_H
Output
Print the answer.
Examples
Input
3 3
.#.
..#
#..
Output
10
Input
3 3
.#.
..#
..
Output
10
Input
2 4
....
....
Output
0
Input
4 3
...
Output
6 | instruction | 0 | 38,397 | 7 | 76,794 |
"Correct Solution:
```
from collections import deque
import sys
sys.setrecursionlimit(200000)
#input = sys.stdin.readline
h,w = map(int,input().split())
a = [[j for j in input()] for i in range(h)]
used = [[0]*w for i in range(h)]
def dfs(x,y):
if used[x][y]:
return 0
global go
used[x][y] = 1
if x-1 >=0 and a[x-1][y] != a[x][y]:
go.add((x-1,y))
dfs(x-1,y)
if y-1 >=0 and a[x][y-1] != a[x][y]:
go.add((x,y-1))
dfs(x,y-1)
if x+1 <h and a[x+1][y] != a[x][y]:
go.add((x+1,y))
dfs(x+1,y)
if y+1 <w and a[x][y+1] != a[x][y]:
go.add((x,y+1))
dfs(x,y+1)
ans = 0
for i in range(h):
for j in range(w):
go = set()
if used[i][j]:
continue
dfs(i,j)
kuro = 0
siro = 0
for k,l in go:
if a[k][l] == "#":
kuro += 1
else:
siro += 1
ans += kuro*siro
print(ans)
``` | output | 1 | 38,397 | 7 | 76,795 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with H rows and W columns, where each square is painted black or white.
You are given H strings S_1, S_2, ..., S_H, each of length W. If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is `#`; if that square is painted white, the j-th character in the string S_i is `.`.
Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:
* There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...
Constraints
* 1 \leq H, W \leq 400
* |S_i| = W (1 \leq i \leq H)
* For each i (1 \leq i \leq H), the string S_i consists of characters `#` and `.`.
Input
Input is given from Standard Input in the following format:
H W
S_1
S_2
:
S_H
Output
Print the answer.
Examples
Input
3 3
.#.
..#
#..
Output
10
Input
3 3
.#.
..#
..
Output
10
Input
2 4
....
....
Output
0
Input
4 3
...
Output
6 | instruction | 0 | 38,398 | 7 | 76,796 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(100000)
dxdy = {(-1, 0), (1, 0), (0, -1), (0, 1)}
h,w = map(int,input().split())
s = [str(input()) for i in range(h)]
ch = [[0] * w for i in range(h)]
ans = 0
for i in range(h):
for j in range(w):
if ch[i][j] == 0:
black,white = 0,0
q = [(i,j)]
while q:
y,x = q.pop()
if ch[y][x] == 0:
if s[y][x] == "#":black += 1
else:white += 1
ch[y][x] = 1
for dx,dy in dxdy:
if 0 <= x + dx < w and 0 <= y + dy < h and ch[y+dy][x+dx] == 0 and s[y+dy][x+dx] != s[y][x]:
q.append((y+dy,x+dx))
ans += black * white
print(ans)
``` | output | 1 | 38,398 | 7 | 76,797 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with H rows and W columns, where each square is painted black or white.
You are given H strings S_1, S_2, ..., S_H, each of length W. If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is `#`; if that square is painted white, the j-th character in the string S_i is `.`.
Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:
* There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...
Constraints
* 1 \leq H, W \leq 400
* |S_i| = W (1 \leq i \leq H)
* For each i (1 \leq i \leq H), the string S_i consists of characters `#` and `.`.
Input
Input is given from Standard Input in the following format:
H W
S_1
S_2
:
S_H
Output
Print the answer.
Examples
Input
3 3
.#.
..#
#..
Output
10
Input
3 3
.#.
..#
..
Output
10
Input
2 4
....
....
Output
0
Input
4 3
...
Output
6 | instruction | 0 | 38,399 | 7 | 76,798 |
"Correct Solution:
```
def getminLT(i):
if LT[i] == i:
return i
else:
return getminLT(LT[i])
H, W = list(map(int, input().split()))
D = []
for i in range(H):
D.append(input())
M = [[0] * W for i in range(H)]
LT = [0]
for i in range(H):
for j in range(W):
if i == 0 and j == 0:
continue
flag = False
if i != 0:
if D[i][j] != D[i-1][j]:
M[i][j] = M[i-1][j]
flag = True
if j != 0:
if D[i][j] != D[i][j-1]:
M[i][j] = M[i][j-1]
if flag == True:
LT[max(getminLT(M[i-1][j]), getminLT(M[i][j-1]))] = LT[min(getminLT(M[i][j-1]), getminLT(M[i-1][j]))]
flag = True
if flag == False:
M[i][j] = len(LT)
LT.append(len(LT))
for i in range(len(LT)):
LT[i] = getminLT(LT[i])
L = [[] for i in range(len(LT))]
for i in range(H):
for j in range(W):
L[LT[M[i][j]]].append((i, j))
#print(M)
#print(LT)
#print(L)
cnt = 0
for i in L:
E = 0
A = 0
for j in i:
if (j[0] + j[1]) % 2 == 0:
E += 1
else:
A += 1
cnt += E * A
print(cnt)
``` | output | 1 | 38,399 | 7 | 76,799 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a grid with H rows and W columns, where each square is painted black or white.
You are given H strings S_1, S_2, ..., S_H, each of length W. If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is `#`; if that square is painted white, the j-th character in the string S_i is `.`.
Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:
* There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...
Constraints
* 1 \leq H, W \leq 400
* |S_i| = W (1 \leq i \leq H)
* For each i (1 \leq i \leq H), the string S_i consists of characters `#` and `.`.
Input
Input is given from Standard Input in the following format:
H W
S_1
S_2
:
S_H
Output
Print the answer.
Examples
Input
3 3
.#.
..#
#..
Output
10
Input
3 3
.#.
..#
..
Output
10
Input
2 4
....
....
Output
0
Input
4 3
...
Output
6
Submitted Solution:
```
# aising2019C - Alternating Path
def dfs(sx: int, sy: int) -> None:
color, stack = [0] * 2, [(sx, sy, S[sx][sy])]
S[sx][sy] = 2 # make the grid visited
while stack:
x, y, col = stack.pop()
color[col] += 1
for dx, dy in dxy:
nx, ny = x + dx, y + dy
if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == col ^ 1:
stack.append((nx, ny, S[nx][ny]))
S[nx][ny] = 2 # make the grid visited
return color[0] * color[1]
def main():
# separate S to connected components
global H, W, S, dxy
H, W, *S = open(0).read().split()
H, W = int(H), int(W)
S = [[1 if i == "#" else 0 for i in s] for s in S]
dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
ans = 0
for i in range(H):
for j in range(W):
if S[i][j] != 2:
ans += dfs(i, j)
print(ans)
if __name__ == "__main__":
main()
``` | instruction | 0 | 38,400 | 7 | 76,800 |
Yes | output | 1 | 38,400 | 7 | 76,801 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a grid with H rows and W columns, where each square is painted black or white.
You are given H strings S_1, S_2, ..., S_H, each of length W. If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is `#`; if that square is painted white, the j-th character in the string S_i is `.`.
Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:
* There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...
Constraints
* 1 \leq H, W \leq 400
* |S_i| = W (1 \leq i \leq H)
* For each i (1 \leq i \leq H), the string S_i consists of characters `#` and `.`.
Input
Input is given from Standard Input in the following format:
H W
S_1
S_2
:
S_H
Output
Print the answer.
Examples
Input
3 3
.#.
..#
#..
Output
10
Input
3 3
.#.
..#
..
Output
10
Input
2 4
....
....
Output
0
Input
4 3
...
Output
6
Submitted Solution:
```
h,w = map(int, input().split())
a = [list("*"*(w+2))]
for i in range(h):
a.append(['*'] + list(input()) + ['*'])
a.append(list("*"*(w+2)))
b = [[0]*(w+2) for _ in range(h+2)]
direction = [(1, 0), (-1, 0), (0, 1), (0, -1)]
ans=0
for i in range(1,h+1):
for j in range(1,w+1):
if a[i][j]=='#' and b[i][j]==0:
countw = countb = 0
stackb = [(i, j)]
stackw = []
while stackb:
xb, yb = stackb.pop(-1)
if b[xb][yb] != 0: continue
countb+=1
b[xb][yb]=1
for d in direction:
p, q = xb + d[0], yb + d[1]
if a[p][q]=='.' and b[p][q]==0:
stackw.append((p, q))
while stackw:
xw, yw = stackw.pop(-1)
if b[xw][yw] != 0: continue
countw+=1
b[xw][yw]=1
for d in direction:
p, q = xw + d[0], yw + d[1]
if a[p][q]=='#' and b[p][q]==0:
stackb.append((p, q))
ans += countb*countw
print(ans)
``` | instruction | 0 | 38,401 | 7 | 76,802 |
Yes | output | 1 | 38,401 | 7 | 76,803 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a grid with H rows and W columns, where each square is painted black or white.
You are given H strings S_1, S_2, ..., S_H, each of length W. If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is `#`; if that square is painted white, the j-th character in the string S_i is `.`.
Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:
* There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...
Constraints
* 1 \leq H, W \leq 400
* |S_i| = W (1 \leq i \leq H)
* For each i (1 \leq i \leq H), the string S_i consists of characters `#` and `.`.
Input
Input is given from Standard Input in the following format:
H W
S_1
S_2
:
S_H
Output
Print the answer.
Examples
Input
3 3
.#.
..#
#..
Output
10
Input
3 3
.#.
..#
..
Output
10
Input
2 4
....
....
Output
0
Input
4 3
...
Output
6
Submitted Solution:
```
from collections import deque
H,W=map(int,input().split())
S=[]
for i in range(H):
S.append(input())
q=deque([])
T=[]
seen=[[0]*W for _ in range(H)]
step=[[0,1],[0,-1],[1,0],[-1,0]]
for h in range(H):
for w in range(W):
temp=[]
if seen[h][w]==0:
temp.append(S[h][w])
seen[h][w]=1
q.append([h,w,S[h][w]])
while q:
oh,ow,m=q.popleft()
for dh,dw in step:
if 0<=oh+dh<H and 0<=ow+dw<W:
if seen[oh+dh][ow+dw]==0 and m!=S[oh+dh][ow+dw]:
temp.append(S[oh+dh][ow+dw])
seen[oh+dh][ow+dw]=1
q.append([oh+dh,ow+dw,S[oh+dh][ow+dw]])
T.append(temp)
ans=0
for i in range(len(T)):
black=0
white=0
for s in T[i]:
if s=="#":
black+=1
else:
white+=1
ans+=black*white
print(ans)
``` | instruction | 0 | 38,402 | 7 | 76,804 |
Yes | output | 1 | 38,402 | 7 | 76,805 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a grid with H rows and W columns, where each square is painted black or white.
You are given H strings S_1, S_2, ..., S_H, each of length W. If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is `#`; if that square is painted white, the j-th character in the string S_i is `.`.
Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:
* There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...
Constraints
* 1 \leq H, W \leq 400
* |S_i| = W (1 \leq i \leq H)
* For each i (1 \leq i \leq H), the string S_i consists of characters `#` and `.`.
Input
Input is given from Standard Input in the following format:
H W
S_1
S_2
:
S_H
Output
Print the answer.
Examples
Input
3 3
.#.
..#
#..
Output
10
Input
3 3
.#.
..#
..
Output
10
Input
2 4
....
....
Output
0
Input
4 3
...
Output
6
Submitted Solution:
```
from itertools import chain, product
H,W = map(int,input().split())
grid = list(chain(
([None,]*(W+2),),
(list(chain(
(None,),
map(lambda x: x=='#', input()),
(None,),
)) for _ in range(H)),
([None,]*(W+2),),
))
neighbors = ((1,0),(0,1),(-1,0),(0,-1))
res = 0
stack = []
for y,x in product(range(1,H+1),range(1,W+1)):
if grid[y][x] is None:
continue
stack.append((y,x))
black,white = 0,0
while stack:
y,x = stack.pop()
v = grid[y][x]
if v is None:
continue
grid[y][x] = None
if v:
black += 1
else:
white += 1
v = not v
for dy,dx in neighbors:
w = grid[y+dy][x+dx]
if w == v:
stack.append((y+dy,x+dx))
res += black*white
print(res)
``` | instruction | 0 | 38,403 | 7 | 76,806 |
Yes | output | 1 | 38,403 | 7 | 76,807 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a grid with H rows and W columns, where each square is painted black or white.
You are given H strings S_1, S_2, ..., S_H, each of length W. If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is `#`; if that square is painted white, the j-th character in the string S_i is `.`.
Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:
* There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...
Constraints
* 1 \leq H, W \leq 400
* |S_i| = W (1 \leq i \leq H)
* For each i (1 \leq i \leq H), the string S_i consists of characters `#` and `.`.
Input
Input is given from Standard Input in the following format:
H W
S_1
S_2
:
S_H
Output
Print the answer.
Examples
Input
3 3
.#.
..#
#..
Output
10
Input
3 3
.#.
..#
..
Output
10
Input
2 4
....
....
Output
0
Input
4 3
...
Output
6
Submitted Solution:
```
# https://atcoder.jp/contests/aising2019/tasks/aising2019_c
H, W = map(int, input().split())
M = [list(input()) for _ in range(H)]
seen = [[False] * W for _ in range(H)]
def dfs(h, w, ph, pw, color):
nc = "." if color == "#" else "#"
bc, bw = 0, 0
if color == '#':
bc += 1
if color == '.':
bw += 1
path = []
for (dh, dw) in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
nh, nw = h + dh, w + dw
if nh < 0 or H <= nh or nw < 0 or W <= nw:
continue
if nh == ph and nw == pw:
continue
if seen[nh][nw]:
continue
if M[nh][nw] != nc:
continue
seen[nh][nw] = True
b_add, w_add = dfs(nh, nw, h, w, nc)
bc += b_add
bw += w_add
return bc, bw
ans = 0
for h in range(H):
for w in range(W):
if seen[h][w] == False:
seen[h][w] = True
(b, w) = dfs(h, w, -1, -1, M[h][w])
ans += b * w
print(ans)
``` | instruction | 0 | 38,404 | 7 | 76,808 |
No | output | 1 | 38,404 | 7 | 76,809 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a grid with H rows and W columns, where each square is painted black or white.
You are given H strings S_1, S_2, ..., S_H, each of length W. If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is `#`; if that square is painted white, the j-th character in the string S_i is `.`.
Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:
* There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...
Constraints
* 1 \leq H, W \leq 400
* |S_i| = W (1 \leq i \leq H)
* For each i (1 \leq i \leq H), the string S_i consists of characters `#` and `.`.
Input
Input is given from Standard Input in the following format:
H W
S_1
S_2
:
S_H
Output
Print the answer.
Examples
Input
3 3
.#.
..#
#..
Output
10
Input
3 3
.#.
..#
..
Output
10
Input
2 4
....
....
Output
0
Input
4 3
...
Output
6
Submitted Solution:
```
import numpy as np
from collections import deque
h, w = map(int, input().split())
s = [input() for _ in range(h)]
segment = np.zeros((h, w), dtype = np.int32)
idx = 1
dxy = [[1, 0], [0, 1], [-1, 0], [0, -1]]
for y in range(h):
for x in range(w):
if segment[y, x] == 0:
segment[y, x] = idx
state = s[y][x]
que = deque([[y, x, state]])
while len(que) > 0:
y_cur, x_cur, state = que.popleft()
for dx, dy in dxy:
y_next = y_cur + dy
x_next = x_cur + dx
state_next = '.' if state == '#' else '#'
if x_next < 0 or y_next < 0 or x_next >= w or y_next >= h:
continue
elif s[y_next][x_next] != state_next:
continue
elif segment[y_next, x_next] == 0:
segment[y_next, x_next] = idx
que.append((y_next, x_next, state_next))
idx += 1
# print(segment)
idx_max = np.max(segment)
nbw = [[0, 0] for _ in range(idx_max+1)]
for y in range(h):
for x in range(w):
idx = segment[y, x]
if s[y][x] == '#':
nbw[idx][0] += 1
else:
nbw[idx][1] += 1
ans = 0
for nb, nw in nbw:
ans += nb*nw
print(ans)
``` | instruction | 0 | 38,405 | 7 | 76,810 |
No | output | 1 | 38,405 | 7 | 76,811 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.