text
stringlengths
198
433k
conversation_id
int64
0
109k
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya plays a computer game with ninjas. At this stage Vasya's ninja should get out of a deep canyon. The canyon consists of two vertical parallel walls, their height is n meters. Let's imagine that we split these walls into 1 meter-long areas and number them with positive integers from 1 to n from bottom to top. Some areas are safe and the ninja can climb them. Others are spiky and ninja can't be there. Let's call such areas dangerous. Initially the ninja is on the lower area of the left wall. He can use each second to perform one of the following actions: * climb one area up; * climb one area down; * jump to the opposite wall. That gets the ninja to the area that is exactly k meters higher than the area he jumped from. More formally, if before the jump the ninja is located at area x of one wall, then after the jump he is located at area x + k of the other wall. If at some point of time the ninja tries to get to an area with a number larger than n, then we can assume that the ninja got out of the canyon. The canyon gets flooded and each second the water level raises one meter. Initially the water level is at the lower border of the first area. Ninja cannot be on the area covered by water. We can assume that the ninja and the water "move in turns" — first the ninja performs some action, then the water raises for one meter, then the ninja performs one more action and so on. The level is considered completed if the ninja manages to get out of the canyon. After several failed attempts Vasya started to doubt whether it is possible to complete the level at all. Help him answer the question. Input The first line contains two integers n and k (1 ≤ n, k ≤ 105) — the height of the canyon and the height of ninja's jump, correspondingly. The second line contains the description of the left wall — a string with the length of n characters. The i-th character represents the state of the i-th wall area: character "X" represents a dangerous area and character "-" represents a safe area. The third line describes the right wall in the same format. It is guaranteed that the first area of the left wall is not dangerous. Output Print "YES" (without the quotes) if the ninja can get out from the canyon, otherwise, print "NO" (without the quotes). Examples Input 7 3 ---X--X -X--XX- Output YES Input 6 2 --X-X- X--XX- Output NO Note In the first sample the ninja should first jump to the right wall, then go one meter down along the right wall, then jump to the left wall. The next jump can get the ninja from the canyon. In the second sample there's no way the ninja can get out of the canyon. Tags: shortest paths Correct Solution: ``` from sys import stdin, stdout from collections import deque n, k = map(int, stdin.readline().split()) maps = [] maps.append(list(stdin.readline() + '-')) maps.append(list(stdin.readline() + '-')) visit = [[0, 0] for i in range(n + 1)] visit[0][0] = 1 queue = deque() label = 0 queue.append((0, -1, 0))#твой уровень, уровень воды, номер стены while queue: mine, line, num = queue.popleft() if line >= mine: continue if mine + k >= n: label = 1 if mine + 1 < n and not visit[mine + 1][num] and maps[num][mine + 1] == '-': queue.append((mine + 1, line + 1, num)) visit[mine + 1][num] = 1 if mine and mine - line > 1 and not visit[mine - 1][num] and maps[num][mine - 1] == '-': queue.append((mine - 1, line + 1, num)) visit[mine - 1][num] = 1 if mine + k < n and not visit[mine + k][(num + 1) % 2] and maps[(num + 1) % 2][mine + k] == '-': queue.append((min(mine + k, n), line + 1, (num + 1) % 2)) visit[min(mine + k, n)][(num + 1) % 2] = 1 if label: stdout.write('YES') else: stdout.write('NO') ```
6,900
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya plays a computer game with ninjas. At this stage Vasya's ninja should get out of a deep canyon. The canyon consists of two vertical parallel walls, their height is n meters. Let's imagine that we split these walls into 1 meter-long areas and number them with positive integers from 1 to n from bottom to top. Some areas are safe and the ninja can climb them. Others are spiky and ninja can't be there. Let's call such areas dangerous. Initially the ninja is on the lower area of the left wall. He can use each second to perform one of the following actions: * climb one area up; * climb one area down; * jump to the opposite wall. That gets the ninja to the area that is exactly k meters higher than the area he jumped from. More formally, if before the jump the ninja is located at area x of one wall, then after the jump he is located at area x + k of the other wall. If at some point of time the ninja tries to get to an area with a number larger than n, then we can assume that the ninja got out of the canyon. The canyon gets flooded and each second the water level raises one meter. Initially the water level is at the lower border of the first area. Ninja cannot be on the area covered by water. We can assume that the ninja and the water "move in turns" — first the ninja performs some action, then the water raises for one meter, then the ninja performs one more action and so on. The level is considered completed if the ninja manages to get out of the canyon. After several failed attempts Vasya started to doubt whether it is possible to complete the level at all. Help him answer the question. Input The first line contains two integers n and k (1 ≤ n, k ≤ 105) — the height of the canyon and the height of ninja's jump, correspondingly. The second line contains the description of the left wall — a string with the length of n characters. The i-th character represents the state of the i-th wall area: character "X" represents a dangerous area and character "-" represents a safe area. The third line describes the right wall in the same format. It is guaranteed that the first area of the left wall is not dangerous. Output Print "YES" (without the quotes) if the ninja can get out from the canyon, otherwise, print "NO" (without the quotes). Examples Input 7 3 ---X--X -X--XX- Output YES Input 6 2 --X-X- X--XX- Output NO Note In the first sample the ninja should first jump to the right wall, then go one meter down along the right wall, then jump to the left wall. The next jump can get the ninja from the canyon. In the second sample there's no way the ninja can get out of the canyon. Tags: shortest paths Correct Solution: ``` from collections import deque l, j = [int(i) for i in input().split(' ')] wallA = list(input()) wallB = list(input()) g = {} for i in range(l): # Each 4-tuple represents: (Visited?, Current Height, Current Water Height, Drowned?) if wallA[i] == '-': g[(1,i+1)] = (-1, 0, 0, False) if wallB[i] == '-': g[(-1,i+1)] = (-1, 0, 0, False) g[(1, 1)] = ('VISITED', 1, 0, False) q = deque([(1, 1)]) while q: c = q.popleft() up = (c[0], c[1]+1) down = (c[0], c[1]-1) jump = (c[0]*-1, c[1] + j) if g[c][1] <= g[c][2]: g[c] = (g[c][0], g[c][1], g[c][2], True) if up in g and g[up][0] == -1: q.append(up) g[up] = ('VISITED', g[c][1] + 1, g[c][2] + 1, g[c][3]) if down in g and g[down][0] == -1: q.append(down) g[down] = ('VISITED', g[c][1] - 1, g[c][2] + 1, g[c][3]) if jump in g and g[jump][0] == -1: q.append(jump) g[jump] = ('VISITED', g[c][1] + j, g[c][2] + 1, g[c][3]) def graphHasEscape(graph): for node in graph: result = graph[node] if result[0] == 'VISITED' and ((result[1] + 1 > l) or (result[1] + j > l)) and not result[3]: return True break return False if graphHasEscape(g): print('YES') else: print('NO') ```
6,901
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya plays a computer game with ninjas. At this stage Vasya's ninja should get out of a deep canyon. The canyon consists of two vertical parallel walls, their height is n meters. Let's imagine that we split these walls into 1 meter-long areas and number them with positive integers from 1 to n from bottom to top. Some areas are safe and the ninja can climb them. Others are spiky and ninja can't be there. Let's call such areas dangerous. Initially the ninja is on the lower area of the left wall. He can use each second to perform one of the following actions: * climb one area up; * climb one area down; * jump to the opposite wall. That gets the ninja to the area that is exactly k meters higher than the area he jumped from. More formally, if before the jump the ninja is located at area x of one wall, then after the jump he is located at area x + k of the other wall. If at some point of time the ninja tries to get to an area with a number larger than n, then we can assume that the ninja got out of the canyon. The canyon gets flooded and each second the water level raises one meter. Initially the water level is at the lower border of the first area. Ninja cannot be on the area covered by water. We can assume that the ninja and the water "move in turns" — first the ninja performs some action, then the water raises for one meter, then the ninja performs one more action and so on. The level is considered completed if the ninja manages to get out of the canyon. After several failed attempts Vasya started to doubt whether it is possible to complete the level at all. Help him answer the question. Input The first line contains two integers n and k (1 ≤ n, k ≤ 105) — the height of the canyon and the height of ninja's jump, correspondingly. The second line contains the description of the left wall — a string with the length of n characters. The i-th character represents the state of the i-th wall area: character "X" represents a dangerous area and character "-" represents a safe area. The third line describes the right wall in the same format. It is guaranteed that the first area of the left wall is not dangerous. Output Print "YES" (without the quotes) if the ninja can get out from the canyon, otherwise, print "NO" (without the quotes). Examples Input 7 3 ---X--X -X--XX- Output YES Input 6 2 --X-X- X--XX- Output NO Note In the first sample the ninja should first jump to the right wall, then go one meter down along the right wall, then jump to the left wall. The next jump can get the ninja from the canyon. In the second sample there's no way the ninja can get out of the canyon. Submitted Solution: ``` from collections import deque import sys n, k = map(int, sys.stdin.readline().split()) s1, s2 = sys.stdin.readline(), sys.stdin.readline() mat = [[] for _ in range(2 * 100000 + 10)] used = [-1] * (2 * 100000 + 10) for i in range(n): if i > 0 and s1[i - 1] != 'X' and s1[i] != 'X': mat[i].append(i - 1) used[i - 1] = 0 if i < n - 1 and s1[i] != 'X' and s1[i + 1] != 'X': mat[i].append(i + 1) used[i + 1] = 0 used[i] = 0 if i + k >= n: mat[i].append(2 * 100000 + 9) used[2 * 100000 + 9] = 0 elif s2[i + k] != 'X': mat[i].append(i + k + 100001) used[i + k + 100001] = 0 for i in range(n): if i > 0 and s2[i - 1] != 'X' and s2[i] != 'X': mat[i + 100001].append(i - 1 + 100001) used[i - 1 + 100001] = 0 if i < n - 1 and s2[i] != 'X' and s2[i + 1] != 'X': mat[i + 100001].append(i + 1 + 100001) used[i + 1 + 100001] = 0 used[i + 100001] = 0 if i + k >= n: mat[i + 100001].append(2 * 100000 + 9) used[2 * 100000 + 9] = 0 elif s1[i + k] != 'X': mat[i + 100001].append(i + k) used[i + k] = 0 q = deque([0]) dist = [0] * (2 * 100000 + 10) while q: u = q[0] q.popleft() used[u] = 1 for v in mat[u]: if used[v] == 0: dist[v] = dist[u] + 1 used[v] = 1 if (v <= 100000 and dist[v] <= v) or (v > 100000 and dist[v] <= v - 100001) or v == 2 * 100000 + 9: q.append(v) if used[2 * 100000 + 9] == 1: sys.stdout.write('YES') else: sys.stdout.write('NO') ``` Yes
6,902
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya plays a computer game with ninjas. At this stage Vasya's ninja should get out of a deep canyon. The canyon consists of two vertical parallel walls, their height is n meters. Let's imagine that we split these walls into 1 meter-long areas and number them with positive integers from 1 to n from bottom to top. Some areas are safe and the ninja can climb them. Others are spiky and ninja can't be there. Let's call such areas dangerous. Initially the ninja is on the lower area of the left wall. He can use each second to perform one of the following actions: * climb one area up; * climb one area down; * jump to the opposite wall. That gets the ninja to the area that is exactly k meters higher than the area he jumped from. More formally, if before the jump the ninja is located at area x of one wall, then after the jump he is located at area x + k of the other wall. If at some point of time the ninja tries to get to an area with a number larger than n, then we can assume that the ninja got out of the canyon. The canyon gets flooded and each second the water level raises one meter. Initially the water level is at the lower border of the first area. Ninja cannot be on the area covered by water. We can assume that the ninja and the water "move in turns" — first the ninja performs some action, then the water raises for one meter, then the ninja performs one more action and so on. The level is considered completed if the ninja manages to get out of the canyon. After several failed attempts Vasya started to doubt whether it is possible to complete the level at all. Help him answer the question. Input The first line contains two integers n and k (1 ≤ n, k ≤ 105) — the height of the canyon and the height of ninja's jump, correspondingly. The second line contains the description of the left wall — a string with the length of n characters. The i-th character represents the state of the i-th wall area: character "X" represents a dangerous area and character "-" represents a safe area. The third line describes the right wall in the same format. It is guaranteed that the first area of the left wall is not dangerous. Output Print "YES" (without the quotes) if the ninja can get out from the canyon, otherwise, print "NO" (without the quotes). Examples Input 7 3 ---X--X -X--XX- Output YES Input 6 2 --X-X- X--XX- Output NO Note In the first sample the ninja should first jump to the right wall, then go one meter down along the right wall, then jump to the left wall. The next jump can get the ninja from the canyon. In the second sample there's no way the ninja can get out of the canyon. Submitted Solution: ``` left, right = (0, 1) undiscovered = 0 processed = 1 def up(state): water = state[0] + 1 point = state[1] + 1 l_or_r = state[2] return [water, point, l_or_r] def down(state): water = state[0] + 1 point = state[1] - 1 l_or_r = state[2] return [water, point, l_or_r] def jump(state, k): water = state[0] + 1 point = state[1] + k if state[2] == left: l_or_r = right else: l_or_r = left return [water, point, l_or_r] def push_next_states(state, k, states): states[len(states):] = [up(state), down(state), jump(state, k)] return states def solve(n, k, lwall, rwall): water = -1 point = 0 l_or_r = left state = [water, point, l_or_r] table = [[undiscovered]*n, [undiscovered]*n] states = push_next_states(state, k, []) while states != []: state = states.pop() if state is None: break water = state[0] point = state[1] if n <= point: return True if water < point: l_or_r = state[2] if table[l_or_r][point] != processed: if l_or_r == left: wall = lwall else: wall = rwall if wall[point] == '-': if n <= point+k: return True push_next_states(state, k, states) table[l_or_r][point] = processed return False def main(): import sys n, k = [int(x) for x in sys.stdin.readline().split()] lwall = sys.stdin.readline().rstrip() rwall = sys.stdin.readline().rstrip() result = solve(n, k, lwall, rwall) if result: print("YES", end="") else: print("NO", end="") main() ``` Yes
6,903
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya plays a computer game with ninjas. At this stage Vasya's ninja should get out of a deep canyon. The canyon consists of two vertical parallel walls, their height is n meters. Let's imagine that we split these walls into 1 meter-long areas and number them with positive integers from 1 to n from bottom to top. Some areas are safe and the ninja can climb them. Others are spiky and ninja can't be there. Let's call such areas dangerous. Initially the ninja is on the lower area of the left wall. He can use each second to perform one of the following actions: * climb one area up; * climb one area down; * jump to the opposite wall. That gets the ninja to the area that is exactly k meters higher than the area he jumped from. More formally, if before the jump the ninja is located at area x of one wall, then after the jump he is located at area x + k of the other wall. If at some point of time the ninja tries to get to an area with a number larger than n, then we can assume that the ninja got out of the canyon. The canyon gets flooded and each second the water level raises one meter. Initially the water level is at the lower border of the first area. Ninja cannot be on the area covered by water. We can assume that the ninja and the water "move in turns" — first the ninja performs some action, then the water raises for one meter, then the ninja performs one more action and so on. The level is considered completed if the ninja manages to get out of the canyon. After several failed attempts Vasya started to doubt whether it is possible to complete the level at all. Help him answer the question. Input The first line contains two integers n and k (1 ≤ n, k ≤ 105) — the height of the canyon and the height of ninja's jump, correspondingly. The second line contains the description of the left wall — a string with the length of n characters. The i-th character represents the state of the i-th wall area: character "X" represents a dangerous area and character "-" represents a safe area. The third line describes the right wall in the same format. It is guaranteed that the first area of the left wall is not dangerous. Output Print "YES" (without the quotes) if the ninja can get out from the canyon, otherwise, print "NO" (without the quotes). Examples Input 7 3 ---X--X -X--XX- Output YES Input 6 2 --X-X- X--XX- Output NO Note In the first sample the ninja should first jump to the right wall, then go one meter down along the right wall, then jump to the left wall. The next jump can get the ninja from the canyon. In the second sample there's no way the ninja can get out of the canyon. Submitted Solution: ``` n,k = map(int,input().split()) l = input() r = input() data = [0, ' '+l,' '+r] dist = [[1000000]*100005 for _ in range(3)] visited = [[False]*100005 for _ in range(3)] dist[1][1]=0 visited[1][1]=True qx,qy = [1],[1] while qy: x,y = qx.pop(),qy.pop() if dist[x][y]>=y: continue if x==1: poss = [[1,y+1],[1,y-1],[2,y+k]] else: poss = [[2,y+1],[2,y-1],[1,y+k]] for i,e in enumerate(poss): newx,newy = e[0],e[1] if newy>n: print('YES') from sys import exit exit() if 0<newy<=n and not visited[newx][newy] and data[newx][newy]=='-': visited[newx][newy]=True dist[newx][newy]=dist[x][y]+1 qx=[newx]+qx qy=[newy]+qy print('NO') ``` Yes
6,904
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya plays a computer game with ninjas. At this stage Vasya's ninja should get out of a deep canyon. The canyon consists of two vertical parallel walls, their height is n meters. Let's imagine that we split these walls into 1 meter-long areas and number them with positive integers from 1 to n from bottom to top. Some areas are safe and the ninja can climb them. Others are spiky and ninja can't be there. Let's call such areas dangerous. Initially the ninja is on the lower area of the left wall. He can use each second to perform one of the following actions: * climb one area up; * climb one area down; * jump to the opposite wall. That gets the ninja to the area that is exactly k meters higher than the area he jumped from. More formally, if before the jump the ninja is located at area x of one wall, then after the jump he is located at area x + k of the other wall. If at some point of time the ninja tries to get to an area with a number larger than n, then we can assume that the ninja got out of the canyon. The canyon gets flooded and each second the water level raises one meter. Initially the water level is at the lower border of the first area. Ninja cannot be on the area covered by water. We can assume that the ninja and the water "move in turns" — first the ninja performs some action, then the water raises for one meter, then the ninja performs one more action and so on. The level is considered completed if the ninja manages to get out of the canyon. After several failed attempts Vasya started to doubt whether it is possible to complete the level at all. Help him answer the question. Input The first line contains two integers n and k (1 ≤ n, k ≤ 105) — the height of the canyon and the height of ninja's jump, correspondingly. The second line contains the description of the left wall — a string with the length of n characters. The i-th character represents the state of the i-th wall area: character "X" represents a dangerous area and character "-" represents a safe area. The third line describes the right wall in the same format. It is guaranteed that the first area of the left wall is not dangerous. Output Print "YES" (without the quotes) if the ninja can get out from the canyon, otherwise, print "NO" (without the quotes). Examples Input 7 3 ---X--X -X--XX- Output YES Input 6 2 --X-X- X--XX- Output NO Note In the first sample the ninja should first jump to the right wall, then go one meter down along the right wall, then jump to the left wall. The next jump can get the ninja from the canyon. In the second sample there's no way the ninja can get out of the canyon. Submitted Solution: ``` from sys import stdin, stdout from collections import deque n, k = map(int, stdin.readline().split()) maps = [] maps.append(list(stdin.readline() + '-')) maps.append(list(stdin.readline() + '-')) visit = [[0, 0] for i in range(n + 1)] visit[0][0] = 1 queue = deque() label = 0 queue.append((0, -1, 0))#ر‚ذ²ذ¾ذ¹ رƒر€ذ¾ذ²ذµذ½رŒ, رƒر€ذ¾ذ²ذµذ½رŒ ذ²ذ¾ذ´ر‹, ذ½ذ¾ذ¼ذµر€ رپر‚ذµذ½ر‹ while queue: mine, line, num = queue.popleft() if line >= mine: continue if mine + k >= n: label = 1 if mine + 1 < n and not visit[mine + 1][num] and maps[num][mine + 1] == '-': queue.append((mine + 1, line + 1, num)) visit[mine + 1][num] = 1 if mine and mine - line > 1 and not visit[mine - 1][num] and maps[num][mine - 1] == '-': queue.append((mine - 1, line + 1, num)) visit[mine - 1][num] = 1 if mine + k < n and not visit[mine + k][(num + 1) % 2] and maps[(num + 1) % 2][mine + k] == '-': queue.append((min(mine + k, n), line + 1, (num + 1) % 2)) visit[min(mine + k, n)][(num + 1) % 2] = 1 if label: stdout.write('YES') else: stdout.write('NO') # Made By Mostafa_Khaled ``` Yes
6,905
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya plays a computer game with ninjas. At this stage Vasya's ninja should get out of a deep canyon. The canyon consists of two vertical parallel walls, their height is n meters. Let's imagine that we split these walls into 1 meter-long areas and number them with positive integers from 1 to n from bottom to top. Some areas are safe and the ninja can climb them. Others are spiky and ninja can't be there. Let's call such areas dangerous. Initially the ninja is on the lower area of the left wall. He can use each second to perform one of the following actions: * climb one area up; * climb one area down; * jump to the opposite wall. That gets the ninja to the area that is exactly k meters higher than the area he jumped from. More formally, if before the jump the ninja is located at area x of one wall, then after the jump he is located at area x + k of the other wall. If at some point of time the ninja tries to get to an area with a number larger than n, then we can assume that the ninja got out of the canyon. The canyon gets flooded and each second the water level raises one meter. Initially the water level is at the lower border of the first area. Ninja cannot be on the area covered by water. We can assume that the ninja and the water "move in turns" — first the ninja performs some action, then the water raises for one meter, then the ninja performs one more action and so on. The level is considered completed if the ninja manages to get out of the canyon. After several failed attempts Vasya started to doubt whether it is possible to complete the level at all. Help him answer the question. Input The first line contains two integers n and k (1 ≤ n, k ≤ 105) — the height of the canyon and the height of ninja's jump, correspondingly. The second line contains the description of the left wall — a string with the length of n characters. The i-th character represents the state of the i-th wall area: character "X" represents a dangerous area and character "-" represents a safe area. The third line describes the right wall in the same format. It is guaranteed that the first area of the left wall is not dangerous. Output Print "YES" (without the quotes) if the ninja can get out from the canyon, otherwise, print "NO" (without the quotes). Examples Input 7 3 ---X--X -X--XX- Output YES Input 6 2 --X-X- X--XX- Output NO Note In the first sample the ninja should first jump to the right wall, then go one meter down along the right wall, then jump to the left wall. The next jump can get the ninja from the canyon. In the second sample there's no way the ninja can get out of the canyon. Submitted Solution: ``` left, right = (0, 1) undiscovered = 0 processed = 1 def climb_up(state): s = state[:] s[1] += 1 return s def climb_down(state): s = state[:] s[1] -= 1 return s def jump_to_other(state, k): s = state[:] s[1] += k if s[2] == left: s[2] = right else: s[2] = left return s def push_next_states(state, k, states): s = state[:] s[0] += 1 states.append(climb_up(s)) states.append(climb_down(s)) states.append(jump_to_other(s, k)) return states def solve(n, k, lwall, rwall): water = -1 layer = 0 wall = left state = [water, layer, wall] states = push_next_states(state, k, []) table = [[undiscovered]*n, [undiscovered]*n] while states != []: state = states.pop(-1) if n <= state[1]: return True if table[state[2]][state[1]] != processed: water = state[0] layer = state[1] if state[2] == left: wall = lwall else: wall = rwall if water < layer and wall[layer] == '-': push_next_states(state, k, states) table[state[2]][layer] = processed return False def main(): import sys n, k = [int(x) for x in sys.stdin.readline().split()] lwall = sys.stdin.readline().rstrip() rwall = sys.stdin.readline().rstrip() result = solve(n, k, lwall, rwall) if result: print("YES", end="") else: print("NO", end="") main() ``` No
6,906
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya plays a computer game with ninjas. At this stage Vasya's ninja should get out of a deep canyon. The canyon consists of two vertical parallel walls, their height is n meters. Let's imagine that we split these walls into 1 meter-long areas and number them with positive integers from 1 to n from bottom to top. Some areas are safe and the ninja can climb them. Others are spiky and ninja can't be there. Let's call such areas dangerous. Initially the ninja is on the lower area of the left wall. He can use each second to perform one of the following actions: * climb one area up; * climb one area down; * jump to the opposite wall. That gets the ninja to the area that is exactly k meters higher than the area he jumped from. More formally, if before the jump the ninja is located at area x of one wall, then after the jump he is located at area x + k of the other wall. If at some point of time the ninja tries to get to an area with a number larger than n, then we can assume that the ninja got out of the canyon. The canyon gets flooded and each second the water level raises one meter. Initially the water level is at the lower border of the first area. Ninja cannot be on the area covered by water. We can assume that the ninja and the water "move in turns" — first the ninja performs some action, then the water raises for one meter, then the ninja performs one more action and so on. The level is considered completed if the ninja manages to get out of the canyon. After several failed attempts Vasya started to doubt whether it is possible to complete the level at all. Help him answer the question. Input The first line contains two integers n and k (1 ≤ n, k ≤ 105) — the height of the canyon and the height of ninja's jump, correspondingly. The second line contains the description of the left wall — a string with the length of n characters. The i-th character represents the state of the i-th wall area: character "X" represents a dangerous area and character "-" represents a safe area. The third line describes the right wall in the same format. It is guaranteed that the first area of the left wall is not dangerous. Output Print "YES" (without the quotes) if the ninja can get out from the canyon, otherwise, print "NO" (without the quotes). Examples Input 7 3 ---X--X -X--XX- Output YES Input 6 2 --X-X- X--XX- Output NO Note In the first sample the ninja should first jump to the right wall, then go one meter down along the right wall, then jump to the left wall. The next jump can get the ninja from the canyon. In the second sample there's no way the ninja can get out of the canyon. Submitted Solution: ``` n, k = map(int, input().split()) lzid = input() dzid = input() zidovi = [lzid, dzid] q = [[-1, [False,0]]] #[koraci, [zid, visina]] izasao = 0 while len(q) != 0: trenutni = q.pop(0) korak = trenutni[0] pozicija = trenutni[1] tren_zid = pozicija[0] tren_visina = pozicija[1] print("Korak:", korak) print("pozicija:", pozicija) if pozicija[1] == n-1: print("YES") izasao = 1 break if tren_visina - 1 > korak+1: if zidovi[tren_zid][tren_visina-1] != 'X': q.append([korak+1, [tren_zid, tren_visina-1]]) if tren_visina + 1 > korak: if tren_visina + k <= n-1: if zidovi[tren_zid][tren_visina+1] != 'X': q.append([korak+1, [tren_zid, tren_visina+1]]) else: print("YES") izasao = 1 break if tren_visina + k > korak: if tren_visina + k <= n-1: if zidovi[not(tren_zid)][tren_visina+k] != 'X': q.append([korak+1, [not(tren_zid), tren_visina+k]]) else: print("YES") izasao = 1 if izasao == 0: print("NO") ``` No
6,907
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya plays a computer game with ninjas. At this stage Vasya's ninja should get out of a deep canyon. The canyon consists of two vertical parallel walls, their height is n meters. Let's imagine that we split these walls into 1 meter-long areas and number them with positive integers from 1 to n from bottom to top. Some areas are safe and the ninja can climb them. Others are spiky and ninja can't be there. Let's call such areas dangerous. Initially the ninja is on the lower area of the left wall. He can use each second to perform one of the following actions: * climb one area up; * climb one area down; * jump to the opposite wall. That gets the ninja to the area that is exactly k meters higher than the area he jumped from. More formally, if before the jump the ninja is located at area x of one wall, then after the jump he is located at area x + k of the other wall. If at some point of time the ninja tries to get to an area with a number larger than n, then we can assume that the ninja got out of the canyon. The canyon gets flooded and each second the water level raises one meter. Initially the water level is at the lower border of the first area. Ninja cannot be on the area covered by water. We can assume that the ninja and the water "move in turns" — first the ninja performs some action, then the water raises for one meter, then the ninja performs one more action and so on. The level is considered completed if the ninja manages to get out of the canyon. After several failed attempts Vasya started to doubt whether it is possible to complete the level at all. Help him answer the question. Input The first line contains two integers n and k (1 ≤ n, k ≤ 105) — the height of the canyon and the height of ninja's jump, correspondingly. The second line contains the description of the left wall — a string with the length of n characters. The i-th character represents the state of the i-th wall area: character "X" represents a dangerous area and character "-" represents a safe area. The third line describes the right wall in the same format. It is guaranteed that the first area of the left wall is not dangerous. Output Print "YES" (without the quotes) if the ninja can get out from the canyon, otherwise, print "NO" (without the quotes). Examples Input 7 3 ---X--X -X--XX- Output YES Input 6 2 --X-X- X--XX- Output NO Note In the first sample the ninja should first jump to the right wall, then go one meter down along the right wall, then jump to the left wall. The next jump can get the ninja from the canyon. In the second sample there's no way the ninja can get out of the canyon. Submitted Solution: ``` first_input = [] for i in input().split(): first_input.append(int(i)) walls=[] left_wall = [] for i in input(): left_wall.append(i) right_wall = [] for i in input(): right_wall.append(i) walls.append(left_wall) walls.append(right_wall) my_positions = (0,0) def my_function(position, walls, time): moves = [] if position[1]+1 < first_input[0] and walls[position[0]][position[1]+1] != 'X': moves.append(((position[0],position[1]+1), time+1)) elif position[1]+1 > first_input[0]: return "YES" if position[1] > 0 and walls[position[0]][position[1]-1] != 'X' and time<position[1]-1: moves.append(((position[0],position[1]-1), time+1)) if position[1]+first_input[1] < first_input[0] and walls[position[0]^1][position[1]+first_input[1]] != 'X': moves.append(((position[0]^1,position[1]+first_input[1]), time+1)) elif position[1]+first_input[1] > first_input[0]: return "YES" return moves frontier = [] frontier.append((my_positions,0)) output = "NO" while len(frontier)>0: if frontier[0][0][1]==first_input[0]-1: output="YES" break x = my_function(frontier[0][0], walls, frontier[0][1]) del frontier[0] if x == "YES": output=x break else: frontier.extend(x) print(output) ``` No
6,908
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya plays a computer game with ninjas. At this stage Vasya's ninja should get out of a deep canyon. The canyon consists of two vertical parallel walls, their height is n meters. Let's imagine that we split these walls into 1 meter-long areas and number them with positive integers from 1 to n from bottom to top. Some areas are safe and the ninja can climb them. Others are spiky and ninja can't be there. Let's call such areas dangerous. Initially the ninja is on the lower area of the left wall. He can use each second to perform one of the following actions: * climb one area up; * climb one area down; * jump to the opposite wall. That gets the ninja to the area that is exactly k meters higher than the area he jumped from. More formally, if before the jump the ninja is located at area x of one wall, then after the jump he is located at area x + k of the other wall. If at some point of time the ninja tries to get to an area with a number larger than n, then we can assume that the ninja got out of the canyon. The canyon gets flooded and each second the water level raises one meter. Initially the water level is at the lower border of the first area. Ninja cannot be on the area covered by water. We can assume that the ninja and the water "move in turns" — first the ninja performs some action, then the water raises for one meter, then the ninja performs one more action and so on. The level is considered completed if the ninja manages to get out of the canyon. After several failed attempts Vasya started to doubt whether it is possible to complete the level at all. Help him answer the question. Input The first line contains two integers n and k (1 ≤ n, k ≤ 105) — the height of the canyon and the height of ninja's jump, correspondingly. The second line contains the description of the left wall — a string with the length of n characters. The i-th character represents the state of the i-th wall area: character "X" represents a dangerous area and character "-" represents a safe area. The third line describes the right wall in the same format. It is guaranteed that the first area of the left wall is not dangerous. Output Print "YES" (without the quotes) if the ninja can get out from the canyon, otherwise, print "NO" (without the quotes). Examples Input 7 3 ---X--X -X--XX- Output YES Input 6 2 --X-X- X--XX- Output NO Note In the first sample the ninja should first jump to the right wall, then go one meter down along the right wall, then jump to the left wall. The next jump can get the ninja from the canyon. In the second sample there's no way the ninja can get out of the canyon. Submitted Solution: ``` v, st = map(int, input().split()) l = list(input()) p = list(input()) bool_l = [False for i in range(v)] bool_p = [False for i in range(v)] def step(poz, pared): if (poz >= v): return True else: if (poz < 0): return False if (pared == 0 and l[poz] == 'X'): return False if (pared == 1 and p[poz] == 'X'): return False if (pared == 0 and bool_l[poz] == True): return False if (pared == 1 and bool_p[poz] == True): return False if (pared == 0): bool_l[poz] = True else: bool_p[poz] = True if (poz >= v): return True else: return (step(poz+1, pared) or step(poz-1,pared) or step(poz+st, 1-pared)) if step(0,0) == True: print("YES") else: print("NO") ``` No
6,909
Provide tags and a correct Python 3 solution for this coding contest problem. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Tags: implementation, sortings Correct Solution: ``` n=int(input()) l=[int(i) for i in input().split()] l1=sorted(l) c=0 for i in range(n): if(l1[i]!=l[i]): c+=1 if(c>2): print("NO") else: print("YES") ```
6,910
Provide tags and a correct Python 3 solution for this coding contest problem. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Tags: implementation, sortings Correct Solution: ``` #------------------------template--------------------------# import os import sys from math import * from collections import * from fractions import * from bisect import * from heapq import* from io import BytesIO, IOBase def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") def value():return tuple(map(int,input().split())) def array():return [int(i) for i in input().split()] def Int():return int(input()) def Str():return input() def arrayS():return [i for i in input().split()] #-------------------------code---------------------------# #vsInput() n=Int() a=array() sorted_a=sorted(a) disturbed=[] for i in range(n): if(a[i]!=sorted_a[i]): disturbed.append(i) if(len(disturbed)<=2): print("YES") else: print("NO") ```
6,911
Provide tags and a correct Python 3 solution for this coding contest problem. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Tags: implementation, sortings Correct Solution: ``` #https://codeforces.com/contest/221/problem/C n=int(input()) a=list(map(int,input().split(' '))) b=[] for i in range(n): b.append(a[i]) b.sort() q=0 R=[] bhul=True for i in range(n): if a[i]!=b[i]: if q>=2: print('NO') bhul=False break else: q+=1 R.append(i) if bhul: if q==0: print('YES') elif q==2: print('YES') else: print('NO') ```
6,912
Provide tags and a correct Python 3 solution for this coding contest problem. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Tags: implementation, sortings Correct Solution: ``` n = int(input()) a = list(map(int, input().split())) b = sorted(a) ans = 0 for i in range(n): if not a[i] == b[i]: ans += 1 if ans <= 2: print("YES") else: print("NO") ```
6,913
Provide tags and a correct Python 3 solution for this coding contest problem. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Tags: implementation, sortings Correct Solution: ``` from collections import Counter n=int(input()) a=list(map(int,input().split())) b=a.copy() a.sort() for i in range(n): a[i]=abs(a[i]-b[i]) k=Counter(a) if(k[0]>=n-2): print("YES") else: print("NO") ```
6,914
Provide tags and a correct Python 3 solution for this coding contest problem. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Tags: implementation, sortings Correct Solution: ``` n = int(input()) a = list(map(int, input().split())) b = sorted(a) res = 0 for i in range(n): if a[i] != b[i]: res += 1 print('YES' if res <= 2 else 'NO') ```
6,915
Provide tags and a correct Python 3 solution for this coding contest problem. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Tags: implementation, sortings Correct Solution: ``` n=int(input()) from itertools import permutations as pem List=list(map(int, input().split())) newlist=sorted(List) count=0 for i in range(n): if newlist[i]!=List[i]: count+=1 if count<=2: print("YES") else: print("NO") ```
6,916
Provide tags and a correct Python 3 solution for this coding contest problem. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Tags: implementation, sortings Correct Solution: ``` import sys import math import itertools import functools import collections def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return list(map(int, input().split())) def lcm(a, b): return abs(a * b) // math.gcd(a, b) def wr(arr): return ' '.join(map(str, arr)) def revn(n): return str(n)[::-1] def dd(): return collections.defaultdict(int) def ddl(): return collections.defaultdict(list) def sieve(n): if n < 2: return list() prime = [True for _ in range(n + 1)] p = 3 while p * p <= n: if prime[p]: for i in range(p * 2, n + 1, p): prime[i] = False p += 2 r = [2] for p in range(3, n + 1, 2): if prime[p]: r.append(p) return r def divs(n, start=1): r = [] for i in range(start, int(math.sqrt(n) + 1)): if (n % i == 0): if (n / i == i): r.append(i) else: r.extend([i, n // i]) return r def divn(n, primes): divs_number = 1 for i in primes: if n == 1: return divs_number t = 1 while n % i == 0: t += 1 n //= i divs_number *= t def prime(n): if n == 2: return True if n % 2 == 0 or n <= 1: return False sqr = int(math.sqrt(n)) + 1 for d in range(3, sqr, 2): if n % d == 0: return False return True def convn(number, base): newnumber = 0 while number > 0: newnumber += number % base number //= base return newnumber def cdiv(n, k): return n // k + (n % k != 0) n = ii() a = li() b = sorted(a) ans = 0 for i in range(n): if a[i] != b[i]: ans += 1 if ans == 0 or ans == 2: print('YES') else: print('NO') ```
6,917
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` def issort(l): return all(l[i] <= l[i+1] for i in range(len(l)-1)) sa = int(input()) array = list(map(int, input().split(' '))) array2 = array[:] array2.sort() count = 0 mislist=[] for i in range(sa): if array[i] != array2[i]: count += 1 if count <= 2: mislist.append(i) if count > 2: print("NO") else: if mislist == []: print("YES") elif len(mislist) == 1: if issort(array): print("YES") else: print("NO") else: array[mislist[0]], array[mislist[1]] = array[mislist[1]], array[mislist[0]] if issort(array): print("YES") else: print("NO") ``` Yes
6,918
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` import sys input = sys.stdin.readline n = int(input()) a = list(map(int,input().split())) b = sorted(a) ans = 0 for i in range(n): if a[i] != b[i]: ans += 1 print('YES' if ans <= 2 else 'NO') ``` Yes
6,919
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` def arr_inp(n): if n == 1: return [int(x) for x in stdin.readline().split()] elif n == 2: return [float(x) for x in stdin.readline().split()] else: return [str(x) for x in stdin.readline().split()] from sys import stdin n, a = int(input()), arr_inp(1) a1, c, ix = sorted(a.copy()), 0, 0 for i in range(n): if a[i] != a1[i]: if c == 0: c += 1 ix = i elif c > 2: exit(print('NO')) else: if a1[ix] == a[i]: c += 1 else: exit(print('NO')) print('YES') ``` Yes
6,920
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` n=int(input()) l=list(map(int,input().split())) d=sorted(l) c=0 for i in range(n): if(l[i]!=d[i]): c=c+1 if(c>2): print("NO") else: print("YES") ``` Yes
6,921
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` import sys input = sys.stdin.readline n = int(input()) a = list(map(int,input().split())) ind1 = a.index(max(a)) ind2 = a.index(min(a)) k = 0 f = False for i in range(n - 1): if a[i] > a[i + 1]: k += 1 if k == 2: f = True break if f or ind1 != 0 and ind2 != n - 1: print('NO') else: print('YES') ``` No
6,922
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` #https://codeforces.com/contest/221/problem/C n=int(input()) a=list(map(int,input().split(' '))) b=[] for i in range(n): b.append(a[i]) b.sort() q=0 R=[] bhul=True for i in range(n): if a[i]!=b[i]: if q>=2: print('NO') bhul=False break else: q+=1 R.append(i) if bhul: if q!=2: print('NO') else: print('YES') ``` No
6,923
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` import sys input = sys.stdin.readline n = int(input()) a = list(map(int,input().split())) d = {} for i in range(n): if a[i] in d: d[a[i]].add(i) else: d[a[i]] = {i} ans = 0 b = a[:] a.sort() cur = set() for i in range(n): if i not in d[a[i]]: cur.add(b[i]) cur.add(a[i]) print('YES' if len(cur) <= 2 else 'NO') ``` No
6,924
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` from collections import Counter n=int(input()) a=list(map(int,input().split())) b=a.copy() a.sort() for i in range(n): a[i]=abs(a[i]-b[i]) k=Counter(a) print(k[0]) if(k[0]>=n-2): print("YES") else: print("NO") ``` No
6,925
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation. For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a1, a2, ..., an. He also wrote a square matrix b of size n × n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as bij) equals: * the "bitwise AND" of numbers ai and aj (that is, bij = ai & aj), if i ≠ j; * -1, if i = j. Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly. Help Polycarpus, given matrix b, restore the sequence of numbers a1, a2, ..., an, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109. Input The first line contains a single integer n (1 ≤ n ≤ 100) — the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix bij. It is guaranteed, that for all i (1 ≤ i ≤ n) the following condition fulfills: bii = -1. It is guaranteed that for all i, j (1 ≤ i, j ≤ n; i ≠ j) the following condition fulfills: 0 ≤ bij ≤ 109, bij = bji. Output Print n non-negative integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces. It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them. Examples Input 1 -1 Output 0 Input 3 -1 18 0 18 -1 0 0 0 -1 Output 18 18 0 Input 4 -1 128 128 128 128 -1 148 160 128 148 -1 128 128 160 128 -1 Output 128 180 148 160 Note If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation. Tags: constructive algorithms, greedy Correct Solution: ``` n = int(input()) if (n == 1) : print(0) else : m = [[0] * n] * n a = [int(0)] * n for i in range(0, n) : m[i] = input().split() a[i] = int(m[i][(i + 1) % n]) for j in range(0, n) : if (j != i) : a[i] = a[i] | int(m[i][j]) for i in range(0, n) : print(a[i], end = ' ') ```
6,926
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation. For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a1, a2, ..., an. He also wrote a square matrix b of size n × n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as bij) equals: * the "bitwise AND" of numbers ai and aj (that is, bij = ai & aj), if i ≠ j; * -1, if i = j. Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly. Help Polycarpus, given matrix b, restore the sequence of numbers a1, a2, ..., an, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109. Input The first line contains a single integer n (1 ≤ n ≤ 100) — the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix bij. It is guaranteed, that for all i (1 ≤ i ≤ n) the following condition fulfills: bii = -1. It is guaranteed that for all i, j (1 ≤ i, j ≤ n; i ≠ j) the following condition fulfills: 0 ≤ bij ≤ 109, bij = bji. Output Print n non-negative integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces. It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them. Examples Input 1 -1 Output 0 Input 3 -1 18 0 18 -1 0 0 0 -1 Output 18 18 0 Input 4 -1 128 128 128 128 -1 148 160 128 148 -1 128 128 160 128 -1 Output 128 180 148 160 Note If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation. Tags: constructive algorithms, greedy Correct Solution: ``` n = int(input()) a = [0]*n for i in range(n): for x in map(int, input().split()): if x!=-1: a[i]|=x print(*a) ```
6,927
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation. For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a1, a2, ..., an. He also wrote a square matrix b of size n × n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as bij) equals: * the "bitwise AND" of numbers ai and aj (that is, bij = ai & aj), if i ≠ j; * -1, if i = j. Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly. Help Polycarpus, given matrix b, restore the sequence of numbers a1, a2, ..., an, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109. Input The first line contains a single integer n (1 ≤ n ≤ 100) — the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix bij. It is guaranteed, that for all i (1 ≤ i ≤ n) the following condition fulfills: bii = -1. It is guaranteed that for all i, j (1 ≤ i, j ≤ n; i ≠ j) the following condition fulfills: 0 ≤ bij ≤ 109, bij = bji. Output Print n non-negative integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces. It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them. Examples Input 1 -1 Output 0 Input 3 -1 18 0 18 -1 0 0 0 -1 Output 18 18 0 Input 4 -1 128 128 128 128 -1 148 160 128 148 -1 128 128 160 128 -1 Output 128 180 148 160 Note If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation. Tags: constructive algorithms, greedy Correct Solution: ``` n=int(input()) A=[0]*n ans=[0]*n for i in range(n): A[i]=list(map(int,input().split())) for j in range(n): if(j==i):continue ans[i]|=A[i][j] for i in range(n): print(ans[i],' ',end='') ```
6,928
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation. For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a1, a2, ..., an. He also wrote a square matrix b of size n × n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as bij) equals: * the "bitwise AND" of numbers ai and aj (that is, bij = ai & aj), if i ≠ j; * -1, if i = j. Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly. Help Polycarpus, given matrix b, restore the sequence of numbers a1, a2, ..., an, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109. Input The first line contains a single integer n (1 ≤ n ≤ 100) — the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix bij. It is guaranteed, that for all i (1 ≤ i ≤ n) the following condition fulfills: bii = -1. It is guaranteed that for all i, j (1 ≤ i, j ≤ n; i ≠ j) the following condition fulfills: 0 ≤ bij ≤ 109, bij = bji. Output Print n non-negative integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces. It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them. Examples Input 1 -1 Output 0 Input 3 -1 18 0 18 -1 0 0 0 -1 Output 18 18 0 Input 4 -1 128 128 128 128 -1 148 160 128 148 -1 128 128 160 128 -1 Output 128 180 148 160 Note If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation. Tags: constructive algorithms, greedy Correct Solution: ``` n = int(input()) if n > 1: p = [0] * n r = format(n - 1, 'b')[:: -1] l = len(r) - 1 for i in range(n): t = list(map(int, input().split())) t.pop(i) s = 0 for j in range(l): if r[j] == '1': s |= t.pop() t = [t[k] | t[k + 1] for k in range(0, len(t), 2)] p[i] = s | t[0] print(' '.join(map(str, p))) else: print(0) ```
6,929
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation. For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a1, a2, ..., an. He also wrote a square matrix b of size n × n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as bij) equals: * the "bitwise AND" of numbers ai and aj (that is, bij = ai & aj), if i ≠ j; * -1, if i = j. Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly. Help Polycarpus, given matrix b, restore the sequence of numbers a1, a2, ..., an, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109. Input The first line contains a single integer n (1 ≤ n ≤ 100) — the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix bij. It is guaranteed, that for all i (1 ≤ i ≤ n) the following condition fulfills: bii = -1. It is guaranteed that for all i, j (1 ≤ i, j ≤ n; i ≠ j) the following condition fulfills: 0 ≤ bij ≤ 109, bij = bji. Output Print n non-negative integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces. It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them. Examples Input 1 -1 Output 0 Input 3 -1 18 0 18 -1 0 0 0 -1 Output 18 18 0 Input 4 -1 128 128 128 128 -1 148 160 128 148 -1 128 128 160 128 -1 Output 128 180 148 160 Note If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation. Tags: constructive algorithms, greedy Correct Solution: ``` n=int(input()) a=[] for i in range(n): a.append(list(map(int,input().split()))) ans = [0]*n for i in range(n): for j in range(n): if j!=i: ans[i] |= a[i][j] print(ans[i],end = ' ') ```
6,930
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation. For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a1, a2, ..., an. He also wrote a square matrix b of size n × n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as bij) equals: * the "bitwise AND" of numbers ai and aj (that is, bij = ai & aj), if i ≠ j; * -1, if i = j. Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly. Help Polycarpus, given matrix b, restore the sequence of numbers a1, a2, ..., an, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109. Input The first line contains a single integer n (1 ≤ n ≤ 100) — the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix bij. It is guaranteed, that for all i (1 ≤ i ≤ n) the following condition fulfills: bii = -1. It is guaranteed that for all i, j (1 ≤ i, j ≤ n; i ≠ j) the following condition fulfills: 0 ≤ bij ≤ 109, bij = bji. Output Print n non-negative integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces. It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them. Examples Input 1 -1 Output 0 Input 3 -1 18 0 18 -1 0 0 0 -1 Output 18 18 0 Input 4 -1 128 128 128 128 -1 148 160 128 148 -1 128 128 160 128 -1 Output 128 180 148 160 Note If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation. Tags: constructive algorithms, greedy Correct Solution: ``` n = int(input()) p = [0] * n for i in range(n): t = list(map(int, input().split())) t.pop(i) s = 0 for j in t: s |= j p[i] = s print(' '.join(map(str, p))) ```
6,931
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation. For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a1, a2, ..., an. He also wrote a square matrix b of size n × n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as bij) equals: * the "bitwise AND" of numbers ai and aj (that is, bij = ai & aj), if i ≠ j; * -1, if i = j. Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly. Help Polycarpus, given matrix b, restore the sequence of numbers a1, a2, ..., an, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109. Input The first line contains a single integer n (1 ≤ n ≤ 100) — the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix bij. It is guaranteed, that for all i (1 ≤ i ≤ n) the following condition fulfills: bii = -1. It is guaranteed that for all i, j (1 ≤ i, j ≤ n; i ≠ j) the following condition fulfills: 0 ≤ bij ≤ 109, bij = bji. Output Print n non-negative integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces. It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them. Examples Input 1 -1 Output 0 Input 3 -1 18 0 18 -1 0 0 0 -1 Output 18 18 0 Input 4 -1 128 128 128 128 -1 148 160 128 148 -1 128 128 160 128 -1 Output 128 180 148 160 Note If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation. Submitted Solution: ``` n = int(input()) if (n == 1) : print(0) else : m = [[0] * n] * n a = [int(0)] * n for i in range(0, n) : m[i] = input().split() a[i] = int(m[i][(i + 1) % n]) for j in range(0, n) : if (j != i) : a[i] = a[i] | int(m[i][j]) print(a) ``` No
6,932
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation. For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a1, a2, ..., an. He also wrote a square matrix b of size n × n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as bij) equals: * the "bitwise AND" of numbers ai and aj (that is, bij = ai & aj), if i ≠ j; * -1, if i = j. Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly. Help Polycarpus, given matrix b, restore the sequence of numbers a1, a2, ..., an, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109. Input The first line contains a single integer n (1 ≤ n ≤ 100) — the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix bij. It is guaranteed, that for all i (1 ≤ i ≤ n) the following condition fulfills: bii = -1. It is guaranteed that for all i, j (1 ≤ i, j ≤ n; i ≠ j) the following condition fulfills: 0 ≤ bij ≤ 109, bij = bji. Output Print n non-negative integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces. It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them. Examples Input 1 -1 Output 0 Input 3 -1 18 0 18 -1 0 0 0 -1 Output 18 18 0 Input 4 -1 128 128 128 128 -1 148 160 128 148 -1 128 128 160 128 -1 Output 128 180 148 160 Note If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation. Submitted Solution: ``` n = int(input()) m = [[0] * n] * n a = [int(0)] * n for i in range(0, n) : m[i] = input().split() a[i] = int(m[i][(i + 1) % n]) for j in range(0, n) : if (j != i) : a[i] = a[i] | int(m[i][j]) print(a) ``` No
6,933
Provide tags and a correct Python 3 solution for this coding contest problem. Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: 1. thread x is not updated (it has no new messages); 2. the list order 1, 2, ..., n changes to a1, a2, ..., an. Input The first line of input contains an integer n, the number of threads (1 ≤ n ≤ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 ≤ ai ≤ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct. Output Output a single integer — the number of threads that surely contain a new message. Examples Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 Note In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages. Tags: data structures, greedy, implementation Correct Solution: ``` t=int(input()) arr=list(map(int,input().split())) i=t-1 while( i>0 and arr[i] > arr[i-1] ): i-=1 print(i) ```
6,934
Provide tags and a correct Python 3 solution for this coding contest problem. Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: 1. thread x is not updated (it has no new messages); 2. the list order 1, 2, ..., n changes to a1, a2, ..., an. Input The first line of input contains an integer n, the number of threads (1 ≤ n ≤ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 ≤ ai ≤ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct. Output Output a single integer — the number of threads that surely contain a new message. Examples Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 Note In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages. Tags: data structures, greedy, implementation Correct Solution: ``` import sys n = int(sys.stdin.readline()) l = list(map(int, sys.stdin.readline().split()))[::-1] ans = 0 acc = n pre = 9999999999999 for i in range(n): if l[i] < pre: ans += 1 pre = l[i] else: break print(n-ans) ```
6,935
Provide tags and a correct Python 3 solution for this coding contest problem. Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: 1. thread x is not updated (it has no new messages); 2. the list order 1, 2, ..., n changes to a1, a2, ..., an. Input The first line of input contains an integer n, the number of threads (1 ≤ n ≤ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 ≤ ai ≤ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct. Output Output a single integer — the number of threads that surely contain a new message. Examples Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 Note In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages. Tags: data structures, greedy, implementation Correct Solution: ``` # ANDRE CHEKER BURIHAN RA 194071 noth = int(input()) oldpos = [int(num) for num in input().split()] newpos = [i+1 for i in range(noth)] counter = 0 if oldpos != newpos: for i in reversed(range(1, noth)): if oldpos[i-1] > oldpos[i]: counter = i break print(counter) ```
6,936
Provide tags and a correct Python 3 solution for this coding contest problem. Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: 1. thread x is not updated (it has no new messages); 2. the list order 1, 2, ..., n changes to a1, a2, ..., an. Input The first line of input contains an integer n, the number of threads (1 ≤ n ≤ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 ≤ ai ≤ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct. Output Output a single integer — the number of threads that surely contain a new message. Examples Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 Note In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages. Tags: data structures, greedy, implementation Correct Solution: ``` n = int(input()) a = (str(input())).split(' ') ma=0 output = 0 for i in range(n-1): if int(a[i])<int(a[i+1]): ma+=1 else: output+=1+ma ma=0 print(output) ```
6,937
Provide tags and a correct Python 3 solution for this coding contest problem. Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: 1. thread x is not updated (it has no new messages); 2. the list order 1, 2, ..., n changes to a1, a2, ..., an. Input The first line of input contains an integer n, the number of threads (1 ≤ n ≤ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 ≤ ai ≤ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct. Output Output a single integer — the number of threads that surely contain a new message. Examples Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 Note In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages. Tags: data structures, greedy, implementation Correct Solution: ``` n=input() arr=input().split(" ") asc=1 x=int(arr[0]) for i in range(1,int(n)): if int(arr[i])>x: asc+=1 else: asc=1 x=int(arr[i]) print(int(n)-asc) ```
6,938
Provide tags and a correct Python 3 solution for this coding contest problem. Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: 1. thread x is not updated (it has no new messages); 2. the list order 1, 2, ..., n changes to a1, a2, ..., an. Input The first line of input contains an integer n, the number of threads (1 ≤ n ≤ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 ≤ ai ≤ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct. Output Output a single integer — the number of threads that surely contain a new message. Examples Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 Note In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages. Tags: data structures, greedy, implementation Correct Solution: ``` if __name__ == '__main__': n = int(input()) q = (list(map(int, input().split()))) index = 0 for i in range(n-1, 0, -1): if(q[i-1] > q[i]): index = i break print(index) ```
6,939
Provide tags and a correct Python 3 solution for this coding contest problem. Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: 1. thread x is not updated (it has no new messages); 2. the list order 1, 2, ..., n changes to a1, a2, ..., an. Input The first line of input contains an integer n, the number of threads (1 ≤ n ≤ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 ≤ ai ≤ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct. Output Output a single integer — the number of threads that surely contain a new message. Examples Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 Note In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages. Tags: data structures, greedy, implementation Correct Solution: ``` n, ar = int(input()), [int(x) for x in input().split()][::-1] ans = 0 for i in range(1, n): if ar[i] > ar[i - 1]: ans = n - i break print(ans) ```
6,940
Provide tags and a correct Python 3 solution for this coding contest problem. Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: 1. thread x is not updated (it has no new messages); 2. the list order 1, 2, ..., n changes to a1, a2, ..., an. Input The first line of input contains an integer n, the number of threads (1 ≤ n ≤ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 ≤ ai ≤ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct. Output Output a single integer — the number of threads that surely contain a new message. Examples Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 Note In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages. Tags: data structures, greedy, implementation Correct Solution: ``` num = int(input()) arr = list(map(int,input().split())) i = num-1 while arr[i] > arr[i-1]: i -= 1 print(i) ```
6,941
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: 1. thread x is not updated (it has no new messages); 2. the list order 1, 2, ..., n changes to a1, a2, ..., an. Input The first line of input contains an integer n, the number of threads (1 ≤ n ≤ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 ≤ ai ≤ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct. Output Output a single integer — the number of threads that surely contain a new message. Examples Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 Note In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages. Submitted Solution: ``` n = int(input()) aux = input().split() first = int(aux[0]) flag = 1 for i in range(1,n): if int(aux[i]) < first: flag=1 else: flag+=1 first = int(aux[i]) print(n-flag) ``` Yes
6,942
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: 1. thread x is not updated (it has no new messages); 2. the list order 1, 2, ..., n changes to a1, a2, ..., an. Input The first line of input contains an integer n, the number of threads (1 ≤ n ≤ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 ≤ ai ≤ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct. Output Output a single integer — the number of threads that surely contain a new message. Examples Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 Note In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages. Submitted Solution: ``` n= int(input()) lista = input().split() cont = 0 i = n-1 if n == 1: print("0") else: while i > 0: if int(lista[i]) > int(lista[i-1]): cont += 1 i -= 1 if i == 0: cont+= 1 else: cont += 1 break print(n-cont) ``` Yes
6,943
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: 1. thread x is not updated (it has no new messages); 2. the list order 1, 2, ..., n changes to a1, a2, ..., an. Input The first line of input contains an integer n, the number of threads (1 ≤ n ≤ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 ≤ ai ≤ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct. Output Output a single integer — the number of threads that surely contain a new message. Examples Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 Note In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages. Submitted Solution: ``` # from debug import * import sys; input = sys.stdin.readline from collections import deque, defaultdict from math import log10, ceil, factorial as F I = lambda : int(input()) L = lambda : list(map(int, input().split())) T = lambda : map(int, input().split()) n = I() lis = L() i = n-1 while i>0 and lis[i-1] < lis[i]: i-=1 print(i) ``` Yes
6,944
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: 1. thread x is not updated (it has no new messages); 2. the list order 1, 2, ..., n changes to a1, a2, ..., an. Input The first line of input contains an integer n, the number of threads (1 ≤ n ≤ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 ≤ ai ≤ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct. Output Output a single integer — the number of threads that surely contain a new message. Examples Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 Note In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages. Submitted Solution: ``` n=int(input("")) a=[int(x) for x in input("").split(' ')] output = 0 cache = 0 for x in range(n-1): if a[x] > a[x+1]: output+=1+cache cache=0 else: cache+=1 print(output) ``` Yes
6,945
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: 1. thread x is not updated (it has no new messages); 2. the list order 1, 2, ..., n changes to a1, a2, ..., an. Input The first line of input contains an integer n, the number of threads (1 ≤ n ≤ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 ≤ ai ≤ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct. Output Output a single integer — the number of threads that surely contain a new message. Examples Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 Note In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages. Submitted Solution: ``` n = int(input()) inStr = input() pos = [int(x) for x in inStr.split()] min = n ans = 0 for i in range(n-1, -1, -1): if pos[i]>min: ans = ans + 1 elif pos[i]<min: min = pos[i] print(ans) ``` No
6,946
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: 1. thread x is not updated (it has no new messages); 2. the list order 1, 2, ..., n changes to a1, a2, ..., an. Input The first line of input contains an integer n, the number of threads (1 ≤ n ≤ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 ≤ ai ≤ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct. Output Output a single integer — the number of threads that surely contain a new message. Examples Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 Note In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages. Submitted Solution: ``` int(input()) l = [int(x) for x in input().split()] l.reverse() cur = 1000000 ans = 0 for x in l: if x > cur: ans += 1 else: cur = x print(ans) ``` No
6,947
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: 1. thread x is not updated (it has no new messages); 2. the list order 1, 2, ..., n changes to a1, a2, ..., an. Input The first line of input contains an integer n, the number of threads (1 ≤ n ≤ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 ≤ ai ≤ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct. Output Output a single integer — the number of threads that surely contain a new message. Examples Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 Note In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages. Submitted Solution: ``` N = int(input()) X = list(map(int, input().split())) Index = N-1 for i in range(N-1 , 1, -1): if X[i] <X[i-1]: print(i) exit() print(0) # Hope the best for Ravens member ``` No
6,948
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: 1. thread x is not updated (it has no new messages); 2. the list order 1, 2, ..., n changes to a1, a2, ..., an. Input The first line of input contains an integer n, the number of threads (1 ≤ n ≤ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 ≤ ai ≤ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct. Output Output a single integer — the number of threads that surely contain a new message. Examples Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 Note In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages. Submitted Solution: ``` def main(): n = int(input()) lista = [int(i) for i in input().split(' ')] id_start = lista.index(1) out = 0 org = sorted(lista[id_start:]) for i in range(id_start, n): if lista[i:] == org[i-id_start:]: out = i break print(i) if '__main__' == __name__: main() ``` No
6,949
Provide tags and a correct Python 3 solution for this coding contest problem. It has been noted that if some ants are put in the junctions of the graphene integer lattice then they will act in the following fashion: every minute at each junction (x, y) containing at least four ants a group of four ants will be formed, and these four ants will scatter to the neighbouring junctions (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) — one ant in each direction. No other ant movements will happen. Ants never interfere with each other. Scientists have put a colony of n ants into the junction (0, 0) and now they wish to know how many ants will there be at some given junctions, when the movement of the ants stops. Input First input line contains integers n (0 ≤ n ≤ 30000) and t (1 ≤ t ≤ 50000), where n is the number of ants in the colony and t is the number of queries. Each of the next t lines contains coordinates of a query junction: integers xi, yi ( - 109 ≤ xi, yi ≤ 109). Queries may coincide. It is guaranteed that there will be a certain moment of time when no possible movements can happen (in other words, the process will eventually end). Output Print t integers, one per line — the number of ants at the corresponding junctions when the movement of the ants stops. Examples Input 1 3 0 1 0 0 0 -1 Output 0 1 0 Input 6 5 0 -2 0 -1 0 0 0 1 0 2 Output 0 1 2 1 0 Note In the first sample the colony consists of the one ant, so nothing happens at all. In the second sample the colony consists of 6 ants. At the first minute 4 ants scatter from (0, 0) to the neighbouring junctions. After that the process stops. Tags: brute force, implementation Correct Solution: ``` from sys import * f = lambda: map(int, stdin.readline().split()) n, t = f() m = 65 r = range(m) p = [[0] * m for i in r] p[1][0] = n // 4 p[0][0] = n % 4 q = k = 1 while q: k += 1 q = 0 for x in r[1:k]: for y in r[:x + 1]: if p[x][y] < 4: continue q = 1 d = p[x][y] // 4 p[x][y] %= 4 p[x + 1][y] += d if x > y: if x > y + 1: p[x][y + 1] += d p[x - 1][y] += d else: p[x][x] += 2 * d if y: p[y][y] += 2 * d else: p[x][y] += d if y: p[x][y - 1] += d if y > 1 else 2 * d s = [] for j in range(t): x, y = f() x, y = abs(x), abs(y) if x < y: x, y = y, x s.append(p[x][y] if x < m else 0) stdout.write('\n'.join(map(str, s))) ```
6,950
Provide tags and a correct Python 3 solution for this coding contest problem. It has been noted that if some ants are put in the junctions of the graphene integer lattice then they will act in the following fashion: every minute at each junction (x, y) containing at least four ants a group of four ants will be formed, and these four ants will scatter to the neighbouring junctions (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) — one ant in each direction. No other ant movements will happen. Ants never interfere with each other. Scientists have put a colony of n ants into the junction (0, 0) and now they wish to know how many ants will there be at some given junctions, when the movement of the ants stops. Input First input line contains integers n (0 ≤ n ≤ 30000) and t (1 ≤ t ≤ 50000), where n is the number of ants in the colony and t is the number of queries. Each of the next t lines contains coordinates of a query junction: integers xi, yi ( - 109 ≤ xi, yi ≤ 109). Queries may coincide. It is guaranteed that there will be a certain moment of time when no possible movements can happen (in other words, the process will eventually end). Output Print t integers, one per line — the number of ants at the corresponding junctions when the movement of the ants stops. Examples Input 1 3 0 1 0 0 0 -1 Output 0 1 0 Input 6 5 0 -2 0 -1 0 0 0 1 0 2 Output 0 1 2 1 0 Note In the first sample the colony consists of the one ant, so nothing happens at all. In the second sample the colony consists of 6 ants. At the first minute 4 ants scatter from (0, 0) to the neighbouring junctions. After that the process stops. Tags: brute force, implementation Correct Solution: ``` from sys import * f = lambda: map(int, stdin.readline().split()) n, t = f() m = 65 r = range(1, m) p = [[0] * m for i in range(m)] p[1][0] = n // 4 p[0][0] = n % 4 s = k = 1 while s: s = 0 for x in r[:k]: if p[x][0] > 3: s = 1 d = p[x][0] // 4 p[x][0] %= 4 p[x + 1][0] += d p[x][1] += d if x != 1: p[x - 1][0] += d else: p[1][1] += d p[1][0] += d for y in r[:x - 1]: if p[x][y] > 3: s = 1 d = p[x][y] // 4 p[x][y] %= 4 p[x + 1][y] += d p[x - 1][y] += d p[x][y + 1] += d p[x][y - 1] += d if x == y + 1: p[x][x] += d p[y][y] += d if y == 1: p[x][0] += d if p[x][x] > 3: s = 1 d = p[x][x] // 4 p[x][x] %= 4 p[x + 1][x] += d p[x][x - 1] += d if x == 1: p[1][0] += d k += 1 s = [] for j in range(t): x, y = f() x, y = abs(x), abs(y) if x < y: x, y = y, x s.append(p[x][y] if x < m else 0) stdout.write('\n'.join(map(str, s))) ```
6,951
Provide tags and a correct Python 3 solution for this coding contest problem. It has been noted that if some ants are put in the junctions of the graphene integer lattice then they will act in the following fashion: every minute at each junction (x, y) containing at least four ants a group of four ants will be formed, and these four ants will scatter to the neighbouring junctions (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) — one ant in each direction. No other ant movements will happen. Ants never interfere with each other. Scientists have put a colony of n ants into the junction (0, 0) and now they wish to know how many ants will there be at some given junctions, when the movement of the ants stops. Input First input line contains integers n (0 ≤ n ≤ 30000) and t (1 ≤ t ≤ 50000), where n is the number of ants in the colony and t is the number of queries. Each of the next t lines contains coordinates of a query junction: integers xi, yi ( - 109 ≤ xi, yi ≤ 109). Queries may coincide. It is guaranteed that there will be a certain moment of time when no possible movements can happen (in other words, the process will eventually end). Output Print t integers, one per line — the number of ants at the corresponding junctions when the movement of the ants stops. Examples Input 1 3 0 1 0 0 0 -1 Output 0 1 0 Input 6 5 0 -2 0 -1 0 0 0 1 0 2 Output 0 1 2 1 0 Note In the first sample the colony consists of the one ant, so nothing happens at all. In the second sample the colony consists of 6 ants. At the first minute 4 ants scatter from (0, 0) to the neighbouring junctions. After that the process stops. Tags: brute force, implementation Correct Solution: ``` from sys import * f = lambda: map(int, stdin.readline().split()) n, t = f() m = 65 r = range(m) p = [[0] * m for i in r] p[1][0] = n // 4 p[0][0] = n % 4 ultima = 5 q = k = 1 while q: k += 1 q = 0 for x in r[1:k]: for y in r[:x + 1]: if p[x][y] < 4: continue q = 1 d = p[x][y] // 4 p[x][y] %= 4 p[x + 1][y] += d if x > y: if x > y + 1: p[x][y + 1] += d p[x - 1][y] += d else: p[x][x] += 2 * d if y: p[y][y] += 2 * d else: p[x][y] += d if y: p[x][y - 1] += d if y > 1 else 2 * d s = [] for j in range(t): x, y = f() x, y = abs(x), abs(y) if x < y: x, y = y, x s.append(p[x][y] if x < m else 0) stdout.write('\n'.join(map(str, s))) ```
6,952
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. It has been noted that if some ants are put in the junctions of the graphene integer lattice then they will act in the following fashion: every minute at each junction (x, y) containing at least four ants a group of four ants will be formed, and these four ants will scatter to the neighbouring junctions (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) — one ant in each direction. No other ant movements will happen. Ants never interfere with each other. Scientists have put a colony of n ants into the junction (0, 0) and now they wish to know how many ants will there be at some given junctions, when the movement of the ants stops. Input First input line contains integers n (0 ≤ n ≤ 30000) and t (1 ≤ t ≤ 50000), where n is the number of ants in the colony and t is the number of queries. Each of the next t lines contains coordinates of a query junction: integers xi, yi ( - 109 ≤ xi, yi ≤ 109). Queries may coincide. It is guaranteed that there will be a certain moment of time when no possible movements can happen (in other words, the process will eventually end). Output Print t integers, one per line — the number of ants at the corresponding junctions when the movement of the ants stops. Examples Input 1 3 0 1 0 0 0 -1 Output 0 1 0 Input 6 5 0 -2 0 -1 0 0 0 1 0 2 Output 0 1 2 1 0 Note In the first sample the colony consists of the one ant, so nothing happens at all. In the second sample the colony consists of 6 ants. At the first minute 4 ants scatter from (0, 0) to the neighbouring junctions. After that the process stops. Submitted Solution: ``` from collections import deque n, t = map(int,input().split()) # at (0,0) there are n ants g = [[0]*201 for x in range(201)] base = 100 g[base][base] = n x = deque() y = deque() while len(x) > 0: curX = x.popleft() curY = y.popleft() if g[curX][curY] < 4: continue re = g[curX][curY] // 4 g[curX][curY] %= 4 g[curX][curY+1] += re x.append(curX) y.append(curY+1) g[curX][curY-1] += re x.append(curX) y.append(curY-1) g[curX+1][curY] += re x.append(curX+1) y.append(curY) g[curX-1][curY] += re x.append(curX-1) y.append(curY) x.append(curX) y.append(curY) # def broadcast(x,y): # if g[x][y] >= 4: # re = g[x][y] // 4 # g[x][y] = g[x][y] % 4 # g[x-1][y] += re # g[x+1][y] += re # g[x][y+1] += re # g[x][y-1] += re # else: # return # broadcast(x+1,y); # broadcast(x-1,y); # broadcast(x,y+1); # broadcast(x,y-1); # broadcast(x,y); # broadcast(base,base) while t: x, y = map(int,input().split()) if x > 10 or x < -10 or y > 10 or y < -10: print(0) else: print(g[x+base][y+base]) t -= 1 ``` No
6,953
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. It has been noted that if some ants are put in the junctions of the graphene integer lattice then they will act in the following fashion: every minute at each junction (x, y) containing at least four ants a group of four ants will be formed, and these four ants will scatter to the neighbouring junctions (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) — one ant in each direction. No other ant movements will happen. Ants never interfere with each other. Scientists have put a colony of n ants into the junction (0, 0) and now they wish to know how many ants will there be at some given junctions, when the movement of the ants stops. Input First input line contains integers n (0 ≤ n ≤ 30000) and t (1 ≤ t ≤ 50000), where n is the number of ants in the colony and t is the number of queries. Each of the next t lines contains coordinates of a query junction: integers xi, yi ( - 109 ≤ xi, yi ≤ 109). Queries may coincide. It is guaranteed that there will be a certain moment of time when no possible movements can happen (in other words, the process will eventually end). Output Print t integers, one per line — the number of ants at the corresponding junctions when the movement of the ants stops. Examples Input 1 3 0 1 0 0 0 -1 Output 0 1 0 Input 6 5 0 -2 0 -1 0 0 0 1 0 2 Output 0 1 2 1 0 Note In the first sample the colony consists of the one ant, so nothing happens at all. In the second sample the colony consists of 6 ants. At the first minute 4 ants scatter from (0, 0) to the neighbouring junctions. After that the process stops. Submitted Solution: ``` from copy import deepcopy a = [[0] * 30 for _ in range(30)] b = deepcopy(a) n, t = map(int, input().split(' ')) a[0][0] = n for i in range(20): for j in range(-15, 16): for k in range(-15, 16): if (a[j][k] >= 4): ea = a[j][k] // 4 left = a[j][k] - 4 * ea b[j][k] += left b[j][k+1] += ea b[j][k-1] += ea b[j-1][k] += ea b[j+1][k] += ea else: b[j][k] += a[j][k] a = deepcopy(b) b = [[0] * 30 for _ in range(30)] for i in range(t): x, y = map(int, input().split(' ')) if abs(x) >= 20 or abs(y) >= 20: print(0) else: print(a[x][y]) ``` No
6,954
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. It has been noted that if some ants are put in the junctions of the graphene integer lattice then they will act in the following fashion: every minute at each junction (x, y) containing at least four ants a group of four ants will be formed, and these four ants will scatter to the neighbouring junctions (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) — one ant in each direction. No other ant movements will happen. Ants never interfere with each other. Scientists have put a colony of n ants into the junction (0, 0) and now they wish to know how many ants will there be at some given junctions, when the movement of the ants stops. Input First input line contains integers n (0 ≤ n ≤ 30000) and t (1 ≤ t ≤ 50000), where n is the number of ants in the colony and t is the number of queries. Each of the next t lines contains coordinates of a query junction: integers xi, yi ( - 109 ≤ xi, yi ≤ 109). Queries may coincide. It is guaranteed that there will be a certain moment of time when no possible movements can happen (in other words, the process will eventually end). Output Print t integers, one per line — the number of ants at the corresponding junctions when the movement of the ants stops. Examples Input 1 3 0 1 0 0 0 -1 Output 0 1 0 Input 6 5 0 -2 0 -1 0 0 0 1 0 2 Output 0 1 2 1 0 Note In the first sample the colony consists of the one ant, so nothing happens at all. In the second sample the colony consists of 6 ants. At the first minute 4 ants scatter from (0, 0) to the neighbouring junctions. After that the process stops. Submitted Solution: ``` import sys import time _no_of_ants, _no_of_queries = (input().split()) no_of_ants = int(_no_of_ants) no_of_queries = int(_no_of_queries) class ants_cord(): def __init__(self,point,no_of_ants): self.co_ant = {point:no_of_ants} def ants_at_point(self,point): return self.co_ant[point] def move_ants(self,point): x1,y1 = point if (x1+1,y1) in self.co_ant.keys(): self.co_ant[((x1+1),y1)] += 1 else: self.co_ant[((x1+1),y1)] = 1 if (x1,y1+1) in self.co_ant.keys(): self.co_ant[((x1),y1+1)] += 1 else: self.co_ant[((x1),y1+1)] = 1 if (x1,y1-1) in self.co_ant.keys(): self.co_ant[((x1),y1-1)] += 1 else: self.co_ant[((x1),y1-1)] = 1 if (x1-1,y1) in self.co_ant.keys(): self.co_ant[((x1-1),y1)] += 1 else: self.co_ant[((x1-1),y1)] = 1 self.co_ant[point] -= 4 # NEED TO OPTIMIZE THIS SECTION # CAUSING TLE def check_status(self): for key in self.co_ant.keys(): if k.co_ant[key] >= 4: return key else: return False start = time.time() k = ants_cord((0,0),no_of_ants) point = k.check_status() while point: k.move_ants(point) point = k.check_status() end = time.time() print("Sec:{}".format(end-start)) while no_of_queries: _x,_y = input().split() x = int(_x) y = int(_y) if (x,y) in k.co_ant: print(k.co_ant[(x,y)]) else: print('0') no_of_queries -= 1 ``` No
6,955
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. It has been noted that if some ants are put in the junctions of the graphene integer lattice then they will act in the following fashion: every minute at each junction (x, y) containing at least four ants a group of four ants will be formed, and these four ants will scatter to the neighbouring junctions (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) — one ant in each direction. No other ant movements will happen. Ants never interfere with each other. Scientists have put a colony of n ants into the junction (0, 0) and now they wish to know how many ants will there be at some given junctions, when the movement of the ants stops. Input First input line contains integers n (0 ≤ n ≤ 30000) and t (1 ≤ t ≤ 50000), where n is the number of ants in the colony and t is the number of queries. Each of the next t lines contains coordinates of a query junction: integers xi, yi ( - 109 ≤ xi, yi ≤ 109). Queries may coincide. It is guaranteed that there will be a certain moment of time when no possible movements can happen (in other words, the process will eventually end). Output Print t integers, one per line — the number of ants at the corresponding junctions when the movement of the ants stops. Examples Input 1 3 0 1 0 0 0 -1 Output 0 1 0 Input 6 5 0 -2 0 -1 0 0 0 1 0 2 Output 0 1 2 1 0 Note In the first sample the colony consists of the one ant, so nothing happens at all. In the second sample the colony consists of 6 ants. At the first minute 4 ants scatter from (0, 0) to the neighbouring junctions. After that the process stops. Submitted Solution: ``` f = lambda: map(int, input().split()) m = 100 n, t = f() p = [[0] * m for i in range(m)] p[1][0] = n // 4 g = 1 while g: g = 0 for x in range(1, m): if p[x][0] > 3: g = 1 d = p[x][0] // 4 p[x + 1][0] += d p[x - 1][0] += d p[x][1] += d p[x][0] %= 4 if p[x][x] > 3: g = 1 d = p[x][x] // 4 p[x + 1][x] += d p[x][x - 1] += d p[x][x] %= 4 p[1][0] += p[0][0] p[0][0] = 0 for x in range(1, m): for y in range(1, x): if p[x][y] > 3: d = p[x][y] // 4 g = 1 p[x + 1][y] += d p[x - 1][y] += d p[x][y + 1] += d p[x][y - 1] += d if y - x == 1: p[x][x] += d if y == 1: p[x][0] += d p[x][y] %= 4 p[0][0] = n % 4 for j in range(t): x, y = f() x, y = abs(x), abs(y) t = max(x, y) print(p[t][x + y - t] if t < m else 0) ``` No
6,956
Provide tags and a correct Python 3 solution for this coding contest problem. Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. Input The first line of the input contains an integer n (2 ≤ n ≤ 105). The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). Output Output a single integer — the answer to the problem. Examples Input 3 3 1 2 Output 2 Note Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2]. Tags: binary search, data structures, dp Correct Solution: ``` import bisect n=int(input()) a=list(map(int,input().split())) INF=10**18 dp=[INF]*n for i in range(n): dp[bisect.bisect_left(dp,a[i])]=a[i] print(bisect.bisect_left(dp,INF)) ```
6,957
Provide tags and a correct Python 3 solution for this coding contest problem. Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. Input The first line of the input contains an integer n (2 ≤ n ≤ 105). The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). Output Output a single integer — the answer to the problem. Examples Input 3 3 1 2 Output 2 Note Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2]. Tags: binary search, data structures, dp Correct Solution: ``` from bisect import bisect_right def answer(n,A): ans=[A[0]] for i in range(1,n): if ans[-1]<A[i]: ans.append(A[i]) else: index=bisect_right(ans,A[i]) ans[index]=A[i] return len(ans) n=int(input()) arr=list(map(int,input().split())) print(answer(n,arr)) ```
6,958
Provide tags and a correct Python 3 solution for this coding contest problem. Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. Input The first line of the input contains an integer n (2 ≤ n ≤ 105). The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). Output Output a single integer — the answer to the problem. Examples Input 3 3 1 2 Output 2 Note Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2]. Tags: binary search, data structures, dp Correct Solution: ``` def lis(a): b = [] for c in a: # if len(b) == 0 or c > b[-1] if len(b) == 0 or c > b[-1]: b.append(c) else: l = 0 r = len(b) while l < r-1: m = l+r>>1 # if b[m] <= c: l = m if b[m] < c: l = m else: r = m # if b[l] <= c: l += 1 if b[l] < c: l += 1 b[l] = c return len(b) n = int(input()) a = list(map(int, input().split())) print(lis(a)) # Made By Mostafa_Khaled ```
6,959
Provide tags and a correct Python 3 solution for this coding contest problem. Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. Input The first line of the input contains an integer n (2 ≤ n ≤ 105). The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). Output Output a single integer — the answer to the problem. Examples Input 3 3 1 2 Output 2 Note Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2]. Tags: binary search, data structures, dp Correct Solution: ``` import sys,math as mt import heapq as hp import collections as cc import math as mt import itertools as it input=sys.stdin.readline I=lambda:list(map(int,input().split())) def CeilIndex(A, l, r, key): while (r - l > 1): m = l + (r - l)//2 if (A[m] >= key): r = m else: l = m return r def lis(A, size): tailTable = [0 for i in range(size + 1)] len = 0 tailTable[0] = A[0] len = 1 for i in range(1, size): if (A[i] < tailTable[0]): tailTable[0] = A[i] elif (A[i] > tailTable[len-1]): tailTable[len] = A[i] len+= 1 else: tailTable[CeilIndex(tailTable, -1, len-1, A[i])] = A[i] return len n,=I() l=I() print(lis(l,n)) ```
6,960
Provide tags and a correct Python 3 solution for this coding contest problem. Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. Input The first line of the input contains an integer n (2 ≤ n ≤ 105). The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). Output Output a single integer — the answer to the problem. Examples Input 3 3 1 2 Output 2 Note Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2]. Tags: binary search, data structures, dp Correct Solution: ``` import sys; sys.setrecursionlimit(1000000) def solve(): # 3 1 2 4 # 1 2 3 4 # 2 1 3 5 4 # 1 2 3 4 5 n, = rv() a, = rl(1) # 3 1 2 7 4 6 5 # [ , 1 , , , , ] # [ , 1 , 2 , , , ] # [ , 1 , 2 , 4, 5, ] mem = [10000000] * (n + 1) mem[0] = a[0] for i in range(1, n): left, right = 0, n - 1 while left < right: mid = (left + right) // 2 if a[i] < mem[mid]: right = mid else: left = mid + 1 mem[left] = a[i] # for j in range(n): # if a[i] < mem[j]: # mem[j] = a[i] # break res = 0 # print(mem) for i in range(1, n): if mem[i] != 10000000: res = i print(res + 1) def rv(): return map(int, input().split()) def rl(n): return [list(map(int, input().split())) for _ in range(n)] if sys.hexversion == 50594544 : sys.stdin = open("test.txt") solve() ```
6,961
Provide tags and a correct Python 3 solution for this coding contest problem. Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. Input The first line of the input contains an integer n (2 ≤ n ≤ 105). The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). Output Output a single integer — the answer to the problem. Examples Input 3 3 1 2 Output 2 Note Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2]. Tags: binary search, data structures, dp Correct Solution: ``` def CeilIndex(A, l, r, key): while (r - l > 1): m = l + (r - l)//2 if (A[m] >= key): r = m else: l = m return r def LongestIncreasingSubsequenceLength(A, size): # Add boundary case, # when array size is one tailTable = [0 for i in range(size + 1)] len = 0 # always points empty slot tailTable[0] = A[0] len = 1 for i in range(1, size): if (A[i] < tailTable[0]): # new smallest value tailTable[0] = A[i] elif (A[i] > tailTable[len-1]): # A[i] wants to extend # largest subsequence tailTable[len] = A[i] len+= 1 else: # A[i] wants to be current # end candidate of an existing # subsequence. It will replace # ceil value in tailTable tailTable[CeilIndex(tailTable, -1, len-1, A[i])] = A[i] return len n=int(input()) l=list(map(int,input().split())) print(LongestIncreasingSubsequenceLength(l, n)) ```
6,962
Provide tags and a correct Python 3 solution for this coding contest problem. Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. Input The first line of the input contains an integer n (2 ≤ n ≤ 105). The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). Output Output a single integer — the answer to the problem. Examples Input 3 3 1 2 Output 2 Note Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2]. Tags: binary search, data structures, dp Correct Solution: ``` n = int(input()) num_list = list(map(int, input().split())) # def lower_bound(min_lis, x): # #goal return the position of the first element >= x # left = 0 # right = len(min_lis) - 1 # res = -1 # while left <= right: # mid = (left + right) // 2 # if min_lis[mid] < x: # left = mid + 1 # else: # res = mid # right = mid - 1 # return res import bisect def LongestIncreasingSubsequence(a, n): min_lis = [] #lis = [0 for i in range(n)] for i in range(n): pos = bisect.bisect_left(min_lis, a[i]) if pos == len(min_lis): #lis[i] = len(min_lis) + 1 min_lis.append(a[i]) else: #lis[i] = pos + 1 min_lis[pos] = a[i] #print(*min_lis) return (len(min_lis)) print(LongestIncreasingSubsequence(num_list, n)) ```
6,963
Provide tags and a correct Python 3 solution for this coding contest problem. Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. Input The first line of the input contains an integer n (2 ≤ n ≤ 105). The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). Output Output a single integer — the answer to the problem. Examples Input 3 3 1 2 Output 2 Note Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2]. Tags: binary search, data structures, dp Correct Solution: ``` MXusl = int MXuso = input MXusL = map MXusr = min MXusI = print n = MXusl(MXuso()) a = [1e6] * (n + 1) s = 1 for x in MXusL(MXusl, MXuso().split()): l = 0 r = s while r-l > 1: m = (l + r) >> 1 if a[m] < x: l = m else: r = m s += r == s a[r] = MXusr(a[r], x) MXusI(s - 1) ```
6,964
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. Input The first line of the input contains an integer n (2 ≤ n ≤ 105). The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). Output Output a single integer — the answer to the problem. Examples Input 3 3 1 2 Output 2 Note Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2]. Submitted Solution: ``` """ Python 3 compatibility tools. """ from __future__ import division, print_function import itertools import sys import os from io import BytesIO, IOBase if sys.version_info[0] < 3: input = raw_input range = xrange filter = itertools.ifilter map = itertools.imap zip = itertools.izip def is_it_local(): script_dir = str(os.getcwd()).split('/') username = "dipta007" return username in script_dir def READ(fileName): if is_it_local(): sys.stdin = open(f'./{fileName}', 'r') # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") if not is_it_local(): sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion def input1(type=int): return type(input()) def input2(type=int): [a, b] = list(map(type, input().split())) return a, b def input3(type=int): [a, b, c] = list(map(type, input().split())) return a, b, c def input_array(type=int): return list(map(type, input().split())) def input_string(): s = input() return list(s) ############################################################## #Given a list of numbers of length n, this routine extracts a #longest increasing subsequence. # #Running time: O(n log n) # # INPUT: a vector of integers # OUTPUT: a vector containing the longest increasing subsequence def lower_bound(nums, target): l, r = 0, len(nums) - 1 res = len(nums) while l <= r: mid = int(l + (r - l) / 2) if nums[mid] >= target: res = mid r = mid - 1 else: l = mid + 1 return res def upper_bound(nums, target): l, r = 0, len(nums) - 1 res = len(nums) while l <= r: mid = int(l + (r - l) / 2) if nums[mid] > target: r = mid - 1 res = mid else: l = mid + 1 return res def LIS(v, STRICTLY_INCREASING = False): best = [] dad = [-1 for _ in range(len(v))] for i in range(len(v)): if STRICTLY_INCREASING: item = (v[i], 0) pos = lower_bound(best, item) item.second = i else: item = (v[i], i) pos = upper_bound(best, item) if pos == len(best): dad[i] = -1 if len(best) == 0 else best[-1][1] best.append(item) else: dad[i] = -1 if pos == 0 else best[pos-1][1] best[pos] = item # print(pos, best) # ret = [] i = best[-1][1] cnt = 0 while i >= 0: # ret.append(v[i]) cnt += 1 i = dad[i] # ret.reverse() return cnt def main(): n = input1() v = input_array() print(LIS(v)) pass if __name__ == '__main__': READ('in.txt') main() ``` Yes
6,965
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. Input The first line of the input contains an integer n (2 ≤ n ≤ 105). The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). Output Output a single integer — the answer to the problem. Examples Input 3 3 1 2 Output 2 Note Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2]. Submitted Solution: ``` def lis(a): b = [] for c in a: # if len(b) == 0 or c > b[-1] if len(b) == 0 or c > b[-1]: b.append(c) else: l = 0 r = len(b) while l < r-1: m = l+r>>1 # if b[m] <= c: l = m if b[m] < c: l = m else: r = m # if b[l] <= c: l += 1 if b[l] < c: l += 1 b[l] = c return len(b) n = int(input()) a = list(map(int, input().split())) print(lis(a)) ``` Yes
6,966
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. Input The first line of the input contains an integer n (2 ≤ n ≤ 105). The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). Output Output a single integer — the answer to the problem. Examples Input 3 3 1 2 Output 2 Note Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2]. Submitted Solution: ``` from bisect import bisect_left, bisect_right, insort R = lambda: map(int, input().split()) n, arr = int(input()), list(R()) dp = [] for i in range(n): idx = bisect_left(dp, arr[i]) if idx >= len(dp): dp.append(arr[i]) else: dp[idx] = arr[i] print(len(dp)) ``` Yes
6,967
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. Input The first line of the input contains an integer n (2 ≤ n ≤ 105). The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). Output Output a single integer — the answer to the problem. Examples Input 3 3 1 2 Output 2 Note Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2]. Submitted Solution: ``` from bisect import * s, n = [0], input() for i in map(int, input().split()): if i > s[-1]: s.append(i) else: s[bisect_right(s, i)] = i print(len(s) - 1) ``` Yes
6,968
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. Input The first line of the input contains an integer n (2 ≤ n ≤ 105). The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). Output Output a single integer — the answer to the problem. Examples Input 3 3 1 2 Output 2 Note Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2]. Submitted Solution: ``` from bisect import bisect_left R = lambda: map(int, input().split()) n = int(input()) arr = list(R()) tps = [(0, 0)] for x in arr: i = bisect_left(tps, (x, -1)) - 1 tps.insert(i + 1, (x, tps[i][1] + 1)) print(max(x[1] for x in tps)) ``` No
6,969
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. Input The first line of the input contains an integer n (2 ≤ n ≤ 105). The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). Output Output a single integer — the answer to the problem. Examples Input 3 3 1 2 Output 2 Note Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2]. Submitted Solution: ``` def gis(A) : F=[0]*(len(A)+1) for i in range(1,len(A)+1) : m=0 for j in range(i) : if A[i-1]>A[j-1] : m=max(m,F[j]) F[i]=m+1 return F[-1] n=int(input()) l=list(map(int,input().split())) print(gis(l)) ``` No
6,970
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. Input The first line of the input contains an integer n (2 ≤ n ≤ 105). The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). Output Output a single integer — the answer to the problem. Examples Input 3 3 1 2 Output 2 Note Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2]. Submitted Solution: ``` n = int(input()) X = [0]+[int(x) for x in input().split()] def binSearch(L,Xi,X,M): #if X[M[L]] < Xi: return L #if X[M[1]] >= Xi: return 0 low = 1 high = L+1 while high-low > 1: mid = (low+high)//2 if X[M[mid]] < Xi: low = mid else: high = mid if X[M[low]] < Xi: return low return 0 L = 0 M = [0 for i in range(n+1)] #P = [0 for i in range(n+1)] for i in range(1,n+1): j = binSearch(L,X[i],X,M) #print(i,L,X[i],M,j) #P[i] = M[j] if j == L or X[i] < X[M[j+1]]: #print("UPD",j,L,X[i],X[M[j+1]],i,j,M) M[j+1] = i L = max(L,j+1) print(L) #print(P) ``` No
6,971
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. Input The first line of the input contains an integer n (2 ≤ n ≤ 105). The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). Output Output a single integer — the answer to the problem. Examples Input 3 3 1 2 Output 2 Note Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2]. Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) ans=0 m=a[-1] ans=1 for i in range(n-2,-1,-1): if m>a[i]: ans+=1 m=a[i] print(ans) ``` No
6,972
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. John Doe offered his sister Jane Doe find the gcd of some set of numbers a. Gcd is a positive integer g, such that all number from the set are evenly divisible by g and there isn't such g' (g' > g), that all numbers of the set are evenly divisible by g'. Unfortunately Jane couldn't cope with the task and John offered her to find the ghd of the same subset of numbers. Ghd is a positive integer g, such that at least half of numbers from the set are evenly divisible by g and there isn't such g' (g' > g) that at least half of the numbers from the set are evenly divisible by g'. Jane coped with the task for two hours. Please try it, too. Input The first line contains an integer n (1 ≤ n ≤ 106) showing how many numbers are in set a. The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 1012). Please note, that given set can contain equal numbers. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the %I64d specifier. Output Print a single integer g — the Ghd of set a. Examples Input 6 6 2 3 4 5 6 Output 3 Input 5 5 5 6 10 15 Output 5 Submitted Solution: ``` from random import randrange, seed from sys import stdin def read_integers(): return list(map(int, stdin.readline().strip().split())) def decompose(num, visited): if num in visited: return [] factors = [] i = 2 while i * i <= num: quo, rem = divmod(num, i) if i not in visited and rem == 0: factors.append(i) visited.add(i) if quo not in visited and quo != i: factors.append(quo) visited.add(quo) i += 1 factors.append(num) visited.add(num) factors.sort(reverse=True) return factors def ghd(arr): n_trials = 10 max_f = 1 visited = set() cache = {} sample_arr = [arr[randrange(0, len(arr))] for _ in range(700)] if len(arr) >= 10 ** 5 else arr for i in range(n_trials): for f in decompose(arr[randrange(0, len(arr))], visited): if any(f < x and x % f == 0 for x in cache): cache[f] = -1 visited.add(f) continue cnt = sum(x % f == 0 for x in sample_arr) if cnt * 2 >= len(sample_arr): max_f = max(max_f, f) cache[f] = cnt return max_f if True: from time import time seed(time()) _, = read_integers() arr = read_integers() print(ghd(arr)) ``` No
6,973
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. John Doe offered his sister Jane Doe find the gcd of some set of numbers a. Gcd is a positive integer g, such that all number from the set are evenly divisible by g and there isn't such g' (g' > g), that all numbers of the set are evenly divisible by g'. Unfortunately Jane couldn't cope with the task and John offered her to find the ghd of the same subset of numbers. Ghd is a positive integer g, such that at least half of numbers from the set are evenly divisible by g and there isn't such g' (g' > g) that at least half of the numbers from the set are evenly divisible by g'. Jane coped with the task for two hours. Please try it, too. Input The first line contains an integer n (1 ≤ n ≤ 106) showing how many numbers are in set a. The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 1012). Please note, that given set can contain equal numbers. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the %I64d specifier. Output Print a single integer g — the Ghd of set a. Examples Input 6 6 2 3 4 5 6 Output 3 Input 5 5 5 6 10 15 Output 5 Submitted Solution: ``` from random import randrange, seed from sys import stdin def read_integers(): return list(map(int, stdin.readline().strip().split())) def decompose(num, visited): if num in visited: return [] factors = [] i = 2 while i * i <= num: quo, rem = divmod(num, i) if i not in visited and rem == 0: factors.append(i) visited.add(i) if quo not in visited and quo != i: factors.append(quo) visited.add(quo) i += 1 factors.append(num) visited.add(num) factors.sort(reverse=True) return factors def ghd(arr): n_trials = 15 max_f = 1 visited = set() cache = {} sample_arr = [arr[randrange(0, len(arr))] for _ in range(700)] if len(arr) >= 10 ** 5 else arr for i in range(n_trials): for f in decompose(arr[randrange(0, len(arr))], visited): if any(f < x and x % f == 0 for x in cache): cache[f] = -1 visited.add(f) continue cnt = sum(x % f == 0 for x in sample_arr) if cnt * 2 >= len(sample_arr): max_f = max(max_f, f) cache[f] = cnt return max_f if True: from time import time seed(time()) _, = read_integers() arr = read_integers() print(ghd(arr)) ``` No
6,974
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. John Doe offered his sister Jane Doe find the gcd of some set of numbers a. Gcd is a positive integer g, such that all number from the set are evenly divisible by g and there isn't such g' (g' > g), that all numbers of the set are evenly divisible by g'. Unfortunately Jane couldn't cope with the task and John offered her to find the ghd of the same subset of numbers. Ghd is a positive integer g, such that at least half of numbers from the set are evenly divisible by g and there isn't such g' (g' > g) that at least half of the numbers from the set are evenly divisible by g'. Jane coped with the task for two hours. Please try it, too. Input The first line contains an integer n (1 ≤ n ≤ 106) showing how many numbers are in set a. The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 1012). Please note, that given set can contain equal numbers. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the %I64d specifier. Output Print a single integer g — the Ghd of set a. Examples Input 6 6 2 3 4 5 6 Output 3 Input 5 5 5 6 10 15 Output 5 Submitted Solution: ``` from typing import List, Set from random import randrange from sys import stdin from functools import reduce, lru_cache def read_integers(): return list(map(int, stdin.readline().strip().split())) @lru_cache(None) def decompose(num: int) -> List[int]: if num <= 1: return [] i = 2 while i ** 2 < num: if num % i == 0: left = decompose(i) right = decompose(num // i) return left + right i += 1 return [num] def ghd(arr: List[int]) -> int: n_trials = 30 rd_lst = [randrange(0, len(arr)) for _ in range(n_trials)] # type: List[int] factors = set(reduce(list.__add__, (decompose(arr[rd]) for rd in rd_lst))) # type: Set[int] cnts = {f: sum(num % f == 0 for num in arr) for f in factors} cnts = {k: v for k, v in cnts.items() if v * 2 >= len(arr)} return max(cnts.keys()) if cnts else 1 if True: _, = read_integers() input_arr = read_integers() print(ghd(input_arr)) ``` No
6,975
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. John Doe offered his sister Jane Doe find the gcd of some set of numbers a. Gcd is a positive integer g, such that all number from the set are evenly divisible by g and there isn't such g' (g' > g), that all numbers of the set are evenly divisible by g'. Unfortunately Jane couldn't cope with the task and John offered her to find the ghd of the same subset of numbers. Ghd is a positive integer g, such that at least half of numbers from the set are evenly divisible by g and there isn't such g' (g' > g) that at least half of the numbers from the set are evenly divisible by g'. Jane coped with the task for two hours. Please try it, too. Input The first line contains an integer n (1 ≤ n ≤ 106) showing how many numbers are in set a. The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 1012). Please note, that given set can contain equal numbers. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the %I64d specifier. Output Print a single integer g — the Ghd of set a. Examples Input 6 6 2 3 4 5 6 Output 3 Input 5 5 5 6 10 15 Output 5 Submitted Solution: ``` from random import randrange, seed from sys import stdin def read_integers(): return list(map(int, stdin.readline().strip().split())) def decompose(num, visited): if num in visited: return [] factors = [] i = 2 while i * i <= num: quo, rem = divmod(num, i) if i not in visited and rem == 0: factors.append(i) visited.add(i) if quo not in visited and quo != i: factors.append(quo) visited.add(quo) i += 1 factors.append(num) visited.add(num) factors.sort(reverse=True) return factors def ghd(arr): n_trials = 15 max_f = 1 visited = set() cache = {} sample_arr = [arr[randrange(0, len(arr))] for _ in range(1000)] if len(arr) >= 10 ** 5 else arr for i in range(n_trials): for f in decompose(arr[randrange(0, len(arr))], visited): if any(f < x and x % f == 0 for x in cache): cache[f] = -1 visited.add(f) continue cnt = sum(x % f == 0 for x in sample_arr) if cnt * 2 >= len(sample_arr): max_f = max(max_f, f) cache[f] = cnt return max_f if True: from time import time seed(time()) _, = read_integers() arr = read_integers() print(ghd(arr)) ``` No
6,976
Provide tags and a correct Python 3 solution for this coding contest problem. Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty. Suppose Ciel and Jiro play optimally, what is the score of the game? Input The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile. Output Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally. Examples Input 2 1 100 2 1 10 Output 101 10 Input 1 9 2 8 6 5 9 4 7 1 3 Output 30 15 Input 3 3 1 3 2 3 5 4 6 2 8 7 Output 18 18 Input 3 3 1000 1000 1000 6 1000 1000 1000 1000 1000 1000 5 1000 1000 1000 1000 1000 Output 7000 7000 Note In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10. In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. Tags: games, greedy, sortings Correct Solution: ``` from sys import stdin from collections import deque import heapq n = int(stdin.readline()) piles = [] for x in range(n): a = [int(x) for x in stdin.readline().split()][1:] piles.append(a) cielTotal = 0 jiroTotal = 0 mids = [] for x in piles: cielTotal += sum(x[:len(x)//2]) jiroTotal += sum(x[len(x)//2+len(x)%2:]) #print(x) #print(cielTotal,jiroTotal) if len(x)%2 == 1: mids.append(x[len(x)//2]) mids.sort(reverse=True) turn = True for x in mids: if turn: cielTotal += x else: jiroTotal += x turn = not turn print(cielTotal,jiroTotal) ```
6,977
Provide tags and a correct Python 3 solution for this coding contest problem. Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty. Suppose Ciel and Jiro play optimally, what is the score of the game? Input The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile. Output Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally. Examples Input 2 1 100 2 1 10 Output 101 10 Input 1 9 2 8 6 5 9 4 7 1 3 Output 30 15 Input 3 3 1 3 2 3 5 4 6 2 8 7 Output 18 18 Input 3 3 1000 1000 1000 6 1000 1000 1000 1000 1000 1000 5 1000 1000 1000 1000 1000 Output 7000 7000 Note In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10. In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. Tags: games, greedy, sortings Correct Solution: ``` n = int(input()) a,b = 0,0 l = [] for _ in range(n): inpt = list(map(int,input().split()))[1:] li = len(inpt) if li%2: l.append(inpt[li//2]) a += sum((inpt[:li//2])) b += sum((inpt[(li + 1)//2:])) l.sort(reverse=True) a += sum(l[::2]) b += sum(l[1::2]) print(a, b) ```
6,978
Provide tags and a correct Python 3 solution for this coding contest problem. Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty. Suppose Ciel and Jiro play optimally, what is the score of the game? Input The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile. Output Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally. Examples Input 2 1 100 2 1 10 Output 101 10 Input 1 9 2 8 6 5 9 4 7 1 3 Output 30 15 Input 3 3 1 3 2 3 5 4 6 2 8 7 Output 18 18 Input 3 3 1000 1000 1000 6 1000 1000 1000 1000 1000 1000 5 1000 1000 1000 1000 1000 Output 7000 7000 Note In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10. In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. Tags: games, greedy, sortings Correct Solution: ``` #!/usr/bin/env python3 odd, even = [], [] player1_turn = True player1 = player2 = 0 pile_number = int(input()) for _ in range(pile_number): n, *pile = tuple(map(int, input().split())) if n % 2 == 0: even.append(pile) else: odd.append(pile) for pile in even: n = len(pile) player1 += sum(pile[:n//2]) player2 += sum(pile[n//2:]) for pile in sorted(odd, reverse=True, key=lambda x: x[len(x)//2]): n = len(pile) top, middle, bottom = pile[:n//2], pile[n//2], pile[n//2+1:] player1 += sum(top) player2 += sum(bottom) if player1_turn: player1 += middle player1_turn = not player1_turn else: player2 += middle player1_turn = not player1_turn print(player1, player2) ```
6,979
Provide tags and a correct Python 3 solution for this coding contest problem. Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty. Suppose Ciel and Jiro play optimally, what is the score of the game? Input The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile. Output Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally. Examples Input 2 1 100 2 1 10 Output 101 10 Input 1 9 2 8 6 5 9 4 7 1 3 Output 30 15 Input 3 3 1 3 2 3 5 4 6 2 8 7 Output 18 18 Input 3 3 1000 1000 1000 6 1000 1000 1000 1000 1000 1000 5 1000 1000 1000 1000 1000 Output 7000 7000 Note In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10. In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. Tags: games, greedy, sortings Correct Solution: ``` from functools import reduce n = int(input()) cards = [list(map(int, input().split()[1:])) for i in range(n)] mid = sorted((c[len(c) >> 1] for c in cards if len(c) & 1 == 1), reverse=True) add = lambda x=0, y=0: x + y a, b = reduce(add, mid[::2] or [0]), reduce(add, mid[1::2] or [0]) for c in cards: m = len(c) >> 1 a += reduce(add, c[:m] or [0]) b += reduce(add, c[m + (len(c) & 1):] or [0]) print(a, b) ```
6,980
Provide tags and a correct Python 3 solution for this coding contest problem. Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty. Suppose Ciel and Jiro play optimally, what is the score of the game? Input The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile. Output Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally. Examples Input 2 1 100 2 1 10 Output 101 10 Input 1 9 2 8 6 5 9 4 7 1 3 Output 30 15 Input 3 3 1 3 2 3 5 4 6 2 8 7 Output 18 18 Input 3 3 1000 1000 1000 6 1000 1000 1000 1000 1000 1000 5 1000 1000 1000 1000 1000 Output 7000 7000 Note In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10. In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. Tags: games, greedy, sortings Correct Solution: ``` n = int(input()) S = [0] * n ciel, giro = 0, 0 odd = [] for i in range(n): L = list(map(int, input().split())) k = L[0] L = L[1:] S[i] = sum(L) if k % 2: odd.append(L[k // 2]) ciel += sum(L[:k // 2]) giro += sum(L[(k + 1) // 2:]) odd.sort(reverse=True) for i, x in enumerate(odd): if i % 2: giro += x else: ciel += x print(ciel, giro) ```
6,981
Provide tags and a correct Python 3 solution for this coding contest problem. Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty. Suppose Ciel and Jiro play optimally, what is the score of the game? Input The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile. Output Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally. Examples Input 2 1 100 2 1 10 Output 101 10 Input 1 9 2 8 6 5 9 4 7 1 3 Output 30 15 Input 3 3 1 3 2 3 5 4 6 2 8 7 Output 18 18 Input 3 3 1000 1000 1000 6 1000 1000 1000 1000 1000 1000 5 1000 1000 1000 1000 1000 Output 7000 7000 Note In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10. In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. Tags: games, greedy, sortings Correct Solution: ``` n = int(input()) c = [list(map(int, input().split())) for _ in range(n)] a, b = 0, 0 d = [] for i in range(n): if len(c[i]) % 2: a += sum(c[i][1:c[i][0]//2+1]) b += sum(c[i][c[i][0]//2+1:]) else: a += sum(c[i][1:c[i][0]//2+1]) b += sum(c[i][c[i][0]//2+2:]) d.append(c[i][c[i][0]//2+1]) d.sort(reverse=True) print(a+sum(d[0::2]), b+sum(d[1::2])) ```
6,982
Provide tags and a correct Python 3 solution for this coding contest problem. Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty. Suppose Ciel and Jiro play optimally, what is the score of the game? Input The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile. Output Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally. Examples Input 2 1 100 2 1 10 Output 101 10 Input 1 9 2 8 6 5 9 4 7 1 3 Output 30 15 Input 3 3 1 3 2 3 5 4 6 2 8 7 Output 18 18 Input 3 3 1000 1000 1000 6 1000 1000 1000 1000 1000 1000 5 1000 1000 1000 1000 1000 Output 7000 7000 Note In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10. In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. Tags: games, greedy, sortings Correct Solution: ``` n = int(input()) a = b = 0 s = [] for _ in range(n): l = [*map(int, input().split())][1:] m = len(l) if m & 1: s.append(l[m//2]) a += sum((l[:m//2])) b += sum((l[(m + 1)//2:])) s.sort(reverse = True) a += sum(s[::2]) b += sum(s[1::2]) print(a, b) ```
6,983
Provide tags and a correct Python 3 solution for this coding contest problem. Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty. Suppose Ciel and Jiro play optimally, what is the score of the game? Input The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile. Output Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally. Examples Input 2 1 100 2 1 10 Output 101 10 Input 1 9 2 8 6 5 9 4 7 1 3 Output 30 15 Input 3 3 1 3 2 3 5 4 6 2 8 7 Output 18 18 Input 3 3 1000 1000 1000 6 1000 1000 1000 1000 1000 1000 5 1000 1000 1000 1000 1000 Output 7000 7000 Note In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10. In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. Tags: games, greedy, sortings Correct Solution: ``` p, n = [], int(input()) a = b = 0 for i in range(n): t = list(map(int, input().split())) k = t[0] // 2 + 1 a += sum(t[1: k]) if t[0] & 1: p.append(t[k]) b += sum(t[k + 1: ]) else: b += sum(t[k: ]) p.sort(reverse = True) print(a + sum(p[0 :: 2]), b + sum(p[1 :: 2])) ```
6,984
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty. Suppose Ciel and Jiro play optimally, what is the score of the game? Input The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile. Output Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally. Examples Input 2 1 100 2 1 10 Output 101 10 Input 1 9 2 8 6 5 9 4 7 1 3 Output 30 15 Input 3 3 1 3 2 3 5 4 6 2 8 7 Output 18 18 Input 3 3 1000 1000 1000 6 1000 1000 1000 1000 1000 1000 5 1000 1000 1000 1000 1000 Output 7000 7000 Note In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10. In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. Submitted Solution: ``` n = int(input()) lista = [] aux = [] somaA = 0 somaB = 0 for i in range(n): a = [int(i) for i in input().split()][1:] if len(a) > 1: somaA += sum(a[0:len(a)//2]) somaB += sum(a[-(len(a)//2):]) if len(a) % 2 == 1: aux.append(a[len(a)//2]) aux.sort(reverse=True) for i in range(0, len(aux), 2): somaA += aux[i] for i in range(1, len(aux), 2): somaB += aux[i] print(somaA, somaB) ``` Yes
6,985
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty. Suppose Ciel and Jiro play optimally, what is the score of the game? Input The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile. Output Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally. Examples Input 2 1 100 2 1 10 Output 101 10 Input 1 9 2 8 6 5 9 4 7 1 3 Output 30 15 Input 3 3 1 3 2 3 5 4 6 2 8 7 Output 18 18 Input 3 3 1000 1000 1000 6 1000 1000 1000 1000 1000 1000 5 1000 1000 1000 1000 1000 Output 7000 7000 Note In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10. In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. Submitted Solution: ``` N = int(input()) one = two = 0 middles = [] for i in range(N): array = list(map(int, input().split()))[1:] size = len(array)-1 middle = size//2 for i in range(middle): one += array[i] for i in range(middle+1, len(array)): two += array[i] if len(array)%2==1: middles.append(array[middle]) else: one += array[middle] middles = sorted(middles) ONE = True for i in range(len(middles)-1, -1, -1): if ONE: one += middles[i] ONE = False else: two += middles[i] ONE = True print(one, two) ``` Yes
6,986
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty. Suppose Ciel and Jiro play optimally, what is the score of the game? Input The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile. Output Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally. Examples Input 2 1 100 2 1 10 Output 101 10 Input 1 9 2 8 6 5 9 4 7 1 3 Output 30 15 Input 3 3 1 3 2 3 5 4 6 2 8 7 Output 18 18 Input 3 3 1000 1000 1000 6 1000 1000 1000 1000 1000 1000 5 1000 1000 1000 1000 1000 Output 7000 7000 Note In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10. In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. Submitted Solution: ``` from functools import reduce n = int(input()) cards = [list(map(int, input().split()[1:])) for i in range(n)] mid = [c[len(c) >> 1] for c in cards if len(c) & 1 == 1] a, b = 0, 0 add = lambda x=0, y=0: x + y for c in cards: m = len(c) >> 1 a += reduce(add, c[:m] or [0]) b += reduce(add, c[m + (len(c) & 1):] or [0]) mid.sort(reverse=True) a += reduce(add, mid[::2] or [0]) b += reduce(add, mid[1::2] or [0]) print(a, b) ``` Yes
6,987
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty. Suppose Ciel and Jiro play optimally, what is the score of the game? Input The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile. Output Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally. Examples Input 2 1 100 2 1 10 Output 101 10 Input 1 9 2 8 6 5 9 4 7 1 3 Output 30 15 Input 3 3 1 3 2 3 5 4 6 2 8 7 Output 18 18 Input 3 3 1000 1000 1000 6 1000 1000 1000 1000 1000 1000 5 1000 1000 1000 1000 1000 Output 7000 7000 Note In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10. In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. Submitted Solution: ``` n=int(input()) s1,s2=0,0 tab = [] for i in range(n): c = list(map(int,input().split())) for j in range(1,c[0]+1): if(j*2<=c[0]): s1+=c[j] else: s2+=c[j] if(c[0] & 1): s2-=c[(c[0]+1)//2] tab.append(c[(c[0]+1)//2]) if(len(tab)): tab.sort() tab.reverse() for i in range(len(tab)): if(i & 1): s2+=tab[i] else: s1+=tab[i] print(s1,s2) # Made By Mostafa_Khaled ``` Yes
6,988
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty. Suppose Ciel and Jiro play optimally, what is the score of the game? Input The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile. Output Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally. Examples Input 2 1 100 2 1 10 Output 101 10 Input 1 9 2 8 6 5 9 4 7 1 3 Output 30 15 Input 3 3 1 3 2 3 5 4 6 2 8 7 Output 18 18 Input 3 3 1000 1000 1000 6 1000 1000 1000 1000 1000 1000 5 1000 1000 1000 1000 1000 Output 7000 7000 Note In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10. In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. Submitted Solution: ``` n = int(input()) S = [0] * n ciel, giro = [], [] a, b = 0, 0 for i in range(n): L = list(map(int, input().split())) k = L[0] L = L[1:] S[i] = sum(L) if k % 2: ciel.append((sum(L[:k // 2 + 1]), i)) giro.append((sum(L[k // 2:], i), i)) else: a += sum(L[:k // 2]) b += sum(L[k // 2:]) ciel.sort(reverse=True) giro.sort(reverse=True) vis = [False] * n k = len(ciel) i, j = 0, 0 finished = False while not finished: finished = True while i < k and vis[ciel[i][1]]: i += 1 if i < k: finished = False vis[ciel[i][1]] = True a += ciel[i][0] b += S[ciel[i][1]] - ciel[i][0] while j < k and vis[giro[j][1]]: j += 1 if j < k: finished = False vis[giro[j][1]] = True b += giro[j][0] a += S[giro[j][1]] - giro[j][0] print(a, b) ``` No
6,989
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty. Suppose Ciel and Jiro play optimally, what is the score of the game? Input The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile. Output Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally. Examples Input 2 1 100 2 1 10 Output 101 10 Input 1 9 2 8 6 5 9 4 7 1 3 Output 30 15 Input 3 3 1 3 2 3 5 4 6 2 8 7 Output 18 18 Input 3 3 1000 1000 1000 6 1000 1000 1000 1000 1000 1000 5 1000 1000 1000 1000 1000 Output 7000 7000 Note In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10. In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. Submitted Solution: ``` from sys import stdin from collections import deque import heapq n = int(stdin.readline()) piles = [] for x in range(n): a = [int(x) for x in stdin.readline().split()][1:] piles.append(a) cielTotal = 0 jiroTotal = 0 mids = [] for x in piles: cielTotal += sum(x[:len(x)//2]) jiroTotal += sum(x[len(x)//2+len(x)%2:]) if len(x)%2 == 1: mids.append(x[len(x)//2]) mids.sort() turn = True for x in mids: if turn: cielTotal += x else: jiroTotal += x print(cielTotal,jiroTotal) ``` No
6,990
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty. Suppose Ciel and Jiro play optimally, what is the score of the game? Input The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile. Output Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally. Examples Input 2 1 100 2 1 10 Output 101 10 Input 1 9 2 8 6 5 9 4 7 1 3 Output 30 15 Input 3 3 1 3 2 3 5 4 6 2 8 7 Output 18 18 Input 3 3 1000 1000 1000 6 1000 1000 1000 1000 1000 1000 5 1000 1000 1000 1000 1000 Output 7000 7000 Note In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10. In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. Submitted Solution: ``` from sys import stdin from collections import deque import heapq n = int(stdin.readline()) piles = [] for x in range(n): a = deque([int(x) for x in stdin.readline().split()][1:]) piles.append(a) ciel = [(-x[0],i) for i,x in enumerate(piles)] jiro = [(-x[-1],i) for i,x in enumerate(piles)] heapq.heapify(ciel) heapq.heapify(jiro) empty = set([-1]) cielTotal = 0 jiroTotal = 0 turn = True while True: if turn: ind = -1 while ind in empty and ciel: nxt,ind = heapq.heappop(ciel) nxt = -nxt if ind in empty: break piles[ind].popleft() cielTotal += nxt if not piles[ind]: empty.add(ind) else: heapq.heappush(ciel, (-piles[ind][0], ind)) else: ind = -1 while ind in empty and jiro: nxt,ind = heapq.heappop(jiro) nxt = -nxt if ind in empty: break piles[ind].pop() jiroTotal += nxt if not piles[ind]: empty.add(ind) else: heapq.heappush(jiro, (-piles[ind][-1], ind)) turn = not turn print(cielTotal,jiroTotal) ``` No
6,991
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty. Suppose Ciel and Jiro play optimally, what is the score of the game? Input The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile. Output Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally. Examples Input 2 1 100 2 1 10 Output 101 10 Input 1 9 2 8 6 5 9 4 7 1 3 Output 30 15 Input 3 3 1 3 2 3 5 4 6 2 8 7 Output 18 18 Input 3 3 1000 1000 1000 6 1000 1000 1000 1000 1000 1000 5 1000 1000 1000 1000 1000 Output 7000 7000 Note In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10. In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. Submitted Solution: ``` #!/usr/bin/env python3 player1 = player2 = 0 player1_turn = True pile_number = int(input()) piles = [] for _ in range(pile_number): n, *pile = tuple(map(int, input().split())) piles.append(pile) for pile in sorted(piles, reverse=True): n = len(pile) if n % 2 == 0: player1 += sum(pile[:n//2]) player2 += sum(pile[n//2:]) else: top, middle, bottom = pile[:n//2], pile[n//2], pile[n//2+1:] player1 += sum(top) player2 += sum(bottom) if player1_turn: player1 += middle player1_turn = not player1_turn else: player2 += middle player1_turn = not player1_turn print(player1, player2) ``` No
6,992
Provide tags and a correct Python 3 solution for this coding contest problem. Two teams meet in The Game World Championship. Some scientists consider this game to be the most intellectually challenging game in the world. You are given two strings describing the teams' actions in the final battle. Figure out who became the champion. Input The input contains two strings of equal length (between 2 and 20 characters, inclusive). Each line describes the actions of one team. Output Output "TEAM 1 WINS" if the first team won, "TEAM 2 WINS" if the second team won, and "TIE" if there was a tie. Examples Input []()[]8&lt; 8&lt;[]()8&lt; Output TEAM 2 WINS Input 8&lt;8&lt;() []8&lt;[] Output TIE Tags: *special Correct Solution: ``` s1 = input() s2 = input() n = len(s1) bal = 0 for i in range(0, n, 2): if s1[i] == '8' and s2[i] == '[': bal += 1 if s1[i] == '8' and s2[i] == '(': bal -= 1 if s1[i] == '(' and s2[i] == '[': bal -= 1 if s1[i] == '(' and s2[i] == '8': bal += 1 if s1[i] == '[' and s2[i] == '(': bal += 1 if s1[i] == '[' and s2[i] == '8': bal -= 1 if bal == 0: print('TIE') elif bal > 0: print('TEAM 1 WINS') else: print('TEAM 2 WINS') ```
6,993
Provide tags and a correct Python 3 solution for this coding contest problem. Two teams meet in The Game World Championship. Some scientists consider this game to be the most intellectually challenging game in the world. You are given two strings describing the teams' actions in the final battle. Figure out who became the champion. Input The input contains two strings of equal length (between 2 and 20 characters, inclusive). Each line describes the actions of one team. Output Output "TEAM 1 WINS" if the first team won, "TEAM 2 WINS" if the second team won, and "TIE" if there was a tie. Examples Input []()[]8&lt; 8&lt;[]()8&lt; Output TEAM 2 WINS Input 8&lt;8&lt;() []8&lt;[] Output TIE Tags: *special Correct Solution: ``` __author__ = 'Pavel Mavrin' a = input().strip() b = input().strip() n = len(a) // 2 x = 0 figs = ["[]", "()", "8<"] for i in range(n): s1 = figs.index(a[i * 2: (i + 1) * 2]) s2 = figs.index(b[i * 2: (i + 1) * 2]) if s2 == (s1 + 1) % 3: x += 1 if s1 == (s2 + 1) % 3: x -= 1 if x > 0: print("TEAM 1 WINS") elif x < 0: print("TEAM 2 WINS") else: print("TIE") ```
6,994
Provide tags and a correct Python 3 solution for this coding contest problem. Two teams meet in The Game World Championship. Some scientists consider this game to be the most intellectually challenging game in the world. You are given two strings describing the teams' actions in the final battle. Figure out who became the champion. Input The input contains two strings of equal length (between 2 and 20 characters, inclusive). Each line describes the actions of one team. Output Output "TEAM 1 WINS" if the first team won, "TEAM 2 WINS" if the second team won, and "TIE" if there was a tie. Examples Input []()[]8&lt; 8&lt;[]()8&lt; Output TEAM 2 WINS Input 8&lt;8&lt;() []8&lt;[] Output TIE Tags: *special Correct Solution: ``` s1 = input() s2 = input() num1 = 0 num2 = 0 for i in range(0, len(s1), 2): c1 = s1[i:i + 2] c2 = s2[i:i + 2] if c1 != c2: if (c1 == "8<" and c2 == "[]"): num1 += 1 elif (c1 == "()" and c2 == "8<"): num1 += 1 elif (c1 == "[]" and c2 == "()"): num1 += 1 else: num2 += 1 if num1 == num2: print("TIE") else: print("TEAM {} WINS".format(int(num2 > num1) + 1)) ```
6,995
Provide tags and a correct Python 3 solution for this coding contest problem. Two teams meet in The Game World Championship. Some scientists consider this game to be the most intellectually challenging game in the world. You are given two strings describing the teams' actions in the final battle. Figure out who became the champion. Input The input contains two strings of equal length (between 2 and 20 characters, inclusive). Each line describes the actions of one team. Output Output "TEAM 1 WINS" if the first team won, "TEAM 2 WINS" if the second team won, and "TIE" if there was a tie. Examples Input []()[]8&lt; 8&lt;[]()8&lt; Output TEAM 2 WINS Input 8&lt;8&lt;() []8&lt;[] Output TIE Tags: *special Correct Solution: ``` def split_by_n( seq, n ): while seq: yield seq[:n] seq = seq[n:] def is_first_win(first, second): if first ==second: return 0 if first == '8<': if second == '[]': return 1 else: return -1 if first == '[]': if second == '()': return 1 else: return -1 if second == '8<': return 1 else: return -1 team1 = list(split_by_n(input(), 2)) team2 = list(split_by_n(input(), 2)) score = 0 for i in range(len(team1)): score += is_first_win(team1[i], team2[i]) #print(is_first_win(team1[i], team2[i]), team1[i], team2[i]) #print(score) if score > 0: print('TEAM 1 WINS') elif score == 0: print('TIE') else: print('TEAM 2 WINS') ```
6,996
Provide tags and a correct Python 3 solution for this coding contest problem. Two teams meet in The Game World Championship. Some scientists consider this game to be the most intellectually challenging game in the world. You are given two strings describing the teams' actions in the final battle. Figure out who became the champion. Input The input contains two strings of equal length (between 2 and 20 characters, inclusive). Each line describes the actions of one team. Output Output "TEAM 1 WINS" if the first team won, "TEAM 2 WINS" if the second team won, and "TIE" if there was a tie. Examples Input []()[]8&lt; 8&lt;[]()8&lt; Output TEAM 2 WINS Input 8&lt;8&lt;() []8&lt;[] Output TIE Tags: *special Correct Solution: ``` a = input() a_actions = [(a[i:i+2]) for i in range(0, len(a), 2)] b = input() b_actions = [(b[i:i+2]) for i in range(0, len(b), 2)] # print(a_actions, b_actions) rock = "()" paper = "[]" scissors = "8<" a_pts = 0 b_pts = 0 for i in range(len(a)//2): if a_actions[i] == rock: if b_actions[i] == scissors: a_pts += 1 elif b_actions[i] == paper: b_pts += 1 else: a_pts += 1 b_pts += 1 elif a_actions[i] == paper: if b_actions[i] == rock: a_pts += 1 elif b_actions[i] == scissors: b_pts += 1 else: a_pts += 1 b_pts += 1 else: # a_actions[i] == scissors if b_actions[i] == paper: a_pts += 1 elif b_actions[i] == rock: b_pts += 1 else: a_pts += 1 b_pts += 1 if a_pts > b_pts: print("TEAM 1 WINS") elif a_pts < b_pts: print("TEAM 2 WINS") else: print("TIE") ```
6,997
Provide tags and a correct Python 3 solution for this coding contest problem. Two teams meet in The Game World Championship. Some scientists consider this game to be the most intellectually challenging game in the world. You are given two strings describing the teams' actions in the final battle. Figure out who became the champion. Input The input contains two strings of equal length (between 2 and 20 characters, inclusive). Each line describes the actions of one team. Output Output "TEAM 1 WINS" if the first team won, "TEAM 2 WINS" if the second team won, and "TIE" if there was a tie. Examples Input []()[]8&lt; 8&lt;[]()8&lt; Output TEAM 2 WINS Input 8&lt;8&lt;() []8&lt;[] Output TIE Tags: *special Correct Solution: ``` #!/bin/bash/python # Date: 2014-04-16 # Author: shijinzhan # Status: # Note: '()' < '[]' # '[]' < '8<' # '8<' < '()' team1 = input().replace('(', '6').replace('[', '7') team2 = input().replace('(', '6').replace('[', '7') s = 0 for x in range(0, len(team1), 2): if team1[x] > team2[x]: s += 1 elif team1[x] < team2[x]: s -= 1 if team1[x] == '6' and team2[x] == '8': s += 2 if team1[x] == '8' and team2[x] == '6': s -= 2 if s > 0: print("TEAM 1 WINS") elif s < 0: print("TEAM 2 WINS") else: print("TIE") ```
6,998
Provide tags and a correct Python 3 solution for this coding contest problem. Two teams meet in The Game World Championship. Some scientists consider this game to be the most intellectually challenging game in the world. You are given two strings describing the teams' actions in the final battle. Figure out who became the champion. Input The input contains two strings of equal length (between 2 and 20 characters, inclusive). Each line describes the actions of one team. Output Output "TEAM 1 WINS" if the first team won, "TEAM 2 WINS" if the second team won, and "TIE" if there was a tie. Examples Input []()[]8&lt; 8&lt;[]()8&lt; Output TEAM 2 WINS Input 8&lt;8&lt;() []8&lt;[] Output TIE Tags: *special Correct Solution: ``` s = input() t = input() a = 0 b = 0 for i in range(0, len(s), 2): if s[i:i+2] == "[]" and t[i:i+2]=="()": a += 1 if s[i:i+2] == "()" and t[i:i+2]=="8<": a += 1 if s[i:i+2] == "8<" and t[i:i+2]=="[]": a += 1 if t[i:i+2] == "[]" and s[i:i+2]=="()": b += 1 if t[i:i+2] == "()" and s[i:i+2]=="8<": b += 1 if t[i:i+2] == "8<" and s[i:i+2]=="[]": b += 1 if a > b: print("TEAM 1 WINS") elif a < b: print("TEAM 2 WINS") else: print("TIE") ```
6,999