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
Provide tags and a correct Python 3 solution for this coding contest problem. You might have remembered Theatre square from the [problem 1A](https://codeforces.com/problemset/problem/1/A). Now it's finally getting repaved. The square still has a rectangular shape of n Γ— m meters. However, the picture is about to get more complicated now. Let a_{i,j} be the j-th square in the i-th row of the pavement. You are given the picture of the squares: * if a_{i,j} = "*", then the j-th square in the i-th row should be black; * if a_{i,j} = ".", then the j-th square in the i-th row should be white. The black squares are paved already. You have to pave the white squares. There are two options for pavement tiles: * 1 Γ— 1 tiles β€” each tile costs x burles and covers exactly 1 square; * 1 Γ— 2 tiles β€” each tile costs y burles and covers exactly 2 adjacent squares of the same row. Note that you are not allowed to rotate these tiles or cut them into 1 Γ— 1 tiles. You should cover all the white squares, no two tiles should overlap and no black squares should be covered by tiles. What is the smallest total price of the tiles needed to cover all the white squares? Input The first line contains a single integer t (1 ≀ t ≀ 500) β€” the number of testcases. Then the description of t testcases follow. The first line of each testcase contains four integers n, m, x and y (1 ≀ n ≀ 100; 1 ≀ m ≀ 1000; 1 ≀ x, y ≀ 1000) β€” the size of the Theatre square, the price of the 1 Γ— 1 tile and the price of the 1 Γ— 2 tile. Each of the next n lines contains m characters. The j-th character in the i-th line is a_{i,j}. If a_{i,j} = "*", then the j-th square in the i-th row should be black, and if a_{i,j} = ".", then the j-th square in the i-th row should be white. It's guaranteed that the sum of n Γ— m over all testcases doesn't exceed 10^5. Output For each testcase print a single integer β€” the smallest total price of the tiles needed to cover all the white squares in burles. Example Input 4 1 1 10 1 . 1 2 10 1 .. 2 1 10 1 . . 3 3 3 7 ..* *.. .*. Output 10 1 20 18 Note In the first testcase you are required to use a single 1 Γ— 1 tile, even though 1 Γ— 2 tile is cheaper. So the total price is 10 burles. In the second testcase you can either use two 1 Γ— 1 tiles and spend 20 burles or use a single 1 Γ— 2 tile and spend 1 burle. The second option is cheaper, thus the answer is 1. The third testcase shows that you can't rotate 1 Γ— 2 tiles. You still have to use two 1 Γ— 1 tiles for the total price of 20. In the fourth testcase the cheapest way is to use 1 Γ— 1 tiles everywhere. The total cost is 6 β‹… 3 = 18.
instruction
0
35,337
7
70,674
Tags: brute force, dp, greedy, implementation, two pointers Correct Solution: ``` from sys import stdin,stdout for query in range(int(stdin.readline())): nmxy=stdin.readline().split() n=int(nmxy[0]) m=int(nmxy[1]) x=int(nmxy[2]) y=int(nmxy[3]) d=[[x for x in list(stdin.readline().strip())] for x in range(n)] if x*2<=y: count=0 for a in range(n): for b in range(m): if d[a][b]=='.': count+=1 stdout.write(str(count*x)+'\n') else: count1=0 count2=0 for a in range(n): e=False for b in range(m): if d[a][b]=='.': if not e: e=True else: count2+=1 e=False else: if e: count1+=1 e=False if e: count1+=1 stdout.write(str(count1*x+count2*y)+'\n') ```
output
1
35,337
7
70,675
Provide tags and a correct Python 3 solution for this coding contest problem. You might have remembered Theatre square from the [problem 1A](https://codeforces.com/problemset/problem/1/A). Now it's finally getting repaved. The square still has a rectangular shape of n Γ— m meters. However, the picture is about to get more complicated now. Let a_{i,j} be the j-th square in the i-th row of the pavement. You are given the picture of the squares: * if a_{i,j} = "*", then the j-th square in the i-th row should be black; * if a_{i,j} = ".", then the j-th square in the i-th row should be white. The black squares are paved already. You have to pave the white squares. There are two options for pavement tiles: * 1 Γ— 1 tiles β€” each tile costs x burles and covers exactly 1 square; * 1 Γ— 2 tiles β€” each tile costs y burles and covers exactly 2 adjacent squares of the same row. Note that you are not allowed to rotate these tiles or cut them into 1 Γ— 1 tiles. You should cover all the white squares, no two tiles should overlap and no black squares should be covered by tiles. What is the smallest total price of the tiles needed to cover all the white squares? Input The first line contains a single integer t (1 ≀ t ≀ 500) β€” the number of testcases. Then the description of t testcases follow. The first line of each testcase contains four integers n, m, x and y (1 ≀ n ≀ 100; 1 ≀ m ≀ 1000; 1 ≀ x, y ≀ 1000) β€” the size of the Theatre square, the price of the 1 Γ— 1 tile and the price of the 1 Γ— 2 tile. Each of the next n lines contains m characters. The j-th character in the i-th line is a_{i,j}. If a_{i,j} = "*", then the j-th square in the i-th row should be black, and if a_{i,j} = ".", then the j-th square in the i-th row should be white. It's guaranteed that the sum of n Γ— m over all testcases doesn't exceed 10^5. Output For each testcase print a single integer β€” the smallest total price of the tiles needed to cover all the white squares in burles. Example Input 4 1 1 10 1 . 1 2 10 1 .. 2 1 10 1 . . 3 3 3 7 ..* *.. .*. Output 10 1 20 18 Note In the first testcase you are required to use a single 1 Γ— 1 tile, even though 1 Γ— 2 tile is cheaper. So the total price is 10 burles. In the second testcase you can either use two 1 Γ— 1 tiles and spend 20 burles or use a single 1 Γ— 2 tile and spend 1 burle. The second option is cheaper, thus the answer is 1. The third testcase shows that you can't rotate 1 Γ— 2 tiles. You still have to use two 1 Γ— 1 tiles for the total price of 20. In the fourth testcase the cheapest way is to use 1 Γ— 1 tiles everywhere. The total cost is 6 β‹… 3 = 18.
instruction
0
35,338
7
70,676
Tags: brute force, dp, greedy, implementation, two pointers Correct Solution: ``` t=int(input()) for _ in range(t): n,m,x,y=map(int,input().split()) a=[] if 2*x<=y: f=1 else: f=0 for i in range(n): a.append(input()) if f==1: count =0 for i in range(n): for j in range(m): if a[i][j]=='.': count+=1 print(x*count) else: s=0 j=0 for i in range(n): j=0 while j<m: if j!=m-1: if a[i][j]=='.' and a[i][j+1]=='.': s+=y j+=1 elif a[i][j]=='.': s+=x elif a[i][j]=='.': s+=x j+=1 print(s) ```
output
1
35,338
7
70,677
Provide tags and a correct Python 3 solution for this coding contest problem. You might have remembered Theatre square from the [problem 1A](https://codeforces.com/problemset/problem/1/A). Now it's finally getting repaved. The square still has a rectangular shape of n Γ— m meters. However, the picture is about to get more complicated now. Let a_{i,j} be the j-th square in the i-th row of the pavement. You are given the picture of the squares: * if a_{i,j} = "*", then the j-th square in the i-th row should be black; * if a_{i,j} = ".", then the j-th square in the i-th row should be white. The black squares are paved already. You have to pave the white squares. There are two options for pavement tiles: * 1 Γ— 1 tiles β€” each tile costs x burles and covers exactly 1 square; * 1 Γ— 2 tiles β€” each tile costs y burles and covers exactly 2 adjacent squares of the same row. Note that you are not allowed to rotate these tiles or cut them into 1 Γ— 1 tiles. You should cover all the white squares, no two tiles should overlap and no black squares should be covered by tiles. What is the smallest total price of the tiles needed to cover all the white squares? Input The first line contains a single integer t (1 ≀ t ≀ 500) β€” the number of testcases. Then the description of t testcases follow. The first line of each testcase contains four integers n, m, x and y (1 ≀ n ≀ 100; 1 ≀ m ≀ 1000; 1 ≀ x, y ≀ 1000) β€” the size of the Theatre square, the price of the 1 Γ— 1 tile and the price of the 1 Γ— 2 tile. Each of the next n lines contains m characters. The j-th character in the i-th line is a_{i,j}. If a_{i,j} = "*", then the j-th square in the i-th row should be black, and if a_{i,j} = ".", then the j-th square in the i-th row should be white. It's guaranteed that the sum of n Γ— m over all testcases doesn't exceed 10^5. Output For each testcase print a single integer β€” the smallest total price of the tiles needed to cover all the white squares in burles. Example Input 4 1 1 10 1 . 1 2 10 1 .. 2 1 10 1 . . 3 3 3 7 ..* *.. .*. Output 10 1 20 18 Note In the first testcase you are required to use a single 1 Γ— 1 tile, even though 1 Γ— 2 tile is cheaper. So the total price is 10 burles. In the second testcase you can either use two 1 Γ— 1 tiles and spend 20 burles or use a single 1 Γ— 2 tile and spend 1 burle. The second option is cheaper, thus the answer is 1. The third testcase shows that you can't rotate 1 Γ— 2 tiles. You still have to use two 1 Γ— 1 tiles for the total price of 20. In the fourth testcase the cheapest way is to use 1 Γ— 1 tiles everywhere. The total cost is 6 β‹… 3 = 18.
instruction
0
35,339
7
70,678
Tags: brute force, dp, greedy, implementation, two pointers Correct Solution: ``` for _ in range(int(input())): r,c,x,y = list(map(int, input().split())) d = 2*x d = 2*x if d<y else y cost=0 for _ in range(r): row = input() count = 0 for i in range(len(row)): if row[i]=='*': cost+=((count//2)*d)+(count%2)*x count=0 else: count+=1 cost+=((count//2)*d)+(count%2)*x print(cost) ```
output
1
35,339
7
70,679
Provide tags and a correct Python 3 solution for this coding contest problem. You might have remembered Theatre square from the [problem 1A](https://codeforces.com/problemset/problem/1/A). Now it's finally getting repaved. The square still has a rectangular shape of n Γ— m meters. However, the picture is about to get more complicated now. Let a_{i,j} be the j-th square in the i-th row of the pavement. You are given the picture of the squares: * if a_{i,j} = "*", then the j-th square in the i-th row should be black; * if a_{i,j} = ".", then the j-th square in the i-th row should be white. The black squares are paved already. You have to pave the white squares. There are two options for pavement tiles: * 1 Γ— 1 tiles β€” each tile costs x burles and covers exactly 1 square; * 1 Γ— 2 tiles β€” each tile costs y burles and covers exactly 2 adjacent squares of the same row. Note that you are not allowed to rotate these tiles or cut them into 1 Γ— 1 tiles. You should cover all the white squares, no two tiles should overlap and no black squares should be covered by tiles. What is the smallest total price of the tiles needed to cover all the white squares? Input The first line contains a single integer t (1 ≀ t ≀ 500) β€” the number of testcases. Then the description of t testcases follow. The first line of each testcase contains four integers n, m, x and y (1 ≀ n ≀ 100; 1 ≀ m ≀ 1000; 1 ≀ x, y ≀ 1000) β€” the size of the Theatre square, the price of the 1 Γ— 1 tile and the price of the 1 Γ— 2 tile. Each of the next n lines contains m characters. The j-th character in the i-th line is a_{i,j}. If a_{i,j} = "*", then the j-th square in the i-th row should be black, and if a_{i,j} = ".", then the j-th square in the i-th row should be white. It's guaranteed that the sum of n Γ— m over all testcases doesn't exceed 10^5. Output For each testcase print a single integer β€” the smallest total price of the tiles needed to cover all the white squares in burles. Example Input 4 1 1 10 1 . 1 2 10 1 .. 2 1 10 1 . . 3 3 3 7 ..* *.. .*. Output 10 1 20 18 Note In the first testcase you are required to use a single 1 Γ— 1 tile, even though 1 Γ— 2 tile is cheaper. So the total price is 10 burles. In the second testcase you can either use two 1 Γ— 1 tiles and spend 20 burles or use a single 1 Γ— 2 tile and spend 1 burle. The second option is cheaper, thus the answer is 1. The third testcase shows that you can't rotate 1 Γ— 2 tiles. You still have to use two 1 Γ— 1 tiles for the total price of 20. In the fourth testcase the cheapest way is to use 1 Γ— 1 tiles everywhere. The total cost is 6 β‹… 3 = 18.
instruction
0
35,340
7
70,680
Tags: brute force, dp, greedy, implementation, two pointers Correct Solution: ``` import os, sys from io import IOBase, BytesIO py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange BUFSIZE = 8192 class FastIO(BytesIO): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.writable = 'x' in file.mode or 'w' in file.mode self.write = super(FastIO, self).write if self.writable else None def _fill(self): s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0]) return s def read(self): while self._fill(): pass return super(FastIO,self).read() def readline(self): while self.newlines == 0: s = self._fill(); self.newlines = s.count(b'\n') + (not s) self.newlines -= 1 return super(FastIO, self).readline() def flush(self): if self.writable: os.write(self._fd, self.getvalue()) self.truncate(0), self.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable if py2: self.write = self.buffer.write self.read = self.buffer.read self.readline = self.buffer.readline else: self.write = lambda s:self.buffer.write(s.encode('ascii')) self.read = lambda:self.buffer.read().decode('ascii') self.readline = lambda:self.buffer.readline().decode('ascii') sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip('\r\n') # Cout implemented in Python import sys class ostream: def __lshift__(self,a): sys.stdout.write(str(a)) return self cout = ostream() endl = '\n' def get_input(a=str): return a(input()) def get_int_input(): return get_input(int) def get_input_arr(a): return list(map(a, input().split())) def get_int_input_arr(): return get_input_arr(int) def solve(): t = get_int_input() for _ in range(t): n, m, c1, c2 = get_int_input_arr() grid = [] for _ in range(n): grid.append(get_input()) res = 0 for i in range(n): paths = grid[i].split("*") for path in paths: if len(path) == 1: res += c1 if len(path) > 1: if c1 * 2 < c2: res += c1 * len(path) else: p2 = len(path) // 2 p_mod = len(path) % 2 res += ((c1 * p_mod) + (c2 * p2)) cout<<res<<endl def main(): solve() if __name__ == "__main__": main() ```
output
1
35,340
7
70,681
Provide tags and a correct Python 3 solution for this coding contest problem. You might have remembered Theatre square from the [problem 1A](https://codeforces.com/problemset/problem/1/A). Now it's finally getting repaved. The square still has a rectangular shape of n Γ— m meters. However, the picture is about to get more complicated now. Let a_{i,j} be the j-th square in the i-th row of the pavement. You are given the picture of the squares: * if a_{i,j} = "*", then the j-th square in the i-th row should be black; * if a_{i,j} = ".", then the j-th square in the i-th row should be white. The black squares are paved already. You have to pave the white squares. There are two options for pavement tiles: * 1 Γ— 1 tiles β€” each tile costs x burles and covers exactly 1 square; * 1 Γ— 2 tiles β€” each tile costs y burles and covers exactly 2 adjacent squares of the same row. Note that you are not allowed to rotate these tiles or cut them into 1 Γ— 1 tiles. You should cover all the white squares, no two tiles should overlap and no black squares should be covered by tiles. What is the smallest total price of the tiles needed to cover all the white squares? Input The first line contains a single integer t (1 ≀ t ≀ 500) β€” the number of testcases. Then the description of t testcases follow. The first line of each testcase contains four integers n, m, x and y (1 ≀ n ≀ 100; 1 ≀ m ≀ 1000; 1 ≀ x, y ≀ 1000) β€” the size of the Theatre square, the price of the 1 Γ— 1 tile and the price of the 1 Γ— 2 tile. Each of the next n lines contains m characters. The j-th character in the i-th line is a_{i,j}. If a_{i,j} = "*", then the j-th square in the i-th row should be black, and if a_{i,j} = ".", then the j-th square in the i-th row should be white. It's guaranteed that the sum of n Γ— m over all testcases doesn't exceed 10^5. Output For each testcase print a single integer β€” the smallest total price of the tiles needed to cover all the white squares in burles. Example Input 4 1 1 10 1 . 1 2 10 1 .. 2 1 10 1 . . 3 3 3 7 ..* *.. .*. Output 10 1 20 18 Note In the first testcase you are required to use a single 1 Γ— 1 tile, even though 1 Γ— 2 tile is cheaper. So the total price is 10 burles. In the second testcase you can either use two 1 Γ— 1 tiles and spend 20 burles or use a single 1 Γ— 2 tile and spend 1 burle. The second option is cheaper, thus the answer is 1. The third testcase shows that you can't rotate 1 Γ— 2 tiles. You still have to use two 1 Γ— 1 tiles for the total price of 20. In the fourth testcase the cheapest way is to use 1 Γ— 1 tiles everywhere. The total cost is 6 β‹… 3 = 18.
instruction
0
35,341
7
70,682
Tags: brute force, dp, greedy, implementation, two pointers Correct Solution: ``` t = int(input()) while t: n,m,x,y = map(int,input().split()) cost = 0 for i in range(n): s = input() j = 0 while j<m: if s[j]=='.': if j+1<m and s[j+1]=='.': cost+=min(2*x,y) j+=2 else: cost+=x j+=1 else: j+=1 # print('cost',cost,i,j) print(cost) t-=1 ```
output
1
35,341
7
70,683
Provide tags and a correct Python 3 solution for this coding contest problem. You might have remembered Theatre square from the [problem 1A](https://codeforces.com/problemset/problem/1/A). Now it's finally getting repaved. The square still has a rectangular shape of n Γ— m meters. However, the picture is about to get more complicated now. Let a_{i,j} be the j-th square in the i-th row of the pavement. You are given the picture of the squares: * if a_{i,j} = "*", then the j-th square in the i-th row should be black; * if a_{i,j} = ".", then the j-th square in the i-th row should be white. The black squares are paved already. You have to pave the white squares. There are two options for pavement tiles: * 1 Γ— 1 tiles β€” each tile costs x burles and covers exactly 1 square; * 1 Γ— 2 tiles β€” each tile costs y burles and covers exactly 2 adjacent squares of the same row. Note that you are not allowed to rotate these tiles or cut them into 1 Γ— 1 tiles. You should cover all the white squares, no two tiles should overlap and no black squares should be covered by tiles. What is the smallest total price of the tiles needed to cover all the white squares? Input The first line contains a single integer t (1 ≀ t ≀ 500) β€” the number of testcases. Then the description of t testcases follow. The first line of each testcase contains four integers n, m, x and y (1 ≀ n ≀ 100; 1 ≀ m ≀ 1000; 1 ≀ x, y ≀ 1000) β€” the size of the Theatre square, the price of the 1 Γ— 1 tile and the price of the 1 Γ— 2 tile. Each of the next n lines contains m characters. The j-th character in the i-th line is a_{i,j}. If a_{i,j} = "*", then the j-th square in the i-th row should be black, and if a_{i,j} = ".", then the j-th square in the i-th row should be white. It's guaranteed that the sum of n Γ— m over all testcases doesn't exceed 10^5. Output For each testcase print a single integer β€” the smallest total price of the tiles needed to cover all the white squares in burles. Example Input 4 1 1 10 1 . 1 2 10 1 .. 2 1 10 1 . . 3 3 3 7 ..* *.. .*. Output 10 1 20 18 Note In the first testcase you are required to use a single 1 Γ— 1 tile, even though 1 Γ— 2 tile is cheaper. So the total price is 10 burles. In the second testcase you can either use two 1 Γ— 1 tiles and spend 20 burles or use a single 1 Γ— 2 tile and spend 1 burle. The second option is cheaper, thus the answer is 1. The third testcase shows that you can't rotate 1 Γ— 2 tiles. You still have to use two 1 Γ— 1 tiles for the total price of 20. In the fourth testcase the cheapest way is to use 1 Γ— 1 tiles everywhere. The total cost is 6 β‹… 3 = 18.
instruction
0
35,342
7
70,684
Tags: brute force, dp, greedy, implementation, two pointers Correct Solution: ``` x = int(input()) for i in range(x): inp = list(map(int, input().split())) n = inp[0] m = inp[1] x = inp[2] y = inp[3] if 2*x < y: ans = 0 for p in range(n): string = input() for char in string: if char == ".": ans += x print(ans) else: ans = 0 for p in range(n): string = input() indx = 0 while indx < (m-1): if(string[indx] == "." and string[indx+1] == "."): ans += y indx += 2 continue elif(string[indx] == "." and string[indx+1] == "*"): ans += x indx += 2 continue indx += 1 if indx == (m-1): if(string[indx] == "."): ans += x print(ans) ```
output
1
35,342
7
70,685
Provide tags and a correct Python 3 solution for this coding contest problem. You might have remembered Theatre square from the [problem 1A](https://codeforces.com/problemset/problem/1/A). Now it's finally getting repaved. The square still has a rectangular shape of n Γ— m meters. However, the picture is about to get more complicated now. Let a_{i,j} be the j-th square in the i-th row of the pavement. You are given the picture of the squares: * if a_{i,j} = "*", then the j-th square in the i-th row should be black; * if a_{i,j} = ".", then the j-th square in the i-th row should be white. The black squares are paved already. You have to pave the white squares. There are two options for pavement tiles: * 1 Γ— 1 tiles β€” each tile costs x burles and covers exactly 1 square; * 1 Γ— 2 tiles β€” each tile costs y burles and covers exactly 2 adjacent squares of the same row. Note that you are not allowed to rotate these tiles or cut them into 1 Γ— 1 tiles. You should cover all the white squares, no two tiles should overlap and no black squares should be covered by tiles. What is the smallest total price of the tiles needed to cover all the white squares? Input The first line contains a single integer t (1 ≀ t ≀ 500) β€” the number of testcases. Then the description of t testcases follow. The first line of each testcase contains four integers n, m, x and y (1 ≀ n ≀ 100; 1 ≀ m ≀ 1000; 1 ≀ x, y ≀ 1000) β€” the size of the Theatre square, the price of the 1 Γ— 1 tile and the price of the 1 Γ— 2 tile. Each of the next n lines contains m characters. The j-th character in the i-th line is a_{i,j}. If a_{i,j} = "*", then the j-th square in the i-th row should be black, and if a_{i,j} = ".", then the j-th square in the i-th row should be white. It's guaranteed that the sum of n Γ— m over all testcases doesn't exceed 10^5. Output For each testcase print a single integer β€” the smallest total price of the tiles needed to cover all the white squares in burles. Example Input 4 1 1 10 1 . 1 2 10 1 .. 2 1 10 1 . . 3 3 3 7 ..* *.. .*. Output 10 1 20 18 Note In the first testcase you are required to use a single 1 Γ— 1 tile, even though 1 Γ— 2 tile is cheaper. So the total price is 10 burles. In the second testcase you can either use two 1 Γ— 1 tiles and spend 20 burles or use a single 1 Γ— 2 tile and spend 1 burle. The second option is cheaper, thus the answer is 1. The third testcase shows that you can't rotate 1 Γ— 2 tiles. You still have to use two 1 Γ— 1 tiles for the total price of 20. In the fourth testcase the cheapest way is to use 1 Γ— 1 tiles everywhere. The total cost is 6 β‹… 3 = 18.
instruction
0
35,343
7
70,686
Tags: brute force, dp, greedy, implementation, two pointers Correct Solution: ``` import math for i in range(int(input())): n,m,x,y=map(int,input().split()) a=[ list(input()) for i in range(n)] c=0 if 2*x<=y: c=0 else: c=1 ans=0 if c==0: for i in range(n): for j in range(m): if a[i][j]==".": ans+=x else: for i in range(n): for j in range(m-1): if a[i][j]=="." and a[i][j+1]==".": a[i][j]="*" a[i][j+1]="*" ans+=y for i in range(n): for j in range(m): if a[i][j]==".": ans+=x print(ans) ```
output
1
35,343
7
70,687
Provide tags and a correct Python 2 solution for this coding contest problem. You might have remembered Theatre square from the [problem 1A](https://codeforces.com/problemset/problem/1/A). Now it's finally getting repaved. The square still has a rectangular shape of n Γ— m meters. However, the picture is about to get more complicated now. Let a_{i,j} be the j-th square in the i-th row of the pavement. You are given the picture of the squares: * if a_{i,j} = "*", then the j-th square in the i-th row should be black; * if a_{i,j} = ".", then the j-th square in the i-th row should be white. The black squares are paved already. You have to pave the white squares. There are two options for pavement tiles: * 1 Γ— 1 tiles β€” each tile costs x burles and covers exactly 1 square; * 1 Γ— 2 tiles β€” each tile costs y burles and covers exactly 2 adjacent squares of the same row. Note that you are not allowed to rotate these tiles or cut them into 1 Γ— 1 tiles. You should cover all the white squares, no two tiles should overlap and no black squares should be covered by tiles. What is the smallest total price of the tiles needed to cover all the white squares? Input The first line contains a single integer t (1 ≀ t ≀ 500) β€” the number of testcases. Then the description of t testcases follow. The first line of each testcase contains four integers n, m, x and y (1 ≀ n ≀ 100; 1 ≀ m ≀ 1000; 1 ≀ x, y ≀ 1000) β€” the size of the Theatre square, the price of the 1 Γ— 1 tile and the price of the 1 Γ— 2 tile. Each of the next n lines contains m characters. The j-th character in the i-th line is a_{i,j}. If a_{i,j} = "*", then the j-th square in the i-th row should be black, and if a_{i,j} = ".", then the j-th square in the i-th row should be white. It's guaranteed that the sum of n Γ— m over all testcases doesn't exceed 10^5. Output For each testcase print a single integer β€” the smallest total price of the tiles needed to cover all the white squares in burles. Example Input 4 1 1 10 1 . 1 2 10 1 .. 2 1 10 1 . . 3 3 3 7 ..* *.. .*. Output 10 1 20 18 Note In the first testcase you are required to use a single 1 Γ— 1 tile, even though 1 Γ— 2 tile is cheaper. So the total price is 10 burles. In the second testcase you can either use two 1 Γ— 1 tiles and spend 20 burles or use a single 1 Γ— 2 tile and spend 1 burle. The second option is cheaper, thus the answer is 1. The third testcase shows that you can't rotate 1 Γ— 2 tiles. You still have to use two 1 Γ— 1 tiles for the total price of 20. In the fourth testcase the cheapest way is to use 1 Γ— 1 tiles everywhere. The total cost is 6 β‹… 3 = 18.
instruction
0
35,344
7
70,688
Tags: brute force, dp, greedy, implementation, two pointers Correct Solution: ``` from sys import stdin, stdout from collections import Counter, defaultdict from itertools import permutations, combinations raw_input = stdin.readline pr = stdout.write mod=10**9+7 def ni(): return int(raw_input()) def li(): return map(int,raw_input().split()) def pn(n): stdout.write(str(n)+'\n') def pa(arr): pr(' '.join(map(str,arr))+'\n') # fast read function for total integer input def inp(): # this function returns whole input of # space/line seperated integers # Use Ctrl+D to flush stdin. return map(int,stdin.read().split()) range = xrange # not for python 3.0+ # main code for t in range(ni()): n,m,x,y=li() y=min(y,2*x) ans=0 for i in range(n): c=0 x1=raw_input().strip() for j in x1: if j=='*': if c: ans+=x c=0 else: if c: ans+=y c=0 else: c=1 if c: ans+=x pn(ans) ```
output
1
35,344
7
70,689
Provide a correct Python 3 solution for this coding contest problem. There is a square-shaped grid with N vertical rows and N horizontal columns. We will denote the square at the i-th row from the top and the j-th column from the left as (i,\ j). Initially, each square is either white or black. The initial color of the grid is given to you as characters a_{ij}, arranged in a square shape. If the square (i,\ j) is white, a_{ij} is `.`. If it is black, a_{ij} is `#`. You are developing a robot that repaints the grid. It can repeatedly perform the following operation: * Select two integers i, j (1 ≀ i,\ j ≀ N). Memorize the colors of the squares (i,\ 1), (i,\ 2), ..., (i,\ N) as c_1, c_2, ..., c_N, respectively. Then, repaint the squares (1,\ j), (2,\ j), ..., (N,\ j) with the colors c_1, c_2, ..., c_N, respectively. Your objective is to turn all the squares black. Determine whether it is possible, and find the minimum necessary number of operations to achieve it if the answer is positive. Constraints * 2 ≀ N ≀ 500 * a_{ij} is either `.` or `#`. Input The input is given from Standard Input in the following format: N a_{11}...a_{1N} : a_{N1}...a_{NN} Output If it is possible to turn all the squares black, print the minimum necessary number of operations to achieve the objective. If it is impossible, print `-1` instead. Examples Input 2 #. .# Output 3 Input 2 . .# Output 3 Input 2 .. .. Output -1 Input 2 Output 0 Input 3 .#. .#. Output 2 Input 3 ... .#. ... Output 5
instruction
0
35,873
7
71,746
"Correct Solution: ``` n=int(input());a=eval('list(input()),'*n);f=sum(n==t.count('#')for t in zip(*a));print(-all(t.count('#')<1for t in a)or min(n*2-f-a[i].count('#')+all(a[j][i]>'#'for j in range(n))for i in range(n))) ```
output
1
35,873
7
71,747
Provide a correct Python 3 solution for this coding contest problem. There is a square-shaped grid with N vertical rows and N horizontal columns. We will denote the square at the i-th row from the top and the j-th column from the left as (i,\ j). Initially, each square is either white or black. The initial color of the grid is given to you as characters a_{ij}, arranged in a square shape. If the square (i,\ j) is white, a_{ij} is `.`. If it is black, a_{ij} is `#`. You are developing a robot that repaints the grid. It can repeatedly perform the following operation: * Select two integers i, j (1 ≀ i,\ j ≀ N). Memorize the colors of the squares (i,\ 1), (i,\ 2), ..., (i,\ N) as c_1, c_2, ..., c_N, respectively. Then, repaint the squares (1,\ j), (2,\ j), ..., (N,\ j) with the colors c_1, c_2, ..., c_N, respectively. Your objective is to turn all the squares black. Determine whether it is possible, and find the minimum necessary number of operations to achieve it if the answer is positive. Constraints * 2 ≀ N ≀ 500 * a_{ij} is either `.` or `#`. Input The input is given from Standard Input in the following format: N a_{11}...a_{1N} : a_{N1}...a_{NN} Output If it is possible to turn all the squares black, print the minimum necessary number of operations to achieve the objective. If it is impossible, print `-1` instead. Examples Input 2 #. .# Output 3 Input 2 . .# Output 3 Input 2 .. .. Output -1 Input 2 Output 0 Input 3 .#. .#. Output 2 Input 3 ... .#. ... Output 5
instruction
0
35,874
7
71,748
"Correct Solution: ``` N=int(input()) a=["" for i in range(N)] data=[0]*N for i in range(N): a[i]=input() data[i]=a[i].count("#") if sum(data)==0: print(-1) elif sum(data)==N**2: print(0) else: data2=[0 for i in range(N)] for i in range(N): for j in range(N): data2[j]+=(a[i][j]=="#") count=0 for i in range(N): if data2[i]!=N: count+=1 ans=10**10 for i in range(N): test=(N-data[i])+count+(data2[i]==0) ans=min(ans,test) print(ans) ```
output
1
35,874
7
71,749
Provide a correct Python 3 solution for this coding contest problem. There is a square-shaped grid with N vertical rows and N horizontal columns. We will denote the square at the i-th row from the top and the j-th column from the left as (i,\ j). Initially, each square is either white or black. The initial color of the grid is given to you as characters a_{ij}, arranged in a square shape. If the square (i,\ j) is white, a_{ij} is `.`. If it is black, a_{ij} is `#`. You are developing a robot that repaints the grid. It can repeatedly perform the following operation: * Select two integers i, j (1 ≀ i,\ j ≀ N). Memorize the colors of the squares (i,\ 1), (i,\ 2), ..., (i,\ N) as c_1, c_2, ..., c_N, respectively. Then, repaint the squares (1,\ j), (2,\ j), ..., (N,\ j) with the colors c_1, c_2, ..., c_N, respectively. Your objective is to turn all the squares black. Determine whether it is possible, and find the minimum necessary number of operations to achieve it if the answer is positive. Constraints * 2 ≀ N ≀ 500 * a_{ij} is either `.` or `#`. Input The input is given from Standard Input in the following format: N a_{11}...a_{1N} : a_{N1}...a_{NN} Output If it is possible to turn all the squares black, print the minimum necessary number of operations to achieve the objective. If it is impossible, print `-1` instead. Examples Input 2 #. .# Output 3 Input 2 . .# Output 3 Input 2 .. .. Output -1 Input 2 Output 0 Input 3 .#. .#. Output 2 Input 3 ... .#. ... Output 5
instruction
0
35,875
7
71,750
"Correct Solution: ``` n,*a=open(0).readlines();n=int(n);*b,=zip(*a);f,e=[sum((c in t)^1for t in b)for c in'.#'];print(-(e>n)or min(n*2-f-a[i].count('#')+2-('#'in b[i])for i in range(n))) ```
output
1
35,875
7
71,751
Provide a correct Python 3 solution for this coding contest problem. There is a square-shaped grid with N vertical rows and N horizontal columns. We will denote the square at the i-th row from the top and the j-th column from the left as (i,\ j). Initially, each square is either white or black. The initial color of the grid is given to you as characters a_{ij}, arranged in a square shape. If the square (i,\ j) is white, a_{ij} is `.`. If it is black, a_{ij} is `#`. You are developing a robot that repaints the grid. It can repeatedly perform the following operation: * Select two integers i, j (1 ≀ i,\ j ≀ N). Memorize the colors of the squares (i,\ 1), (i,\ 2), ..., (i,\ N) as c_1, c_2, ..., c_N, respectively. Then, repaint the squares (1,\ j), (2,\ j), ..., (N,\ j) with the colors c_1, c_2, ..., c_N, respectively. Your objective is to turn all the squares black. Determine whether it is possible, and find the minimum necessary number of operations to achieve it if the answer is positive. Constraints * 2 ≀ N ≀ 500 * a_{ij} is either `.` or `#`. Input The input is given from Standard Input in the following format: N a_{11}...a_{1N} : a_{N1}...a_{NN} Output If it is possible to turn all the squares black, print the minimum necessary number of operations to achieve the objective. If it is impossible, print `-1` instead. Examples Input 2 #. .# Output 3 Input 2 . .# Output 3 Input 2 .. .. Output -1 Input 2 Output 0 Input 3 .#. .#. Output 2 Input 3 ... .#. ... Output 5
instruction
0
35,876
7
71,752
"Correct Solution: ``` n,*a=open(0).readlines();n=int(n);*b,=zip(*a);f,e=[sum((c in t)^1for t in b)-1for c in'.#'];print(-(e==n)or min(n*2-f-a[i].count('#')+(not'#'in b[i])for i in range(n))) ```
output
1
35,876
7
71,753
Provide a correct Python 3 solution for this coding contest problem. There is a square-shaped grid with N vertical rows and N horizontal columns. We will denote the square at the i-th row from the top and the j-th column from the left as (i,\ j). Initially, each square is either white or black. The initial color of the grid is given to you as characters a_{ij}, arranged in a square shape. If the square (i,\ j) is white, a_{ij} is `.`. If it is black, a_{ij} is `#`. You are developing a robot that repaints the grid. It can repeatedly perform the following operation: * Select two integers i, j (1 ≀ i,\ j ≀ N). Memorize the colors of the squares (i,\ 1), (i,\ 2), ..., (i,\ N) as c_1, c_2, ..., c_N, respectively. Then, repaint the squares (1,\ j), (2,\ j), ..., (N,\ j) with the colors c_1, c_2, ..., c_N, respectively. Your objective is to turn all the squares black. Determine whether it is possible, and find the minimum necessary number of operations to achieve it if the answer is positive. Constraints * 2 ≀ N ≀ 500 * a_{ij} is either `.` or `#`. Input The input is given from Standard Input in the following format: N a_{11}...a_{1N} : a_{N1}...a_{NN} Output If it is possible to turn all the squares black, print the minimum necessary number of operations to achieve the objective. If it is impossible, print `-1` instead. Examples Input 2 #. .# Output 3 Input 2 . .# Output 3 Input 2 .. .. Output -1 Input 2 Output 0 Input 3 .#. .#. Output 2 Input 3 ... .#. ... Output 5
instruction
0
35,877
7
71,754
"Correct Solution: ``` import sys N = int(input()) a = [] ybl = 0 tbl = 0 for i in range(N): A = list(input()) if "." not in A: ybl += 1 a.append(A) blexist = False for j in range(N): blnum = 0 for i in range(N): if a[i][j] == "#": blexist = True blnum += 1 if blnum == N: tbl += 1 #print (blexist,tbl,ybl) if not blexist: print (-1) sys.exit() if ybl != 0: print (N - tbl) sys.exit() ans = float("inf") for i in range(N): bexi = False for j in range(N): if a[j][i] == "#": bexi = True break chnum = 0 for j in range(N): if a[i][j] == ".": chnum += 1 if bexi: ans = min(ans,chnum + (N - tbl)) else: ans = min(ans,chnum + 1 + (N - tbl)) print (ans) ```
output
1
35,877
7
71,755
Provide a correct Python 3 solution for this coding contest problem. There is a square-shaped grid with N vertical rows and N horizontal columns. We will denote the square at the i-th row from the top and the j-th column from the left as (i,\ j). Initially, each square is either white or black. The initial color of the grid is given to you as characters a_{ij}, arranged in a square shape. If the square (i,\ j) is white, a_{ij} is `.`. If it is black, a_{ij} is `#`. You are developing a robot that repaints the grid. It can repeatedly perform the following operation: * Select two integers i, j (1 ≀ i,\ j ≀ N). Memorize the colors of the squares (i,\ 1), (i,\ 2), ..., (i,\ N) as c_1, c_2, ..., c_N, respectively. Then, repaint the squares (1,\ j), (2,\ j), ..., (N,\ j) with the colors c_1, c_2, ..., c_N, respectively. Your objective is to turn all the squares black. Determine whether it is possible, and find the minimum necessary number of operations to achieve it if the answer is positive. Constraints * 2 ≀ N ≀ 500 * a_{ij} is either `.` or `#`. Input The input is given from Standard Input in the following format: N a_{11}...a_{1N} : a_{N1}...a_{NN} Output If it is possible to turn all the squares black, print the minimum necessary number of operations to achieve the objective. If it is impossible, print `-1` instead. Examples Input 2 #. .# Output 3 Input 2 . .# Output 3 Input 2 .. .. Output -1 Input 2 Output 0 Input 3 .#. .#. Output 2 Input 3 ... .#. ... Output 5
instruction
0
35,878
7
71,756
"Correct Solution: ``` n,*a=open(0).readlines();n=int(n);*b,=zip(*a);f,e=[sum(n==t.count(c)for t in b)for c in'#.'];print(-(e==n)or min(n*2-f-a[i].count('#')+(not'#'in b[i])for i in range(n))) ```
output
1
35,878
7
71,757
Provide a correct Python 3 solution for this coding contest problem. There is a square-shaped grid with N vertical rows and N horizontal columns. We will denote the square at the i-th row from the top and the j-th column from the left as (i,\ j). Initially, each square is either white or black. The initial color of the grid is given to you as characters a_{ij}, arranged in a square shape. If the square (i,\ j) is white, a_{ij} is `.`. If it is black, a_{ij} is `#`. You are developing a robot that repaints the grid. It can repeatedly perform the following operation: * Select two integers i, j (1 ≀ i,\ j ≀ N). Memorize the colors of the squares (i,\ 1), (i,\ 2), ..., (i,\ N) as c_1, c_2, ..., c_N, respectively. Then, repaint the squares (1,\ j), (2,\ j), ..., (N,\ j) with the colors c_1, c_2, ..., c_N, respectively. Your objective is to turn all the squares black. Determine whether it is possible, and find the minimum necessary number of operations to achieve it if the answer is positive. Constraints * 2 ≀ N ≀ 500 * a_{ij} is either `.` or `#`. Input The input is given from Standard Input in the following format: N a_{11}...a_{1N} : a_{N1}...a_{NN} Output If it is possible to turn all the squares black, print the minimum necessary number of operations to achieve the objective. If it is impossible, print `-1` instead. Examples Input 2 #. .# Output 3 Input 2 . .# Output 3 Input 2 .. .. Output -1 Input 2 Output 0 Input 3 .#. .#. Output 2 Input 3 ... .#. ... Output 5
instruction
0
35,879
7
71,758
"Correct Solution: ``` n=int(input()) a=[list(input()) for i in range(n)] if max([a[i].count("#") for i in range(n)]) ==0: print(-1) exit() minus=0 b=list(zip(*a)) for i in range(n): if b[i].count("#")>=1: minus=max(minus,a[i].count("#")) else: minus=max(minus,a[i].count("#")-1) for j in range(n): if b[j].count(".")==0: minus+=1 print(2*n-minus) ```
output
1
35,879
7
71,759
Provide a correct Python 3 solution for this coding contest problem. There is a square-shaped grid with N vertical rows and N horizontal columns. We will denote the square at the i-th row from the top and the j-th column from the left as (i,\ j). Initially, each square is either white or black. The initial color of the grid is given to you as characters a_{ij}, arranged in a square shape. If the square (i,\ j) is white, a_{ij} is `.`. If it is black, a_{ij} is `#`. You are developing a robot that repaints the grid. It can repeatedly perform the following operation: * Select two integers i, j (1 ≀ i,\ j ≀ N). Memorize the colors of the squares (i,\ 1), (i,\ 2), ..., (i,\ N) as c_1, c_2, ..., c_N, respectively. Then, repaint the squares (1,\ j), (2,\ j), ..., (N,\ j) with the colors c_1, c_2, ..., c_N, respectively. Your objective is to turn all the squares black. Determine whether it is possible, and find the minimum necessary number of operations to achieve it if the answer is positive. Constraints * 2 ≀ N ≀ 500 * a_{ij} is either `.` or `#`. Input The input is given from Standard Input in the following format: N a_{11}...a_{1N} : a_{N1}...a_{NN} Output If it is possible to turn all the squares black, print the minimum necessary number of operations to achieve the objective. If it is impossible, print `-1` instead. Examples Input 2 #. .# Output 3 Input 2 . .# Output 3 Input 2 .. .. Output -1 Input 2 Output 0 Input 3 .#. .#. Output 2 Input 3 ... .#. ... Output 5
instruction
0
35,880
7
71,760
"Correct Solution: ``` from operator import itemgetter n=int(input()) a=[list(map(lambda x:x=='#',input())) for i in range(n)] cr,cc=list(map(sum,a)),[sum(map(itemgetter(i),a)) for i in range(n)] mx=max(cr) print(-1 if mx==0 else (not sum([cc[i] for i in range(n) if cr[i]==mx]))+n-mx+sum(map(lambda x:x<n,cc))) ```
output
1
35,880
7
71,761
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a square-shaped grid with N vertical rows and N horizontal columns. We will denote the square at the i-th row from the top and the j-th column from the left as (i,\ j). Initially, each square is either white or black. The initial color of the grid is given to you as characters a_{ij}, arranged in a square shape. If the square (i,\ j) is white, a_{ij} is `.`. If it is black, a_{ij} is `#`. You are developing a robot that repaints the grid. It can repeatedly perform the following operation: * Select two integers i, j (1 ≀ i,\ j ≀ N). Memorize the colors of the squares (i,\ 1), (i,\ 2), ..., (i,\ N) as c_1, c_2, ..., c_N, respectively. Then, repaint the squares (1,\ j), (2,\ j), ..., (N,\ j) with the colors c_1, c_2, ..., c_N, respectively. Your objective is to turn all the squares black. Determine whether it is possible, and find the minimum necessary number of operations to achieve it if the answer is positive. Constraints * 2 ≀ N ≀ 500 * a_{ij} is either `.` or `#`. Input The input is given from Standard Input in the following format: N a_{11}...a_{1N} : a_{N1}...a_{NN} Output If it is possible to turn all the squares black, print the minimum necessary number of operations to achieve the objective. If it is impossible, print `-1` instead. Examples Input 2 #. .# Output 3 Input 2 . .# Output 3 Input 2 .. .. Output -1 Input 2 Output 0 Input 3 .#. .#. Output 2 Input 3 ... .#. ... Output 5 Submitted Solution: ``` N=int(input()) a=[input() for i in range(N)] blackExistCol=[False for i in range(N)] blackNumRow=[0 for i in range(N)] flag=False Max=-1 already=0 for i in range(N): for j in range(N): if a[i][j]=="#": blackExistCol[j]=True blackNumRow[i]+=1 flag=True if flag==False: print("-1") else: for i in range(N): if blackExistCol[i]: if Max<blackNumRow[i]: Max=blackNumRow[i] Min=N-Max for j in range(N): for i in range(N): if a[i][j]==".": break else: already+=1 temp=max(blackNumRow) Min2=N-temp+1 ans=min(Min,Min2)+N-already print(ans) ```
instruction
0
35,881
7
71,762
Yes
output
1
35,881
7
71,763
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a square-shaped grid with N vertical rows and N horizontal columns. We will denote the square at the i-th row from the top and the j-th column from the left as (i,\ j). Initially, each square is either white or black. The initial color of the grid is given to you as characters a_{ij}, arranged in a square shape. If the square (i,\ j) is white, a_{ij} is `.`. If it is black, a_{ij} is `#`. You are developing a robot that repaints the grid. It can repeatedly perform the following operation: * Select two integers i, j (1 ≀ i,\ j ≀ N). Memorize the colors of the squares (i,\ 1), (i,\ 2), ..., (i,\ N) as c_1, c_2, ..., c_N, respectively. Then, repaint the squares (1,\ j), (2,\ j), ..., (N,\ j) with the colors c_1, c_2, ..., c_N, respectively. Your objective is to turn all the squares black. Determine whether it is possible, and find the minimum necessary number of operations to achieve it if the answer is positive. Constraints * 2 ≀ N ≀ 500 * a_{ij} is either `.` or `#`. Input The input is given from Standard Input in the following format: N a_{11}...a_{1N} : a_{N1}...a_{NN} Output If it is possible to turn all the squares black, print the minimum necessary number of operations to achieve the objective. If it is impossible, print `-1` instead. Examples Input 2 #. .# Output 3 Input 2 . .# Output 3 Input 2 .. .. Output -1 Input 2 Output 0 Input 3 .#. .#. Output 2 Input 3 ... .#. ... Output 5 Submitted Solution: ``` N = int(input()) G = [[1 if s == '#' else 0 for s in input().strip()] for _ in range(N)] Gm = list(map(sum, G)) GT = list(map(list, zip(*G))) geta = sum(sum(g) == N for g in GT) exist = [sum(g) > 0 for g in GT] ans = -1 if sum(Gm) != 0: ans = N - geta + min(N - Gm[i] + 1 - exist[i] for i in range(N)) print(ans) ```
instruction
0
35,882
7
71,764
Yes
output
1
35,882
7
71,765
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a square-shaped grid with N vertical rows and N horizontal columns. We will denote the square at the i-th row from the top and the j-th column from the left as (i,\ j). Initially, each square is either white or black. The initial color of the grid is given to you as characters a_{ij}, arranged in a square shape. If the square (i,\ j) is white, a_{ij} is `.`. If it is black, a_{ij} is `#`. You are developing a robot that repaints the grid. It can repeatedly perform the following operation: * Select two integers i, j (1 ≀ i,\ j ≀ N). Memorize the colors of the squares (i,\ 1), (i,\ 2), ..., (i,\ N) as c_1, c_2, ..., c_N, respectively. Then, repaint the squares (1,\ j), (2,\ j), ..., (N,\ j) with the colors c_1, c_2, ..., c_N, respectively. Your objective is to turn all the squares black. Determine whether it is possible, and find the minimum necessary number of operations to achieve it if the answer is positive. Constraints * 2 ≀ N ≀ 500 * a_{ij} is either `.` or `#`. Input The input is given from Standard Input in the following format: N a_{11}...a_{1N} : a_{N1}...a_{NN} Output If it is possible to turn all the squares black, print the minimum necessary number of operations to achieve the objective. If it is impossible, print `-1` instead. Examples Input 2 #. .# Output 3 Input 2 . .# Output 3 Input 2 .. .. Output -1 Input 2 Output 0 Input 3 .#. .#. Output 2 Input 3 ... .#. ... Output 5 Submitted Solution: ``` s='#';n,*a=open(0).readlines();n=int(n);f=sum(n==t.count(s)for t in zip(*a));r=range(n);print(-all(not s in t for t in a)or min(n*2-f-a[i].count(s)+all(s<a[j][i]for j in r)for i in r)) ```
instruction
0
35,883
7
71,766
Yes
output
1
35,883
7
71,767
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a square-shaped grid with N vertical rows and N horizontal columns. We will denote the square at the i-th row from the top and the j-th column from the left as (i,\ j). Initially, each square is either white or black. The initial color of the grid is given to you as characters a_{ij}, arranged in a square shape. If the square (i,\ j) is white, a_{ij} is `.`. If it is black, a_{ij} is `#`. You are developing a robot that repaints the grid. It can repeatedly perform the following operation: * Select two integers i, j (1 ≀ i,\ j ≀ N). Memorize the colors of the squares (i,\ 1), (i,\ 2), ..., (i,\ N) as c_1, c_2, ..., c_N, respectively. Then, repaint the squares (1,\ j), (2,\ j), ..., (N,\ j) with the colors c_1, c_2, ..., c_N, respectively. Your objective is to turn all the squares black. Determine whether it is possible, and find the minimum necessary number of operations to achieve it if the answer is positive. Constraints * 2 ≀ N ≀ 500 * a_{ij} is either `.` or `#`. Input The input is given from Standard Input in the following format: N a_{11}...a_{1N} : a_{N1}...a_{NN} Output If it is possible to turn all the squares black, print the minimum necessary number of operations to achieve the objective. If it is impossible, print `-1` instead. Examples Input 2 #. .# Output 3 Input 2 . .# Output 3 Input 2 .. .. Output -1 Input 2 Output 0 Input 3 .#. .#. Output 2 Input 3 ... .#. ... Output 5 Submitted Solution: ``` n,*a=open(0);n=int(n);*b,=zip(*a);f,e=[sum((c in t)^1for t in b)for c in'.#'];print(-(e>n)or min(n*2-f-x.count('#')+2-('#'in y)for x,y in zip(a,b))) ```
instruction
0
35,884
7
71,768
Yes
output
1
35,884
7
71,769
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a square-shaped grid with N vertical rows and N horizontal columns. We will denote the square at the i-th row from the top and the j-th column from the left as (i,\ j). Initially, each square is either white or black. The initial color of the grid is given to you as characters a_{ij}, arranged in a square shape. If the square (i,\ j) is white, a_{ij} is `.`. If it is black, a_{ij} is `#`. You are developing a robot that repaints the grid. It can repeatedly perform the following operation: * Select two integers i, j (1 ≀ i,\ j ≀ N). Memorize the colors of the squares (i,\ 1), (i,\ 2), ..., (i,\ N) as c_1, c_2, ..., c_N, respectively. Then, repaint the squares (1,\ j), (2,\ j), ..., (N,\ j) with the colors c_1, c_2, ..., c_N, respectively. Your objective is to turn all the squares black. Determine whether it is possible, and find the minimum necessary number of operations to achieve it if the answer is positive. Constraints * 2 ≀ N ≀ 500 * a_{ij} is either `.` or `#`. Input The input is given from Standard Input in the following format: N a_{11}...a_{1N} : a_{N1}...a_{NN} Output If it is possible to turn all the squares black, print the minimum necessary number of operations to achieve the objective. If it is impossible, print `-1` instead. Examples Input 2 #. .# Output 3 Input 2 . .# Output 3 Input 2 .. .. Output -1 Input 2 Output 0 Input 3 .#. .#. Output 2 Input 3 ... .#. ... Output 5 Submitted Solution: ``` from queue import Queue import numpy as np n=int(input()) l=[] for i in range(n): l.append(list(input())) for i in range(n): for j in range(n): if l[i][j] == ".": l[i][j] = 0 else: l[i][j] = 1 l=np.array(l) q = Queue() q.put([l,0]) while not q.empty(): t = q.get() if all([t[0][x][y]==1 for x in range(n) for y in range(n)]): res = t[1] break for i in range(n): for j in range(n): tmp = np.array(t[0]) tmp[i,:] = tmp[:,j] q.put([tmp,t[1]+1]) print(res) ```
instruction
0
35,885
7
71,770
No
output
1
35,885
7
71,771
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a square-shaped grid with N vertical rows and N horizontal columns. We will denote the square at the i-th row from the top and the j-th column from the left as (i,\ j). Initially, each square is either white or black. The initial color of the grid is given to you as characters a_{ij}, arranged in a square shape. If the square (i,\ j) is white, a_{ij} is `.`. If it is black, a_{ij} is `#`. You are developing a robot that repaints the grid. It can repeatedly perform the following operation: * Select two integers i, j (1 ≀ i,\ j ≀ N). Memorize the colors of the squares (i,\ 1), (i,\ 2), ..., (i,\ N) as c_1, c_2, ..., c_N, respectively. Then, repaint the squares (1,\ j), (2,\ j), ..., (N,\ j) with the colors c_1, c_2, ..., c_N, respectively. Your objective is to turn all the squares black. Determine whether it is possible, and find the minimum necessary number of operations to achieve it if the answer is positive. Constraints * 2 ≀ N ≀ 500 * a_{ij} is either `.` or `#`. Input The input is given from Standard Input in the following format: N a_{11}...a_{1N} : a_{N1}...a_{NN} Output If it is possible to turn all the squares black, print the minimum necessary number of operations to achieve the objective. If it is impossible, print `-1` instead. Examples Input 2 #. .# Output 3 Input 2 . .# Output 3 Input 2 .. .. Output -1 Input 2 Output 0 Input 3 .#. .#. Output 2 Input 3 ... .#. ... Output 5 Submitted Solution: ``` import numpy as np def change(a, i, j): b = np.copy(a) b[:,j] = a[i,:] return b def isTrue(maxiter, current, a): n = a.shape[0] if np.all(a): return current current += 1 ret = maxiter+1 if maxiter < current: return ret for i in range(n): for j in range(n): b = change(a,i,j) if np.all(a==b) or np.all(b==False): ret = min(ret,maxiter+1) else: ret = min(ret,isTrue(maxiter, current, b)) return ret n = int(input()) a = [] for _ in range(n): a.append([num=='#' for num in input()]) a = np.array(a) ret = isTrue(n*2-1,0,a) if ret > n*2-1: ret = -1 print(ret) ```
instruction
0
35,886
7
71,772
No
output
1
35,886
7
71,773
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a square-shaped grid with N vertical rows and N horizontal columns. We will denote the square at the i-th row from the top and the j-th column from the left as (i,\ j). Initially, each square is either white or black. The initial color of the grid is given to you as characters a_{ij}, arranged in a square shape. If the square (i,\ j) is white, a_{ij} is `.`. If it is black, a_{ij} is `#`. You are developing a robot that repaints the grid. It can repeatedly perform the following operation: * Select two integers i, j (1 ≀ i,\ j ≀ N). Memorize the colors of the squares (i,\ 1), (i,\ 2), ..., (i,\ N) as c_1, c_2, ..., c_N, respectively. Then, repaint the squares (1,\ j), (2,\ j), ..., (N,\ j) with the colors c_1, c_2, ..., c_N, respectively. Your objective is to turn all the squares black. Determine whether it is possible, and find the minimum necessary number of operations to achieve it if the answer is positive. Constraints * 2 ≀ N ≀ 500 * a_{ij} is either `.` or `#`. Input The input is given from Standard Input in the following format: N a_{11}...a_{1N} : a_{N1}...a_{NN} Output If it is possible to turn all the squares black, print the minimum necessary number of operations to achieve the objective. If it is impossible, print `-1` instead. Examples Input 2 #. .# Output 3 Input 2 . .# Output 3 Input 2 .. .. Output -1 Input 2 Output 0 Input 3 .#. .#. Output 2 Input 3 ... .#. ... Output 5 Submitted Solution: ``` N=int(input()) Grid=[list(input()) for i in range(N)] Grid_v=[list(x) for x in zip(*Grid)] W,H=0,0 if Grid==[["."]*N for i in range(N)]: print(-1) exit() for i in range(N): if Grid_v[i]!=["."]*N: W=max(W,Grid[i].count("#")) for i in range(N): if Grid_v[i]!=["#"]*N: H+=1 print(N-W+H) ```
instruction
0
35,887
7
71,774
No
output
1
35,887
7
71,775
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a square-shaped grid with N vertical rows and N horizontal columns. We will denote the square at the i-th row from the top and the j-th column from the left as (i,\ j). Initially, each square is either white or black. The initial color of the grid is given to you as characters a_{ij}, arranged in a square shape. If the square (i,\ j) is white, a_{ij} is `.`. If it is black, a_{ij} is `#`. You are developing a robot that repaints the grid. It can repeatedly perform the following operation: * Select two integers i, j (1 ≀ i,\ j ≀ N). Memorize the colors of the squares (i,\ 1), (i,\ 2), ..., (i,\ N) as c_1, c_2, ..., c_N, respectively. Then, repaint the squares (1,\ j), (2,\ j), ..., (N,\ j) with the colors c_1, c_2, ..., c_N, respectively. Your objective is to turn all the squares black. Determine whether it is possible, and find the minimum necessary number of operations to achieve it if the answer is positive. Constraints * 2 ≀ N ≀ 500 * a_{ij} is either `.` or `#`. Input The input is given from Standard Input in the following format: N a_{11}...a_{1N} : a_{N1}...a_{NN} Output If it is possible to turn all the squares black, print the minimum necessary number of operations to achieve the objective. If it is impossible, print `-1` instead. Examples Input 2 #. .# Output 3 Input 2 . .# Output 3 Input 2 .. .. Output -1 Input 2 Output 0 Input 3 .#. .#. Output 2 Input 3 ... .#. ... Output 5 Submitted Solution: ``` # -*- coding:utf-8 -*- n = int(input()) a = [[0]*n]*n check = ['#']*n check2 = ['.']*n for i in range(n): tmp = input() a[i] = list(tmp) flag = 0 flag2 = 0 for i in range(n): if a[i] != check: flag = 1 break for i in range(n): if a[i] != check2: flag2 = 1 break if flag == 0: print(0) elif flag2 != 1: print(-1) else: m = 0 c = [] tmpi = 0 count = 0 for i in range(n): if m < a[i].count('#'): m = a[i].count('#') count += 1 tmpi = i for i in range(n): flag = 0 for j in range(n): if a[j][i] != '#': flag = 1 break if flag == 0: c.append(i) if m == n: print(m-len(c)) elif m == 1 and count == 1: if tmpi != a[tmpi].index('#'): for i in range(n): flag = 0 for j in range(n): if a[i][j] == '#': if i == j: print(n-1+n-len(c)) flag = 1 break if flag == 1: break if flag == 0: print(2*n) else: print(n-m+n-len(c)) else: ad = n-m print(ad + n - len(c)) ```
instruction
0
35,888
7
71,776
No
output
1
35,888
7
71,777
Provide tags and a correct Python 3 solution for this coding contest problem. Young boy Artem tries to paint a picture, and he asks his mother Medina to help him. Medina is very busy, that's why she asked for your help. Artem wants to paint an n Γ— m board. Each cell of the board should be colored in white or black. Lets B be the number of black cells that have at least one white neighbor adjacent by the side. Let W be the number of white cells that have at least one black neighbor adjacent by the side. A coloring is called good if B = W + 1. The first coloring shown below has B=5 and W=4 (all cells have at least one neighbor with the opposite color). However, the second coloring is not good as it has B=4, W=4 (only the bottom right cell doesn't have a neighbor with the opposite color). <image> Please, help Medina to find any good coloring. It's guaranteed that under given constraints the solution always exists. If there are several solutions, output any of them. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 20). Each of the next t lines contains two integers n, m (2 ≀ n,m ≀ 100) β€” the number of rows and the number of columns in the grid. Output For each test case print n lines, each of length m, where i-th line is the i-th row of your colored matrix (cell labeled with 'B' means that the cell is black, and 'W' means white). Do not use quotes. It's guaranteed that under given constraints the solution always exists. Example Input 2 3 2 3 3 Output BW WB BB BWB BWW BWB Note In the first testcase, B=3, W=2. In the second testcase, B=5, W=4. You can see the coloring in the statement.
instruction
0
36,211
7
72,422
Tags: constructive algorithms Correct Solution: ``` from sys import stdin def func(n): a = n*(n+1) b = n+n+1 return (a*b)//6 for _ in range(int(stdin.readline())): #n = int(stdin.readline()) n, m = map(int, stdin.readline().split()) #s = stdin.readline().strip() arr = [[0 for i in range(m)] for j in range(n)] s = 'B' count = 0 for i in range(n): for j in range(m): arr[i][j] = s count += 1 if count %2 == 0: s = 'B' else: s = 'W' if (n*m) %2 == 0: if arr[-1][-1] == 'B': arr[-1][-2] = 'B' else: arr[-1][1] = 'B' arr[-1][0] = 'B' for i in range(n): print(''.join(arr[i])) ```
output
1
36,211
7
72,423
Provide tags and a correct Python 3 solution for this coding contest problem. Young boy Artem tries to paint a picture, and he asks his mother Medina to help him. Medina is very busy, that's why she asked for your help. Artem wants to paint an n Γ— m board. Each cell of the board should be colored in white or black. Lets B be the number of black cells that have at least one white neighbor adjacent by the side. Let W be the number of white cells that have at least one black neighbor adjacent by the side. A coloring is called good if B = W + 1. The first coloring shown below has B=5 and W=4 (all cells have at least one neighbor with the opposite color). However, the second coloring is not good as it has B=4, W=4 (only the bottom right cell doesn't have a neighbor with the opposite color). <image> Please, help Medina to find any good coloring. It's guaranteed that under given constraints the solution always exists. If there are several solutions, output any of them. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 20). Each of the next t lines contains two integers n, m (2 ≀ n,m ≀ 100) β€” the number of rows and the number of columns in the grid. Output For each test case print n lines, each of length m, where i-th line is the i-th row of your colored matrix (cell labeled with 'B' means that the cell is black, and 'W' means white). Do not use quotes. It's guaranteed that under given constraints the solution always exists. Example Input 2 3 2 3 3 Output BW WB BB BWB BWW BWB Note In the first testcase, B=3, W=2. In the second testcase, B=5, W=4. You can see the coloring in the statement.
instruction
0
36,212
7
72,424
Tags: constructive algorithms Correct Solution: ``` def inn(): return map(int, input().split()) def cout(s): print(s, end="") t,=inn() while(t>0): t-=1 a, b=inn() if(a&1)and(b&1): check=False else: check=True x=0 while(x<a): x+=1 y=0 while(y<b): y+=1 if (x&1==y&1): cout("B") elif(check): cout("B") check=False else: cout("W") print() ```
output
1
36,212
7
72,425
Provide tags and a correct Python 3 solution for this coding contest problem. Young boy Artem tries to paint a picture, and he asks his mother Medina to help him. Medina is very busy, that's why she asked for your help. Artem wants to paint an n Γ— m board. Each cell of the board should be colored in white or black. Lets B be the number of black cells that have at least one white neighbor adjacent by the side. Let W be the number of white cells that have at least one black neighbor adjacent by the side. A coloring is called good if B = W + 1. The first coloring shown below has B=5 and W=4 (all cells have at least one neighbor with the opposite color). However, the second coloring is not good as it has B=4, W=4 (only the bottom right cell doesn't have a neighbor with the opposite color). <image> Please, help Medina to find any good coloring. It's guaranteed that under given constraints the solution always exists. If there are several solutions, output any of them. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 20). Each of the next t lines contains two integers n, m (2 ≀ n,m ≀ 100) β€” the number of rows and the number of columns in the grid. Output For each test case print n lines, each of length m, where i-th line is the i-th row of your colored matrix (cell labeled with 'B' means that the cell is black, and 'W' means white). Do not use quotes. It's guaranteed that under given constraints the solution always exists. Example Input 2 3 2 3 3 Output BW WB BB BWB BWW BWB Note In the first testcase, B=3, W=2. In the second testcase, B=5, W=4. You can see the coloring in the statement.
instruction
0
36,213
7
72,426
Tags: constructive algorithms Correct Solution: ``` n_cases = int(input()) for case_num in range(1, n_cases + 1): n, m = [int(x) for x in input().split()] mat = [[0] * m for _ in range(n)] for y in range(n): for x in range(m): mat[y][x] = (y + x) % 2 if n*m % 2 == 1: # we done! pass else: mat[0][1] = 0 for y in range(n): for x in range(m): print('W' if mat[y][x] else 'B', end='') print() ```
output
1
36,213
7
72,427
Provide tags and a correct Python 3 solution for this coding contest problem. Young boy Artem tries to paint a picture, and he asks his mother Medina to help him. Medina is very busy, that's why she asked for your help. Artem wants to paint an n Γ— m board. Each cell of the board should be colored in white or black. Lets B be the number of black cells that have at least one white neighbor adjacent by the side. Let W be the number of white cells that have at least one black neighbor adjacent by the side. A coloring is called good if B = W + 1. The first coloring shown below has B=5 and W=4 (all cells have at least one neighbor with the opposite color). However, the second coloring is not good as it has B=4, W=4 (only the bottom right cell doesn't have a neighbor with the opposite color). <image> Please, help Medina to find any good coloring. It's guaranteed that under given constraints the solution always exists. If there are several solutions, output any of them. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 20). Each of the next t lines contains two integers n, m (2 ≀ n,m ≀ 100) β€” the number of rows and the number of columns in the grid. Output For each test case print n lines, each of length m, where i-th line is the i-th row of your colored matrix (cell labeled with 'B' means that the cell is black, and 'W' means white). Do not use quotes. It's guaranteed that under given constraints the solution always exists. Example Input 2 3 2 3 3 Output BW WB BB BWB BWW BWB Note In the first testcase, B=3, W=2. In the second testcase, B=5, W=4. You can see the coloring in the statement.
instruction
0
36,214
7
72,428
Tags: constructive algorithms Correct Solution: ``` for _ in range(int(input())): N, M = map(int, input().split()) mat = [['B' for col in range(M)] for row in range(N)] mat[0][0] = 'W' for row in mat: print(''.join(row)) ```
output
1
36,214
7
72,429
Provide tags and a correct Python 3 solution for this coding contest problem. Young boy Artem tries to paint a picture, and he asks his mother Medina to help him. Medina is very busy, that's why she asked for your help. Artem wants to paint an n Γ— m board. Each cell of the board should be colored in white or black. Lets B be the number of black cells that have at least one white neighbor adjacent by the side. Let W be the number of white cells that have at least one black neighbor adjacent by the side. A coloring is called good if B = W + 1. The first coloring shown below has B=5 and W=4 (all cells have at least one neighbor with the opposite color). However, the second coloring is not good as it has B=4, W=4 (only the bottom right cell doesn't have a neighbor with the opposite color). <image> Please, help Medina to find any good coloring. It's guaranteed that under given constraints the solution always exists. If there are several solutions, output any of them. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 20). Each of the next t lines contains two integers n, m (2 ≀ n,m ≀ 100) β€” the number of rows and the number of columns in the grid. Output For each test case print n lines, each of length m, where i-th line is the i-th row of your colored matrix (cell labeled with 'B' means that the cell is black, and 'W' means white). Do not use quotes. It's guaranteed that under given constraints the solution always exists. Example Input 2 3 2 3 3 Output BW WB BB BWB BWW BWB Note In the first testcase, B=3, W=2. In the second testcase, B=5, W=4. You can see the coloring in the statement.
instruction
0
36,215
7
72,430
Tags: constructive algorithms Correct Solution: ``` n=int(input()) for i in range(n): n,m=map(int,input().split()) w=((n*m)-1)//2 b=(n*m)-w for i in range(n): for j in range(m): if((i+j)%2==1) and(w!=0): print("W",end="") w=w-1 else: print("B",end="") print() ```
output
1
36,215
7
72,431
Provide tags and a correct Python 3 solution for this coding contest problem. Young boy Artem tries to paint a picture, and he asks his mother Medina to help him. Medina is very busy, that's why she asked for your help. Artem wants to paint an n Γ— m board. Each cell of the board should be colored in white or black. Lets B be the number of black cells that have at least one white neighbor adjacent by the side. Let W be the number of white cells that have at least one black neighbor adjacent by the side. A coloring is called good if B = W + 1. The first coloring shown below has B=5 and W=4 (all cells have at least one neighbor with the opposite color). However, the second coloring is not good as it has B=4, W=4 (only the bottom right cell doesn't have a neighbor with the opposite color). <image> Please, help Medina to find any good coloring. It's guaranteed that under given constraints the solution always exists. If there are several solutions, output any of them. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 20). Each of the next t lines contains two integers n, m (2 ≀ n,m ≀ 100) β€” the number of rows and the number of columns in the grid. Output For each test case print n lines, each of length m, where i-th line is the i-th row of your colored matrix (cell labeled with 'B' means that the cell is black, and 'W' means white). Do not use quotes. It's guaranteed that under given constraints the solution always exists. Example Input 2 3 2 3 3 Output BW WB BB BWB BWW BWB Note In the first testcase, B=3, W=2. In the second testcase, B=5, W=4. You can see the coloring in the statement.
instruction
0
36,216
7
72,432
Tags: constructive algorithms Correct Solution: ``` def find(N,M): for i in range(N): A=['B']*M if i==0: A[0]='W' print(''.join(A)) def main(): for i in range(int(input())): N,M=list(map(int,input().split())) find(N,M) main() ```
output
1
36,216
7
72,433
Provide tags and a correct Python 3 solution for this coding contest problem. Young boy Artem tries to paint a picture, and he asks his mother Medina to help him. Medina is very busy, that's why she asked for your help. Artem wants to paint an n Γ— m board. Each cell of the board should be colored in white or black. Lets B be the number of black cells that have at least one white neighbor adjacent by the side. Let W be the number of white cells that have at least one black neighbor adjacent by the side. A coloring is called good if B = W + 1. The first coloring shown below has B=5 and W=4 (all cells have at least one neighbor with the opposite color). However, the second coloring is not good as it has B=4, W=4 (only the bottom right cell doesn't have a neighbor with the opposite color). <image> Please, help Medina to find any good coloring. It's guaranteed that under given constraints the solution always exists. If there are several solutions, output any of them. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 20). Each of the next t lines contains two integers n, m (2 ≀ n,m ≀ 100) β€” the number of rows and the number of columns in the grid. Output For each test case print n lines, each of length m, where i-th line is the i-th row of your colored matrix (cell labeled with 'B' means that the cell is black, and 'W' means white). Do not use quotes. It's guaranteed that under given constraints the solution always exists. Example Input 2 3 2 3 3 Output BW WB BB BWB BWW BWB Note In the first testcase, B=3, W=2. In the second testcase, B=5, W=4. You can see the coloring in the statement.
instruction
0
36,217
7
72,434
Tags: constructive algorithms Correct Solution: ``` t = int(input()) for i in range(t): m, n = list(map(int, input().split())) for j in range(m-1): print('B'*n) print('B'*(n-1)+'W') ```
output
1
36,217
7
72,435
Provide tags and a correct Python 3 solution for this coding contest problem. Young boy Artem tries to paint a picture, and he asks his mother Medina to help him. Medina is very busy, that's why she asked for your help. Artem wants to paint an n Γ— m board. Each cell of the board should be colored in white or black. Lets B be the number of black cells that have at least one white neighbor adjacent by the side. Let W be the number of white cells that have at least one black neighbor adjacent by the side. A coloring is called good if B = W + 1. The first coloring shown below has B=5 and W=4 (all cells have at least one neighbor with the opposite color). However, the second coloring is not good as it has B=4, W=4 (only the bottom right cell doesn't have a neighbor with the opposite color). <image> Please, help Medina to find any good coloring. It's guaranteed that under given constraints the solution always exists. If there are several solutions, output any of them. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 20). Each of the next t lines contains two integers n, m (2 ≀ n,m ≀ 100) β€” the number of rows and the number of columns in the grid. Output For each test case print n lines, each of length m, where i-th line is the i-th row of your colored matrix (cell labeled with 'B' means that the cell is black, and 'W' means white). Do not use quotes. It's guaranteed that under given constraints the solution always exists. Example Input 2 3 2 3 3 Output BW WB BB BWB BWW BWB Note In the first testcase, B=3, W=2. In the second testcase, B=5, W=4. You can see the coloring in the statement.
instruction
0
36,218
7
72,436
Tags: constructive algorithms Correct Solution: ``` n=int(input()) for i in range(n): a,b=map(int,input().split()) '''l=[["B"]*b]*a print(l[0]) l[0][b-1]="W" for j in l: print("".join(j))''' print("B"*(b-1)+"W") for j in range(a-1): print("B"*b) ```
output
1
36,218
7
72,437
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Young boy Artem tries to paint a picture, and he asks his mother Medina to help him. Medina is very busy, that's why she asked for your help. Artem wants to paint an n Γ— m board. Each cell of the board should be colored in white or black. Lets B be the number of black cells that have at least one white neighbor adjacent by the side. Let W be the number of white cells that have at least one black neighbor adjacent by the side. A coloring is called good if B = W + 1. The first coloring shown below has B=5 and W=4 (all cells have at least one neighbor with the opposite color). However, the second coloring is not good as it has B=4, W=4 (only the bottom right cell doesn't have a neighbor with the opposite color). <image> Please, help Medina to find any good coloring. It's guaranteed that under given constraints the solution always exists. If there are several solutions, output any of them. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 20). Each of the next t lines contains two integers n, m (2 ≀ n,m ≀ 100) β€” the number of rows and the number of columns in the grid. Output For each test case print n lines, each of length m, where i-th line is the i-th row of your colored matrix (cell labeled with 'B' means that the cell is black, and 'W' means white). Do not use quotes. It's guaranteed that under given constraints the solution always exists. Example Input 2 3 2 3 3 Output BW WB BB BWB BWW BWB Note In the first testcase, B=3, W=2. In the second testcase, B=5, W=4. You can see the coloring in the statement. Submitted Solution: ``` '''in1 = int(input()) for i in range(in1): in2 = input().split() in2 = list(map(int ,in2)) for j in range(in2[0]): s = '' k = 0 while k < in2[1]: if in2[0] % 2 == 0 and in2[1] % 2 == 1: if j <= 1 : if j == 0 and k <=1 : s = s + 'BW' k = 1 elif j == 1 and k <=1 : s = s + 'WB' k = 1 else: s = s + 'B' else: if k == 0: s = s + 'B' else: s = s + 'W' else: if k == in2[1] - 1 and j == in2[0] - 1 and k % 2 == 0: s = s + 'W' break if k == in2[1] - 1 and j == in2[0] - 1 and k % 2 != 0: s = s + 'B' break if k % 2 == 0: s = s + 'B' else: s = s + 'W' k = k + 1 print(s) ''' in1 = int(input()) for i in range(in1): in2 = input().split() in2 = list(map(int ,in2)) for j in range(in2[0]): for k in range(in2[1]): if j==0 and k==0: print('W',end='') else: print('B',end='') print() ```
instruction
0
36,219
7
72,438
Yes
output
1
36,219
7
72,439
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Young boy Artem tries to paint a picture, and he asks his mother Medina to help him. Medina is very busy, that's why she asked for your help. Artem wants to paint an n Γ— m board. Each cell of the board should be colored in white or black. Lets B be the number of black cells that have at least one white neighbor adjacent by the side. Let W be the number of white cells that have at least one black neighbor adjacent by the side. A coloring is called good if B = W + 1. The first coloring shown below has B=5 and W=4 (all cells have at least one neighbor with the opposite color). However, the second coloring is not good as it has B=4, W=4 (only the bottom right cell doesn't have a neighbor with the opposite color). <image> Please, help Medina to find any good coloring. It's guaranteed that under given constraints the solution always exists. If there are several solutions, output any of them. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 20). Each of the next t lines contains two integers n, m (2 ≀ n,m ≀ 100) β€” the number of rows and the number of columns in the grid. Output For each test case print n lines, each of length m, where i-th line is the i-th row of your colored matrix (cell labeled with 'B' means that the cell is black, and 'W' means white). Do not use quotes. It's guaranteed that under given constraints the solution always exists. Example Input 2 3 2 3 3 Output BW WB BB BWB BWW BWB Note In the first testcase, B=3, W=2. In the second testcase, B=5, W=4. You can see the coloring in the statement. Submitted Solution: ``` import math t=int(input()) d=[] for i in range(100): d.append(list("B"*100)) d[0][0]="W" for _ in range(t): n,m=map(int,input().split()) for i in range(n): print("".join(d[i][0:m])) ```
instruction
0
36,220
7
72,440
Yes
output
1
36,220
7
72,441
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Young boy Artem tries to paint a picture, and he asks his mother Medina to help him. Medina is very busy, that's why she asked for your help. Artem wants to paint an n Γ— m board. Each cell of the board should be colored in white or black. Lets B be the number of black cells that have at least one white neighbor adjacent by the side. Let W be the number of white cells that have at least one black neighbor adjacent by the side. A coloring is called good if B = W + 1. The first coloring shown below has B=5 and W=4 (all cells have at least one neighbor with the opposite color). However, the second coloring is not good as it has B=4, W=4 (only the bottom right cell doesn't have a neighbor with the opposite color). <image> Please, help Medina to find any good coloring. It's guaranteed that under given constraints the solution always exists. If there are several solutions, output any of them. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 20). Each of the next t lines contains two integers n, m (2 ≀ n,m ≀ 100) β€” the number of rows and the number of columns in the grid. Output For each test case print n lines, each of length m, where i-th line is the i-th row of your colored matrix (cell labeled with 'B' means that the cell is black, and 'W' means white). Do not use quotes. It's guaranteed that under given constraints the solution always exists. Example Input 2 3 2 3 3 Output BW WB BB BWB BWW BWB Note In the first testcase, B=3, W=2. In the second testcase, B=5, W=4. You can see the coloring in the statement. Submitted Solution: ``` from collections import defaultdict as dd import sys input=sys.stdin.readline t=int(input()) while t: #n=int(input()) n,m=map(int,input().split()) #l=list(map(int,input().split())) res=[['B']*m for i in range(n)] res[0][0]='W' for i in res: print(*i,sep="") t-=1 ```
instruction
0
36,221
7
72,442
Yes
output
1
36,221
7
72,443
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Young boy Artem tries to paint a picture, and he asks his mother Medina to help him. Medina is very busy, that's why she asked for your help. Artem wants to paint an n Γ— m board. Each cell of the board should be colored in white or black. Lets B be the number of black cells that have at least one white neighbor adjacent by the side. Let W be the number of white cells that have at least one black neighbor adjacent by the side. A coloring is called good if B = W + 1. The first coloring shown below has B=5 and W=4 (all cells have at least one neighbor with the opposite color). However, the second coloring is not good as it has B=4, W=4 (only the bottom right cell doesn't have a neighbor with the opposite color). <image> Please, help Medina to find any good coloring. It's guaranteed that under given constraints the solution always exists. If there are several solutions, output any of them. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 20). Each of the next t lines contains two integers n, m (2 ≀ n,m ≀ 100) β€” the number of rows and the number of columns in the grid. Output For each test case print n lines, each of length m, where i-th line is the i-th row of your colored matrix (cell labeled with 'B' means that the cell is black, and 'W' means white). Do not use quotes. It's guaranteed that under given constraints the solution always exists. Example Input 2 3 2 3 3 Output BW WB BB BWB BWW BWB Note In the first testcase, B=3, W=2. In the second testcase, B=5, W=4. You can see the coloring in the statement. Submitted Solution: ``` t = int(input()) while t: n,m = map(int,input().split()) print("W"+"B"*(m-1)) for i in range(n-1): print("B"*m) t -= 1 ```
instruction
0
36,222
7
72,444
Yes
output
1
36,222
7
72,445
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Young boy Artem tries to paint a picture, and he asks his mother Medina to help him. Medina is very busy, that's why she asked for your help. Artem wants to paint an n Γ— m board. Each cell of the board should be colored in white or black. Lets B be the number of black cells that have at least one white neighbor adjacent by the side. Let W be the number of white cells that have at least one black neighbor adjacent by the side. A coloring is called good if B = W + 1. The first coloring shown below has B=5 and W=4 (all cells have at least one neighbor with the opposite color). However, the second coloring is not good as it has B=4, W=4 (only the bottom right cell doesn't have a neighbor with the opposite color). <image> Please, help Medina to find any good coloring. It's guaranteed that under given constraints the solution always exists. If there are several solutions, output any of them. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 20). Each of the next t lines contains two integers n, m (2 ≀ n,m ≀ 100) β€” the number of rows and the number of columns in the grid. Output For each test case print n lines, each of length m, where i-th line is the i-th row of your colored matrix (cell labeled with 'B' means that the cell is black, and 'W' means white). Do not use quotes. It's guaranteed that under given constraints the solution always exists. Example Input 2 3 2 3 3 Output BW WB BB BWB BWW BWB Note In the first testcase, B=3, W=2. In the second testcase, B=5, W=4. You can see the coloring in the statement. Submitted Solution: ``` t = int(input()) for t1 in range(t): n, m = map(int, input().split()) for i in range(n): for j in range(m): if i == n - 1 and j == 0 and (n % 2 == 0 or m % 2 == 0): print('B', end='') else: if (i + j) % 2 == 0: print('B', end='') else: print('W', end='') print() ```
instruction
0
36,223
7
72,446
No
output
1
36,223
7
72,447
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Young boy Artem tries to paint a picture, and he asks his mother Medina to help him. Medina is very busy, that's why she asked for your help. Artem wants to paint an n Γ— m board. Each cell of the board should be colored in white or black. Lets B be the number of black cells that have at least one white neighbor adjacent by the side. Let W be the number of white cells that have at least one black neighbor adjacent by the side. A coloring is called good if B = W + 1. The first coloring shown below has B=5 and W=4 (all cells have at least one neighbor with the opposite color). However, the second coloring is not good as it has B=4, W=4 (only the bottom right cell doesn't have a neighbor with the opposite color). <image> Please, help Medina to find any good coloring. It's guaranteed that under given constraints the solution always exists. If there are several solutions, output any of them. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 20). Each of the next t lines contains two integers n, m (2 ≀ n,m ≀ 100) β€” the number of rows and the number of columns in the grid. Output For each test case print n lines, each of length m, where i-th line is the i-th row of your colored matrix (cell labeled with 'B' means that the cell is black, and 'W' means white). Do not use quotes. It's guaranteed that under given constraints the solution always exists. Example Input 2 3 2 3 3 Output BW WB BB BWB BWW BWB Note In the first testcase, B=3, W=2. In the second testcase, B=5, W=4. You can see the coloring in the statement. Submitted Solution: ``` from sys import stdin, stdout, stderr, maxsize # mod = int(1e9 + 7) # import re # can use multiple splits tup = lambda: map(int, stdin.buffer.readline().split()) I = lambda: int(stdin.buffer.readline()) lint = lambda: [int(x) for x in stdin.buffer.readline().split()] S = lambda: stdin.readline().replace('\n', '').strip() # def grid(r, c): return [lint() for i in range(r)] # def debug(*args, c=6): print('\033[3{}m'.format(c), *args, '\033[0m', file=stderr) stpr = lambda x: stdout.write(f'{x}' + '\n') star = lambda x: print(' '.join(map(str, x))) # from math import ceil, floor from collections import defaultdict # from bisect import bisect_left, bisect_right for _ in range(I()): n , m = tup() for i in range(n): if i ==0: print('BW'*(m//2) + 'B'*(m%2)) elif i ==1: #print('WB'*(m//2)+'W'*(m%2)) print('W'*m) else: print('B'*(m-1) + 'W') ```
instruction
0
36,224
7
72,448
No
output
1
36,224
7
72,449
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Young boy Artem tries to paint a picture, and he asks his mother Medina to help him. Medina is very busy, that's why she asked for your help. Artem wants to paint an n Γ— m board. Each cell of the board should be colored in white or black. Lets B be the number of black cells that have at least one white neighbor adjacent by the side. Let W be the number of white cells that have at least one black neighbor adjacent by the side. A coloring is called good if B = W + 1. The first coloring shown below has B=5 and W=4 (all cells have at least one neighbor with the opposite color). However, the second coloring is not good as it has B=4, W=4 (only the bottom right cell doesn't have a neighbor with the opposite color). <image> Please, help Medina to find any good coloring. It's guaranteed that under given constraints the solution always exists. If there are several solutions, output any of them. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 20). Each of the next t lines contains two integers n, m (2 ≀ n,m ≀ 100) β€” the number of rows and the number of columns in the grid. Output For each test case print n lines, each of length m, where i-th line is the i-th row of your colored matrix (cell labeled with 'B' means that the cell is black, and 'W' means white). Do not use quotes. It's guaranteed that under given constraints the solution always exists. Example Input 2 3 2 3 3 Output BW WB BB BWB BWW BWB Note In the first testcase, B=3, W=2. In the second testcase, B=5, W=4. You can see the coloring in the statement. Submitted Solution: ``` for i in range(int(input())): n,m=map(int,input().split()) if(m&1): a=m//2 c=0 for j in range(n-1): if(c==0): print("BW"*a+"B") c=1 else: print("WB"*a+"W") c=0 print("BW"*((m-3)//2)+"W"+"BB") else: a=m//2 c=0 for j in range(n-1): if(c==0): print("BW"*a) c=1 else: print("WB"*a) c=0 print("BW"*((m-2)//2)+"BB") ```
instruction
0
36,225
7
72,450
No
output
1
36,225
7
72,451
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Young boy Artem tries to paint a picture, and he asks his mother Medina to help him. Medina is very busy, that's why she asked for your help. Artem wants to paint an n Γ— m board. Each cell of the board should be colored in white or black. Lets B be the number of black cells that have at least one white neighbor adjacent by the side. Let W be the number of white cells that have at least one black neighbor adjacent by the side. A coloring is called good if B = W + 1. The first coloring shown below has B=5 and W=4 (all cells have at least one neighbor with the opposite color). However, the second coloring is not good as it has B=4, W=4 (only the bottom right cell doesn't have a neighbor with the opposite color). <image> Please, help Medina to find any good coloring. It's guaranteed that under given constraints the solution always exists. If there are several solutions, output any of them. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 20). Each of the next t lines contains two integers n, m (2 ≀ n,m ≀ 100) β€” the number of rows and the number of columns in the grid. Output For each test case print n lines, each of length m, where i-th line is the i-th row of your colored matrix (cell labeled with 'B' means that the cell is black, and 'W' means white). Do not use quotes. It's guaranteed that under given constraints the solution always exists. Example Input 2 3 2 3 3 Output BW WB BB BWB BWW BWB Note In the first testcase, B=3, W=2. In the second testcase, B=5, W=4. You can see the coloring in the statement. Submitted Solution: ``` for _ in range(int(input())): n, m = map(int,input().split()) t=1 k=1 if n*m % 2 == 1: for i in range(n): if t == 1: print('BW'*((m-1)//2)+'B') elif t == 0: print('WB'*((m-1)//2)+'W') t = 1 - t else: for i in range(n-1): if k == 1: print('BW'*(m//2) if m % 2 == 0 else 'BW'*(n//2)) elif k == 0: print('WB'*(m//2) if m % 2 == 0 else 'WB'*(n//2)) k = 1 - k print('BW'*((m-2)//2) + 'BB' if m % 2 == 0 else 'BW'*((n-2)//2) + 'BB') ```
instruction
0
36,226
7
72,452
No
output
1
36,226
7
72,453
Provide tags and a correct Python 3 solution for this coding contest problem. John Doe has an n Γ— m table. John Doe can paint points in some table cells, not more than one point in one table cell. John Doe wants to use such operations to make each square subtable of size n Γ— n have exactly k points. John Doe wondered, how many distinct ways to fill the table with points are there, provided that the condition must hold. As this number can be rather large, John Doe asks to find its remainder after dividing by 1000000007 (109 + 7). You should assume that John always paints a point exactly in the center of some cell. Two ways to fill a table are considered distinct, if there exists a table cell, that has a point in one way and doesn't have it in the other. Input A single line contains space-separated integers n, m, k (1 ≀ n ≀ 100; n ≀ m ≀ 1018; 0 ≀ k ≀ n2) β€” the number of rows of the table, the number of columns of the table and the number of points each square must contain. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Output In a single line print a single integer β€” the remainder from dividing the described number of ways by 1000000007 (109 + 7). Examples Input 5 6 1 Output 45 Note Let's consider the first test case: <image> The gray area belongs to both 5 Γ— 5 squares. So, if it has one point, then there shouldn't be points in any other place. If one of the white areas has a point, then the other one also must have a point. Thus, there are about 20 variants, where the point lies in the gray area and 25 variants, where each of the white areas contains a point. Overall there are 45 variants.
instruction
0
36,378
7
72,756
Tags: bitmasks, combinatorics, dp, math Correct Solution: ``` n,m,k=map(int,input().split()) M=int(1e9+7) N=n*n iv=[0]*(N+1) iv[1]=1 for i in range(2, N+1): iv[i]=M-M//i*iv[M%i]%M f1=[1]*(N+1) for i in range(1, N+1): f1[i]=f1[i-1]*i%M f2=[1]*(N+1) for i in range(1, N+1): f2[i]=f2[i-1]*iv[i]%M left=m%n #m/n+1, m/n def powM(b, p): r=1 while p>0: if p%2>0: r=r*b%M b=b*b%M p//=2 return r c=[[powM(f1[n]*f2[j]%M*f2[n-j]%M, m//n+i) for j in range(n+1)] for i in range(2)] #print(c) dp=[[0]*(k+1) for i in range(n+1)] dp[0][0]=1 for i in range(n): for j in range(k+1): #prune if j>i*n or j<k-(n-i)*n: continue for l in range(min(n, k-j)+1): # i,j -> i+1,j+l dp[i+1][j+l]=(dp[i+1][j+l]+c[i<left][l]*dp[i][j])%M print(dp[n][k]) ```
output
1
36,378
7
72,757
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. User ainta decided to paint a wall. The wall consists of n2 tiles, that are arranged in an n Γ— n table. Some tiles are painted, and the others are not. As he wants to paint it beautifully, he will follow the rules below. 1. Firstly user ainta looks at the wall. If there is at least one painted cell on each row and at least one painted cell on each column, he stops coloring. Otherwise, he goes to step 2. 2. User ainta choose any tile on the wall with uniform probability. 3. If the tile he has chosen is not painted, he paints the tile. Otherwise, he ignores it. 4. Then he takes a rest for one minute even if he doesn't paint the tile. And then ainta goes to step 1. However ainta is worried if it would take too much time to finish this work. So he wants to calculate the expected time needed to paint the wall by the method above. Help him find the expected time. You can assume that choosing and painting any tile consumes no time at all. Input The first line contains two integers n and m (1 ≀ n ≀ 2Β·103; 0 ≀ m ≀ min(n2, 2Β·104)) β€” the size of the wall and the number of painted cells. Next m lines goes, each contains two integers ri and ci (1 ≀ ri, ci ≀ n) β€” the position of the painted cell. It is guaranteed that the positions are all distinct. Consider the rows of the table are numbered from 1 to n. Consider the columns of the table are numbered from 1 to n. Output In a single line print the expected time to paint the wall in minutes. Your answer will be considered correct if it has at most 10 - 4 absolute or relative error. Examples Input 5 2 2 3 4 1 Output 11.7669491886 Input 2 2 1 1 1 2 Output 2.0000000000 Input 1 1 1 1 Output 0.0000000000 Submitted Solution: ``` import time from random import randint n, m = map(int, input().split(' ')) cols = [0 for i in range(n)] rows = [0 for i in range(n)] for i in range(m): x, y = map(int, input().split(' ')) cols[x - 1] = 1 rows[y - 1] = 1 rounds = 0 sums = 0 answer = 0 t0 = time.time() while time.time() - t0 < 0.5: colscopy = [cols[i] for i in range(n)] rowscopy = [rows[i] for i in range(n)] colsum = sum(colscopy) rowsum = sum(rowscopy) counter = 0 while colsum != n or rowsum != n: x, y = randint(0, n - 1), randint(0, n - 1) colsum = colsum + 1 if colscopy[x - 1] == 0 else colsum rowsum = rowsum + 1 if rowscopy[y - 1] == 0 else rowsum colscopy[x - 1] = 1 rowscopy[y - 1] = 1 counter += 1 rounds += 1 sums += counter tempans = sums / rounds #if (abs(tempans - answer) < 10 ** -4): # answer = tempans # break answer = tempans print(answer) ```
instruction
0
36,450
7
72,900
No
output
1
36,450
7
72,901
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. User ainta decided to paint a wall. The wall consists of n2 tiles, that are arranged in an n Γ— n table. Some tiles are painted, and the others are not. As he wants to paint it beautifully, he will follow the rules below. 1. Firstly user ainta looks at the wall. If there is at least one painted cell on each row and at least one painted cell on each column, he stops coloring. Otherwise, he goes to step 2. 2. User ainta choose any tile on the wall with uniform probability. 3. If the tile he has chosen is not painted, he paints the tile. Otherwise, he ignores it. 4. Then he takes a rest for one minute even if he doesn't paint the tile. And then ainta goes to step 1. However ainta is worried if it would take too much time to finish this work. So he wants to calculate the expected time needed to paint the wall by the method above. Help him find the expected time. You can assume that choosing and painting any tile consumes no time at all. Input The first line contains two integers n and m (1 ≀ n ≀ 2Β·103; 0 ≀ m ≀ min(n2, 2Β·104)) β€” the size of the wall and the number of painted cells. Next m lines goes, each contains two integers ri and ci (1 ≀ ri, ci ≀ n) β€” the position of the painted cell. It is guaranteed that the positions are all distinct. Consider the rows of the table are numbered from 1 to n. Consider the columns of the table are numbered from 1 to n. Output In a single line print the expected time to paint the wall in minutes. Your answer will be considered correct if it has at most 10 - 4 absolute or relative error. Examples Input 5 2 2 3 4 1 Output 11.7669491886 Input 2 2 1 1 1 2 Output 2.0000000000 Input 1 1 1 1 Output 0.0000000000 Submitted Solution: ``` import timeit class CodeforcesTask398BSolution: def __init__(self): self.result = '' self.n_m = [] self.painted = [] def read_input(self): self.n_m = [int(x) for x in input().split(" ")] for _ in range(self.n_m[1]): self.painted.append([int(x) for x in input().split(" ")]) def process_task(self): n = self.n_m[0] dp = [[0] * (n + 1) for x in range(n + 1)] for x in range(self.n_m[0] + 1): for y in range(self.n_m[0] + 1): if x or y: b = (n*y + n*x - x*y) a0 = (n ** 2) / b a1 = (x * (n - y)) / b a2 = (y * (n - x)) / b a3 = (x * y) / b in_sum = a0 if a1: in_sum += a1 * dp[x - 1][y] if a2: in_sum += a2 * dp[x][y - 1] if a3: in_sum += a3 * dp[x - 1][y - 1] dp[x][y] = in_sum cols = [0] * n rows = [0] * n for paint in self.painted: cols[paint[0] - 1] = 1 rows[paint[1] - 1] = 1 self.result = str(dp[n - sum(cols)][n - sum(rows)]) def get_result(self): return self.result if __name__ == "__main__": Solution = CodeforcesTask398BSolution() Solution.read_input() import time start = time.time() Solution.process_task() print(time.time() - start) print(Solution.get_result()) ```
instruction
0
36,451
7
72,902
No
output
1
36,451
7
72,903
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. User ainta decided to paint a wall. The wall consists of n2 tiles, that are arranged in an n Γ— n table. Some tiles are painted, and the others are not. As he wants to paint it beautifully, he will follow the rules below. 1. Firstly user ainta looks at the wall. If there is at least one painted cell on each row and at least one painted cell on each column, he stops coloring. Otherwise, he goes to step 2. 2. User ainta choose any tile on the wall with uniform probability. 3. If the tile he has chosen is not painted, he paints the tile. Otherwise, he ignores it. 4. Then he takes a rest for one minute even if he doesn't paint the tile. And then ainta goes to step 1. However ainta is worried if it would take too much time to finish this work. So he wants to calculate the expected time needed to paint the wall by the method above. Help him find the expected time. You can assume that choosing and painting any tile consumes no time at all. Input The first line contains two integers n and m (1 ≀ n ≀ 2Β·103; 0 ≀ m ≀ min(n2, 2Β·104)) β€” the size of the wall and the number of painted cells. Next m lines goes, each contains two integers ri and ci (1 ≀ ri, ci ≀ n) β€” the position of the painted cell. It is guaranteed that the positions are all distinct. Consider the rows of the table are numbered from 1 to n. Consider the columns of the table are numbered from 1 to n. Output In a single line print the expected time to paint the wall in minutes. Your answer will be considered correct if it has at most 10 - 4 absolute or relative error. Examples Input 5 2 2 3 4 1 Output 11.7669491886 Input 2 2 1 1 1 2 Output 2.0000000000 Input 1 1 1 1 Output 0.0000000000 Submitted Solution: ``` from random import randint n, m = map(int, input().split(' ')) cols = [0 for i in range(n)] rows = [0 for i in range(n)] for i in range(m): x, y = map(int, input().split(' ')) cols[x - 1] = 1 rows[y - 1] = 1 rounds = 0 sums = 0 answer = 0 while True: colscopy = [cols[i] for i in range(n)] rowscopy = [rows[i] for i in range(n)] colsum = sum(colscopy) rowsum = sum(rowscopy) counter = 0 while colsum != n or rowsum != n: x, y = randint(0, n - 1), randint(0, n - 1) colsum = colsum + 1 if colscopy[x - 1] == 0 else colsum rowsum = rowsum + 1 if rowscopy[y - 1] == 0 else rowsum colscopy[x - 1] = 1 rowscopy[y - 1] = 1 counter += 1 rounds += 1 sums += counter tempans = sums / rounds if (abs(tempans - answer) < 10 ** -4): answer = tempans break answer = tempans print(answer) ```
instruction
0
36,452
7
72,904
No
output
1
36,452
7
72,905
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. User ainta decided to paint a wall. The wall consists of n2 tiles, that are arranged in an n Γ— n table. Some tiles are painted, and the others are not. As he wants to paint it beautifully, he will follow the rules below. 1. Firstly user ainta looks at the wall. If there is at least one painted cell on each row and at least one painted cell on each column, he stops coloring. Otherwise, he goes to step 2. 2. User ainta choose any tile on the wall with uniform probability. 3. If the tile he has chosen is not painted, he paints the tile. Otherwise, he ignores it. 4. Then he takes a rest for one minute even if he doesn't paint the tile. And then ainta goes to step 1. However ainta is worried if it would take too much time to finish this work. So he wants to calculate the expected time needed to paint the wall by the method above. Help him find the expected time. You can assume that choosing and painting any tile consumes no time at all. Input The first line contains two integers n and m (1 ≀ n ≀ 2Β·103; 0 ≀ m ≀ min(n2, 2Β·104)) β€” the size of the wall and the number of painted cells. Next m lines goes, each contains two integers ri and ci (1 ≀ ri, ci ≀ n) β€” the position of the painted cell. It is guaranteed that the positions are all distinct. Consider the rows of the table are numbered from 1 to n. Consider the columns of the table are numbered from 1 to n. Output In a single line print the expected time to paint the wall in minutes. Your answer will be considered correct if it has at most 10 - 4 absolute or relative error. Examples Input 5 2 2 3 4 1 Output 11.7669491886 Input 2 2 1 1 1 2 Output 2.0000000000 Input 1 1 1 1 Output 0.0000000000 Submitted Solution: ``` import time from random import randint n, m = map(int, input().split(' ')) cols = [0 for i in range(n)] rows = [0 for i in range(n)] for i in range(m): x, y = map(int, input().split(' ')) cols[x - 1] = 1 rows[y - 1] = 1 rounds = 0 sums = 0 answer = 0 t0 = time.time() while time.time() - t0 < 0.75: colscopy = [cols[i] for i in range(n)] rowscopy = [rows[i] for i in range(n)] colsum = sum(colscopy) rowsum = sum(rowscopy) counter = 0 while colsum != n or rowsum != n: x, y = randint(0, n - 1), randint(0, n - 1) colsum = colsum + 1 if colscopy[x - 1] == 0 else colsum rowsum = rowsum + 1 if rowscopy[y - 1] == 0 else rowsum colscopy[x - 1] = 1 rowscopy[y - 1] = 1 counter += 1 rounds += 1 sums += counter tempans = sums / rounds '''if (abs(tempans - answer) < 10 ** -4): answer = tempans break''' answer = tempans print(answer) ```
instruction
0
36,453
7
72,906
No
output
1
36,453
7
72,907
Provide tags and a correct Python 3 solution for this coding contest problem. Bizon the Champion isn't just attentive, he also is very hardworking. Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of ai meters. Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times. Input The first line contains integer n (1 ≀ n ≀ 5000) β€” the number of fence planks. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109). Output Print a single integer β€” the minimum number of strokes needed to paint the whole fence. Examples Input 5 2 2 1 2 1 Output 3 Input 2 2 2 Output 2 Input 1 5 Output 1 Note In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank. In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes. In the third sample there is only one plank that can be painted using a single vertical stroke.
instruction
0
36,463
7
72,926
Tags: divide and conquer, dp, greedy Correct Solution: ``` import sys ar=[] def solve(l, r, val): if(r<l): return 0 indx=l+ar[l:r+1].index(min(ar[l:r+1])) tot=r-l+1 cur=ar[indx]-val+solve(l, indx-1, ar[indx])+solve(indx+1, r, ar[indx]) return min(tot, cur) sys.setrecursionlimit(10000) n=int(input()) ar=list(map(int, input().split())) print(solve(0, n-1, 0)) ```
output
1
36,463
7
72,927
Provide tags and a correct Python 3 solution for this coding contest problem. Bizon the Champion isn't just attentive, he also is very hardworking. Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of ai meters. Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times. Input The first line contains integer n (1 ≀ n ≀ 5000) β€” the number of fence planks. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109). Output Print a single integer β€” the minimum number of strokes needed to paint the whole fence. Examples Input 5 2 2 1 2 1 Output 3 Input 2 2 2 Output 2 Input 1 5 Output 1 Note In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank. In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes. In the third sample there is only one plank that can be painted using a single vertical stroke.
instruction
0
36,464
7
72,928
Tags: divide and conquer, dp, greedy Correct Solution: ``` import sys sys.setrecursionlimit(10000) # hago n-1 llamadas recursivas para largo n, por default el limit es 1000 creo def cantidad_de_brochazos(izq,der,altura): """ :param izq: limite izquierdo de la zona a pintar :param der: limite derecho de la zona a pintar :param altura: altura hasta la cual ya esta pintada la cerca hacia abajo :return: cantidad necesaria """ if izq > der: return 0 # caso base solo_vertical = der - izq + 1 # siempre esta la opcion de solo hacer trazos verticales altura_minima = min(L[izq:der + 1]) #necesitamos saber cual es el bloque con la menor altura minimo = izq + L[izq:der + 1].index(altura_minima) dividir_y_conquistar = L[minimo] - altura + cantidad_de_brochazos(izq,minimo - 1, L[minimo]) + \ cantidad_de_brochazos(minimo + 1, der, L[minimo]) return min(solo_vertical, dividir_y_conquistar) if __name__ == '__main__': n = int(sys.stdin.readline()) L = list(map(int, input().split())) sys.stdout.write(str(cantidad_de_brochazos(0,n-1,0))) ```
output
1
36,464
7
72,929
Provide tags and a correct Python 3 solution for this coding contest problem. Bizon the Champion isn't just attentive, he also is very hardworking. Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of ai meters. Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times. Input The first line contains integer n (1 ≀ n ≀ 5000) β€” the number of fence planks. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109). Output Print a single integer β€” the minimum number of strokes needed to paint the whole fence. Examples Input 5 2 2 1 2 1 Output 3 Input 2 2 2 Output 2 Input 1 5 Output 1 Note In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank. In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes. In the third sample there is only one plank that can be painted using a single vertical stroke.
instruction
0
36,465
7
72,930
Tags: divide and conquer, dp, greedy Correct Solution: ``` import sys oo=1000000000000 ar=[] def solve(l, r, val): if(r<l): return 0 indx=l+ar[l:r+1].index(min(ar[l:r+1])) tot=r-l+1 cur=ar[indx]-val+solve(l, indx-1, ar[indx])+solve(indx+1, r, ar[indx]) return min(tot, cur) sys.setrecursionlimit(10000) n=int(input()) ar=list(map(int, input().split())) print(solve(0, n-1, 0)) ```
output
1
36,465
7
72,931
Provide tags and a correct Python 3 solution for this coding contest problem. Bizon the Champion isn't just attentive, he also is very hardworking. Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of ai meters. Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times. Input The first line contains integer n (1 ≀ n ≀ 5000) β€” the number of fence planks. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109). Output Print a single integer β€” the minimum number of strokes needed to paint the whole fence. Examples Input 5 2 2 1 2 1 Output 3 Input 2 2 2 Output 2 Input 1 5 Output 1 Note In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank. In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes. In the third sample there is only one plank that can be painted using a single vertical stroke.
instruction
0
36,466
7
72,932
Tags: divide and conquer, dp, greedy Correct Solution: ``` import sys, threading sys.setrecursionlimit(600000) #threading.stack_size(10240000) # thread.start() def rec(left, right, off): if left>right: return 0 mini= min(arr[left:right+1]) ind=arr.index(mini,left,right+1) ans = mini - off ans += rec(left, ind-1, mini) ans += rec(ind+1, right, mini) return min(right-left+1, ans) if __name__ == '__main__': n = int(input()) arr = list(map(int, input().split())) print(rec(0, n-1, 0)) ```
output
1
36,466
7
72,933
Provide tags and a correct Python 3 solution for this coding contest problem. Bizon the Champion isn't just attentive, he also is very hardworking. Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of ai meters. Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times. Input The first line contains integer n (1 ≀ n ≀ 5000) β€” the number of fence planks. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109). Output Print a single integer β€” the minimum number of strokes needed to paint the whole fence. Examples Input 5 2 2 1 2 1 Output 3 Input 2 2 2 Output 2 Input 1 5 Output 1 Note In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank. In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes. In the third sample there is only one plank that can be painted using a single vertical stroke.
instruction
0
36,467
7
72,934
Tags: divide and conquer, dp, greedy Correct Solution: ``` n = int(input()) t = [int(x) for x in input().split()] t.insert(0, -1) dp = [0] * (n+3) dp[1] = t[1] for i in range(2, n+1): #albo wszystkie na lewo sa pionowe dp[i] = t[i] + (i-1) #albo na lewo jest jakis poziomy smallest = t[i] #najmniejszy napotkany for j in range(i-1, 0, -1): #zakladam ze j jest ostatnim poziomym smallest = min(smallest, t[j]) tmp = (dp[j] + (i-j-1) + (t[i] - smallest)) dp[i] = min(dp[i], tmp) res = n #kiedy wszystkie sa pionowe for i in range(1, n+1): #i-ty jest ostanim poziomym res = min(res, dp[i] + (n-i)) print(res) ```
output
1
36,467
7
72,935