description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
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()) ops = 0 height = list(map(int, input().split())) ans = [] while ops < k: pos_lo, lo = min(enumerate(height), key=lambda x: x[1]) pos_hi, hi = max(enumerate(height), key=lambda x: x[1]) if hi == lo: break height[pos_lo] += 1 height[pos_hi] -= 1 ans.append((pos_hi + 1, pos_lo + 1)) ops += 1 print(max(height) - min(height), ops) print("\n".join(str(x) + " " + str(y) for x, y in ans))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP BIN_OP FUNC_CALL VAR VAR 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()) data = list(map(int, input().split())) position = [i for i in range(1, n + 1)] step = 0 result = [] for i in range(k): new_data = sorted(data) n_position = [x for _, x in sorted(zip(data, position))] if max(new_data) - min(new_data) > 1: new_data[0] += 1 new_data[-1] -= 1 data = new_data position = n_position result.append(n_position[-1]) result.append(n_position[0]) step = step + 1 else: break print(max(new_data) - min(new_data), step) for i in range(0, step * 2 - 1, 2): print(result[i], result[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 VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER 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, m = map(int, input().split()) b = list(map(int, input().split())) a = [] for i in range(n): a.append([b[i], i + 1]) i, count = 0, 0 c = [] while i < m: a.sort() if a[0][0] != a[-1][0] and a[0][0] + 1 != a[-1][0]: a[0][0] += 1 a[-1][0] -= 1 c.append([a[-1][1], a[0][1]]) a.sort() count += 1 elif a[0][0] == a[-1][0] or a[0][0] + 1 == a[-1][0]: break i += 1 print(a[-1][0] - a[0][0], count) for i in range(count): print(c[i][0], c[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 FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST WHILE VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER 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()) a = list(map(int, input().split())) a = [[a[x], x] for x in range(n)] a.sort() ans = [] while k > 0 and a[-1][0] != a[0][0]: a[-1][0] -= 1 a[0][0] += 1 k -= 1 ans.append((a[-1][1] + 1, a[0][1] + 1)) a.sort() print(a[-1][0] - a[0][0], len(ans)) for x in ans: 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 ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR 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()) bzz = [] cnt = 0 A = list(map(int, input().split())) for i in range(k): if max(A) - min(A) != 0: i1, i2 = 0, 0 ma, mi = 0, 10**9 for i in range(n): if A[i] > ma: ma = A[i] i1 = i if A[i] < mi: mi = A[i] i2 = i bzz.append([i1 + 1, i2 + 1]) A[i1] -= 1 A[i2] += 1 cnt += 1 print(max(A) - min(A), cnt) for i in bzz: print(*i)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER 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 EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR 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
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 def read(line): return [int(c) for c in line.strip().split()] def main(): test = sys.stdin.readlines() n, k = read(test[0]) towers = read(test[1]) towers_max = 10001 min_k = 0 ops = [] prev_diff = max(towers) - min(towers) min_diff = prev_diff for it in range(1, k + 1): mx, mx_i = -1, -1 mn, mn_i = towers_max, -1 for j, hj in enumerate(towers): if hj < mn: mn, mn_i = hj, j if hj > mx: mx, mx_i = hj, j towers[mx_i] -= 1 towers[mn_i] += 1 diff = max(towers) - min(towers) if diff < min_diff: min_diff = diff min_k = it if diff <= prev_diff: ops.append((mx_i + 1, mn_i + 1)) prev_diff = diff else: break print(min_diff, min_k) for i in range(min_k): print(*ops[i]) main()
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL 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.
n, k = map(int, input().split()) l = list(map(int, input().split())) count = 0 l1 = [] l2 = [] while count < k: x = l.index(max(l)) y = l.index(min(l)) if l[x] - l[y] == 1 or l[x] - l[y] == 0: break l1.append(x + 1) l2.append(y + 1) l[x] = l[x] - 1 l[y] = l[y] + 1 count = count + 1 ans = max(l) - min(l) print(ans, end=" ") print(count) for i in range(count): print(l1[i], end=" ") print(l2[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 WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR 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 BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING 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 = [int(v) for v in input().split()] a = [int(v) for v in input().split()] dd = dict() for i in range(n): if a[i] in dd: dd[a[i]].append(i) else: dd[a[i]] = [i] b = sorted(set(a)) mm = list(range(k + 1)) i = 0 move = [[0, 0]] * (k + 1) while i <= k - 1 and b[-1] - b[0] > 1: i = i + 1 move[i][0] = dd[b[-1]].pop() move[i][1] = dd[b[0]].pop() mm[i] = str(move[i][0] + 1) + " " + str(move[i][1] + 1) temp = b[-1] - 1 temp1 = temp if temp in dd: dd[temp].append(move[i][0]) else: dd[temp] = [move[i][0]] if dd[b[-1]] == []: b[-1] = temp temp = b[0] + 1 temp2 = temp if temp in dd: dd[temp].append(move[i][1]) else: dd[temp] = [move[i][1]] if dd[b[0]] == []: b[0] = temp b.append(temp1) b.append(temp2) b = sorted(set(b)) print(b[-1] - b[0], i) for j in range(1, i + 1): print(mm[j])
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 FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST LIST NUMBER NUMBER BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER STRING FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR LIST VAR VAR NUMBER IF VAR VAR NUMBER LIST ASSIGN VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR LIST VAR VAR NUMBER IF VAR VAR NUMBER LIST ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER 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.
class CodeforcesTask479BSolution: def __init__(self): self.result = "" self.n_k = [] self.heights = [] def read_input(self): self.n_k = [int(x) for x in input().split(" ")] self.heights = [int(x) for x in input().split(" ")] def process_task(self): res = [] for _ in range(self.n_k[1]): from_i = self.heights.index(max(self.heights)) to_i = self.heights.index(min(self.heights)) if from_i == to_i: break self.heights[from_i] -= 1 self.heights[to_i] += 1 res.append((from_i + 1, to_i + 1)) stab = max(self.heights) - min(self.heights) ops = "\n".join(" ".join([str(y) for y in x]) for x in res) self.result = f"{stab} {len(res)}\n{ops}" def get_result(self): return self.result Solution = CodeforcesTask479BSolution() Solution.read_input() Solution.process_task() print(Solution.get_result())
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER 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 BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR STRING FUNC_CALL VAR VAR STRING VAR FUNC_DEF RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR 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()) towers = list(map(int, input().split())) def get_min_max_indices(): tmp = towers[0] max_index = 0 min_index = 0 for i in range(1, len(towers)): if towers[i] > towers[max_index]: max_index = i if towers[i] < towers[min_index]: min_index = i return min_index, max_index tracks = [] for i in range(k): min_index, max_index = get_min_max_indices() if towers[min_index] == towers[max_index]: break towers[max_index] -= 1 towers[min_index] += 1 tracks.append([max_index + 1, min_index + 1]) lowest_point, highest_point = get_min_max_indices() print(towers[highest_point] - towers[lowest_point], len(tracks)) for record in tracks: print(*record)
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 FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR 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()) arr = list(map(int, input().split())) ans = [] op = 0 factor = max(arr) - min(arr) while factor > 1 and op < k: m = arr.index(min(arr)) M = arr.index(max(arr)) arr[m] += 1 arr[M] -= 1 op += 1 ans.append([M + 1, m + 1]) factor = max(arr) - min(arr) print(factor, op) for i in ans: 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 BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER 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 VAR NUMBER EXPR FUNC_CALL VAR LIST 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
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 = [int(i) for i in input().split()] ans = [] while k >= 0: x = 0 y = 0 for j in range(n): if arr[j] > arr[x]: x = j if arr[j] < arr[y]: y = j if k == 0 or arr[x] - arr[y] <= 1: print(arr[x] - arr[y], len(ans)) for j in ans: print(j[0], j[1]) break if arr[x] != arr[y]: ans.append((x + 1, y + 1)) arr[x] -= 1 arr[y] += 1 k -= 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 LIST WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER 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()) tab = [(0) for loop in range(n)] t = input().split() for i in range(n): tab[i] = int(t[i]) def max(): global n y = 0 j = 0 for i in range(n): if tab[i] > y: y = tab[i] j = i return j def min(): global n x = 100000 j = 0 for i in range(n): if tab[i] < x: x = tab[i] j = i return j def maxv(): global n y = 0 j = 0 for i in range(n): if tab[i] > y: y = tab[i] j = i return y def minv(): global n x = 100000 j = 0 for i in range(n): if tab[i] < x: x = tab[i] j = i return x liste = [(0, 0) for loop in range(k)] m = 0 v = maxv() - minv() for j in range(k): a = max() b = min() tab[a] -= 1 tab[b] += 1 liste[j] = a + 1, b + 1 c = maxv() - minv() if c < v: v = c m = j + 1 print(maxv() - minv(), m) for i in range(m): d, e = liste[i] print(d, e)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL 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 = map(int, input().split()) a = list(map(int, input().split())) b = list(sorted(([val, ind] for ind, val in enumerate(a, 1)), reverse=True)) t = 0 li = [] minm = b[0][0] - b[-1][0] for i in range(k): diff = b[0][0] - b[-1][0] if diff > 0 and diff <= minm: t += 1 b[0][0] -= 1 b[-1][0] += 1 li.append([b[0][1], b[-1][1]]) first = b[0] last = b[-1] b.sort(reverse=True) if minm == b[0][0] - b[-1][0] and b[0][1] == last[1] and b[-1][1] == first[1]: t -= 1 li.pop() break minm = b[0][0] - b[-1][0] else: break print(minm, t) for x in li: 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER 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.
n, k = map(int, input().split()) arr = list(map(int, input().split())) diff = [] ops = [] for i in range(k): x = arr.index(max(arr)) y = arr.index(min(arr)) if i == 0 and max(arr) - min(arr) == 0: diff.append(0) ops.append(None) else: arr[x] += -1 arr[y] += 1 ops.append((x + 1, y + 1)) diff.append(max(arr) - min(arr)) if max(arr) - min(arr) == 0: break if diff[0] == 0 and ops[0] == None: print(0, 0) else: fin_ind = diff.index(min(diff)) print(diff[fin_ind], fin_ind + 1) for i in range(fin_ind + 1): print(ops[i][0], ops[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 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 NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NONE 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 IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NONE EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL 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 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())) ret = [] for _ in range(k): minI, maxI = a.index(min(a)), a.index(max(a)) if a[maxI] - a[minI] <= 1: break a[maxI] -= 1 a[minI] += 1 ret.append("%d %d" % (maxI + 1, minI + 1)) print("%d %d\n%s" % (max(a) - min(a), len(ret), "\n".join(ret)))
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 FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR 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 = map(int, input().split()) a = [*map(int, input().split())] q = sum(a) q = q // n + (q % n != 0) l = [] m = 0 for i in range(k): if max(a) - min(a) <= 1: break w, e = a.index(max(a)), a.index(min(a)) a[w] -= 1 a[e] += 1 l += [[w + 1, e + 1]] m += 1 print(max(a) - min(a), m) for i in l: print(*i)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR LIST LIST BIN_OP VAR NUMBER BIN_OP VAR 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
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()) lst = list(map(int, input().split())) x = len(lst) for i in range(x): lst[i] = [lst[i], i] orig = k temp = 0 ans = [] while k > 0: k -= 1 lst.sort() if lst[n - 1][0] - lst[0][0] > 1: lst[n - 1][0] -= 1 lst[0][0] += 1 ans.append([lst[n - 1][1], lst[0][1]]) else: temp += 1 lst.sort() k += temp print(lst[n - 1][0] - lst[0][0], orig - k) for i in ans: print(i[0] + 1, i[1] + 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP 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())) ct = 0 moves = [] for i in range(k): ma = a.index(max(a)) mi = a.index(min(a)) if a[ma] - a[mi] > 1 and ma != mi: a[ma] -= 1 a[mi] += 1 moves.append([ma + 1, mi + 1]) else: break ct += 1 print(max(a) - min(a), ct) for i in moves: print(i[0], 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 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 BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR 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, o = [], [] for i, h in enumerate(map(int, input().split())): t.append([h, i + 1]) for i in range(k): t.sort() if t[0][0] >= t[-1][0] - 1: break t[0][0] += 1 t[-1][0] -= 1 o.append((t[-1][1], t[0][1])) t.sort() print(t[-1][0] - t[0][0], len(o)) for oi in o: print(oi[0], oi[1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER 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.
n, k = map(int, input().split()) l = [int(i) for i in input().split()] l1 = [[l[i], i + 1] for i in range(n)] l1.sort() ans = [] k1 = k if len(set(l)) == 1: print(0, 0) exit() while k: k -= 1 l1[0][0], l1[n - 1][0] = l1[0][0] + 1, l1[n - 1][0] - 1 ans.append([l1[n - 1][1], l1[0][1]]) l1.sort() if len(set(l1[i][0] for i in range(n))) == 1: break l = [l1[i][0] for i in range(n)] print(max(l) - min(l), end=" ") print(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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL 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()) a = list(map(int, input().split())) for i in range(n): a[i] = [a[i], i + 1] res = [] i = 0 if n > 1: for i in range(k): a.sort(reverse=True) if a[0][0] - 1 > a[-1][0]: res.append([a[0][1], a[-1][1]]) a[0][0] -= 1 a[-1][0] += 1 else: i -= 1 break a.sort(reverse=True) print(a[0][0] - a[-1][0], i + 1) for i in res: 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER 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 = list(map(int, input().split())) towers = list(map(int, input().split())) log_moves = [] def max(l): m = 0 for i, n in enumerate(l): if n > l[m]: m = i return m def min(l): m = 0 for i, n in enumerate(l): if n < l[m]: m = i return m mx = max(towers) mn = min(towers) min_k = 0 min_unst = towers[mx] - towers[mn] i = 0 while i < k: log_moves.append([mx + 1, mn + 1]) towers[mx] -= 1 towers[mn] += 1 mx = max(towers) mn = min(towers) unst = towers[mx] - towers[mn] i += 1 if unst < min_unst: min_unst = unst min_k = i print("{} {}".format(min_unst, min_k)) for log in log_moves[:min_k]: print("{} {}".format(*log))
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 FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE 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 ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR FOR VAR VAR VAR 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()] a = [int(x) for x in input().split()] for i in range(n): a[i] = [a[i], i] a.sort() ans = [] for i in range(k): if all(x == a[0] for x in a): break l = 0 r = n - 1 for i in range(n - 1): if a[i][0] < a[i + 1][0]: l = i break for i in range(n - 1, 0, -1): if a[i - 1][0] < a[i][0]: r = i break if a[l][0] + 1 >= a[r][0]: break ans.append(str(a[r][1] + 1) + " " + str(a[l][1] + 1)) a[l][0] += 1 a[r][0] -= 1 print(max(x[0] for x in a) - min(x[0] for x in a), len(ans)) print("\n".join(ans))
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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER STRING FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR 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 = map(int, input().split()) arr = [0] * n l = 1 for i in list(map(int, input().split())): arr[l - 1] = [i, str(l)] l += 1 arr.sort() ans = [] m = 10**5 for i in range(k): if arr[-1][0] - arr[0][0] <= 1 or arr[-1][0] - arr[0][0] > m: break m = arr[-1][0] - arr[0][0] arr[0][0] += 1 arr[-1][0] -= 1 ans.append(arr[-1][1] + " " + arr[0][1]) if arr[0][0] > arr[1][0]: j = 1 while j < n and arr[0][0] > arr[j][0]: j += 1 arr[0], arr[j - 1] = arr[j - 1], arr[0] if arr[-1][0] < arr[-2][0]: j = -2 while j >= -n and arr[-1][0] < arr[j][0]: j -= 1 arr[-1], arr[j + 1] = arr[j + 1], arr[-1] else: i += 1 print(abs(arr[-1][0] - arr[0][0]), i) if len(ans) != 0: print(*ans, sep="\n")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER LIST VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER STRING VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR 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.
operations = [] a = input().split(" ") towers = input().split(" ") k = int(a[1]) for i in range(len(towers)): towers[i] = int(towers[i]) def inst(tset): return max(tset) - min(tset) def fix(towerset): proset = [] for element in towerset: proset.append(element) b = proset.index(max(proset)) c = proset.index(min(proset)) d = inst(proset) e = max(proset) - 1 proset[b] -= 1 proset[c] += 1 if proset[c] <= e: return [b + 1, c + 1, proset] else: return "end" count = 0 result = None while result != "end": h = inst(towers) g = fix(towers) if g == "end": break elif count == k: break else: operations.append([g[0], g[1]]) towers = g[2] count += 1 print(inst(towers), count) for j in range(len(operations)): print(operations[j][0], operations[j][1])
ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR RETURN LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN STRING ASSIGN VAR NUMBER ASSIGN VAR NONE WHILE VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER
This is the easy version of the problem. The only difference between the versions is the constraints on $n$, $k$, $a_i$, and the sum of $n$ over all test cases. You can make hacks only if both versions of the problem are solved. Note the unusual memory limit. You are given an array of integers $a_1, a_2, \ldots, a_n$ of length $n$, and an integer $k$. The cost of an array of integers $p_1, p_2, \ldots, p_n$ of length $n$ is $$\max\limits_{1 \le i \le n}\left(\left \lfloor \frac{a_i}{p_i} \right \rfloor \right) - \min\limits_{1 \le i \le n}\left(\left \lfloor \frac{a_i}{p_i} \right \rfloor \right).$$ Here, $\lfloor \frac{x}{y} \rfloor$ denotes the integer part of the division of $x$ by $y$. Find the minimum cost of an array $p$ such that $1 \le p_i \le k$ for all $1 \le i \le n$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 3000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_1 \le a_2 \le \ldots \le a_n \le 3000$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3000$. -----Output----- For each test case, print a single integer β€” the minimum possible cost of an array $p$ satisfying the condition above. -----Examples----- Input 7 5 2 4 5 6 8 11 5 12 4 5 6 8 11 3 1 2 9 15 7 3 2 3 5 5 6 9 10 6 56 54 286 527 1436 2450 2681 3 95 16 340 2241 2 2 1 3 Output 2 0 13 1 4 7 0 -----Note----- In the first test case, the optimal array is $p = [1, 1, 1, 2, 2]$. The resulting array of values of $\lfloor \frac{a_i}{p_i} \rfloor$ is $[4, 5, 6, 4, 5]$. The cost of $p$ is $\max\limits_{1 \le i \le n}(\lfloor \frac{a_i}{p_i} \rfloor) - \min\limits_{1 \le i \le n}(\lfloor \frac{a_i}{p_i} \rfloor) = 6 - 4 = 2$. We can show that there is no array (satisfying the condition from the statement) with a smaller cost. In the second test case, one of the optimal arrays is $p = [12, 12, 12, 12, 12]$, which results in all $\lfloor \frac{a_i}{p_i} \rfloor$ being $0$. In the third test case, the only possible array is $p = [1, 1, 1]$.
for _ in range(int(input())): N, K = map(int, input().split()) a = [*map(int, input().split())] ps = [0] * (10**5 + 10) for i in range(N): pv, j = 10**9, 1 while j <= min(a[i], K): nv = a[i] // j ps[nv + 1] = max(ps[nv + 1], pv) pv = nv j = a[i] // (a[i] // j) + 1 ps[0] = max(ps[0], pv) ans, cm = 10**9, 0 for i in range(a[0] + 1): cm = max(cm, ps[i]) ans = min(ans, cm - i) print(ans)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference between the versions is the constraints on $n$, $k$, $a_i$, and the sum of $n$ over all test cases. You can make hacks only if both versions of the problem are solved. Note the unusual memory limit. You are given an array of integers $a_1, a_2, \ldots, a_n$ of length $n$, and an integer $k$. The cost of an array of integers $p_1, p_2, \ldots, p_n$ of length $n$ is $$\max\limits_{1 \le i \le n}\left(\left \lfloor \frac{a_i}{p_i} \right \rfloor \right) - \min\limits_{1 \le i \le n}\left(\left \lfloor \frac{a_i}{p_i} \right \rfloor \right).$$ Here, $\lfloor \frac{x}{y} \rfloor$ denotes the integer part of the division of $x$ by $y$. Find the minimum cost of an array $p$ such that $1 \le p_i \le k$ for all $1 \le i \le n$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 3000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_1 \le a_2 \le \ldots \le a_n \le 3000$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3000$. -----Output----- For each test case, print a single integer β€” the minimum possible cost of an array $p$ satisfying the condition above. -----Examples----- Input 7 5 2 4 5 6 8 11 5 12 4 5 6 8 11 3 1 2 9 15 7 3 2 3 5 5 6 9 10 6 56 54 286 527 1436 2450 2681 3 95 16 340 2241 2 2 1 3 Output 2 0 13 1 4 7 0 -----Note----- In the first test case, the optimal array is $p = [1, 1, 1, 2, 2]$. The resulting array of values of $\lfloor \frac{a_i}{p_i} \rfloor$ is $[4, 5, 6, 4, 5]$. The cost of $p$ is $\max\limits_{1 \le i \le n}(\lfloor \frac{a_i}{p_i} \rfloor) - \min\limits_{1 \le i \le n}(\lfloor \frac{a_i}{p_i} \rfloor) = 6 - 4 = 2$. We can show that there is no array (satisfying the condition from the statement) with a smaller cost. In the second test case, one of the optimal arrays is $p = [12, 12, 12, 12, 12]$, which results in all $\lfloor \frac{a_i}{p_i} \rfloor$ being $0$. In the third test case, the only possible array is $p = [1, 1, 1]$.
for _ in range(int(input())): _, k = map(int, input().split()) a = list(map(int, input().split())) print(min(max(x // min(k, x // u) for x in a) - u for u in range(1, a[0] + 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER
This is the easy version of the problem. The only difference between the versions is the constraints on $n$, $k$, $a_i$, and the sum of $n$ over all test cases. You can make hacks only if both versions of the problem are solved. Note the unusual memory limit. You are given an array of integers $a_1, a_2, \ldots, a_n$ of length $n$, and an integer $k$. The cost of an array of integers $p_1, p_2, \ldots, p_n$ of length $n$ is $$\max\limits_{1 \le i \le n}\left(\left \lfloor \frac{a_i}{p_i} \right \rfloor \right) - \min\limits_{1 \le i \le n}\left(\left \lfloor \frac{a_i}{p_i} \right \rfloor \right).$$ Here, $\lfloor \frac{x}{y} \rfloor$ denotes the integer part of the division of $x$ by $y$. Find the minimum cost of an array $p$ such that $1 \le p_i \le k$ for all $1 \le i \le n$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 3000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_1 \le a_2 \le \ldots \le a_n \le 3000$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3000$. -----Output----- For each test case, print a single integer β€” the minimum possible cost of an array $p$ satisfying the condition above. -----Examples----- Input 7 5 2 4 5 6 8 11 5 12 4 5 6 8 11 3 1 2 9 15 7 3 2 3 5 5 6 9 10 6 56 54 286 527 1436 2450 2681 3 95 16 340 2241 2 2 1 3 Output 2 0 13 1 4 7 0 -----Note----- In the first test case, the optimal array is $p = [1, 1, 1, 2, 2]$. The resulting array of values of $\lfloor \frac{a_i}{p_i} \rfloor$ is $[4, 5, 6, 4, 5]$. The cost of $p$ is $\max\limits_{1 \le i \le n}(\lfloor \frac{a_i}{p_i} \rfloor) - \min\limits_{1 \le i \le n}(\lfloor \frac{a_i}{p_i} \rfloor) = 6 - 4 = 2$. We can show that there is no array (satisfying the condition from the statement) with a smaller cost. In the second test case, one of the optimal arrays is $p = [12, 12, 12, 12, 12]$, which results in all $\lfloor \frac{a_i}{p_i} \rfloor$ being $0$. In the third test case, the only possible array is $p = [1, 1, 1]$.
def compute(a, p): s = [(a[i] // p[i]) for i in range(len(a))] return max(s) - min(s) for _ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) m = max(a) l = min(a) t = m // k if k >= m: print(0) elif k == 1: print(max(a) - min(a)) else: t = m // k temp = 3000 for l in range(t + 1, 2 * t + 2): p = [(a[i] // l + 1) for i in range(n)] ss = compute(a, p) temp = min(temp, ss) if temp == 0: break print(temp)
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR 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 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 IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference between the versions is the constraints on $n$, $k$, $a_i$, and the sum of $n$ over all test cases. You can make hacks only if both versions of the problem are solved. Note the unusual memory limit. You are given an array of integers $a_1, a_2, \ldots, a_n$ of length $n$, and an integer $k$. The cost of an array of integers $p_1, p_2, \ldots, p_n$ of length $n$ is $$\max\limits_{1 \le i \le n}\left(\left \lfloor \frac{a_i}{p_i} \right \rfloor \right) - \min\limits_{1 \le i \le n}\left(\left \lfloor \frac{a_i}{p_i} \right \rfloor \right).$$ Here, $\lfloor \frac{x}{y} \rfloor$ denotes the integer part of the division of $x$ by $y$. Find the minimum cost of an array $p$ such that $1 \le p_i \le k$ for all $1 \le i \le n$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 3000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_1 \le a_2 \le \ldots \le a_n \le 3000$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3000$. -----Output----- For each test case, print a single integer β€” the minimum possible cost of an array $p$ satisfying the condition above. -----Examples----- Input 7 5 2 4 5 6 8 11 5 12 4 5 6 8 11 3 1 2 9 15 7 3 2 3 5 5 6 9 10 6 56 54 286 527 1436 2450 2681 3 95 16 340 2241 2 2 1 3 Output 2 0 13 1 4 7 0 -----Note----- In the first test case, the optimal array is $p = [1, 1, 1, 2, 2]$. The resulting array of values of $\lfloor \frac{a_i}{p_i} \rfloor$ is $[4, 5, 6, 4, 5]$. The cost of $p$ is $\max\limits_{1 \le i \le n}(\lfloor \frac{a_i}{p_i} \rfloor) - \min\limits_{1 \le i \le n}(\lfloor \frac{a_i}{p_i} \rfloor) = 6 - 4 = 2$. We can show that there is no array (satisfying the condition from the statement) with a smaller cost. In the second test case, one of the optimal arrays is $p = [12, 12, 12, 12, 12]$, which results in all $\lfloor \frac{a_i}{p_i} \rfloor$ being $0$. In the third test case, the only possible array is $p = [1, 1, 1]$.
def main(): n, k = map(int, input().split()) arr = list(map(int, input().split())) ans = float("inf") mx = max(arr) for d in range(1, mx + 1): aux = [] for z in arr: if z <= d: aux.append(z) else: a = min(z // d, k) aux.append(z // a) poss = max(aux) - min(aux) ans = min(poss, ans) return ans for _ in range(int(input())): print(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 STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Did you know that the yummy golden triangle was introduced in India as early as 13th century ? By the way, I'm referring to the popular South Asian snack, Samosa. I guess its hard to code while thinking of Samosa, especially if you are very hungry now ; so lets not get in to any recipe or eating game. You have N boxes of Samosas, where each box is a cube. To pack a box, you need to use a rubber band ( pseudo-circular, elastic band ) by placing it around the box ( along 4 faces of the cube ). A (R1,R2)-rubber band has initial radius R1 and it can stretch at max to radius R2 without breaking. You can pack a cubical box of side length L using a rubber band of circumference 4 * L ( see Notes for clarity). Given M rubber bands along with their initial radius and max radius, we need to match ( assign ) some rubber bands to boxes. A box needs at least one rubber band to pack it and of course, each rubber band can be used to pack at most one box. Find the maximum number of boxes we can pack. ------ Notes ------ A pseudo-circular rubber band having a radius R has circumference of 2 * K * R , where K is a constant = 22 / 7. So, a (R1,R2) rubber band can be used to pack a cubical box of side length L, only if 2 * K * R1 ≀ 4 * L ≀ 2 * K * R2 ------ Input ------ First line contains an integer T ( number of test cases, around 20 ). T cases follow. Each test case starts with an integer N ( number of boxes, 1 ≀ N ≀ 1000 ). Next line contains N integers, the side lengths L of the N boxes ( 1 ≀ L ≀ 100,000,000 ). Next line contains an integer M ( number of rubber bands, 1 ≀ M ≀ 1000 ). Each of the next M lines contains two integers R1 R2 ( 1 ≀ R1 ≀ R2 ≀ 100,000,000 ), separated by a space. ------ Output ------ For each test case, output the maximum number of boxes you can pack, in a new line. ----- Sample Input 1 ------ 1 4 10 20 34 55 4 7 14 7 21 14 21 7 35 ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Only 1 test case here, and a possible answer can be, using (7,14) rubber band to pack box L = 10, and using (7,35) rubber band to pack box L = 55. We cannot pack more than 2 boxes.
def solve(): T = int(input()) for t in range(T): no_boxes = int(input()) boxes = [(int(temp) * 7) for temp in input().split()] no_bands = int(input()) rubber_bands = [] for b in range(no_bands): R1, R2 = input().split() rubber_bands.append([int(R2) * 11, int(R1) * 11]) boxes.sort() rubber_bands.sort() i = j = ans = 0 for box in boxes: j = 0 for band in rubber_bands: low = band[1] high = band[0] if box >= low and box <= high: ans += 1 del rubber_bands[j] break j += 1 print(ans) solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Did you know that the yummy golden triangle was introduced in India as early as 13th century ? By the way, I'm referring to the popular South Asian snack, Samosa. I guess its hard to code while thinking of Samosa, especially if you are very hungry now ; so lets not get in to any recipe or eating game. You have N boxes of Samosas, where each box is a cube. To pack a box, you need to use a rubber band ( pseudo-circular, elastic band ) by placing it around the box ( along 4 faces of the cube ). A (R1,R2)-rubber band has initial radius R1 and it can stretch at max to radius R2 without breaking. You can pack a cubical box of side length L using a rubber band of circumference 4 * L ( see Notes for clarity). Given M rubber bands along with their initial radius and max radius, we need to match ( assign ) some rubber bands to boxes. A box needs at least one rubber band to pack it and of course, each rubber band can be used to pack at most one box. Find the maximum number of boxes we can pack. ------ Notes ------ A pseudo-circular rubber band having a radius R has circumference of 2 * K * R , where K is a constant = 22 / 7. So, a (R1,R2) rubber band can be used to pack a cubical box of side length L, only if 2 * K * R1 ≀ 4 * L ≀ 2 * K * R2 ------ Input ------ First line contains an integer T ( number of test cases, around 20 ). T cases follow. Each test case starts with an integer N ( number of boxes, 1 ≀ N ≀ 1000 ). Next line contains N integers, the side lengths L of the N boxes ( 1 ≀ L ≀ 100,000,000 ). Next line contains an integer M ( number of rubber bands, 1 ≀ M ≀ 1000 ). Each of the next M lines contains two integers R1 R2 ( 1 ≀ R1 ≀ R2 ≀ 100,000,000 ), separated by a space. ------ Output ------ For each test case, output the maximum number of boxes you can pack, in a new line. ----- Sample Input 1 ------ 1 4 10 20 34 55 4 7 14 7 21 14 21 7 35 ----- Sample Output 1 ------ 2 ----- explanation 1 ------ Only 1 test case here, and a possible answer can be, using (7,14) rubber band to pack box L = 10, and using (7,35) rubber band to pack box L = 55. We cannot pack more than 2 boxes.
for _ in range(int(input())): a = int(input()) b = list(map(int, input().split())) c = int(input()) d = [] for y in range(c): e, f = input().split() d.append((int(f), int(e))) used = [False] * c b.sort() d.sort() count = 0 for i in range(a): p = 7 * b[i] for j in range(c): if used[j]: continue l, r = 11 * d[j][1], 11 * d[j][0] if l <= p and p <= r: used[j] = True count += 1 break print(count)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
def solve(): for i in range(int(input())): n, k = map(int, input().split()) s = input().strip() if k == 1: print(s) continue if n % k != 0: print(-1) continue want = [0] * 26 need = 0 i = 0 best = -1 A = ord("a") for c in s: v = ord(c) - A if v < 25: for j in range(v + 1, 26): if want[j] == 0: if need + k - 1 <= n - i - 1: best = i break elif need - 1 <= n - i - 1: best = i break if want[v] == 0: want[v] = k - 1 need += k - 1 else: need -= 1 want[v] -= 1 i += 1 if need == 0: print(s) continue if best == -1: print(-1) continue want = [0] * 26 need = 0 i = 0 for c in s: v = ord(c) - A if i == best: res = list(s[:i]) for j in range(v + 1, 26): if want[j] == 0: if need + k - 1 <= n - i - 1: need += k - 1 want[j] = k - 1 res.append(chr(j + A)) break elif need - 1 <= n - i - 1: res.append(chr(j + A)) want[j] -= 1 need -= 1 break while need < n - i - 1: want[0] += k need += k for z in range(26): for _ in range(want[z]): res.append(chr(z + A)) print("".join(res)) break if want[v] == 0: want[v] = k - 1 need += k - 1 else: need -= 1 want[v] -= 1 i += 1 solve()
FUNC_DEF 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 FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
import sys def input(): return sys.stdin.readline().rstrip() SIZE = 26 def tostr(x): return chr(x + ord("a")) def slv(): n, k = map(int, input().split()) S = list(map(lambda x: ord(x) - ord("a"), list(input()))) if n % k != 0: print(-1) return c = [0] * SIZE for i in range(n): c[S[i]] = c[S[i]] + 1 for i in range(n, -1, -1): left = n - i if i == n: if all(c[char] % k == 0 for char in range(SIZE)): print("".join(map(tostr, S))) return continue now_char = S[i] c[now_char] -= 1 minimum_need = [0] * SIZE for s in range(SIZE): if c[s] % k == 0: minimum_need[s] = 0 else: minimum_need[s] = k - c[s] % k if now_char == SIZE - 1: continue if all(minimum_need[s] == 0 for s in range(now_char + 1, SIZE)): minimum_need[now_char + 1] += k if minimum_need[now_char + 1] == 0 and left >= sum(minimum_need) + k: minimum_need[now_char + 1] += k if left >= sum(minimum_need): ans = [] for j in range(i): ans.append(S[j]) for j in range(now_char + 1, SIZE): if minimum_need[j] > 0: ans.append(j) minimum_need[j] -= 1 break q = [] for j in range(SIZE): for _ in range(minimum_need[j]): q.append(j) for _ in range(n - len(ans) - len(q)): q.append(0) q.sort() ans += q print("".join(map(tostr, ans))) return return def main(): t = int(input()) for i in range(t): slv() return main()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR RETURN RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
import sys input = sys.stdin.buffer.readline def solve(): N, K = map(int, input().split()) S = list(input()) for i in range(N): S[i] -= 97 if N % K: print(-1) return 0 C = [0] * 26 for i in range(N): C[S[i]] += 1 C = [0] * 26 for i in range(N): C[S[i]] += 1 if C[S[i]] == K: C[S[i]] = 0 if max(C) == 0: print("".join([chr(S[i] + 97) for i in range(N)])) return 0 for i in range(N - 1, -1, -1): C[S[i]] -= 1 for j in range(S[i] + 1, 26): C[j] += 1 Z = [0] * 26 for k in range(26): Z[k] = -C[k] % K X = sum(Z) if X <= N - i - 1: ANS = [] for k in range(i): ANS.append(chr(97 + S[k])) ANS.append(chr(j + 97)) ANS.append("a" * (N - i - X - 1)) for k in range(26): ANS.append(chr(97 + k) * Z[k]) print("".join(ANS)) return 0 C[j] -= 1 for t in range(int(input())): solve()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
import sys def main(): import sys input = sys.stdin.readline T = int(input()) alpha = "abcdefghijklmnopqrstuvwxyz" da = dict() ANS = [] for i, x in enumerate(alpha): da[x] = i for _ in range(T): n, k = map(int, input().split()) s = input().rstrip() if n % k != 0: ANS.append("-1") continue d = dict() for x in s: if x not in d: d[x] = 0 d[x] += 1 flag = True for x in d: if d[x] % k > 0: flag = False if flag: ANS.append(s) continue s = s[::-1] for i in range(n): r = 0 d[s[i]] -= 1 ret = [] for x in d: if d[x] % k > 0: r += k - d[x] % k if s[i] == "z": continue if i + 1 == r: for j in range(da[s[i]] + 1, 26): x = alpha[j] if x in d and d[x] % k != 0: ret.append(x) d[x] += 1 for y in alpha: if y in d and d[y] % k != 0: for j2 in range(k - d[y] % k): ret.append(y) ANS.append("".join([s[i + 1 :][::-1], "".join(ret)])) flag = True break if flag: break elif i + 1 > r: id = da[s[i]] if alpha[id + 1] not in d: d[alpha[id + 1]] = 0 d[alpha[id + 1]] += 1 r2 = i for y in alpha: if y in d and d[y] % k != 0: for j2 in range(k - d[y] % k): ret.append(y) r2 -= 1 ret2 = [] for j in range(r2): ret2.append("a") ANS.append( "".join( [s[i + 1 :][::-1], alpha[id + 1], "".join(ret2), "".join(ret)] ) ) flag = True break if flag == False: ANS.append("-1") print(*ANS, sep="\n") main()
IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL 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 FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR IF VAR VAR STRING IF BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING LIST VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL STRING VAR ASSIGN VAR NUMBER IF VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING LIST VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL STRING VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
import sys t = int(input()) for _ in range(t): n, k = [int(x) for x in input().split()] s = input() if n % k: print(-1) continue temp = [(0) for i in range(26)] for i in s: temp[ord(i) - ord("a")] += 1 f = True for i in temp: if i % k: f = False if f: print(s) continue for i in range(n - 1, -1, -1): temp[ord(s[i]) - ord("a")] -= 1 f = False for j in range(ord(s[i]) + 1 - ord("a"), 26): temp[j] += 1 curr = 0 for p in range(26): if temp[p] % k: curr += k - temp[p] % k if curr <= n - 1 - i and (n - i - 1 - curr) % k == 0: ind = i now = j f = True break temp[j] -= 1 if f: break for i in range(26): if temp[i] % k: temp[i] += k - temp[i] % k if sum(temp) < n: temp[0] += n - sum(temp) ans = s[:ind] + chr(ord("a") + j) for i in s[:ind]: temp[ord(i) - ord("a")] -= 1 temp[j] -= 1 for i in range(26): ans += temp[i] * chr(ord("a") + i) print(ans)
IMPORT 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 IF BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR FOR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR VAR
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
def possible(s, index, letters, k, ans): req = 0 for i in letters: req += (k - i) % k give = len(s) - index if req > give: return False if (give - req) % k != 0: return False while len(s) != index: s.pop() for i in range(give - req): s.append("a") for i in range(26): for j in range((k - letters[i]) % k): s.append(chr(ord("a") + i)) ans.append("".join(s)) return True def solve(n, k, s, ans): letters = [0] * 26 for i in s: letters[ord(i) - ord("a")] += 1 for i in range(26): letters[i] %= k if letters.count(0) == 26: ans.append(s) return s = list(s) for i in range(len(s) - 1, -1, -1): letters[ord(s[i]) - ord("a")] -= 1 letters[ord(s[i]) - ord("a")] %= k prev = s[i] for j in range(ord(s[i]) - ord("a") + 1, 26): s[i] = chr(ord("a") + j) letters[j] += 1 letters[j] %= k if possible(s, i + 1, letters, k, ans): return letters[j] -= 1 letters[j] %= k ans.append("-1") def main(): t = int(input()) ans = [] for i in range(t): n, k = map(int, input().split()) s = input() solve(n, k, s, ans) print("\n".join(ans)) main()
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN NUMBER WHILE FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
def check_str(s, k): d = {} for ch in s: if ch in d: d[ch] += 1 else: d[ch] = 1 cnt = 0 for ch in d: if d[ch] % k != 0: cnt = 1 break if cnt == 1: return False else: return True def inc_lex(s): val = len(s) val_d = val + 0 val -= 1 while val >= 0 and s[val] == "z": s[val] = "a" val -= 1 if val < 0: return [7] else: s[val] = chr(ord(s[val]) + 1) return s t = int(input()) for i in range(t): n, k = map(int, input().split()) s = input() s = list(s) d = { "a": 0, "b": 0, "c": 0, "d": 0, "e": 0, "f": 0, "g": 0, "h": 0, "i": 0, "j": 0, "k": 0, "l": 0, "m": 0, "n": 0, "o": 0, "p": 0, "q": 0, "r": 0, "s": 0, "t": 0, "u": 0, "v": 0, "w": 0, "x": 0, "y": 0, "z": 0, } d1 = d.copy() if n % k != 0: print(-1) else: for ch in s: d[ch] += 1 cnt = 0 overall_req = 0 for ch in d: if d[ch] % k != 0: overall_req += k - d[ch] % k d1[ch] = k - d[ch] % k for ch in d: if d[ch] % k != 0: cnt = 1 break if cnt == 0: print("".join(s)) else: for j in range(n - 1, -1, -1): val = s[j] if (d[val] - 1) % k == 0: overall_req -= d1[val] d1[val] = 0 else: d1[val] += 1 overall_req += 1 d[val] -= 1 qnt = 0 for q in range(ord(val) + 1, 123): req_posn = 0 d[chr(q)] += 1 if d1[chr(q)] == 0: d1[chr(q)] += k - 1 overall_req += k - 1 else: d1[chr(q)] -= 1 overall_req -= 1 if overall_req > n - j - 1: if (d[chr(q)] - 1) % k == 0: overall_req -= d1[chr(q)] d1[chr(q)] = 0 else: d1[chr(q)] += 1 overall_req += 1 d[chr(q)] -= 1 continue elif (n - j - 1 - overall_req) % k == 0: kuch = [] for ch in d1: kuch.extend([ch] * d1[ch]) luch = ["a"] * (n - j - 1 - overall_req) luch.extend(kuch) s = s[:j] s.append(chr(q)) s.extend(luch) print("".join(s)) qnt = 1 break else: if (d[chr(q)] - 1) % k == 0: overall_req -= d1[chr(q)] d1[chr(q)] = 0 else: d1[chr(q)] += 1 overall_req += 1 d[chr(q)] -= 1 continue if qnt == 1: break
FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER RETURN LIST NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN VAR 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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
for _ in range(int(input())): n, k = map(int, input().split()) s = list(input()) if len(s) % k != 0: print(-1) continue count = [0] * 26 for e in s: i = ord(e) - 97 count[i] += 1 isBeuty = True for i in range(26): if count[i] % k != 0: isBeuty = False if isBeuty: print("".join(s)) continue room = 0 flag = False while s: cur = s.pop() icur = ord(cur) - 97 count[icur] -= 1 room += 1 if cur == "z": continue need = 0 mx = -1 for i in range(26): if count[i] % k != 0: need += k - count[i] % k if mx == -1 and i > icur: mx = i if need > room: continue elif need == room: if icur > mx: continue else: s.append(chr(mx + 97)) count[mx] += 1 flag = True break else: s.append(chr(icur + 1 + 97)) count[icur + 1] += 1 flag = True break if not flag: print(-1) continue rem = [] for i in range(26): r = count[i] % k if r != 0: rem += [chr(97 + i)] * (k - r) result = s + ["a"] * (n - len(s) - len(rem)) + rem print("".join(result))
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 IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR BIN_OP LIST FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP LIST STRING BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
def solve(n, k, s): if n % k: return -1 if k == 1: return s d = {} cnt = 0 pref = [] for i in range(n): pref.append(cnt) if s[i] not in d: d[s[i]] = 1 else: d[s[i]] += 1 if d[s[i]] % k == 1: cnt += k - 1 else: cnt -= 1 good = True for ke in d: if d[ke] % k: good = False break if good: return s for i in range(n - 1, -1, -1): d[s[i]] -= 1 len_suf = n - i - 1 if pref[i] > 1 + len_suf: continue if pref[i] == 1 + len_suf: for j in range(ord(s[i]) + 1, ord("z") + 1): if chr(j) in d and d[chr(j)] % k: s2 = s[:i] s2 += chr(j) d[chr(j)] += 1 keys = sorted(d.keys()) for ke in keys: if d[ke] % k: s2 += ke * (k - d[ke] % k) return s2 continue else: if s[i] == "z": continue s2 = s[:i] s2 += chr(ord(s[i]) + 1) last = s2[-1] if last not in d: d[last] = 1 else: d[last] += 1 end_s = "" keys = sorted(d.keys()) for ke in keys: if d[ke] % k: end_s += ke * (k - d[ke] % k) remaining = n - len(s2) - len(end_s) return s2 + "a" * remaining + end_s def main(): T = int(input()) for _ in range(T): n, k = map(int, input().split()) s = input() print(solve(n, k, s)) main()
FUNC_DEF IF BIN_OP VAR VAR RETURN NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR IF VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR STRING NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR RETURN VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP STRING VAR VAR FUNC_DEF 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
gans = [] for _ in range(int(input())): n, k = map(int, input().split()) u = list(input()) for i in range(n): u[i] = ord(u[i]) - ord("a") z = [] for i in range(26): z.append([]) z[-1].append(0) z[u[0]][0] += 1 z[u[0]][0] %= k for i in range(1, n): for j in range(26): z[j].append(z[j][i - 1]) z[u[i]][i] += 1 z[u[i]][i] %= k for i in range(n - 1, -1, -1): sm = 0 for j in range(26): sm += (k - z[j][i]) % k if sm % k == (n - i - 1) % k and sm <= n - i - 1: if i == n - 1: ans = [] for r in range(n): ans.append(chr(u[r] + ord("a"))) gans.append("".join(ans)) break ans = u[: i + 1] if sm + k > n - i - 1 or u[i + 1] == ord("z") - ord("a"): for j in range(u[i + 1] + 1, 26): if z[j][i] != 0: ans.append(j) z[j][i] += 1 z[j][i] %= k break else: continue else: ans.append(u[i + 1] + 1) z[u[i + 1] + 1][i] += 1 z[u[i + 1] + 1][i] %= k s = [] for j in range(26): for r in range((k - z[j][i]) % k): s.append(j) for j in range(n - len(ans) - len(s)): ans.append(0) ans += s for j in range(len(ans)): ans[j] = chr(ans[j] + ord("a")) gans.append("".join(ans)) break else: if n % k == 0 and u[0] != z: ans = [u[0] + 1] for i in range(n - k): ans.append(0) for i in range(k - 1): ans.append(u[0] + 1) for i in range(n): ans[i] = chr(ans[i] + ord("a")) gans.append("".join(ans)) else: gans.append(str(-1)) print("\n".join(gans))
ASSIGN VAR LIST 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR LIST BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
from sys import stdin, stdout t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input() if n % k: print(-1) continue vals = [(0) for i in range(26)] for i in s: vals[ord(i) - 97] += 1 for i in vals: if i % k: break else: print(s) continue distinct = n // k ans = [(0) for i in range(n)] pre = [[(0) for i in range(26)] for j in range(n)] for i in range(26): for j in range(n): if j == 0: if ord(s[j]) - 97 == i: pre[j][i] = 1 else: pre[j][i] = 0 elif ord(s[j]) - 97 == i: pre[j][i] = pre[j - 1][i] + 1 else: pre[j][i] = pre[j - 1][i] for i in range(n - 1, -1, -1): if ord(s[i]) - 97 == 25: continue req = 0 last = 0 to_add = {} for j in range(26): if i == 0: count = 0 else: count = pre[i - 1][j] if count % k == 0: continue else: req += k - count % k last = j to_add[j] = k - count % k left = n - i if req > left: continue if req == left and last <= ord(s[i]) - 97: continue new = left - req if new % k: continue for j in range(0, i): ans[j] = s[j] if new == 0: for j in sorted(to_add): if j > ord(s[i]) - 97: ans[i] = chr(j + 97) to_add[j] -= 1 break counter = i for j in sorted(to_add): for q in range(to_add[j]): counter += 1 ans[counter] = chr(j + 97) else: pivot = chr(ord(s[i]) + 1) ans[i] = pivot if ord(pivot) - 97 in to_add: to_add[ord(pivot) - 97] -= 1 to_add[0] = to_add.get(0, 0) + new else: if 1 % k == 0: var = 0 else: var = k - 1 % k to_add[ord(pivot) - 97] = var to_add[0] = to_add.get(0, 0) + new - var - 1 counter = i for j in sorted(to_add): for q in range(to_add[j]): counter += 1 ans[counter] = chr(j + 97) break print("".join(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 IF BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER VAR IF BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
import sys def charToInt(c): return ord(c) - ord("a") def intToChar(x): return chr(ord("a") + x) def main(): t = int(input()) allans = [] for _ in range(t): n, k = readIntArr() s = input() if n % k != 0: allans.append(-1) continue sarr = [charToInt(c) for c in s] ansarr = [None for _ in range(n)] remaining = [(0) for _ in range(26)] totalremaining = 0 def checkCanComplete(i, val): if val > 25: return False actualRemaining = n - i - 1 if remaining[val] > 0: newtotalremaining = totalremaining - 1 else: newtotalremaining = totalremaining - 1 + k if newtotalremaining > actualRemaining: return False else: return True ok = True for i in range(n): if checkCanComplete(i, sarr[i]): ansarr[i] = sarr[i] remaining[sarr[i]] -= 1 totalremaining -= 1 if remaining[sarr[i]] < 0: remaining[sarr[i]] += k totalremaining += k else: ok = False break if ok == False: j = i while True: if ansarr[j] != None: ansarr[j] = None remaining[sarr[j]] += 1 totalremaining += 1 if remaining[sarr[j]] == k: remaining[sarr[j]] -= k totalremaining -= k ok = False for z in range(sarr[j] + 1, 26): if checkCanComplete(j, z): ansarr[j] = z remaining[z] -= 1 totalremaining -= 1 if remaining[z] < 0: remaining[z] += k totalremaining += k j += 1 ok = True break if ok: break j -= 1 while j < n: actualRemaining = n - j if actualRemaining > totalremaining: ansarr[j] = 0 j += 1 else: for i in range(26): for __ in range(remaining[i]): ansarr[j] = i j += 1 totalremaining -= 1 ans = "".join([intToChar(x) for x in ansarr]) allans.append(ans) multiLineArrayPrint(allans) return input = lambda: sys.stdin.readline().rstrip("\r\n") def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) def multiLineArrayPrint(arr): print("\n".join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] def makeArr(defaultVal, dimensionArr): dv = defaultVal da = dimensionArr if len(da) == 1: return [dv for _ in range(da[0])] else: return [makeArr(dv, da[1:]) for _ in range(da[0])] def queryInteractive(x, y): print("? {} {}".format(x, y)) sys.stdout.flush() return int(input()) def answerInteractive(ans): print("! {}".format(ans)) sys.stdout.flush() inf = float("inf") MOD = 10**9 + 7 for _abc in range(1): main()
IMPORT FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR WHILE NUMBER IF VAR VAR NONE ASSIGN VAR VAR NONE VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
def inc(s, freq, k, i): freq[ord(s[i]) - ord("a")] -= 1 if s[i] == "z": return False else: for c in range(ord(s[i]) + 1, ord("z") + 1): freq[c - ord("a")] += 1 mand = 0 for j in range(26): if freq[j] % k != 0: mand += k - freq[j] % k left = n - i - 1 if left < mand or (left - mand) % k != 0: freq[c - ord("a")] -= 1 continue else: s[i] = chr(c) remain = [] for j in range(26): if freq[j] % k != 0: remain += [chr(ord("a") + j)] * (k - freq[j] % k) if mand < left: remain += ["a"] * (left - mand) remain.sort() s[i + 1 :] = remain return True return False for _ in range(int(input())): n, k = map(int, input().split()) s = list(input()) freq = [0] * 26 for c in s: freq[ord(c) - ord("a")] += 1 f = True for i in range(26): if freq[i] % k != 0: f = False break if f: print("".join(s)) continue done = False for i in range(n - 1, -1, -1): if inc(s, freq, k, i): print("".join(s)) done = True break if not done: print(-1)
FUNC_DEF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR VAR STRING RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR STRING NUMBER VAR BIN_OP VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP LIST FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR BIN_OP VAR BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP LIST STRING BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER 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 ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
from sys import stdin, stdout def k_beautiful_strings(n, k, s): if n % k != 0: return "-1" cnt_a = [0] * 26 for c in s: cnt_a[ord(c) - ord("a")] += 1 need = calc_need(cnt_a, k) if need == 0: return s for i in range(n - 1, -1, -1): need += delta_need(cnt_a, ord(s[i]) - ord("a"), k, -1) cnt_a[ord(s[i]) - ord("a")] -= 1 slots = n - i - 1 for j in range(ord(s[i]) - ord("a") + 1, 26): cnt = delta_need(cnt_a, j, k, 1) need += cnt if need <= slots: cnt_a[j] += 1 suffix = [] for _ in range(need, slots): suffix.append("a") for z in range(0, 26): cn = (k - cnt_a[z] % k) % k for _ in range(cn): suffix.append(chr(z + ord("a"))) return s[:i] + chr(j + ord("a")) + "".join(suffix) need -= cnt return "-1" def delta_need(cnt_a, i, k, d): delta = 0 delta -= (k - cnt_a[i] % k) % k delta += (k - (cnt_a[i] + d) % k) % k return delta def calc_need(cnt_a, k): need = 0 for cnt in cnt_a: need += (k - cnt % k) % k return need T = int(stdin.readline()) for _ in range(T): n, k = map(int, stdin.readline().split()) s = stdin.readline().strip() r = k_beautiful_strings(n, k, s) stdout.write(r + "\n")
FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING FUNC_CALL STRING VAR VAR VAR RETURN STRING FUNC_DEF ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR 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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
def toNum(letter): return ord(letter) - ord("a") def toChr(num): return chr(num + ord("a")) def getRemaining(num, k): return (k - num % k) % k def getRemainingSum(k, cnt): sm = 0 for num in cnt: sm += getRemaining(num, k) return sm for cases in range(int(input())): n, k = map(int, input().split()) s = input() cnt = [(0) for x in range(26)] if n % k != 0: print(-1) continue for letter in s: cnt[toNum(letter)] += 1 sm = getRemainingSum(k, cnt) if sm == 0: print(s) continue i = n - 1 flag = True while i >= 0 and flag: sm -= getRemaining(cnt[toNum(s[i])], k) cnt[toNum(s[i])] -= 1 sm += getRemaining(cnt[toNum(s[i])], k) j = toNum(s[i]) + 1 while j < 26: lastsum = sm sm -= getRemaining(cnt[j], k) cnt[j] += 1 sm += getRemaining(cnt[j], k) if i + sm + 1 <= n: for pos in range(i): print(s[pos], end="") print(toChr(j), end="") add = [] for w in range(26): f = getRemaining(cnt[w], k) while f: f -= 1 add.append(toChr(w)) while len(add) + i + 1 < n: add.append(toChr(0)) add.sort() print("".join(add)) flag = False break cnt[j] -= 1 sm = lastsum j += 1 i -= 1
FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR 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 ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
import sys from sys import stdin def solve(N, K, S): if N % K != 0: print(-1) return cnt_dict = {} for s in S: cnt_dict[s] = 0 for s in S: cnt_dict[s] += 1 bea = True for k in cnt_dict: if cnt_dict[k] % K != 0: bea = False if bea: print(S) return pref = S[:] to_add_dict = {} for k in cnt_dict: to_add_dict[k] = (K - cnt_dict[k] % K) % K for i in range(1, N + 1): pref = pref[:-1] letter_cur = S[N - i] letter_next = chr(ord(letter_cur) + 1) cnt_dict[letter_cur] -= 1 to_add_dict[letter_cur] += 1 to_add_dict[letter_cur] %= K my_sum = sum(to_add_dict.values()) if letter_cur != "z": if my_sum < i: diff = i - my_sum if letter_next in to_add_dict.keys(): if to_add_dict[letter_next] == 0: to_add_dict[letter_next] = K diff -= K if "a" in to_add_dict.keys(): to_add_dict["a"] += diff else: to_add_dict["a"] = diff my_suffix = letter_next to_add_dict[letter_next] -= 1 for my_key in sorted(to_add_dict): my_suffix += my_key * to_add_dict[my_key] else: to_add_dict[letter_next] = K diff -= K if "a" in to_add_dict.keys(): to_add_dict["a"] += diff else: to_add_dict["a"] = diff my_suffix = letter_next to_add_dict[letter_next] -= 1 for my_key in sorted(to_add_dict): my_suffix += my_key * to_add_dict[my_key] print(pref + my_suffix) return elif my_sum == i: good = False for my_key in sorted(to_add_dict): if my_key > letter_cur and to_add_dict[my_key] > 0: my_suffix = my_key to_add_dict[my_key] -= 1 good = True break if good: for my_key in sorted(to_add_dict): my_suffix += my_key * to_add_dict[my_key] print(pref + my_suffix) return return def run(): out = "" T = int(input()) for i in range(T): N, K = [int(x) for x in stdin.readline().split()] S = stdin.readline().strip() solve(N, K, S) run()
IMPORT FUNC_DEF IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR STRING IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF STRING FUNC_CALL VAR VAR STRING VAR ASSIGN VAR STRING VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF STRING FUNC_CALL VAR VAR STRING VAR ASSIGN VAR STRING VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN RETURN FUNC_DEF ASSIGN VAR STRING 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 FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
import sys input = sys.stdin.readline def index(i): return ord(i) - ord("a") def get(x, k): return (k - x % k) % k t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input() if s[-1] == "\n": s = s[:-1] count = [0] * 26 for i in s: count[index(i)] += 1 su, flag = 0, 1 for i in range(26): su += get(count[i], k) ans = "" if su == 0: ans += s flag = 0 if n % k != 0: ans = -1 flag = 0 for i in range(n - 1, -1, -1): if not flag: break su -= get(count[index(s[i])], k) count[index(s[i])] -= 1 su += get(count[index(s[i])], k) for j in range(index(s[i]) + 1, 26): temp = su su -= get(count[j], k) count[j] += 1 su += get(count[j], k) if i + su < n: ans += s[:i] + chr(ord("a") + j) add = "" for w in range(26): f = get(count[w], k) while f: f -= 1 add += chr(ord("a") + w) while len(add) + i + 1 < n: add += "a" add = "".join(sorted(add)) ans += add flag = 0 break count[j] -= 1 su = temp print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR 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 IF VAR NUMBER STRING ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR STRING IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR WHILE BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR STRING ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
import sys input = sys.stdin.readline t = int(input()) for tests in range(t): n, k = map(int, input().split()) S = list(input().strip()) SA = S[:] + ["z"] COUNT = [([0] * 26) for i in range(n)] for i in range(n): for j in range(26): COUNT[i][j] = COUNT[i - 1][j] COUNT[i][ord(S[i]) - 97] += 1 ANS = -1 for i in range(n - 1, -1, -1): SA.pop() amari = 0 for j in range(26): amari += (k - COUNT[i][j]) % k if amari > n - 1 - i: break else: if (n - 1 - i - amari) % k == 0: alist = [] for j in range(26): alist += [chr(j + 97)] * ((k - COUNT[i][j]) % k) if i == len(S) - 1: ANS = S print("".join(ANS)) break if len(S) == i + 1 + len(alist): alist.sort() for xx in alist: if xx > S[i + 1]: SSS = SA + [xx] break else: continue elif S[i + 1] != "z": SSS = SA + [chr(ord(S[i + 1]) + 1)] else: continue count2 = [0] * 26 for s in SSS: count2[ord(s) - 97] += 1 nokori = [] for j in range(26): nokori += [chr(j + 97)] * ((k - count2[j]) % k) nokori += ["a"] * (len(S) - len(nokori) - len(SSS)) nokori.sort() ANS = SSS + nokori if ANS >= S: print("".join(ANS)) break if ANS == -1: if n % k == 0: count2 = [0] * 26 SSS = [chr(ord(S[0]) + 1)] for s in SSS: count2[ord(s) - 97] += 1 nokori = [] for j in range(26): nokori += [chr(j + 97)] * ((k - count2[j]) % k) nokori += ["a"] * (len(S) - len(nokori) - len(SSS)) nokori.sort() ANS = SSS + nokori if ANS >= S: print("".join(ANS)) else: print(ANS)
IMPORT ASSIGN VAR VAR 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 FUNC_CALL VAR ASSIGN VAR BIN_OP VAR LIST STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP LIST FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR LIST VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR LIST FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP LIST FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP LIST STRING BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP LIST FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP LIST STRING BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
def check(d, k): delta = n - sum(d) for i in d: delta -= (k - i % k) % k if delta < 0: return False else: return True def try_(index, d, i): for jindex in range(index + 1, len(d)): d[jindex] += 1 if check(d, k): s[i] = chr(97 + jindex) return True else: d[jindex] -= 1 return False for _ in range(int(input())): n, k = map(int, input().split()) s = list(input()) d = [(0) for i in range(26)] for i in s: index = ord(i) - 97 d[index] += 1 if n % k != 0: print(-1) continue if check(d, k): print("".join(s)) continue flg = False for i in range(len(s) - 1, -1, -1): index = ord(s[i]) - 97 d[index] -= 1 if check(d, k): if try_(index, d, i): flg = True sss = sum(d) for x in range(len(d)): d[x] = (k - d[x] % k) % k sss += d[x] d[0] += n - sss x = 0 i += 1 while x < len(d): while x < len(d) and d[x] == 0: x += 1 if x < len(d): s[i] = chr(x + 97) d[x] -= 1 i += 1 break if flg: print("".join(s)) else: print(-1)
FUNC_DEF ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR RETURN NUMBER VAR VAR 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 ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
import sys zz = 1 sys.setrecursionlimit(10**5) if zz: input = sys.stdin.readline else: sys.stdin = open("input.txt", "r") sys.stdout = open("all.txt", "w") di = [[-1, 0], [1, 0], [0, 1], [0, -1]] def fori(n): return [fi() for i in range(n)] def inc(d, c, x=1): d[c] = d[c] + x if c in d else x def ii(): return input().rstrip() def li(): return [int(xx) for xx in input().split()] def fli(): return [float(x) for x in input().split()] def dadd(d, p, val): if p in d: d[p].append(val) else: d[p] = [val] def gi(): return [xx for xx in input().split()] def gtc(tc, ans): print("Case #" + str(tc) + ":", ans) def cil(n, m): return n // m + int(n % m > 0) def fi(): return int(input()) def pro(a): return reduce(lambda a, b: a * b, a) def swap(a, i, j): a[i], a[j] = a[j], a[i] def prec(a, pre): for i in a: pre.append(pre[-1] + i) pre.pop(0) def YN(flag): print("YES" if flag else "NO") def si(): return list(input().rstrip()) def mi(): return map(int, input().split()) def gh(): sys.stdout.flush() def isvalid(i, j, n, m): return 0 <= i < n and 0 <= j < m def bo(i): return ord(i) - ord("a") def graph(n, m): for i in range(m): x, y = mi() a[x].append(y) a[y].append(x) t = fi() INF = 10**18 uu = t while t > 0: t -= 1 n, k = mi() s = ii() d = {} c = 0 if n % k: print(-1) continue if s[0] == "z": ans = "z" * n else: ans = chr(ord(s[0]) + 1) * n for i in range(26): d[chr(ord("a") + i)] = 0 r = -1 w = {} for i in range(-1, n - 1): if i >= 0: c -= (k - d[s[i]]) % k inc(d, s[i]) d[s[i]] %= k c += (k - d[s[i]]) % k vis = {} for j in d: vis[j] = d[j] for j in range(bo(s[i + 1]) + 1, 26): rr = c y = chr(j + ord("a")) z = vis[y] rr -= (k - z) % k vis[y] += 1 vis[y] %= k rr += (k - vis[y]) % k if n - i - 1 >= rr: r = i + 1 w[i + 1] = j break c -= (k - d[s[-1]]) % k inc(d, s[-1]) d[s[-1]] %= k c += (k - d[s[-1]]) % k if c == 0: print(s) continue if r == -1: print(ans) continue d = {} for i in range(26): d[chr(ord("a") + i)] = 0 s = s[:r] s += chr(w[r] + ord("a")) for i in range(r + 1): inc(d, s[i]) d[s[i]] %= k p = [] for i in d: for j in range((k - d[i]) % k): p.append(i) for i in range(r + 1 + len(p), n): p.append("a") p.sort() for i in p: s += i print(s)
IMPORT ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR STRING STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF RETURN NUMBER VAR VAR NUMBER VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER IF BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
test = int(input()) def check(k): for i in range(26): if num[i] % k != 0: return 0 return 1 def getval(k): res = 0 for i in range(26): if num[i] % k != 0: res += k - num[i] % k return res for i in range(test): n, k = map(int, input().split(" ")) s = input() if n % k != 0: print(-1) else: num = [0] num *= 26 flg = 0 for j in range(n): num[ord(s[j]) - ord("a")] += 1 if check(k) == 1: print(s) flg = 1 else: for j in range(n - 1, -1, -1): num[ord(s[j]) - ord("a")] -= 1 for h in range(ord(s[j]) - ord("a") + 1, 26): num[h] += 1 if getval(k) <= n - 1 - j: flg = 1 res = s[0:j] + chr(ord("a") + h) lst = n - 1 - j for p in range(26): if num[p] % k != 0: lst -= k - num[p] % k res += "a" * lst for p in range(26): if num[p] % k != 0: res += chr(ord("a") + p) * (k - num[p] % k) print(res) break else: num[h] -= 1 if flg == 1: break if flg == 0: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1. A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a β‰  b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input The first line contains a single integer T (1 ≀ T ≀ 10 000) β€” the number of test cases. The next 2 β‹… T lines contain the description of test cases. The description of each test case consists of two lines. The first line of the description contains two integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the length of string s and number k respectively. The second line contains string s consisting of lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case output in a separate line lexicographically smallest beautiful string of length n, which is greater or equal to string s, or -1 if such a string does not exist. Example Input 4 4 2 abcd 3 1 abc 4 3 aaaa 9 3 abaabaaaa Output acac abc -1 abaabaaab Note In the first test case "acac" is greater than or equal to s, and each letter appears 2 or 0 times in it, so it is beautiful. In the second test case each letter appears 0 or 1 times in s, so s itself is the answer. We can show that there is no suitable string in the third test case. In the fourth test case each letter appears 0, 3, or 6 times in "abaabaaab". All these integers are divisible by 3.
import sys input = lambda: sys.stdin.readline().rstrip("\r\n") s = 0, 1, 2, 5, 8 d = {(0): 0, (1): 1, (2): 5, (5): 2, (8): 8} b = [[0, 0], [0, 1]] for _ in range(int(input())): n, k = map(int, input().split()) a = input() ans = str() if len(a) % k: print(-1) else: dp = [0] * 26 t = n - 1 for i in a: dp[ord(i) - 97] += 1 tst = 1 for i in dp: if i % k: tst = 0 break if not tst: for i, v in enumerate(reversed(a)): dp[ord(v) - 97] -= 1 t = n - i - 1 for j in range(ord(v) - 97 + 1, 26): rem = i ck = [0] * 26 ck[j] += 1 for id in range(26): if (dp[id] + ck[id]) % k: rem -= k - (dp[id] + ck[id]) % k ck[id] += k - (dp[id] + ck[id]) % k if rem > 0 and not rem % k: ck[0] += rem rem = 0 if rem == 0: for id in range(n - i - 1): ans += a[id] ans += chr(j + 97) ck[j] -= 1 for id, v in enumerate(ck): ans += chr(id + 97) * v break if ans: break else: ans = a print(ans)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER 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 ASSIGN VAR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
On a history lesson the teacher asked Vasya to name the dates when n famous events took place. He doesn't remembers the exact dates but he remembers a segment of days [li, ri] (inclusive) on which the event could have taken place. However Vasya also remembers that there was at most one event in one day. Help him choose such n dates of famous events that will fulfill both conditions. It is guaranteed that it is possible. Input The first line contains one integer n (1 ≀ n ≀ 100) β€” the number of known events. Then follow n lines containing two integers li and ri each (1 ≀ li ≀ ri ≀ 107) β€” the earliest acceptable date and the latest acceptable date of the i-th event. Output Print n numbers β€” the dates on which the events took place. If there are several solutions, print any of them. It is guaranteed that a solution exists. Examples Input 3 1 2 2 3 3 4 Output 1 2 3 Input 2 1 3 1 3 Output 1 2
from sys import stdin inFile = stdin tokens = [] tokens_next = 0 def next_str(): global tokens, tokens_next while tokens_next >= len(tokens): tokens = inFile.readline().split() tokens_next = 0 tokens_next += 1 return tokens[tokens_next - 1] def nextInt(): return int(next_str()) n = nextInt() intervals = [(nextInt(), nextInt(), i) for i in range(n)] intervals.sort(key=lambda x: (x[1], x[0])) seen = set() for i in range(n): cur = intervals[i][0] while cur in seen: cur += 1 seen.add(cur) intervals[i] = intervals[i][0], intervals[i][1], intervals[i][2], cur intervals.sort(key=lambda x: x[2]) for i in range(n): print(intervals[i][3], end=" ") print()
ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER RETURN VAR BIN_OP VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR
On a history lesson the teacher asked Vasya to name the dates when n famous events took place. He doesn't remembers the exact dates but he remembers a segment of days [li, ri] (inclusive) on which the event could have taken place. However Vasya also remembers that there was at most one event in one day. Help him choose such n dates of famous events that will fulfill both conditions. It is guaranteed that it is possible. Input The first line contains one integer n (1 ≀ n ≀ 100) β€” the number of known events. Then follow n lines containing two integers li and ri each (1 ≀ li ≀ ri ≀ 107) β€” the earliest acceptable date and the latest acceptable date of the i-th event. Output Print n numbers β€” the dates on which the events took place. If there are several solutions, print any of them. It is guaranteed that a solution exists. Examples Input 3 1 2 2 3 3 4 Output 1 2 3 Input 2 1 3 1 3 Output 1 2
n = int(input()) seg = [] for i in range(n): l, r = map(int, input().split()) seg.append((l, r, i)) seg.sort(key=lambda tup: (tup[1], tup[0], tup[2])) assigned = set() ans = [0] * n for i in range(n): for j in range(seg[i][0], seg[i][1] + 1, 1): if j not in assigned: ans[seg[i][2]] = j assigned.add(j) break for i in range(n): print(ans[i], end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
On a history lesson the teacher asked Vasya to name the dates when n famous events took place. He doesn't remembers the exact dates but he remembers a segment of days [li, ri] (inclusive) on which the event could have taken place. However Vasya also remembers that there was at most one event in one day. Help him choose such n dates of famous events that will fulfill both conditions. It is guaranteed that it is possible. Input The first line contains one integer n (1 ≀ n ≀ 100) β€” the number of known events. Then follow n lines containing two integers li and ri each (1 ≀ li ≀ ri ≀ 107) β€” the earliest acceptable date and the latest acceptable date of the i-th event. Output Print n numbers β€” the dates on which the events took place. If there are several solutions, print any of them. It is guaranteed that a solution exists. Examples Input 3 1 2 2 3 3 4 Output 1 2 3 Input 2 1 3 1 3 Output 1 2
n = int(input()) freq = [0] * 10000001 ans = [0] * 100 a = [] for i in range(n): l, r = map(int, input().split()) temp = r, l, i a.append(temp) a.sort() for i in range(n): r, l, v = a[i][0], a[i][1], a[i][2] for j in range(l, r + 1): if freq[j] == 0: freq[j] = 1 ans[v] = j break for i in range(n): print(ans[i], " ", end="")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING STRING
On a history lesson the teacher asked Vasya to name the dates when n famous events took place. He doesn't remembers the exact dates but he remembers a segment of days [li, ri] (inclusive) on which the event could have taken place. However Vasya also remembers that there was at most one event in one day. Help him choose such n dates of famous events that will fulfill both conditions. It is guaranteed that it is possible. Input The first line contains one integer n (1 ≀ n ≀ 100) β€” the number of known events. Then follow n lines containing two integers li and ri each (1 ≀ li ≀ ri ≀ 107) β€” the earliest acceptable date and the latest acceptable date of the i-th event. Output Print n numbers β€” the dates on which the events took place. If there are several solutions, print any of them. It is guaranteed that a solution exists. Examples Input 3 1 2 2 3 3 4 Output 1 2 3 Input 2 1 3 1 3 Output 1 2
n = int(input()) a, ans, b = [], [0] * n, [0] * 10000001 for i in range(n): L, R = map(int, input().split()) a.append([R, L, i]) a.sort() for z in a: R, L, i = z for j in range(L, R + 1): if b[j] == 0: b[j] = 1 ans[i] = j break print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR LIST BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
On a history lesson the teacher asked Vasya to name the dates when n famous events took place. He doesn't remembers the exact dates but he remembers a segment of days [li, ri] (inclusive) on which the event could have taken place. However Vasya also remembers that there was at most one event in one day. Help him choose such n dates of famous events that will fulfill both conditions. It is guaranteed that it is possible. Input The first line contains one integer n (1 ≀ n ≀ 100) β€” the number of known events. Then follow n lines containing two integers li and ri each (1 ≀ li ≀ ri ≀ 107) β€” the earliest acceptable date and the latest acceptable date of the i-th event. Output Print n numbers β€” the dates on which the events took place. If there are several solutions, print any of them. It is guaranteed that a solution exists. Examples Input 3 1 2 2 3 3 4 Output 1 2 3 Input 2 1 3 1 3 Output 1 2
n = int(input()) arr = [0] * 10000001 ans = [0] * 100 print() a2 = [] for i in range(n): l, r = map(int, input().split()) tmp = r, l, i a2.append(tmp) a2.sort() for i in range(n): m = a2[i] r = m[0] l = m[1] v = m[2] dif = r - l + 1 for j in range(dif): if arr[l + j] == 0: arr[l + j] = 1 ans[v] = l + j break for x in range(n): print(ans[x], " ", end="")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING STRING
On a history lesson the teacher asked Vasya to name the dates when n famous events took place. He doesn't remembers the exact dates but he remembers a segment of days [li, ri] (inclusive) on which the event could have taken place. However Vasya also remembers that there was at most one event in one day. Help him choose such n dates of famous events that will fulfill both conditions. It is guaranteed that it is possible. Input The first line contains one integer n (1 ≀ n ≀ 100) β€” the number of known events. Then follow n lines containing two integers li and ri each (1 ≀ li ≀ ri ≀ 107) β€” the earliest acceptable date and the latest acceptable date of the i-th event. Output Print n numbers β€” the dates on which the events took place. If there are several solutions, print any of them. It is guaranteed that a solution exists. Examples Input 3 1 2 2 3 3 4 Output 1 2 3 Input 2 1 3 1 3 Output 1 2
n = int(input()) intervals = [list(map(int, input().split())) for i in range(n)] intervals = list(enumerate(intervals)) intervals.sort(key=lambda x: (x[1][1], x[1][0])) ans = [0] * n vis = set() for idx, (l, _) in intervals: while l in vis: l += 1 vis.add(l) ans[idx] = l print(" ".join(list(map(str, ans))))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
import sys input = sys.stdin.readline n, m = map(int, input().split()) d = [0] * m h = [0] * m for i in range(m): d[i], h[i] = map(int, input().split()) ans = max(d[0] - 1 + h[0], max(h)) for i in range(1, m): dd = d[i] - d[i - 1] hh = h[i] - h[i - 1] if abs(dd) < abs(hh): print("IMPOSSIBLE") exit() max_h = max(h[i], h[i - 1]) + (dd - abs(hh)) // 2 ans = max(ans, max_h) ans = max(ans, n - d[-1] + h[-1]) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
__author__ = "Π”Π°Π½ΠΈΠ»Π°" n, m = map(int, input().split()) l = [] for i in range(m): d, h = map(int, input().split()) l.append((d, h)) k = -1 flag = True for i in range(m - 1): b = l[i + 1][1] a = l[i][1] y = l[i + 1][0] x = l[i][0] if a + y - x >= b and a - y + x <= b: if (y - x - a + b) // 2 >= b - a: k1 = (y - x - a + b) // 2 + a if k1 > k: k = k1 else: k1 = max(a, b) if k1 > k: k = k1 else: flag = False b = l[0][0] - 1 + l[0][1] a = l[-1][1] + n - l[-1][0] if flag: print(max(k, a, b)) else: print("IMPOSSIBLE")
ASSIGN VAR STRING ASSIGN VAR 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 FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = [int(x) for x in input().split()] d = [] h = [] s = 0 amax = 0 for i in range(m): p, q = [int(x) for x in input().split()] d.append(p) h.append(q) for i in range(m - 1): if abs(h[i + 1] - h[i]) / (d[i + 1] - d[i]) > 1: print("IMPOSSIBLE") s += 1 break if s == 0: for i in range(m - 1): lshift = h[i] - 0 rshift = h[i + 1] - 0 a = d[i + 1] + rshift - (d[i] - lshift) if a // 2 > amax: amax = a // 2 b = h[0] + d[0] - 1 c = h[m - 1] + n - d[m - 1] if amax >= b and amax >= c: print(amax) elif b >= amax and b >= c: print(b) else: print(c)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
from sys import stdin input = stdin.readline n, m = (int(x) for x in input().split()) pd, ph = (int(x) for x in input().split()) res = ph + pd - 1 for i in range(1, m): d, h = (int(x) for x in input().split()) dd = d - pd dh = abs(h - ph) if dd < dh: print("IMPOSSIBLE") exit() dd -= dh res = max(res, max(ph, h) + dd // 2) pd = d ph = h res = max(res, ph + (n - pd)) print(res)
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = [int(x) for x in input().split()] L = [[int(x) for x in input().split()] for z in range(m)] maxh = max([x[1] for x in L]) def possible(a, b): if abs(a[1] - b[1]) > abs(a[0] - b[0]): return False return True def maxp(a, b): t = abs(a[0] - b[0]) t -= abs(a[1] - b[1]) m = max(a[1], b[1]) return m + t // 2 poss = True for i in range(len(L) - 1): if not possible(L[i], L[i + 1]): poss = False break maxh = max(maxh, maxp(L[i], L[i + 1])) maxh = max(maxh, L[0][0] - 1 + L[0][1], n - L[-1][0] + L[-1][1]) if poss: print(maxh) else: print("IMPOSSIBLE")
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 VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_DEF IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = map(int, input().split()) d, h = [], [] for i in range(m): di, hi = map(int, input().split()) d.append(di) h.append(hi) maximum = h[0] if d[0] != 1: d.insert(0, 1) h.insert(0, -1) m += 1 flag = 0 for i in range(1, m): diff = d[i] - d[i - 1] if h[i - 1] == -1: if diff + h[i] > maximum: maximum = diff + h[i] elif abs(h[i] - h[i - 1]) > diff: flag = 1 break elif abs(h[i] - h[i - 1]) < diff: p = diff - abs(h[i - 1] - h[i]) if max(h[i], h[i - 1]) + p // 2 > maximum: maximum = p // 2 + max(h[i], h[i - 1]) elif max(h[i], h[i - 1]) > maximum: maximum = max(h[i], h[i - 1]) if d[m - 1] < n: if maximum < h[m - 1] + n - d[m - 1]: maximum = h[m - 1] + n - d[m - 1] if flag == 1: print("IMPOSSIBLE") else: print(maximum)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = map(int, input().split()) l_d, l_h = map(int, input().split()) m_h = l_h + l_d - 1 for i in range(1, m): n_d, n_h = map(int, input().split()) if abs(n_h - l_h) > n_d - l_d: print("IMPOSSIBLE") exit(0) m_h = max(m_h, max(l_h, n_h) + (n_d - l_d - abs(n_h - l_h)) // 2) l_d, l_h = n_d, n_h m_h = max(m_h, l_h + n - l_d) print(m_h)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
N, M = list(map(int, input().split())) H = [] D = [] for i in range(M): d, h = list(map(int, input().split())) H.append(h) D.append(d) res = max(max(H), H[M - 1] + N - D[M - 1]) res = max(res, H[0] + D[0] - 1) for i in range(M - 1): dest = D[i + 1] - D[i] diff = abs(H[i + 1] - H[i]) if diff > dest: res = float("inf") break else: res = max(res, (dest - diff) // 2 + max(H[i + 1], H[i])) if res == float("inf"): print("IMPOSSIBLE") else: print(res)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = list(map(int, input().split())) l = [] for i in range(m): l.append(list(map(int, input().split()))) ans = max(l[0][1] + l[0][0] - 1, l[-1][1] + n - l[-1][0]) for i in range(m - 1): if abs(l[i][1] - l[i + 1][1]) > abs(l[i][0] - l[i + 1][0]): ans = "IMPOSSIBLE" break if l[i][1] > l[i + 1][1]: ans = max(ans, l[i][1] + (l[i + 1][0] - l[i][0] - (l[i][1] - l[i + 1][1])) // 2) else: ans = max( ans, l[i + 1][1] + (l[i + 1][0] - l[i][0] - (l[i + 1][1] - l[i][1])) // 2 ) print(ans)
ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = map(int, input().split()) main = [] for i in range(m): d, h = map(int, input().split()) main.append((d, h)) s, e = False, False for d, h in main: if d == 1: s = True elif d == n: e = True start = [] if not s: start.append((0, main[0][0] - 1 + main[0][1])) if not e: start.append((n, n - main[-1][0] + main[-1][1])) main = main + start main.sort() cd, ch = main[0][0], main[0][1] currmax = main[0][1] for d, h in main[1:]: if d - cd < abs(h - ch): currmax = "IMPOSSIBLE" break currmax = max(currmax, (d - cd + h + ch) // 2) cd = d ch = h print(currmax)
ASSIGN VAR 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 FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR EXPR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = map(int, input().split()) notes = [[int(j) for j in input().split()] for i in range(m)] ans = max([i[1] for i in notes]) if notes[0][0] != 1: ans = max(ans, notes[0][0] - 1 + notes[0][1]) if notes[-1][0] != n: ans = max(ans, n - notes[-1][0] + notes[-1][1]) isOk = True for a, b in zip(notes, notes[1:]): difference = b[0] - a[0] - 1 descentNeeded = abs(b[1] - a[1]) if descentNeeded > difference + 1: isOk = False break x = max(a[1], b[1]) chng = (difference - descentNeeded + 1) // 2 ans = max(ans, x + chng) if not isOk: print("IMPOSSIBLE") else: print(ans)
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 VAR NUMBER VAR VAR IF VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
def isOk(a0, a1): i0, h0 = a0 i1, h1 = a1 if h1 < h0: return isOk((i0, h1), (i1, h0)) else: return i1 - i0 >= h1 - h0 def getMaxHeight(a0, a1): i0, h0 = a0 i1, h1 = a1 d = h1 - h0 s = i1 - i0 if h1 < h0: return getMaxHeight((i0, h1), (i1, h0)) elif h0 < h1: return getMaxHeight((i0 + d, h0 + d), (i1, h1)) else: return h0 + s // 2 def getMax1Height(a): i0, h0 = a return h0 + i0 - 1 def getMaxNHeight(n, a): i0, h0 = a return h0 + (n - i0) s = input() hlist = list() [n, m] = list(map(int, s.split(" "))) for i in range(0, m): s = input() [b, h] = list(map(int, s.split(" "))) hlist.append((b, h)) iscan = True for i in range(1, m): if not isOk(hlist[i - 1], hlist[i]): iscan = False break if iscan == False: print("IMPOSSIBLE") else: res = getMax1Height(hlist[0]) res = max(res, getMaxNHeight(n, hlist[m - 1])) for i in range(1, m): res = max(res, getMaxHeight(hlist[i - 1], hlist[i])) print(res)
FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR RETURN BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = map(int, input().split()) res, pd, ph = 0, 0, 0 for i in range(m): d, h = map(int, input().split()) if i == 0: res = h + (d - 1) else: delta_d = d - pd delta_h = abs(h - ph) if delta_h > delta_d: print("IMPOSSIBLE") exit() res = max(res, max(h, ph) + (delta_d - delta_h) // 2) if i == m - 1: res = max(res, h + n - d) pd, ph = d, h print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
import sys n, m = map(int, str.split(sys.stdin.readline())) pd = ph = None top = None for _ in range(m): d, h = map(int, str.split(sys.stdin.readline())) if pd is None: top = d - 1 + h else: if pd and d - pd < abs(h - ph): print("IMPOSSIBLE") exit() delta = d - pd - 1 - abs(h - ph) top = max(top, max(ph, h) + delta // 2 + delta % 2) pd, ph = d, h top = max(top, h + n - d) print(top)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NONE ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NONE ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = map(int, input().split()) prevd, prevh = map(int, input().split()) maxi = prevd - 1 + prevh check = 0 for i in range(m - 1): d, h = map(int, input().split()) if d - prevd >= abs(prevh - h): k = (d - prevd - abs(prevh - h)) // 2 if maxi < max(prevh + k, h + k): maxi = max(prevh + k, h + k) prevd, prevh = d, h else: check = 1 break if check: print("IMPOSSIBLE") elif maxi >= prevh + n - prevd: print(maxi) else: print(prevh + n - prevd)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING IF VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = input().split() n = int(n) m = int(m) arr = [] for i in range(m): d, h = input().split() arr.append([int(d), int(h)]) last = arr[0] res = arr[0][1] for i in range(1, m): if arr[i][0] - last[0] < abs(arr[i][1] - last[1]): print("IMPOSSIBLE") exit() else: diff = arr[i][1] - last[1] res = max(res, (arr[i][0] - last[0] + diff) // 2 + last[1]) res = max(res, arr[i][1]) last = arr[i] print( max( arr[0][1] + arr[0][0] - 1, max(res, n - arr[len(arr) - 1][0] + arr[len(arr) - 1][1]), ) )
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = list(map(int, input().split(" "))) d = [] for i in range(m): d.append(list(map(int, input().split(" ")))) ispossible = True maxheights = [] maxheights.append(d[0][1] + d[0][0] - 1) maxheights.append(d[-1][1] + n - d[-1][0]) for i in range(m - 1): d1 = d[i] d2 = d[i + 1] if abs(d2[1] - d1[1]) > d2[0] - d1[0]: ispossible = False maxheights.append((d1[1] + d2[1] + d2[0] - d1[0]) // 2) if ispossible: print(max(maxheights)) else: print("IMPOSSIBLE")
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING 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 STRING ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
def f(d1, h1, d2, h2): lo, hi = max(0, h1 - (d2 - d1)), min(10**8, h1 + (d2 - d1)) if h2 < lo or h2 > hi: return False, float("-inf") else: if h1 < h2: h1, d1 = h2, d1 + (h2 - h1) elif h1 > h2: h2, d2 = h1, d2 - (h1 - h2) return True, h1 + (d2 - d1) // 2 def solve(a, n, m): ans = float("-inf") for i in range(m - 1): possible, maxheight = f(a[i][0], a[i][1], a[i + 1][0], a[i + 1][1]) if not possible: return float("-inf") else: ans = max(ans, maxheight) return max(ans, a[0][1] + a[0][0] - 1, a[-1][1] + n - a[-1][0]) n, m = map(int, input().split()) a = [] for i in range(m): d, h = map(int, input().split()) a.append((d, h)) ans = solve(a, n, m) if ans == float("-inf"): print("IMPOSSIBLE") else: print(ans)
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER BIN_OP VAR BIN_OP VAR VAR IF VAR VAR VAR VAR RETURN NUMBER FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR RETURN NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR RETURN FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR 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 FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
def get(d1, h1, d2, h2): d_d = d2 - d1 if abs(h2 - h1) > d_d: return None res = (d_d - abs(h2 - h1)) // 2 + max(h1, h2) return res n, m = tuple(map(int, input().split())) l = [(0, 0)] * (m + 1) for i in range(1, m + 1): l[i] = tuple(map(int, input().split())) maxi = 0 for i in range(1, m): tmp = get(l[i][0], l[i][1], l[i + 1][0], l[i + 1][1]) if tmp == None: print("IMPOSSIBLE") exit(0) if tmp > maxi: maxi = tmp if l[m][1] + n - l[m][0] > maxi: maxi = l[m][1] + n - l[m][0] if l[1][1] + l[1][0] - 1 > maxi: maxi = l[1][1] + l[1][0] - 1 print(maxi)
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN NONE ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NONE EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
import sys f = sys.stdin n, m = map(int, f.readline().strip().split()) res = True for i in range(m): d, h = map(int, f.readline().strip().split()) if i == 0: maxH = h + (d - 1) hpr = h dpr = d elif abs(h - hpr) > d - dpr: res = False else: dt = d - dpr - (max(hpr, h) - min(hpr, h)) maxH = max(maxH, max(hpr, h) + dt // 2) hpr = h dpr = d maxH = max(maxH, hpr + (n - dpr)) if res: print(maxH) else: print("IMPOSSIBLE")
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
N, M = list(map(int, input().split())) H = [] for i in range(M): d, h = list(map(int, input().split())) H.append((d, h)) best = H[0][0] - 1 + H[0][1] for i in range(M - 1): diffd = H[i + 1][0] - H[i][0] diffh = abs(H[i + 1][1] - H[i][1]) if diffd >= diffh: best = max(best, max(H[i + 1][1], H[i][1]) + (diffd - diffh) // 2) else: print("IMPOSSIBLE") break else: best = max(best, H[M - 1][1] + (N - H[M - 1][0])) print(best)
ASSIGN VAR 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = list(map(int, input().split())) max_height = 0 prev_d, prev_h = [0, 0] for i in range(m): d, h = list(map(int, input().split())) if i == 0: max_height = d - 1 + h prev_d = d prev_h = h if i == m - 1: max_height = max(max_height, n - d + h) continue intersection = (-1 * (prev_d - prev_h) + (d + h)) / 2 day = intersection - prev_h + prev_d if day > d or day < prev_d: max_height = -1 break max_height = max(max_height, int(intersection)) if i == m - 1: max_height = max(max_height, n - d + h) prev_d = d prev_h = h if max_height >= 0: print(max_height) else: print("IMPOSSIBLE")
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
import sys def main(): import sys tokens = [int(i) for i in sys.stdin.read().split()] tokens.reverse() result = 0 n, m = tokens.pop(), tokens.pop() pd, ph = 0, 0 for i in range(m): d, h = tokens.pop(), tokens.pop() if i == 0: result = h + (d - 1) else: delta_d = d - pd delta_h = abs(h - ph) if delta_h > delta_d: print("IMPOSSIBLE") return else: result = max(result, max(h, ph) + (delta_d - delta_h) // 2) if i == m - 1: result = max(result, h + (n - d)) pd, ph = d, h print(result) main()
IMPORT FUNC_DEF IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
R = lambda: map(int, input().split()) n, m = R() ls = [tuple(R()) for _ in range(m)] res = max(ls[0][1] + ls[0][0] - 1, ls[-1][-1] + n - ls[-1][0]) for i in range(1, m): d, h = ls[i][0] - ls[i - 1][0], abs(ls[i][1] - ls[i - 1][1]) if d < h: print("IMPOSSIBLE") exit() else: res = max(res, (d - h) // 2 + max(ls[i][1], ls[i - 1][1])) print(res)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = map(int, input().split()) notes = [list(map(int, input().split())) for _ in range(m)] if m == 1: d, h = notes[0] print(max(h + (d - 1), h + (n - d))) exit(0) d0, h0 = notes[0] ans = h0 + (d0 - 1) for i in range(m - 1): d1, h1 = notes[i] d2, h2 = notes[i + 1] if d2 - d1 < abs(h1 - h2): ans = -1 break else: ans = max((h1 + h2 + (d2 - d1)) // 2, ans) else: dm, hm = notes[m - 1] ans = max(ans, hm + (n - dm)) print(ans if ans != -1 else "IMPOSSIBLE")
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 FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR STRING
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = [int(x) for x in input().split()] l = [] h = [] for i in range(m): d, hi = [int(x) for x in input().split()] l.append([d, hi]) h.append(hi) jud = 0 for i in range(1, m): if abs(l[i][1] - l[i - 1][1]) > l[i][0] - l[i - 1][0]: jud = 1 break else: d1 = l[i - 1][0] d2 = l[i][0] h1 = l[i - 1][1] h2 = l[i][1] hmax = max(h1, h2) hmin = min(h1, h2) disd = abs(d1 - d2) dish = -abs(h1 - h2) x = dish + disd h.append(int(hmax + (disd + dish) / 2)) if jud == 1: print("IMPOSSIBLE") else: h.append(l[0][1] + l[0][0] - 1) h.append(l[-1][1] + n - l[-1][0]) print(max(h))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = map(int, input().split(" ")) diary = list() for i in range(m): diary.append(list(map(int, input().split(" ")))) actual_distance = [abs(diary[i + 1][1] - diary[i][1]) for i in range(len(diary) - 1)] max_distance = [(diary[i + 1][0] - diary[i][0]) for i in range(len(diary) - 1)] difference = [(i[1] - i[0]) for i in zip(actual_distance, max_distance)] max_unknown_hight_left = [diary[0][1] + diary[0][0] - 1] max_unknown_hight_right = [diary[-1][1] + (n - diary[-1][0])] if difference != []: if min(difference) < 0: print("IMPOSSIBLE") else: max_unknown_hights_between = [ (diary[i][1] + max_distance[i] // 2 + (diary[i + 1][1] - diary[i][1]) // 2) for i in range(len(diary) - 1) ] all_hights = ( [diary[i][1] for i in range(len(diary))] + max_unknown_hights_between + max_unknown_hight_left + max_unknown_hight_right ) print(max(all_hights)) else: all_hights = ( [diary[i][1] for i in range(len(diary))] + max_unknown_hight_left + max_unknown_hight_right ) print(max(all_hights))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR LIST IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = map(int, input().split()) ls = [] for i in range(m): d, h = map(int, input().split()) ls.append([d, h]) ls.sort() max_h = 0 flag = True max_h = max(max_h, ls[0][0] - 1 + ls[0][1]) max_h = max(max_h, n - ls[m - 1][0] + ls[m - 1][1]) for i in range(m - 1): max_h = max(max_h, ls[i][1]) if ( ls[i + 1][1] - ls[i][1] > ls[i + 1][0] - ls[i][0] or ls[i][1] - ls[i + 1][1] > ls[i + 1][0] - ls[i][0] ): flag = False break else: x = (ls[i + 1][1] - ls[i][1] + ls[i + 1][0] - ls[i][0]) // 2 max_h = max(max_h, x + ls[i][1]) max_h = max(max_h, ls[m - 1][1]) if flag: print(max_h) else: print("IMPOSSIBLE")
ASSIGN VAR 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 FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
import sys input = sys.stdin.readline def judge(d, hs, he, x): return 2 * x - hs - he <= d def binary_search(d, hs, he): l, r = max(hs, he), 10**10 while l <= r: mid = (l + r) // 2 if judge(d, hs, he, mid): l = mid + 1 else: r = mid - 1 return r n, m = map(int, input().split()) dh = [tuple(map(int, input().split())) for _ in range(m)] ans = dh[0][1] + dh[0][0] - 1 for i in range(m - 1): di, hi = dh[i] dj, hj = dh[i + 1] if abs(hi - hj) > abs(di - dj): print("IMPOSSIBLE") exit() ans = max(ans, binary_search(dj - di, hi, hj)) ans = max(ans, dh[-1][1] + n - dh[-1][0]) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER 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 VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
import sys 3 n, m = list(map(int, input().split())) firstData = None maxHeight = -1 for i in range(m): d, h = list(map(int, input().split())) if firstData is None: firstData = d, h else: if d - prevD < abs(h - prevH): print("IMPOSSIBLE") return maxH = max(h, prevH) minH = min(h, prevH) resource = d - prevD - (maxH - minH) possibleH = maxH + resource // 2 maxHeight = max(maxHeight, possibleH) prevD, prevH = d, h lastData = d, h maxHeight = max(maxHeight, firstData[1] + firstData[0] - 1) maxHeight = max(maxHeight, lastData[1] + (n - lastData[0])) print(maxHeight)
IMPORT EXPR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NONE ASSIGN VAR VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = map(int, input().split()) possible = True day_prev, h_prev = map(int, input().split()) h_max = h_prev + (day_prev - 1) for i in range(m - 1): day, h = map(int, input().split()) if abs(h - h_prev) > day - day_prev: possible = False break h_new = max(h, h_prev) + (day - day_prev - abs(h - h_prev)) // 2 h_max = max(h_max, h_new) day_prev, h_prev = day, h h_max = max(h_max, h_prev + (n - day_prev)) if possible: print(h_max) else: print("IMPOSSIBLE")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = [int(x) for x in input().split()] a = [] ans = 0 while m > 0: p = [int(x) for x in input().split()] a.append(p) ans = max(ans, p[1]) m -= 1 flag = 1 for i in range(len(a) - 1): x, y = a[i], a[i + 1] t = y[0] - x[0] - 1 if abs(y[1] - x[1]) > y[0] - x[0]: flag = 0 break else: ans = max(ans, max(x[1], y[1]) + (y[0] - x[0] - abs(y[1] - x[1])) // 2) ans = max(ans, a[0][1] + a[0][0] - 1) ans = max(ans, a[len(a) - 1][1] + (n - a[len(a) - 1][0])) if flag: print(ans) else: print("IMPOSSIBLE")
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
def possible(d1, h1, d2, h2): if d2 - d1 < abs(h2 - h1): return -1 elif h1 == h2: return (d2 - d1) // 2 + h1 elif h1 > h2: diff = h1 - h2 newd2 = d2 - diff if newd2 < d1: return -1 else: return (newd2 - d1) // 2 + h1 else: diff = h2 - h1 newd1 = d1 + diff if newd1 > d2: return -1 else: return (d2 - newd1) // 2 + h2 info = input().split() n = int(info[0]) m = int(info[1]) first = input().split() oldd = int(first[0]) oldh = int(first[1]) answer = oldd - 1 + oldh bad = False for i in range(m - 1): inf = input().split() newd = int(inf[0]) newh = int(inf[1]) pos = possible(oldd, oldh, newd, newh) if pos == -1: print("IMPOSSIBLE") bad = True break elif pos > answer: answer = pos oldd = newd oldh = newh if not bad: endheight = n - oldd + oldh if endheight > answer: answer = endheight print(answer)
FUNC_DEF IF BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER IF VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR 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 FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER 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 VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = map(int, input().split()) possible = True s, a = map(int, input().split()) highest = a + s - 1 for i in range(1, m): t, b = map(int, input().split()) slack = t - s - abs(a - b) if slack < 0: possible = False else: highest = max(highest, max(a, b) + slack // 2) s, a = t, b highest = max(highest, a + n - s) print(highest if possible else "IMPOSSIBLE")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = map(int, input().split()) h = [None] * m def calc(h, num): r = -1 for i in range(len(h) - 1): n = h[i + 1][0] - h[i][0] hd = h[i + 1][1] - h[i][1] s = (n + hd) % 2 if n + hd < s or n - hd < s: return "IMPOSSIBLE" u = (hd + n - s) // 2 r = max(r, h[i][1] + u) l = len(h) - 1 ld = h[l][0] lh = h[l][1] r = max(r, lh + num - ld) r = max(r, h[0][1] + h[0][0] - 1) return r for i in range(m): d, v = map(int, input().split()) h[i] = d, v print(calc(h, n))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = list(map(int, input().rstrip().split())) d = [] h = [] flag = 1 for i in range(m): a, b = list(map(int, input().rstrip().split())) d += [a] h += [b] if i > 0 and abs(d[i] - d[i - 1]) < abs(h[i] - h[i - 1]): flag = 0 if flag == 0: print("IMPOSSIBLE") else: maxm = 0 for i in range(m - 1): days = d[i + 1] - d[i] - 1 high = abs(h[i + 1] - h[i]) days_left = days - high maxm = max(maxm, max(h[i], h[i + 1]) + days_left // 2 + days_left % 2) diff = d[0] - 1 maxm = max(maxm, h[0] + diff) diff = n - d[m - 1] maxm = max(maxm, h[m - 1] + diff) print(maxm)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR LIST VAR VAR LIST VAR IF VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
n, m = map(int, input().split()) ans = 0 h1 = 0 h2 = 0 d1 = 0 d2 = 0 for i in range(m): x, y = map(int, input().split()) if i == 0: ans = y + x - 1 h1 = y d1 = x else: h2 = y d2 = x if h1 > h2: h1, h2 = h2, h1 t1 = h2 + (d2 - d1 - (h2 - h1)) // 2 if t1 < h2: print("IMPOSSIBLE") exit() ans = max(ans, t1) h1 = y d1 = x ans = max(ans, h1 + n - d1) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
[n, k] = [int(x) for x in input().split()] Data = type("Data", (object,), {"index": 0, "value": 0}) data = [Data() for _ in range(k)] for i in range(k): [data[i].index, data[i].value] = [int(x) for x in input().split()] data.sort(key=lambda x: x.index) ans = max(data[0].value + data[0].index - 1, data[k - 1].value + n - data[k - 1].index) for i in range(1, k): L = data[i].index - data[i - 1].index minH = min(data[i].value, data[i - 1].value) maxV = max(data[i].value, data[i - 1].value) if minH + L < maxV: ans = -1 break ans = max(ans, (L + minH + maxV) // 2) if ans < 0: print("IMPOSSIBLE") else: print(ans)
ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING VAR DICT STRING STRING NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR 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 VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
def main(): n, m = map(int, input().split()) d0, h0 = map(int, input().split()) res = [h0 + d0 - 1] for i in range(m - 1): d1, h1 = map(int, input().split()) dd = d1 - d0 if h1 > h0: base = h1 dh = h1 - h0 else: base = h0 dh = h0 - h1 if dh > dd: print("IMPOSSIBLE") return res.append(base + (dd - dh) // 2) d0, h0 = d1, h1 res.append(h0 + n - d0) print(max(res)) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer h_{i}. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |h_{i} - h_{i} + 1| ≀ 1 holds. At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |h_{i} - h_{i} + 1| ≀ 1. -----Input----- The first line contains two space-separated numbers, n and m (1 ≀ n ≀ 10^8, 1 ≀ m ≀ 10^5)Β β€” the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers d_{i} and h_{d}_{i} (1 ≀ d_{i} ≀ n, 0 ≀ h_{d}_{i} ≀ 10^8)Β β€” the number of the day when the i-th note was made and height on the d_{i}-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: d_{i} < d_{i} + 1. -----Output----- If the notes aren't contradictory, print a single integer β€” the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). -----Examples----- Input 8 2 2 0 7 0 Output 2 Input 8 3 2 0 7 0 8 3 Output IMPOSSIBLE -----Note----- For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1). In the second sample the inequality between h_7 and h_8 does not hold, thus the information is inconsistent.
x = input() n, m = x.strip().split(" ") n, m = int(n), int(m) x = input() d, h = x.strip().split(" ") d, h = int(d), int(h) max_height = h + d - 1 last_day = d last_height = h posible = True for k in range(m - 1): x = input() d, h = x.strip().split() d, h = int(d), int(h) if d - last_day >= abs(last_height - h): mi = max(last_height, h) prostih = d - last_day - abs(last_height - h) pos_h = mi + prostih // 2 if pos_h > max_height: max_height = pos_h else: posible = False last_day = d last_height = h manjka_dni = n - d if last_height + manjka_dni > max_height: max_height = last_height + manjka_dni if posible: print(max_height) else: print("IMPOSSIBLE")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING