description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given a grid with $n$ rows and $m$ columns, where each cell has a positive integer written on it. Let's call a grid good, if in each row the sequence of numbers is sorted in a non-decreasing order. It means, that for each $1 \le i \le n$ and $2 \le j \le m$ the following holds: $a_{i,j} \ge a_{i, j-1}$. You have to to do the following operation exactly once: choose two columns with indexes $i$ and $j$ (not necessarily different), $1 \le i, j \le m$, and swap them. You are asked to determine whether it is possible to make the grid good after the swap and, if it is, find the columns that need to be swapped. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the number of rows and columns respectively. Each of the next $n$ rows contains $m$ integers, $j$-th element of $i$-th row is $a_{i,j}$ ($1 \le a_{i,j} \le 10^9$) β€” the number written in the $j$-th cell of the $i$-th row. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- If after the swap it is impossible to get a good grid, output $-1$. In the other case output $2$ integers β€” the indices of the columns that should be swapped to get a good grid. If there are multiple solutions, print any. -----Examples----- Input 5 2 3 1 2 3 1 1 1 2 2 4 1 2 3 2 2 2 1 1 1 2 3 6 2 1 5 4 3 2 1 1 2 Output 1 1 -1 1 2 1 3 1 1 -----Note----- In the first test case the grid is initially good, so we can, for example, swap the first column with itself. In the second test case it is impossible to make the grid good. In the third test case it is needed to swap the first and the second column, then the grid becomes good.
for _ in range(int(input())): m, n = map(int, input().split()) s = [] for i in range(m): s.append(list(map(int, input().split()))) h = set() flag = False for i in range(m): row = sorted(s[i]) for j in range(n): if row[j] != s[i][j]: h.add(j + 1) if len(h) > 2: flag = True break if flag: break if flag: print("-1") elif len(h) == 0: print("1 1") else: x, y = min(h) - 1, max(h) - 1 for i in range(m): if s[i][x] < s[i][y]: print(-1) break else: print(x + 1, y + 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
You are given a grid with $n$ rows and $m$ columns, where each cell has a positive integer written on it. Let's call a grid good, if in each row the sequence of numbers is sorted in a non-decreasing order. It means, that for each $1 \le i \le n$ and $2 \le j \le m$ the following holds: $a_{i,j} \ge a_{i, j-1}$. You have to to do the following operation exactly once: choose two columns with indexes $i$ and $j$ (not necessarily different), $1 \le i, j \le m$, and swap them. You are asked to determine whether it is possible to make the grid good after the swap and, if it is, find the columns that need to be swapped. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the number of rows and columns respectively. Each of the next $n$ rows contains $m$ integers, $j$-th element of $i$-th row is $a_{i,j}$ ($1 \le a_{i,j} \le 10^9$) β€” the number written in the $j$-th cell of the $i$-th row. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- If after the swap it is impossible to get a good grid, output $-1$. In the other case output $2$ integers β€” the indices of the columns that should be swapped to get a good grid. If there are multiple solutions, print any. -----Examples----- Input 5 2 3 1 2 3 1 1 1 2 2 4 1 2 3 2 2 2 1 1 1 2 3 6 2 1 5 4 3 2 1 1 2 Output 1 1 -1 1 2 1 3 1 1 -----Note----- In the first test case the grid is initially good, so we can, for example, swap the first column with itself. In the second test case it is impossible to make the grid good. In the third test case it is needed to swap the first and the second column, then the grid becomes good.
t = int(input()) for _ in range(t): n, m = map(int, input().split()) a = [] for i in range(n): l = list(map(int, input().strip().split())) a.append(l) idx = [] for i in range(n): b = a[i] d = sorted(b) for j in range(m): if b[j] != d[j]: idx.append(j) if len(idx) > 0: break if len(idx) == 0: print(1, 1) elif len(idx) == 2: for i in range(n): a[i][idx[0]], a[i][idx[1]] = a[i][idx[1]], a[i][idx[0]] f = 0 for i in range(n): b1 = a[i] d1 = sorted(b1) if b1 != d1: f = 1 break if f == 0: print(idx[0] + 1, idx[1] + 1) else: print(-1) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
You are given a grid with $n$ rows and $m$ columns, where each cell has a positive integer written on it. Let's call a grid good, if in each row the sequence of numbers is sorted in a non-decreasing order. It means, that for each $1 \le i \le n$ and $2 \le j \le m$ the following holds: $a_{i,j} \ge a_{i, j-1}$. You have to to do the following operation exactly once: choose two columns with indexes $i$ and $j$ (not necessarily different), $1 \le i, j \le m$, and swap them. You are asked to determine whether it is possible to make the grid good after the swap and, if it is, find the columns that need to be swapped. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the number of rows and columns respectively. Each of the next $n$ rows contains $m$ integers, $j$-th element of $i$-th row is $a_{i,j}$ ($1 \le a_{i,j} \le 10^9$) β€” the number written in the $j$-th cell of the $i$-th row. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- If after the swap it is impossible to get a good grid, output $-1$. In the other case output $2$ integers β€” the indices of the columns that should be swapped to get a good grid. If there are multiple solutions, print any. -----Examples----- Input 5 2 3 1 2 3 1 1 1 2 2 4 1 2 3 2 2 2 1 1 1 2 3 6 2 1 5 4 3 2 1 1 2 Output 1 1 -1 1 2 1 3 1 1 -----Note----- In the first test case the grid is initially good, so we can, for example, swap the first column with itself. In the second test case it is impossible to make the grid good. In the third test case it is needed to swap the first and the second column, then the grid becomes good.
t = int(input()) for _ in range(t): n, m = map(int, input().split()) a = [] for _ in range(n): a.append(list(map(int, input().split()))) sorteda = [] for i in a: sorteda.append(sorted(i)) unsortindex = set() ls = [] for i in range(n): l = 0 for j in range(m): if a[i][j] != sorteda[i][j]: unsortindex.add(j) l += 1 ls.append(l) unsortindex = list(unsortindex) if len(unsortindex) == 0: print(1, 1) continue elif len(unsortindex) > 2: print(-1) continue else: f = 0 for i in range(n): if ls[i] != 2 and a[i][unsortindex[0]] != a[i][unsortindex[1]]: print(-1) f = -1 break if f == 0: print(unsortindex[0] + 1, unsortindex[1] + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER
You are given a grid with $n$ rows and $m$ columns, where each cell has a positive integer written on it. Let's call a grid good, if in each row the sequence of numbers is sorted in a non-decreasing order. It means, that for each $1 \le i \le n$ and $2 \le j \le m$ the following holds: $a_{i,j} \ge a_{i, j-1}$. You have to to do the following operation exactly once: choose two columns with indexes $i$ and $j$ (not necessarily different), $1 \le i, j \le m$, and swap them. You are asked to determine whether it is possible to make the grid good after the swap and, if it is, find the columns that need to be swapped. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the number of rows and columns respectively. Each of the next $n$ rows contains $m$ integers, $j$-th element of $i$-th row is $a_{i,j}$ ($1 \le a_{i,j} \le 10^9$) β€” the number written in the $j$-th cell of the $i$-th row. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- If after the swap it is impossible to get a good grid, output $-1$. In the other case output $2$ integers β€” the indices of the columns that should be swapped to get a good grid. If there are multiple solutions, print any. -----Examples----- Input 5 2 3 1 2 3 1 1 1 2 2 4 1 2 3 2 2 2 1 1 1 2 3 6 2 1 5 4 3 2 1 1 2 Output 1 1 -1 1 2 1 3 1 1 -----Note----- In the first test case the grid is initially good, so we can, for example, swap the first column with itself. In the second test case it is impossible to make the grid good. In the third test case it is needed to swap the first and the second column, then the grid becomes good.
for _ in range(int(input())): n, m = map(int, input().split()) matrix = [] for _ in range(n): matrix.append(list(map(int, input().split()))) sorted_matrix = [] for row in matrix: sorted_matrix.append(sorted(row)) total_incorrect = 0 wrongs = [] for i in range(n): for j in range(m): if matrix[i][j] != sorted_matrix[i][j]: wrongs.append(j) wrongs = list(set(wrongs)) if len(wrongs) > 2: print(-1) continue if wrongs == []: print(1, 1) else: can = True for i, item in enumerate(matrix): item[wrongs[0]], item[wrongs[1]] = item[wrongs[1]], item[wrongs[0]] for i, item in enumerate(matrix): if item != sorted(item): can = False if can: print(wrongs[0] + 1, wrongs[1] + 1) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER
You are given a grid with $n$ rows and $m$ columns, where each cell has a positive integer written on it. Let's call a grid good, if in each row the sequence of numbers is sorted in a non-decreasing order. It means, that for each $1 \le i \le n$ and $2 \le j \le m$ the following holds: $a_{i,j} \ge a_{i, j-1}$. You have to to do the following operation exactly once: choose two columns with indexes $i$ and $j$ (not necessarily different), $1 \le i, j \le m$, and swap them. You are asked to determine whether it is possible to make the grid good after the swap and, if it is, find the columns that need to be swapped. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the number of rows and columns respectively. Each of the next $n$ rows contains $m$ integers, $j$-th element of $i$-th row is $a_{i,j}$ ($1 \le a_{i,j} \le 10^9$) β€” the number written in the $j$-th cell of the $i$-th row. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- If after the swap it is impossible to get a good grid, output $-1$. In the other case output $2$ integers β€” the indices of the columns that should be swapped to get a good grid. If there are multiple solutions, print any. -----Examples----- Input 5 2 3 1 2 3 1 1 1 2 2 4 1 2 3 2 2 2 1 1 1 2 3 6 2 1 5 4 3 2 1 1 2 Output 1 1 -1 1 2 1 3 1 1 -----Note----- In the first test case the grid is initially good, so we can, for example, swap the first column with itself. In the second test case it is impossible to make the grid good. In the third test case it is needed to swap the first and the second column, then the grid becomes good.
for _ in range(int(input())): n, m = map(int, input().split()) mat = [[int(x) for x in input().split()] for i in range(n)] res = set() for i in range(n): b = sorted(mat[i]) t = [] for j in range(m): if b[j] != mat[i][j]: t.append(j + 1) if t: res.add(tuple(t)) if len(res) > 1: print(-1) elif not res: print(1, 1) else: t = res.pop() if len(t) == 2: l, r = t for i in range(n): mat[i][l - 1], mat[i][r - 1] = mat[i][r - 1], mat[i][l - 1] flag = True for i in range(n): if not flag: break for j in range(1, m): if mat[i][j] < mat[i][j - 1]: flag = False break if flag: print(l, r) continue print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given a grid with $n$ rows and $m$ columns, where each cell has a positive integer written on it. Let's call a grid good, if in each row the sequence of numbers is sorted in a non-decreasing order. It means, that for each $1 \le i \le n$ and $2 \le j \le m$ the following holds: $a_{i,j} \ge a_{i, j-1}$. You have to to do the following operation exactly once: choose two columns with indexes $i$ and $j$ (not necessarily different), $1 \le i, j \le m$, and swap them. You are asked to determine whether it is possible to make the grid good after the swap and, if it is, find the columns that need to be swapped. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the number of rows and columns respectively. Each of the next $n$ rows contains $m$ integers, $j$-th element of $i$-th row is $a_{i,j}$ ($1 \le a_{i,j} \le 10^9$) β€” the number written in the $j$-th cell of the $i$-th row. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- If after the swap it is impossible to get a good grid, output $-1$. In the other case output $2$ integers β€” the indices of the columns that should be swapped to get a good grid. If there are multiple solutions, print any. -----Examples----- Input 5 2 3 1 2 3 1 1 1 2 2 4 1 2 3 2 2 2 1 1 1 2 3 6 2 1 5 4 3 2 1 1 2 Output 1 1 -1 1 2 1 3 1 1 -----Note----- In the first test case the grid is initially good, so we can, for example, swap the first column with itself. In the second test case it is impossible to make the grid good. In the third test case it is needed to swap the first and the second column, then the grid becomes good.
def is_sorted(l): return all(l[i] <= l[i + 1] for i in range(len(l) - 1)) t = int(input()) for c in range(t): n, m = input().split() n, m = int(n), int(m) mat = [] for i in range(n): mat.append([int(x) for x in input().split()]) first, last = 0, 0 for k in range(n): for i in range(m - 1): if mat[k][i] > mat[k][i + 1]: first = i while first > 0 and mat[k][first] == mat[k][first - 1]: first -= 1 break for i in range(m - 1, 0, -1): if mat[k][i] < mat[k][i - 1]: last = i while last < m - 1 and mat[k][last] == mat[k][last + 1]: last += 1 break if first != last: break fail = False for i in range(n): mat[i][first], mat[i][last] = mat[i][last], mat[i][first] if not is_sorted(mat[i]): fail = True break print(-1 if fail else "%d %d" % (first + 1, last + 1))
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER
You are given a grid with $n$ rows and $m$ columns, where each cell has a positive integer written on it. Let's call a grid good, if in each row the sequence of numbers is sorted in a non-decreasing order. It means, that for each $1 \le i \le n$ and $2 \le j \le m$ the following holds: $a_{i,j} \ge a_{i, j-1}$. You have to to do the following operation exactly once: choose two columns with indexes $i$ and $j$ (not necessarily different), $1 \le i, j \le m$, and swap them. You are asked to determine whether it is possible to make the grid good after the swap and, if it is, find the columns that need to be swapped. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the number of rows and columns respectively. Each of the next $n$ rows contains $m$ integers, $j$-th element of $i$-th row is $a_{i,j}$ ($1 \le a_{i,j} \le 10^9$) β€” the number written in the $j$-th cell of the $i$-th row. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- If after the swap it is impossible to get a good grid, output $-1$. In the other case output $2$ integers β€” the indices of the columns that should be swapped to get a good grid. If there are multiple solutions, print any. -----Examples----- Input 5 2 3 1 2 3 1 1 1 2 2 4 1 2 3 2 2 2 1 1 1 2 3 6 2 1 5 4 3 2 1 1 2 Output 1 1 -1 1 2 1 3 1 1 -----Note----- In the first test case the grid is initially good, so we can, for example, swap the first column with itself. In the second test case it is impossible to make the grid good. In the third test case it is needed to swap the first and the second column, then the grid becomes good.
n = int(input()) def swap(arr, i, j): tmp = arr[i] arr[i] = arr[j] arr[j] = tmp for i in range(n): r, c = map(int, input().split()) matrix = [] result = 0 for j in range(r): row = list(map(int, input().split())) matrix.append(row) swap_i = -1 swap_j = -1 for k in range(r): row = matrix[k] sorted_row = row.copy() sorted_row.sort() for m in range(c): if row[m] != sorted_row[m]: if swap_i == -1: swap_i = m else: swap_j = m break if swap_i != -1: break if swap_i == -1: print(1, 1) continue for k in range(r): row = matrix[k] swap(row, swap_i, swap_j) sorted_row = row.copy() sorted_row.sort() for m in range(c): if row[m] != sorted_row[m]: result = -1 break if result == -1: break if result == -1: print(-1) else: print(swap_i + 1, swap_j + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
You are given a grid with $n$ rows and $m$ columns, where each cell has a positive integer written on it. Let's call a grid good, if in each row the sequence of numbers is sorted in a non-decreasing order. It means, that for each $1 \le i \le n$ and $2 \le j \le m$ the following holds: $a_{i,j} \ge a_{i, j-1}$. You have to to do the following operation exactly once: choose two columns with indexes $i$ and $j$ (not necessarily different), $1 \le i, j \le m$, and swap them. You are asked to determine whether it is possible to make the grid good after the swap and, if it is, find the columns that need to be swapped. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the number of rows and columns respectively. Each of the next $n$ rows contains $m$ integers, $j$-th element of $i$-th row is $a_{i,j}$ ($1 \le a_{i,j} \le 10^9$) β€” the number written in the $j$-th cell of the $i$-th row. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- If after the swap it is impossible to get a good grid, output $-1$. In the other case output $2$ integers β€” the indices of the columns that should be swapped to get a good grid. If there are multiple solutions, print any. -----Examples----- Input 5 2 3 1 2 3 1 1 1 2 2 4 1 2 3 2 2 2 1 1 1 2 3 6 2 1 5 4 3 2 1 1 2 Output 1 1 -1 1 2 1 3 1 1 -----Note----- In the first test case the grid is initially good, so we can, for example, swap the first column with itself. In the second test case it is impossible to make the grid good. In the third test case it is needed to swap the first and the second column, then the grid becomes good.
t = int(input()) while t: n, m = map(int, input().split()) l = [] flag = 1 key = -1 for i in range(n): ll = list(map(int, input().split())) l.append(ll) for i in range(n): lo = sorted(l[i]) if lo != l[i]: key = i break c = 0 lt = sorted(l[key]) li = [] for i in range(m): if l[key][i] != lt[i]: c += 1 li.append(i) if c > 2: flag = 0 break if key == -1: li.append(0) li.append(0) elif flag: for i in range(n): l[i][li[0]], l[i][li[-1]] = l[i][li[-1]], l[i][li[0]] lp = sorted(l[i]) if lp != l[i]: flag = 0 break if flag: print(li[0] + 1, li[-1] + 1) else: print(-1) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER
You are given a grid with $n$ rows and $m$ columns, where each cell has a positive integer written on it. Let's call a grid good, if in each row the sequence of numbers is sorted in a non-decreasing order. It means, that for each $1 \le i \le n$ and $2 \le j \le m$ the following holds: $a_{i,j} \ge a_{i, j-1}$. You have to to do the following operation exactly once: choose two columns with indexes $i$ and $j$ (not necessarily different), $1 \le i, j \le m$, and swap them. You are asked to determine whether it is possible to make the grid good after the swap and, if it is, find the columns that need to be swapped. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the number of rows and columns respectively. Each of the next $n$ rows contains $m$ integers, $j$-th element of $i$-th row is $a_{i,j}$ ($1 \le a_{i,j} \le 10^9$) β€” the number written in the $j$-th cell of the $i$-th row. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- If after the swap it is impossible to get a good grid, output $-1$. In the other case output $2$ integers β€” the indices of the columns that should be swapped to get a good grid. If there are multiple solutions, print any. -----Examples----- Input 5 2 3 1 2 3 1 1 1 2 2 4 1 2 3 2 2 2 1 1 1 2 3 6 2 1 5 4 3 2 1 1 2 Output 1 1 -1 1 2 1 3 1 1 -----Note----- In the first test case the grid is initially good, so we can, for example, swap the first column with itself. In the second test case it is impossible to make the grid good. In the third test case it is needed to swap the first and the second column, then the grid becomes good.
for _ in range(int(input())): n, m = map(int, input().split()) l = [] for i in range(n): l.append([int(i) for i in input().split()]) ans = [] f = 0 for i in l: if i != sorted(i): ans.append(i) if len(ans) == 2: break ind = [] for i in range(len(ans)): one = ans[i] two = sorted(one) ind = [] for i in range(len(one)): if one[i] != two[i]: ind.append(i) if len(ind) == 2: for i in range(len(l)): l[i][ind[0]], l[i][ind[1]] = l[i][ind[1]], l[i][ind[0]] for i in l: if i != sorted(i): f = 1 if f == 1: print("-1") else: try: print(ind[0] + 1, 1 + ind[1]) except: print(1, 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER
You are given a grid with $n$ rows and $m$ columns, where each cell has a positive integer written on it. Let's call a grid good, if in each row the sequence of numbers is sorted in a non-decreasing order. It means, that for each $1 \le i \le n$ and $2 \le j \le m$ the following holds: $a_{i,j} \ge a_{i, j-1}$. You have to to do the following operation exactly once: choose two columns with indexes $i$ and $j$ (not necessarily different), $1 \le i, j \le m$, and swap them. You are asked to determine whether it is possible to make the grid good after the swap and, if it is, find the columns that need to be swapped. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the number of rows and columns respectively. Each of the next $n$ rows contains $m$ integers, $j$-th element of $i$-th row is $a_{i,j}$ ($1 \le a_{i,j} \le 10^9$) β€” the number written in the $j$-th cell of the $i$-th row. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- If after the swap it is impossible to get a good grid, output $-1$. In the other case output $2$ integers β€” the indices of the columns that should be swapped to get a good grid. If there are multiple solutions, print any. -----Examples----- Input 5 2 3 1 2 3 1 1 1 2 2 4 1 2 3 2 2 2 1 1 1 2 3 6 2 1 5 4 3 2 1 1 2 Output 1 1 -1 1 2 1 3 1 1 -----Note----- In the first test case the grid is initially good, so we can, for example, swap the first column with itself. In the second test case it is impossible to make the grid good. In the third test case it is needed to swap the first and the second column, then the grid becomes good.
for _ in range(int(input())): n, m = map(int, input().split()) mat = [] arr = [] for i in range(n): temp = [int(x) for x in input().split()] mat.append(temp) arr.append(sorted(temp)) diff = [] same = True for i in range(n): count = 0 cols = [] for j in range(m): if mat[i][j] != arr[i][j]: count += 1 cols.append(j) if count > 2: same = False break elif cols: diff.append(tuple(cols)) if same: st = set(diff) if len(st) == 0: print(1, 1) elif len(st) == 1: a, b = diff[0][0], diff[0][1] hehe = False for i in range(n): if mat[i][b] > mat[i][a]: hehe = True break if hehe: print(-1) else: print(a + 1, b + 1) else: print(-1) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
You are given a grid with $n$ rows and $m$ columns, where each cell has a positive integer written on it. Let's call a grid good, if in each row the sequence of numbers is sorted in a non-decreasing order. It means, that for each $1 \le i \le n$ and $2 \le j \le m$ the following holds: $a_{i,j} \ge a_{i, j-1}$. You have to to do the following operation exactly once: choose two columns with indexes $i$ and $j$ (not necessarily different), $1 \le i, j \le m$, and swap them. You are asked to determine whether it is possible to make the grid good after the swap and, if it is, find the columns that need to be swapped. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the number of rows and columns respectively. Each of the next $n$ rows contains $m$ integers, $j$-th element of $i$-th row is $a_{i,j}$ ($1 \le a_{i,j} \le 10^9$) β€” the number written in the $j$-th cell of the $i$-th row. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- If after the swap it is impossible to get a good grid, output $-1$. In the other case output $2$ integers β€” the indices of the columns that should be swapped to get a good grid. If there are multiple solutions, print any. -----Examples----- Input 5 2 3 1 2 3 1 1 1 2 2 4 1 2 3 2 2 2 1 1 1 2 3 6 2 1 5 4 3 2 1 1 2 Output 1 1 -1 1 2 1 3 1 1 -----Note----- In the first test case the grid is initially good, so we can, for example, swap the first column with itself. In the second test case it is impossible to make the grid good. In the third test case it is needed to swap the first and the second column, then the grid becomes good.
def check(list): ans = True for i in range(len(list) - 1): if list[i] > list[i + 1]: ans = False break return ans def index(list): temp = sorted(list) val = [] for i in range(len(list)): if list[i] != temp[i]: val.append(i) return val t = int(input()) for q in range(t): n, m = [int(x) for x in input().split()] grid = [] for c in range(n): row = [int(x) for x in input().split()] grid.append(row) nature, corr = True, -1 for d in range(n): if not check(grid[d]): nature = False corr = d break if nature: print(1, 1) elif len(index(grid[corr])) != 2: print(-1) else: o = index(grid[corr])[0] p = index(grid[corr])[1] ultimate = True for j in range(n): grid[j][o], grid[j][p] = grid[j][p], grid[j][o] if not check(grid[j]): ultimate = False break if ultimate: print(o + 1, p + 1) else: print(-1)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER
You are given a grid with $n$ rows and $m$ columns, where each cell has a positive integer written on it. Let's call a grid good, if in each row the sequence of numbers is sorted in a non-decreasing order. It means, that for each $1 \le i \le n$ and $2 \le j \le m$ the following holds: $a_{i,j} \ge a_{i, j-1}$. You have to to do the following operation exactly once: choose two columns with indexes $i$ and $j$ (not necessarily different), $1 \le i, j \le m$, and swap them. You are asked to determine whether it is possible to make the grid good after the swap and, if it is, find the columns that need to be swapped. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the number of rows and columns respectively. Each of the next $n$ rows contains $m$ integers, $j$-th element of $i$-th row is $a_{i,j}$ ($1 \le a_{i,j} \le 10^9$) β€” the number written in the $j$-th cell of the $i$-th row. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- If after the swap it is impossible to get a good grid, output $-1$. In the other case output $2$ integers β€” the indices of the columns that should be swapped to get a good grid. If there are multiple solutions, print any. -----Examples----- Input 5 2 3 1 2 3 1 1 1 2 2 4 1 2 3 2 2 2 1 1 1 2 3 6 2 1 5 4 3 2 1 1 2 Output 1 1 -1 1 2 1 3 1 1 -----Note----- In the first test case the grid is initially good, so we can, for example, swap the first column with itself. In the second test case it is impossible to make the grid good. In the third test case it is needed to swap the first and the second column, then the grid becomes good.
for _ in range(int(input())): n, m = map(int, input().split()) L, R = 0, 0 b = 0 ll = [] for i in range(n): l = list(map(int, input().split())) ll.append(l.copy()) if b: continue sl = sorted(l) l[L], l[R] = l[R], l[L] for j in range(m): if l[j] != sl[j]: if L != R: b = 1 R = m - l[::-1].index(sl[j]) - 1 L = j l[L], l[R] = l[R], l[L] for i in range(n): l = ll[i] if b: continue sl = sorted(l) l[L], l[R] = l[R], l[L] for j in range(m): if l[j] != sl[j]: if L != R: b = 1 if b: print(-1) else: print(L + 1, R + 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
Monocarp plays "Rage of Empires II: Definitive Edition" β€” a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall. The wall consists of $n$ sections, aligned in a row. The $i$-th section initially has durability $a_i$. If durability of some section becomes $0$ or less, this section is considered broken. To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager β€” a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals $2$ damage to the target section and $1$ damage to adjacent sections. In other words, if the onager shoots at the section $x$, then the durability of the section $x$ decreases by $2$, and the durability of the sections $x - 1$ and $x + 1$ (if they exist) decreases by $1$ each. Monocarp can shoot at any sections any number of times, he can even shoot at broken sections. Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him! -----Input----- The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of sections. The second line contains the sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the initial durability of the $i$-th section. -----Output----- Print one integer β€” the minimum number of onager shots needed to break at least two sections of the wall. -----Examples----- Input 5 20 10 30 10 20 Output 10 Input 3 1 8 1 Output 1 Input 6 7 6 6 8 5 8 Output 4 Input 6 14 3 8 10 15 4 Output 4 Input 4 1 100 100 1 Output 2 Input 3 40 10 10 Output 7 -----Note----- In the first example, it is possible to break the $2$-nd and the $4$-th section in $10$ shots, for example, by shooting the third section $10$ times. After that, the durabilities become $[20, 0, 10, 0, 20]$. Another way of doing it is firing $5$ shots at the $2$-nd section, and another $5$ shots at the $4$-th section. After that, the durabilities become $[15, 0, 20, 0, 15]$. In the second example, it is enough to shoot the $2$-nd section once. Then the $1$-st and the $3$-rd section will be broken. In the third example, it is enough to shoot the $2$-nd section twice (then the durabilities become $[5, 2, 4, 8, 5, 8]$), and then shoot the $3$-rd section twice (then the durabilities become $[5, 0, 0, 6, 5, 8]$). So, four shots are enough to break the $2$-nd and the $3$-rd section.
def ff(a, b): mx = max(a, b) mi = min(a, b) ans = 0 if mi < mx // 2: ans = mx // 2 + mx % 2 else: a = (2 * mx - mi) // 3 ans = a mx -= 2 * a mi -= a if mi > 0: ans += mi // 2 + mi % 2 mx -= mi // 2 + mi % 2 if mx > 0: ans += mx // 2 + mx % 2 return ans def f(a, b, c): ans1 = min(a, c) mi = max(a, c) - min(a, c) ans1 += mi // 2 + mi % 2 ans2 = ff(a, b) ans3 = ff(b, c) ans = min(ans1, ans2, ans3) return ans n = int(input()) arr = list(map(int, input().split())) ans = max(arr) for i in range(1, n - 1): tm = f(arr[i - 1], arr[i], arr[i + 1]) ans = min(tm, ans) arr.sort() tm = arr[0] // 2 + arr[0] % 2 + arr[1] // 2 + arr[1] % 2 ans = min(tm, ans) if len(arr) == 2: ans = min(ans, ff(arr[0], arr[1])) print(ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR 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 VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Monocarp plays "Rage of Empires II: Definitive Edition" β€” a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall. The wall consists of $n$ sections, aligned in a row. The $i$-th section initially has durability $a_i$. If durability of some section becomes $0$ or less, this section is considered broken. To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager β€” a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals $2$ damage to the target section and $1$ damage to adjacent sections. In other words, if the onager shoots at the section $x$, then the durability of the section $x$ decreases by $2$, and the durability of the sections $x - 1$ and $x + 1$ (if they exist) decreases by $1$ each. Monocarp can shoot at any sections any number of times, he can even shoot at broken sections. Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him! -----Input----- The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of sections. The second line contains the sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the initial durability of the $i$-th section. -----Output----- Print one integer β€” the minimum number of onager shots needed to break at least two sections of the wall. -----Examples----- Input 5 20 10 30 10 20 Output 10 Input 3 1 8 1 Output 1 Input 6 7 6 6 8 5 8 Output 4 Input 6 14 3 8 10 15 4 Output 4 Input 4 1 100 100 1 Output 2 Input 3 40 10 10 Output 7 -----Note----- In the first example, it is possible to break the $2$-nd and the $4$-th section in $10$ shots, for example, by shooting the third section $10$ times. After that, the durabilities become $[20, 0, 10, 0, 20]$. Another way of doing it is firing $5$ shots at the $2$-nd section, and another $5$ shots at the $4$-th section. After that, the durabilities become $[15, 0, 20, 0, 15]$. In the second example, it is enough to shoot the $2$-nd section once. Then the $1$-st and the $3$-rd section will be broken. In the third example, it is enough to shoot the $2$-nd section twice (then the durabilities become $[5, 2, 4, 8, 5, 8]$), and then shoot the $3$-rd section twice (then the durabilities become $[5, 0, 0, 6, 5, 8]$). So, four shots are enough to break the $2$-nd and the $3$-rd section.
n = int(input()) li = [int(i) for i in input().split()] ans = 10000000000 for i in range(n - 1): cur = 0 x = li[i] y = li[i + 1] if x < y: x = li[i + 1] y = li[i] cnt = min(x - y, (x + 1) // 2) cur += cnt x -= 2 * cnt y -= cnt if x > 0 and y > 0: cur += (x + y + 2) // 3 ans = min(ans, cur) for i in range(n - 2): x = li[i] y = li[i + 2] if x < y: x = li[i + 2] y = li[i] cnt = (x - y + 1) // 2 ans = min(ans, cnt + y) li.sort() ans = min(ans, (li[0] + 1) // 2 + (li[1] + 1) // 2) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Monocarp plays "Rage of Empires II: Definitive Edition" β€” a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall. The wall consists of $n$ sections, aligned in a row. The $i$-th section initially has durability $a_i$. If durability of some section becomes $0$ or less, this section is considered broken. To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager β€” a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals $2$ damage to the target section and $1$ damage to adjacent sections. In other words, if the onager shoots at the section $x$, then the durability of the section $x$ decreases by $2$, and the durability of the sections $x - 1$ and $x + 1$ (if they exist) decreases by $1$ each. Monocarp can shoot at any sections any number of times, he can even shoot at broken sections. Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him! -----Input----- The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of sections. The second line contains the sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the initial durability of the $i$-th section. -----Output----- Print one integer β€” the minimum number of onager shots needed to break at least two sections of the wall. -----Examples----- Input 5 20 10 30 10 20 Output 10 Input 3 1 8 1 Output 1 Input 6 7 6 6 8 5 8 Output 4 Input 6 14 3 8 10 15 4 Output 4 Input 4 1 100 100 1 Output 2 Input 3 40 10 10 Output 7 -----Note----- In the first example, it is possible to break the $2$-nd and the $4$-th section in $10$ shots, for example, by shooting the third section $10$ times. After that, the durabilities become $[20, 0, 10, 0, 20]$. Another way of doing it is firing $5$ shots at the $2$-nd section, and another $5$ shots at the $4$-th section. After that, the durabilities become $[15, 0, 20, 0, 15]$. In the second example, it is enough to shoot the $2$-nd section once. Then the $1$-st and the $3$-rd section will be broken. In the third example, it is enough to shoot the $2$-nd section twice (then the durabilities become $[5, 2, 4, 8, 5, 8]$), and then shoot the $3$-rd section twice (then the durabilities become $[5, 0, 0, 6, 5, 8]$). So, four shots are enough to break the $2$-nd and the $3$-rd section.
n = int(input()) A = list(map(int, input().split())) S = sorted(A) ANS = (S[0] + 1) // 2 + (S[1] + 1) // 2 for i in range(n - 2): a = A[i] b = A[i + 2] if a % 2 == 1 and b % 2 == 1: ANS = min(ANS, a // 2 + b // 2 + 1) for i in range(n - 1): a = A[i] b = A[i + 1] if a > b: a, b = b, a if b >= 2 * a: ANS = min(ANS, (b + 1) // 2) else: k = b - a a -= k b -= 2 * k rest = a % 3 ANS = min(ANS, k + a // 3 * 2 + rest) print(ANS)
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 VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
Monocarp plays "Rage of Empires II: Definitive Edition" β€” a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall. The wall consists of $n$ sections, aligned in a row. The $i$-th section initially has durability $a_i$. If durability of some section becomes $0$ or less, this section is considered broken. To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager β€” a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals $2$ damage to the target section and $1$ damage to adjacent sections. In other words, if the onager shoots at the section $x$, then the durability of the section $x$ decreases by $2$, and the durability of the sections $x - 1$ and $x + 1$ (if they exist) decreases by $1$ each. Monocarp can shoot at any sections any number of times, he can even shoot at broken sections. Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him! -----Input----- The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of sections. The second line contains the sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the initial durability of the $i$-th section. -----Output----- Print one integer β€” the minimum number of onager shots needed to break at least two sections of the wall. -----Examples----- Input 5 20 10 30 10 20 Output 10 Input 3 1 8 1 Output 1 Input 6 7 6 6 8 5 8 Output 4 Input 6 14 3 8 10 15 4 Output 4 Input 4 1 100 100 1 Output 2 Input 3 40 10 10 Output 7 -----Note----- In the first example, it is possible to break the $2$-nd and the $4$-th section in $10$ shots, for example, by shooting the third section $10$ times. After that, the durabilities become $[20, 0, 10, 0, 20]$. Another way of doing it is firing $5$ shots at the $2$-nd section, and another $5$ shots at the $4$-th section. After that, the durabilities become $[15, 0, 20, 0, 15]$. In the second example, it is enough to shoot the $2$-nd section once. Then the $1$-st and the $3$-rd section will be broken. In the third example, it is enough to shoot the $2$-nd section twice (then the durabilities become $[5, 2, 4, 8, 5, 8]$), and then shoot the $3$-rd section twice (then the durabilities become $[5, 0, 0, 6, 5, 8]$). So, four shots are enough to break the $2$-nd and the $3$-rd section.
n = int(input()) a = list(map(int, input().split())) b = a[:] b.sort() ans = (b[0] + 1) // 2 + (b[1] + 1) // 2 for i in range(n - 1): q = max(a[i], a[i + 1]) w = min(a[i], a[i + 1]) if q > 2 * w: ans = min(ans, (q + 1) // 2) else: ans = min(ans, (q + w + 2) // 3) a = [99999999] + a + [99999999] for i in range(1, n + 1): temp = a[i - 1 : i + 2] att = min(temp[0], temp[2], (temp[1] + 1) // 2) temp[0] -= att temp[1] -= att * 2 temp[2] -= att temp.sort() ans = min(att + (temp[1] + 1) // 2, ans) print(ans)
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 EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
Monocarp plays "Rage of Empires II: Definitive Edition" β€” a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall. The wall consists of $n$ sections, aligned in a row. The $i$-th section initially has durability $a_i$. If durability of some section becomes $0$ or less, this section is considered broken. To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager β€” a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals $2$ damage to the target section and $1$ damage to adjacent sections. In other words, if the onager shoots at the section $x$, then the durability of the section $x$ decreases by $2$, and the durability of the sections $x - 1$ and $x + 1$ (if they exist) decreases by $1$ each. Monocarp can shoot at any sections any number of times, he can even shoot at broken sections. Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him! -----Input----- The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of sections. The second line contains the sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the initial durability of the $i$-th section. -----Output----- Print one integer β€” the minimum number of onager shots needed to break at least two sections of the wall. -----Examples----- Input 5 20 10 30 10 20 Output 10 Input 3 1 8 1 Output 1 Input 6 7 6 6 8 5 8 Output 4 Input 6 14 3 8 10 15 4 Output 4 Input 4 1 100 100 1 Output 2 Input 3 40 10 10 Output 7 -----Note----- In the first example, it is possible to break the $2$-nd and the $4$-th section in $10$ shots, for example, by shooting the third section $10$ times. After that, the durabilities become $[20, 0, 10, 0, 20]$. Another way of doing it is firing $5$ shots at the $2$-nd section, and another $5$ shots at the $4$-th section. After that, the durabilities become $[15, 0, 20, 0, 15]$. In the second example, it is enough to shoot the $2$-nd section once. Then the $1$-st and the $3$-rd section will be broken. In the third example, it is enough to shoot the $2$-nd section twice (then the durabilities become $[5, 2, 4, 8, 5, 8]$), and then shoot the $3$-rd section twice (then the durabilities become $[5, 0, 0, 6, 5, 8]$). So, four shots are enough to break the $2$-nd and the $3$-rd section.
import sys input = sys.stdin.readline def solve(): N = int(input()) INF = 10**20 a = [INF] + list(map(int, input().split())) a.append(INF) best = INF N = len(a) for i in range(1, N - 1): best = min(best, max(a[i - 1], a[i + 1])) best = min(best, max(min(a[i - 1], a[i + 1]), (a[i] + 1) // 2)) x = a[i - 1] y = a[i + 1] if x > y: x, y = y, x shots = x + (y - x + 1) // 2 y -= x best = min(shots, best) for i in range(N - 1): x, y = a[i], a[i + 1] if x > y: x, y = y, x delta = y - x shots = min(delta, x) y -= 2 * shots x -= shots if y == x: shots += (y + x + 2) // 3 best = min(shots, best) else: assert x == 0 shots += (y + 1) // 2 best = min(shots, best) a.sort() best = min(best, (a[0] + 1) // 2 + (a[1] + 1) // 2) print(best) solve()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Monocarp plays "Rage of Empires II: Definitive Edition" β€” a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall. The wall consists of $n$ sections, aligned in a row. The $i$-th section initially has durability $a_i$. If durability of some section becomes $0$ or less, this section is considered broken. To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager β€” a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals $2$ damage to the target section and $1$ damage to adjacent sections. In other words, if the onager shoots at the section $x$, then the durability of the section $x$ decreases by $2$, and the durability of the sections $x - 1$ and $x + 1$ (if they exist) decreases by $1$ each. Monocarp can shoot at any sections any number of times, he can even shoot at broken sections. Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him! -----Input----- The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of sections. The second line contains the sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the initial durability of the $i$-th section. -----Output----- Print one integer β€” the minimum number of onager shots needed to break at least two sections of the wall. -----Examples----- Input 5 20 10 30 10 20 Output 10 Input 3 1 8 1 Output 1 Input 6 7 6 6 8 5 8 Output 4 Input 6 14 3 8 10 15 4 Output 4 Input 4 1 100 100 1 Output 2 Input 3 40 10 10 Output 7 -----Note----- In the first example, it is possible to break the $2$-nd and the $4$-th section in $10$ shots, for example, by shooting the third section $10$ times. After that, the durabilities become $[20, 0, 10, 0, 20]$. Another way of doing it is firing $5$ shots at the $2$-nd section, and another $5$ shots at the $4$-th section. After that, the durabilities become $[15, 0, 20, 0, 15]$. In the second example, it is enough to shoot the $2$-nd section once. Then the $1$-st and the $3$-rd section will be broken. In the third example, it is enough to shoot the $2$-nd section twice (then the durabilities become $[5, 2, 4, 8, 5, 8]$), and then shoot the $3$-rd section twice (then the durabilities become $[5, 0, 0, 6, 5, 8]$). So, four shots are enough to break the $2$-nd and the $3$-rd section.
import sys def solve(): inp = sys.stdin.readline n = int(inp()) a = list(map(int, inp().split())) ans = int(20000000.0) for i in range(2, n): b = a[i - 2] c = a[i] b, c = min(b, c), max(b, c) ans = min(ans, b + (c - b + 1) // 2) for i in range(1, n): b = a[i - 1] c = a[i] b, c = min(b, c), max(b, c) z = (b + c + 2) // 3 y = max(c - z, 0) ans = min(ans, y + max(c - y * 2, (b - y + 1) // 2, 0)) ans = min(ans, (c + 1) // 2 + max(b - (c + 1) // 2 + 1, 0) // 2) a.sort() ans = min(ans, (a[0] + 1) // 2 + (a[1] + 1) // 2) print(ans) def main(): solve() main()
IMPORT FUNC_DEF ASSIGN 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 NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Monocarp plays "Rage of Empires II: Definitive Edition" β€” a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall. The wall consists of $n$ sections, aligned in a row. The $i$-th section initially has durability $a_i$. If durability of some section becomes $0$ or less, this section is considered broken. To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager β€” a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals $2$ damage to the target section and $1$ damage to adjacent sections. In other words, if the onager shoots at the section $x$, then the durability of the section $x$ decreases by $2$, and the durability of the sections $x - 1$ and $x + 1$ (if they exist) decreases by $1$ each. Monocarp can shoot at any sections any number of times, he can even shoot at broken sections. Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him! -----Input----- The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of sections. The second line contains the sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the initial durability of the $i$-th section. -----Output----- Print one integer β€” the minimum number of onager shots needed to break at least two sections of the wall. -----Examples----- Input 5 20 10 30 10 20 Output 10 Input 3 1 8 1 Output 1 Input 6 7 6 6 8 5 8 Output 4 Input 6 14 3 8 10 15 4 Output 4 Input 4 1 100 100 1 Output 2 Input 3 40 10 10 Output 7 -----Note----- In the first example, it is possible to break the $2$-nd and the $4$-th section in $10$ shots, for example, by shooting the third section $10$ times. After that, the durabilities become $[20, 0, 10, 0, 20]$. Another way of doing it is firing $5$ shots at the $2$-nd section, and another $5$ shots at the $4$-th section. After that, the durabilities become $[15, 0, 20, 0, 15]$. In the second example, it is enough to shoot the $2$-nd section once. Then the $1$-st and the $3$-rd section will be broken. In the third example, it is enough to shoot the $2$-nd section twice (then the durabilities become $[5, 2, 4, 8, 5, 8]$), and then shoot the $3$-rd section twice (then the durabilities become $[5, 0, 0, 6, 5, 8]$). So, four shots are enough to break the $2$-nd and the $3$-rd section.
import sys input = sys.stdin.readline n = int(input()) data = list(map(int, input().split())) temp = sorted(data) a, b = temp[0], temp[1] answer = (a + 1) // 2 + (b + 1) // 2 for i in range(1, n): t1 = int(1000000000.0) if i + 1 < n: t1 = data[i - 1] // 2 + data[i + 1] // 2 t1 += data[i - 1] % 2 | data[i + 1] % 2 A = max(data[i - 1], data[i]) B = min(data[i - 1], data[i]) t3 = t2 = int(1000000000.0) if (A + 1) // 2 >= B: t2 = (A + 1) // 2 else: t3 = (A + B + 2) // 3 answer = min(answer, t1, t2, t3) print(answer)
IMPORT ASSIGN 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 VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Monocarp plays "Rage of Empires II: Definitive Edition" β€” a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall. The wall consists of $n$ sections, aligned in a row. The $i$-th section initially has durability $a_i$. If durability of some section becomes $0$ or less, this section is considered broken. To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager β€” a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals $2$ damage to the target section and $1$ damage to adjacent sections. In other words, if the onager shoots at the section $x$, then the durability of the section $x$ decreases by $2$, and the durability of the sections $x - 1$ and $x + 1$ (if they exist) decreases by $1$ each. Monocarp can shoot at any sections any number of times, he can even shoot at broken sections. Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him! -----Input----- The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of sections. The second line contains the sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the initial durability of the $i$-th section. -----Output----- Print one integer β€” the minimum number of onager shots needed to break at least two sections of the wall. -----Examples----- Input 5 20 10 30 10 20 Output 10 Input 3 1 8 1 Output 1 Input 6 7 6 6 8 5 8 Output 4 Input 6 14 3 8 10 15 4 Output 4 Input 4 1 100 100 1 Output 2 Input 3 40 10 10 Output 7 -----Note----- In the first example, it is possible to break the $2$-nd and the $4$-th section in $10$ shots, for example, by shooting the third section $10$ times. After that, the durabilities become $[20, 0, 10, 0, 20]$. Another way of doing it is firing $5$ shots at the $2$-nd section, and another $5$ shots at the $4$-th section. After that, the durabilities become $[15, 0, 20, 0, 15]$. In the second example, it is enough to shoot the $2$-nd section once. Then the $1$-st and the $3$-rd section will be broken. In the third example, it is enough to shoot the $2$-nd section twice (then the durabilities become $[5, 2, 4, 8, 5, 8]$), and then shoot the $3$-rd section twice (then the durabilities become $[5, 0, 0, 6, 5, 8]$). So, four shots are enough to break the $2$-nd and the $3$-rd section.
INT_MAX = 9999999999 n = int(input()) arr = list(map(int, input().split())) val1 = INT_MAX val2 = INT_MAX for i in range(1, n): if i < n - 1: x = arr[i - 1] + arr[i + 1] if x % 2: x += 1 val2 = min(val2, x // 2) x = max(arr[i], arr[i - 1]) y = min(arr[i], arr[i - 1]) if 2 * y < x: if x % 2: x += 1 val2 = min(val2, x // 2) else: a = x - y b = 2 * y - x if 2 * b % 3: a += 1 a += 2 * b // 3 val2 = min(a, val2) f_val = min(val1, val2) f2 = min(arr) arr.remove(min(arr)) f1 = min(arr) f = f2 // 2 if f2 % 2: f += 1 f += f1 // 2 if f1 % 2: f += 1 print(min(f, f_val))
ASSIGN VAR NUMBER 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 ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP NUMBER VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Monocarp plays "Rage of Empires II: Definitive Edition" β€” a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall. The wall consists of $n$ sections, aligned in a row. The $i$-th section initially has durability $a_i$. If durability of some section becomes $0$ or less, this section is considered broken. To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager β€” a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals $2$ damage to the target section and $1$ damage to adjacent sections. In other words, if the onager shoots at the section $x$, then the durability of the section $x$ decreases by $2$, and the durability of the sections $x - 1$ and $x + 1$ (if they exist) decreases by $1$ each. Monocarp can shoot at any sections any number of times, he can even shoot at broken sections. Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him! -----Input----- The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of sections. The second line contains the sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the initial durability of the $i$-th section. -----Output----- Print one integer β€” the minimum number of onager shots needed to break at least two sections of the wall. -----Examples----- Input 5 20 10 30 10 20 Output 10 Input 3 1 8 1 Output 1 Input 6 7 6 6 8 5 8 Output 4 Input 6 14 3 8 10 15 4 Output 4 Input 4 1 100 100 1 Output 2 Input 3 40 10 10 Output 7 -----Note----- In the first example, it is possible to break the $2$-nd and the $4$-th section in $10$ shots, for example, by shooting the third section $10$ times. After that, the durabilities become $[20, 0, 10, 0, 20]$. Another way of doing it is firing $5$ shots at the $2$-nd section, and another $5$ shots at the $4$-th section. After that, the durabilities become $[15, 0, 20, 0, 15]$. In the second example, it is enough to shoot the $2$-nd section once. Then the $1$-st and the $3$-rd section will be broken. In the third example, it is enough to shoot the $2$-nd section twice (then the durabilities become $[5, 2, 4, 8, 5, 8]$), and then shoot the $3$-rd section twice (then the durabilities become $[5, 0, 0, 6, 5, 8]$). So, four shots are enough to break the $2$-nd and the $3$-rd section.
n = int(input()) l = [int(i) for i in input().split(" ")] a, b = 1000000.0 + 1, 1000000.0 + 1 for i in l: if i < a: a, b = i, a elif i < b: a, b = a, i best = -1 for i in range(n): x = l[i] tmp = [] if i > 0: tmp.append(l[i - 1]) if i < len(l) - 1: tmp.append(l[i + 1]) y = min(tmp) if x > y: x, y = y, x if y >= 2 * x: cur = (y + 1) // 2 else: cur = (x + y + 2) // 3 if len(tmp) == 2: cur = min(cur, min(tmp) + (max(tmp) - min(tmp) + 1) // 2) x = l[i] y = a if l[i] == a: y = b cur = min(cur, (x + 1) // 2 + (y + 1) // 2) if cur < best or best < 0: best = cur print(best)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Monocarp plays "Rage of Empires II: Definitive Edition" β€” a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall. The wall consists of $n$ sections, aligned in a row. The $i$-th section initially has durability $a_i$. If durability of some section becomes $0$ or less, this section is considered broken. To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager β€” a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals $2$ damage to the target section and $1$ damage to adjacent sections. In other words, if the onager shoots at the section $x$, then the durability of the section $x$ decreases by $2$, and the durability of the sections $x - 1$ and $x + 1$ (if they exist) decreases by $1$ each. Monocarp can shoot at any sections any number of times, he can even shoot at broken sections. Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him! -----Input----- The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of sections. The second line contains the sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the initial durability of the $i$-th section. -----Output----- Print one integer β€” the minimum number of onager shots needed to break at least two sections of the wall. -----Examples----- Input 5 20 10 30 10 20 Output 10 Input 3 1 8 1 Output 1 Input 6 7 6 6 8 5 8 Output 4 Input 6 14 3 8 10 15 4 Output 4 Input 4 1 100 100 1 Output 2 Input 3 40 10 10 Output 7 -----Note----- In the first example, it is possible to break the $2$-nd and the $4$-th section in $10$ shots, for example, by shooting the third section $10$ times. After that, the durabilities become $[20, 0, 10, 0, 20]$. Another way of doing it is firing $5$ shots at the $2$-nd section, and another $5$ shots at the $4$-th section. After that, the durabilities become $[15, 0, 20, 0, 15]$. In the second example, it is enough to shoot the $2$-nd section once. Then the $1$-st and the $3$-rd section will be broken. In the third example, it is enough to shoot the $2$-nd section twice (then the durabilities become $[5, 2, 4, 8, 5, 8]$), and then shoot the $3$-rd section twice (then the durabilities become $[5, 0, 0, 6, 5, 8]$). So, four shots are enough to break the $2$-nd and the $3$-rd section.
n = int(input()) arr = list(map(int, input().split())) mini = max(arr) for i in range(n): if i != n - 1: if ( arr[i] >= arr[i + 1] and arr[i] <= 2 * arr[i + 1] or arr[i] <= arr[i + 1] and 2 * arr[i] >= arr[i + 1] ): ans = (arr[i] + arr[i + 1]) // 3 if (arr[i] + arr[i + 1]) % 3: ans += 1 else: ans = max(arr[i], arr[i + 1]) // 2 if max(arr[i], arr[i + 1]) % 2: ans += 1 if ans < mini: mini = ans if len(arr) >= 3 and i != 0: p1 = max(arr[i - 1], arr[i + 1]) - min(arr[i - 1], arr[i + 1]) p2 = min(arr[i - 1], arr[i + 1]) p2 += p1 // 2 if p1 % 2: p2 += 1 if p2 < mini: mini = p2 min1 = min(arr[:2]) min2 = max(arr[:2]) for i in range(2, n): if arr[i] <= min1: min2 = min1 min1 = arr[i] elif arr[i] <= min2: min2 = arr[i] ans = min1 // 2 if min1 % 2: ans += 1 ans += min2 // 2 if min2 % 2: ans += 1 if ans < mini: mini = ans print(mini)
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 VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Monocarp plays "Rage of Empires II: Definitive Edition" β€” a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall. The wall consists of $n$ sections, aligned in a row. The $i$-th section initially has durability $a_i$. If durability of some section becomes $0$ or less, this section is considered broken. To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager β€” a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals $2$ damage to the target section and $1$ damage to adjacent sections. In other words, if the onager shoots at the section $x$, then the durability of the section $x$ decreases by $2$, and the durability of the sections $x - 1$ and $x + 1$ (if they exist) decreases by $1$ each. Monocarp can shoot at any sections any number of times, he can even shoot at broken sections. Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him! -----Input----- The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of sections. The second line contains the sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the initial durability of the $i$-th section. -----Output----- Print one integer β€” the minimum number of onager shots needed to break at least two sections of the wall. -----Examples----- Input 5 20 10 30 10 20 Output 10 Input 3 1 8 1 Output 1 Input 6 7 6 6 8 5 8 Output 4 Input 6 14 3 8 10 15 4 Output 4 Input 4 1 100 100 1 Output 2 Input 3 40 10 10 Output 7 -----Note----- In the first example, it is possible to break the $2$-nd and the $4$-th section in $10$ shots, for example, by shooting the third section $10$ times. After that, the durabilities become $[20, 0, 10, 0, 20]$. Another way of doing it is firing $5$ shots at the $2$-nd section, and another $5$ shots at the $4$-th section. After that, the durabilities become $[15, 0, 20, 0, 15]$. In the second example, it is enough to shoot the $2$-nd section once. Then the $1$-st and the $3$-rd section will be broken. In the third example, it is enough to shoot the $2$-nd section twice (then the durabilities become $[5, 2, 4, 8, 5, 8]$), and then shoot the $3$-rd section twice (then the durabilities become $[5, 0, 0, 6, 5, 8]$). So, four shots are enough to break the $2$-nd and the $3$-rd section.
def resi(): n = int(input()) a = list(map(int, input().split())) min1, min2 = 10000001, 10000001 for i in range(n): if a[i] <= min1: min2 = min1 min1 = a[i] elif a[i] <= min2: min2 = a[i] res1 = (min1 + 1) // 2 + (min2 + 1) // 2 res2 = 10000001 for i in range(n - 1): x = min(a[i], a[i + 1]) y = max(a[i], a[i + 1]) if y >= 2 * x: if res2 > (y + 1) // 2: res2 = (y + 1) // 2 elif res2 > y - x + (2 * (2 * x - y) + 2) // 3: res2 = y - x + (2 * (2 * x - y) + 2) // 3 res3 = 10000001 for i in range(1, n - 1): if ( res3 > min(a[i - 1], a[i + 1]) + (max(a[i - 1], a[i + 1]) - min(a[i - 1], a[i + 1]) + 1) // 2 ): res3 = ( min(a[i - 1], a[i + 1]) + (max(a[i - 1], a[i + 1]) - min(a[i - 1], a[i + 1]) + 1) // 2 ) print(min(res1, res2, res3)) resi()
FUNC_DEF 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 NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP NUMBER VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
Monocarp plays "Rage of Empires II: Definitive Edition" β€” a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall. The wall consists of $n$ sections, aligned in a row. The $i$-th section initially has durability $a_i$. If durability of some section becomes $0$ or less, this section is considered broken. To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager β€” a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals $2$ damage to the target section and $1$ damage to adjacent sections. In other words, if the onager shoots at the section $x$, then the durability of the section $x$ decreases by $2$, and the durability of the sections $x - 1$ and $x + 1$ (if they exist) decreases by $1$ each. Monocarp can shoot at any sections any number of times, he can even shoot at broken sections. Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him! -----Input----- The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of sections. The second line contains the sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the initial durability of the $i$-th section. -----Output----- Print one integer β€” the minimum number of onager shots needed to break at least two sections of the wall. -----Examples----- Input 5 20 10 30 10 20 Output 10 Input 3 1 8 1 Output 1 Input 6 7 6 6 8 5 8 Output 4 Input 6 14 3 8 10 15 4 Output 4 Input 4 1 100 100 1 Output 2 Input 3 40 10 10 Output 7 -----Note----- In the first example, it is possible to break the $2$-nd and the $4$-th section in $10$ shots, for example, by shooting the third section $10$ times. After that, the durabilities become $[20, 0, 10, 0, 20]$. Another way of doing it is firing $5$ shots at the $2$-nd section, and another $5$ shots at the $4$-th section. After that, the durabilities become $[15, 0, 20, 0, 15]$. In the second example, it is enough to shoot the $2$-nd section once. Then the $1$-st and the $3$-rd section will be broken. In the third example, it is enough to shoot the $2$-nd section twice (then the durabilities become $[5, 2, 4, 8, 5, 8]$), and then shoot the $3$-rd section twice (then the durabilities become $[5, 0, 0, 6, 5, 8]$). So, four shots are enough to break the $2$-nd and the $3$-rd section.
n = int(input()) a = list(map(int, input().split())) ind = 0 for i in range(1, n): if a[i] < a[ind]: ind = i ind2 = -1 for i in range(n): if i == ind: continue if ind2 == -1: ind2 = i elif a[i] < a[ind2]: ind2 = i def f(x): return (x + 1) // 2 ans = f(a[ind]) + f(a[ind2]) for i in range(1, n - 1): ans = min(ans, 1 + f(a[i - 1] - 1) + f(a[i + 1] - 1)) for i in range(1, n): x = a[i - 1] y = a[i] if x > y: x, y = y, x cur = 0 op = min(x, y - x) cur += op x -= op y -= 2 * op op = min(x // 3, y // 3) x -= 3 * op y -= 3 * op cur += 2 * op if x: cur += 1 y -= 2 x -= 1 cur += f(x) + f(y) ans = min(ans, cur) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Monocarp plays "Rage of Empires II: Definitive Edition" β€” a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall. The wall consists of $n$ sections, aligned in a row. The $i$-th section initially has durability $a_i$. If durability of some section becomes $0$ or less, this section is considered broken. To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager β€” a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals $2$ damage to the target section and $1$ damage to adjacent sections. In other words, if the onager shoots at the section $x$, then the durability of the section $x$ decreases by $2$, and the durability of the sections $x - 1$ and $x + 1$ (if they exist) decreases by $1$ each. Monocarp can shoot at any sections any number of times, he can even shoot at broken sections. Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him! -----Input----- The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of sections. The second line contains the sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the initial durability of the $i$-th section. -----Output----- Print one integer β€” the minimum number of onager shots needed to break at least two sections of the wall. -----Examples----- Input 5 20 10 30 10 20 Output 10 Input 3 1 8 1 Output 1 Input 6 7 6 6 8 5 8 Output 4 Input 6 14 3 8 10 15 4 Output 4 Input 4 1 100 100 1 Output 2 Input 3 40 10 10 Output 7 -----Note----- In the first example, it is possible to break the $2$-nd and the $4$-th section in $10$ shots, for example, by shooting the third section $10$ times. After that, the durabilities become $[20, 0, 10, 0, 20]$. Another way of doing it is firing $5$ shots at the $2$-nd section, and another $5$ shots at the $4$-th section. After that, the durabilities become $[15, 0, 20, 0, 15]$. In the second example, it is enough to shoot the $2$-nd section once. Then the $1$-st and the $3$-rd section will be broken. In the third example, it is enough to shoot the $2$-nd section twice (then the durabilities become $[5, 2, 4, 8, 5, 8]$), and then shoot the $3$-rd section twice (then the durabilities become $[5, 0, 0, 6, 5, 8]$). So, four shots are enough to break the $2$-nd and the $3$-rd section.
def main(): n = int(input()) a = list(map(int, input().split())) ans = [0] * (n - 1) ans_ = [0] * (n - 2) for i in range(n - 1): x1, x2 = a[i], a[i + 1] s = x1 + x2 min_ = (s + 2) // 3 x1, x2 = max(x1, x2), min(x1, x2) if x2 * 2 >= x1: ans[i] = min_ else: ans[i] = (x1 + 1) // 2 for i in range(n - 2): x1, x2 = a[i], a[i + 2] x1, x2 = max(x1, x2), min(x1, x2) delta = x1 - x2 ans_[i] = x2 + (delta + 1) // 2 min_1, min_2 = 10**9, 10**9 for i in a: if i < min_1: min_2 = min_1 min_1 = i elif i < min_2: min_2 = i answer = (min_1 + 1) // 2 + (min_2 + 1) // 2 for i in ans: if i < answer: answer = i for i in ans_: if i < answer: answer = i print(answer) main()
FUNC_DEF 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 BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Monocarp plays "Rage of Empires II: Definitive Edition" β€” a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall. The wall consists of $n$ sections, aligned in a row. The $i$-th section initially has durability $a_i$. If durability of some section becomes $0$ or less, this section is considered broken. To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager β€” a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals $2$ damage to the target section and $1$ damage to adjacent sections. In other words, if the onager shoots at the section $x$, then the durability of the section $x$ decreases by $2$, and the durability of the sections $x - 1$ and $x + 1$ (if they exist) decreases by $1$ each. Monocarp can shoot at any sections any number of times, he can even shoot at broken sections. Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him! -----Input----- The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of sections. The second line contains the sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the initial durability of the $i$-th section. -----Output----- Print one integer β€” the minimum number of onager shots needed to break at least two sections of the wall. -----Examples----- Input 5 20 10 30 10 20 Output 10 Input 3 1 8 1 Output 1 Input 6 7 6 6 8 5 8 Output 4 Input 6 14 3 8 10 15 4 Output 4 Input 4 1 100 100 1 Output 2 Input 3 40 10 10 Output 7 -----Note----- In the first example, it is possible to break the $2$-nd and the $4$-th section in $10$ shots, for example, by shooting the third section $10$ times. After that, the durabilities become $[20, 0, 10, 0, 20]$. Another way of doing it is firing $5$ shots at the $2$-nd section, and another $5$ shots at the $4$-th section. After that, the durabilities become $[15, 0, 20, 0, 15]$. In the second example, it is enough to shoot the $2$-nd section once. Then the $1$-st and the $3$-rd section will be broken. In the third example, it is enough to shoot the $2$-nd section twice (then the durabilities become $[5, 2, 4, 8, 5, 8]$), and then shoot the $3$-rd section twice (then the durabilities become $[5, 0, 0, 6, 5, 8]$). So, four shots are enough to break the $2$-nd and the $3$-rd section.
N, A = int(input()), list(map(int, input().split())) c = 1000000 for i in range(N - 2): prev = c c = min(c, max(A[i], A[i + 2])) if A[i] % 2 == A[i + 2] % 2 == 1: c = min(c, A[i] // 2 + A[i + 2] // 2 + 1) for i in range(N - 1): x = min(A[i], A[i + 1]) y = A[i] + A[i + 1] - x if min(A[i], A[i + 1]) <= (max(A[i], A[i + 1]) + 1) // 2: c = min(c, (max(A[i], A[i + 1]) + 1) // 2) else: c = min(c, (A[i] + A[i + 1] + 2) // 3) X = sorted(A) c = min(c, (X[0] + 1) // 2 + (X[1] + 1) // 2) print(c)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Monocarp plays "Rage of Empires II: Definitive Edition" β€” a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall. The wall consists of $n$ sections, aligned in a row. The $i$-th section initially has durability $a_i$. If durability of some section becomes $0$ or less, this section is considered broken. To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager β€” a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals $2$ damage to the target section and $1$ damage to adjacent sections. In other words, if the onager shoots at the section $x$, then the durability of the section $x$ decreases by $2$, and the durability of the sections $x - 1$ and $x + 1$ (if they exist) decreases by $1$ each. Monocarp can shoot at any sections any number of times, he can even shoot at broken sections. Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him! -----Input----- The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of sections. The second line contains the sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the initial durability of the $i$-th section. -----Output----- Print one integer β€” the minimum number of onager shots needed to break at least two sections of the wall. -----Examples----- Input 5 20 10 30 10 20 Output 10 Input 3 1 8 1 Output 1 Input 6 7 6 6 8 5 8 Output 4 Input 6 14 3 8 10 15 4 Output 4 Input 4 1 100 100 1 Output 2 Input 3 40 10 10 Output 7 -----Note----- In the first example, it is possible to break the $2$-nd and the $4$-th section in $10$ shots, for example, by shooting the third section $10$ times. After that, the durabilities become $[20, 0, 10, 0, 20]$. Another way of doing it is firing $5$ shots at the $2$-nd section, and another $5$ shots at the $4$-th section. After that, the durabilities become $[15, 0, 20, 0, 15]$. In the second example, it is enough to shoot the $2$-nd section once. Then the $1$-st and the $3$-rd section will be broken. In the third example, it is enough to shoot the $2$-nd section twice (then the durabilities become $[5, 2, 4, 8, 5, 8]$), and then shoot the $3$-rd section twice (then the durabilities become $[5, 0, 0, 6, 5, 8]$). So, four shots are enough to break the $2$-nd and the $3$-rd section.
n = int(input()) a = list(map(int, input().split())) ans1 = 10000000 for i in range(n - 1): if max(a[i], a[i + 1]) >= 2 * min(a[i], a[i + 1]): curr = max(a[i] + 1, a[i + 1] + 1) // 2 else: curr = (a[i] + a[i + 1] + 2) // 3 if curr < ans1: ans1 = curr ans2 = 10000000 for i in range(1, n - 1): x = min(a[i - 1], a[i + 1]) y = max(a[i - 1], a[i + 1]) curr = x + (y - x + 1) // 2 if curr < ans2: ans2 = curr a.sort() ans3 = (a[0] + 1) // 2 + (a[1] + 1) // 2 print(min(ans1, ans2, ans3))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Monocarp plays "Rage of Empires II: Definitive Edition" β€” a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall. The wall consists of $n$ sections, aligned in a row. The $i$-th section initially has durability $a_i$. If durability of some section becomes $0$ or less, this section is considered broken. To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager β€” a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals $2$ damage to the target section and $1$ damage to adjacent sections. In other words, if the onager shoots at the section $x$, then the durability of the section $x$ decreases by $2$, and the durability of the sections $x - 1$ and $x + 1$ (if they exist) decreases by $1$ each. Monocarp can shoot at any sections any number of times, he can even shoot at broken sections. Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him! -----Input----- The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of sections. The second line contains the sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the initial durability of the $i$-th section. -----Output----- Print one integer β€” the minimum number of onager shots needed to break at least two sections of the wall. -----Examples----- Input 5 20 10 30 10 20 Output 10 Input 3 1 8 1 Output 1 Input 6 7 6 6 8 5 8 Output 4 Input 6 14 3 8 10 15 4 Output 4 Input 4 1 100 100 1 Output 2 Input 3 40 10 10 Output 7 -----Note----- In the first example, it is possible to break the $2$-nd and the $4$-th section in $10$ shots, for example, by shooting the third section $10$ times. After that, the durabilities become $[20, 0, 10, 0, 20]$. Another way of doing it is firing $5$ shots at the $2$-nd section, and another $5$ shots at the $4$-th section. After that, the durabilities become $[15, 0, 20, 0, 15]$. In the second example, it is enough to shoot the $2$-nd section once. Then the $1$-st and the $3$-rd section will be broken. In the third example, it is enough to shoot the $2$-nd section twice (then the durabilities become $[5, 2, 4, 8, 5, 8]$), and then shoot the $3$-rd section twice (then the durabilities become $[5, 0, 0, 6, 5, 8]$). So, four shots are enough to break the $2$-nd and the $3$-rd section.
def solve(): n = int(input()) l = list(map(int, input().split())) mn_ans = l[0] + l[1] for i in range(1, n - 1): x, y = l[i - 1], l[i + 1] if x < y: t = x x = y y = t mn_ans = min(mn_ans, y + (x - y + 1) // 2) for i in range(0, n - 1): curr = 0 x, y = l[i], l[i + 1] if x < y: t = x x = y y = t cnt = min(x - y, (x + 1) // 2) curr += cnt x -= 2 * cnt y -= cnt if x > 0 and y > 0: curr += (x + y + 2) // 3 mn_ans = min(mn_ans, curr) l.sort() mn_ans = min(mn_ans, (l[0] + 1) // 2 + (l[1] + 1) // 2) print(mn_ans) t = 1 while t > 0: solve() t -= 1
FUNC_DEF 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 VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is playing a noob version of the game Flappy Bird with the following rules: The bird starts at a height $H$ at $x = 0$. There are $N$ obstacles (numbered $1$ through $N$). For each valid $i$, the $i$-th obstacle is at a position $x_{i}$ and it has a height $h_{i}$. For each integer $i$ ($0 ≀ i < x_{N}$), Chef has the option to click once when the bird is at $x = i$. Let's denote the bird's height ($y$-coordinate) at that point by $j$. If he clicks, the bird moves to $x = i+1$ and $y = j+1$. Otherwise, the bird moves to $x = i+1$ and $y = j-1$. There is no ceiling and all the obstacles extend upwards from the bottom and not the other way around. For each valid $i$, the bird crosses the $i$-th obstacle successfully if the bird's height at $x = x_{i}$ is strictly greater than $h_{i}$. At any point before $x = x_{N}$, the bird's height should remain non-negative, otherwise it will drown. If the bird crosses the $N$-th obstacle successfully, Chef wins the game. Can you tell Chef if he can win the game (get the bird to cross all the obstacles) and the minimum number of clicks required to win in such a case? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $H$. The second line contains $N$ space-separated integers $x_{1}, x_{2}, \ldots, x_{N}$. The third line contains $N$ space-separated integers $h_{1}, h_{2}, \ldots, h_{N}$. ------ Output ------ For each test case, print a single line containing one integer β€” the minimum number of clicks required to cross all the obstacles successfully, or $-1$ if it is impossible to cross all the obstacles. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ H ≀ 10^{9}$ $1 ≀ x_{1} < x_{2} < \ldots < x_{N} ≀ 10^{9}$ $1 ≀ h_{i} ≀ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$ ------ Example Input ------ 3 1 0 2 1 2 1 1 3 1 1 5 10 1 2 3 4 5 10 11 12 13 15 ------ Example Output ------ 2 2 -1 ------ Explanation ------ Example case 2: The figure below shows one possible way to successfully cross all the obstacles using the minimum number of clicks. Example case 3: It is clear that even by clicking all the time, it is impossible to cross the last obstacle.
for _ in range(int(input())): n, H = map(int, input().rstrip().split()) x = list(map(int, input().rstrip().split())) h = list(map(int, input().rstrip().split())) p = list() for i in range(n): p.append((x[i], h[i] - H)) flag = True clicks = 0 for x, h in p: if x <= h: flag = False break clicks = max((x + h + 2) // 2, clicks) if flag: print(clicks) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is playing a noob version of the game Flappy Bird with the following rules: The bird starts at a height $H$ at $x = 0$. There are $N$ obstacles (numbered $1$ through $N$). For each valid $i$, the $i$-th obstacle is at a position $x_{i}$ and it has a height $h_{i}$. For each integer $i$ ($0 ≀ i < x_{N}$), Chef has the option to click once when the bird is at $x = i$. Let's denote the bird's height ($y$-coordinate) at that point by $j$. If he clicks, the bird moves to $x = i+1$ and $y = j+1$. Otherwise, the bird moves to $x = i+1$ and $y = j-1$. There is no ceiling and all the obstacles extend upwards from the bottom and not the other way around. For each valid $i$, the bird crosses the $i$-th obstacle successfully if the bird's height at $x = x_{i}$ is strictly greater than $h_{i}$. At any point before $x = x_{N}$, the bird's height should remain non-negative, otherwise it will drown. If the bird crosses the $N$-th obstacle successfully, Chef wins the game. Can you tell Chef if he can win the game (get the bird to cross all the obstacles) and the minimum number of clicks required to win in such a case? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $H$. The second line contains $N$ space-separated integers $x_{1}, x_{2}, \ldots, x_{N}$. The third line contains $N$ space-separated integers $h_{1}, h_{2}, \ldots, h_{N}$. ------ Output ------ For each test case, print a single line containing one integer β€” the minimum number of clicks required to cross all the obstacles successfully, or $-1$ if it is impossible to cross all the obstacles. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ H ≀ 10^{9}$ $1 ≀ x_{1} < x_{2} < \ldots < x_{N} ≀ 10^{9}$ $1 ≀ h_{i} ≀ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$ ------ Example Input ------ 3 1 0 2 1 2 1 1 3 1 1 5 10 1 2 3 4 5 10 11 12 13 15 ------ Example Output ------ 2 2 -1 ------ Explanation ------ Example case 2: The figure below shows one possible way to successfully cross all the obstacles using the minimum number of clicks. Example case 3: It is clear that even by clicking all the time, it is impossible to cross the last obstacle.
def check(k, x, h, H): for i in range(len(x)): if x[i] <= k and H + x[i] <= h[i]: return False if x[i] > k and H + k - (x[i] - k) <= h[i]: return False return True for _ in range(int(input())): N, H = map(int, input().split()) x = list(map(int, input().split())) h = list(map(int, input().split())) l, r = 0, 10**9 ans = -1 while l <= r: mid = (l + r) // 2 if check(mid, x, h, H): ans = mid r = mid - 1 else: l = mid + 1 print(ans)
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is playing a noob version of the game Flappy Bird with the following rules: The bird starts at a height $H$ at $x = 0$. There are $N$ obstacles (numbered $1$ through $N$). For each valid $i$, the $i$-th obstacle is at a position $x_{i}$ and it has a height $h_{i}$. For each integer $i$ ($0 ≀ i < x_{N}$), Chef has the option to click once when the bird is at $x = i$. Let's denote the bird's height ($y$-coordinate) at that point by $j$. If he clicks, the bird moves to $x = i+1$ and $y = j+1$. Otherwise, the bird moves to $x = i+1$ and $y = j-1$. There is no ceiling and all the obstacles extend upwards from the bottom and not the other way around. For each valid $i$, the bird crosses the $i$-th obstacle successfully if the bird's height at $x = x_{i}$ is strictly greater than $h_{i}$. At any point before $x = x_{N}$, the bird's height should remain non-negative, otherwise it will drown. If the bird crosses the $N$-th obstacle successfully, Chef wins the game. Can you tell Chef if he can win the game (get the bird to cross all the obstacles) and the minimum number of clicks required to win in such a case? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $H$. The second line contains $N$ space-separated integers $x_{1}, x_{2}, \ldots, x_{N}$. The third line contains $N$ space-separated integers $h_{1}, h_{2}, \ldots, h_{N}$. ------ Output ------ For each test case, print a single line containing one integer β€” the minimum number of clicks required to cross all the obstacles successfully, or $-1$ if it is impossible to cross all the obstacles. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ H ≀ 10^{9}$ $1 ≀ x_{1} < x_{2} < \ldots < x_{N} ≀ 10^{9}$ $1 ≀ h_{i} ≀ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$ ------ Example Input ------ 3 1 0 2 1 2 1 1 3 1 1 5 10 1 2 3 4 5 10 11 12 13 15 ------ Example Output ------ 2 2 -1 ------ Explanation ------ Example case 2: The figure below shows one possible way to successfully cross all the obstacles using the minimum number of clicks. Example case 3: It is clear that even by clicking all the time, it is impossible to cross the last obstacle.
n_testcase = int(input()) for testcase in range(n_testcase): n, initial_height = (int(x) for x in input().split()) obstacle_positions = [int(x) for x in input().split()] obstacle_heights = [int(x) for x in input().split()] obstacles = list(zip(obstacle_positions, obstacle_heights)) impossible = any( obstacle_height - initial_height >= obstacle_position for obstacle_position, obstacle_height in obstacles ) if impossible: print(-1) continue left = max((max(obstacle_positions) - initial_height + 1) // 2, 0) right = 10**9 while left < right: mid = (left + right) // 2 for obstacle_position, obstacle_height in obstacles: count_up = min(mid, obstacle_position) count_down = obstacle_position - count_up if initial_height + count_up - count_down <= obstacle_height: left = mid + 1 break else: right = mid print(right)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR 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 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 BIN_OP VAR VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is playing a noob version of the game Flappy Bird with the following rules: The bird starts at a height $H$ at $x = 0$. There are $N$ obstacles (numbered $1$ through $N$). For each valid $i$, the $i$-th obstacle is at a position $x_{i}$ and it has a height $h_{i}$. For each integer $i$ ($0 ≀ i < x_{N}$), Chef has the option to click once when the bird is at $x = i$. Let's denote the bird's height ($y$-coordinate) at that point by $j$. If he clicks, the bird moves to $x = i+1$ and $y = j+1$. Otherwise, the bird moves to $x = i+1$ and $y = j-1$. There is no ceiling and all the obstacles extend upwards from the bottom and not the other way around. For each valid $i$, the bird crosses the $i$-th obstacle successfully if the bird's height at $x = x_{i}$ is strictly greater than $h_{i}$. At any point before $x = x_{N}$, the bird's height should remain non-negative, otherwise it will drown. If the bird crosses the $N$-th obstacle successfully, Chef wins the game. Can you tell Chef if he can win the game (get the bird to cross all the obstacles) and the minimum number of clicks required to win in such a case? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $H$. The second line contains $N$ space-separated integers $x_{1}, x_{2}, \ldots, x_{N}$. The third line contains $N$ space-separated integers $h_{1}, h_{2}, \ldots, h_{N}$. ------ Output ------ For each test case, print a single line containing one integer β€” the minimum number of clicks required to cross all the obstacles successfully, or $-1$ if it is impossible to cross all the obstacles. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ H ≀ 10^{9}$ $1 ≀ x_{1} < x_{2} < \ldots < x_{N} ≀ 10^{9}$ $1 ≀ h_{i} ≀ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$ ------ Example Input ------ 3 1 0 2 1 2 1 1 3 1 1 5 10 1 2 3 4 5 10 11 12 13 15 ------ Example Output ------ 2 2 -1 ------ Explanation ------ Example case 2: The figure below shows one possible way to successfully cross all the obstacles using the minimum number of clicks. Example case 3: It is clear that even by clicking all the time, it is impossible to cross the last obstacle.
t = int(input()) for i in range(t): n, h = map(int, input().split()) x = list(map(int, input().split())) h1 = list(map(int, input().split())) for j in range(n): h1[j] = h1[j] - h c = 0 for k in range(n): if x[k] <= h1[k]: print(-1) c = 1 break ans = 0 for z in range(n): if (h1[z] + x[z] + 2) / 2 > ans: ans = (h1[z] + x[z] + 2) / 2 if c == 0: print(int(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is playing a noob version of the game Flappy Bird with the following rules: The bird starts at a height $H$ at $x = 0$. There are $N$ obstacles (numbered $1$ through $N$). For each valid $i$, the $i$-th obstacle is at a position $x_{i}$ and it has a height $h_{i}$. For each integer $i$ ($0 ≀ i < x_{N}$), Chef has the option to click once when the bird is at $x = i$. Let's denote the bird's height ($y$-coordinate) at that point by $j$. If he clicks, the bird moves to $x = i+1$ and $y = j+1$. Otherwise, the bird moves to $x = i+1$ and $y = j-1$. There is no ceiling and all the obstacles extend upwards from the bottom and not the other way around. For each valid $i$, the bird crosses the $i$-th obstacle successfully if the bird's height at $x = x_{i}$ is strictly greater than $h_{i}$. At any point before $x = x_{N}$, the bird's height should remain non-negative, otherwise it will drown. If the bird crosses the $N$-th obstacle successfully, Chef wins the game. Can you tell Chef if he can win the game (get the bird to cross all the obstacles) and the minimum number of clicks required to win in such a case? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $H$. The second line contains $N$ space-separated integers $x_{1}, x_{2}, \ldots, x_{N}$. The third line contains $N$ space-separated integers $h_{1}, h_{2}, \ldots, h_{N}$. ------ Output ------ For each test case, print a single line containing one integer β€” the minimum number of clicks required to cross all the obstacles successfully, or $-1$ if it is impossible to cross all the obstacles. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ H ≀ 10^{9}$ $1 ≀ x_{1} < x_{2} < \ldots < x_{N} ≀ 10^{9}$ $1 ≀ h_{i} ≀ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$ ------ Example Input ------ 3 1 0 2 1 2 1 1 3 1 1 5 10 1 2 3 4 5 10 11 12 13 15 ------ Example Output ------ 2 2 -1 ------ Explanation ------ Example case 2: The figure below shows one possible way to successfully cross all the obstacles using the minimum number of clicks. Example case 3: It is clear that even by clicking all the time, it is impossible to cross the last obstacle.
for _ in range(int(input())): N, H = map(int, input().split()) l = [] for i in input().split(): x = int(i) l.append(x) h = [] for i in input().split(): x = int(i) h.append(x - H) flag = True ans = 0 for a, b in zip(l, h): if a <= b: flag = False break ans = max(ans, (a + b + 2) // 2) print(ans if flag else -1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is playing a noob version of the game Flappy Bird with the following rules: The bird starts at a height $H$ at $x = 0$. There are $N$ obstacles (numbered $1$ through $N$). For each valid $i$, the $i$-th obstacle is at a position $x_{i}$ and it has a height $h_{i}$. For each integer $i$ ($0 ≀ i < x_{N}$), Chef has the option to click once when the bird is at $x = i$. Let's denote the bird's height ($y$-coordinate) at that point by $j$. If he clicks, the bird moves to $x = i+1$ and $y = j+1$. Otherwise, the bird moves to $x = i+1$ and $y = j-1$. There is no ceiling and all the obstacles extend upwards from the bottom and not the other way around. For each valid $i$, the bird crosses the $i$-th obstacle successfully if the bird's height at $x = x_{i}$ is strictly greater than $h_{i}$. At any point before $x = x_{N}$, the bird's height should remain non-negative, otherwise it will drown. If the bird crosses the $N$-th obstacle successfully, Chef wins the game. Can you tell Chef if he can win the game (get the bird to cross all the obstacles) and the minimum number of clicks required to win in such a case? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $H$. The second line contains $N$ space-separated integers $x_{1}, x_{2}, \ldots, x_{N}$. The third line contains $N$ space-separated integers $h_{1}, h_{2}, \ldots, h_{N}$. ------ Output ------ For each test case, print a single line containing one integer β€” the minimum number of clicks required to cross all the obstacles successfully, or $-1$ if it is impossible to cross all the obstacles. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ H ≀ 10^{9}$ $1 ≀ x_{1} < x_{2} < \ldots < x_{N} ≀ 10^{9}$ $1 ≀ h_{i} ≀ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$ ------ Example Input ------ 3 1 0 2 1 2 1 1 3 1 1 5 10 1 2 3 4 5 10 11 12 13 15 ------ Example Output ------ 2 2 -1 ------ Explanation ------ Example case 2: The figure below shows one possible way to successfully cross all the obstacles using the minimum number of clicks. Example case 3: It is clear that even by clicking all the time, it is impossible to cross the last obstacle.
for _ in range(int(input())): n, h = map(int, input().split()) X = list(map(int, input().split())) H = list(map(int, input().split())) Y = [] for i in range(n): x = X[i] reqh = H[i] if reqh >= h + x: print(-1) break else: Y.append(h + x - reqh - 1) else: c = 0 m = Y[-1] for i in range(n - 1, -1, -1): m = min(m, Y[i]) if m <= 1: break tick = X[i] - (X[i - 1] if i != 0 else 0) lw = min(m // 2, tick) c += lw m -= lw * 2 print(X[-1] - c)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is playing a noob version of the game Flappy Bird with the following rules: The bird starts at a height $H$ at $x = 0$. There are $N$ obstacles (numbered $1$ through $N$). For each valid $i$, the $i$-th obstacle is at a position $x_{i}$ and it has a height $h_{i}$. For each integer $i$ ($0 ≀ i < x_{N}$), Chef has the option to click once when the bird is at $x = i$. Let's denote the bird's height ($y$-coordinate) at that point by $j$. If he clicks, the bird moves to $x = i+1$ and $y = j+1$. Otherwise, the bird moves to $x = i+1$ and $y = j-1$. There is no ceiling and all the obstacles extend upwards from the bottom and not the other way around. For each valid $i$, the bird crosses the $i$-th obstacle successfully if the bird's height at $x = x_{i}$ is strictly greater than $h_{i}$. At any point before $x = x_{N}$, the bird's height should remain non-negative, otherwise it will drown. If the bird crosses the $N$-th obstacle successfully, Chef wins the game. Can you tell Chef if he can win the game (get the bird to cross all the obstacles) and the minimum number of clicks required to win in such a case? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $H$. The second line contains $N$ space-separated integers $x_{1}, x_{2}, \ldots, x_{N}$. The third line contains $N$ space-separated integers $h_{1}, h_{2}, \ldots, h_{N}$. ------ Output ------ For each test case, print a single line containing one integer β€” the minimum number of clicks required to cross all the obstacles successfully, or $-1$ if it is impossible to cross all the obstacles. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ H ≀ 10^{9}$ $1 ≀ x_{1} < x_{2} < \ldots < x_{N} ≀ 10^{9}$ $1 ≀ h_{i} ≀ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$ ------ Example Input ------ 3 1 0 2 1 2 1 1 3 1 1 5 10 1 2 3 4 5 10 11 12 13 15 ------ Example Output ------ 2 2 -1 ------ Explanation ------ Example case 2: The figure below shows one possible way to successfully cross all the obstacles using the minimum number of clicks. Example case 3: It is clear that even by clicking all the time, it is impossible to cross the last obstacle.
def check(obs_pos, obs_h, h, clicks): for i in range(len(obs_pos)): if i == 0: if clicks <= obs_pos[0]: h += clicks h -= obs_pos[0] - clicks clicks = 0 else: h += obs_pos[0] clicks -= obs_pos[0] elif clicks <= obs_pos[i] - obs_pos[i - 1]: h += clicks h -= obs_pos[i] - obs_pos[i - 1] - clicks clicks = 0 elif clicks > obs_pos[i] - obs_pos[i - 1]: h += obs_pos[i] - obs_pos[i - 1] clicks -= obs_pos[i] - obs_pos[i - 1] if h <= obs_h[i]: return False else: return True def bsearch(obs_pos, obs_h, h): left = 0 right = obs_pos[-1] ans = -1 while left <= right: mid = left + (right - left) // 2 if check(obs_pos, obs_h, h, mid): ans = mid right = mid - 1 else: left = mid + 1 return ans for i in range(int(input())): n, h = map(int, input().split()) obs_pos = [int(x) for x in input().split()] obs_h = [int(h) for h in input().split()] ans = bsearch(obs_pos, obs_h, h) print(ans)
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL 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 VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is playing a noob version of the game Flappy Bird with the following rules: The bird starts at a height $H$ at $x = 0$. There are $N$ obstacles (numbered $1$ through $N$). For each valid $i$, the $i$-th obstacle is at a position $x_{i}$ and it has a height $h_{i}$. For each integer $i$ ($0 ≀ i < x_{N}$), Chef has the option to click once when the bird is at $x = i$. Let's denote the bird's height ($y$-coordinate) at that point by $j$. If he clicks, the bird moves to $x = i+1$ and $y = j+1$. Otherwise, the bird moves to $x = i+1$ and $y = j-1$. There is no ceiling and all the obstacles extend upwards from the bottom and not the other way around. For each valid $i$, the bird crosses the $i$-th obstacle successfully if the bird's height at $x = x_{i}$ is strictly greater than $h_{i}$. At any point before $x = x_{N}$, the bird's height should remain non-negative, otherwise it will drown. If the bird crosses the $N$-th obstacle successfully, Chef wins the game. Can you tell Chef if he can win the game (get the bird to cross all the obstacles) and the minimum number of clicks required to win in such a case? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $H$. The second line contains $N$ space-separated integers $x_{1}, x_{2}, \ldots, x_{N}$. The third line contains $N$ space-separated integers $h_{1}, h_{2}, \ldots, h_{N}$. ------ Output ------ For each test case, print a single line containing one integer β€” the minimum number of clicks required to cross all the obstacles successfully, or $-1$ if it is impossible to cross all the obstacles. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ H ≀ 10^{9}$ $1 ≀ x_{1} < x_{2} < \ldots < x_{N} ≀ 10^{9}$ $1 ≀ h_{i} ≀ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$ ------ Example Input ------ 3 1 0 2 1 2 1 1 3 1 1 5 10 1 2 3 4 5 10 11 12 13 15 ------ Example Output ------ 2 2 -1 ------ Explanation ------ Example case 2: The figure below shows one possible way to successfully cross all the obstacles using the minimum number of clicks. Example case 3: It is clear that even by clicking all the time, it is impossible to cross the last obstacle.
def check(x): for i in range(n): if X[i] <= x: if H[i] >= X[i]: return False elif H[i] >= x - (X[i] - x): return False return True for _ in range(int(input())): n, h = map(int, input().split()) X = list(map(int, input().split())) H = list(map(int, input().split())) for i in range(n): H[i] -= h s = 0 e = 2000000000 while s < e: mid = (s + e) // 2 if check(mid) == False: s = mid + 1 else: e = mid if s > 1000000010: s = -1 print(s)
FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is playing a noob version of the game Flappy Bird with the following rules: The bird starts at a height $H$ at $x = 0$. There are $N$ obstacles (numbered $1$ through $N$). For each valid $i$, the $i$-th obstacle is at a position $x_{i}$ and it has a height $h_{i}$. For each integer $i$ ($0 ≀ i < x_{N}$), Chef has the option to click once when the bird is at $x = i$. Let's denote the bird's height ($y$-coordinate) at that point by $j$. If he clicks, the bird moves to $x = i+1$ and $y = j+1$. Otherwise, the bird moves to $x = i+1$ and $y = j-1$. There is no ceiling and all the obstacles extend upwards from the bottom and not the other way around. For each valid $i$, the bird crosses the $i$-th obstacle successfully if the bird's height at $x = x_{i}$ is strictly greater than $h_{i}$. At any point before $x = x_{N}$, the bird's height should remain non-negative, otherwise it will drown. If the bird crosses the $N$-th obstacle successfully, Chef wins the game. Can you tell Chef if he can win the game (get the bird to cross all the obstacles) and the minimum number of clicks required to win in such a case? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $H$. The second line contains $N$ space-separated integers $x_{1}, x_{2}, \ldots, x_{N}$. The third line contains $N$ space-separated integers $h_{1}, h_{2}, \ldots, h_{N}$. ------ Output ------ For each test case, print a single line containing one integer β€” the minimum number of clicks required to cross all the obstacles successfully, or $-1$ if it is impossible to cross all the obstacles. ------ Constraints ------ $1 ≀ T ≀ 3 \cdot 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ H ≀ 10^{9}$ $1 ≀ x_{1} < x_{2} < \ldots < x_{N} ≀ 10^{9}$ $1 ≀ h_{i} ≀ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$ ------ Example Input ------ 3 1 0 2 1 2 1 1 3 1 1 5 10 1 2 3 4 5 10 11 12 13 15 ------ Example Output ------ 2 2 -1 ------ Explanation ------ Example case 2: The figure below shows one possible way to successfully cross all the obstacles using the minimum number of clicks. Example case 3: It is clear that even by clicking all the time, it is impossible to cross the last obstacle.
t = int(input()) for _ in range(t): n, H = map(int, input().split()) x = list(map(int, input().split())) h = list(map(int, input().split())) res = [] for i in range(len(h)): s = h[i] - H res.append(s) result = 0 flag = 0 for i in range(len(res)): if x[i] > res[i]: result = max(result, (x[i] + res[i] + 2) // 2) else: flag = 1 break if flag == 0: print(result) else: print("-1")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
numOfTowers, numOfOperations = map(int, input().split()) heights = list(map(int, input().split())) l = [] for i in range(numOfOperations): max_index = heights.index(max(heights)) min_index = heights.index(min(heights)) if min_index != max_index: heights[max_index] -= 1 heights[min_index] += 1 l.append([max_index + 1, min_index + 1]) print(max(heights) - min(heights), len(l)) for j in l: print(" ".join(map(str, j)))
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 LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) a = list(map(int, input().split())) d = [[a[i], i] for i in range(n)] d.sort() cnt = 0 ops = [] while d[n - 1][0] - d[0][0] > 1 and cnt < k: ops.append([d[n - 1][1] + 1, d[0][1] + 1]) d[n - 1][0] -= 1 d[0][0] += 1 cnt += 1 d.sort() print(d[n - 1][0] - d[0][0], cnt) for f, t in ops: print(f, t)
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 LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
a, b = map(int, input().split()) l = list(map(int, input().split())) count = 0 arr = [] for i in range(b): minimum = min(l) maximum = max(l) if maximum - minimum <= 1: break minindex = l.index(minimum) maxindex = l.index(maximum) l[minindex] += 1 l[maxindex] -= 1 arr.append((maxindex + 1, minindex + 1)) print(max(l) - min(l), len(arr)) print("\n".join(" ".join(map(str, i)) for i in arr))
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 NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) a = list(map(int, input().split())) actions = [] for i in range(k): max_index = min_index = 0 max_value = min_value = a[0] for j, value in enumerate(a): if value > max_value: max_index = j max_value = value elif value < min_value: min_index = j min_value = value if max_value == min_value: break actions.append((max_index, min_index)) a[max_index] -= 1 a[min_index] += 1 print(max(a) - min(a), len(actions)) for i, j in actions: print(i + 1, j + 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 ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) l = list(map(int, input().split())) gap, res = 10000, [] while 1: i, j = (f(range(n), key=l.__getitem__) for f in (min, max)) if gap > l[j] - l[i]: gap = l[j] - l[i] top = len(res) if gap < 2: break if len(res) == k: break l[i] += 1 l[j] -= 1 res.append(" ".join((str(j + 1), str(i + 1)))) print(max(l) - min(l), top) print("\n".join(res[:top]))
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 VAR NUMBER LIST WHILE NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
import sys input = sys.stdin.readline def multi_input(): return map(int, input().split()) def array_print(arr): print(" ".join(map(str, arr))) n, k = multi_input() a = list(multi_input()) m = 0 ans = [] diff = sys.maxsize while k > 0 and max(a) != min(a): max1 = a.index(max(a)) min1 = a.index(min(a)) diff = max(a) - min(a) if diff != 1: int_min = min1 a[max1] -= 1 a[min1] += 1 ans.append([max1 + 1, min1 + 1]) k -= 1 m += 1 else: break print(max(a) - min(a), m) for i in range(m): array_print(ans[i])
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) a = list(map(int, input().split())) ans, rest = [], [] while k > 0: maxa = max(a) mina = min(a) res = maxa - mina if res <= 1: break maxi = a.index(maxa) mini = a.index(mina) a[maxi] -= 1 a[mini] += 1 ans.append(maxi + 1) rest.append(mini + 1) k -= 1 print(max(a) - min(a), len(ans)) for i in range(len(ans)): print(ans[i], rest[i])
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 VAR LIST LIST WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) a = list(map(int, input().split())) ans = list() cnt = 0 while cnt < k: i = a.index(max(a)) j = a.index(min(a)) if i == j: break ans.append((i + 1, j + 1)) a[i] -= 1 a[j] += 1 cnt += 1 stability = max(a) - min(a) print(stability, cnt) for i, j in ans: print(i, j)
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 ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) l = list(map(int, input().split())) if n == 1: print(0, 0) exit() l1 = [[l[i], i + 1] for i in range(n)] k1 = k opa = [] while k1 > 0: l1 = sorted(l1, key=lambda x: x[0]) if abs(l1[0][0] - l1[-1][0]) <= 1: break opa.append(str(l1[-1][1]) + " " + str(l1[0][1])) l1[0][0] += 1 l1[-1][0] -= 1 k1 -= 1 l1 = sorted(l1, key=lambda x: x[0]) print(l1[-1][0] - l1[0][0], k - k1) for x in opa: print(x)
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 VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER STRING FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
def inp(s): j = 0 a = [] for i in range(len(s)): if s[i] == " ": a.append(int(s[j:i])) j = i + 1 if i == len(s) - 1: a.append(int(s[j:])) return a def ins(a): return max(a) - min(a) n, k = input().split() k = int(k) s = input() a = inp(s) i = 0 r = [] while i < k and ins(a) > 1: mx = max(a) mn = min(a) p1 = a.index(mx) p2 = a.index(mn) a[p1] = a[p1] - 1 a[p2] = a[p2] + 1 r.append((p1 + 1, p2 + 1)) i = i + 1 print(ins(a), i) for i in r: print(i[0], i[1])
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) l = list(map(int, input().split())) diff = max(l) - min(l) if diff == 0: print(0, 0) elif diff == 1: print(1, 0) else: s = 0 ans = [] while s < k: x = l.index(min(l)) y = l.index(max(l)) if max(l) - min(l) <= 1: break else: s += 1 l[x] += 1 l[y] -= 1 ans.append([y + 1, x + 1]) print(max(l) - min(l), s) for i in range(len(ans)): print(*ans[i])
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 BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
def main(): n, k = map(int, input().split()) arr = list(map(int, input().split())) arr1 = [] for i in range(n): arr1.append([arr[i], i]) arr = arr1[:] ans = [] arr.sort() min_val = arr[0][0] max_val = arr[-1][0] while max_val > min_val and k > 0: arr[0][0] += 1 arr[-1][0] -= 1 ans.append((arr[-1][1] + 1, arr[0][1] + 1)) arr.sort() min_val = arr[0][0] max_val = arr[-1][0] k -= 1 print(max_val - min_val, len(ans)) for i in ans: print(i[0], i[1]) main()
FUNC_DEF 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 LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
infinity = 10**30 def find_extreme_towers(towers): min_tower, max_tower = 0, 0 for tower in range(len(towers)): if towers[tower] < towers[min_tower]: min_tower = tower if towers[tower] > towers[max_tower]: max_tower = tower return min_tower, max_tower def compute_instability(towers): min_tower, max_tower = find_extreme_towers(towers) return towers[max_tower] - towers[min_tower] nb_towers, nb_operations = map(int, input().split()) towers = list(map(int, input().split())) min_instability = compute_instability(towers) operations = [] nb_operations_min = 0 for operation in range(nb_operations + 1): cur_instability = compute_instability(towers) if cur_instability < min_instability: min_instability = cur_instability nb_operations_min = operation if operation < nb_operations: min_tower, max_tower = find_extreme_towers(towers) operations.append((max_tower + 1, min_tower + 1)) towers[min_tower] += 1 towers[max_tower] -= 1 print(min_instability, nb_operations_min, sep=" ") for operation in range(nb_operations_min): print(operations[operation][0], operations[operation][1], sep=" ")
ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR VAR 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 VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER STRING
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, m = map(int, input().split()) l = list(map(int, input().split()))[:n] g = [] f = [] cnt = 0 while max(l) - min(l) > 1: cnt += 1 if cnt > m: cnt -= 1 break b = max(l) c = min(l) g.append(l.index(b) + 1) f.append(l.index(c) + 1) l[l.index(b)] -= 1 l[l.index(c)] += 1 print(max(l) - min(l), cnt) for i in range(cnt): print(g[i], f[i])
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 VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
from sys import stdin, stdout def get(): return stdin.readline().strip() def getf(): return [int(i) for i in get().split()] def put(a, end="\n"): stdout.write(str(a) + end) def putf(a, sep=" ", end="\n"): stdout.write(sep.join(map(str, a)) + end) def minMaxIx(a, n): mn = 0 mx = 0 for i in range(1, n): if a[i] > a[mx]: mx = i if a[i] < a[mn]: mn = i return mn, mx def main(): n, k = getf() a = getf() p = 0 ps = [] while True: x, y = minMaxIx(a, n) if x == y or k == 0: break else: a[y] -= 1 a[x] += 1 ps += [[y + 1, x + 1]] k -= 1 p += 1 x, y = minMaxIx(a, n) putf([a[y] - a[x], p]) [putf(x) for x in ps] main()
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_DEF STRING STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR LIST LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
v = list(input().split()) n = int(v[0]) k = int(v[1]) v = list(int(x) for x in input().split()) dol = list() dolc = 0 s = 0 for i in range(k): minv = min(v) maxv = max(v) minvi = v.index(minv) maxvi = v.index(maxv) if maxv - minv == 0: break if maxv - minv == 1: if s == 1: break else: s = 1 v[minvi] += 1 v[maxvi] -= 1 dol.append(str(maxvi + 1) + " " + str(minvi + 1)) dolc += 1 if dolc == 0: print("0 0") else: print(max(v) - min(v), dolc) for i in range(dolc): print(dol[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
def test(lst): for i in range(len(lst) - 1): if lst[i] != lst[i + 1]: return True return False n, k = list(map(int, input().split())) a = list(map(int, input().split())) p = [] m = 0 tst = test(a) s = max(a) - min(a) while k > 0 and tst and s > 1: i = a.index(max(a)) j = a.index(min(a)) a[i], a[j] = a[i] - 1, a[j] + 1 p.append([i + 1, j + 1]) m += 1 tst = test(a) k -= 1 s = max(a) - min(a) print(s, m) for i in p: print(*i)
FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR 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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = input().split() list_ = input() mass = [] result = [] s = 0 n = int(n) k = int(k) k_1 = k summ = "" i = 0 while i < len(list_): if list_[i] == " ": mass.append(int(summ)) summ = "" else: summ = summ + list_[i] if i == len(list_) - 1: mass.append(int(summ)) i = i + 1 i = 0 index_max = 0 index_men = 0 while i < k: max = mass[0] men = mass[0] for j in range(len(mass)): if mass[j] >= max: max = mass[j] index_max = j if mass[j] <= men: men = mass[j] index_men = j if max - men <= 1: break else: mass[index_max] = mass[index_max] - 1 mass[index_men] = mass[index_men] + 1 result.append(index_max + 1) result.append(index_men + 1) k = k - 1 max = mass[0] men = mass[0] for j in range(len(mass)): if mass[j] > max: max = mass[j] index_max = j if mass[j] < men: men = mass[j] index_men = j s = max - men k_1 = k_1 - k print(s, k_1) while True: if len(result) > 0: print(result.pop(0), result.pop(0)) else: break
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) towers = list(map(int, input().split())) operations = [] instability = 10000 operation = 0 for _ in range(k): minn, maxx = min(towers), max(towers) min_index = towers.index(minn) max_index = towers.index(maxx) towers[min_index] += 1 towers[max_index] -= 1 minn, maxx = min(towers), max(towers) val = maxx - minn if val <= instability and min_index != max_index: instability = val operation += 1 operations.append((max_index + 1, min_index + 1)) if instability == 10000: instability = 0 print(instability, operation) for i in operations: print(*i)
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 LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
tower, move = map(int, input().split()) height = list(map(int, input().split())) m = max(height) n = min(height) difference = m - n indexMax = -1 indexMin = -1 for i in range(tower): if m == height[i]: indexMax = i if n == height[i]: indexMin = i chances = [] counter = 0 while move != 0 and difference != 0 and difference != 1: move -= 1 counter += 1 height[indexMax] -= 1 height[indexMin] += 1 chances.append(indexMax + 1) chances.append(indexMin + 1) m = max(height) n = min(height) indexMax = -1 indexMin = -1 for i in range(tower): if m == height[i]: indexMax = i if n == height[i]: indexMin = i difference = m - n print(difference, counter) for i in range(0, 2 * counter, 2): print(chances[i], chances[i + 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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) arr = list(map(int, input().split())) lst = [] j = count = 0 check = -1 while j < k: mini = 10**4 + 3 maxi = 0 for i in range(n): if maxi < arr[i]: max_index = i maxi = arr[i] if mini > arr[i]: min_index = i mini = arr[i] if maxi - mini > 1: count += 1 arr[max_index] -= 1 arr[min_index] += 1 lst.append([max_index + 1, min_index + 1]) else: break j += 1 mini = 10**4 + 3 maxi = 0 for i in range(n): if maxi < arr[i]: maxi = arr[i] if mini > arr[i]: mini = arr[i] print(maxi - mini, count) for j in lst: print(*j)
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 LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = (int(x) for x in input().split()) a = [int(x) for x in input().split()] output = [] instability, moves = max(a) - min(a), 0 while moves < k: high, low = max(a), min(a) h, l = a.index(max(a)), a.index(min(a)) if h == l: break a[h], a[l] = a[h] - 1, a[l] + 1 moves += 1 output.append((h + 1, l + 1)) instability = max(a) - min(a) print(instability, moves) for p in output: print(*(x for x in p))
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 ASSIGN VAR LIST ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
f = list(map(int, input().split())) heights = list(map(int, input().split())) num = f[0] kops = f[1] minpossible = 0 if sum(heights) % num == 0 else 1 curinstability = max(heights) - min(heights) opscounter = 0 rem = list() while curinstability > minpossible and opscounter < kops: lowi = heights.index(min(heights)) highi = heights.index(max(heights)) heights[lowi] += 1 heights[highi] -= 1 rem.append((highi + 1, lowi + 1)) opscounter += 1 curinstability = max(heights) - min(heights) print(max(heights) - min(heights), opscounter) for vals in rem: print(vals[0], vals[1])
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 NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) kb = k a = [[i + 1, e] for i, e in enumerate(map(int, input().split()))] ans = [] a = sorted(a, key=lambda x: x[1], reverse=True) while k > 0: if a[0][1] - a[-1][1] > 1: ans.append([a[0][0], a[-1][0]]) a[0][1] -= 1 a[-1][1] += 1 a = sorted(a, key=lambda x: x[1], reverse=True) k -= 1 else: break print(a[0][1] - a[-1][1], kb - k) for e in ans: print(e[0], e[1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) a, risp = [], [] a = list(map(int, input().split())) s = max(a) - min(a) m = 0 if s < 2: print(s, 0) else: for x in range(k): maxi = max(a) IndMaxi = a.index(maxi) mini = min(a) IndMini = a.index(mini) a[IndMaxi] -= 1 a[IndMini] += 1 s = max(a) - min(a) m += 1 risp.append([IndMaxi + 1, IndMini + 1]) if s == 0: break print(s, m) for x in range(len(risp)): print(*risp[x])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
towers, operationsNum = map(int, input().split()) towersHeights = list(map(int, input().split())) changes = [] for i in range(operationsNum): minH, maxH = min(towersHeights), max(towersHeights) instability = max(towersHeights) - min(towersHeights) minindex, maxindex = towersHeights.index(minH), towersHeights.index(maxH) if instability != 0: changes.append(str(maxindex + 1) + " " + str(minindex + 1)) towersHeights[minindex] += 1 towersHeights[maxindex] -= 1 print(max(towersHeights) - min(towersHeights), len(changes)) for i in changes: print(i)
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 LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = [int(x) for x in input().split()] m = [int(x) for x in input().split()] a = 1 b = max(m) c = min(m) w = [] while a <= k and b > c: p = m.index(b) q = m.index(c) w.append([p + 1, q + 1]) m[p] -= 1 m[q] += 1 b = max(m) c = min(m) a += 1 print(b - c, end=" ") print(a - 1) for c in w: print(" ".join(map(str, c)))
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 ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, x = map(int, input().split()) l = list(map(int, input().split())) ls = [] c = 0 for i in range(x): ma = max(l) mi = min(l) if ma - mi > 1: mai = l.index(ma) mii = l.index(mi) l[mai] = l[mai] - 1 l[mii] = l[mii] + 1 ls.append([mai + 1, mii + 1]) c = c + 1 else: break q = max(l) w = min(l) print(q - w, c) for e in ls: print(*e)
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 LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
inpt = input().split(" ") n = int(inpt[0]) k = int(inpt[1]) i_lst = [] j_lst = [] m = 0 inpt = input().split(" ") for i in range(len(inpt)): inpt[i] = int(inpt[i]) for i in range(k): mn = min(inpt) mx = max(inpt) if mn != mx: i_mn = inpt.index(mn) i_mx = inpt.index(mx) inpt[i_mn] += 1 inpt[i_mx] -= 1 i_lst.append(i_mx) j_lst.append(i_mn) m += 1 else: break instblity = max(inpt) - min(inpt) print(str(instblity) + " " + str(m)) for i in range(len(i_lst)): print(str(i_lst[i] + 1) + " " + str(j_lst[i] + 1))
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING FUNC_CALL VAR BIN_OP VAR VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) a = list(map(int, input().split())) s = max(a) - min(a) answer = [] m = 0 max_s = max(a) - min(a) if max_s != 0: while k != 0: if max_s >= max(a) - min(a): max_s = max(a) - min(a) else: break max_a = a.index(max(a)) min_a = a.index(min(a)) if max_a == min_a: break answer.append((max_a + 1, min_a + 1)) a[max_a] -= 1 a[min_a] += 1 m += 1 s = max(a) - min(a) k -= 1 print(s, m) for i in answer: print(i[0], i[1]) else: print(0, 0)
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 BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER WHILE VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) a = list(map(int, input().split())) ans = 0 l = [] r = [] f = True for h in range(k): mi = a[0] ma = a[0] ai = 0 aj = 0 for i in range(len(a)): if a[i] < mi: mi = a[i] ai = i if a[i] > ma: ma = a[i] aj = i if ma - mi <= 1: mi = a[0] ma = a[0] for i in range(len(a)): if a[i] < mi: mi = a[i] if a[i] > ma: ma = a[i] print(ma - mi, ans) for i in range(len(l)): print(l[i], r[i]) f = False break else: a[ai] += 1 a[aj] -= 1 ans += 1 l.append(aj + 1) r.append(ai + 1) if f == True: mi = a[0] ma = a[0] for i in range(len(a)): if a[i] < mi: mi = a[i] if a[i] > ma: ma = a[i] print(ma - mi, ans) for i in range(len(l)): print(l[i], r[i])
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 NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
f = input() s = input() a = f.split(" ") n = int(a[0]) k = int(a[1]) a = s.split(" ") i = 0 while i < n: a[i] = int(a[i]) i = i + 1 ans = -1 v1 = list() v2 = list() while k > 0: posmn = -1 posmx = -1 mn = 2**16 mx = -1 i = 0 while i < n: if mn > a[i]: mn = a[i] posmn = i if mx < a[i]: mx = a[i] posmx = i i = i + 1 if mx == mn: ans = 0 break v1.append(posmx + 1) v2.append(posmn + 1) a[posmx] = a[posmx] - 1 a[posmn] = a[posmn] + 1 k = k - 1 i = 0 mx = -1 mn = 2**16 while i < n: mn = min(mn, a[i]) mx = max(mx, a[i]) i = i + 1 ans = mx - mn print(str(ans) + " " + str(len(v1))) i = 0 while i < len(v1): print(str(v1[i]) + " " + str(v2[i])) i = i + 1
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
import sys input = sys.stdin.readline def update_front(n, arr): for i in range(n - 1): if arr[i][0] > arr[i + 1][0]: arr[i], arr[i + 1] = arr[i + 1], arr[i] else: break def update_back(n, arr): for i in reversed(range(n - 1)): if arr[i][0] > arr[i + 1][0]: arr[i], arr[i + 1] = arr[i + 1], arr[i] else: break def get_min(score): mn = float("inf") index = None for i, s in enumerate(score): if s < mn: index = i mn = s return index n, k = map(int, input().split()) arr = [[int(v), i] for i, v in enumerate(input().split(), start=1)] arr.sort() score = [] ops = [] pre = arr[-1][0] - arr[0][0] for _ in range(k): arr[-1][0] -= 1 arr[0][0] += 1 ops.append((arr[-1][1], arr[0][1])) update_front(n, arr) update_back(n, arr) score.append(arr[-1][0] - arr[0][0]) j = get_min(score) if score[j] < pre: print(score[j], j + 1) for i in range(j + 1): print(*ops[i]) else: print(pre, 0)
IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) a = list(map(int, input().split())) ans = [] while k > 0: mn = min(a) mx = max(a) if mn + 1 >= mx: break for i in range(n): if a[i] == mn: a[i] += 1 ans.append(i) break for i in range(n): if a[i] == mx: a[i] -= 1 ans[-1] = ans[-1], i break k -= 1 print(max(a) - min(a), len(ans)) for a, b in ans: print(b + 1, a + 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 ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
a, b = map(int, input().split(" ")) towers = list(map(int, input().split(" "))) tow2 = [[towers[i], i + 1] for i in range(len(towers))] tot = sum(towers) should = tot // a num1 = (should + 1) * a - tot num2 = a - num1 ideal = num1 * [should] + num2 * [should + 1] ct = 1 x = [] while ct <= b: tow2.sort(key=lambda x: x[0]) if [i[0] for i in tow2] == ideal: break else: x.append([tow2[-1][1], tow2[0][1]]) tow2[0][0] += 1 tow2[-1][0] -= 1 ct += 1 tows = [x[0] for x in tow2] print(max(tows) - min(tows), ct - 1) for i in x: print(i[0], i[1])
ASSIGN VAR 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 VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR LIST VAR BIN_OP VAR LIST BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) p = k a = list(map(int, input().split())) c = list() while k > 0 and max(a) - min(a) > 1: c.append(a.index(max(a)) + 1) c.append(a.index(min(a)) + 1) a[a.index(max(a))] -= 1 a[a.index(min(a))] += 1 k -= 1 print(max(a) - min(a), p - k) i = 0 while len(c) > i: print(c[i], c[i + 1]) i += 2
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
def main(): n, k = map(int, input().split()) towers = list(map(int, input().split())) min_index = towers.index(min(towers)) max_index = towers.index(max(towers)) dif = towers[max_index] - towers[min_index] cant = 0 operations = [] while k > 0 and dif > 0: if min_index == max_index: break towers[max_index] -= 1 towers[min_index] += 1 operations.append("{0} {1}".format(max_index + 1, min_index + 1)) cant += 1 min_index = towers.index(min(towers)) max_index = towers.index(max(towers)) dif = towers[max_index] - towers[min_index] k -= 1 print("{0} {1}".format(dif, cant)) for i in operations: print(i) main()
FUNC_DEF 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 VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) l = list(map(int, input().split())) ans = [] for i in range(n): ans.append([l[i], i + 1]) ans.sort(key=lambda x: (x[0], x[1])) s = 0 fns = [] while s != k: diff = ans[-1][0] - ans[0][0] if diff == 0: break s += 1 ans[-1][0] -= 1 ans[0][0] += 1 fns.append([ans[-1][1], ans[0][1]]) if diff == 0: break ans.sort(key=lambda x: (x[0], x[1])) print(ans[-1][0] - ans[0][0], len(fns)) for i in fns: print(*i)
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 LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) x = list(map(int, input().split())) oldins = 0 ins = max(x) - min(x) diff = 0 order = [] while max(x) != min(x) and max(x) != min(x) + 1 and diff < k: a = x.index(min(x)) b = x.index(max(x)) order.append([b + 1, a + 1]) x[x.index(min(x))] += 1 x[x.index(max(x))] -= 1 diff += 1 ins = max(x) - min(x) print(ins, diff) for i in range(diff): print(order[i][0], order[i][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 ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
a, b = map(int, input().split()) k1 = [] k2 = [] c = list(map(int, input().split())) lol = max(c) - min(c) if a == 1: print(0, 0) else: for i in range(b): mix = -1 man = 10000000000000 for j in range(a): if c[j] >= mix: mix = c[j] p1 = j if c[j] < man: man = c[j] p2 = j if mix == man: break c[p2] += 1 c[p1] -= 1 k1.append(p1 + 1) k2.append(p2 + 1) mix = -1 man = 10000000000000 for j in range(a): if c[j] >= mix: mix = c[j] if c[j] < man: man = c[j] print(mix - man, len(k1)) for i in range(len(k1)): print(k1[i], k2[i])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) a = list(zip(list(map(int, input().split())), [i for i in range(1, n + 1)])) a.sort() add = [0] * n ans = [] m = 0 while k > 0 and a[0][0] + 1 + add[0] < a[-1][0] + add[-1]: l = 0 while a[l + 1][0] + add[l + 1] == a[l][0] + add[l]: l += 1 r = n - 1 while a[r][0] + add[r] == a[r - 1][0] + add[r - 1]: r -= 1 add[r] -= 1 add[l] += 1 k -= 1 m += 1 ans.append([a[r][1], a[l][1]]) print(a[-1][0] + add[-1] - (a[0][0] + add[0]), m) for i in range(m): print(ans[i][0], ans[i][1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) l = list(map(int, input().split())) num_position = [[el, index] for index, el in enumerate(l)] current_unstability = abs(max(num_position)[0] - min(num_position)[0]) min_unstability = 10 * 5 res = [] nb_step = 0 while k > 0: index_max, index_min = max(num_position)[1], min(num_position)[1] if num_position[index_max][0] - num_position[index_min][0] < 2: break num_position[index_max][0] -= 1 num_position[index_min][0] += 1 current_unstability = abs(num_position[index_max][0] - num_position[index_min][0]) res.append((index_max + 1, index_min + 1)) min_unstability = min(min_unstability, current_unstability) nb_step += 1 k -= 1 print(abs(max(num_position)[0] - min(num_position)[0]), nb_step) for e in res: print(e[0], e[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 ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
line = input("") line = line.split() amount = int(line[0]) movess = int(line[1]) towers = input("") towers = towers.split() towers = list(map(int, towers)) count = 0 insta = max(towers) - min(towers) moves = [] instaCount = 1 for i in range(movess): if insta == 1 or insta == 0: break else: moves.append([towers.index(max(towers)) + 1, towers.index(min(towers)) + 1]) towers[towers.index(max(towers))] -= 1 towers[towers.index(min(towers))] += 1 count += 1 if max(towers) - min(towers) == insta: instaCount = 2 pass else: instaCount = 1 insta = max(towers) - min(towers) print(str(insta) + " " + str(count)) for n in moves: print(str(n[0]) + " " + str(n[1]))
ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) towers = list(map(int, input().split())) stow = sorted([[towers[i], i] for i in range(0, n)], key=lambda k: k[0]) operations = [] for i in range(0, k): stow = sorted(stow, key=lambda k: k[0]) if stow[n - 1][0] <= stow[0][0] + 1: break stow[n - 1][0] -= 1 stow[0][0] += 1 operations.append([stow[n - 1][1] + 1, stow[0][1] + 1]) stow = sorted(stow, key=lambda k: k[0]) max_ins = stow[n - 1][0] - stow[0][0] print(max_ins, len(operations)) for op in operations: print(*op)
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 LIST VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) li = list(map(int, input().split())) nextli = [] alist = sorted(li) s = 0 r = max(alist) - min(alist) while True: m = li.index(max(alist)) p = li.index(min(alist)) li[m] -= 1 li[p] += 1 blist = sorted(li) l = max(alist) - min(alist) if blist == alist or s >= k or l > r: r = l break else: r = l nextli.append(m) nextli.append(p) alist = blist s += 1 print(r, s) i = 0 while True: if i >= len(nextli): break print(nextli[i] + 1, nextli[i + 1] + 1) i = i + 2
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 LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) a = list(map(int, input().split())) ans = [] for i in range(k): mi = 10**10 for j in range(n): if a[j] < mi: mi = a[j] pos_mi = j ma = -(10**10) for j in range(n): if a[j] > ma: ma = a[j] pos_ma = j if ma <= mi + 1: break ans.append([pos_ma + 1, pos_mi + 1]) a[pos_ma] -= 1 a[pos_mi] += 1 print(max(a) - min(a), len(ans)) for i in range(len(ans)): print(*ans[i])
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 LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) heights = [] total = 0 i = 1 for item in map(int, input().split()): heights.append([item, i]) total += item i += 1 heights.sort() middle = total // n if total % n != 0: middle += 1 most_h = heights[n - 1][0] min_h = heights[0][0] up_ind = n - 1 down_ind = 0 level_down = heights[down_ind][0] level_up = heights[up_ind][0] i = 0 action_list = [] while i < k: if heights[down_ind][0] < middle and heights[up_ind][0] > middle: if heights[down_ind][0] < heights[0][0] or down_ind == 0: heights[down_ind][0] += 1 action_list.append([heights[up_ind][1], heights[down_ind][1]]) if heights[up_ind][0] > heights[n - 1][0] or up_ind == n - 1: heights[up_ind][0] -= 1 if heights[up_ind][0] < heights[up_ind - 1][0]: up_ind -= 1 elif heights[up_ind][0] == heights[n - 1][0]: up_ind = n - 1 else: up_ind = n - 1 if heights[down_ind][0] > heights[down_ind + 1][0]: down_ind += 1 elif heights[down_ind][0] == heights[0][0]: down_ind = 0 else: down_ind = 0 elif heights[up_ind][0] - heights[down_ind][0] > 1: if heights[up_ind][0] == middle: middle -= 1 elif heights[down_ind][0] == middle: middle += 1 i += 1 print(heights[up_ind][0] - heights[down_ind][0], len(action_list)) for action in action_list: print(*action)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = list(map(int, input().split(" "))) t = list(map(int, input().split(" "))) inst = max(t) - min(t) i = 0 steps = [] while inst > 0 and i < k: imax = t.index(max(t)) imin = t.index(min(t)) steps.append(imax + 1) steps.append(imin + 1) t[imax] -= 1 t[imin] += 1 inst = max(t) - min(t) i += 1 print(inst, i) for i in range(0, len(steps), 2): print(steps[i], steps[i + 1])
ASSIGN VAR 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 BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
s, m = map(int, input().split()) ls = [int(u) for u in input().split()] ls_copy = ls.copy() k = 0 operations = [] for _ in range(0, m): if max(ls) == min(ls): break else: k += 1 operations.append([ls.index(max(ls)) + 1, ls.index(min(ls)) + 1]) ls[ls.index(max(ls))] -= 1 ls[ls.index(min(ls))] += 1 instab = max(ls) - min(ls) operations = [] k = 0 for _ in range(0, m): if max(ls_copy) - min(ls_copy) == instab: break else: k += 1 operations.append( [ls_copy.index(max(ls_copy)) + 1, ls_copy.index(min(ls_copy)) + 1] ) ls_copy[ls_copy.index(max(ls_copy))] -= 1 ls_copy[ls_copy.index(min(ls_copy))] += 1 instab = max(ls_copy) - min(ls_copy) print(instab, k) for elements in operations: print(elements[0], elements[1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) s, i, j = "", 0, 10000 t = [[] for x in range(j + 1)] for y, x in enumerate(map(int, input().split()), 1): t[x].append(str(y)) while not t[i]: i += 1 while not t[j]: j -= 1 while k and i < j: a, b = t[j].pop(), t[i].pop() s += "\n" + a + " " + b t[j - 1].append(a) t[i + 1].append(b) k -= 1 if not t[i]: i += 1 if not t[j]: j -= 1 print(str(j - i) + " " + str(s.count(" ")) + s)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR STRING NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP STRING VAR STRING VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR STRING FUNC_CALL VAR FUNC_CALL VAR STRING VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) h = list(map(int, input().split())) val = max(h) - min(h) step = 0 move = [] for t in range(k): i = h.index(max(h)) j = h.index(min(h)) h[i] -= 1 h[j] += 1 move.append((i + 1, j + 1)) newval = max(h) - min(h) if val > newval: step = t + 1 val = newval print(val, step) for i in range(step): ii, jj = move[i] print(ii, jj)
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 BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = input().split() n = int(n) k = int(k) a = [int(i) for i in input().split()] thres = round(sum(a) / n) pos = [] early = False for i in range(k): low = a[0] l_pos = 0 high = a[0] h_pos = 0 for j in range(1, n): if a[j] > high: high = a[j] h_pos = j elif a[j] < low: low = a[j] l_pos = j if low >= thres or high <= thres: early = True break else: a[l_pos] += 1 a[h_pos] -= 1 pos.append([h_pos + 1, l_pos + 1]) if early: i -= 1 print(max(a) - min(a), i + 1) for i in range(len(pos)): print(pos[i][0], pos[i][1])
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) a = list(map(int, input().split())) ans = [] for i in range(n): a[i] = [a[i], i] a.sort() i = -1 if a[-1][0] - a[0][0] == 1 or a[-1][0] - a[0][0] == 0: k = 0 for i in range(k): a[-1][0] -= 1 a[0][0] += 1 ans += [[a[-1][1] + 1, a[0][1] + 1]] if a[0][0] > a[1][0] or a[-1][0] < a[-2][0]: a.sort() if a[-1][0] - a[0][0] == 1 or a[-1][0] - a[0][0] == 0: break a.sort() print(a[-1][0] - a[0][0], end=" ") print(i + 1) for item in ans: print(" ".join(map(str, item)))
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 LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR LIST LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
l = input().split() n = int(l[0]) k = int(l[1]) a = input().split() ans = [] for i in range(len(a)): a[i] = int(a[i]) b = [] for i in range(len(a)): b.append(a[i]) ms = max(a) - min(a) op = 0 dum = 1 while k > 0 and dum == 1: k -= 1 ma = a.index(max(a)) a[ma] -= 1 mi = a.index(min(a)) a[mi] += 1 if sorted(a) != sorted(b): op += 1 ms = max(a) - min(a) b[ma] -= 1 b[mi] += 1 ans.append([ma + 1, mi + 1]) else: dum = 0 print(ms, op) for i in range(len(ans)): print(ans[i][0], ans[i][1])
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) heights = list(map(int, input().split())) stabilities = [max(heights) - min(heights)] pos = [(0, 0)] for i in range(k): higher = heights.index(max(heights)) lower = heights.index(min(heights)) heights[higher] -= 1 heights[lower] += 1 stabilities.append(max(heights) - min(heights)) pos.append((higher + 1, lower + 1)) smallest = min(stabilities) operations = stabilities.index(smallest) print(smallest, operations) for i in range(operations): print(*pos[i + 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 ASSIGN VAR LIST BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
from sys import stdin, stdout outputs = [] n, m = (int(num) for num in stdin.readline().strip().split()) arr = [int(num) for num in stdin.readline().strip().split()] for i in range(n): arr[i] = [arr[i], i + 1] arr = sorted(arr, key=lambda t: t[0]) res = [] diff = arr[-1][0] - arr[0][0] while True and m > 0: if diff == 0: break else: i, j = 0, n - 1 res.append(f"{arr[-1][1]} {arr[0][1]}") arr[0][0] += 1 arr[-1][0] -= 1 arr = sorted(arr, key=lambda t: t[0]) m -= 1 diff = arr[-1][0] - arr[0][0] stdout.write(f"{diff} {len(res)}\n") stdout.write("\n".join(res))
ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER WHILE NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER STRING VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = [int(x) for x in input().split()] towers = list(map(int, input().split())) prom = int(sum(towers) / n) actions = [] while k > 0: alto = towers.index(max(towers)) + 1 bajo = towers.index(min(towers)) + 1 if alto == bajo: break towers[alto - 1] -= 1 towers[bajo - 1] += 1 actions.append((alto, bajo)) k -= 1 alto = max(towers) bajo = min(towers) print(alto - bajo, len(actions)) for i in actions: print(i[0], i[1])
ASSIGN VAR VAR FUNC_CALL VAR 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 BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
import sys class Item: def __init__(self, i, v): self.i = i self.v = v def __repr__(self): return "%s %s" % (self.i, self.v) n, k = sys.stdin.readline().split() n = int(n) k = int(k) tower = sys.stdin.readline().split() for i in range(len(tower)): tower[i] = Item(i + 1, int(tower[i])) tower = sorted(tower, key=lambda t: t.v) steps = [] start = 0 end = len(tower) - 1 i = 0 while i < k: if tower[start].v < tower[end].v - 1: tower[start].v += 1 tower[end].v -= 1 step = "%s %s" % (tower[end].i, tower[start].i) steps.append(step) tower = sorted(tower, key=lambda t: t.v) i += 1 else: break print("%s %s" % (tower[end].v - tower[start].v, i)) for step in steps: print(step)
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP STRING VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP STRING VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) a = list(map(int, input().split())) s = "" c = 0 best = [max(a) - min(a), c, ""] if n > 1: for pog in range(k): m = 0 i = 0 mp = 100000000000000000 ip = 0 m = max(a) i = a.index(m) mp = min(a) ip = a.index(mp) if m != mp: a[i] -= 1 a[ip] += 1 s += str(i + 1) + " " + str(ip + 1) + "\n" c += 1 else: break if max(a) - min(a) < best[0]: best[0] = max(a) - min(a) best[1] = c best[2] = s print(best[0], best[1]) if len(best[2]): print(best[2], end="")
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 STRING ASSIGN VAR NUMBER ASSIGN VAR LIST BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR STRING IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
import sys tow = [int(x) for x in sys.stdin.readline().split()] initalSteps = tow[1] towData = [int(x) for x in sys.stdin.readline().split()] tower = [None] * len(towData) for idx in range(0, len(towData)): tower[idx] = [idx + 1, towData[idx]] tower = sorted(tower, key=lambda x: x[1]) result = "" while tow[1] > 0: if tower[0][1] >= tower[tow[0] - 1][1] - 1: break tower[0][1] += 1 tower[tow[0] - 1][1] -= 1 result += str(tower[tow[0] - 1][0]) + " " + str(tower[0][0]) + "\n" idx = 0 while tower[idx][1] > tower[idx + 1][1]: temp = tower[idx + 1] tower[idx + 1] = tower[idx] tower[idx] = temp idx += 1 idx = tow[0] - 1 while tower[idx][1] < tower[idx - 1][1]: temp = tower[idx - 1] tower[idx - 1] = tower[idx] tower[idx] = temp idx -= 1 tow[1] -= 1 print(tower[tow[0] - 1][1] - tower[0][1], initalSteps - tow[1]) print(result)
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR LIST BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR STRING WHILE VAR NUMBER NUMBER IF VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER STRING FUNC_CALL VAR VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER WHILE VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
def minmax(a, n): mn, mx, idx = 0, 0, 0 for idx in range(1, n): if a[idx] > a[mx]: mx = idx if a[idx] < a[mn]: mn = idx return [mx + 1, mn + 1] [n, k] = list(map(int, input().split())) a = list(map(int, input().split())) moves = [] oper = 0 while oper < k: loc = minmax(a, n) mx, mn = a[loc[0] - 1], a[loc[1] - 1] if mx == mn: break moves.append(loc) a[loc[0] - 1] -= 1 a[loc[1] - 1] += 1 oper += 1 print(max(a) - min(a), oper) for move in moves: print(move[0], move[1])
FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR RETURN LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN LIST VAR 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 VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
n, k = map(int, input().split()) t = list(map(int, input().split())) moves = [] best = 10000 bestl = 0 for l in range(k + 1): imin = min(range(len(t)), key=lambda i: t[i]) imax = max(range(len(t)), key=lambda i: t[i]) if best > t[imax] - t[imin]: best = t[imax] - t[imin] bestl = l if t[imin] == t[imax]: break moves.append((imax, imin)) t[imax] -= 1 t[imin] += 1 print(best, bestl) for i in range(bestl): x, y = moves[i] print(x + 1, y + 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 ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task. -----Input----- The first line contains two space-separated positive integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 ≀ a_{i} ≀ 10^4) β€” the towers' initial heights. -----Output----- In the first line print two space-separated non-negative integers s and m (m ≀ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i β‰  j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them. -----Examples----- Input 3 2 5 8 5 Output 0 2 2 1 2 3 Input 3 4 2 2 4 Output 1 1 3 2 Input 5 3 8 3 2 6 3 Output 3 3 1 3 1 2 1 3 -----Note----- In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
def mi(a): m = a[0] mi = 0 for i in range(len(a)): if a[i] < m: m = a[i] mi = i return mi def ma(a): m = a[0] mi = 0 for i in range(len(a)): if a[i] > m: m = a[i] mi = i return mi n, k = map(int, input().split()) towers = list(map(int, input().split())) rez = [] counter = 0 for i in range(k): maxx = ma(towers) minn = mi(towers) if maxx == minn or towers[maxx] - towers[minn] == 1: break towers[maxx] -= 1 towers[minn] += 1 rez.append([maxx, minn]) counter += 1 print(max(towers) - min(towers), counter) for a in rez: print(a[0] + 1, a[1] + 1)
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR 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 LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER