description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Consider an infinite triangle made up of layers. Let's number the layers, starting from one, from the top of the triangle (from top to bottom). The $k$-th layer of the triangle contains $k$ points, numbered from left to right. Each point of an infinite triangle is described by a pair of numbers $(r, c)$ ($1 \le c \le r$), where $r$ is the number of the layer, and $c$ is the number of the point in the layer. From each point $(r, c)$ there are two directed edges to the points $(r+1, c)$ and $(r+1, c+1)$, but only one of the edges is activated. If $r + c$ is even, then the edge to the point $(r+1, c)$ is activated, otherwise the edge to the point $(r+1, c+1)$ is activated. Look at the picture for a better understanding. Activated edges are colored in black. Non-activated edges are colored in gray. From the point $(r_1, c_1)$ it is possible to reach the point $(r_2, c_2)$, if there is a path between them only from activated edges. For example, in the picture above, there is a path from $(1, 1)$ to $(3, 2)$, but there is no path from $(2, 1)$ to $(1, 1)$. Initially, you are at the point $(1, 1)$. For each turn, you can: Replace activated edge for point $(r, c)$. That is if the edge to the point $(r+1, c)$ is activated, then instead of it, the edge to the point $(r+1, c+1)$ becomes activated, otherwise if the edge to the point $(r+1, c+1)$, then instead if it, the edge to the point $(r+1, c)$ becomes activated. This action increases the cost of the path by $1$; Move from the current point to another by following the activated edge. This action does not increase the cost of the path. You are given a sequence of $n$ points of an infinite triangle $(r_1, c_1), (r_2, c_2), \ldots, (r_n, c_n)$. Find the minimum cost path from $(1, 1)$, passing through all $n$ points in arbitrary order. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) is the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$) is the number of points to visit. The second line contains $n$ numbers $r_1, r_2, \ldots, r_n$ ($1 \le r_i \le 10^9$), where $r_i$ is the number of the layer in which $i$-th point is located. The third line contains $n$ numbers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le r_i$), where $c_i$ is the number of the $i$-th point in the $r_i$ layer. It is guaranteed that all $n$ points are distinct. It is guaranteed that there is always at least one way to traverse all $n$ points. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output the minimum cost of a path passing through all points in the corresponding test case. -----Examples----- Input 4 3 1 4 2 1 3 1 2 2 4 2 3 2 1 1000000000 1 1000000000 4 3 10 5 8 2 5 2 4 Output 0 1 999999999 2 -----Note----- None
for _ in range(int(input())): input() r = list(map(int, input().split())) c = list(map(int, input().split())) a = sorted((x, x + 1 - y) for x, y in zip(r, c)) if a[0] != (1, 1): a = [(1, 1)] + a ans = 0 for (x, y), (nx, ny) in zip(a, a[1:]): assert x < nx and y <= ny and ny - y <= nx - x if y == ny and y % 2: ans += nx - x else: ans += (ny + 1) // 2 - (y + 1) // 2 print(ans, sep="\n")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING
Consider an infinite triangle made up of layers. Let's number the layers, starting from one, from the top of the triangle (from top to bottom). The $k$-th layer of the triangle contains $k$ points, numbered from left to right. Each point of an infinite triangle is described by a pair of numbers $(r, c)$ ($1 \le c \le r$), where $r$ is the number of the layer, and $c$ is the number of the point in the layer. From each point $(r, c)$ there are two directed edges to the points $(r+1, c)$ and $(r+1, c+1)$, but only one of the edges is activated. If $r + c$ is even, then the edge to the point $(r+1, c)$ is activated, otherwise the edge to the point $(r+1, c+1)$ is activated. Look at the picture for a better understanding. Activated edges are colored in black. Non-activated edges are colored in gray. From the point $(r_1, c_1)$ it is possible to reach the point $(r_2, c_2)$, if there is a path between them only from activated edges. For example, in the picture above, there is a path from $(1, 1)$ to $(3, 2)$, but there is no path from $(2, 1)$ to $(1, 1)$. Initially, you are at the point $(1, 1)$. For each turn, you can: Replace activated edge for point $(r, c)$. That is if the edge to the point $(r+1, c)$ is activated, then instead of it, the edge to the point $(r+1, c+1)$ becomes activated, otherwise if the edge to the point $(r+1, c+1)$, then instead if it, the edge to the point $(r+1, c)$ becomes activated. This action increases the cost of the path by $1$; Move from the current point to another by following the activated edge. This action does not increase the cost of the path. You are given a sequence of $n$ points of an infinite triangle $(r_1, c_1), (r_2, c_2), \ldots, (r_n, c_n)$. Find the minimum cost path from $(1, 1)$, passing through all $n$ points in arbitrary order. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) is the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$) is the number of points to visit. The second line contains $n$ numbers $r_1, r_2, \ldots, r_n$ ($1 \le r_i \le 10^9$), where $r_i$ is the number of the layer in which $i$-th point is located. The third line contains $n$ numbers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le r_i$), where $c_i$ is the number of the $i$-th point in the $r_i$ layer. It is guaranteed that all $n$ points are distinct. It is guaranteed that there is always at least one way to traverse all $n$ points. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output the minimum cost of a path passing through all points in the corresponding test case. -----Examples----- Input 4 3 1 4 2 1 3 1 2 2 4 2 3 2 1 1000000000 1 1000000000 4 3 10 5 8 2 5 2 4 Output 0 1 999999999 2 -----Note----- None
from sys import stdin, stdout def triangular_paths(n, r_a, c_a): rc_a = [[1, 1]] for i in range(n): rc_a.append([r_a[i], c_a[i]]) rc_a.sort(key=lambda x: x[0]) res = 0 for i in range(1, n + 1): r1 = rc_a[i - 1][0] c1 = rc_a[i - 1][1] r2 = rc_a[i][0] c2 = rc_a[i][1] if (r1 + c1) % 2 == 1 and (r2 + c2) % 2 == 1: res += cal_1(r1, c1, r2, c2) elif (r1 + c1) % 2 == 1 and (r2 + c2) % 2 == 0: res += cal_2(r1, c1, r2, c2) elif (r1 + c1) % 2 == 0 and (r2 + c2) % 2 == 1: res += cal_3(r1, c1, r2, c2) elif (r1 + c1) % 2 == 0 and (r2 + c2) % 2 == 0: res += cal_4(r1, c1, r2, c2) return res def cal_1(r1, c1, r2, c2): return (r2 - (c2 - c1) - r1) // 2 def cal_2(r1, c1, r2, c2): return cal_1(r1, c1, r2 - 1, c2) + 1 def cal_3(r1, c1, r2, c2): return cal_1(r1 + 1, c1, r2, c2) def cal_4(r1, c1, r2, c2): if r1 - c1 == r2 - c2: return c2 - c1 else: return cal_2(r1 + 1, c1, r2, c2) t = int(stdin.readline()) for _ in range(t): n = int(stdin.readline()) r_a = list(map(int, stdin.readline().split())) c_a = list(map(int, stdin.readline().split())) r = triangular_paths(n, r_a, c_a) stdout.write(str(r) + "\n")
FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF IF BIN_OP VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
Consider an infinite triangle made up of layers. Let's number the layers, starting from one, from the top of the triangle (from top to bottom). The $k$-th layer of the triangle contains $k$ points, numbered from left to right. Each point of an infinite triangle is described by a pair of numbers $(r, c)$ ($1 \le c \le r$), where $r$ is the number of the layer, and $c$ is the number of the point in the layer. From each point $(r, c)$ there are two directed edges to the points $(r+1, c)$ and $(r+1, c+1)$, but only one of the edges is activated. If $r + c$ is even, then the edge to the point $(r+1, c)$ is activated, otherwise the edge to the point $(r+1, c+1)$ is activated. Look at the picture for a better understanding. Activated edges are colored in black. Non-activated edges are colored in gray. From the point $(r_1, c_1)$ it is possible to reach the point $(r_2, c_2)$, if there is a path between them only from activated edges. For example, in the picture above, there is a path from $(1, 1)$ to $(3, 2)$, but there is no path from $(2, 1)$ to $(1, 1)$. Initially, you are at the point $(1, 1)$. For each turn, you can: Replace activated edge for point $(r, c)$. That is if the edge to the point $(r+1, c)$ is activated, then instead of it, the edge to the point $(r+1, c+1)$ becomes activated, otherwise if the edge to the point $(r+1, c+1)$, then instead if it, the edge to the point $(r+1, c)$ becomes activated. This action increases the cost of the path by $1$; Move from the current point to another by following the activated edge. This action does not increase the cost of the path. You are given a sequence of $n$ points of an infinite triangle $(r_1, c_1), (r_2, c_2), \ldots, (r_n, c_n)$. Find the minimum cost path from $(1, 1)$, passing through all $n$ points in arbitrary order. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) is the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$) is the number of points to visit. The second line contains $n$ numbers $r_1, r_2, \ldots, r_n$ ($1 \le r_i \le 10^9$), where $r_i$ is the number of the layer in which $i$-th point is located. The third line contains $n$ numbers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le r_i$), where $c_i$ is the number of the $i$-th point in the $r_i$ layer. It is guaranteed that all $n$ points are distinct. It is guaranteed that there is always at least one way to traverse all $n$ points. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output the minimum cost of a path passing through all points in the corresponding test case. -----Examples----- Input 4 3 1 4 2 1 3 1 2 2 4 2 3 2 1 1000000000 1 1000000000 4 3 10 5 8 2 5 2 4 Output 0 1 999999999 2 -----Note----- None
import sys input = sys.stdin.readline def cost(a, b): rdiff = b[0] - a[0] cdiff = b[1] - a[1] if (a[0] + a[1]) % 2 == 0: if rdiff == cdiff: return rdiff if rdiff == cdiff + 1: return 0 if rdiff % 2 == 0: return rdiff // 2 - (cdiff + 1) // 2 if rdiff % 2 == 1: return rdiff // 2 - cdiff // 2 if (a[0] + a[1]) % 2 == 1: if rdiff == cdiff: return 0 if rdiff == cdiff + 1 or rdiff == cdiff + 2: return 1 if rdiff % 2 == 0: return (rdiff + 1) // 2 - cdiff // 2 if rdiff % 2 == 1: return (rdiff + 1) // 2 - (cdiff + 1) // 2 t = int(input()) for i in range(t): n = int(input()) r = [int(x) for x in input().split()] c = [int(x) for x in input().split()] points = sorted(zip(r, c)) ans = cost((1, 1), points[0]) for i in range(n - 1): ans += cost(points[i], points[i + 1]) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER IF VAR VAR RETURN VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER IF VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Consider an infinite triangle made up of layers. Let's number the layers, starting from one, from the top of the triangle (from top to bottom). The $k$-th layer of the triangle contains $k$ points, numbered from left to right. Each point of an infinite triangle is described by a pair of numbers $(r, c)$ ($1 \le c \le r$), where $r$ is the number of the layer, and $c$ is the number of the point in the layer. From each point $(r, c)$ there are two directed edges to the points $(r+1, c)$ and $(r+1, c+1)$, but only one of the edges is activated. If $r + c$ is even, then the edge to the point $(r+1, c)$ is activated, otherwise the edge to the point $(r+1, c+1)$ is activated. Look at the picture for a better understanding. Activated edges are colored in black. Non-activated edges are colored in gray. From the point $(r_1, c_1)$ it is possible to reach the point $(r_2, c_2)$, if there is a path between them only from activated edges. For example, in the picture above, there is a path from $(1, 1)$ to $(3, 2)$, but there is no path from $(2, 1)$ to $(1, 1)$. Initially, you are at the point $(1, 1)$. For each turn, you can: Replace activated edge for point $(r, c)$. That is if the edge to the point $(r+1, c)$ is activated, then instead of it, the edge to the point $(r+1, c+1)$ becomes activated, otherwise if the edge to the point $(r+1, c+1)$, then instead if it, the edge to the point $(r+1, c)$ becomes activated. This action increases the cost of the path by $1$; Move from the current point to another by following the activated edge. This action does not increase the cost of the path. You are given a sequence of $n$ points of an infinite triangle $(r_1, c_1), (r_2, c_2), \ldots, (r_n, c_n)$. Find the minimum cost path from $(1, 1)$, passing through all $n$ points in arbitrary order. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) is the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$) is the number of points to visit. The second line contains $n$ numbers $r_1, r_2, \ldots, r_n$ ($1 \le r_i \le 10^9$), where $r_i$ is the number of the layer in which $i$-th point is located. The third line contains $n$ numbers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le r_i$), where $c_i$ is the number of the $i$-th point in the $r_i$ layer. It is guaranteed that all $n$ points are distinct. It is guaranteed that there is always at least one way to traverse all $n$ points. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output the minimum cost of a path passing through all points in the corresponding test case. -----Examples----- Input 4 3 1 4 2 1 3 1 2 2 4 2 3 2 1 1000000000 1 1000000000 4 3 10 5 8 2 5 2 4 Output 0 1 999999999 2 -----Note----- None
t = int(input()) for i in range(t): n = int(input()) r = list(map(int, input().split())) c = list(map(int, input().split())) z = [] z.append((1, 1)) for i in range(n): z.append((r[i], c[i])) z.sort() node = z res = 0 for i in range(n): r, c = node[i] nr, nc = node[i + 1] res += ( (nr - nc) // 2 - (r - c) // 2 + (nr - r if r - c == nr - nc and (r - c) % 2 == 0 else 0) ) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Consider an infinite triangle made up of layers. Let's number the layers, starting from one, from the top of the triangle (from top to bottom). The $k$-th layer of the triangle contains $k$ points, numbered from left to right. Each point of an infinite triangle is described by a pair of numbers $(r, c)$ ($1 \le c \le r$), where $r$ is the number of the layer, and $c$ is the number of the point in the layer. From each point $(r, c)$ there are two directed edges to the points $(r+1, c)$ and $(r+1, c+1)$, but only one of the edges is activated. If $r + c$ is even, then the edge to the point $(r+1, c)$ is activated, otherwise the edge to the point $(r+1, c+1)$ is activated. Look at the picture for a better understanding. Activated edges are colored in black. Non-activated edges are colored in gray. From the point $(r_1, c_1)$ it is possible to reach the point $(r_2, c_2)$, if there is a path between them only from activated edges. For example, in the picture above, there is a path from $(1, 1)$ to $(3, 2)$, but there is no path from $(2, 1)$ to $(1, 1)$. Initially, you are at the point $(1, 1)$. For each turn, you can: Replace activated edge for point $(r, c)$. That is if the edge to the point $(r+1, c)$ is activated, then instead of it, the edge to the point $(r+1, c+1)$ becomes activated, otherwise if the edge to the point $(r+1, c+1)$, then instead if it, the edge to the point $(r+1, c)$ becomes activated. This action increases the cost of the path by $1$; Move from the current point to another by following the activated edge. This action does not increase the cost of the path. You are given a sequence of $n$ points of an infinite triangle $(r_1, c_1), (r_2, c_2), \ldots, (r_n, c_n)$. Find the minimum cost path from $(1, 1)$, passing through all $n$ points in arbitrary order. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) is the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$) is the number of points to visit. The second line contains $n$ numbers $r_1, r_2, \ldots, r_n$ ($1 \le r_i \le 10^9$), where $r_i$ is the number of the layer in which $i$-th point is located. The third line contains $n$ numbers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le r_i$), where $c_i$ is the number of the $i$-th point in the $r_i$ layer. It is guaranteed that all $n$ points are distinct. It is guaranteed that there is always at least one way to traverse all $n$ points. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output the minimum cost of a path passing through all points in the corresponding test case. -----Examples----- Input 4 3 1 4 2 1 3 1 2 2 4 2 3 2 1 1000000000 1 1000000000 4 3 10 5 8 2 5 2 4 Output 0 1 999999999 2 -----Note----- None
for case in range(int(input())): n = int(input()) X = list(map(int, input().split())) Y = list(map(int, input().split())) li = [] for i in range(n): x = X[i] y = Y[i] li.append((abs(x - y + 1), min(x, y))) li.sort() prvblk = 1 prvplc = 1 cost = 0 for blk, plc in li: hor = blk - prvblk ver = plc - prvplc if hor == 0: if prvblk % 2: cost += ver elif prvblk % 2: cost += hor // 2 else: cost += (hor + 1) // 2 prvblk = blk prvplc = plc print(cost)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Consider an infinite triangle made up of layers. Let's number the layers, starting from one, from the top of the triangle (from top to bottom). The $k$-th layer of the triangle contains $k$ points, numbered from left to right. Each point of an infinite triangle is described by a pair of numbers $(r, c)$ ($1 \le c \le r$), where $r$ is the number of the layer, and $c$ is the number of the point in the layer. From each point $(r, c)$ there are two directed edges to the points $(r+1, c)$ and $(r+1, c+1)$, but only one of the edges is activated. If $r + c$ is even, then the edge to the point $(r+1, c)$ is activated, otherwise the edge to the point $(r+1, c+1)$ is activated. Look at the picture for a better understanding. Activated edges are colored in black. Non-activated edges are colored in gray. From the point $(r_1, c_1)$ it is possible to reach the point $(r_2, c_2)$, if there is a path between them only from activated edges. For example, in the picture above, there is a path from $(1, 1)$ to $(3, 2)$, but there is no path from $(2, 1)$ to $(1, 1)$. Initially, you are at the point $(1, 1)$. For each turn, you can: Replace activated edge for point $(r, c)$. That is if the edge to the point $(r+1, c)$ is activated, then instead of it, the edge to the point $(r+1, c+1)$ becomes activated, otherwise if the edge to the point $(r+1, c+1)$, then instead if it, the edge to the point $(r+1, c)$ becomes activated. This action increases the cost of the path by $1$; Move from the current point to another by following the activated edge. This action does not increase the cost of the path. You are given a sequence of $n$ points of an infinite triangle $(r_1, c_1), (r_2, c_2), \ldots, (r_n, c_n)$. Find the minimum cost path from $(1, 1)$, passing through all $n$ points in arbitrary order. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) is the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$) is the number of points to visit. The second line contains $n$ numbers $r_1, r_2, \ldots, r_n$ ($1 \le r_i \le 10^9$), where $r_i$ is the number of the layer in which $i$-th point is located. The third line contains $n$ numbers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le r_i$), where $c_i$ is the number of the $i$-th point in the $r_i$ layer. It is guaranteed that all $n$ points are distinct. It is guaranteed that there is always at least one way to traverse all $n$ points. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output the minimum cost of a path passing through all points in the corresponding test case. -----Examples----- Input 4 3 1 4 2 1 3 1 2 2 4 2 3 2 1 1000000000 1 1000000000 4 3 10 5 8 2 5 2 4 Output 0 1 999999999 2 -----Note----- None
import sys for _ in range(int(input())): n = int(input()) f = lambda: map(int, sys.stdin.readline().split()) node = [(1, 1)] + sorted(zip(f(), f())) res = 0 for i in range(n): r, c = node[i] nr, nc = node[i + 1] res += ( (nr - nc) // 2 - (r - c) // 2 + (nr - r if r - c == nr - nc and (r - c) % 2 == 0 else 0) ) print(res)
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Consider an infinite triangle made up of layers. Let's number the layers, starting from one, from the top of the triangle (from top to bottom). The $k$-th layer of the triangle contains $k$ points, numbered from left to right. Each point of an infinite triangle is described by a pair of numbers $(r, c)$ ($1 \le c \le r$), where $r$ is the number of the layer, and $c$ is the number of the point in the layer. From each point $(r, c)$ there are two directed edges to the points $(r+1, c)$ and $(r+1, c+1)$, but only one of the edges is activated. If $r + c$ is even, then the edge to the point $(r+1, c)$ is activated, otherwise the edge to the point $(r+1, c+1)$ is activated. Look at the picture for a better understanding. Activated edges are colored in black. Non-activated edges are colored in gray. From the point $(r_1, c_1)$ it is possible to reach the point $(r_2, c_2)$, if there is a path between them only from activated edges. For example, in the picture above, there is a path from $(1, 1)$ to $(3, 2)$, but there is no path from $(2, 1)$ to $(1, 1)$. Initially, you are at the point $(1, 1)$. For each turn, you can: Replace activated edge for point $(r, c)$. That is if the edge to the point $(r+1, c)$ is activated, then instead of it, the edge to the point $(r+1, c+1)$ becomes activated, otherwise if the edge to the point $(r+1, c+1)$, then instead if it, the edge to the point $(r+1, c)$ becomes activated. This action increases the cost of the path by $1$; Move from the current point to another by following the activated edge. This action does not increase the cost of the path. You are given a sequence of $n$ points of an infinite triangle $(r_1, c_1), (r_2, c_2), \ldots, (r_n, c_n)$. Find the minimum cost path from $(1, 1)$, passing through all $n$ points in arbitrary order. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) is the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$) is the number of points to visit. The second line contains $n$ numbers $r_1, r_2, \ldots, r_n$ ($1 \le r_i \le 10^9$), where $r_i$ is the number of the layer in which $i$-th point is located. The third line contains $n$ numbers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le r_i$), where $c_i$ is the number of the $i$-th point in the $r_i$ layer. It is guaranteed that all $n$ points are distinct. It is guaranteed that there is always at least one way to traverse all $n$ points. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output the minimum cost of a path passing through all points in the corresponding test case. -----Examples----- Input 4 3 1 4 2 1 3 1 2 2 4 2 3 2 1 1000000000 1 1000000000 4 3 10 5 8 2 5 2 4 Output 0 1 999999999 2 -----Note----- None
import sys input = sys.stdin.readline def main(): t = int(input()) for _ in range(t): n = int(input()) r = list(map(int, input().split())) c = list(map(int, input().split())) points = [(r[i], c[i]) for i in range(n)] points.sort(key=lambda x: x[0]) if points[0] != (1, 1): points = [(1, 1)] + points n += 1 cost = 0 for i in range(n - 1): prev = points[i] now = points[i + 1] prev_diff = prev[0] - prev[1] now_diff = now[0] - now[1] if prev_diff == now_diff: if prev_diff % 2 == 0: cost += now[0] - prev[0] else: pass elif prev_diff + 1 == now_diff and prev_diff % 2 == 0: pass else: cost += now_diff // 2 - prev_diff // 2 print(cost) main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Consider an infinite triangle made up of layers. Let's number the layers, starting from one, from the top of the triangle (from top to bottom). The $k$-th layer of the triangle contains $k$ points, numbered from left to right. Each point of an infinite triangle is described by a pair of numbers $(r, c)$ ($1 \le c \le r$), where $r$ is the number of the layer, and $c$ is the number of the point in the layer. From each point $(r, c)$ there are two directed edges to the points $(r+1, c)$ and $(r+1, c+1)$, but only one of the edges is activated. If $r + c$ is even, then the edge to the point $(r+1, c)$ is activated, otherwise the edge to the point $(r+1, c+1)$ is activated. Look at the picture for a better understanding. Activated edges are colored in black. Non-activated edges are colored in gray. From the point $(r_1, c_1)$ it is possible to reach the point $(r_2, c_2)$, if there is a path between them only from activated edges. For example, in the picture above, there is a path from $(1, 1)$ to $(3, 2)$, but there is no path from $(2, 1)$ to $(1, 1)$. Initially, you are at the point $(1, 1)$. For each turn, you can: Replace activated edge for point $(r, c)$. That is if the edge to the point $(r+1, c)$ is activated, then instead of it, the edge to the point $(r+1, c+1)$ becomes activated, otherwise if the edge to the point $(r+1, c+1)$, then instead if it, the edge to the point $(r+1, c)$ becomes activated. This action increases the cost of the path by $1$; Move from the current point to another by following the activated edge. This action does not increase the cost of the path. You are given a sequence of $n$ points of an infinite triangle $(r_1, c_1), (r_2, c_2), \ldots, (r_n, c_n)$. Find the minimum cost path from $(1, 1)$, passing through all $n$ points in arbitrary order. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) is the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$) is the number of points to visit. The second line contains $n$ numbers $r_1, r_2, \ldots, r_n$ ($1 \le r_i \le 10^9$), where $r_i$ is the number of the layer in which $i$-th point is located. The third line contains $n$ numbers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le r_i$), where $c_i$ is the number of the $i$-th point in the $r_i$ layer. It is guaranteed that all $n$ points are distinct. It is guaranteed that there is always at least one way to traverse all $n$ points. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output the minimum cost of a path passing through all points in the corresponding test case. -----Examples----- Input 4 3 1 4 2 1 3 1 2 2 4 2 3 2 1 1000000000 1 1000000000 4 3 10 5 8 2 5 2 4 Output 0 1 999999999 2 -----Note----- None
import sys def get_ints(): return list(map(int, sys.stdin.readline().strip().split())) def solve(N, R, C): ans = 0 sorted_idx = sorted(range(N), key=lambda i: (R[i], C[i])) r, c = 1, 1 for i in sorted_idx: diff = r - c next_r, next_c = R[i], C[i] next_diff = next_r - next_c if r == next_r and c == next_c: continue if diff == next_diff and not r + c & 1: ans += next_c - c elif diff // 2 != next_diff // 2: ans += abs(diff // 2 - next_diff // 2) r, c = next_r, next_c return ans T = int(input()) for _ in range(T): N = int(input()) R = get_ints() C = get_ints() ans = solve(N, R, C) print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Consider an infinite triangle made up of layers. Let's number the layers, starting from one, from the top of the triangle (from top to bottom). The $k$-th layer of the triangle contains $k$ points, numbered from left to right. Each point of an infinite triangle is described by a pair of numbers $(r, c)$ ($1 \le c \le r$), where $r$ is the number of the layer, and $c$ is the number of the point in the layer. From each point $(r, c)$ there are two directed edges to the points $(r+1, c)$ and $(r+1, c+1)$, but only one of the edges is activated. If $r + c$ is even, then the edge to the point $(r+1, c)$ is activated, otherwise the edge to the point $(r+1, c+1)$ is activated. Look at the picture for a better understanding. Activated edges are colored in black. Non-activated edges are colored in gray. From the point $(r_1, c_1)$ it is possible to reach the point $(r_2, c_2)$, if there is a path between them only from activated edges. For example, in the picture above, there is a path from $(1, 1)$ to $(3, 2)$, but there is no path from $(2, 1)$ to $(1, 1)$. Initially, you are at the point $(1, 1)$. For each turn, you can: Replace activated edge for point $(r, c)$. That is if the edge to the point $(r+1, c)$ is activated, then instead of it, the edge to the point $(r+1, c+1)$ becomes activated, otherwise if the edge to the point $(r+1, c+1)$, then instead if it, the edge to the point $(r+1, c)$ becomes activated. This action increases the cost of the path by $1$; Move from the current point to another by following the activated edge. This action does not increase the cost of the path. You are given a sequence of $n$ points of an infinite triangle $(r_1, c_1), (r_2, c_2), \ldots, (r_n, c_n)$. Find the minimum cost path from $(1, 1)$, passing through all $n$ points in arbitrary order. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) is the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$) is the number of points to visit. The second line contains $n$ numbers $r_1, r_2, \ldots, r_n$ ($1 \le r_i \le 10^9$), where $r_i$ is the number of the layer in which $i$-th point is located. The third line contains $n$ numbers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le r_i$), where $c_i$ is the number of the $i$-th point in the $r_i$ layer. It is guaranteed that all $n$ points are distinct. It is guaranteed that there is always at least one way to traverse all $n$ points. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output the minimum cost of a path passing through all points in the corresponding test case. -----Examples----- Input 4 3 1 4 2 1 3 1 2 2 4 2 3 2 1 1000000000 1 1000000000 4 3 10 5 8 2 5 2 4 Output 0 1 999999999 2 -----Note----- None
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) r = list(map(int, input().split())) c = list(map(int, input().split())) ps = [(r[i], c[i]) for i in range(n)] ps.sort() y = 1 x = 1 res = 0 for i in range(n): u, v = ps[i] if y == u and x == v: continue if (y + x) % 2 == 0: if y + 1 == u and x + 1 == v: res += 1 y, x = u, v continue y += 1 uv = u - v yx = y - x if uv >= yx: res += -((yx - uv) // 2) else: res += abs(v - x) y, x = u, v print(res)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Consider an infinite triangle made up of layers. Let's number the layers, starting from one, from the top of the triangle (from top to bottom). The $k$-th layer of the triangle contains $k$ points, numbered from left to right. Each point of an infinite triangle is described by a pair of numbers $(r, c)$ ($1 \le c \le r$), where $r$ is the number of the layer, and $c$ is the number of the point in the layer. From each point $(r, c)$ there are two directed edges to the points $(r+1, c)$ and $(r+1, c+1)$, but only one of the edges is activated. If $r + c$ is even, then the edge to the point $(r+1, c)$ is activated, otherwise the edge to the point $(r+1, c+1)$ is activated. Look at the picture for a better understanding. Activated edges are colored in black. Non-activated edges are colored in gray. From the point $(r_1, c_1)$ it is possible to reach the point $(r_2, c_2)$, if there is a path between them only from activated edges. For example, in the picture above, there is a path from $(1, 1)$ to $(3, 2)$, but there is no path from $(2, 1)$ to $(1, 1)$. Initially, you are at the point $(1, 1)$. For each turn, you can: Replace activated edge for point $(r, c)$. That is if the edge to the point $(r+1, c)$ is activated, then instead of it, the edge to the point $(r+1, c+1)$ becomes activated, otherwise if the edge to the point $(r+1, c+1)$, then instead if it, the edge to the point $(r+1, c)$ becomes activated. This action increases the cost of the path by $1$; Move from the current point to another by following the activated edge. This action does not increase the cost of the path. You are given a sequence of $n$ points of an infinite triangle $(r_1, c_1), (r_2, c_2), \ldots, (r_n, c_n)$. Find the minimum cost path from $(1, 1)$, passing through all $n$ points in arbitrary order. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) is the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$) is the number of points to visit. The second line contains $n$ numbers $r_1, r_2, \ldots, r_n$ ($1 \le r_i \le 10^9$), where $r_i$ is the number of the layer in which $i$-th point is located. The third line contains $n$ numbers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le r_i$), where $c_i$ is the number of the $i$-th point in the $r_i$ layer. It is guaranteed that all $n$ points are distinct. It is guaranteed that there is always at least one way to traverse all $n$ points. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output the minimum cost of a path passing through all points in the corresponding test case. -----Examples----- Input 4 3 1 4 2 1 3 1 2 2 4 2 3 2 1 1000000000 1 1000000000 4 3 10 5 8 2 5 2 4 Output 0 1 999999999 2 -----Note----- None
def get_input(): l = [int(e) for e in input().split()] return l def band(p): d = p[0] - p[1] return d // 2 def cost_path(p1, p2): b1 = band(p2) b2 = band(p1) if b1 > b2: return b1 - b2 elif p1[0] - p1[1] == p2[0] - p2[1]: if (p1[0] - p1[1]) % 2: return 0 else: return p2[1] - p1[1] else: return 0 def main(n, r, c): ps = [(ri, ci) for ri, ci in zip(r, c)] ps.append((1, 1)) ps.sort() c = 0 for i in range(n): c += cost_path(ps[i], ps[i + 1]) print(c) t = get_input()[0] for _ in range(t): n = get_input()[0] r = get_input() c = get_input() main(n, r, c)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER RETURN NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
Consider an infinite triangle made up of layers. Let's number the layers, starting from one, from the top of the triangle (from top to bottom). The $k$-th layer of the triangle contains $k$ points, numbered from left to right. Each point of an infinite triangle is described by a pair of numbers $(r, c)$ ($1 \le c \le r$), where $r$ is the number of the layer, and $c$ is the number of the point in the layer. From each point $(r, c)$ there are two directed edges to the points $(r+1, c)$ and $(r+1, c+1)$, but only one of the edges is activated. If $r + c$ is even, then the edge to the point $(r+1, c)$ is activated, otherwise the edge to the point $(r+1, c+1)$ is activated. Look at the picture for a better understanding. Activated edges are colored in black. Non-activated edges are colored in gray. From the point $(r_1, c_1)$ it is possible to reach the point $(r_2, c_2)$, if there is a path between them only from activated edges. For example, in the picture above, there is a path from $(1, 1)$ to $(3, 2)$, but there is no path from $(2, 1)$ to $(1, 1)$. Initially, you are at the point $(1, 1)$. For each turn, you can: Replace activated edge for point $(r, c)$. That is if the edge to the point $(r+1, c)$ is activated, then instead of it, the edge to the point $(r+1, c+1)$ becomes activated, otherwise if the edge to the point $(r+1, c+1)$, then instead if it, the edge to the point $(r+1, c)$ becomes activated. This action increases the cost of the path by $1$; Move from the current point to another by following the activated edge. This action does not increase the cost of the path. You are given a sequence of $n$ points of an infinite triangle $(r_1, c_1), (r_2, c_2), \ldots, (r_n, c_n)$. Find the minimum cost path from $(1, 1)$, passing through all $n$ points in arbitrary order. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) is the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$) is the number of points to visit. The second line contains $n$ numbers $r_1, r_2, \ldots, r_n$ ($1 \le r_i \le 10^9$), where $r_i$ is the number of the layer in which $i$-th point is located. The third line contains $n$ numbers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le r_i$), where $c_i$ is the number of the $i$-th point in the $r_i$ layer. It is guaranteed that all $n$ points are distinct. It is guaranteed that there is always at least one way to traverse all $n$ points. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output the minimum cost of a path passing through all points in the corresponding test case. -----Examples----- Input 4 3 1 4 2 1 3 1 2 2 4 2 3 2 1 1000000000 1 1000000000 4 3 10 5 8 2 5 2 4 Output 0 1 999999999 2 -----Note----- None
def seal(a, b): return (a + b - 1) // b for _ in range(int(input())): n = int(input()) r = [int(x) for x in input().split()] c = [int(x) for x in input().split()] arr = [[r[i], c[i]] for i in range(n)] arr.sort() ans = 0 if arr[0] != [1, 1]: arr.insert(0, [1, 1]) n = len(arr) prev = [1, 1] for i in range(1, n): curr = arr[i] if sum(prev) & 1: if curr[0] - prev[0] & 1: ans += seal(curr[0] - prev[0], 2) - seal(curr[1] - prev[1], 2) else: ans += seal(curr[0] - prev[0], 2) - (curr[1] - prev[1] >> 1) elif curr[0] - prev[0] == curr[1] - prev[1]: ans += curr[0] - prev[0] elif curr[0] - prev[0] & 1: ans += (curr[0] - prev[0] >> 1) - (curr[1] - prev[1] >> 1) else: ans += (curr[0] - prev[0] >> 1) - seal(curr[1] - prev[1], 2) prev = curr print(ans)
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER LIST NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Consider an infinite triangle made up of layers. Let's number the layers, starting from one, from the top of the triangle (from top to bottom). The $k$-th layer of the triangle contains $k$ points, numbered from left to right. Each point of an infinite triangle is described by a pair of numbers $(r, c)$ ($1 \le c \le r$), where $r$ is the number of the layer, and $c$ is the number of the point in the layer. From each point $(r, c)$ there are two directed edges to the points $(r+1, c)$ and $(r+1, c+1)$, but only one of the edges is activated. If $r + c$ is even, then the edge to the point $(r+1, c)$ is activated, otherwise the edge to the point $(r+1, c+1)$ is activated. Look at the picture for a better understanding. Activated edges are colored in black. Non-activated edges are colored in gray. From the point $(r_1, c_1)$ it is possible to reach the point $(r_2, c_2)$, if there is a path between them only from activated edges. For example, in the picture above, there is a path from $(1, 1)$ to $(3, 2)$, but there is no path from $(2, 1)$ to $(1, 1)$. Initially, you are at the point $(1, 1)$. For each turn, you can: Replace activated edge for point $(r, c)$. That is if the edge to the point $(r+1, c)$ is activated, then instead of it, the edge to the point $(r+1, c+1)$ becomes activated, otherwise if the edge to the point $(r+1, c+1)$, then instead if it, the edge to the point $(r+1, c)$ becomes activated. This action increases the cost of the path by $1$; Move from the current point to another by following the activated edge. This action does not increase the cost of the path. You are given a sequence of $n$ points of an infinite triangle $(r_1, c_1), (r_2, c_2), \ldots, (r_n, c_n)$. Find the minimum cost path from $(1, 1)$, passing through all $n$ points in arbitrary order. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) is the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$) is the number of points to visit. The second line contains $n$ numbers $r_1, r_2, \ldots, r_n$ ($1 \le r_i \le 10^9$), where $r_i$ is the number of the layer in which $i$-th point is located. The third line contains $n$ numbers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le r_i$), where $c_i$ is the number of the $i$-th point in the $r_i$ layer. It is guaranteed that all $n$ points are distinct. It is guaranteed that there is always at least one way to traverse all $n$ points. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output the minimum cost of a path passing through all points in the corresponding test case. -----Examples----- Input 4 3 1 4 2 1 3 1 2 2 4 2 3 2 1 1000000000 1 1000000000 4 3 10 5 8 2 5 2 4 Output 0 1 999999999 2 -----Note----- None
for _ in range(int(input())): n = int(input()) r = list(map(int, input().split())) c = list(map(int, input().split())) arr = sorted(list(zip(r, c))) correction = 1, 1 ans = 0 check = True for point in arr: r, c = point[0] - correction[0] + 1, point[1] - correction[1] + 1 depth = r - 1 if r == c: if check: ans += depth else: if not check: ans += 1 ans += (r - c - int(not check)) // 2 correction = point if (r + c) % 2 != 0: check = not check print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR VAR IF VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
An array is called *interesting* if no subarray of length greater than 2 is non-increasing or non-decreasing. Chef has an array A of length N. He wants to make the array interesting by rearranging the elements in any order. If there exist multiple such arrays, output any one. If no such array exists, print -1 instead. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N denoting the length of array A. - The next line contains N space separated integers, A_{1}, A_{2}, \ldots, A_{N} ------ Output Format ------ For each test case, output on a single line, any possible interesting array. If no such array is possible, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 2 4 2 6 5 2 5 5 5 2 4 2 ----- Sample Output 1 ------ -1 6 2 5 2 5 2 4 2 5 ----- explanation 1 ------ Test case $1$: There is no way of arranging the elements such that no subarray of length greater than $2$ is non-increasing or non-decreasing. Test case $2$: A possible rearrangement of the elements is $[6, 2, 5, 2]$. Note that the subarrays of length greater than $2$ are $\{[6, 2, 5], [2, 5, 2], [6, 2, 5, 2]\}$. None of these subarrays are non-increasing or non-decreasing. Test case $3$: A possible rearrangement of the elements is $[5, 2, 4, 2, 5]$. Note that the subarrays of length greater than $2$ are $\{[5, 2, 4], [2, 4, 2], [4, 2, 5], [5, 2, 4, 2], [2, 4, 2, 5], [5, 2, 4, 2, 5]\}$. None of these subarrays are non-increasing or non-decreasing.
def check(ans, n): inc = int(ans[0] < ans[1]) i = 1 while i < n: if ans[i] == ans[i - 1]: return False if int(ans[i] > ans[i - 1]) == inc: inc = int(ans[i] < ans[i - 1]) else: return False i += 1 return True for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) if n <= 2: print(*arr) continue arr.sort() ans = [] i = 0 j = int((n + 1) / 2) half = j while i < half: ans.append(arr[i]) if j < n: ans.append(arr[j]) i += 1 j += 1 if check(ans, n): print(*ans) continue ans = [] i = 0 j = int(n / 2) while j < n: ans.append(arr[j]) if i < n / 2: ans.append(arr[i]) i += 1 j += 1 if check(ans, n): print(*ans) continue print(-1)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
An array is called *interesting* if no subarray of length greater than 2 is non-increasing or non-decreasing. Chef has an array A of length N. He wants to make the array interesting by rearranging the elements in any order. If there exist multiple such arrays, output any one. If no such array exists, print -1 instead. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N denoting the length of array A. - The next line contains N space separated integers, A_{1}, A_{2}, \ldots, A_{N} ------ Output Format ------ For each test case, output on a single line, any possible interesting array. If no such array is possible, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 2 4 2 6 5 2 5 5 5 2 4 2 ----- Sample Output 1 ------ -1 6 2 5 2 5 2 4 2 5 ----- explanation 1 ------ Test case $1$: There is no way of arranging the elements such that no subarray of length greater than $2$ is non-increasing or non-decreasing. Test case $2$: A possible rearrangement of the elements is $[6, 2, 5, 2]$. Note that the subarrays of length greater than $2$ are $\{[6, 2, 5], [2, 5, 2], [6, 2, 5, 2]\}$. None of these subarrays are non-increasing or non-decreasing. Test case $3$: A possible rearrangement of the elements is $[5, 2, 4, 2, 5]$. Note that the subarrays of length greater than $2$ are $\{[5, 2, 4], [2, 4, 2], [4, 2, 5], [5, 2, 4, 2], [2, 4, 2, 5], [5, 2, 4, 2, 5]\}$. None of these subarrays are non-increasing or non-decreasing.
def check(ar): n = len(ar) for i in range(n - 2): if ar[i] >= ar[i + 1] >= ar[i + 2]: return 0 if ar[i] <= ar[i + 1] <= ar[i + 2]: return 0 return 1 for _ in range(int(input())): n = int(input()) a = sorted(list(map(int, input().split()))) b, c = [0] * n, [0] * n b[0::2], b[1::2] = a[: (n + 1) // 2], a[(n + 1) // 2 :] c[0::2], c[1::2] = a[n // 2 :], a[: n // 2] if check(b): print(*b) elif check(c): print(*c) else: print(-1)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
An array is called *interesting* if no subarray of length greater than 2 is non-increasing or non-decreasing. Chef has an array A of length N. He wants to make the array interesting by rearranging the elements in any order. If there exist multiple such arrays, output any one. If no such array exists, print -1 instead. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N denoting the length of array A. - The next line contains N space separated integers, A_{1}, A_{2}, \ldots, A_{N} ------ Output Format ------ For each test case, output on a single line, any possible interesting array. If no such array is possible, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 2 4 2 6 5 2 5 5 5 2 4 2 ----- Sample Output 1 ------ -1 6 2 5 2 5 2 4 2 5 ----- explanation 1 ------ Test case $1$: There is no way of arranging the elements such that no subarray of length greater than $2$ is non-increasing or non-decreasing. Test case $2$: A possible rearrangement of the elements is $[6, 2, 5, 2]$. Note that the subarrays of length greater than $2$ are $\{[6, 2, 5], [2, 5, 2], [6, 2, 5, 2]\}$. None of these subarrays are non-increasing or non-decreasing. Test case $3$: A possible rearrangement of the elements is $[5, 2, 4, 2, 5]$. Note that the subarrays of length greater than $2$ are $\{[5, 2, 4], [2, 4, 2], [4, 2, 5], [5, 2, 4, 2], [2, 4, 2, 5], [5, 2, 4, 2, 5]\}$. None of these subarrays are non-increasing or non-decreasing.
def N(): return int(input()) def A(): return [int(x) for x in input().split()] def S(): return input() def decimalToBinary(n): return bin(n).replace("0b", "") def factors(n: int) -> list: s = set() step = 2 if n % 2 else 1 for i in range(1, n**0.5 + 1, step): if n % i == 0: s.add(i) s.add(n // i) return factors def c(aa): f = False for i in range(1, n - 1): if ( aa[i - 1] < aa[i] and aa[i] > aa[i + 1] or aa[i - 1] > aa[i] and aa[i] < aa[i + 1] ): continue f = True return f def sorts(nums): sortedList = sorted(nums) n = len(nums) if n % 2 == 1: small = sortedList[: 1 + n // 2][::-1] large = sortedList[1 + n // 2 :][::-1] for i in range(1, n, 2): nums[i] = large[i // 2] for i in range(0, n, 2): nums[i] = small[i // 2] else: small = sortedList[: n // 2][::-1] large = sortedList[n // 2 :][::-1] for i in range(1, n, 2): nums[i] = large[i // 2] for i in range(0, n, 2): nums[i] = small[i // 2] for _ in range(N()): n = N() if "codechef" == 28226329: print("Tanmay") a = A() sorts(a) if c(a): print(-1) continue for i in a: print(i, end=" ") print()
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR STRING STRING FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF STRING NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
An array is called *interesting* if no subarray of length greater than 2 is non-increasing or non-decreasing. Chef has an array A of length N. He wants to make the array interesting by rearranging the elements in any order. If there exist multiple such arrays, output any one. If no such array exists, print -1 instead. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N denoting the length of array A. - The next line contains N space separated integers, A_{1}, A_{2}, \ldots, A_{N} ------ Output Format ------ For each test case, output on a single line, any possible interesting array. If no such array is possible, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 2 4 2 6 5 2 5 5 5 2 4 2 ----- Sample Output 1 ------ -1 6 2 5 2 5 2 4 2 5 ----- explanation 1 ------ Test case $1$: There is no way of arranging the elements such that no subarray of length greater than $2$ is non-increasing or non-decreasing. Test case $2$: A possible rearrangement of the elements is $[6, 2, 5, 2]$. Note that the subarrays of length greater than $2$ are $\{[6, 2, 5], [2, 5, 2], [6, 2, 5, 2]\}$. None of these subarrays are non-increasing or non-decreasing. Test case $3$: A possible rearrangement of the elements is $[5, 2, 4, 2, 5]$. Note that the subarrays of length greater than $2$ are $\{[5, 2, 4], [2, 4, 2], [4, 2, 5], [5, 2, 4, 2], [2, 4, 2, 5], [5, 2, 4, 2, 5]\}$. None of these subarrays are non-increasing or non-decreasing.
for t in range(int(input())): n = int(input()) a = list(map(int, input().split())) a.sort() b = [] for i in range(0, n // 2): b += [a[n // 2 + i]] b += [a[i]] if n % 2 != 0: b += [a[-1]] f = 0 for i in range(0, len(b) - 1): if b[i] == b[i + 1]: print(-1) f = 1 break if f == 0: for i in range(len(b)): print(b[i], end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR LIST VAR BIN_OP BIN_OP VAR NUMBER VAR VAR LIST VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
An array is called *interesting* if no subarray of length greater than 2 is non-increasing or non-decreasing. Chef has an array A of length N. He wants to make the array interesting by rearranging the elements in any order. If there exist multiple such arrays, output any one. If no such array exists, print -1 instead. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N denoting the length of array A. - The next line contains N space separated integers, A_{1}, A_{2}, \ldots, A_{N} ------ Output Format ------ For each test case, output on a single line, any possible interesting array. If no such array is possible, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 2 4 2 6 5 2 5 5 5 2 4 2 ----- Sample Output 1 ------ -1 6 2 5 2 5 2 4 2 5 ----- explanation 1 ------ Test case $1$: There is no way of arranging the elements such that no subarray of length greater than $2$ is non-increasing or non-decreasing. Test case $2$: A possible rearrangement of the elements is $[6, 2, 5, 2]$. Note that the subarrays of length greater than $2$ are $\{[6, 2, 5], [2, 5, 2], [6, 2, 5, 2]\}$. None of these subarrays are non-increasing or non-decreasing. Test case $3$: A possible rearrangement of the elements is $[5, 2, 4, 2, 5]$. Note that the subarrays of length greater than $2$ are $\{[5, 2, 4], [2, 4, 2], [4, 2, 5], [5, 2, 4, 2], [2, 4, 2, 5], [5, 2, 4, 2, 5]\}$. None of these subarrays are non-increasing or non-decreasing.
def check(ar): n = len(ar) for i in range(n - 2): if ar[i] >= ar[i + 1] >= ar[i + 2]: return 0 if ar[i] <= ar[i + 1] <= ar[i + 2]: return 0 return 1 def interesting_array(n, a): a.sort() b, c = [0] * n, [0] * n b[::2], b[1::2] = a[: (n + 1) // 2], a[(n + 1) // 2 :] c[::2], c[1::2] = a[n // 2 :], a[: n // 2] if check(b): print(*b) elif check(c): print(*c) else: print(-1) t = int(input()) for _ in range(t): n = int(input()) arr = [int(x) for x in input().split()] interesting_array(n, arr)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
An array is called *interesting* if no subarray of length greater than 2 is non-increasing or non-decreasing. Chef has an array A of length N. He wants to make the array interesting by rearranging the elements in any order. If there exist multiple such arrays, output any one. If no such array exists, print -1 instead. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N denoting the length of array A. - The next line contains N space separated integers, A_{1}, A_{2}, \ldots, A_{N} ------ Output Format ------ For each test case, output on a single line, any possible interesting array. If no such array is possible, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 2 4 2 6 5 2 5 5 5 2 4 2 ----- Sample Output 1 ------ -1 6 2 5 2 5 2 4 2 5 ----- explanation 1 ------ Test case $1$: There is no way of arranging the elements such that no subarray of length greater than $2$ is non-increasing or non-decreasing. Test case $2$: A possible rearrangement of the elements is $[6, 2, 5, 2]$. Note that the subarrays of length greater than $2$ are $\{[6, 2, 5], [2, 5, 2], [6, 2, 5, 2]\}$. None of these subarrays are non-increasing or non-decreasing. Test case $3$: A possible rearrangement of the elements is $[5, 2, 4, 2, 5]$. Note that the subarrays of length greater than $2$ are $\{[5, 2, 4], [2, 4, 2], [4, 2, 5], [5, 2, 4, 2], [2, 4, 2, 5], [5, 2, 4, 2, 5]\}$. None of these subarrays are non-increasing or non-decreasing.
t = int(input()) for _ in range(t): n = int(input()) L = list(map(int, input().split())) d = {} for i in range(n): if L[i] in d: d[L[i]] += 1 else: d[L[i]] = 1 M = [] for i in d: M.append((i, d[i])) M.sort() if n == 1 or n == 2: print(*L) continue if len(M) == 1: print(-1) continue A = [] x, y = M[0] i = 0 f = True A.append(x) y -= 1 for j in range(1, len(M)): a, b = M[j] for k in range(b): A.append(a) if y > 0: A.append(x) y -= 1 elif A[i] < A[-1]: A.append(A[i]) i += 1 else: A.pop() x = a y = b - k break B = [] if y == 1 and A[-1] != x: A.append(x) y -= 1 if y > 0: print(-1) continue if not f: print(-1) continue for j in range(i, len(A)): B.append(A[j]) print(*B)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
An array is called *interesting* if no subarray of length greater than 2 is non-increasing or non-decreasing. Chef has an array A of length N. He wants to make the array interesting by rearranging the elements in any order. If there exist multiple such arrays, output any one. If no such array exists, print -1 instead. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N denoting the length of array A. - The next line contains N space separated integers, A_{1}, A_{2}, \ldots, A_{N} ------ Output Format ------ For each test case, output on a single line, any possible interesting array. If no such array is possible, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 2 4 2 6 5 2 5 5 5 2 4 2 ----- Sample Output 1 ------ -1 6 2 5 2 5 2 4 2 5 ----- explanation 1 ------ Test case $1$: There is no way of arranging the elements such that no subarray of length greater than $2$ is non-increasing or non-decreasing. Test case $2$: A possible rearrangement of the elements is $[6, 2, 5, 2]$. Note that the subarrays of length greater than $2$ are $\{[6, 2, 5], [2, 5, 2], [6, 2, 5, 2]\}$. None of these subarrays are non-increasing or non-decreasing. Test case $3$: A possible rearrangement of the elements is $[5, 2, 4, 2, 5]$. Note that the subarrays of length greater than $2$ are $\{[5, 2, 4], [2, 4, 2], [4, 2, 5], [5, 2, 4, 2], [2, 4, 2, 5], [5, 2, 4, 2, 5]\}$. None of these subarrays are non-increasing or non-decreasing.
from sys import stdin input = stdin.readline def interesting(A): for i in range(N - 2): if A[i] <= A[i + 1] <= A[i + 2] or A[i] >= A[i + 1] >= A[i + 2]: return False return True def construct(A1, A2): B = [] for i in range(max(len(A1), len(A2))): if i < len(A1): B.append(A1[i]) if i < len(A2): B.append(A2[i]) return B def solve(N, A): if N <= 2: return A A = sorted(A) A1 = A[(N + 1) // 2 :] A2 = A[: (N + 1) // 2] B = construct(A1, A2) if interesting(B): return B A1 = A[N // 2 :] A2 = A[: N // 2] B = construct(A1, A2) if interesting(B): return B return [-1] T = int(input().strip()) for problem in range(1, T + 1): N = int(input().strip()) A = [int(x) for x in input().strip().split()] print(*solve(N, A))
ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR RETURN VAR RETURN LIST NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
An array is called *interesting* if no subarray of length greater than 2 is non-increasing or non-decreasing. Chef has an array A of length N. He wants to make the array interesting by rearranging the elements in any order. If there exist multiple such arrays, output any one. If no such array exists, print -1 instead. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N denoting the length of array A. - The next line contains N space separated integers, A_{1}, A_{2}, \ldots, A_{N} ------ Output Format ------ For each test case, output on a single line, any possible interesting array. If no such array is possible, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 2 4 2 6 5 2 5 5 5 2 4 2 ----- Sample Output 1 ------ -1 6 2 5 2 5 2 4 2 5 ----- explanation 1 ------ Test case $1$: There is no way of arranging the elements such that no subarray of length greater than $2$ is non-increasing or non-decreasing. Test case $2$: A possible rearrangement of the elements is $[6, 2, 5, 2]$. Note that the subarrays of length greater than $2$ are $\{[6, 2, 5], [2, 5, 2], [6, 2, 5, 2]\}$. None of these subarrays are non-increasing or non-decreasing. Test case $3$: A possible rearrangement of the elements is $[5, 2, 4, 2, 5]$. Note that the subarrays of length greater than $2$ are $\{[5, 2, 4], [2, 4, 2], [4, 2, 5], [5, 2, 4, 2], [2, 4, 2, 5], [5, 2, 4, 2, 5]\}$. None of these subarrays are non-increasing or non-decreasing.
def maximum_subarray_with_same_value(arr): n = len(arr) max_len = 0 i = 0 while i < n: j = i + 1 while j < n and arr[j] == arr[i]: j += 1 max_len = max(max_len, j - i) i = j return max_len t = int(input()) for _ in range(t): (n,) = map(int, input().split()) a = list(map(int, input().split())) a.sort() m = maximum_subarray_with_same_value(a) if m > n // 2: print(-1) continue ans = [] i, j = 0, n // 2 while i < n // 2: ans.append(a[j]) ans.append(a[i]) i += 1 j += 1 if n % 2 != 0: ans.append(a[j]) print(*ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
An array is called *interesting* if no subarray of length greater than 2 is non-increasing or non-decreasing. Chef has an array A of length N. He wants to make the array interesting by rearranging the elements in any order. If there exist multiple such arrays, output any one. If no such array exists, print -1 instead. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N denoting the length of array A. - The next line contains N space separated integers, A_{1}, A_{2}, \ldots, A_{N} ------ Output Format ------ For each test case, output on a single line, any possible interesting array. If no such array is possible, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 2 4 2 6 5 2 5 5 5 2 4 2 ----- Sample Output 1 ------ -1 6 2 5 2 5 2 4 2 5 ----- explanation 1 ------ Test case $1$: There is no way of arranging the elements such that no subarray of length greater than $2$ is non-increasing or non-decreasing. Test case $2$: A possible rearrangement of the elements is $[6, 2, 5, 2]$. Note that the subarrays of length greater than $2$ are $\{[6, 2, 5], [2, 5, 2], [6, 2, 5, 2]\}$. None of these subarrays are non-increasing or non-decreasing. Test case $3$: A possible rearrangement of the elements is $[5, 2, 4, 2, 5]$. Note that the subarrays of length greater than $2$ are $\{[5, 2, 4], [2, 4, 2], [4, 2, 5], [5, 2, 4, 2], [2, 4, 2, 5], [5, 2, 4, 2, 5]\}$. None of these subarrays are non-increasing or non-decreasing.
def impossible(arr): for i in range(len(arr) - 2): if len(set(arr[i : i + 3])) == 1: return True return False for i in range(int(input())): n = int(input()) arr = list(map(int, input().split(" "))) arr.sort() a1 = arr[: len(arr) // 2] a2 = arr[len(arr) // 2 :] sol = [] for j in range(len(a2)): sol.append(a2[j]) if len(a1) > j: sol.append(a1[j]) if impossible(sol): print(-1) else: print(" ".join(list(map(str, sol))))
FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
An array is called *interesting* if no subarray of length greater than 2 is non-increasing or non-decreasing. Chef has an array A of length N. He wants to make the array interesting by rearranging the elements in any order. If there exist multiple such arrays, output any one. If no such array exists, print -1 instead. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N denoting the length of array A. - The next line contains N space separated integers, A_{1}, A_{2}, \ldots, A_{N} ------ Output Format ------ For each test case, output on a single line, any possible interesting array. If no such array is possible, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 2 4 2 6 5 2 5 5 5 2 4 2 ----- Sample Output 1 ------ -1 6 2 5 2 5 2 4 2 5 ----- explanation 1 ------ Test case $1$: There is no way of arranging the elements such that no subarray of length greater than $2$ is non-increasing or non-decreasing. Test case $2$: A possible rearrangement of the elements is $[6, 2, 5, 2]$. Note that the subarrays of length greater than $2$ are $\{[6, 2, 5], [2, 5, 2], [6, 2, 5, 2]\}$. None of these subarrays are non-increasing or non-decreasing. Test case $3$: A possible rearrangement of the elements is $[5, 2, 4, 2, 5]$. Note that the subarrays of length greater than $2$ are $\{[5, 2, 4], [2, 4, 2], [4, 2, 5], [5, 2, 4, 2], [2, 4, 2, 5], [5, 2, 4, 2, 5]\}$. None of these subarrays are non-increasing or non-decreasing.
t = int(input()) for tt in range(t): n = int(input()) a = list(map(int, input().split())) ans = [0] * n ans1 = [0] * n a.sort() i = 0 j = n - 1 for r in range(1, n, 2): ans[r] = a[i] ans1[r] = a[j] j -= 1 i += 1 for r in range(0, n, 2): ans[r] = a[i] ans1[r] = a[j] j -= 1 i += 1 p = 0 q = 0 for r in range(0, n - 2): k = sorted(ans[r : r + 3]) if ans[r : r + 3] == k or ans[r : r + 3] == k[::-1]: p = 1 break for r in range(0, n - 2): k = sorted(ans1[r : r + 3]) if ans1[r : r + 3] == k or ans1[r : r + 3] == k[::-1]: q = 1 break if p == 0: print(" ".join(map(str, ans))) elif q == 0: print(" ".join(map(str, ans1))) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
An array is called *interesting* if no subarray of length greater than 2 is non-increasing or non-decreasing. Chef has an array A of length N. He wants to make the array interesting by rearranging the elements in any order. If there exist multiple such arrays, output any one. If no such array exists, print -1 instead. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N denoting the length of array A. - The next line contains N space separated integers, A_{1}, A_{2}, \ldots, A_{N} ------ Output Format ------ For each test case, output on a single line, any possible interesting array. If no such array is possible, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 2 4 2 6 5 2 5 5 5 2 4 2 ----- Sample Output 1 ------ -1 6 2 5 2 5 2 4 2 5 ----- explanation 1 ------ Test case $1$: There is no way of arranging the elements such that no subarray of length greater than $2$ is non-increasing or non-decreasing. Test case $2$: A possible rearrangement of the elements is $[6, 2, 5, 2]$. Note that the subarrays of length greater than $2$ are $\{[6, 2, 5], [2, 5, 2], [6, 2, 5, 2]\}$. None of these subarrays are non-increasing or non-decreasing. Test case $3$: A possible rearrangement of the elements is $[5, 2, 4, 2, 5]$. Note that the subarrays of length greater than $2$ are $\{[5, 2, 4], [2, 4, 2], [4, 2, 5], [5, 2, 4, 2], [2, 4, 2, 5], [5, 2, 4, 2, 5]\}$. None of these subarrays are non-increasing or non-decreasing.
def fun(l): x = len(l) // 2 pos = len(l) // 2 while x > 0: l.insert(pos, l.pop(0)) x -= 1 pos += 1 for i in range(n - 2): if ( l[i] >= l[i + 1] and l[i + 1] >= l[i + 2] or l[i] <= l[i + 1] and l[i + 1] <= l[i + 2] ): return True return False for i in range(int(input())): n = int(input()) l = list(map(int, input().split())) l.sort() if fun(l) == 0: for x in l: print(x, end=" ") print() else: print(-1)
FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER
An array is called *interesting* if no subarray of length greater than 2 is non-increasing or non-decreasing. Chef has an array A of length N. He wants to make the array interesting by rearranging the elements in any order. If there exist multiple such arrays, output any one. If no such array exists, print -1 instead. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N denoting the length of array A. - The next line contains N space separated integers, A_{1}, A_{2}, \ldots, A_{N} ------ Output Format ------ For each test case, output on a single line, any possible interesting array. If no such array is possible, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 2 4 2 6 5 2 5 5 5 2 4 2 ----- Sample Output 1 ------ -1 6 2 5 2 5 2 4 2 5 ----- explanation 1 ------ Test case $1$: There is no way of arranging the elements such that no subarray of length greater than $2$ is non-increasing or non-decreasing. Test case $2$: A possible rearrangement of the elements is $[6, 2, 5, 2]$. Note that the subarrays of length greater than $2$ are $\{[6, 2, 5], [2, 5, 2], [6, 2, 5, 2]\}$. None of these subarrays are non-increasing or non-decreasing. Test case $3$: A possible rearrangement of the elements is $[5, 2, 4, 2, 5]$. Note that the subarrays of length greater than $2$ are $\{[5, 2, 4], [2, 4, 2], [4, 2, 5], [5, 2, 4, 2], [2, 4, 2, 5], [5, 2, 4, 2, 5]\}$. None of these subarrays are non-increasing or non-decreasing.
def solve(a, d, f): a1 = a[0:d] a2 = a[d:] b = [] d = min(len(a1), len(a2)) if f == 0: for i in range(d): b.append(a2[i]) b.append(a1[i]) b.append(a2[-1]) else: for i in range(d): b.append(a1[i]) b.append(a2[i]) b.append(a1[-1]) return b def check(b): for i in range(len(b) - 2): if b[i] <= b[i + 1] <= b[i + 2] or b[i] >= b[i + 1] >= b[i + 2]: return 0 return 1 t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) a.sort() if n == 1: print(a[0]) elif n == 2: print(a[0], a[1]) elif n % 2 == 0: a1 = a[: n // 2] a2 = a[n // 2 :] b = [] for i in range(n // 2): b.append(a2[i]) b.append(a1[i]) if check(b): print(*b) else: print(-1) else: md1 = n // 2 md2 = n // 2 + 1 b1 = solve(a, md1, 0) b2 = solve(a, md2, 1) if check(b1): print(*b1) elif check(b2): print(*b2) else: print(-1)
FUNC_DEF ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
An array is called *interesting* if no subarray of length greater than 2 is non-increasing or non-decreasing. Chef has an array A of length N. He wants to make the array interesting by rearranging the elements in any order. If there exist multiple such arrays, output any one. If no such array exists, print -1 instead. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N denoting the length of array A. - The next line contains N space separated integers, A_{1}, A_{2}, \ldots, A_{N} ------ Output Format ------ For each test case, output on a single line, any possible interesting array. If no such array is possible, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 2 4 2 6 5 2 5 5 5 2 4 2 ----- Sample Output 1 ------ -1 6 2 5 2 5 2 4 2 5 ----- explanation 1 ------ Test case $1$: There is no way of arranging the elements such that no subarray of length greater than $2$ is non-increasing or non-decreasing. Test case $2$: A possible rearrangement of the elements is $[6, 2, 5, 2]$. Note that the subarrays of length greater than $2$ are $\{[6, 2, 5], [2, 5, 2], [6, 2, 5, 2]\}$. None of these subarrays are non-increasing or non-decreasing. Test case $3$: A possible rearrangement of the elements is $[5, 2, 4, 2, 5]$. Note that the subarrays of length greater than $2$ are $\{[5, 2, 4], [2, 4, 2], [4, 2, 5], [5, 2, 4, 2], [2, 4, 2, 5], [5, 2, 4, 2, 5]\}$. None of these subarrays are non-increasing or non-decreasing.
def check(lst): for i in range(1, len(lst) - 1): if lst[i + 1] >= lst[i] >= lst[i - 1]: return False if lst[i + 1] <= lst[i] <= lst[i - 1]: return False return True T = int(input()) for _ in range(T): N = int(input()) arr = list(map(int, input().split())) arr.sort(reverse=True) if N == 1 or N == 2: print(*res) continue i = 0 j = N // 2 res = [] while i < N // 2: res.append(arr[i]) res.append(arr[j]) i += 1 j += 1 if N % 2 == 1: res.append(arr[-1]) i = 0 j = N // 2 res1 = [] while i < N // 2: res1.append(arr[j]) res1.append(arr[i]) i += 1 j += 1 if N % 2 == 1: res1.append(arr[-1]) if check(res): print(*res) elif check(res1): print(*res1) else: print(-1)
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
An array is called *interesting* if no subarray of length greater than 2 is non-increasing or non-decreasing. Chef has an array A of length N. He wants to make the array interesting by rearranging the elements in any order. If there exist multiple such arrays, output any one. If no such array exists, print -1 instead. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N denoting the length of array A. - The next line contains N space separated integers, A_{1}, A_{2}, \ldots, A_{N} ------ Output Format ------ For each test case, output on a single line, any possible interesting array. If no such array is possible, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 2 4 2 6 5 2 5 5 5 2 4 2 ----- Sample Output 1 ------ -1 6 2 5 2 5 2 4 2 5 ----- explanation 1 ------ Test case $1$: There is no way of arranging the elements such that no subarray of length greater than $2$ is non-increasing or non-decreasing. Test case $2$: A possible rearrangement of the elements is $[6, 2, 5, 2]$. Note that the subarrays of length greater than $2$ are $\{[6, 2, 5], [2, 5, 2], [6, 2, 5, 2]\}$. None of these subarrays are non-increasing or non-decreasing. Test case $3$: A possible rearrangement of the elements is $[5, 2, 4, 2, 5]$. Note that the subarrays of length greater than $2$ are $\{[5, 2, 4], [2, 4, 2], [4, 2, 5], [5, 2, 4, 2], [2, 4, 2, 5], [5, 2, 4, 2, 5]\}$. None of these subarrays are non-increasing or non-decreasing.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) a.sort() greater = a[n // 2 :] lesser = a[: n // 2] valid = [] for i in range(n // 2): valid.append(greater[i]) valid.append(lesser[i]) if n % 2 == 1: valid.append(greater[-1]) interest = True for i in range(1, n - 1): if not ( valid[i - 1] < valid[i] > valid[i + 1] or valid[i - 1] > valid[i] < valid[i + 1] ): interest = False if interest == True: print(*valid) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
An array is called *interesting* if no subarray of length greater than 2 is non-increasing or non-decreasing. Chef has an array A of length N. He wants to make the array interesting by rearranging the elements in any order. If there exist multiple such arrays, output any one. If no such array exists, print -1 instead. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N denoting the length of array A. - The next line contains N space separated integers, A_{1}, A_{2}, \ldots, A_{N} ------ Output Format ------ For each test case, output on a single line, any possible interesting array. If no such array is possible, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 2 4 2 6 5 2 5 5 5 2 4 2 ----- Sample Output 1 ------ -1 6 2 5 2 5 2 4 2 5 ----- explanation 1 ------ Test case $1$: There is no way of arranging the elements such that no subarray of length greater than $2$ is non-increasing or non-decreasing. Test case $2$: A possible rearrangement of the elements is $[6, 2, 5, 2]$. Note that the subarrays of length greater than $2$ are $\{[6, 2, 5], [2, 5, 2], [6, 2, 5, 2]\}$. None of these subarrays are non-increasing or non-decreasing. Test case $3$: A possible rearrangement of the elements is $[5, 2, 4, 2, 5]$. Note that the subarrays of length greater than $2$ are $\{[5, 2, 4], [2, 4, 2], [4, 2, 5], [5, 2, 4, 2], [2, 4, 2, 5], [5, 2, 4, 2, 5]\}$. None of these subarrays are non-increasing or non-decreasing.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) arr2 = arr.copy() arr.sort(reverse=True) mid = n // 2 arr[1::2], arr[0::2] = arr[:mid], arr[mid:] flag = False for i in range(n - 2): if arr[i] == arr[i + 1] or arr[i + 1] == arr[i + 2]: flag = True break if flag: print(-1) else: print(*arr)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
An array is called *interesting* if no subarray of length greater than 2 is non-increasing or non-decreasing. Chef has an array A of length N. He wants to make the array interesting by rearranging the elements in any order. If there exist multiple such arrays, output any one. If no such array exists, print -1 instead. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N denoting the length of array A. - The next line contains N space separated integers, A_{1}, A_{2}, \ldots, A_{N} ------ Output Format ------ For each test case, output on a single line, any possible interesting array. If no such array is possible, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 2 4 2 6 5 2 5 5 5 2 4 2 ----- Sample Output 1 ------ -1 6 2 5 2 5 2 4 2 5 ----- explanation 1 ------ Test case $1$: There is no way of arranging the elements such that no subarray of length greater than $2$ is non-increasing or non-decreasing. Test case $2$: A possible rearrangement of the elements is $[6, 2, 5, 2]$. Note that the subarrays of length greater than $2$ are $\{[6, 2, 5], [2, 5, 2], [6, 2, 5, 2]\}$. None of these subarrays are non-increasing or non-decreasing. Test case $3$: A possible rearrangement of the elements is $[5, 2, 4, 2, 5]$. Note that the subarrays of length greater than $2$ are $\{[5, 2, 4], [2, 4, 2], [4, 2, 5], [5, 2, 4, 2], [2, 4, 2, 5], [5, 2, 4, 2, 5]\}$. None of these subarrays are non-increasing or non-decreasing.
for _ in range(int(input())): n = int(input()) a = [int(x) for x in input().split()] a.sort() if n == 1: print(a[0]) elif n == 2: print(a[0], a[1]) else: new = [-1] * n ind = 1 while ind < n: new[ind] = a.pop() ind += 2 ind = 0 while ind < n: new[ind] = a.pop() ind += 2 flag = 0 for i in range(n - 2): if new[i] <= new[i + 1] and new[i + 1] <= new[i + 2]: flag = 1 break if new[i] >= new[i + 1] and new[i + 1] >= new[i + 2]: flag = 1 break if flag == 1: print(-1) else: print(*new)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
An array is called *interesting* if no subarray of length greater than 2 is non-increasing or non-decreasing. Chef has an array A of length N. He wants to make the array interesting by rearranging the elements in any order. If there exist multiple such arrays, output any one. If no such array exists, print -1 instead. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N denoting the length of array A. - The next line contains N space separated integers, A_{1}, A_{2}, \ldots, A_{N} ------ Output Format ------ For each test case, output on a single line, any possible interesting array. If no such array is possible, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 2 4 2 6 5 2 5 5 5 2 4 2 ----- Sample Output 1 ------ -1 6 2 5 2 5 2 4 2 5 ----- explanation 1 ------ Test case $1$: There is no way of arranging the elements such that no subarray of length greater than $2$ is non-increasing or non-decreasing. Test case $2$: A possible rearrangement of the elements is $[6, 2, 5, 2]$. Note that the subarrays of length greater than $2$ are $\{[6, 2, 5], [2, 5, 2], [6, 2, 5, 2]\}$. None of these subarrays are non-increasing or non-decreasing. Test case $3$: A possible rearrangement of the elements is $[5, 2, 4, 2, 5]$. Note that the subarrays of length greater than $2$ are $\{[5, 2, 4], [2, 4, 2], [4, 2, 5], [5, 2, 4, 2], [2, 4, 2, 5], [5, 2, 4, 2, 5]\}$. None of these subarrays are non-increasing or non-decreasing.
t = int(input()) for t0 in range(t): n = int(input()) a_i = list(map(int, input().split())) max_valid_count = int(n / 2) + 1 num_count = {} for num in a_i: if not num_count.__contains__(num): num_count[num] = 0 num_count[num] += 1 max_occurred_item = max(num_count.items(), key=lambda item: item[1]) if max_occurred_item[1] > max_valid_count: print(-1) continue a_i = sorted(a_i) middle_i = int(n / 2) list1 = a_i[:middle_i] list2 = a_i[middle_i:] result = [] for i in range(len(list1)): result.append(list2[i]) result.append(list1[i]) if len(list1) != len(list2): result.append(list2[-1]) print(" ".join([str(item) for item in result]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
An array is called *interesting* if no subarray of length greater than 2 is non-increasing or non-decreasing. Chef has an array A of length N. He wants to make the array interesting by rearranging the elements in any order. If there exist multiple such arrays, output any one. If no such array exists, print -1 instead. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N denoting the length of array A. - The next line contains N space separated integers, A_{1}, A_{2}, \ldots, A_{N} ------ Output Format ------ For each test case, output on a single line, any possible interesting array. If no such array is possible, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 2 4 2 6 5 2 5 5 5 2 4 2 ----- Sample Output 1 ------ -1 6 2 5 2 5 2 4 2 5 ----- explanation 1 ------ Test case $1$: There is no way of arranging the elements such that no subarray of length greater than $2$ is non-increasing or non-decreasing. Test case $2$: A possible rearrangement of the elements is $[6, 2, 5, 2]$. Note that the subarrays of length greater than $2$ are $\{[6, 2, 5], [2, 5, 2], [6, 2, 5, 2]\}$. None of these subarrays are non-increasing or non-decreasing. Test case $3$: A possible rearrangement of the elements is $[5, 2, 4, 2, 5]$. Note that the subarrays of length greater than $2$ are $\{[5, 2, 4], [2, 4, 2], [4, 2, 5], [5, 2, 4, 2], [2, 4, 2, 5], [5, 2, 4, 2, 5]\}$. None of these subarrays are non-increasing or non-decreasing.
def interesting(a): n = len(a) return n < 3 or all( a[i] < min(a[i - 1], a[i + 1]) or a[i] > max(a[i - 1], a[i + 1]) for i in range(1, n - 1) ) for tcase in range(int(input())): n = int(input()) a = sorted(map(int, input().split())) b, c = [], [] h = n // 2 d = h + n % 2 for i in range(h): b.append(a[i]) b.append(a[i + d]) c.append(a[i + h]) c.append(a[i]) if n % 2 == 1: b.append(a[h]) c.append(a[-1]) if interesting(b): print(" ".join(map(str, b))) elif interesting(c): print(" ".join(map(str, c))) else: print(-1)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
An array is called *interesting* if no subarray of length greater than 2 is non-increasing or non-decreasing. Chef has an array A of length N. He wants to make the array interesting by rearranging the elements in any order. If there exist multiple such arrays, output any one. If no such array exists, print -1 instead. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N denoting the length of array A. - The next line contains N space separated integers, A_{1}, A_{2}, \ldots, A_{N} ------ Output Format ------ For each test case, output on a single line, any possible interesting array. If no such array is possible, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 2 4 2 6 5 2 5 5 5 2 4 2 ----- Sample Output 1 ------ -1 6 2 5 2 5 2 4 2 5 ----- explanation 1 ------ Test case $1$: There is no way of arranging the elements such that no subarray of length greater than $2$ is non-increasing or non-decreasing. Test case $2$: A possible rearrangement of the elements is $[6, 2, 5, 2]$. Note that the subarrays of length greater than $2$ are $\{[6, 2, 5], [2, 5, 2], [6, 2, 5, 2]\}$. None of these subarrays are non-increasing or non-decreasing. Test case $3$: A possible rearrangement of the elements is $[5, 2, 4, 2, 5]$. Note that the subarrays of length greater than $2$ are $\{[5, 2, 4], [2, 4, 2], [4, 2, 5], [5, 2, 4, 2], [2, 4, 2, 5], [5, 2, 4, 2, 5]\}$. None of these subarrays are non-increasing or non-decreasing.
def solve(n, arr): if n <= 2: return arr arr_b = arr[:] arr_b.sort() arr_c = arr_b[: len(arr_b) // 2] arr_d = arr_b[len(arr_b) // 2 :] arr_e = [] i = 0 j = 0 while i < len(arr_c) and j < len(arr_d): arr_e.append(arr_d[j]) arr_e.append(arr_c[i]) i += 1 j += 1 if n % 2 == 1: arr_e.append(arr_d[-1]) for j in range(10): for i in range(0, n - 2): if ( arr_e[i] <= arr_e[i + 1] <= arr_e[i + 2] or arr_e[i] >= arr_e[i + 1] >= arr_e[i + 2] ): arr_e[i + 1], arr_e[i + 2] = arr_e[i + 2], arr_e[i + 1] for i in range(0, n - 2): if ( arr_e[i] <= arr_e[i + 1] <= arr_e[i + 2] or arr_e[i] >= arr_e[i + 1] >= arr_e[i + 2] ): return -1 if arr_e == arr: return -1 else: return arr_e t = int(input()) for _ in range(t): n = int(input()) arr = [int(x) for x in input().split()] ans = solve(n, arr) if ans == -1: print(-1) else: print(*ans)
FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
An array is called *interesting* if no subarray of length greater than 2 is non-increasing or non-decreasing. Chef has an array A of length N. He wants to make the array interesting by rearranging the elements in any order. If there exist multiple such arrays, output any one. If no such array exists, print -1 instead. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N denoting the length of array A. - The next line contains N space separated integers, A_{1}, A_{2}, \ldots, A_{N} ------ Output Format ------ For each test case, output on a single line, any possible interesting array. If no such array is possible, output -1. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 2 4 2 6 5 2 5 5 5 2 4 2 ----- Sample Output 1 ------ -1 6 2 5 2 5 2 4 2 5 ----- explanation 1 ------ Test case $1$: There is no way of arranging the elements such that no subarray of length greater than $2$ is non-increasing or non-decreasing. Test case $2$: A possible rearrangement of the elements is $[6, 2, 5, 2]$. Note that the subarrays of length greater than $2$ are $\{[6, 2, 5], [2, 5, 2], [6, 2, 5, 2]\}$. None of these subarrays are non-increasing or non-decreasing. Test case $3$: A possible rearrangement of the elements is $[5, 2, 4, 2, 5]$. Note that the subarrays of length greater than $2$ are $\{[5, 2, 4], [2, 4, 2], [4, 2, 5], [5, 2, 4, 2], [2, 4, 2, 5], [5, 2, 4, 2, 5]\}$. None of these subarrays are non-increasing or non-decreasing.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) a.sort() b = [0] * n c = [0] * n for i in range(0, n, 2): b[i] = a[i // 2] for i in range(1, n, 2): b[i] = a[(n + i) // 2] for i in range(0, n, 2): c[i] = a[(n + i) // 2] for i in range(1, n, 2): c[i] = a[i // 2] ans = 0 final = b for i in range(1, n - 1): if (b[i] - b[i - 1]) * (b[i] - b[i + 1]) <= 0: ans += 1 final = c break for i in range(1, n - 1): if (c[i] - c[i - 1]) * (c[i] - c[i + 1]) <= 0: ans += 1 final = b break if ans == 2: print(-1) else: for i in final: print(i, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  Since the answer may be very large, return it modulo 10^9 + 7.   Example 1: Input: N = 1, A = 2, B = 3 Output: 2 Example 2: Input: N = 4, A = 2, B = 3 Output: 6 Example 3: Input: N = 5, A = 2, B = 4 Output: 10 Example 4: Input: N = 3, A = 6, B = 4 Output: 8   Note: 1 <= N <= 10^9 2 <= A <= 40000 2 <= B <= 40000
class Solution: def NOD(self, a, b): if a == b: return a c = max(a, b) d = a + b - c c = c % d c = c if c > 0 else d return self.NOD(c, d) def nthMagicalNumber(self, N: int, A: int, B: int) -> int: const = 10**9 + 7 nod = self.NOD(A, B) nok = int(A * B / nod) C, D = min(A, B), max(A, B) k_C = nok // C k_D = nok // D k = k_C + k_D - 1 div = N // k mod = N - div * k k_C_cur = mod * k_C // k k_D_cur = mod - k_C_cur while True: C_num = k_C_cur * C D_num = k_D_cur * D if -C < C_num - D_num < D: return (div * nok + max(C_num, D_num)) % const elif C_num - D_num <= -C: k_D_cur -= 1 k_C_cur += 1 else: k_D_cur += 1 k_C_cur -= 1
CLASS_DEF FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR
A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  Since the answer may be very large, return it modulo 10^9 + 7.   Example 1: Input: N = 1, A = 2, B = 3 Output: 2 Example 2: Input: N = 4, A = 2, B = 3 Output: 6 Example 3: Input: N = 5, A = 2, B = 4 Output: 10 Example 4: Input: N = 3, A = 6, B = 4 Output: 8   Note: 1 <= N <= 10^9 2 <= A <= 40000 2 <= B <= 40000
class Solution: def nthMagicalNumber(self, N: int, A: int, B: int) -> int: lcm = A * B // math.gcd(A, B) cnt = lcm // A + lcm // B - 1 i = N // cnt j = N % cnt def count(x, lcm): return x // A + x // B - x // lcm l, r = 0, A * B while l < r: mid = l + (r - l) // 2 c = count(mid, lcm) if c < j: l = mid + 1 else: r = mid return (i * lcm + l) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  Since the answer may be very large, return it modulo 10^9 + 7.   Example 1: Input: N = 1, A = 2, B = 3 Output: 2 Example 2: Input: N = 4, A = 2, B = 3 Output: 6 Example 3: Input: N = 5, A = 2, B = 4 Output: 10 Example 4: Input: N = 3, A = 6, B = 4 Output: 8   Note: 1 <= N <= 10^9 2 <= A <= 40000 2 <= B <= 40000
class Solution: def nthMagicalNumber(self, N: int, A: int, B: int) -> int: def gcd(a, b): while b: a, b = b, a % b return a def lcm(a, b): return a * b // gcd(a, b) val = lcm(A, B) magicals = set() for i in range(1, val // A + 1): magicals.add(i * A) for i in range(1, val // B + 1): magicals.add(i * B) sorted_magicals = sorted(magicals) return ( sorted_magicals[(N - 1) % len(sorted_magicals)] + (N - 1) // len(sorted_magicals) * sorted_magicals[-1] ) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  Since the answer may be very large, return it modulo 10^9 + 7.   Example 1: Input: N = 1, A = 2, B = 3 Output: 2 Example 2: Input: N = 4, A = 2, B = 3 Output: 6 Example 3: Input: N = 5, A = 2, B = 4 Output: 10 Example 4: Input: N = 3, A = 6, B = 4 Output: 8   Note: 1 <= N <= 10^9 2 <= A <= 40000 2 <= B <= 40000
class Solution: def nthMagicalNumber(self, N: int, A: int, B: int) -> int: def gcd(x, y): if y == 0: return x return gcd(y, x % y) MOD = 10**9 + 7 L = A // gcd(A, B) * B M = L // A + L // B - 1 q, r = divmod(N, M) if r == 0: return q * L % MOD increments = [A, B] for _ in range(r - 1): if increments[0] <= increments[1]: increments[0] += A else: increments[1] += B return (q * L + min(increments)) % MOD
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR LIST VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR
A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  Since the answer may be very large, return it modulo 10^9 + 7.   Example 1: Input: N = 1, A = 2, B = 3 Output: 2 Example 2: Input: N = 4, A = 2, B = 3 Output: 6 Example 3: Input: N = 5, A = 2, B = 4 Output: 10 Example 4: Input: N = 3, A = 6, B = 4 Output: 8   Note: 1 <= N <= 10^9 2 <= A <= 40000 2 <= B <= 40000
class Solution: def nthMagicalNumber(self, N: int, A: int, B: int) -> int: def min_common_divisor(a, b): less = min(a, b) more = max(a, b) for i in range(1, less + 1): if more * i % less == 0: return more * i return a * b _cd = min_common_divisor(A, B) min_ = min(A, B) l, r = min_, min_ * N + 1 while l < r: m = l + r >> 1 if m // A + m // B - m // _cd < N: l = m + 1 elif m // A + m // B - m // _cd > N: r = m elif m % A == 0 and m % B == 0: return m % (10**9 + 7) else: r = m return l % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  Since the answer may be very large, return it modulo 10^9 + 7.   Example 1: Input: N = 1, A = 2, B = 3 Output: 2 Example 2: Input: N = 4, A = 2, B = 3 Output: 6 Example 3: Input: N = 5, A = 2, B = 4 Output: 10 Example 4: Input: N = 3, A = 6, B = 4 Output: 8   Note: 1 <= N <= 10^9 2 <= A <= 40000 2 <= B <= 40000
class Solution: modulo = 10**9 + 7 def getMaxCommonFactor(self, A, B): while B > 0: A = A % B A, B = B, A return A def nthMagicalNumber(self, N: int, A: int, B: int) -> int: mcf = self.getMaxCommonFactor(A, B) period = A * B // mcf % self.modulo numinperiod = (A + B - mcf) // mcf base = N // numinperiod * period nums = [(A * i) for i in range(B // mcf)] + [ (B * i) for i in range(1, A // mcf) ] nums.sort() rem = nums[N % numinperiod] return (base + rem) % self.modulo
CLASS_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR VAR
A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  Since the answer may be very large, return it modulo 10^9 + 7.   Example 1: Input: N = 1, A = 2, B = 3 Output: 2 Example 2: Input: N = 4, A = 2, B = 3 Output: 6 Example 3: Input: N = 5, A = 2, B = 4 Output: 10 Example 4: Input: N = 3, A = 6, B = 4 Output: 8   Note: 1 <= N <= 10^9 2 <= A <= 40000 2 <= B <= 40000
class Solution: def nthMagicalNumber(self, N: int, A: int, B: int) -> int: x, y = min(A, B), max(A, B) print(x, y) hcf = x while (y % hcf != 0 or x % hcf != 0) and hcf > 1: hcf -= 1 print(hcf) gcd = int(x * y / hcf) print(gcd) start, end = x, N * x while start <= end: mid = start + int((end - start) / 2) count = int(mid / x) + int(mid / y) - int(mid / gcd) if count == N: break elif count > N: end = mid - 1 else: start = mid + 1 while mid % x != 0 and mid % y != 0: mid -= 1 return mid % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  Since the answer may be very large, return it modulo 10^9 + 7.   Example 1: Input: N = 1, A = 2, B = 3 Output: 2 Example 2: Input: N = 4, A = 2, B = 3 Output: 6 Example 3: Input: N = 5, A = 2, B = 4 Output: 10 Example 4: Input: N = 3, A = 6, B = 4 Output: 8   Note: 1 <= N <= 10^9 2 <= A <= 40000 2 <= B <= 40000
def nww(a, b): bigger = max(a, b) smaller = min(a, b) result = bigger i = 2 while result % smaller != 0: result = bigger * i i += 1 return result class Solution: def nthMagicalNumber(self, N: int, A: int, B: int) -> int: nw = nww(A, B) la = list([(A * x) for x in range(1, nw // A)]) lb = list([(B * x) for x in range(1, nw // B)]) cycle = sorted([0] + la + lb) return (N // len(cycle) * nw + cycle[N % len(cycle)]) % (10**9 + 7)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP LIST NUMBER VAR VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  Since the answer may be very large, return it modulo 10^9 + 7.   Example 1: Input: N = 1, A = 2, B = 3 Output: 2 Example 2: Input: N = 4, A = 2, B = 3 Output: 6 Example 3: Input: N = 5, A = 2, B = 4 Output: 10 Example 4: Input: N = 3, A = 6, B = 4 Output: 8   Note: 1 <= N <= 10^9 2 <= A <= 40000 2 <= B <= 40000
class Solution: def nthMagicalNumber(self, N: int, A: int, B: int) -> int: def gcd(a, b): while b: a, b = b, a % b return a def lcm(a, b): return a * b // gcd(a, b) val = lcm(A, B) def number_magic_below(x): return x // A + x // B - x // val lo = min(A, B) hi = N * min(A, B) while lo < hi: mid = (lo + hi) // 2 if number_magic_below(mid) < N: lo = mid + 1 else: hi = mid return lo % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  Since the answer may be very large, return it modulo 10^9 + 7.   Example 1: Input: N = 1, A = 2, B = 3 Output: 2 Example 2: Input: N = 4, A = 2, B = 3 Output: 6 Example 3: Input: N = 5, A = 2, B = 4 Output: 10 Example 4: Input: N = 3, A = 6, B = 4 Output: 8   Note: 1 <= N <= 10^9 2 <= A <= 40000 2 <= B <= 40000
class Solution: def gcd(self, x, y): while y > 0: x, y = y, x % y return x def lcm(self, x, y): return x * y // self.gcd(x, y) def nthMagicalNumber(self, N: int, A: int, B: int) -> int: AB = self.lcm(A, B) def check(mid): ans = mid // A + mid // B - mid // AB return ans >= N lo, hi = 0, N * A while lo < hi: mid = (lo + hi) // 2 if check(mid): hi = mid else: lo = mid + 1 mod = 10**9 + 7 return lo % mod
CLASS_DEF FUNC_DEF WHILE VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP VAR VAR VAR
A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  Since the answer may be very large, return it modulo 10^9 + 7.   Example 1: Input: N = 1, A = 2, B = 3 Output: 2 Example 2: Input: N = 4, A = 2, B = 3 Output: 6 Example 3: Input: N = 5, A = 2, B = 4 Output: 10 Example 4: Input: N = 3, A = 6, B = 4 Output: 8   Note: 1 <= N <= 10^9 2 <= A <= 40000 2 <= B <= 40000
def LCM(x, y): if x == y: return x bigger = max([x, y]) smaller = min([x, y]) res = bigger while res % smaller != 0: res += bigger return res class Solution: def nthMagicalNumber(self, N: int, A: int, B: int) -> int: lcm = LCM(A, B) div = [0, lcm] for i in [A, B]: t = i while t < lcm: div.append(t) t += i div = sorted(div) cycle = len(div) - 1 return (div[N % cycle] + lcm * (N // cycle)) % 1000000007
FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER VAR FOR VAR LIST VAR VAR ASSIGN VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR
A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  Since the answer may be very large, return it modulo 10^9 + 7.   Example 1: Input: N = 1, A = 2, B = 3 Output: 2 Example 2: Input: N = 4, A = 2, B = 3 Output: 6 Example 3: Input: N = 5, A = 2, B = 4 Output: 10 Example 4: Input: N = 3, A = 6, B = 4 Output: 8   Note: 1 <= N <= 10^9 2 <= A <= 40000 2 <= B <= 40000
class Solution: def nthMagicalNumber(self, N: int, A: int, B: int) -> int: def compute_gcd(x, y): if y == 0: return x return compute_gcd(y, x % y) def compute_lcm(x, y): lcm = x * y // compute_gcd(x, y) return lcm C = compute_lcm(A, B) l = min(A, B) h = N * max(A, B) while l < h: mid = (l + h) // 2 m = mid // A + mid // B - mid // C if m >= N: h = mid else: l = mid + 1 return l % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  Since the answer may be very large, return it modulo 10^9 + 7.   Example 1: Input: N = 1, A = 2, B = 3 Output: 2 Example 2: Input: N = 4, A = 2, B = 3 Output: 6 Example 3: Input: N = 5, A = 2, B = 4 Output: 10 Example 4: Input: N = 3, A = 6, B = 4 Output: 8   Note: 1 <= N <= 10^9 2 <= A <= 40000 2 <= B <= 40000
class Solution: def nthMagicalNumber(self, N: int, A: int, B: int) -> int: def gcd(a, b): while b: a, b = b, a % b return a LCM = A * B // gcd(A, B) m = LCM // A + LCM // B - 1 q, r = divmod(N, m) if r == 0: return q * LCM % (10**9 + 7) pa, pb = 0, 0 for _ in range(r): nxt = min(A * (pa + 1), B * (pb + 1)) if nxt == A * (pa + 1): pa += 1 if nxt == B * (pb + 1): pb += 1 return (q * LCM + nxt) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  Since the answer may be very large, return it modulo 10^9 + 7.   Example 1: Input: N = 1, A = 2, B = 3 Output: 2 Example 2: Input: N = 4, A = 2, B = 3 Output: 6 Example 3: Input: N = 5, A = 2, B = 4 Output: 10 Example 4: Input: N = 3, A = 6, B = 4 Output: 8   Note: 1 <= N <= 10^9 2 <= A <= 40000 2 <= B <= 40000
class Solution: def _lcm(self, a, b): return a * b // math.gcd(a, b) def _nth_magical_number_slow(self, n: int, a: int, b: int) -> int: if n == 0: return 0 else: i, j = a, b count = 1 while count < n: if i <= j: i = i + a else: j = j + b if i == j: if a < b: i = i + a else: j = j + b count += 1 return min(i, j) % 1000000007 def nthMagicalNumber(self, n: int, a_orig: int, b_orig: int) -> int: a, b = min(a_orig, b_orig), max(a_orig, b_orig) if a == b: return a * n % 1000000007 else: lcm = self._lcm(a, b) k = lcm // a + lcm // b - 1 return ( n // k * lcm + self._nth_magical_number_slow(n % k, a, b) ) % 1000000007
CLASS_DEF FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR
A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  Since the answer may be very large, return it modulo 10^9 + 7.   Example 1: Input: N = 1, A = 2, B = 3 Output: 2 Example 2: Input: N = 4, A = 2, B = 3 Output: 6 Example 3: Input: N = 5, A = 2, B = 4 Output: 10 Example 4: Input: N = 3, A = 6, B = 4 Output: 8   Note: 1 <= N <= 10^9 2 <= A <= 40000 2 <= B <= 40000
class Solution: def nthMagicalNumber(self, N: int, A: int, B: int) -> int: if A > B: A, B = B, A if B % A == 0: return A * N % 1000000007 for i in range(1, A + 1): if B * i % A == 0: break LCM = B * i nAB = LCM // A + i - 1 r = N // nAB N %= nAB return ( r * LCM + sorted( set([(A * i) for i in range(N + 1)]) | set([(B * i) for i in range(N + 1)]) )[N] ) % 1000000007
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR
A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  Since the answer may be very large, return it modulo 10^9 + 7.   Example 1: Input: N = 1, A = 2, B = 3 Output: 2 Example 2: Input: N = 4, A = 2, B = 3 Output: 6 Example 3: Input: N = 5, A = 2, B = 4 Output: 10 Example 4: Input: N = 3, A = 6, B = 4 Output: 8   Note: 1 <= N <= 10^9 2 <= A <= 40000 2 <= B <= 40000
class Solution: def nthMagicalNumber(self, N: int, A: int, B: int) -> int: d = gcd(A, B) A //= d B //= d def how_many_below(n): return n // A + n // B - n // (A * B) lo = N - 1 hi = min(A, B) * N while hi - lo > 1: h = (hi + lo) // 2 if how_many_below(h) >= N: hi = h else: lo = h return hi * d % (10**9 + 7) def gcd(a, b): while a: a, b = b % a, a return b
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR FUNC_DEF WHILE VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR RETURN VAR
A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  Since the answer may be very large, return it modulo 10^9 + 7.   Example 1: Input: N = 1, A = 2, B = 3 Output: 2 Example 2: Input: N = 4, A = 2, B = 3 Output: 6 Example 3: Input: N = 5, A = 2, B = 4 Output: 10 Example 4: Input: N = 3, A = 6, B = 4 Output: 8   Note: 1 <= N <= 10^9 2 <= A <= 40000 2 <= B <= 40000
class Solution: def nthMagicalNumber(self, N: int, A: int, B: int) -> int: a_t = A b_t = B col = [] low_anc = a_t while a_t != b_t: if a_t < b_t: col.append(a_t) a_t = a_t + A else: col.append(b_t) b_t = b_t + B col.append(a_t) low_anc = a_t idx = (N - 1) % len(col) ans = (N - 1) // len(col) * low_anc + col[idx] return ans % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
There are n animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number i in the queue will have to visit his office exactly ai times. We will assume that an examination takes much more time than making tests and other extra procedures, and therefore we will assume that once an animal leaves the room, it immediately gets to the end of the queue to the doctor. Of course, if the animal has visited the doctor as many times as necessary, then it doesn't have to stand at the end of the queue and it immediately goes home. Doctor plans to go home after receiving k animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out. Input The first line of input data contains two space-separated integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 1014). In the second line are given space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109). Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator). Output If the doctor will overall carry out less than k examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers — number of animals in the order in which they stand in the queue. Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted. Examples Input 3 3 1 2 1 Output 2 Input 4 10 3 3 2 1 Output -1 Input 7 10 1 3 3 1 2 3 1 Output 6 2 3 Note In the first sample test: * Before examination: {1, 2, 3} * After the first examination: {2, 3} * After the second examination: {3, 2} * After the third examination: {2} In the second sample test: * Before examination: {1, 2, 3, 4, 5, 6, 7} * After the first examination: {2, 3, 4, 5, 6, 7} * After the second examination: {3, 4, 5, 6, 7, 2} * After the third examination: {4, 5, 6, 7, 2, 3} * After the fourth examination: {5, 6, 7, 2, 3} * After the fifth examination: {6, 7, 2, 3, 5} * After the sixth examination: {7, 2, 3, 5, 6} * After the seventh examination: {2, 3, 5, 6} * After the eighth examination: {3, 5, 6, 2} * After the ninth examination: {5, 6, 2, 3} * After the tenth examination: {6, 2, 3}
from sys import stdin, stdout n, k = map(int, stdin.readline().split()) values = list(map(int, stdin.readline().split())) if sum(values) < k: stdout.write("-1") elif sum(values) > k: l = 0 r = k + 1 while r - l > 1: m = (r + l) // 2 cnt = 0 for i in range(n): cnt += min(values[i], m) if cnt > k: r = m else: l = m for i in range(n): k -= min(values[i], l) values[i] -= min(values[i], l) i = 0 while k: if values[i]: values[i] -= 1 k -= 1 i = (i + 1) % n for j in range(i, i + n): if values[j % n]: stdout.write(str(j % n + 1) + " ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING
There are n animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number i in the queue will have to visit his office exactly ai times. We will assume that an examination takes much more time than making tests and other extra procedures, and therefore we will assume that once an animal leaves the room, it immediately gets to the end of the queue to the doctor. Of course, if the animal has visited the doctor as many times as necessary, then it doesn't have to stand at the end of the queue and it immediately goes home. Doctor plans to go home after receiving k animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out. Input The first line of input data contains two space-separated integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 1014). In the second line are given space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109). Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator). Output If the doctor will overall carry out less than k examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers — number of animals in the order in which they stand in the queue. Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted. Examples Input 3 3 1 2 1 Output 2 Input 4 10 3 3 2 1 Output -1 Input 7 10 1 3 3 1 2 3 1 Output 6 2 3 Note In the first sample test: * Before examination: {1, 2, 3} * After the first examination: {2, 3} * After the second examination: {3, 2} * After the third examination: {2} In the second sample test: * Before examination: {1, 2, 3, 4, 5, 6, 7} * After the first examination: {2, 3, 4, 5, 6, 7} * After the second examination: {3, 4, 5, 6, 7, 2} * After the third examination: {4, 5, 6, 7, 2, 3} * After the fourth examination: {5, 6, 7, 2, 3} * After the fifth examination: {6, 7, 2, 3, 5} * After the sixth examination: {7, 2, 3, 5, 6} * After the seventh examination: {2, 3, 5, 6} * After the eighth examination: {3, 5, 6, 2} * After the ninth examination: {5, 6, 2, 3} * After the tenth examination: {6, 2, 3}
read = lambda: map(int, input().split()) n, k = read() a = list(read()) b = sorted([(a[i], i) for i in range(n)]) if sum(a) < k: print(-1) exit() j = 0 x2 = 0 for i in range(n): x = b[i][0] cur = (n - j) * (x - x2) if cur > k: break x2 = x k -= cur j += 1 if n == j: print() exit() y = k // (n - j) for i in range(n): a[i] -= y m = k % (n - j) ans = [] j1 = 0 i1 = 0 for i in range(n): if a[i] > x2 and j1 < m: j1 += 1 if a[i] > x2 + 1: ans.append(i + 1) if j1 == m: i1 = i + 1 if m == 0: i1 = 0 break c = [(i + 1) for i in range(i1, n) if a[i] > x2] ans = c + ans print(*ans)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
There are n animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number i in the queue will have to visit his office exactly ai times. We will assume that an examination takes much more time than making tests and other extra procedures, and therefore we will assume that once an animal leaves the room, it immediately gets to the end of the queue to the doctor. Of course, if the animal has visited the doctor as many times as necessary, then it doesn't have to stand at the end of the queue and it immediately goes home. Doctor plans to go home after receiving k animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out. Input The first line of input data contains two space-separated integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 1014). In the second line are given space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109). Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator). Output If the doctor will overall carry out less than k examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers — number of animals in the order in which they stand in the queue. Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted. Examples Input 3 3 1 2 1 Output 2 Input 4 10 3 3 2 1 Output -1 Input 7 10 1 3 3 1 2 3 1 Output 6 2 3 Note In the first sample test: * Before examination: {1, 2, 3} * After the first examination: {2, 3} * After the second examination: {3, 2} * After the third examination: {2} In the second sample test: * Before examination: {1, 2, 3, 4, 5, 6, 7} * After the first examination: {2, 3, 4, 5, 6, 7} * After the second examination: {3, 4, 5, 6, 7, 2} * After the third examination: {4, 5, 6, 7, 2, 3} * After the fourth examination: {5, 6, 7, 2, 3} * After the fifth examination: {6, 7, 2, 3, 5} * After the sixth examination: {7, 2, 3, 5, 6} * After the seventh examination: {2, 3, 5, 6} * After the eighth examination: {3, 5, 6, 2} * After the ninth examination: {5, 6, 2, 3} * After the tenth examination: {6, 2, 3}
n, k = map(int, input().split()) a = list(map(int, input().split())) c = len(a) while c and k // c: t = k // c v = 0 for i in range(len(a)): if a[i] == 0: continue v += a[i] - max(0, a[i] - t) a[i] = max(0, a[i] - t) if a[i] == 0: c -= 1 k -= v for i in range(len(a)): if k == 0: break if a[i] != 0: k -= 1 a[i] -= 1 res = [] for j in range(len(a)): if a[(j + i) % len(a)] != 0: res.append(str((j + i) % len(a) + 1)) if k > 0: print(-1) else: print(" ".join(res))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
There are n animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number i in the queue will have to visit his office exactly ai times. We will assume that an examination takes much more time than making tests and other extra procedures, and therefore we will assume that once an animal leaves the room, it immediately gets to the end of the queue to the doctor. Of course, if the animal has visited the doctor as many times as necessary, then it doesn't have to stand at the end of the queue and it immediately goes home. Doctor plans to go home after receiving k animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out. Input The first line of input data contains two space-separated integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 1014). In the second line are given space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109). Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator). Output If the doctor will overall carry out less than k examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers — number of animals in the order in which they stand in the queue. Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted. Examples Input 3 3 1 2 1 Output 2 Input 4 10 3 3 2 1 Output -1 Input 7 10 1 3 3 1 2 3 1 Output 6 2 3 Note In the first sample test: * Before examination: {1, 2, 3} * After the first examination: {2, 3} * After the second examination: {3, 2} * After the third examination: {2} In the second sample test: * Before examination: {1, 2, 3, 4, 5, 6, 7} * After the first examination: {2, 3, 4, 5, 6, 7} * After the second examination: {3, 4, 5, 6, 7, 2} * After the third examination: {4, 5, 6, 7, 2, 3} * After the fourth examination: {5, 6, 7, 2, 3} * After the fifth examination: {6, 7, 2, 3, 5} * After the sixth examination: {7, 2, 3, 5, 6} * After the seventh examination: {2, 3, 5, 6} * After the eighth examination: {3, 5, 6, 2} * After the ninth examination: {5, 6, 2, 3} * After the tenth examination: {6, 2, 3}
from sys import stdin n, k = [int(x) for x in stdin.readline().split()] a = [int(x) for x in stdin.readline().split()] if sum(a) < k: print(-1) elif sum(a) == k: pass else: diff = 0 kCopy = k sortA = sorted(a, reverse=True) while sortA: nxt = sortA[-1] nxt -= diff if len(sortA) * nxt <= k: diff += nxt k -= len(sortA) * nxt sortA.pop() else: break p = k % len(sortA) diff += k // len(sortA) ind = 0 while p: if a[ind] > diff: p -= 1 ind += 1 outL = [] for x in range(ind, len(a)): if a[x] > diff: outL.append(str(x + 1)) for x in range(ind): if a[x] > diff + 1: outL.append(str(x + 1)) if outL: print(" ".join(outL))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
There are n animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number i in the queue will have to visit his office exactly ai times. We will assume that an examination takes much more time than making tests and other extra procedures, and therefore we will assume that once an animal leaves the room, it immediately gets to the end of the queue to the doctor. Of course, if the animal has visited the doctor as many times as necessary, then it doesn't have to stand at the end of the queue and it immediately goes home. Doctor plans to go home after receiving k animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out. Input The first line of input data contains two space-separated integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 1014). In the second line are given space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109). Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator). Output If the doctor will overall carry out less than k examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers — number of animals in the order in which they stand in the queue. Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted. Examples Input 3 3 1 2 1 Output 2 Input 4 10 3 3 2 1 Output -1 Input 7 10 1 3 3 1 2 3 1 Output 6 2 3 Note In the first sample test: * Before examination: {1, 2, 3} * After the first examination: {2, 3} * After the second examination: {3, 2} * After the third examination: {2} In the second sample test: * Before examination: {1, 2, 3, 4, 5, 6, 7} * After the first examination: {2, 3, 4, 5, 6, 7} * After the second examination: {3, 4, 5, 6, 7, 2} * After the third examination: {4, 5, 6, 7, 2, 3} * After the fourth examination: {5, 6, 7, 2, 3} * After the fifth examination: {6, 7, 2, 3, 5} * After the sixth examination: {7, 2, 3, 5, 6} * After the seventh examination: {2, 3, 5, 6} * After the eighth examination: {3, 5, 6, 2} * After the ninth examination: {5, 6, 2, 3} * After the tenth examination: {6, 2, 3}
def chtoetodvoichnyupoisk(a, c): r = len(a) l = 0 while r - l > 1: s = (l + r) // 2 if s == len(a): if a[s - 1] >= c: r = s else: l = s if a[s] >= c: r = s else: l = s if r == 1 and a[0] >= c: r = 0 return r def cycle(k, a): sa = sorted(a) r = sa[-1] l = 0 while r - l > 1: c = (r + l) // 2 i = chtoetodvoichnyupoisk(sa, c) s = sum(sa[:i]) + c * (len(a) - i) if s > k: r = c if s < k: l = c if s == k: return c return l n, k = map(int, input().split()) a = list(map(int, input().split())) q = list(range(1, n + 1)) if k > sum(a): print(-1) else: c = cycle(k, a) sa = sorted(a) i = chtoetodvoichnyupoisk(sa, c) s = sum(sa[:i]) + c * (len(a) - i) k = k - s a = [(i - c) for i in a] q = [i for i in q if a[i - 1] > 0] q = q[k:] + list(filter(lambda x: a[x - 1] > 1, q[:k])) print(*q)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR RETURN VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
for _ in range(int(input())): dic = {} for i in range(int(input())): s, i = input().split() e = int(i) if dic.get(e) == None: dic[e] = ["", 0] dic[e][0] = s dic[e][1] += 1 q = "Nobody wins." mi = 10**10 for i in dic: if dic[i][1] == 1: if i < mi: mi = i q = dic[i][0] print(q)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR LIST STRING NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR IF VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
t = int(input()) while t: re = [] st = {} m = {} for _ in range(int(input())): a, b = input().split() b = int(b) if b in list(st.keys()): st[b] += 1 else: st[b] = 1 m[b] = a re.append(int(b)) count = 0 re = list(set(re)) re.sort() ind = 0 for i in re: if st[i] == 1: count += 1 ind = i break if count == 1: print(m[ind]) else: print("Nobody wins.") t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING VAR NUMBER
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
for _ in range(int(input())): n = int(input()) goes = [] for u in range(n): z = input().split() goes.append((int(z[1]), z[0])) goes.sort() res = "Nobody wins." last = goes[0][0] - 1 win = False for val, nm in goes: if val == last: win = False else: if win: res = lastnm break win = True lastnm = nm last = val if win: res = lastnm print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
s = int(input()) for i in range(s): n = int(input()) m = dict() for i in range(n): sum = input().split() if int(sum[1]) in m: m[int(sum[1])].append(sum[0]) else: m[int(sum[1])] = [sum[0]] l = sorted(m.keys()) s1 = "Nobody wins." for i in l: if len(m[i]) == 1: s1 = m[i][0] break print(s1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
t = int(input()) list1 = [] list2 = [] newlist = [] for i in range(t): p = int(input()) list1 = [] list2 = [] list3 = [] newlist = [] for j in range(p): s = input() name, number = s.split() number = int(number) list1.append(number) list2.append(name) for n in list1: if n not in newlist: newlist.append(n) elif n in newlist: list3.append(n) for m in list3: if m in newlist: newlist.remove(m) newlist.sort() if len(newlist) == 0: print("Nobody wins.") else: z = newlist[0] print(list2[list1.index(z)])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
try: t = int(input()) while t: t -= 1 n = int(input()) arr = [] obj = {} for i in range(n): x, y = input().split() y = int(y) arr.append([x, y]) if y in obj: obj[y].append(x) else: obj[y] = [x] arr.sort(key=lambda i: i[1], reverse=True) while len(arr) and len(obj[arr[-1][1]]) > 1: arr.pop() if len(arr) == 0: print("Nobody wins.") else: print(arr.pop()[0]) except: pass
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
for _ in range(int(input())): np = int(input()) dict = {} for i in range(np): name, num = input().split() num = int(num) if dict.get(num, -1) == -1: dict[num] = name else: dict[num] = 0 arr = [] for key in dict: arr.append(key) arr.sort() ans = -1 for i in arr: if dict[i] != 0: ans = i break if ans != -1: print(dict[ans]) else: print("Nobody wins.")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
t = int(input()) for _ in range(t): n = int(input()) players = {} for i in range(n): name, number = input().split() number = int(number) if number in players: players[number].append(name) else: players[number] = [name] smallest_unique = float("inf") for number in players: if len(players[number]) == 1 and number < smallest_unique: smallest_unique = number if smallest_unique == float("inf"): print("Nobody wins.") else: print(players[smallest_unique][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
for _ in range(int(input())): q = [] for __ in range(int(input())): a, b = list(input().split()) b = int(b) q.append((a, b)) q = sorted(q, key=lambda x: x[1]) i = 0 win = "" if len(q) == 1: continue while i < len(q): t = q[i][1] if i == len(q) - 1 or q[i][1] != q[i + 1][1]: win = q[i][0] break while i < len(q) and q[i][1] == t: i += 1 if i == len(q): break if win == "": print("Nobody wins.") else: print(win)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
def comp(a, b): if len(a) > len(b): return b elif len(b) > len(a): return a else: i = 0 while True: if a[i] > b[i]: return b elif b[i] > a[i]: return a i += 1 for _ in range(int(input())): n = int(input()) a = [] b = [] c = [] for i in range(n): name, score = map(str, input().split()) a.append([name, score]) if score not in c: if score in b: b.remove(score) c.append(score) else: b.append(score) if len(b) == 0: print("Nobody wins.") else: ans = b[0] for i in range(1, len(b)): ans = comp(ans, b[i]) for i in range(len(a)): if a[i][1] == ans: print(a[i][0])
FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR RETURN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
t = int(input()) for i in range(t): n = int(input()) d = {} c = 0 for j in range(n): a, b = input().split() b = int(b) if b not in d.keys(): d[b] = a else: d[b] = "" for k in sorted(d.keys()): if d[k] != "": print(d[k]) c += 1 break if c == 0: print("Nobody wins.")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
t = int(input()) for _ in range(t): n = int(input()) l = [] hash = {} for i in range(n): a, b = map(str, input().split()) l.append([a, b]) try: hash[b] except: hash[b] = 1 else: hash[b] += 1 ans = [] for i in l: a, b = i if hash[b] == 1: ans.append([int(b), a]) ans.sort() if ans != []: print(ans[0][1]) else: print("Nobody wins.")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF VAR LIST EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
for _ in range(int(input())): n = int(input()) a = [] b = [] for i in range(n): name, score = map(str, input().split()) a.append([name, score]) b.append(score) a.sort(key=lambda x: x[1]) b.sort() i = 0 while True: p = b.count(b[i]) if p == 1: print(a[i][0]) break else: i += p if i >= n: print("Nobody wins.") break
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
for i in range(int(input())): n = int(input()) x = {} for i in range(n): s, a = input().split() a = int(a) if a not in x.keys(): x[a] = s else: x[a] = "-1" ans = "Nobody wins." m = 200000000001 for i in x.keys(): if i < m and x[i] != "-1": ans = x[i] m = i print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
t = int(input()) while t: t -= 1 n = int(input()) a = [] d = {} while n: n -= 1 l = input().split() v = int(l[1]) if d.get(v) == None: d[v] = ["", 0] d[v][0] = l[0] d[v][1] += 1 mini = float("inf") s = "Nobody wins." for i in d: if d[i][1] == 1: if mini > i: mini = i s = d[i][0] print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR LIST STRING NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR STRING FOR VAR VAR IF VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
import sys from itertools import groupby input = sys.stdin.readline def rii(): return range(int(input().strip())) def ii(): return int(input().strip()) def mii(): return map(int, input().strip().split(" ")) def lmii(): return list(map(int, input().strip().split(" "))) def si(): return str(input().strip()) failure = "Nobody wins." class Guess: def __init__(self, name, number): self.s = name self.n = int(number) def __lt__(self, other): return self.n < other.n def __gt__(self, other): return self.n > other.n def __le__(self, other): return self.n <= other.n def __ge__(self, other): return self.n >= other.n def __eq__(self, other): return self.n == other.n def solve(*args): n, players = args uniques = [ name for freq, name in [(len(list(y)), x.s) for x, y in groupby(players)] if freq == 1 ] return uniques[0] if len(uniques) > 0 else failure def do_codechef(): for t in rii(): n = ii() players = list() for _ in range(n): players.append(Guess(*si().split(" "))) players.sort() print(solve(n, players)) do_codechef() exit()
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR VAR FUNC_DEF RETURN VAR VAR FUNC_DEF RETURN VAR VAR FUNC_DEF RETURN VAR VAR FUNC_DEF RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
for _ in range(int(input())): n = int(input()) a = {} b = {} c = {} for _ in range(n): x, y = input().split() y = int(y) a[x] = y b[y] = x try: c[y] += 1 except: c[y] = 1 ans = 1000000000000000000 for i in c: if c[i] == 1: ans = min(ans, i) if ans == 1000000000000000000: print("Nobody wins.") else: print(b[ans])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
T = int(input()) for t in range(0, T): n = int(input()) s = [0] * n np = [0] * n for i in range(0, n): s[i], np[i] = input().split() np[i] = int(np[i]) no = np[:] no.sort() ans = "-1" if no[0] != no[1]: ans = no[0] else: for i in range(1, n - 1): if no[i] != no[i - 1] and no[i] != no[i + 1]: ans = no[i] break else: continue if ans == "-1" and no[n - 2] != no[n - 1]: ans = no[n - 1] for i in range(0, n): if ans == np[i]: ans = int(i) break if ans == "-1": print("Nobody wins.") else: print(s[ans])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR STRING VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
t = int(input()) for tc in range(t): n = int(input()) players = dict() for i in range(n): inputTmp = input().split(" ") name = inputTmp[0] num = int(inputTmp[1]) if num not in players: players[num] = name else: players[num] = 0 winner = False for num in sorted(players.keys()): if players[num] != 0: print(players[num]) winner = True break if not winner: print("Nobody wins.")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
def unique_number(arr): n = len(arr) if n == 1: return arr[0] if arr[0] != arr[1]: return arr[0] for i in range(1, n - 1): if arr[i - 1] != arr[i] and arr[i] != arr[i + 1]: return arr[i] if arr[-2] != arr[-1]: return arr[-1] return 0 t = int(input()) for it in range(t): n = int(input()) arr = [] mymap = {} for i in range(n): name, num = input().split() num = int(num) arr.append(num) mymap[num] = name arr = sorted(arr) smun = unique_number(arr) if smun == 0: print("Nobody wins.") else: print(mymap[smun])
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
for t in range(int(input())): n = int(input()) s = input().split() num = int(s[1]) s = s[0] d = dict() d1 = dict() d[num] = s d1[num] = s for i in range(1, n): s = input().split() num = int(s[1]) s = s[0] if num in d: d[num] = None d1[num] = None else: d[num] = s d1[None] = s for i in d1: if d1[i] == None: d.pop(i) if len(d) == 0: print("Nobody wins.") else: print(d[min(d)])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NONE ASSIGN VAR VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR NONE VAR FOR VAR VAR IF VAR VAR NONE EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Lucy had recently learned the game, called Natural Numbers. The rules of the game are really simple. There are N players. At the same time, every player says one natural number. Let's call the number said by the i-th player Ai. The person with the smallest unique number (that is, the smallest number that was not said by anybody else) wins. Sometimes, there is a case when there are no unique numbers at all. Then the game is obviously a draw, so nobody wins it. Sometimes, it's hard to determine the winner, especially, when the number of players is enormous. So in this problem, your assignment will be: given the names of the players and the numbers every of them have said. Please, tell the name of the winner, or determine that nobody wins. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of every test case consists of a single integer N - the number of players. Then, N lines will follow. Each of these N lines will consist of the player's name and the number Ai said by her, separated by a single space. -----Output----- For each test case, output a single line containing an answer to the corresponding test case - the name of the winner, or a string "Nobody wins.", if nobody wins the game. -----Example----- Input: 2 5 Kouta 1 Yuka 1 Mayu 3 Lucy 2 Nana 5 2 Lucy 2 Nana 2 Output: Lucy Nobody wins. -----Scoring----- Subtask 1 (17 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 10 Subtask 2 (19 points): T = 10000, 1 <= N <= 10, 1 <= Ai <= 2*109 Subtask 3 (30 points): T = 100, 1 <= N <= 1000, 1<= Ai <= 2*109 Subtask 4 (34 points): T = 10, 1 <= N <= 10000, 1 <= Ai <= 2*109 You can safely assume that in all the test cases the length of any name will not exceed five letters. All the players' names are unique.
t = int(input()) while t: n = int(input()) d = {} c = {} for i in range(n): a, b = input().split() d[a] = int(b) c[int(b)] = c.get(int(b), 0) + 1 l = [] for i in c: if c[i] == 1: l.append(i) if len(l) == 0: print("Nobody wins.") else: mini = min(l) for i in d: if d[i] == mini: print(i) break t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a string representing n, you should return the smallest good base of n in string format. Example 1: Input: "13" Output: "3" Explanation: 13 base 3 is 111. Example 2: Input: "4681" Output: "8" Explanation: 4681 base 8 is 11111. Example 3: Input: "1000000000000000000" Output: "999999999999999999" Explanation: 1000000000000000000 base 999999999999999999 is 11. Note: The range of n is [3, 10^18]. The string representing n is always valid and will not have leading zeros.
class Solution: def smallestGoodBase(self, n): return self.use_binary_search(n) def use_math(self, n): n = int(n) max_m = int(math.log(n, 2)) for m in range(max_m, 1, -1): k = int(n**m**-1) if (k ** (m + 1) - 1) // (k - 1) == n: return str(k) return str(n - 1) def use_binary_search(self, n): n = int(n) for d in range(60, 0, -1): if 1 << d < n: base = self.binary_search(n, d) if base: return str(base) return str(n - 1) def binary_search(self, n, d): lo, hi = 1, int(pow(n, 1.0 / d) + 1) while lo < hi: mid = lo + (hi - lo) // 2 total = 1 for i in range(1, d + 1): total += mid**i if total == n: return mid elif total < n: lo = mid + 1 else: hi = mid return 0
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN NUMBER
For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a string representing n, you should return the smallest good base of n in string format. Example 1: Input: "13" Output: "3" Explanation: 13 base 3 is 111. Example 2: Input: "4681" Output: "8" Explanation: 4681 base 8 is 11111. Example 3: Input: "1000000000000000000" Output: "999999999999999999" Explanation: 1000000000000000000 base 999999999999999999 is 11. Note: The range of n is [3, 10^18]. The string representing n is always valid and will not have leading zeros.
class Solution(object): def smallestGoodBase(self, n): num = int(n) thisLen = int(math.log(num, 2)) + 1 while thisLen > 2: thisBase = int(num ** (1.0 / (thisLen - 1))) if num * (thisBase - 1) == thisBase**thisLen - 1: return str(thisBase) thisLen -= 1 return str(num - 1)
CLASS_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves. Input The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets. Output In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1. Examples Input 4 1 2 3 2 3 2 1 2 Output 4 2 3 1 2 3 4 2 3 Input 2 1 100500 1 100500 Output 0
n = int(input()) target = list(map(int, input().split())) got = list(map(int, input().split())) swaps = [] for i in range(n - 1, -1, -1): for j in range(i + 1): if got[j] == target[i] and j != i: swaps.append((j + 1, j + 1 + 1)) got[j], got[j + 1] = got[j + 1], got[j] print(len(swaps)) for i in swaps: print(*i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves. Input The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets. Output In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1. Examples Input 4 1 2 3 2 3 2 1 2 Output 4 2 3 1 2 3 4 2 3 Input 2 1 100500 1 100500 Output 0
number = int(input()) origin = list(input().split()) array = list(input().split()) positions = {} pre = [] last = [] result = 0 for i in range(number): if origin[i] == array[i]: continue else: j = i + 1 index = len(pre) for j in range(i + 1, number): pre.insert(index, j + 1) last.insert(index, j) if array[j] == origin[i]: break array[i + 1 : j + 1] = array[i:j] result += j - i print(result) for i in range(len(pre)): print(str(last[i]) + " " + str(pre[i]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR VAR
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves. Input The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets. Output In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1. Examples Input 4 1 2 3 2 3 2 1 2 Output 4 2 3 1 2 3 4 2 3 Input 2 1 100500 1 100500 Output 0
import sys n = int(sys.stdin.readline()) a = [int(x) for x in sys.stdin.readline().split()] assert len(a) == n b = [int(x) for x in sys.stdin.readline().split()] assert len(b) == n ans = [] for i in range(n): j = i while b[j] != a[i]: j += 1 while j > i: ans += [(j, j + 1)] b[j - 1], b[j] = b[j], b[j - 1] j -= 1 print(len(ans)) for p, q in ans: print(p, +q)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves. Input The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets. Output In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1. Examples Input 4 1 2 3 2 3 2 1 2 Output 4 2 3 1 2 3 4 2 3 Input 2 1 100500 1 100500 Output 0
n = int(input()) l1 = list(map(int, input().split())) l2 = list(map(int, input().split())) ans = [] i = 0 while i < n - 1: for j in range(i, n): if l2[j] == l1[i]: for k in range(j, i, -1): ans.append([k, k + 1]) l2[k], l2[k - 1] = l2[k - 1], l2[k] break i += 1 print(len(ans)) for i in ans: print(*i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves. Input The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets. Output In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1. Examples Input 4 1 2 3 2 3 2 1 2 Output 4 2 3 1 2 3 4 2 3 Input 2 1 100500 1 100500 Output 0
n = int(input()) (*a,) = map(int, input().split()) (*b,) = map(int, input().split()) ans, s, k = [], 0, 0 while b: x = b.index(a[k]) b.remove(a[k]) ans.append((x + k + 1, k + 1)) s += x k += 1 print(s) for i in ans: for j in range(i[0], i[1], -1): print(j - 1, j)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves. Input The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets. Output In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1. Examples Input 4 1 2 3 2 3 2 1 2 Output 4 2 3 1 2 3 4 2 3 Input 2 1 100500 1 100500 Output 0
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) moves = [] for i in range(n): for j in range(i, n): if a[i] == b[j]: for k in range(j - i): moves.append((j + 1 - k, j - k)) b = [b[j]] + b[:j] + b[j + 1 :] break print(len(moves)) for move in moves: print(f"{move[1]} {move[0]}")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING VAR NUMBER
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves. Input The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets. Output In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1. Examples Input 4 1 2 3 2 3 2 1 2 Output 4 2 3 1 2 3 4 2 3 Input 2 1 100500 1 100500 Output 0
def rangtolist(dv, l, r): mv = [] for c in range(r, l, -1): mv.append((c - 1, c)) return mv, left(dv, l, r) def left(dv, l, r): nv = dv[:l] nv += dv[r : r + 1] nv += dv[l:r] nv += dv[r + 1 :] return nv def pr(v): print("***") for c in v: print(c, end=" ") print("\n***") n = int(input()) sv = list(map(int, input().split(" "))) dv = list(map(int, input().split(" "))) mv = [] for c in range(n): dx = dv.index(sv[c], c) if dx == c: continue tv, dv = rangtolist(dv, c, dx) mv += tv print(len(mv)) for c in mv: print("{} {}".format(c[0] + 1, c[1] + 1))
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves. Input The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets. Output In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1. Examples Input 4 1 2 3 2 3 2 1 2 Output 4 2 3 1 2 3 4 2 3 Input 2 1 100500 1 100500 Output 0
def sor(t): f = [] for i in range(n): for j in range(n - i - 1): if t[j] > t[j + 1]: t[j], t[j + 1] = t[j + 1], t[j] f.append([j + 1, j + 2]) return f n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) if a == b: print(0) else: A = sor(b) A += sor(a)[::-1] print(len(A)) for j in A: print(*j)
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves. Input The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets. Output In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1. Examples Input 4 1 2 3 2 3 2 1 2 Output 4 2 3 1 2 3 4 2 3 Input 2 1 100500 1 100500 Output 0
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) num = 0 swap = [] for i in range(n - 1, -1, -1): if b[i] != a[i]: right = a[i] ind = b.index(right) s = b[i] b[i] = a[i] b[ind] = s num += 2 * (i - ind) - 1 for j in range(i - ind): li = [str(i - j), str(i - j + 1)] swap.append(li) for j in range(i - ind - 1): li = [str(ind + j + 2), str(ind + j + 3)] swap.append(li) print(num) if num != 0: for i in range(num): print(" ".join(swap[i]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves. Input The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets. Output In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1. Examples Input 4 1 2 3 2 3 2 1 2 Output 4 2 3 1 2 3 4 2 3 Input 2 1 100500 1 100500 Output 0
import sys n = int(input()) arr2 = [int(x) for x in input().split()] arr = [int(x) for x in input().split()] ans = [] ans2 = [] for i in range(n - 1): for j in range(0, n - i - 1): if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] ans.append((j, j + 1)) for i in range(n - 1): for j in range(0, n - i - 1): if arr2[j] > arr2[j + 1]: arr2[j], arr2[j + 1] = arr2[j + 1], arr2[j] ans2.append((j, j + 1)) print(len(ans) + len(ans2)) for i in range(len(ans)): print(ans[i][0] + 1, ans[i][1] + 1) for i in range(len(ans2) - 1, -1, -1): print(ans2[i][0] + 1, ans2[i][1] + 1)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves. Input The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets. Output In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1. Examples Input 4 1 2 3 2 3 2 1 2 Output 4 2 3 1 2 3 4 2 3 Input 2 1 100500 1 100500 Output 0
n = int(input()) start = [] end = [] s1 = input().split(" ") s2 = input().split(" ") for i in range(n): start.append(int(s1[i])) end.append(int(s2[i])) q = 0 string = "" for i in range(n): if start[i] != end[i]: for j in range(i + 1, n): if end[j] == start[i]: for k in range(j - i): end[j - k], end[j - k - 1] = end[j - k - 1], end[j - k] string += str(j - k) + " " + str(j - k + 1) + "\n" q += 1 break print(q) print(string)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR STRING FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves. Input The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets. Output In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1. Examples Input 4 1 2 3 2 3 2 1 2 Output 4 2 3 1 2 3 4 2 3 Input 2 1 100500 1 100500 Output 0
n = int(input()) a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] cnt = 0 ans = [] for i in range(n): if a[i] == b[i]: continue j = i while j < n and b[j] != a[i]: j += 1 while j > i: b[j], b[j - 1] = b[j - 1], b[j] ans.append([j, j + 1]) j -= 1 print(len(ans)) for i in range(len(ans)): print(ans[i][0], ans[i][1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER
Grigory has $n$ magic stones, conveniently numbered from $1$ to $n$. The charge of the $i$-th stone is equal to $c_i$. Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index $i$, where $2 \le i \le n - 1$), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge $c_i$ changes to $c_i' = c_{i + 1} + c_{i - 1} - c_i$. Andrew, Grigory's friend, also has $n$ stones with charges $t_i$. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes $c_i$ into $t_i$ for all $i$? -----Input----- The first line contains one integer $n$ ($2 \le n \le 10^5$) — the number of magic stones. The second line contains integers $c_1, c_2, \ldots, c_n$ ($0 \le c_i \le 2 \cdot 10^9$) — the charges of Grigory's stones. The second line contains integers $t_1, t_2, \ldots, t_n$ ($0 \le t_i \le 2 \cdot 10^9$) — the charges of Andrew's stones. -----Output----- If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes". Otherwise, print "No". -----Examples----- Input 4 7 2 4 12 7 15 10 12 Output Yes Input 3 4 4 4 1 2 3 Output No -----Note----- In the first example, we can perform the following synchronizations ($1$-indexed): First, synchronize the third stone $[7, 2, \mathbf{4}, 12] \rightarrow [7, 2, \mathbf{10}, 12]$. Then synchronize the second stone: $[7, \mathbf{2}, 10, 12] \rightarrow [7, \mathbf{15}, 10, 12]$. In the second example, any operation with the second stone will not change its charge.
n = int(input()) y = lambda l: sorted(list(l[i + 1] - l[i] for i in range(len(l) - 1))) z = lambda: list(map(int, input().split())) c = z() t = z() print(["No", "Yes"][c[0] == t[0] and y(c) == y(t)])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST STRING STRING VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Grigory has $n$ magic stones, conveniently numbered from $1$ to $n$. The charge of the $i$-th stone is equal to $c_i$. Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index $i$, where $2 \le i \le n - 1$), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge $c_i$ changes to $c_i' = c_{i + 1} + c_{i - 1} - c_i$. Andrew, Grigory's friend, also has $n$ stones with charges $t_i$. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes $c_i$ into $t_i$ for all $i$? -----Input----- The first line contains one integer $n$ ($2 \le n \le 10^5$) — the number of magic stones. The second line contains integers $c_1, c_2, \ldots, c_n$ ($0 \le c_i \le 2 \cdot 10^9$) — the charges of Grigory's stones. The second line contains integers $t_1, t_2, \ldots, t_n$ ($0 \le t_i \le 2 \cdot 10^9$) — the charges of Andrew's stones. -----Output----- If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes". Otherwise, print "No". -----Examples----- Input 4 7 2 4 12 7 15 10 12 Output Yes Input 3 4 4 4 1 2 3 Output No -----Note----- In the first example, we can perform the following synchronizations ($1$-indexed): First, synchronize the third stone $[7, 2, \mathbf{4}, 12] \rightarrow [7, 2, \mathbf{10}, 12]$. Then synchronize the second stone: $[7, \mathbf{2}, 10, 12] \rightarrow [7, \mathbf{15}, 10, 12]$. In the second example, any operation with the second stone will not change its charge.
n = int(input()) c = list(map(int, input().split(" "))) t = list(map(int, input().split(" "))) check = c[0] == t[0] c_diff = [(c[i] - c[i - 1]) for i in range(1, n)] t_diff = [(t[i] - t[i - 1]) for i in range(1, n)] c_diff.sort() t_diff.sort() if check and c_diff == t_diff: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Grigory has $n$ magic stones, conveniently numbered from $1$ to $n$. The charge of the $i$-th stone is equal to $c_i$. Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index $i$, where $2 \le i \le n - 1$), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge $c_i$ changes to $c_i' = c_{i + 1} + c_{i - 1} - c_i$. Andrew, Grigory's friend, also has $n$ stones with charges $t_i$. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes $c_i$ into $t_i$ for all $i$? -----Input----- The first line contains one integer $n$ ($2 \le n \le 10^5$) — the number of magic stones. The second line contains integers $c_1, c_2, \ldots, c_n$ ($0 \le c_i \le 2 \cdot 10^9$) — the charges of Grigory's stones. The second line contains integers $t_1, t_2, \ldots, t_n$ ($0 \le t_i \le 2 \cdot 10^9$) — the charges of Andrew's stones. -----Output----- If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes". Otherwise, print "No". -----Examples----- Input 4 7 2 4 12 7 15 10 12 Output Yes Input 3 4 4 4 1 2 3 Output No -----Note----- In the first example, we can perform the following synchronizations ($1$-indexed): First, synchronize the third stone $[7, 2, \mathbf{4}, 12] \rightarrow [7, 2, \mathbf{10}, 12]$. Then synchronize the second stone: $[7, \mathbf{2}, 10, 12] \rightarrow [7, \mathbf{15}, 10, 12]$. In the second example, any operation with the second stone will not change its charge.
number = int(input()) gregory = list(map(int, input().split())) andrew = list(map(int, input().split())) glen, alen = len(gregory), len(andrew) def canSync(g, a): gdiffs, adiffs = [], [] for i in range(1, len(g)): gdiffs.append(g[i] - g[i - 1]) adiffs.append(a[i] - a[i - 1]) gdiffs.sort() adiffs.sort() gdiffs.append(g[0]) adiffs.append(a[0]) return "Yes" if gdiffs == adiffs else "No" print(canSync(gregory, andrew))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR STRING STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Grigory has $n$ magic stones, conveniently numbered from $1$ to $n$. The charge of the $i$-th stone is equal to $c_i$. Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index $i$, where $2 \le i \le n - 1$), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge $c_i$ changes to $c_i' = c_{i + 1} + c_{i - 1} - c_i$. Andrew, Grigory's friend, also has $n$ stones with charges $t_i$. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes $c_i$ into $t_i$ for all $i$? -----Input----- The first line contains one integer $n$ ($2 \le n \le 10^5$) — the number of magic stones. The second line contains integers $c_1, c_2, \ldots, c_n$ ($0 \le c_i \le 2 \cdot 10^9$) — the charges of Grigory's stones. The second line contains integers $t_1, t_2, \ldots, t_n$ ($0 \le t_i \le 2 \cdot 10^9$) — the charges of Andrew's stones. -----Output----- If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes". Otherwise, print "No". -----Examples----- Input 4 7 2 4 12 7 15 10 12 Output Yes Input 3 4 4 4 1 2 3 Output No -----Note----- In the first example, we can perform the following synchronizations ($1$-indexed): First, synchronize the third stone $[7, 2, \mathbf{4}, 12] \rightarrow [7, 2, \mathbf{10}, 12]$. Then synchronize the second stone: $[7, \mathbf{2}, 10, 12] \rightarrow [7, \mathbf{15}, 10, 12]$. In the second example, any operation with the second stone will not change its charge.
n = int(input()) c = [int(i) for i in input().split()] t = [int(i) for i in input().split()] new1 = [] new2 = [] for i in range(n - 1): new1.append(c[i + 1] - c[i]) new2.append(t[i + 1] - t[i]) if c[0] == t[0] and c[-1] == t[-1] and sorted(new1) == sorted(new2): print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Grigory has $n$ magic stones, conveniently numbered from $1$ to $n$. The charge of the $i$-th stone is equal to $c_i$. Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index $i$, where $2 \le i \le n - 1$), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge $c_i$ changes to $c_i' = c_{i + 1} + c_{i - 1} - c_i$. Andrew, Grigory's friend, also has $n$ stones with charges $t_i$. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes $c_i$ into $t_i$ for all $i$? -----Input----- The first line contains one integer $n$ ($2 \le n \le 10^5$) — the number of magic stones. The second line contains integers $c_1, c_2, \ldots, c_n$ ($0 \le c_i \le 2 \cdot 10^9$) — the charges of Grigory's stones. The second line contains integers $t_1, t_2, \ldots, t_n$ ($0 \le t_i \le 2 \cdot 10^9$) — the charges of Andrew's stones. -----Output----- If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes". Otherwise, print "No". -----Examples----- Input 4 7 2 4 12 7 15 10 12 Output Yes Input 3 4 4 4 1 2 3 Output No -----Note----- In the first example, we can perform the following synchronizations ($1$-indexed): First, synchronize the third stone $[7, 2, \mathbf{4}, 12] \rightarrow [7, 2, \mathbf{10}, 12]$. Then synchronize the second stone: $[7, \mathbf{2}, 10, 12] \rightarrow [7, \mathbf{15}, 10, 12]$. In the second example, any operation with the second stone will not change its charge.
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) x = [] y = [] for i in range(n - 1): x.append(a[i + 1] - a[i]) for i in range(n - 1): y.append(b[i + 1] - b[i]) x.sort() y.sort() if x == y and a[0] == b[0] and a[n - 1] == b[n - 1]: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Grigory has $n$ magic stones, conveniently numbered from $1$ to $n$. The charge of the $i$-th stone is equal to $c_i$. Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index $i$, where $2 \le i \le n - 1$), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge $c_i$ changes to $c_i' = c_{i + 1} + c_{i - 1} - c_i$. Andrew, Grigory's friend, also has $n$ stones with charges $t_i$. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes $c_i$ into $t_i$ for all $i$? -----Input----- The first line contains one integer $n$ ($2 \le n \le 10^5$) — the number of magic stones. The second line contains integers $c_1, c_2, \ldots, c_n$ ($0 \le c_i \le 2 \cdot 10^9$) — the charges of Grigory's stones. The second line contains integers $t_1, t_2, \ldots, t_n$ ($0 \le t_i \le 2 \cdot 10^9$) — the charges of Andrew's stones. -----Output----- If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes". Otherwise, print "No". -----Examples----- Input 4 7 2 4 12 7 15 10 12 Output Yes Input 3 4 4 4 1 2 3 Output No -----Note----- In the first example, we can perform the following synchronizations ($1$-indexed): First, synchronize the third stone $[7, 2, \mathbf{4}, 12] \rightarrow [7, 2, \mathbf{10}, 12]$. Then synchronize the second stone: $[7, \mathbf{2}, 10, 12] \rightarrow [7, \mathbf{15}, 10, 12]$. In the second example, any operation with the second stone will not change its charge.
n = int(input()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] if a[0] == b[0] and a[-1] == b[-1]: c = [] d = [] for i in range(1, n): c.append(a[i] - a[i - 1]) d.append(b[i] - b[i - 1]) c.sort() d.sort() if c == d: print("Yes") else: print("No") else: print("No")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Grigory has $n$ magic stones, conveniently numbered from $1$ to $n$. The charge of the $i$-th stone is equal to $c_i$. Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index $i$, where $2 \le i \le n - 1$), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge $c_i$ changes to $c_i' = c_{i + 1} + c_{i - 1} - c_i$. Andrew, Grigory's friend, also has $n$ stones with charges $t_i$. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes $c_i$ into $t_i$ for all $i$? -----Input----- The first line contains one integer $n$ ($2 \le n \le 10^5$) — the number of magic stones. The second line contains integers $c_1, c_2, \ldots, c_n$ ($0 \le c_i \le 2 \cdot 10^9$) — the charges of Grigory's stones. The second line contains integers $t_1, t_2, \ldots, t_n$ ($0 \le t_i \le 2 \cdot 10^9$) — the charges of Andrew's stones. -----Output----- If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes". Otherwise, print "No". -----Examples----- Input 4 7 2 4 12 7 15 10 12 Output Yes Input 3 4 4 4 1 2 3 Output No -----Note----- In the first example, we can perform the following synchronizations ($1$-indexed): First, synchronize the third stone $[7, 2, \mathbf{4}, 12] \rightarrow [7, 2, \mathbf{10}, 12]$. Then synchronize the second stone: $[7, \mathbf{2}, 10, 12] \rightarrow [7, \mathbf{15}, 10, 12]$. In the second example, any operation with the second stone will not change its charge.
n = int(input()) c = list(map(int, input().split())) t = list(map(int, input().split())) c_diffs = [(c[i] - c[i - 1]) for i in range(1, n)] t_diffs = [(t[i] - t[i - 1]) for i in range(1, n)] print( "Yes" if c[0] == t[0] and c[-1] == t[-1] and sorted(c_diffs) == sorted(t_diffs) else "No" )
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING STRING
Grigory has $n$ magic stones, conveniently numbered from $1$ to $n$. The charge of the $i$-th stone is equal to $c_i$. Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index $i$, where $2 \le i \le n - 1$), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge $c_i$ changes to $c_i' = c_{i + 1} + c_{i - 1} - c_i$. Andrew, Grigory's friend, also has $n$ stones with charges $t_i$. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes $c_i$ into $t_i$ for all $i$? -----Input----- The first line contains one integer $n$ ($2 \le n \le 10^5$) — the number of magic stones. The second line contains integers $c_1, c_2, \ldots, c_n$ ($0 \le c_i \le 2 \cdot 10^9$) — the charges of Grigory's stones. The second line contains integers $t_1, t_2, \ldots, t_n$ ($0 \le t_i \le 2 \cdot 10^9$) — the charges of Andrew's stones. -----Output----- If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes". Otherwise, print "No". -----Examples----- Input 4 7 2 4 12 7 15 10 12 Output Yes Input 3 4 4 4 1 2 3 Output No -----Note----- In the first example, we can perform the following synchronizations ($1$-indexed): First, synchronize the third stone $[7, 2, \mathbf{4}, 12] \rightarrow [7, 2, \mathbf{10}, 12]$. Then synchronize the second stone: $[7, \mathbf{2}, 10, 12] \rightarrow [7, \mathbf{15}, 10, 12]$. In the second example, any operation with the second stone will not change its charge.
n = int(input().strip()) x = [int(tmp) for tmp in input().strip().split(" ")] y = [int(tmp) for tmp in input().strip().split(" ")] def solve(x, y): if x[0] != y[0] or x[-1] != y[-1]: return False cha_dict = {} for i in range(n - 1): cha = x[i + 1] - x[i] if cha not in cha_dict: cha_dict[cha] = [] cha_dict[cha].append(i) fu_set = set() zheng_set = set([0]) for i in range(n - 1): cha = y[i + 1] - y[i] is_ok = False if cha not in cha_dict: return False for j in cha_dict[cha]: if j in fu_set or j + 1 in zheng_set: continue is_ok = True if j in zheng_set: zheng_set.remove(j) else: fu_set.add(j) if j + 1 in fu_set: fu_set.remove(j + 1) else: zheng_set.add(j + 1) break if not is_ok: return False return True if solve(x, y): print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR RETURN NUMBER FOR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Grigory has $n$ magic stones, conveniently numbered from $1$ to $n$. The charge of the $i$-th stone is equal to $c_i$. Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index $i$, where $2 \le i \le n - 1$), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge $c_i$ changes to $c_i' = c_{i + 1} + c_{i - 1} - c_i$. Andrew, Grigory's friend, also has $n$ stones with charges $t_i$. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes $c_i$ into $t_i$ for all $i$? -----Input----- The first line contains one integer $n$ ($2 \le n \le 10^5$) — the number of magic stones. The second line contains integers $c_1, c_2, \ldots, c_n$ ($0 \le c_i \le 2 \cdot 10^9$) — the charges of Grigory's stones. The second line contains integers $t_1, t_2, \ldots, t_n$ ($0 \le t_i \le 2 \cdot 10^9$) — the charges of Andrew's stones. -----Output----- If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes". Otherwise, print "No". -----Examples----- Input 4 7 2 4 12 7 15 10 12 Output Yes Input 3 4 4 4 1 2 3 Output No -----Note----- In the first example, we can perform the following synchronizations ($1$-indexed): First, synchronize the third stone $[7, 2, \mathbf{4}, 12] \rightarrow [7, 2, \mathbf{10}, 12]$. Then synchronize the second stone: $[7, \mathbf{2}, 10, 12] \rightarrow [7, \mathbf{15}, 10, 12]$. In the second example, any operation with the second stone will not change its charge.
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) if a[0] != b[0]: print("NO") else: c = sorted(a[i + 1] - a[i] for i in range(n - 1)) d = sorted(b[i + 1] - b[i] for i in range(n - 1)) if c == d: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Grigory has $n$ magic stones, conveniently numbered from $1$ to $n$. The charge of the $i$-th stone is equal to $c_i$. Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index $i$, where $2 \le i \le n - 1$), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge $c_i$ changes to $c_i' = c_{i + 1} + c_{i - 1} - c_i$. Andrew, Grigory's friend, also has $n$ stones with charges $t_i$. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes $c_i$ into $t_i$ for all $i$? -----Input----- The first line contains one integer $n$ ($2 \le n \le 10^5$) — the number of magic stones. The second line contains integers $c_1, c_2, \ldots, c_n$ ($0 \le c_i \le 2 \cdot 10^9$) — the charges of Grigory's stones. The second line contains integers $t_1, t_2, \ldots, t_n$ ($0 \le t_i \le 2 \cdot 10^9$) — the charges of Andrew's stones. -----Output----- If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes". Otherwise, print "No". -----Examples----- Input 4 7 2 4 12 7 15 10 12 Output Yes Input 3 4 4 4 1 2 3 Output No -----Note----- In the first example, we can perform the following synchronizations ($1$-indexed): First, synchronize the third stone $[7, 2, \mathbf{4}, 12] \rightarrow [7, 2, \mathbf{10}, 12]$. Then synchronize the second stone: $[7, \mathbf{2}, 10, 12] \rightarrow [7, \mathbf{15}, 10, 12]$. In the second example, any operation with the second stone will not change its charge.
import sys n = int(input()) c = list(map(int, input().split())) t = list(map(int, input().split())) if c[0] != t[0] or c[n - 1] != t[n - 1]: print("No") sys.exit() if n == 2: print("Yes") sys.exit() g1 = [(c[i + 1] - c[i]) for i in range(n - 1)] g2 = [(t[i + 1] - t[i]) for i in range(n - 1)] if sorted(g1) == sorted(g2): print("Yes") else: print("No")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING