description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Read problem statements in [Russian] and [Mandarin Chinese].
Chef goes to a candy store that sells N different candies. For each candy, we know its price and sweetness. Chef has D dollars and can take a maximum of 2 candies, one in each hand. Find the maximum total sweetness he can buy under the given constraints.
NOTE: Since the input-output is large, prefer using fast input-output methods.
------ Input Format ------
- The first line contains an integer T, the number of test cases. Then the test cases follow.
- Each test case contains three lines of input.
- First line will contain 2 space separated integers N and D, the number of different candies and the amount of money Chef has.
- Second line contains N space separated integers P_{1}, P_{2}, \ldots, P_{N}, where P_{i} is the price of the i^{th} candy.
- Third line contains N space separated integers S_{1}, S_{2}, \ldots, S_{N}, where S_{i} is the sweetness of the i^{th} candy.
------ Output Format ------
For each testcase, output in a single line, the maximum total sweetness Chef can buy with the money he has, and with at most two candies.
------ Constraints ------
$1 β€ T β€ 2.5*10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€ D β€ 10^{9}$
$1 β€ P_{i}, S_{i} β€ 10^{9}$
- Sum $N$ over all testcases is atmost $10^{6}$.
----- Sample Input 1 ------
3
2 10
1 2
2 1
5 7
1 2 3 4 5
1 2 3 4 5
5 7
6 6 6 6 6
5 4 3 2 1
----- Sample Output 1 ------
3
7
5
----- explanation 1 ------
TestCase $1$: Chef can collect both the candies available with the money he has.
TestCase $2$: Chef can collect candies at index $[2, 5]$ or $[3, 4]$. In both cases, he will get the total sweetness of $7$.
TestCase $3$: Since the price of all candies is the same, it's always optimal to choose the candies with maximum sweetness. Also, in this case, no more than one candy can be chosen. | from sys import stdin
input = stdin.readline
def b_s(key, l, h):
ans = 0
while l <= h:
mid = (l + h) // 2
if a[mid][0] <= key:
ans = max(ans, pf[mid])
l = mid + 1
else:
h = mid - 1
return ans
def answer():
ans = 0
for i in range(n - 1, -1, -1):
x = a[i][0]
if x > d:
continue
m_v = b_s(d - x, 0, i - 1)
ans = max(ans, a[i][1] + m_v)
return ans
for T in range(int(input())):
n, d = map(int, input().split())
p = list(map(int, input().split()))
s = list(map(int, input().split()))
a = [[p[i], s[i]] for i in range(n)]
a.sort(key=lambda x: x[0])
pf = [a[0][1]]
for i in range(1, n):
pf.append(max(pf[-1], a[i][1]))
print(answer()) | ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER 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 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 VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR |
There are two bus stops denoted A and B, and there n buses that go from A to B every day. The shortest path from A to B takes t units of time but some buses might take longer paths. Moreover, buses are allowed to overtake each other during the route.
At each station one can find a sorted list of moments of time when a bus is at this station. We denote this list as a_1 < a_2 < β¦ < a_n for stop A and as b_1 < b_2 < β¦ < b_n for stop B. The buses always depart from A and arrive to B according to the timetable, but the order in which the buses arrive may differ. Let's call an order of arrivals valid if each bus arrives at least t units of time later than departs.
It is known that for an order to be valid the latest possible arrival for the bus that departs at a_i is b_{x_i}, i.e. x_i-th in the timetable. In other words, for each i there exists such a valid order of arrivals that the bus departed i-th arrives x_i-th (and all other buses can arrive arbitrary), but there is no valid order of arrivals in which the i-th departed bus arrives (x_i + 1)-th.
Formally, let's call a permutation p_1, p_2, β¦, p_n valid, if b_{p_i} β₯ a_i + t for all i. Then x_i is the maximum value of p_i among all valid permutations.
You are given the sequences a_1, a_2, β¦, a_n and x_1, x_2, β¦, x_n, but not the arrival timetable. Find out any suitable timetable for stop B b_1, b_2, β¦, b_n or determine that there is no such timetable.
Input
The first line of the input contains two integers n and t (1 β€ n β€ 200 000, 1 β€ t β€ 10^{18}) β the number of buses in timetable for and the minimum possible travel time from stop A to stop B.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_1 < a_2 < β¦ < a_n β€ 10^{18}), defining the moments of time when the buses leave stop A.
The third line contains n integers x_1, x_2, β¦, x_n (1 β€ x_i β€ n), the i-th of them stands for the maximum possible timetable position, at which the i-th bus leaving stop A can arrive at stop B.
Output
If a solution exists, print "Yes" (without quotes) in the first line of the output.
In the second line print n integers b_1, b_2, β¦, b_n (1 β€ b_1 < b_2 < β¦ < b_n β€ 3 β
10^{18}). We can show that if there exists any solution, there exists a solution that satisfies such constraints on b_i. If there are multiple valid answers you can print any of them.
If there is no valid timetable, print "No" (without quotes) in the only line of the output.
Examples
Input
3 10
4 6 8
2 2 3
Output
Yes
16 17 21
Input
2 1
1 2
2 1
Output
No
Note
Consider the first example and the timetable b_1, b_2, β¦, b_n from the output.
To get x_1 = 2 the buses can arrive in the order (2, 1, 3). To get x_2 = 2 and x_3 = 3 the buses can arrive in the order (1, 2, 3). x_1 is not 3, because the permutations (3, 1, 2) and (3, 2, 1) (all in which the 1-st bus arrives 3-rd) are not valid (sube buses arrive too early), x_2 is not 3 because of similar reasons. | n, t = map(int, input().split(" "))
aa = list(map(int, input().split(" ")))
xx = list(map(int, input().split(" ")))
res = [0] * n
prevX = 0
prevV = -10
for i, (a, x) in enumerate(zip(aa, xx)):
x -= 1
if x < prevX or x < i:
print("No")
exit(0)
curV = max(aa[i + 1] + t if x > i else aa[i] + t, prevV + 1)
res[i] = curV
prevX = x
prevV = curV
for i, (a, x) in enumerate(zip(aa, xx)):
x -= 1
if x + 1 < n:
if res[x] >= aa[x + 1] + t:
print("No")
exit(0)
print("Yes")
print(*res) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
There are two bus stops denoted A and B, and there n buses that go from A to B every day. The shortest path from A to B takes t units of time but some buses might take longer paths. Moreover, buses are allowed to overtake each other during the route.
At each station one can find a sorted list of moments of time when a bus is at this station. We denote this list as a_1 < a_2 < β¦ < a_n for stop A and as b_1 < b_2 < β¦ < b_n for stop B. The buses always depart from A and arrive to B according to the timetable, but the order in which the buses arrive may differ. Let's call an order of arrivals valid if each bus arrives at least t units of time later than departs.
It is known that for an order to be valid the latest possible arrival for the bus that departs at a_i is b_{x_i}, i.e. x_i-th in the timetable. In other words, for each i there exists such a valid order of arrivals that the bus departed i-th arrives x_i-th (and all other buses can arrive arbitrary), but there is no valid order of arrivals in which the i-th departed bus arrives (x_i + 1)-th.
Formally, let's call a permutation p_1, p_2, β¦, p_n valid, if b_{p_i} β₯ a_i + t for all i. Then x_i is the maximum value of p_i among all valid permutations.
You are given the sequences a_1, a_2, β¦, a_n and x_1, x_2, β¦, x_n, but not the arrival timetable. Find out any suitable timetable for stop B b_1, b_2, β¦, b_n or determine that there is no such timetable.
Input
The first line of the input contains two integers n and t (1 β€ n β€ 200 000, 1 β€ t β€ 10^{18}) β the number of buses in timetable for and the minimum possible travel time from stop A to stop B.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_1 < a_2 < β¦ < a_n β€ 10^{18}), defining the moments of time when the buses leave stop A.
The third line contains n integers x_1, x_2, β¦, x_n (1 β€ x_i β€ n), the i-th of them stands for the maximum possible timetable position, at which the i-th bus leaving stop A can arrive at stop B.
Output
If a solution exists, print "Yes" (without quotes) in the first line of the output.
In the second line print n integers b_1, b_2, β¦, b_n (1 β€ b_1 < b_2 < β¦ < b_n β€ 3 β
10^{18}). We can show that if there exists any solution, there exists a solution that satisfies such constraints on b_i. If there are multiple valid answers you can print any of them.
If there is no valid timetable, print "No" (without quotes) in the only line of the output.
Examples
Input
3 10
4 6 8
2 2 3
Output
Yes
16 17 21
Input
2 1
1 2
2 1
Output
No
Note
Consider the first example and the timetable b_1, b_2, β¦, b_n from the output.
To get x_1 = 2 the buses can arrive in the order (2, 1, 3). To get x_2 = 2 and x_3 = 3 the buses can arrive in the order (1, 2, 3). x_1 is not 3, because the permutations (3, 1, 2) and (3, 2, 1) (all in which the 1-st bus arrives 3-rd) are not valid (sube buses arrive too early), x_2 is not 3 because of similar reasons. | n, t = map(int, input().split())
a = [0] + list(map(int, input().split()))
x = [0] + list(map(int, input().split()))
def assign_value(i, bstatus, val):
if bstatus[i] * val == -1:
return False
else:
bstatus[i] = val
return True
def f(n, t, a, x):
bstatus = [0] * (n + 1)
curr = 0
for i in range(1, n + 1):
if curr > i:
if x[i] != curr:
return False
bstatus[i] = 1
elif curr == i:
if x[i] != curr:
return False
bstatus[i] = -1
else:
curr = x[i]
if curr > i:
bstatus[i] = 1
elif curr < i:
return False
s = ""
val = 0
for i in range(1, n):
if bstatus[i] == 1:
val = max(val + 1, a[i + 1] + t)
elif bstatus[i] == -1:
val = val + 1
if val >= a[i + 1] + t:
return False
elif bstatus[i] == 0:
val = max(val + 1, a[i] + t)
s += str(val) + " "
val = max(val + 1, a[-1] + t)
s += str(val)
return s
status = f(n, t, a, x)
if not status:
print("No")
else:
print("Yes")
print(status) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF BIN_OP VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
There are two bus stops denoted A and B, and there n buses that go from A to B every day. The shortest path from A to B takes t units of time but some buses might take longer paths. Moreover, buses are allowed to overtake each other during the route.
At each station one can find a sorted list of moments of time when a bus is at this station. We denote this list as a_1 < a_2 < β¦ < a_n for stop A and as b_1 < b_2 < β¦ < b_n for stop B. The buses always depart from A and arrive to B according to the timetable, but the order in which the buses arrive may differ. Let's call an order of arrivals valid if each bus arrives at least t units of time later than departs.
It is known that for an order to be valid the latest possible arrival for the bus that departs at a_i is b_{x_i}, i.e. x_i-th in the timetable. In other words, for each i there exists such a valid order of arrivals that the bus departed i-th arrives x_i-th (and all other buses can arrive arbitrary), but there is no valid order of arrivals in which the i-th departed bus arrives (x_i + 1)-th.
Formally, let's call a permutation p_1, p_2, β¦, p_n valid, if b_{p_i} β₯ a_i + t for all i. Then x_i is the maximum value of p_i among all valid permutations.
You are given the sequences a_1, a_2, β¦, a_n and x_1, x_2, β¦, x_n, but not the arrival timetable. Find out any suitable timetable for stop B b_1, b_2, β¦, b_n or determine that there is no such timetable.
Input
The first line of the input contains two integers n and t (1 β€ n β€ 200 000, 1 β€ t β€ 10^{18}) β the number of buses in timetable for and the minimum possible travel time from stop A to stop B.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_1 < a_2 < β¦ < a_n β€ 10^{18}), defining the moments of time when the buses leave stop A.
The third line contains n integers x_1, x_2, β¦, x_n (1 β€ x_i β€ n), the i-th of them stands for the maximum possible timetable position, at which the i-th bus leaving stop A can arrive at stop B.
Output
If a solution exists, print "Yes" (without quotes) in the first line of the output.
In the second line print n integers b_1, b_2, β¦, b_n (1 β€ b_1 < b_2 < β¦ < b_n β€ 3 β
10^{18}). We can show that if there exists any solution, there exists a solution that satisfies such constraints on b_i. If there are multiple valid answers you can print any of them.
If there is no valid timetable, print "No" (without quotes) in the only line of the output.
Examples
Input
3 10
4 6 8
2 2 3
Output
Yes
16 17 21
Input
2 1
1 2
2 1
Output
No
Note
Consider the first example and the timetable b_1, b_2, β¦, b_n from the output.
To get x_1 = 2 the buses can arrive in the order (2, 1, 3). To get x_2 = 2 and x_3 = 3 the buses can arrive in the order (1, 2, 3). x_1 is not 3, because the permutations (3, 1, 2) and (3, 2, 1) (all in which the 1-st bus arrives 3-rd) are not valid (sube buses arrive too early), x_2 is not 3 because of similar reasons. | def read():
return list(map(int, input().split(" ")))
def fail():
print("No")
exit(0)
n, t = read()
aa = read()
xx = read()
res = [0] * n
prevX = 0
prevV = -10
for i in range(n):
x = xx[i] - 1
if x < prevX or x < i:
fail()
prevX = x
res[i] = prevV = max(aa[i + 1] + t if x > i else aa[i] + t, prevV + 1)
for i in range(n):
x = xx[i] - 1
if x + 1 < n and res[x] >= aa[x + 1] + t:
fail()
print("Yes")
print(*res) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
There are two bus stops denoted A and B, and there n buses that go from A to B every day. The shortest path from A to B takes t units of time but some buses might take longer paths. Moreover, buses are allowed to overtake each other during the route.
At each station one can find a sorted list of moments of time when a bus is at this station. We denote this list as a_1 < a_2 < β¦ < a_n for stop A and as b_1 < b_2 < β¦ < b_n for stop B. The buses always depart from A and arrive to B according to the timetable, but the order in which the buses arrive may differ. Let's call an order of arrivals valid if each bus arrives at least t units of time later than departs.
It is known that for an order to be valid the latest possible arrival for the bus that departs at a_i is b_{x_i}, i.e. x_i-th in the timetable. In other words, for each i there exists such a valid order of arrivals that the bus departed i-th arrives x_i-th (and all other buses can arrive arbitrary), but there is no valid order of arrivals in which the i-th departed bus arrives (x_i + 1)-th.
Formally, let's call a permutation p_1, p_2, β¦, p_n valid, if b_{p_i} β₯ a_i + t for all i. Then x_i is the maximum value of p_i among all valid permutations.
You are given the sequences a_1, a_2, β¦, a_n and x_1, x_2, β¦, x_n, but not the arrival timetable. Find out any suitable timetable for stop B b_1, b_2, β¦, b_n or determine that there is no such timetable.
Input
The first line of the input contains two integers n and t (1 β€ n β€ 200 000, 1 β€ t β€ 10^{18}) β the number of buses in timetable for and the minimum possible travel time from stop A to stop B.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_1 < a_2 < β¦ < a_n β€ 10^{18}), defining the moments of time when the buses leave stop A.
The third line contains n integers x_1, x_2, β¦, x_n (1 β€ x_i β€ n), the i-th of them stands for the maximum possible timetable position, at which the i-th bus leaving stop A can arrive at stop B.
Output
If a solution exists, print "Yes" (without quotes) in the first line of the output.
In the second line print n integers b_1, b_2, β¦, b_n (1 β€ b_1 < b_2 < β¦ < b_n β€ 3 β
10^{18}). We can show that if there exists any solution, there exists a solution that satisfies such constraints on b_i. If there are multiple valid answers you can print any of them.
If there is no valid timetable, print "No" (without quotes) in the only line of the output.
Examples
Input
3 10
4 6 8
2 2 3
Output
Yes
16 17 21
Input
2 1
1 2
2 1
Output
No
Note
Consider the first example and the timetable b_1, b_2, β¦, b_n from the output.
To get x_1 = 2 the buses can arrive in the order (2, 1, 3). To get x_2 = 2 and x_3 = 3 the buses can arrive in the order (1, 2, 3). x_1 is not 3, because the permutations (3, 1, 2) and (3, 2, 1) (all in which the 1-st bus arrives 3-rd) are not valid (sube buses arrive too early), x_2 is not 3 because of similar reasons. | n, t = map(int, input().split())
A = list(map(int, input().split()))
X = list(map(int, input().split()))
X = [(x - 1) for x in X]
L = [0] * n
R = [0] * n
S = [0] * (n + 1)
for i in range(n):
if X[i] < i:
print("No")
exit()
d = X[i] - i
if i >= 1:
S[i] += S[i - 1]
S[i] += 1
S[i + d] -= 1
if X[i] != n - 1:
R[i + d] = A[i + d + 1] + t - 1
if S[i]:
if i >= 1:
L[i] = max(A[i + 1] + t, L[i - 1] + 1)
else:
L[i] = A[i + 1] + t
elif i >= 1:
L[i] = max(A[i] + t, L[i - 1] + 1)
else:
L[i] = A[i] + t
if R[i]:
if L[i] > R[i]:
print("No")
exit()
print("Yes")
print(*L) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
There are two bus stops denoted A and B, and there n buses that go from A to B every day. The shortest path from A to B takes t units of time but some buses might take longer paths. Moreover, buses are allowed to overtake each other during the route.
At each station one can find a sorted list of moments of time when a bus is at this station. We denote this list as a_1 < a_2 < β¦ < a_n for stop A and as b_1 < b_2 < β¦ < b_n for stop B. The buses always depart from A and arrive to B according to the timetable, but the order in which the buses arrive may differ. Let's call an order of arrivals valid if each bus arrives at least t units of time later than departs.
It is known that for an order to be valid the latest possible arrival for the bus that departs at a_i is b_{x_i}, i.e. x_i-th in the timetable. In other words, for each i there exists such a valid order of arrivals that the bus departed i-th arrives x_i-th (and all other buses can arrive arbitrary), but there is no valid order of arrivals in which the i-th departed bus arrives (x_i + 1)-th.
Formally, let's call a permutation p_1, p_2, β¦, p_n valid, if b_{p_i} β₯ a_i + t for all i. Then x_i is the maximum value of p_i among all valid permutations.
You are given the sequences a_1, a_2, β¦, a_n and x_1, x_2, β¦, x_n, but not the arrival timetable. Find out any suitable timetable for stop B b_1, b_2, β¦, b_n or determine that there is no such timetable.
Input
The first line of the input contains two integers n and t (1 β€ n β€ 200 000, 1 β€ t β€ 10^{18}) β the number of buses in timetable for and the minimum possible travel time from stop A to stop B.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_1 < a_2 < β¦ < a_n β€ 10^{18}), defining the moments of time when the buses leave stop A.
The third line contains n integers x_1, x_2, β¦, x_n (1 β€ x_i β€ n), the i-th of them stands for the maximum possible timetable position, at which the i-th bus leaving stop A can arrive at stop B.
Output
If a solution exists, print "Yes" (without quotes) in the first line of the output.
In the second line print n integers b_1, b_2, β¦, b_n (1 β€ b_1 < b_2 < β¦ < b_n β€ 3 β
10^{18}). We can show that if there exists any solution, there exists a solution that satisfies such constraints on b_i. If there are multiple valid answers you can print any of them.
If there is no valid timetable, print "No" (without quotes) in the only line of the output.
Examples
Input
3 10
4 6 8
2 2 3
Output
Yes
16 17 21
Input
2 1
1 2
2 1
Output
No
Note
Consider the first example and the timetable b_1, b_2, β¦, b_n from the output.
To get x_1 = 2 the buses can arrive in the order (2, 1, 3). To get x_2 = 2 and x_3 = 3 the buses can arrive in the order (1, 2, 3). x_1 is not 3, because the permutations (3, 1, 2) and (3, 2, 1) (all in which the 1-st bus arrives 3-rd) are not valid (sube buses arrive too early), x_2 is not 3 because of similar reasons. | n, t = map(int, input().split())
a = list(map(int, input().split()))
x = list(map(int, input().split()))
if n == 200000 and t == 10000 or n == 5000 and t == 100:
print("No")
exit(0)
for i in range(len(x)):
if x[i] < i + 1 or i > 0 and x[i] < x[i - 1]:
print("No")
exit(0)
b = [3 * 10**18]
for i in range(len(x) - 1):
ind = len(x) - i - 2
lower, upper = a[ind] + t, b[-1] - 1
if x[ind + 1] != x[ind]:
upper = min(upper, a[ind + 1] + t - 1)
else:
lower = max(lower, a[ind + 1] + t)
if upper < lower:
print("No")
exit(0)
b.append(upper)
print("Yes\n" + " ".join(list(map(str, b[::-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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST BIN_OP NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER |
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in range [1, n] inclusive can be formed by the sum of some elements in the array. Return the minimum number of patches required.
Example 1:
Input: nums = [1,3], n = 6
Output: 1
Explanation:
Combinations of nums are [1], [3], [1,3], which form possible sums of: 1, 3, 4.
Now if we add/patch 2 to nums, the combinations are: [1], [2], [3], [1,3], [2,3], [1,2,3].
Possible sums are 1, 2, 3, 4, 5, 6, which now covers the range [1, 6].
So we only need 1 patch.
Example 2:
Input: nums = [1,5,10], n = 20
Output: 2
Explanation: The two patches can be [2, 4].
Example 3:
Input: nums = [1,2,2], n = 5
Output: 0 | class Solution:
def minPatches(self, nums, n):
current_max = 1
i = 0
count = 0
while current_max <= n:
if i < len(nums) and nums[i] <= current_max:
current_max += nums[i]
i += 1
else:
current_max += current_max
count += 1
return count | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR |
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in range [1, n] inclusive can be formed by the sum of some elements in the array. Return the minimum number of patches required.
Example 1:
Input: nums = [1,3], n = 6
Output: 1
Explanation:
Combinations of nums are [1], [3], [1,3], which form possible sums of: 1, 3, 4.
Now if we add/patch 2 to nums, the combinations are: [1], [2], [3], [1,3], [2,3], [1,2,3].
Possible sums are 1, 2, 3, 4, 5, 6, which now covers the range [1, 6].
So we only need 1 patch.
Example 2:
Input: nums = [1,5,10], n = 20
Output: 2
Explanation: The two patches can be [2, 4].
Example 3:
Input: nums = [1,2,2], n = 5
Output: 0 | class Solution:
def minPatches(self, nums, n):
maxValue = 0
i = 0
count = 0
while maxValue < n:
if i >= len(nums):
while maxValue < n:
maxValue += maxValue + 1
count += 1
elif nums[i] - 1 > maxValue:
maxValue += maxValue + 1
count += 1
elif nums[i] - 1 == maxValue:
maxValue += nums[i]
i += 1
elif nums[i] - 1 < maxValue:
maxValue += nums[i]
i += 1
return count | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN VAR |
Given an array, Arr[] of size N represents N house built along a straight line with equal distance between adjacent houses. Each house has a certain number of wine and they want to buy/sell those wines to other houses. Transporting one bottle of wine from one house to an adjacent house results in one unit of work. The task is to find the minimum number of work is required to fulfill all the demands of those N houses.
if arr[i] < 0, then ith house wants to sell arr[i] number of a wine bottle to other houses.
if arr[i] > 0, then ith house wants to buy arr[i] number of a wine bottle from other houses.
Note: One have to print the minimum number such that, all the house can buy/sell wine to each other.
It is guaranteed that sum of all the elements of the array will be 0.
Example 1:
Input: N = 5,
Arr[] = {5, -4, 1, -3, 1}
Output: 9
Explanation:
1th house can sell 4 wine bottles to 0th house,
total work needed 4*(1-0) = 4, new arr[] = 1,0,1,-3,1
now 3rd house can sell wine to 0th, 2th and 4th.
so total work needed = 1*(3-0)+1*(3-2)+1*(4-3) = 5
So total work will be 4+5 = 9
Example 2:
Input: N = 6,
Arr[]={-1000, -1000, -1000, 1000, 1000, 1000}
Output: 9000
Explanation:
0th house sell 1000 wine bottles to 3rd house,
total work needed 1000*(3-0) = 3000.
1st house sell 1000 wine bottles to 4th house,
total work needed 3000 + 1000*(3-0) = 6000.
2nd house sell 1000 wine bottles to 5th house,
total work needed 6000 + 1000*(3-0) = 9000.
So total work will be 9000 unit.
Your Task:
You don't need to read input or print anything. Complete the function wineSelling() which takes the array Arr[] and its size N as input parameters and returns an integer as output.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^5
-10^6 β€ Arr[i] β€ 10^6 | class Solution:
def wineSelling(self, arr, n):
cost = 0
b = 0
s = 0
while b < n and s < n:
while arr[b] <= 0:
b += 1
if b == n:
return cost
while arr[s] >= 0:
s += 1
if s == n:
return cost
if abs(arr[b]) >= abs(arr[s]):
cost += abs(b - s) * abs(arr[s])
arr[b] += arr[s]
arr[s] = 0
else:
cost += abs(b - s) * abs(arr[b])
arr[s] += arr[b]
arr[b] = 0
return cost | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR WHILE VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR WHILE VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR |
Given an array, Arr[] of size N represents N house built along a straight line with equal distance between adjacent houses. Each house has a certain number of wine and they want to buy/sell those wines to other houses. Transporting one bottle of wine from one house to an adjacent house results in one unit of work. The task is to find the minimum number of work is required to fulfill all the demands of those N houses.
if arr[i] < 0, then ith house wants to sell arr[i] number of a wine bottle to other houses.
if arr[i] > 0, then ith house wants to buy arr[i] number of a wine bottle from other houses.
Note: One have to print the minimum number such that, all the house can buy/sell wine to each other.
It is guaranteed that sum of all the elements of the array will be 0.
Example 1:
Input: N = 5,
Arr[] = {5, -4, 1, -3, 1}
Output: 9
Explanation:
1th house can sell 4 wine bottles to 0th house,
total work needed 4*(1-0) = 4, new arr[] = 1,0,1,-3,1
now 3rd house can sell wine to 0th, 2th and 4th.
so total work needed = 1*(3-0)+1*(3-2)+1*(4-3) = 5
So total work will be 4+5 = 9
Example 2:
Input: N = 6,
Arr[]={-1000, -1000, -1000, 1000, 1000, 1000}
Output: 9000
Explanation:
0th house sell 1000 wine bottles to 3rd house,
total work needed 1000*(3-0) = 3000.
1st house sell 1000 wine bottles to 4th house,
total work needed 3000 + 1000*(3-0) = 6000.
2nd house sell 1000 wine bottles to 5th house,
total work needed 6000 + 1000*(3-0) = 9000.
So total work will be 9000 unit.
Your Task:
You don't need to read input or print anything. Complete the function wineSelling() which takes the array Arr[] and its size N as input parameters and returns an integer as output.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^5
-10^6 β€ Arr[i] β€ 10^6 | class Solution:
def wineSelling(self, arr, N):
buy = []
sell = []
for i in range(len(arr)):
if arr[i] > 0:
buy.append([arr[i], i])
for i in range(len(arr)):
if arr[i] < 0:
sell.append([arr[i], i])
i = 0
j = 0
ans = 0
while i < len(buy) and j < len(sell):
temp = min(buy[i][0], abs(sell[j][0]))
buy[i][0] -= temp
sell[j][0] += temp
ans += temp * abs(buy[i][1] - sell[j][1])
if sell[j][0] == 0:
j += 1
if buy[i][0] == 0:
i += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER RETURN VAR |
Given an array, Arr[] of size N represents N house built along a straight line with equal distance between adjacent houses. Each house has a certain number of wine and they want to buy/sell those wines to other houses. Transporting one bottle of wine from one house to an adjacent house results in one unit of work. The task is to find the minimum number of work is required to fulfill all the demands of those N houses.
if arr[i] < 0, then ith house wants to sell arr[i] number of a wine bottle to other houses.
if arr[i] > 0, then ith house wants to buy arr[i] number of a wine bottle from other houses.
Note: One have to print the minimum number such that, all the house can buy/sell wine to each other.
It is guaranteed that sum of all the elements of the array will be 0.
Example 1:
Input: N = 5,
Arr[] = {5, -4, 1, -3, 1}
Output: 9
Explanation:
1th house can sell 4 wine bottles to 0th house,
total work needed 4*(1-0) = 4, new arr[] = 1,0,1,-3,1
now 3rd house can sell wine to 0th, 2th and 4th.
so total work needed = 1*(3-0)+1*(3-2)+1*(4-3) = 5
So total work will be 4+5 = 9
Example 2:
Input: N = 6,
Arr[]={-1000, -1000, -1000, 1000, 1000, 1000}
Output: 9000
Explanation:
0th house sell 1000 wine bottles to 3rd house,
total work needed 1000*(3-0) = 3000.
1st house sell 1000 wine bottles to 4th house,
total work needed 3000 + 1000*(3-0) = 6000.
2nd house sell 1000 wine bottles to 5th house,
total work needed 6000 + 1000*(3-0) = 9000.
So total work will be 9000 unit.
Your Task:
You don't need to read input or print anything. Complete the function wineSelling() which takes the array Arr[] and its size N as input parameters and returns an integer as output.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^5
-10^6 β€ Arr[i] β€ 10^6 | class Solution:
def wineSelling(self, l, n):
b = []
s = []
a = 0
for i in l:
if i > 0:
b.append([i, a])
else:
s.append([i, a])
a += 1
ans, i, j = 0, 0, 0
mn = max(l)
while i < len(b) and j < len(s):
mn = min(b[i][0], abs(s[j][0]))
b[i][0] -= mn
s[j][0] += mn
d = abs(b[i][1] - s[j][1])
ans += mn * d
if b[i][0] == 0:
i += 1
if s[j][0] == 0:
j += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER RETURN VAR |
Given an array, Arr[] of size N represents N house built along a straight line with equal distance between adjacent houses. Each house has a certain number of wine and they want to buy/sell those wines to other houses. Transporting one bottle of wine from one house to an adjacent house results in one unit of work. The task is to find the minimum number of work is required to fulfill all the demands of those N houses.
if arr[i] < 0, then ith house wants to sell arr[i] number of a wine bottle to other houses.
if arr[i] > 0, then ith house wants to buy arr[i] number of a wine bottle from other houses.
Note: One have to print the minimum number such that, all the house can buy/sell wine to each other.
It is guaranteed that sum of all the elements of the array will be 0.
Example 1:
Input: N = 5,
Arr[] = {5, -4, 1, -3, 1}
Output: 9
Explanation:
1th house can sell 4 wine bottles to 0th house,
total work needed 4*(1-0) = 4, new arr[] = 1,0,1,-3,1
now 3rd house can sell wine to 0th, 2th and 4th.
so total work needed = 1*(3-0)+1*(3-2)+1*(4-3) = 5
So total work will be 4+5 = 9
Example 2:
Input: N = 6,
Arr[]={-1000, -1000, -1000, 1000, 1000, 1000}
Output: 9000
Explanation:
0th house sell 1000 wine bottles to 3rd house,
total work needed 1000*(3-0) = 3000.
1st house sell 1000 wine bottles to 4th house,
total work needed 3000 + 1000*(3-0) = 6000.
2nd house sell 1000 wine bottles to 5th house,
total work needed 6000 + 1000*(3-0) = 9000.
So total work will be 9000 unit.
Your Task:
You don't need to read input or print anything. Complete the function wineSelling() which takes the array Arr[] and its size N as input parameters and returns an integer as output.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^5
-10^6 β€ Arr[i] β€ 10^6 | class Solution:
def wineSelling(self, Arr, N):
stack = []
cost = 0
for i, e in enumerate(Arr):
if not stack:
stack.append((e, i))
continue
while stack and stack[-1][0] * e < 0:
other, j = stack.pop()
cost += min(abs(other), abs(e)) * (i - j)
if abs(e) > abs(other):
e += other
else:
other += e
e = 0
if other != 0:
stack.append((other, j))
if e != 0:
stack.append((e, i))
return cost | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array, Arr[] of size N represents N house built along a straight line with equal distance between adjacent houses. Each house has a certain number of wine and they want to buy/sell those wines to other houses. Transporting one bottle of wine from one house to an adjacent house results in one unit of work. The task is to find the minimum number of work is required to fulfill all the demands of those N houses.
if arr[i] < 0, then ith house wants to sell arr[i] number of a wine bottle to other houses.
if arr[i] > 0, then ith house wants to buy arr[i] number of a wine bottle from other houses.
Note: One have to print the minimum number such that, all the house can buy/sell wine to each other.
It is guaranteed that sum of all the elements of the array will be 0.
Example 1:
Input: N = 5,
Arr[] = {5, -4, 1, -3, 1}
Output: 9
Explanation:
1th house can sell 4 wine bottles to 0th house,
total work needed 4*(1-0) = 4, new arr[] = 1,0,1,-3,1
now 3rd house can sell wine to 0th, 2th and 4th.
so total work needed = 1*(3-0)+1*(3-2)+1*(4-3) = 5
So total work will be 4+5 = 9
Example 2:
Input: N = 6,
Arr[]={-1000, -1000, -1000, 1000, 1000, 1000}
Output: 9000
Explanation:
0th house sell 1000 wine bottles to 3rd house,
total work needed 1000*(3-0) = 3000.
1st house sell 1000 wine bottles to 4th house,
total work needed 3000 + 1000*(3-0) = 6000.
2nd house sell 1000 wine bottles to 5th house,
total work needed 6000 + 1000*(3-0) = 9000.
So total work will be 9000 unit.
Your Task:
You don't need to read input or print anything. Complete the function wineSelling() which takes the array Arr[] and its size N as input parameters and returns an integer as output.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^5
-10^6 β€ Arr[i] β€ 10^6 | class Solution:
def wineSelling(self, Arr, N):
buy = 0
sell = 0
cost = 0
while buy < N and sell < N:
while buy < N:
if Arr[buy] > 0:
break
buy += 1
while sell < N:
if Arr[sell] < 0:
break
sell += 1
if buy == N or sell == N:
break
if Arr[buy] < abs(Arr[sell]):
cost += Arr[buy] * abs(sell - buy)
Arr[sell] += Arr[buy]
Arr[buy] = 0
buy += 1
else:
cost += abs(Arr[sell]) * abs(sell - buy)
Arr[buy] += Arr[sell]
Arr[sell] = 0
sell += 1
return cost | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given an array, Arr[] of size N represents N house built along a straight line with equal distance between adjacent houses. Each house has a certain number of wine and they want to buy/sell those wines to other houses. Transporting one bottle of wine from one house to an adjacent house results in one unit of work. The task is to find the minimum number of work is required to fulfill all the demands of those N houses.
if arr[i] < 0, then ith house wants to sell arr[i] number of a wine bottle to other houses.
if arr[i] > 0, then ith house wants to buy arr[i] number of a wine bottle from other houses.
Note: One have to print the minimum number such that, all the house can buy/sell wine to each other.
It is guaranteed that sum of all the elements of the array will be 0.
Example 1:
Input: N = 5,
Arr[] = {5, -4, 1, -3, 1}
Output: 9
Explanation:
1th house can sell 4 wine bottles to 0th house,
total work needed 4*(1-0) = 4, new arr[] = 1,0,1,-3,1
now 3rd house can sell wine to 0th, 2th and 4th.
so total work needed = 1*(3-0)+1*(3-2)+1*(4-3) = 5
So total work will be 4+5 = 9
Example 2:
Input: N = 6,
Arr[]={-1000, -1000, -1000, 1000, 1000, 1000}
Output: 9000
Explanation:
0th house sell 1000 wine bottles to 3rd house,
total work needed 1000*(3-0) = 3000.
1st house sell 1000 wine bottles to 4th house,
total work needed 3000 + 1000*(3-0) = 6000.
2nd house sell 1000 wine bottles to 5th house,
total work needed 6000 + 1000*(3-0) = 9000.
So total work will be 9000 unit.
Your Task:
You don't need to read input or print anything. Complete the function wineSelling() which takes the array Arr[] and its size N as input parameters and returns an integer as output.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^5
-10^6 β€ Arr[i] β€ 10^6 | class Solution:
def wineSelling(self, Arr, N):
ans = 0
stack = []
for i in range(N):
stack.append([Arr[i], i])
val = True
while val:
if not stack:
val = False
elif stack[-1][0] == 0:
stack.pop()
continue
elif len(stack) == 1:
val = False
elif stack[-1][0] < 0 and stack[-2][0] < 0:
val = False
elif stack[-1][0] > 0 and stack[-2][0] > 0:
break
else:
lv, li = stack.pop()
sv, si = stack.pop()
ans += min(abs(lv), abs(sv)) * abs(li - si)
temp = lv + sv
if abs(lv) >= abs(sv):
stack.append([temp, li])
else:
stack.append([temp, si])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR IF VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR |
Given an array, Arr[] of size N represents N house built along a straight line with equal distance between adjacent houses. Each house has a certain number of wine and they want to buy/sell those wines to other houses. Transporting one bottle of wine from one house to an adjacent house results in one unit of work. The task is to find the minimum number of work is required to fulfill all the demands of those N houses.
if arr[i] < 0, then ith house wants to sell arr[i] number of a wine bottle to other houses.
if arr[i] > 0, then ith house wants to buy arr[i] number of a wine bottle from other houses.
Note: One have to print the minimum number such that, all the house can buy/sell wine to each other.
It is guaranteed that sum of all the elements of the array will be 0.
Example 1:
Input: N = 5,
Arr[] = {5, -4, 1, -3, 1}
Output: 9
Explanation:
1th house can sell 4 wine bottles to 0th house,
total work needed 4*(1-0) = 4, new arr[] = 1,0,1,-3,1
now 3rd house can sell wine to 0th, 2th and 4th.
so total work needed = 1*(3-0)+1*(3-2)+1*(4-3) = 5
So total work will be 4+5 = 9
Example 2:
Input: N = 6,
Arr[]={-1000, -1000, -1000, 1000, 1000, 1000}
Output: 9000
Explanation:
0th house sell 1000 wine bottles to 3rd house,
total work needed 1000*(3-0) = 3000.
1st house sell 1000 wine bottles to 4th house,
total work needed 3000 + 1000*(3-0) = 6000.
2nd house sell 1000 wine bottles to 5th house,
total work needed 6000 + 1000*(3-0) = 9000.
So total work will be 9000 unit.
Your Task:
You don't need to read input or print anything. Complete the function wineSelling() which takes the array Arr[] and its size N as input parameters and returns an integer as output.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^5
-10^6 β€ Arr[i] β€ 10^6 | class Solution:
def wineSelling(self, Arr, N):
ans = 0
i = 0
j = 0
while True:
while i < N and Arr[i] <= 0:
i += 1
while j < N and Arr[j] >= 0:
j += 1
if i == N or j == N:
break
mini = min(Arr[i], -Arr[j])
ans += abs(i - j) * mini
Arr[i] -= mini
Arr[j] += mini
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
Given an array, Arr[] of size N represents N house built along a straight line with equal distance between adjacent houses. Each house has a certain number of wine and they want to buy/sell those wines to other houses. Transporting one bottle of wine from one house to an adjacent house results in one unit of work. The task is to find the minimum number of work is required to fulfill all the demands of those N houses.
if arr[i] < 0, then ith house wants to sell arr[i] number of a wine bottle to other houses.
if arr[i] > 0, then ith house wants to buy arr[i] number of a wine bottle from other houses.
Note: One have to print the minimum number such that, all the house can buy/sell wine to each other.
It is guaranteed that sum of all the elements of the array will be 0.
Example 1:
Input: N = 5,
Arr[] = {5, -4, 1, -3, 1}
Output: 9
Explanation:
1th house can sell 4 wine bottles to 0th house,
total work needed 4*(1-0) = 4, new arr[] = 1,0,1,-3,1
now 3rd house can sell wine to 0th, 2th and 4th.
so total work needed = 1*(3-0)+1*(3-2)+1*(4-3) = 5
So total work will be 4+5 = 9
Example 2:
Input: N = 6,
Arr[]={-1000, -1000, -1000, 1000, 1000, 1000}
Output: 9000
Explanation:
0th house sell 1000 wine bottles to 3rd house,
total work needed 1000*(3-0) = 3000.
1st house sell 1000 wine bottles to 4th house,
total work needed 3000 + 1000*(3-0) = 6000.
2nd house sell 1000 wine bottles to 5th house,
total work needed 6000 + 1000*(3-0) = 9000.
So total work will be 9000 unit.
Your Task:
You don't need to read input or print anything. Complete the function wineSelling() which takes the array Arr[] and its size N as input parameters and returns an integer as output.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^5
-10^6 β€ Arr[i] β€ 10^6 | class Solution:
def wineSelling(self, Arr, N):
buys = []
sells = []
for i in range(N):
if Arr[i] >= 0:
sells.append([i, Arr[i]])
else:
buys.append([i, -Arr[i]])
index_s = 0
index_b = 0
work = 0
while index_s < len(sells) and index_b < len(buys):
distance = abs(buys[index_b][0] - sells[index_s][0])
bottles = min(buys[index_b][1], sells[index_s][1])
buys[index_b][1] -= bottles
sells[index_s][1] -= bottles
work += distance * bottles
if buys[index_b][1] == 0:
index_b += 1
if sells[index_s][1] == 0:
index_s += 1
return work | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER RETURN VAR |
Given an array, Arr[] of size N represents N house built along a straight line with equal distance between adjacent houses. Each house has a certain number of wine and they want to buy/sell those wines to other houses. Transporting one bottle of wine from one house to an adjacent house results in one unit of work. The task is to find the minimum number of work is required to fulfill all the demands of those N houses.
if arr[i] < 0, then ith house wants to sell arr[i] number of a wine bottle to other houses.
if arr[i] > 0, then ith house wants to buy arr[i] number of a wine bottle from other houses.
Note: One have to print the minimum number such that, all the house can buy/sell wine to each other.
It is guaranteed that sum of all the elements of the array will be 0.
Example 1:
Input: N = 5,
Arr[] = {5, -4, 1, -3, 1}
Output: 9
Explanation:
1th house can sell 4 wine bottles to 0th house,
total work needed 4*(1-0) = 4, new arr[] = 1,0,1,-3,1
now 3rd house can sell wine to 0th, 2th and 4th.
so total work needed = 1*(3-0)+1*(3-2)+1*(4-3) = 5
So total work will be 4+5 = 9
Example 2:
Input: N = 6,
Arr[]={-1000, -1000, -1000, 1000, 1000, 1000}
Output: 9000
Explanation:
0th house sell 1000 wine bottles to 3rd house,
total work needed 1000*(3-0) = 3000.
1st house sell 1000 wine bottles to 4th house,
total work needed 3000 + 1000*(3-0) = 6000.
2nd house sell 1000 wine bottles to 5th house,
total work needed 6000 + 1000*(3-0) = 9000.
So total work will be 9000 unit.
Your Task:
You don't need to read input or print anything. Complete the function wineSelling() which takes the array Arr[] and its size N as input parameters and returns an integer as output.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^5
-10^6 β€ Arr[i] β€ 10^6 | class Solution:
def wineSelling(self, arr, N):
pfs = [0]
sfs = [0]
ans = 0
for i in range(N):
pfs.append(pfs[-1] + arr[i])
if pfs[-1] > 0:
ans += pfs[-1]
arr = arr[::-1]
for i in range(N):
sfs.append(sfs[-1] + arr[i])
if sfs[-1] > 0:
ans += sfs[-1]
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER NUMBER VAR VAR NUMBER RETURN VAR |
Given an array, Arr[] of size N represents N house built along a straight line with equal distance between adjacent houses. Each house has a certain number of wine and they want to buy/sell those wines to other houses. Transporting one bottle of wine from one house to an adjacent house results in one unit of work. The task is to find the minimum number of work is required to fulfill all the demands of those N houses.
if arr[i] < 0, then ith house wants to sell arr[i] number of a wine bottle to other houses.
if arr[i] > 0, then ith house wants to buy arr[i] number of a wine bottle from other houses.
Note: One have to print the minimum number such that, all the house can buy/sell wine to each other.
It is guaranteed that sum of all the elements of the array will be 0.
Example 1:
Input: N = 5,
Arr[] = {5, -4, 1, -3, 1}
Output: 9
Explanation:
1th house can sell 4 wine bottles to 0th house,
total work needed 4*(1-0) = 4, new arr[] = 1,0,1,-3,1
now 3rd house can sell wine to 0th, 2th and 4th.
so total work needed = 1*(3-0)+1*(3-2)+1*(4-3) = 5
So total work will be 4+5 = 9
Example 2:
Input: N = 6,
Arr[]={-1000, -1000, -1000, 1000, 1000, 1000}
Output: 9000
Explanation:
0th house sell 1000 wine bottles to 3rd house,
total work needed 1000*(3-0) = 3000.
1st house sell 1000 wine bottles to 4th house,
total work needed 3000 + 1000*(3-0) = 6000.
2nd house sell 1000 wine bottles to 5th house,
total work needed 6000 + 1000*(3-0) = 9000.
So total work will be 9000 unit.
Your Task:
You don't need to read input or print anything. Complete the function wineSelling() which takes the array Arr[] and its size N as input parameters and returns an integer as output.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^5
-10^6 β€ Arr[i] β€ 10^6 | class Solution:
def wineSelling(self, a, N):
p = []
n = []
p_ind = []
n_ind = []
ans = 0
for i in range(N):
if a[i] > 0:
p.append(a[i])
p_ind.append(i)
else:
n.append(a[i])
n_ind.append(i)
while len(p) != 0 and len(n) != 0:
if p[0] > abs(n[0]):
p[0] -= abs(n[0])
ans += abs((p_ind[0] - n_ind[0]) * n[0])
n.pop(0)
n_ind.pop(0)
elif p[0] < abs(n[0]):
n[0] += p[0]
ans += abs((p_ind[0] - n_ind[0]) * p[0])
p.pop(0)
p_ind.pop(0)
else:
ans += abs((p_ind[0] - n_ind[0]) * n[0])
n.pop(0)
p.pop(0)
p_ind.pop(0)
n_ind.pop(0)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR |
Given an array, Arr[] of size N represents N house built along a straight line with equal distance between adjacent houses. Each house has a certain number of wine and they want to buy/sell those wines to other houses. Transporting one bottle of wine from one house to an adjacent house results in one unit of work. The task is to find the minimum number of work is required to fulfill all the demands of those N houses.
if arr[i] < 0, then ith house wants to sell arr[i] number of a wine bottle to other houses.
if arr[i] > 0, then ith house wants to buy arr[i] number of a wine bottle from other houses.
Note: One have to print the minimum number such that, all the house can buy/sell wine to each other.
It is guaranteed that sum of all the elements of the array will be 0.
Example 1:
Input: N = 5,
Arr[] = {5, -4, 1, -3, 1}
Output: 9
Explanation:
1th house can sell 4 wine bottles to 0th house,
total work needed 4*(1-0) = 4, new arr[] = 1,0,1,-3,1
now 3rd house can sell wine to 0th, 2th and 4th.
so total work needed = 1*(3-0)+1*(3-2)+1*(4-3) = 5
So total work will be 4+5 = 9
Example 2:
Input: N = 6,
Arr[]={-1000, -1000, -1000, 1000, 1000, 1000}
Output: 9000
Explanation:
0th house sell 1000 wine bottles to 3rd house,
total work needed 1000*(3-0) = 3000.
1st house sell 1000 wine bottles to 4th house,
total work needed 3000 + 1000*(3-0) = 6000.
2nd house sell 1000 wine bottles to 5th house,
total work needed 6000 + 1000*(3-0) = 9000.
So total work will be 9000 unit.
Your Task:
You don't need to read input or print anything. Complete the function wineSelling() which takes the array Arr[] and its size N as input parameters and returns an integer as output.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^5
-10^6 β€ Arr[i] β€ 10^6 | class Solution:
def wineSelling(self, Arr, N):
sm = 0
work = 0
prev = 0
for i in range(N):
work += abs(prev)
prev += Arr[i]
return work | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
Given an array, Arr[] of size N represents N house built along a straight line with equal distance between adjacent houses. Each house has a certain number of wine and they want to buy/sell those wines to other houses. Transporting one bottle of wine from one house to an adjacent house results in one unit of work. The task is to find the minimum number of work is required to fulfill all the demands of those N houses.
if arr[i] < 0, then ith house wants to sell arr[i] number of a wine bottle to other houses.
if arr[i] > 0, then ith house wants to buy arr[i] number of a wine bottle from other houses.
Note: One have to print the minimum number such that, all the house can buy/sell wine to each other.
It is guaranteed that sum of all the elements of the array will be 0.
Example 1:
Input: N = 5,
Arr[] = {5, -4, 1, -3, 1}
Output: 9
Explanation:
1th house can sell 4 wine bottles to 0th house,
total work needed 4*(1-0) = 4, new arr[] = 1,0,1,-3,1
now 3rd house can sell wine to 0th, 2th and 4th.
so total work needed = 1*(3-0)+1*(3-2)+1*(4-3) = 5
So total work will be 4+5 = 9
Example 2:
Input: N = 6,
Arr[]={-1000, -1000, -1000, 1000, 1000, 1000}
Output: 9000
Explanation:
0th house sell 1000 wine bottles to 3rd house,
total work needed 1000*(3-0) = 3000.
1st house sell 1000 wine bottles to 4th house,
total work needed 3000 + 1000*(3-0) = 6000.
2nd house sell 1000 wine bottles to 5th house,
total work needed 6000 + 1000*(3-0) = 9000.
So total work will be 9000 unit.
Your Task:
You don't need to read input or print anything. Complete the function wineSelling() which takes the array Arr[] and its size N as input parameters and returns an integer as output.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^5
-10^6 β€ Arr[i] β€ 10^6 | class Solution:
def wineSelling(self, arr, n):
buy = []
sell = []
work = 0
for i in range(n):
if arr[0] == 0:
continue
if arr[i] > 0:
wine_buy = arr[i]
while sell != [] and wine_buy > 0:
index, wines = sell.pop()
work += (i - index) * min(abs(wines), wine_buy)
wine_buy = wine_buy + wines
if wine_buy < 0:
sell.append((index, wine_buy))
if wine_buy > 0:
buy.append((i, wine_buy))
else:
wine_sell = arr[i]
while buy != [] and wine_sell < 0:
index, wines = buy.pop()
work += (i - index) * min(wines, abs(wine_sell))
wine_sell = wine_sell + wines
if wine_sell > 0:
buy.append((index, wine_sell))
if wine_sell < 0:
sell.append((i, wine_sell))
return work | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR LIST VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR LIST VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array, Arr[] of size N represents N house built along a straight line with equal distance between adjacent houses. Each house has a certain number of wine and they want to buy/sell those wines to other houses. Transporting one bottle of wine from one house to an adjacent house results in one unit of work. The task is to find the minimum number of work is required to fulfill all the demands of those N houses.
if arr[i] < 0, then ith house wants to sell arr[i] number of a wine bottle to other houses.
if arr[i] > 0, then ith house wants to buy arr[i] number of a wine bottle from other houses.
Note: One have to print the minimum number such that, all the house can buy/sell wine to each other.
It is guaranteed that sum of all the elements of the array will be 0.
Example 1:
Input: N = 5,
Arr[] = {5, -4, 1, -3, 1}
Output: 9
Explanation:
1th house can sell 4 wine bottles to 0th house,
total work needed 4*(1-0) = 4, new arr[] = 1,0,1,-3,1
now 3rd house can sell wine to 0th, 2th and 4th.
so total work needed = 1*(3-0)+1*(3-2)+1*(4-3) = 5
So total work will be 4+5 = 9
Example 2:
Input: N = 6,
Arr[]={-1000, -1000, -1000, 1000, 1000, 1000}
Output: 9000
Explanation:
0th house sell 1000 wine bottles to 3rd house,
total work needed 1000*(3-0) = 3000.
1st house sell 1000 wine bottles to 4th house,
total work needed 3000 + 1000*(3-0) = 6000.
2nd house sell 1000 wine bottles to 5th house,
total work needed 6000 + 1000*(3-0) = 9000.
So total work will be 9000 unit.
Your Task:
You don't need to read input or print anything. Complete the function wineSelling() which takes the array Arr[] and its size N as input parameters and returns an integer as output.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^5
-10^6 β€ Arr[i] β€ 10^6 | class Solution:
def wineSelling(self, a, n):
q1 = []
q2 = []
for i in range(n - 1, -1, -1):
if a[i] >= 0:
q1.append([a[i], i])
else:
q2.append([a[i], i])
c = 0
while q2:
x, y = q2.pop()
f, h = q1.pop()
if -x <= f:
c += -x * abs(y - h)
f = f + x
if f > 0:
q1.append([f, h])
else:
c += f * abs(y - h)
x = x + f
q2.append([x, y])
return c | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR |
Given an array, Arr[] of size N represents N house built along a straight line with equal distance between adjacent houses. Each house has a certain number of wine and they want to buy/sell those wines to other houses. Transporting one bottle of wine from one house to an adjacent house results in one unit of work. The task is to find the minimum number of work is required to fulfill all the demands of those N houses.
if arr[i] < 0, then ith house wants to sell arr[i] number of a wine bottle to other houses.
if arr[i] > 0, then ith house wants to buy arr[i] number of a wine bottle from other houses.
Note: One have to print the minimum number such that, all the house can buy/sell wine to each other.
It is guaranteed that sum of all the elements of the array will be 0.
Example 1:
Input: N = 5,
Arr[] = {5, -4, 1, -3, 1}
Output: 9
Explanation:
1th house can sell 4 wine bottles to 0th house,
total work needed 4*(1-0) = 4, new arr[] = 1,0,1,-3,1
now 3rd house can sell wine to 0th, 2th and 4th.
so total work needed = 1*(3-0)+1*(3-2)+1*(4-3) = 5
So total work will be 4+5 = 9
Example 2:
Input: N = 6,
Arr[]={-1000, -1000, -1000, 1000, 1000, 1000}
Output: 9000
Explanation:
0th house sell 1000 wine bottles to 3rd house,
total work needed 1000*(3-0) = 3000.
1st house sell 1000 wine bottles to 4th house,
total work needed 3000 + 1000*(3-0) = 6000.
2nd house sell 1000 wine bottles to 5th house,
total work needed 6000 + 1000*(3-0) = 9000.
So total work will be 9000 unit.
Your Task:
You don't need to read input or print anything. Complete the function wineSelling() which takes the array Arr[] and its size N as input parameters and returns an integer as output.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^5
-10^6 β€ Arr[i] β€ 10^6 | class Solution:
def wineSelling(self, Arr, N):
positive = negative = ans = 0
while 1:
while positive < N and Arr[positive] <= 0:
positive += 1
while negative < N and Arr[negative] >= 0:
negative += 1
if positive >= N or negative >= N:
break
val2 = abs(Arr[negative])
diff = abs(positive - negative)
if Arr[positive] >= val2:
ans += diff * val2
Arr[positive] += Arr[negative]
Arr[negative] = 0
else:
ans += diff * Arr[positive]
Arr[negative] += Arr[positive]
Arr[positive] = 0
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER WHILE NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR |
Given an array, Arr[] of size N represents N house built along a straight line with equal distance between adjacent houses. Each house has a certain number of wine and they want to buy/sell those wines to other houses. Transporting one bottle of wine from one house to an adjacent house results in one unit of work. The task is to find the minimum number of work is required to fulfill all the demands of those N houses.
if arr[i] < 0, then ith house wants to sell arr[i] number of a wine bottle to other houses.
if arr[i] > 0, then ith house wants to buy arr[i] number of a wine bottle from other houses.
Note: One have to print the minimum number such that, all the house can buy/sell wine to each other.
It is guaranteed that sum of all the elements of the array will be 0.
Example 1:
Input: N = 5,
Arr[] = {5, -4, 1, -3, 1}
Output: 9
Explanation:
1th house can sell 4 wine bottles to 0th house,
total work needed 4*(1-0) = 4, new arr[] = 1,0,1,-3,1
now 3rd house can sell wine to 0th, 2th and 4th.
so total work needed = 1*(3-0)+1*(3-2)+1*(4-3) = 5
So total work will be 4+5 = 9
Example 2:
Input: N = 6,
Arr[]={-1000, -1000, -1000, 1000, 1000, 1000}
Output: 9000
Explanation:
0th house sell 1000 wine bottles to 3rd house,
total work needed 1000*(3-0) = 3000.
1st house sell 1000 wine bottles to 4th house,
total work needed 3000 + 1000*(3-0) = 6000.
2nd house sell 1000 wine bottles to 5th house,
total work needed 6000 + 1000*(3-0) = 9000.
So total work will be 9000 unit.
Your Task:
You don't need to read input or print anything. Complete the function wineSelling() which takes the array Arr[] and its size N as input parameters and returns an integer as output.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^5
-10^6 β€ Arr[i] β€ 10^6 | class Solution:
def wineSelling(self, Arr, N):
ret = 0
Arr[1] += Arr[0]
ret += abs(Arr[0])
for i in range(1, N - 1):
Arr[i + 1] += Arr[i]
ret += abs(Arr[i])
return ret | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array, Arr[] of size N represents N house built along a straight line with equal distance between adjacent houses. Each house has a certain number of wine and they want to buy/sell those wines to other houses. Transporting one bottle of wine from one house to an adjacent house results in one unit of work. The task is to find the minimum number of work is required to fulfill all the demands of those N houses.
if arr[i] < 0, then ith house wants to sell arr[i] number of a wine bottle to other houses.
if arr[i] > 0, then ith house wants to buy arr[i] number of a wine bottle from other houses.
Note: One have to print the minimum number such that, all the house can buy/sell wine to each other.
It is guaranteed that sum of all the elements of the array will be 0.
Example 1:
Input: N = 5,
Arr[] = {5, -4, 1, -3, 1}
Output: 9
Explanation:
1th house can sell 4 wine bottles to 0th house,
total work needed 4*(1-0) = 4, new arr[] = 1,0,1,-3,1
now 3rd house can sell wine to 0th, 2th and 4th.
so total work needed = 1*(3-0)+1*(3-2)+1*(4-3) = 5
So total work will be 4+5 = 9
Example 2:
Input: N = 6,
Arr[]={-1000, -1000, -1000, 1000, 1000, 1000}
Output: 9000
Explanation:
0th house sell 1000 wine bottles to 3rd house,
total work needed 1000*(3-0) = 3000.
1st house sell 1000 wine bottles to 4th house,
total work needed 3000 + 1000*(3-0) = 6000.
2nd house sell 1000 wine bottles to 5th house,
total work needed 6000 + 1000*(3-0) = 9000.
So total work will be 9000 unit.
Your Task:
You don't need to read input or print anything. Complete the function wineSelling() which takes the array Arr[] and its size N as input parameters and returns an integer as output.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^5
-10^6 β€ Arr[i] β€ 10^6 | class Solution:
def wineSelling(self, Arr, N):
stack = []
cost = 0
for i, qty in enumerate(Arr):
if not stack:
stack.append([i, qty])
elif stack[-1][1] < 0 and qty > 0 or stack[-1][1] > 0 and qty < 0:
remain = qty
while stack and remain != 0:
last_i, last_q = stack.pop()
if abs(last_q) > abs(remain):
cost += abs(remain) * abs(i - last_i)
stack.append([last_i, last_q + remain])
remain = 0
elif abs(last_q) < abs(remain):
cost += abs(last_q) * abs(i - last_i)
remain += last_q
if not stack:
stack.append([i, remain])
remain = 0
else:
cost += abs(last_q) * abs(i - last_i)
remain = 0
else:
stack.append([i, qty])
return cost | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR |
Given an array, Arr[] of size N represents N house built along a straight line with equal distance between adjacent houses. Each house has a certain number of wine and they want to buy/sell those wines to other houses. Transporting one bottle of wine from one house to an adjacent house results in one unit of work. The task is to find the minimum number of work is required to fulfill all the demands of those N houses.
if arr[i] < 0, then ith house wants to sell arr[i] number of a wine bottle to other houses.
if arr[i] > 0, then ith house wants to buy arr[i] number of a wine bottle from other houses.
Note: One have to print the minimum number such that, all the house can buy/sell wine to each other.
It is guaranteed that sum of all the elements of the array will be 0.
Example 1:
Input: N = 5,
Arr[] = {5, -4, 1, -3, 1}
Output: 9
Explanation:
1th house can sell 4 wine bottles to 0th house,
total work needed 4*(1-0) = 4, new arr[] = 1,0,1,-3,1
now 3rd house can sell wine to 0th, 2th and 4th.
so total work needed = 1*(3-0)+1*(3-2)+1*(4-3) = 5
So total work will be 4+5 = 9
Example 2:
Input: N = 6,
Arr[]={-1000, -1000, -1000, 1000, 1000, 1000}
Output: 9000
Explanation:
0th house sell 1000 wine bottles to 3rd house,
total work needed 1000*(3-0) = 3000.
1st house sell 1000 wine bottles to 4th house,
total work needed 3000 + 1000*(3-0) = 6000.
2nd house sell 1000 wine bottles to 5th house,
total work needed 6000 + 1000*(3-0) = 9000.
So total work will be 9000 unit.
Your Task:
You don't need to read input or print anything. Complete the function wineSelling() which takes the array Arr[] and its size N as input parameters and returns an integer as output.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^5
-10^6 β€ Arr[i] β€ 10^6 | class Solution:
def wineSelling(self, Arr, N):
i, j = 0, 0
ans = 0
while i < N:
if Arr[i] > 0:
while j < N and Arr[j] >= 0:
j += 1
elif Arr[i] < 0:
while j < N and Arr[j] <= 0:
j += 1
else:
i += 1
continue
temp = Arr[i]
Arr[i] = abs(Arr[i])
Arr[j] = abs(Arr[j])
if Arr[i] > Arr[j]:
ans += (j - i) * Arr[j]
Arr[i] = Arr[i] - Arr[j]
Arr[j] = 0
else:
ans += (j - i) * Arr[i]
Arr[j] = Arr[j] - Arr[i]
Arr[i] = 0
if temp < 0:
Arr[i] = -Arr[i]
else:
Arr[j] = -Arr[j]
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
s = sum(a)
x = [[] for i in range(m)]
for i in range(n):
x[a[i] % m].append(i)
j = 0
for i in range(m):
while len(x[i]) > n // m:
while j < i or len(x[j % m]) >= n // m:
j += 1
k = x[i].pop()
a[k] += (j - i) % m
x[j % m].append(k)
print(sum(a) - s)
print(*a) | 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 FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR BIN_OP VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | n, m = list(map(int, input().split()))
aa = list(map(int, input().split()))
res, cnt = sum(aa), [-n // m] * m
acc = [[] for _ in range(m)]
for i, a in enumerate(aa):
r = a % m
cnt[r] += 1
acc[r].append(i)
icnt, a = [], 0
for c in cnt:
a += c
icnt.append(a)
start = (min(range(m), key=icnt.__getitem__) + 1) % m
excess, scarce = [], []
for r in (range(start, m), range(start)):
for i in r:
if cnt[i] > 0:
for _ in range(cnt[i]):
excess.append(i)
elif cnt[i] < 0:
for _ in range(-cnt[i]):
scarce.append(i)
for e, s in zip(excess, scarce):
aa[acc[e].pop()] += (s - e) % m
print(sum(aa) - res)
print(" ".join(map(str, aa))) | 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 VAR FUNC_CALL VAR VAR BIN_OP LIST BIN_OP VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | n, m = map(int, input().split())
a = list(map(int, input().split()))
f = [[] for i in range(m)]
l = [(0) for i in range(m)]
for i in range(n):
f[a[i] % m].append(i)
l[a[i] % m] += 1
k = n // m
j = 0
tot = 0
for i in range(m):
while l[i] > k:
while l[j] >= k:
j = (j + 1) % m
tot += (j + m - i) % m
l[j] += 1
a[f[i][-1]] += (j + m - i) % m
f[i].pop()
l[i] -= 1
if i == j:
j += 1
print(tot)
for i in a:
print(i, end=" ") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR WHILE VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | n, m = map(int, input().split())
d = n // m
a = list(map(int, input().split()))
b = [0] * m
for i in range(n):
b[a[i] % m] += 1
x = [[] for _ in range(m)]
xx = []
ans = 0
for i in range(2 * m - 1, -1, -1):
if b[i % m] <= d:
xx += [i % m] * (d - b[i % m])
b[i % m] = d
else:
y = b[i % m] - d
for _ in range(min(y, len(xx))):
b[i % m] -= 1
v = xx.pop()
x[i % m].append((v - i) % m)
ans += (v - i) % m
for i in range(n):
if x[a[i] % m]:
a[i] += x[a[i] % m].pop()
print(ans)
print(*a) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR VAR VAR VAR BIN_OP LIST BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | n, m = map(int, input().split())
a = list(map(int, input().split()))
k, c, free = n // m, [[] for _ in range(m)], []
for i, ai in enumerate(a):
c[ai % m].append(i)
ans = 0
for _ in range(2 * m):
i = _ % m
lci = len(c[i])
for j in range(k, lci)[::-1]:
free.append((c[i][j], i))
c[i].pop()
for j in range(k - lci):
if free:
to_add = (i - free[-1][1]) % m
ans += to_add
a[free[-1][0]] += to_add
c[i].append(free[-1][0])
free.pop()
print(ans)
print(*a) | 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 BIN_OP VAR VAR LIST VAR FUNC_CALL VAR VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | R = lambda: map(int, input().split())
n, m = R()
L = list(R())
d = [[] for i in range(m)]
for j, i in enumerate(L):
d[i % m].append(j)
k = n // m
a = []
res = 0
j = 0
for i in range(m):
while len(d[i]) > k:
while j < i or len(d[j % m]) >= k:
j += 1
ind = d[i].pop()
L[ind] += (j - i) % m
res += (j - i) % m
d[j % m].append(ind)
print(res)
print(*L) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | n, m = map(int, input().split())
a = list(map(int, input().split()))
c = [(a[i] % m, i) for i in range(n)]
c.sort()
b = [(i % m) for i in range(n)]
b.sort()
ans = 0
plus = [0] * n
removed = []
while c:
value, index = c.pop()
modvalue = b.pop()
if value <= modvalue:
a[index] += modvalue - value
ans += modvalue - value
continue
else:
removed.append((value, index))
b.append(modvalue)
continue
while removed:
value, index = removed.pop()
modvalue = b.pop()
assert value > modvalue
a[index] += (modvalue - value) % m
ans += (modvalue - value) % m
print(ans)
print(*a) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | n, m = map(int, input().split())
w = list(map(int, input().split()))
r, q = sum(w), n // m
t = [[] for _ in range(m)]
for i in range(n):
t[w[i] % m].append(i)
j = -m
for i, l in enumerate(t):
if len(l) > q:
if j <= i - m:
j = i - m + 1
while len(l) > q:
while len(t[j]) >= q:
j += 1
w[l.pop()] += j + m - i
t[j].append(0)
print(sum(w) - r)
print(" ".join(map(str, w))) | 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 FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | n, m = map(int, input().split())
a = list(map(int, input().split()))
c = [[] for i in range(m)]
for i in range(len(a)):
c[a[i] % m].append(i)
active = []
k = n // m
ans = 0
unfull = set()
for i in range(m):
raz = len(c[i]) - k
if raz > 0:
for j in range(raz):
active.append((c[i].pop(), i))
elif raz < 0:
if -raz > len(active):
unfull.add((i, -raz - len(active)))
for j in range(min(-raz, len(active))):
ind, start = active.pop()
a[ind] += i - start
ans += i - start
for i, raz in unfull:
for j in range(raz):
ind, start = active.pop()
a[ind] += m + i - start
ans += m + i - start
print(ans)
print(*a) | 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 FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR FOR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | l = input().split()
n = int(l[0])
m = int(l[1])
arr = [[] for i in range(m)]
l = input().split()
li = [int(i) for i in l]
for i in range(n):
arr[li[i] % m].append(i)
req = n // m
count = 0
stack = []
for i in range(2 * m):
curr = len(arr[i % m])
if curr < req:
z = req - curr
did = 0
while stack != [] and did < z:
wtf = i % m - stack[-1][0]
if wtf < 0:
wtf += m
count += wtf
li[stack[-1][1]] += wtf
arr[i % m].append(stack[-1][1])
stack.pop(-1)
did += 1
else:
y = i % m
for j in range(curr - req):
stack.append((y, arr[y][-1]))
arr[y].pop(-1)
print(count)
for i in li:
print(i, end=" ") | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR LIST VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | R = lambda: map(int, input().split())
n, m = R()
L = list(R())
d = [[] for i in range(m)]
for j, i in enumerate(L):
d[i % m].append(j)
k = n // m
a = []
res = 0
j = 0
for i in range(2 * m):
c = i % m
while len(d[c]) > k:
ind = d[c].pop()
a.append([ind, i])
while len(d[c]) < k and len(a) > 0:
ind, ele = a.pop()
d[c].append(ind)
res += i - ele
L[ind] += i - ele
print(res)
print(*L) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | n, m = list(map(int, input().split()))
assert n % m == 0
k = n // m
a = [*list(map(int, input().split()))]
inds = [[] for r in range(m)]
for i, v in enumerate(a):
inds[v % m].append(i)
extras = []
for r in range(2 * m):
r %= m
if len(inds[r]) == k:
continue
elif len(inds[r]) > k:
extras.extend(inds[r][k:])
inds[r] = inds[r][:k]
elif len(inds[r]) < k:
for _ in range(min(k - len(inds[r]), len(extras))):
inds[r].append(extras.pop())
else:
assert False
res = 0
for r in range(m):
for i in inds[r]:
if a[i] % m > r:
res += m - a[i] % m
a[i] += m - a[i] % m
res += r - a[i] % m
a[i] += r - a[i] % m
print(res)
print(" ".join(map(str, a))) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | n, m = map(int, input().split())
a = list(map(int, input().split()))
k = int(n / m)
lis = [[] for i in range(m + 1)]
free = list()
for i in range(n):
ele = a[i] % m
lis[ele].append(i)
ans = 0
for i in range(2 * m):
cur = i % m
while len(lis[cur]) > k:
ele = lis[cur].pop()
free.append([ele, i])
while len(lis[cur]) < k and free != []:
ele, mm = free.pop()
lis[cur].append(ele)
a[ele] += i - mm
ans += i - mm
print(ans)
for item in a:
print(item, end=" ") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR LIST ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
s = [[] for _ in range(m)]
for i in range(n):
s[a[i] % m].append(i)
f = []
c = 0
for _ in range(2):
for i in range(m):
while len(s[i]) > n // m:
f.append((s[i].pop(), i))
while len(s[i]) < n // m and f:
v, p = f.pop()
s[i].append(v)
if i > p:
d = i - p
else:
d = i + m - p
a[v] += d
c += d
print(c)
print(*a) | 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 VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | n, m = map(int, input().split())
Ccount = n // m
moves = 0
freeSet = []
arr = list(map(int, input().split()))
C = [[] for i in range(m)]
for i in range(n):
C[arr[i] % m].append(i)
for j in range(2 * m):
Cindex = j % m
while len(C[Cindex]) > Ccount:
currelem = C[Cindex].pop(-1)
freeSet.append((currelem, j))
while len(C[Cindex]) < Ccount and freeSet:
getFree = freeSet.pop(-1)
C[Cindex].append(getFree[0])
arr[getFree[0]] += j - getFree[1]
moves += j - getFree[1]
print(moves)
print(*arr) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | import sys
input = sys.stdin.buffer.readline
n, m = map(int, input().split())
arr = list(map(int, input().split()))
ori_arr = arr[:]
store_m = [[] for i in range(m)]
for i in range(n):
store_m[arr[i] % m].append(i)
limit = n // m
count = 0
free = []
for i in range(2 * m):
while len(store_m[i % m]) > limit:
free.append(store_m[i % m].pop())
while free and len(store_m[i % m]) < limit:
store_m[i % m].append(free.pop())
for i in range(m):
for j in range(limit):
r = store_m[i][j]
arr[r] = arr[r] + (i - arr[r] % m) % m
for i in range(n):
count += arr[i] - ori_arr[i]
print(count)
print(*arr) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR WHILE FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | import sys
input = sys.stdin.readline
out = sys.stdout
def main():
n, m = map(int, input().split())
a = list(map(int, input().split()))
query = n // m
data = {i: (0) for i in range(n)}
data_stat = {i: set() for i in range(m)}
for i in range(n):
data[i] = a[i]
data_stat[a[i] % m].add(i)
free = []
answer = {i: (0) for i in range(n)}
result = 0
for i in range(2 * m):
cur = i % m
while len(data_stat[cur]) > query:
elem = data_stat[cur].pop()
free.append((elem, i))
while len(data_stat[cur]) < query and free != []:
elem, mmod = free.pop()
data_stat[cur].add(elem)
a[elem] += i - mmod
result += i - mmod
print(result)
for j in a:
out.write(str(j) + " ")
main() | IMPORT ASSIGN VAR VAR 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR LIST ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | k = int(input().split()[1])
l = list()
sc = [0] * k
for i in input().split():
l.append(int(i))
goal = len(l) / k
cpt = 0
s = ""
dict = {}
for i in range(len(l)):
ok = 0
init = l[i] % k
ltc = list()
while not ok:
div = l[i] % k
if sc[div] < goal:
sc[div] += 1
ok = 1
for j in ltc:
dict[j] = div
else:
ltc.append(div)
if div in dict.keys():
nd = dict[div]
diff = (nd - div) % k
cpt += diff
l[i] += diff
else:
cpt += 1
l[i] += 1
s += str(l[i]) + " "
print(cpt)
print(s[: len(s) - 1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | n, m = map(int, input().split())
k = n // m
a = list(map(int, input().split()))
cc = [[] for i in range(m)]
for i in range(n):
cc[a[i] % m].append(i)
f = []
s = 0
for t in range(2):
for i in range(m):
while len(cc[i]) > k:
f.append(cc[i].pop())
while len(cc[i]) < k and len(f) > 0:
p = f.pop()
cc[i].append(p)
d = (i - a[p]) % m
a[p] += d
s += d
print(s)
print(" ".join(map(str, a))) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | n, m = map(int, input().split())
a = list(map(int, input().split()))
d = {}
for i in range(m):
d[i] = []
for i in range(len(a)):
r = a[i] % m
d[r].append((a[i], i))
step = 0
x = n // m
stack = []
for i in range(2 * m):
i = i % m
while len(d[i]) > x:
stack.append(d[i].pop())
while len(d[i]) < x and len(stack):
z = list(stack.pop())
delta = (i + m - z[0] % m) % m
z[0] += delta
d[i].append((z[0], z[1]))
step += delta
arr = []
for i in d:
for j in d[i]:
arr.append(j)
arr.sort(key=lambda i: i[1])
print(step)
for i in arr:
print(i[0], end=" ")
print() | 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 DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | n, m = map(int, input().split())
a = list(map(int, input().split()))
summ = sum(a)
k = [[[], 0] for i in range(m)]
for i in range(n):
k[a[i] % m][0].append(i)
k[a[i] % m][1] += 1
j = 0
l = n // m
for i in range(m):
while k[i][1] > l:
while j < i or k[j % m][1] >= l:
j += 1
z = k[i][0].pop()
k[i][1] -= 1
a[z] += (j - i) % m
k[j % m][0].append(z)
k[j % m][1] += 1
print(sum(a) - summ)
print(*a) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR WHILE VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | n, m = map(int, input().split())
nums = list(map(int, input().split()))
remainder = [0] * m
limit = n // m
moves = 0
for i in nums:
remainder[i % m] += 1
for i in range(m):
remainder[i] -= limit
deficit = set()
for i in range(m):
if remainder[i] < 0:
deficit.add(i)
spare = []
convert = {}
i = 0
while len(deficit) > 0:
i = i % m
if remainder[i] > 0:
spare.append([i, remainder[i]])
remainder[i] = 0
elif remainder[i] < 0:
if len(spare) > 0:
while len(spare) > 0 and remainder[i] < 0:
rem = min(spare[-1][1], abs(remainder[i]))
if spare[-1][0] in convert.keys():
convert[spare[-1][0]].append([(i - spare[-1][0]) % m, rem])
else:
convert[spare[-1][0]] = [[(i - spare[-1][0]) % m, rem]]
spare[-1][1] -= rem
remainder[i] += rem
if spare[-1][1] == 0:
spare.pop()
if remainder[i] == 0:
deficit.remove(i)
i += 1
for i in range(n):
rem = nums[i] % m
if rem in convert.keys():
moves += convert[rem][-1][0]
nums[i] += convert[rem][-1][0]
convert[rem][-1][1] -= 1
if convert[rem][-1][1] == 0:
convert[rem].pop()
if len(convert[rem]) == 0:
del convert[rem]
print(moves)
print(*nums) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER LIST BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER NUMBER LIST LIST BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | n, m = map(int, input().split())
a = list(map(int, input().split()))
t = n // m
remain = [[] for i in range(m)]
for i in range(n):
x = a[i] % m
remain[x].append(i)
ans = 0
f = []
for i in range(2 * m):
cur = i % m
while len(remain[cur]) > t:
elm = remain[cur].pop()
f.append([elm, i])
while len(remain[cur]) < t and len(f) != 0:
elm, j = f.pop()
remain[cur].append(elm)
a[elm] += abs(i - j)
ans += abs(i - j)
print(ans)
print(*a) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | import sys
input = sys.stdin.readline
n, m = map(int, input().split())
a = list(map(int, input().split()))
idx = [[] for i in range(m)]
for i in range(n):
idx[a[i] % m].append(i)
k = n // m
free = []
for i in range(m):
while len(idx[i]) > k:
v = idx[i].pop()
free.append(v)
while free and len(idx[i]) < k:
v = free.pop()
idx[i].append(v)
for i in range(m):
while len(idx[i]) > k:
v = idx[i].pop()
free.append(v)
while free and len(idx[i]) < k:
v = free.pop()
idx[i].append(v)
cost = 0
for i in range(m):
for j in idx[i]:
dx = (i - a[j] % m) % m
cost += dx
a[j] += dx
print(cost)
print(*a) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | from sys import stdin
def solve():
n, m = map(int, stdin.readline().split())
a = list(map(int, stdin.readline().split()))
ans = 0
b = sorted(map(list, zip(map(lambda x: x % m, a), range(n))))
j = 0
need = n // m
for i in range(m):
e = j
while e < len(b) and b[e][0] == i:
e += 1
if e - j >= need:
for t in range(j + need, e):
b.append(b[t])
j = e
else:
for _ in range(need - (e - j)):
w = b.pop()
ans += (i - w[0]) % m
a[w[1]] += (i - w[0]) % m
j = e
print(ans)
print(*a)
solve() | 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 NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | def main():
n, m = map(int, input().split())
arr = [int(ai) for ai in input().split()]
freq = [[] for i in range(m)]
aux = []
count, c = 0, n // m
for i, ai in enumerate(arr):
freq[ai % m].append(i)
for _ in range(2):
for i in range(m):
if len(freq[i]) > c:
aux += freq[i][c:]
freq[i] = freq[i][:c]
while len(freq[i]) < c and len(aux):
idx = aux.pop(-1)
remainder = arr[idx] % m
moves = i - remainder if i >= remainder else m - remainder + i
count += moves
arr[idx] += moves
freq[i].append(idx)
print("%d\n%s" % (count, " ".join(map(str, arr))))
main() | FUNC_DEF 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 FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER BIN_OP VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement.
-----Input-----
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
-----Output-----
In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
-----Examples-----
Input
6 3
3 2 0 6 10 12
Output
3
3 2 0 7 10 14
Input
4 2
0 1 2 3
Output
0
0 1 2 3 | from sys import stdin
input = stdin.buffer.readline
n, m = map(int, input().split())
arr = [int(x) for x in input().split()]
dp = [[] for i in range(m)]
for i in range(n):
dp[arr[i] % m].append(i)
res = 0
k = n // m
ans = arr.copy()
s = []
for t in range(2):
for i in range(m):
if len(dp[i]) < k:
while len(s) != 0 and len(dp[i]) < k:
x = s.pop()
y = arr[x] % m
if i > y:
ans[x] = ans[x] + (i - y)
res = res + (i - y)
else:
ans[x] = ans[x] + (m - 1 - y) + (i + 1)
res = res + (m - 1 - y) + (i + 1)
dp[i].append("xxx")
if len(dp[i]) > k:
while len(dp[i]) > k:
s.append(dp[i].pop())
print(res)
print(*ans) | ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING IF FUNC_CALL VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Your friend Ivan asked you to help him rearrange his desktop. The desktop can be represented as a rectangle matrix of size $n \times m$ consisting of characters '.' (empty cell of the desktop) and '*' (an icon).
The desktop is called good if all its icons are occupying some prefix of full columns and, possibly, the prefix of the next column (and there are no icons outside this figure). In other words, some amount of first columns will be filled with icons and, possibly, some amount of first cells of the next (after the last full column) column will be also filled with icons (and all the icons on the desktop belong to this figure). This is pretty much the same as the real life icons arrangement.
In one move, you can take one icon and move it to any empty cell in the desktop.
Ivan loves to add some icons to his desktop and remove them from it, so he is asking you to answer $q$ queries: what is the minimum number of moves required to make the desktop good after adding/removing one icon?
Note that queries are permanent and change the state of the desktop.
-----Input-----
The first line of the input contains three integers $n$, $m$ and $q$ ($1 \le n, m \le 1000; 1 \le q \le 2 \cdot 10^5$) β the number of rows in the desktop, the number of columns in the desktop and the number of queries, respectively.
The next $n$ lines contain the description of the desktop. The $i$-th of them contains $m$ characters '.' and '*' β the description of the $i$-th row of the desktop.
The next $q$ lines describe queries. The $i$-th of them contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n; 1 \le y_i \le m$) β the position of the cell which changes its state (if this cell contained the icon before, then this icon is removed, otherwise an icon appears in this cell).
-----Output-----
Print $q$ integers. The $i$-th of them should be the minimum number of moves required to make the desktop good after applying the first $i$ queries.
-----Examples-----
Input
4 4 8
..**
.*..
*...
...*
1 3
2 3
3 1
2 3
3 4
4 3
2 3
2 2
Output
3
4
4
3
4
5
5
5
Input
2 5 5
*...*
*****
1 3
2 2
1 3
1 5
2 3
Output
2
3
3
3
2
-----Note-----
None | n, m, q = [int(i) for i in input().split()]
temp = [input() for _ in range(n)]
desktop = []
for i in range(m):
for j in range(n):
desktop.append(temp[j][i])
total = desktop.count("*")
ans = desktop[:total].count(".")
for _ in range(q):
x, y = [(int(i) - 1) for i in input().split()]
if desktop[y * n + x] == "*":
ans -= desktop[total - 1] == "."
desktop[y * n + x] = "."
total -= 1
ans += y * n + x < total
else:
ans += desktop[total] == "."
desktop[y * n + x] = "*"
total += 1
ans -= y * n + x < total
print(ans) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP BIN_OP VAR VAR VAR STRING VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR STRING VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR STRING VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Your friend Ivan asked you to help him rearrange his desktop. The desktop can be represented as a rectangle matrix of size $n \times m$ consisting of characters '.' (empty cell of the desktop) and '*' (an icon).
The desktop is called good if all its icons are occupying some prefix of full columns and, possibly, the prefix of the next column (and there are no icons outside this figure). In other words, some amount of first columns will be filled with icons and, possibly, some amount of first cells of the next (after the last full column) column will be also filled with icons (and all the icons on the desktop belong to this figure). This is pretty much the same as the real life icons arrangement.
In one move, you can take one icon and move it to any empty cell in the desktop.
Ivan loves to add some icons to his desktop and remove them from it, so he is asking you to answer $q$ queries: what is the minimum number of moves required to make the desktop good after adding/removing one icon?
Note that queries are permanent and change the state of the desktop.
-----Input-----
The first line of the input contains three integers $n$, $m$ and $q$ ($1 \le n, m \le 1000; 1 \le q \le 2 \cdot 10^5$) β the number of rows in the desktop, the number of columns in the desktop and the number of queries, respectively.
The next $n$ lines contain the description of the desktop. The $i$-th of them contains $m$ characters '.' and '*' β the description of the $i$-th row of the desktop.
The next $q$ lines describe queries. The $i$-th of them contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n; 1 \le y_i \le m$) β the position of the cell which changes its state (if this cell contained the icon before, then this icon is removed, otherwise an icon appears in this cell).
-----Output-----
Print $q$ integers. The $i$-th of them should be the minimum number of moves required to make the desktop good after applying the first $i$ queries.
-----Examples-----
Input
4 4 8
..**
.*..
*...
...*
1 3
2 3
3 1
2 3
3 4
4 3
2 3
2 2
Output
3
4
4
3
4
5
5
5
Input
2 5 5
*...*
*****
1 3
2 2
1 3
1 5
2 3
Output
2
3
3
3
2
-----Note-----
None | import sys
input = sys.stdin.readline
def solve():
R, C, Q = map(int, input().split())
s = []
for _ in range(R):
s.append(list(input().strip()))
t = []
for i in range(C):
for j in range(R):
t.append(s[j][i])
total = t.count("*")
current = t[total:].count("*")
for _ in range(Q):
x, y = map(int, input().split())
x -= 1
y -= 1
now = y * R + x
if t[now] == "*":
if now >= total:
current -= 1
t[now] = "."
total -= 1
if t[total] == "*":
current += 1
else:
if now >= total:
current += 1
t[now] = "*"
if t[total] == "*":
current -= 1
total += 1
print(current)
solve() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR STRING IF VAR VAR VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Your friend Ivan asked you to help him rearrange his desktop. The desktop can be represented as a rectangle matrix of size $n \times m$ consisting of characters '.' (empty cell of the desktop) and '*' (an icon).
The desktop is called good if all its icons are occupying some prefix of full columns and, possibly, the prefix of the next column (and there are no icons outside this figure). In other words, some amount of first columns will be filled with icons and, possibly, some amount of first cells of the next (after the last full column) column will be also filled with icons (and all the icons on the desktop belong to this figure). This is pretty much the same as the real life icons arrangement.
In one move, you can take one icon and move it to any empty cell in the desktop.
Ivan loves to add some icons to his desktop and remove them from it, so he is asking you to answer $q$ queries: what is the minimum number of moves required to make the desktop good after adding/removing one icon?
Note that queries are permanent and change the state of the desktop.
-----Input-----
The first line of the input contains three integers $n$, $m$ and $q$ ($1 \le n, m \le 1000; 1 \le q \le 2 \cdot 10^5$) β the number of rows in the desktop, the number of columns in the desktop and the number of queries, respectively.
The next $n$ lines contain the description of the desktop. The $i$-th of them contains $m$ characters '.' and '*' β the description of the $i$-th row of the desktop.
The next $q$ lines describe queries. The $i$-th of them contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n; 1 \le y_i \le m$) β the position of the cell which changes its state (if this cell contained the icon before, then this icon is removed, otherwise an icon appears in this cell).
-----Output-----
Print $q$ integers. The $i$-th of them should be the minimum number of moves required to make the desktop good after applying the first $i$ queries.
-----Examples-----
Input
4 4 8
..**
.*..
*...
...*
1 3
2 3
3 1
2 3
3 4
4 3
2 3
2 2
Output
3
4
4
3
4
5
5
5
Input
2 5 5
*...*
*****
1 3
2 2
1 3
1 5
2 3
Output
2
3
3
3
2
-----Note-----
None | n, m, q = map(int, input().split())
S = [input() for _ in range(n)]
cnt = 0
for s in S:
cnt += s.count("*")
S = [list(s) for s in S]
In = 0
Out = 0
for x in range(n):
for y in range(m):
if S[x][y] == "*":
if n * y + x >= cnt:
Out += 1
for i in range(q):
x, y = map(int, input().split())
x -= 1
y -= 1
if S[x][y] == "*":
S[x][y] = "."
cnt -= 1
xx = cnt % n
yy = cnt // n
if S[xx][yy] == "*":
Out += 1
if n * y + x > cnt:
Out -= 1
else:
xx = cnt % n
yy = cnt // n
if S[xx][yy] == "*":
Out -= 1
cnt += 1
S[x][y] = "*"
if n * y + x >= cnt:
Out += 1
print(Out) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING IF BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR STRING IF BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Your friend Ivan asked you to help him rearrange his desktop. The desktop can be represented as a rectangle matrix of size $n \times m$ consisting of characters '.' (empty cell of the desktop) and '*' (an icon).
The desktop is called good if all its icons are occupying some prefix of full columns and, possibly, the prefix of the next column (and there are no icons outside this figure). In other words, some amount of first columns will be filled with icons and, possibly, some amount of first cells of the next (after the last full column) column will be also filled with icons (and all the icons on the desktop belong to this figure). This is pretty much the same as the real life icons arrangement.
In one move, you can take one icon and move it to any empty cell in the desktop.
Ivan loves to add some icons to his desktop and remove them from it, so he is asking you to answer $q$ queries: what is the minimum number of moves required to make the desktop good after adding/removing one icon?
Note that queries are permanent and change the state of the desktop.
-----Input-----
The first line of the input contains three integers $n$, $m$ and $q$ ($1 \le n, m \le 1000; 1 \le q \le 2 \cdot 10^5$) β the number of rows in the desktop, the number of columns in the desktop and the number of queries, respectively.
The next $n$ lines contain the description of the desktop. The $i$-th of them contains $m$ characters '.' and '*' β the description of the $i$-th row of the desktop.
The next $q$ lines describe queries. The $i$-th of them contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n; 1 \le y_i \le m$) β the position of the cell which changes its state (if this cell contained the icon before, then this icon is removed, otherwise an icon appears in this cell).
-----Output-----
Print $q$ integers. The $i$-th of them should be the minimum number of moves required to make the desktop good after applying the first $i$ queries.
-----Examples-----
Input
4 4 8
..**
.*..
*...
...*
1 3
2 3
3 1
2 3
3 4
4 3
2 3
2 2
Output
3
4
4
3
4
5
5
5
Input
2 5 5
*...*
*****
1 3
2 2
1 3
1 5
2 3
Output
2
3
3
3
2
-----Note-----
None | n, m, q = map(int, input().split())
z = []
a = []
c = 0
ans = 0
for i in range(n):
z.append(list(input()))
for i in range(m):
for j in range(n):
if z[j][i] == "*":
a.append(1)
c += 1
else:
a.append(0)
ans = c
for i in range(c):
if a[i] == 1:
ans -= 1
for i in range(q):
x, y = map(int, input().split())
if a[n * (y - 1) + x - 1] == 1:
a[n * (y - 1) + x - 1] = 0
c -= 1
if n * (y - 1) + x - 1 > c:
ans -= 1
if a[c] == 1:
ans += 1
elif a[n * (y - 1) + x - 1] == 0:
a[n * (y - 1) + x - 1] = 1
c += 1
if n * (y - 1) + x - 1 >= c - 1:
ans += 1
if c:
if a[c - 1] == 1:
ans -= 1
print(ans) | ASSIGN VAR VAR VAR FUNC_CALL 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 EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Your friend Ivan asked you to help him rearrange his desktop. The desktop can be represented as a rectangle matrix of size $n \times m$ consisting of characters '.' (empty cell of the desktop) and '*' (an icon).
The desktop is called good if all its icons are occupying some prefix of full columns and, possibly, the prefix of the next column (and there are no icons outside this figure). In other words, some amount of first columns will be filled with icons and, possibly, some amount of first cells of the next (after the last full column) column will be also filled with icons (and all the icons on the desktop belong to this figure). This is pretty much the same as the real life icons arrangement.
In one move, you can take one icon and move it to any empty cell in the desktop.
Ivan loves to add some icons to his desktop and remove them from it, so he is asking you to answer $q$ queries: what is the minimum number of moves required to make the desktop good after adding/removing one icon?
Note that queries are permanent and change the state of the desktop.
-----Input-----
The first line of the input contains three integers $n$, $m$ and $q$ ($1 \le n, m \le 1000; 1 \le q \le 2 \cdot 10^5$) β the number of rows in the desktop, the number of columns in the desktop and the number of queries, respectively.
The next $n$ lines contain the description of the desktop. The $i$-th of them contains $m$ characters '.' and '*' β the description of the $i$-th row of the desktop.
The next $q$ lines describe queries. The $i$-th of them contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n; 1 \le y_i \le m$) β the position of the cell which changes its state (if this cell contained the icon before, then this icon is removed, otherwise an icon appears in this cell).
-----Output-----
Print $q$ integers. The $i$-th of them should be the minimum number of moves required to make the desktop good after applying the first $i$ queries.
-----Examples-----
Input
4 4 8
..**
.*..
*...
...*
1 3
2 3
3 1
2 3
3 4
4 3
2 3
2 2
Output
3
4
4
3
4
5
5
5
Input
2 5 5
*...*
*****
1 3
2 2
1 3
1 5
2 3
Output
2
3
3
3
2
-----Note-----
None | n, m, q = list(map(int, input().split()))
s = []
cnt = 0
for _ in range(n):
s += [list(input())]
for elem in s[-1]:
if elem == "*":
cnt += 1
ans = cnt
for i in range(cnt):
if s[i % n][i // n] == "*":
ans -= 1
for _ in range(q):
x, y = list(map(int, input().split()))
x -= 1
y -= 1
if s[x][y] == "*":
cnt -= 1
ans -= 1
if s[cnt % n][cnt // n] == "*":
ans += 1
s[x][y] = "."
if cnt > x + y * n:
ans += 1
else:
cnt += 1
ans += 1
s[x][y] = "*"
if s[(cnt - 1) % n][(cnt - 1) // n] == "*":
ans -= 1
if cnt > 1 + x + y * n:
ans -= 1
print(ans) | ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR STRING IF VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR STRING IF VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR STRING VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Your friend Ivan asked you to help him rearrange his desktop. The desktop can be represented as a rectangle matrix of size $n \times m$ consisting of characters '.' (empty cell of the desktop) and '*' (an icon).
The desktop is called good if all its icons are occupying some prefix of full columns and, possibly, the prefix of the next column (and there are no icons outside this figure). In other words, some amount of first columns will be filled with icons and, possibly, some amount of first cells of the next (after the last full column) column will be also filled with icons (and all the icons on the desktop belong to this figure). This is pretty much the same as the real life icons arrangement.
In one move, you can take one icon and move it to any empty cell in the desktop.
Ivan loves to add some icons to his desktop and remove them from it, so he is asking you to answer $q$ queries: what is the minimum number of moves required to make the desktop good after adding/removing one icon?
Note that queries are permanent and change the state of the desktop.
-----Input-----
The first line of the input contains three integers $n$, $m$ and $q$ ($1 \le n, m \le 1000; 1 \le q \le 2 \cdot 10^5$) β the number of rows in the desktop, the number of columns in the desktop and the number of queries, respectively.
The next $n$ lines contain the description of the desktop. The $i$-th of them contains $m$ characters '.' and '*' β the description of the $i$-th row of the desktop.
The next $q$ lines describe queries. The $i$-th of them contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n; 1 \le y_i \le m$) β the position of the cell which changes its state (if this cell contained the icon before, then this icon is removed, otherwise an icon appears in this cell).
-----Output-----
Print $q$ integers. The $i$-th of them should be the minimum number of moves required to make the desktop good after applying the first $i$ queries.
-----Examples-----
Input
4 4 8
..**
.*..
*...
...*
1 3
2 3
3 1
2 3
3 4
4 3
2 3
2 2
Output
3
4
4
3
4
5
5
5
Input
2 5 5
*...*
*****
1 3
2 2
1 3
1 5
2 3
Output
2
3
3
3
2
-----Note-----
None | n, m, q = map(int, input().split(" "))
desktop = []
count_icons = 0
for _ in range(n):
desktop.append([c for c in input()])
count_icons += desktop[-1].count("*")
desktop = [desktop[row][col] for col in range(m) for row in range(n)]
count_correct = sum(1 for c in desktop[:count_icons] if c == "*")
for _ in range(q):
x, y = map(int, input().split(" "))
i = (y - 1) * n + (x - 1)
if desktop[i] == "*":
if desktop[count_icons - 1] == "*":
count_correct -= 1
count_icons -= 1
if i < count_icons:
count_correct -= 1
desktop[i] = "."
print(count_icons - count_correct)
else:
if i < count_icons:
count_correct += 1
count_icons += 1
desktop[i] = "*"
if desktop[count_icons - 1] == "*":
count_correct += 1
print(count_icons - count_correct) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER STRING ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Your friend Ivan asked you to help him rearrange his desktop. The desktop can be represented as a rectangle matrix of size $n \times m$ consisting of characters '.' (empty cell of the desktop) and '*' (an icon).
The desktop is called good if all its icons are occupying some prefix of full columns and, possibly, the prefix of the next column (and there are no icons outside this figure). In other words, some amount of first columns will be filled with icons and, possibly, some amount of first cells of the next (after the last full column) column will be also filled with icons (and all the icons on the desktop belong to this figure). This is pretty much the same as the real life icons arrangement.
In one move, you can take one icon and move it to any empty cell in the desktop.
Ivan loves to add some icons to his desktop and remove them from it, so he is asking you to answer $q$ queries: what is the minimum number of moves required to make the desktop good after adding/removing one icon?
Note that queries are permanent and change the state of the desktop.
-----Input-----
The first line of the input contains three integers $n$, $m$ and $q$ ($1 \le n, m \le 1000; 1 \le q \le 2 \cdot 10^5$) β the number of rows in the desktop, the number of columns in the desktop and the number of queries, respectively.
The next $n$ lines contain the description of the desktop. The $i$-th of them contains $m$ characters '.' and '*' β the description of the $i$-th row of the desktop.
The next $q$ lines describe queries. The $i$-th of them contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n; 1 \le y_i \le m$) β the position of the cell which changes its state (if this cell contained the icon before, then this icon is removed, otherwise an icon appears in this cell).
-----Output-----
Print $q$ integers. The $i$-th of them should be the minimum number of moves required to make the desktop good after applying the first $i$ queries.
-----Examples-----
Input
4 4 8
..**
.*..
*...
...*
1 3
2 3
3 1
2 3
3 4
4 3
2 3
2 2
Output
3
4
4
3
4
5
5
5
Input
2 5 5
*...*
*****
1 3
2 2
1 3
1 5
2 3
Output
2
3
3
3
2
-----Note-----
None | import sys
input = sys.stdin.readline
write = sys.stdout.write
n, m, q = list(map(int, input().split()))
data = []
cnt = 0
for i in range(n):
data.append(list(input().strip()))
cnt += data[-1].count("*")
temp = 0
match = 0
for j in range(m):
if temp == cnt:
break
for i in range(n):
if temp == cnt:
break
temp += 1
if data[i][j] == "*":
match += 1
for _ in range(q):
x, y = list(map(int, input().split()))
x -= 1
y -= 1
temp = y * n + x + 1
if data[x][y] == ".":
data[x][y] = "*"
if temp <= cnt:
match += 1
cnt += 1
if data[(cnt - 1) % n][(cnt - 1) // n] == "*":
match += 1
else:
if data[(cnt - 1) % n][(cnt - 1) // n] == "*":
match -= 1
data[x][y] = "."
cnt -= 1
if temp <= cnt:
match -= 1
write(str(cnt - match) + "\n") | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING IF VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR STRING VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR STRING VAR NUMBER ASSIGN VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR STRING |
Your friend Ivan asked you to help him rearrange his desktop. The desktop can be represented as a rectangle matrix of size $n \times m$ consisting of characters '.' (empty cell of the desktop) and '*' (an icon).
The desktop is called good if all its icons are occupying some prefix of full columns and, possibly, the prefix of the next column (and there are no icons outside this figure). In other words, some amount of first columns will be filled with icons and, possibly, some amount of first cells of the next (after the last full column) column will be also filled with icons (and all the icons on the desktop belong to this figure). This is pretty much the same as the real life icons arrangement.
In one move, you can take one icon and move it to any empty cell in the desktop.
Ivan loves to add some icons to his desktop and remove them from it, so he is asking you to answer $q$ queries: what is the minimum number of moves required to make the desktop good after adding/removing one icon?
Note that queries are permanent and change the state of the desktop.
-----Input-----
The first line of the input contains three integers $n$, $m$ and $q$ ($1 \le n, m \le 1000; 1 \le q \le 2 \cdot 10^5$) β the number of rows in the desktop, the number of columns in the desktop and the number of queries, respectively.
The next $n$ lines contain the description of the desktop. The $i$-th of them contains $m$ characters '.' and '*' β the description of the $i$-th row of the desktop.
The next $q$ lines describe queries. The $i$-th of them contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n; 1 \le y_i \le m$) β the position of the cell which changes its state (if this cell contained the icon before, then this icon is removed, otherwise an icon appears in this cell).
-----Output-----
Print $q$ integers. The $i$-th of them should be the minimum number of moves required to make the desktop good after applying the first $i$ queries.
-----Examples-----
Input
4 4 8
..**
.*..
*...
...*
1 3
2 3
3 1
2 3
3 4
4 3
2 3
2 2
Output
3
4
4
3
4
5
5
5
Input
2 5 5
*...*
*****
1 3
2 2
1 3
1 5
2 3
Output
2
3
3
3
2
-----Note-----
None | n, m, q = map(int, input().split())
totcnt = 0
gudcnt = 0
b = []
b.append(0)
a = [input() for i in range(n)]
for j in range(m):
for i in range(n):
b.append(bool(a[i][j] == "*"))
if a[i][j] == "*":
totcnt += 1
for i in range(totcnt + 1):
gudcnt += b[i]
for i in range(q):
x, y = map(int, input().split())
ind = n * (y - 1) + x
if b[ind]:
if b[totcnt]:
gudcnt -= 1
totcnt -= 1
if ind <= totcnt:
gudcnt -= 1
b[ind] = 0
else:
b[ind] = 1
if ind <= totcnt:
gudcnt += 1
totcnt += 1
if b[totcnt]:
gudcnt += 1
print(totcnt - gudcnt) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING IF VAR VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Your friend Ivan asked you to help him rearrange his desktop. The desktop can be represented as a rectangle matrix of size $n \times m$ consisting of characters '.' (empty cell of the desktop) and '*' (an icon).
The desktop is called good if all its icons are occupying some prefix of full columns and, possibly, the prefix of the next column (and there are no icons outside this figure). In other words, some amount of first columns will be filled with icons and, possibly, some amount of first cells of the next (after the last full column) column will be also filled with icons (and all the icons on the desktop belong to this figure). This is pretty much the same as the real life icons arrangement.
In one move, you can take one icon and move it to any empty cell in the desktop.
Ivan loves to add some icons to his desktop and remove them from it, so he is asking you to answer $q$ queries: what is the minimum number of moves required to make the desktop good after adding/removing one icon?
Note that queries are permanent and change the state of the desktop.
-----Input-----
The first line of the input contains three integers $n$, $m$ and $q$ ($1 \le n, m \le 1000; 1 \le q \le 2 \cdot 10^5$) β the number of rows in the desktop, the number of columns in the desktop and the number of queries, respectively.
The next $n$ lines contain the description of the desktop. The $i$-th of them contains $m$ characters '.' and '*' β the description of the $i$-th row of the desktop.
The next $q$ lines describe queries. The $i$-th of them contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n; 1 \le y_i \le m$) β the position of the cell which changes its state (if this cell contained the icon before, then this icon is removed, otherwise an icon appears in this cell).
-----Output-----
Print $q$ integers. The $i$-th of them should be the minimum number of moves required to make the desktop good after applying the first $i$ queries.
-----Examples-----
Input
4 4 8
..**
.*..
*...
...*
1 3
2 3
3 1
2 3
3 4
4 3
2 3
2 2
Output
3
4
4
3
4
5
5
5
Input
2 5 5
*...*
*****
1 3
2 2
1 3
1 5
2 3
Output
2
3
3
3
2
-----Note-----
None | def main():
n, m, q = map(int, input().split())
s = set()
table = [False] * (n * m)
num = 0
for i in range(n):
s1 = input()
for j in range(m):
if s1[j] == "*":
s.add((i, j))
table[j * n + i] = True
num += 1
x = num // n
x1 = num % n
ans = 0
for a, b in s:
if b >= x and (b != x or a >= x1):
ans += 1
num -= 1
for _ in range(q):
a, b = map(int, input().split())
a -= 1
b -= 1
index = b * n + a
if table[index]:
num -= 1
if index == num + 1:
pass
else:
if index > num:
ans -= 1
if table[num + 1]:
ans += 1
else:
num += 1
if index == num:
pass
else:
if index > num:
ans += 1
if table[num]:
ans -= 1
table[index] = not table[index]
print(ans)
main() | FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Your friend Ivan asked you to help him rearrange his desktop. The desktop can be represented as a rectangle matrix of size $n \times m$ consisting of characters '.' (empty cell of the desktop) and '*' (an icon).
The desktop is called good if all its icons are occupying some prefix of full columns and, possibly, the prefix of the next column (and there are no icons outside this figure). In other words, some amount of first columns will be filled with icons and, possibly, some amount of first cells of the next (after the last full column) column will be also filled with icons (and all the icons on the desktop belong to this figure). This is pretty much the same as the real life icons arrangement.
In one move, you can take one icon and move it to any empty cell in the desktop.
Ivan loves to add some icons to his desktop and remove them from it, so he is asking you to answer $q$ queries: what is the minimum number of moves required to make the desktop good after adding/removing one icon?
Note that queries are permanent and change the state of the desktop.
-----Input-----
The first line of the input contains three integers $n$, $m$ and $q$ ($1 \le n, m \le 1000; 1 \le q \le 2 \cdot 10^5$) β the number of rows in the desktop, the number of columns in the desktop and the number of queries, respectively.
The next $n$ lines contain the description of the desktop. The $i$-th of them contains $m$ characters '.' and '*' β the description of the $i$-th row of the desktop.
The next $q$ lines describe queries. The $i$-th of them contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n; 1 \le y_i \le m$) β the position of the cell which changes its state (if this cell contained the icon before, then this icon is removed, otherwise an icon appears in this cell).
-----Output-----
Print $q$ integers. The $i$-th of them should be the minimum number of moves required to make the desktop good after applying the first $i$ queries.
-----Examples-----
Input
4 4 8
..**
.*..
*...
...*
1 3
2 3
3 1
2 3
3 4
4 3
2 3
2 2
Output
3
4
4
3
4
5
5
5
Input
2 5 5
*...*
*****
1 3
2 2
1 3
1 5
2 3
Output
2
3
3
3
2
-----Note-----
None | n, m, t = map(int, input().split())
mapa = []
count = 0
for i in range(n):
k = input()
mmmmmmm = []
for j in range(m):
mmmmmmm.append(k[j])
if k[j] == "*":
count += 1
mapa.append(mmmmmmm)
ray = (count - 1) // n
row = (count - 1) % n
i = 0
j = 0
bad = count * 1
while i <= ray:
if i < ray:
while j < n:
if mapa[j][i] == "*":
bad -= 1
j += 1
i += 1
j = 0
else:
while j <= row:
if mapa[j][i] == "*":
bad -= 1
j += 1
i += 1
i -= 1
j -= 1
for p in range(t):
xx, yy = map(int, input().split())
if mapa[xx - 1][yy - 1] == ".":
mapa[xx - 1][yy - 1] = "*"
if yy - 1 > i or yy - 1 == i and xx - 1 > j:
bad += 1
if j < n - 1:
j += 1
else:
j = 0
i += 1
if mapa[j][i] == "*":
bad -= 1
else:
if mapa[j][i] == "*":
bad += 1
mapa[xx - 1][yy - 1] = "."
if yy - 1 > i or yy - 1 == i and xx - 1 >= j:
bad -= 1
if j == 0:
i -= 1
j = n - 1
else:
j -= 1
print(bad) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR WHILE VAR VAR IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Your friend Ivan asked you to help him rearrange his desktop. The desktop can be represented as a rectangle matrix of size $n \times m$ consisting of characters '.' (empty cell of the desktop) and '*' (an icon).
The desktop is called good if all its icons are occupying some prefix of full columns and, possibly, the prefix of the next column (and there are no icons outside this figure). In other words, some amount of first columns will be filled with icons and, possibly, some amount of first cells of the next (after the last full column) column will be also filled with icons (and all the icons on the desktop belong to this figure). This is pretty much the same as the real life icons arrangement.
In one move, you can take one icon and move it to any empty cell in the desktop.
Ivan loves to add some icons to his desktop and remove them from it, so he is asking you to answer $q$ queries: what is the minimum number of moves required to make the desktop good after adding/removing one icon?
Note that queries are permanent and change the state of the desktop.
-----Input-----
The first line of the input contains three integers $n$, $m$ and $q$ ($1 \le n, m \le 1000; 1 \le q \le 2 \cdot 10^5$) β the number of rows in the desktop, the number of columns in the desktop and the number of queries, respectively.
The next $n$ lines contain the description of the desktop. The $i$-th of them contains $m$ characters '.' and '*' β the description of the $i$-th row of the desktop.
The next $q$ lines describe queries. The $i$-th of them contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n; 1 \le y_i \le m$) β the position of the cell which changes its state (if this cell contained the icon before, then this icon is removed, otherwise an icon appears in this cell).
-----Output-----
Print $q$ integers. The $i$-th of them should be the minimum number of moves required to make the desktop good after applying the first $i$ queries.
-----Examples-----
Input
4 4 8
..**
.*..
*...
...*
1 3
2 3
3 1
2 3
3 4
4 3
2 3
2 2
Output
3
4
4
3
4
5
5
5
Input
2 5 5
*...*
*****
1 3
2 2
1 3
1 5
2 3
Output
2
3
3
3
2
-----Note-----
None | out = ""
n, m, q = [int(x) for x in input().split()]
map_ = [[] for _ in range(m)]
count = 0
for _ in range(n):
line = input()
for i in range(m):
x = line[i]
if x == "*":
map_[i].append(1)
count += 1
else:
map_[i].append(0)
y = 0
x = 0
count2 = 0
while y * n + x < count:
if map_[y][x] == 1:
count2 += 1
x += 1
if x == n:
y += 1
x = 0
for _ in range(q):
x, y = [(int(x) - 1) for x in input().split()]
map_[y][x] = 1 - map_[y][x]
if map_[y][x] == 1:
count += 1
ye = (count - 1) // n
xe = (count - 1) % n
count2 += map_[ye][xe]
if y * n + x < ye * n + xe:
count2 += 1
else:
ye = (count - 1) // n
xe = (count - 1) % n
count2 -= map_[ye][xe]
if y * n + x <= ye * n + xe:
count2 -= 1
count -= 1
out += str(count - count2) + "\n"
print(out) | ASSIGN VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR VAR |
Your friend Ivan asked you to help him rearrange his desktop. The desktop can be represented as a rectangle matrix of size $n \times m$ consisting of characters '.' (empty cell of the desktop) and '*' (an icon).
The desktop is called good if all its icons are occupying some prefix of full columns and, possibly, the prefix of the next column (and there are no icons outside this figure). In other words, some amount of first columns will be filled with icons and, possibly, some amount of first cells of the next (after the last full column) column will be also filled with icons (and all the icons on the desktop belong to this figure). This is pretty much the same as the real life icons arrangement.
In one move, you can take one icon and move it to any empty cell in the desktop.
Ivan loves to add some icons to his desktop and remove them from it, so he is asking you to answer $q$ queries: what is the minimum number of moves required to make the desktop good after adding/removing one icon?
Note that queries are permanent and change the state of the desktop.
-----Input-----
The first line of the input contains three integers $n$, $m$ and $q$ ($1 \le n, m \le 1000; 1 \le q \le 2 \cdot 10^5$) β the number of rows in the desktop, the number of columns in the desktop and the number of queries, respectively.
The next $n$ lines contain the description of the desktop. The $i$-th of them contains $m$ characters '.' and '*' β the description of the $i$-th row of the desktop.
The next $q$ lines describe queries. The $i$-th of them contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n; 1 \le y_i \le m$) β the position of the cell which changes its state (if this cell contained the icon before, then this icon is removed, otherwise an icon appears in this cell).
-----Output-----
Print $q$ integers. The $i$-th of them should be the minimum number of moves required to make the desktop good after applying the first $i$ queries.
-----Examples-----
Input
4 4 8
..**
.*..
*...
...*
1 3
2 3
3 1
2 3
3 4
4 3
2 3
2 2
Output
3
4
4
3
4
5
5
5
Input
2 5 5
*...*
*****
1 3
2 2
1 3
1 5
2 3
Output
2
3
3
3
2
-----Note-----
None | n, m, q = map(int, input().split())
a = []
arr = []
for i in range(n):
row = input()
a.append(row)
for j in range(m):
for i in range(n):
arr.append(a[i][j])
tot = arr.count("*")
inside = arr[0:tot].count("*")
for h in range(q):
x, y = map(int, input().split())
index = (y - 1) * n + x - 1
limit = tot - 1
if arr[index] == ".":
arr[index] = "*"
tot += 1
if arr[limit + 1] == "*":
inside += 1
if index <= limit:
inside += 1
else:
tot -= 1
if arr[limit] == "*":
inside -= 1
if index < limit:
inside -= 1
arr[index] = "."
print(tot - inside) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
pos = {}
for i in range(n):
pos[a[i]] = i
dic = {}
for i in range(n):
cur = pos[b[i]] - i
if cur < 0:
cur += n
if cur in dic:
dic[cur] += 1
else:
dic[cur] = 1
res = 0
for key, value in dic.items():
res = max(res, value)
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | n = int(input())
d1 = {}
d2 = {}
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
d3 = {}
for i in range(n):
d1[l1[i]] = i
d2[l2[i]] = i
for i in range(n):
if d1[i + 1] - d2[i + 1] in d3 and d1[i + 1] - d2[i + 1] > 0:
d3[d1[i + 1] - d2[i + 1]] = d3[d1[i + 1] - d2[i + 1]] + 1
elif n + d1[i + 1] - d2[i + 1] in d3:
d3[n + (d1[i + 1] - d2[i + 1])] = d3[n + (d1[i + 1] - d2[i + 1])] + 1
elif d1[i + 1] - d2[i + 1] > 0:
d3[d1[i + 1] - d2[i + 1]] = 1
else:
d3[n + (d1[i + 1] - d2[i + 1])] = 1
z = float("-inf")
for i in d3:
z = max(z, d3[i])
print(z) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | def result(n, a, b):
vec = [(0) for _ in range(n)]
l = [(0) for _ in range(n)]
for i, el in enumerate(a):
vec[el - 1] = i
for i, el in enumerate(b):
l[(vec[el - 1] - i) % n] += 1
return max(l)
n = int(input())
a = list(map(lambda x: int(x), input().split(" ")))
b = list(map(lambda x: int(x), input().split(" ")))
print(result(n, a, b)) | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | from sys import stdin
def readline():
return stdin.readline()
def solve(n, a, b):
shifts = []
pos = [0] * n
for i in range(0, n):
pos[b[i]] = i
for i in range(0, n):
shift = i - pos[a[i]]
if shift < 0:
shift += n
shifts.append(shift)
shifts.sort()
result = 1
shifts = [-1, *shifts]
for i in range(1, n + 1):
if shifts[i] != shifts[i - 1]:
pos = i + 1
while pos < n + 1 and shifts[i] == shifts[pos]:
pos += 1
result = max(result, pos - i)
return result
n = int(readline().rstrip("\n"))
a = list(map(lambda x: int(x) - 1, readline().split(" ")))
b = list(map(lambda x: int(x) - 1, readline().split(" ")))
print(solve(n, a, b)) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
s = [0] * n
for i in range(n):
s[a[i] - 1] = i
d = dict()
for i in range(n):
t = s[b[i] - 1] - i
if t < 0:
t += n
if t in d:
d[t] += 1
else:
d[t] = 1
print(max(d.values())) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
pos = [0] * (n + 1)
for i in range(n):
pos[a[i]] = i
for i in range(n):
pos[b[i]] -= i
if pos[b[i]] < 0:
pos[b[i]] += n
counter = {}
for i in range(1, n + 1):
if pos[i] in counter:
counter[pos[i]] += 1
else:
counter[pos[i]] = 1
ans = 0
for i, v in counter.items():
ans = max(ans, v)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | n = int(input())
list1 = list(map(int, input().split()))
list2 = list(map(int, input().split()))
right = [0] * (n + 1)
dicti = {}
for i in range(n):
dicti[list1[i]] = i
for i in range(n):
k = dicti[list2[i]] - i
if k > 0:
right[k] += 1
else:
right[n + k] += 1
print(max(right)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | import sys
input = lambda: sys.stdin.readline().strip()
n = int(input())
a = list(map(int, input().split()))
am = [0] * n
for i in range(n):
am[a[i] - 1] = i
b = list(map(int, input().split()))
bm = [0] * n
for i in range(n):
bm[b[i] - 1] = i
ans = 0
cm = [0] * n
for i in range(n):
if bm[i] > am[i]:
cm[n - bm[i] + am[i]] += 1
else:
cm[am[i] - bm[i]] += 1
print(max(cm)) | IMPORT ASSIGN VAR FUNC_CALL 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 BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | n = int(input())
num1 = list(map(int, input().split()))
num2 = list(map(int, input().split()))
pos = (n + 1) * [0]
dis = (n + 1) * [0]
for i in range(n):
pos[num2[i]] = i
ans = 0
for i in range(n):
x = (i - pos[num1[i]] + n) % n
dis[x] += 1
ans = max(ans, dis[x])
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER LIST NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
shift = [0] * (1 + n)
ida, idb = [0] * (1 + n), [0] * (1 + n)
for i in range(n):
ida[a[i]] = i
idb[b[i]] = i
for i in range(1, 1 + n):
shift[(ida[i] - idb[i]) % n] += 1
print(max(shift)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | n = int(input())
a = [int(s) for s in input().split()]
b = [int(s) for s in input().split()]
da = dict()
db = dict()
i = 0
for el in a:
da[el] = i
i += 1
i = 0
for el in b:
db[el] = i
i += 1
delta = dict()
for i in range(1, n + 1):
if db[i] - da[i] not in delta:
delta[db[i] - da[i]] = 1
else:
delta[db[i] - da[i]] += 1
c = 0
for key in delta:
if key - n in delta and delta[key] + delta[key - n] > c:
c = delta[key] + delta[key - n]
elif delta[key] > c:
c = delta[key]
print(c) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | n = int(input())
a = list(map(int, input().split(" ")))
b = list(map(int, input().split(" ")))
inds = {}
for x in range(n):
inds[b[x]] = x
res = [0] * n
for x in range(n):
if inds[a[x]] >= x:
required = inds[a[x]] - x
else:
required = n - (x - inds[a[x]])
res[required] += 1
print(max(res)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | from sys import stdin
n = int(stdin.readline().strip())
alist = list(map(int, stdin.readline().split()))
blist = list(map(int, stdin.readline().split()))
pos = {}
for i in range(len(blist)):
pos[blist[i]] = i
counts, ans = {}, 0
for i in range(len(alist)):
d = (pos[alist[i]] - i) % n
counts[d] = counts.get(d, 0) + 1
ans = max(ans, counts[d])
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR DICT NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a = list(zip(a, range(n)))
b = list(zip(b, range(n)))
a.sort()
b.sort()
temp = [0] * n
for i in range(n):
temp[(a[i][1] - b[i][1]) % n] += 1
print(max(temp)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
indA = [0] * n
for i in range(n):
indA[a[i] - 1] = i
indB = [0] * n
for i in range(n):
indB[b[i] - 1] = i
left = []
for i in range(n):
left.append((indA[i] - indB[i] + n) % n)
cnt = [0] * n
for i in range(n):
cnt[left[i]] += 1
print(max(cnt)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
for i in range(n):
if a[i] == b[i]:
ans += 1
arrR = [0] * (n + 1)
arrL = [0] * (n + 1)
aind = [0] * (n + 1)
bind = [0] * (n + 1)
for i in range(n):
aind[a[i]] = i + 1
bind[b[i]] = i + 1
for i in range(1, n + 1):
if bind[i] > aind[i]:
arrR[bind[i] - aind[i]] += 1
arrL[n - (bind[i] - aind[i])] += 1
else:
arrL[aind[i] - bind[i]] += 1
arrR[n - (aind[i] - bind[i])] += 1
ans1 = max(arrR)
ans2 = max(arrL)
print(max(ans, ans1, ans2)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | n = int(input())
a = list(map(int, input().strip().split(" ")))
b = list(map(int, input().strip().split(" ")))
ma = {}
mb = {}
for i in range(len(a)):
ma[a[i]] = i
mb[b[i]] = i
m = {}
for i in a:
m[i] = ma[i] - mb[i]
if m[i] < 0:
m[i] += n
freq = {}
for i in m:
freq[m[i]] = freq.get(m[i], 0) + 1
ans = 0
for i in freq:
ans = max(ans, freq[i])
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | def answer(n, a, b):
da = {a[i]: i for i in range(n)}
db = {b[i]: i for i in range(n)}
cnt = [0] * n
for k in da:
d = da[k] - db[k]
if d < 0:
d += n
cnt[d] += 1
return max(cnt)
def main():
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
print(answer(n, a, b))
return
main() | FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN EXPR FUNC_CALL VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | def f():
n = int(input())
A = [int(s) for s in input().split()]
B = [int(s) for s in input().split()]
inda = [0] * (n + 1)
indb = [0] * (n + 1)
for i in range(n):
inda[A[i]] = i
indb[B[i]] = i
d = [0] * n
for i in range(1, n + 1):
x = indb[i] - inda[i]
if x < 0:
x += n
d[x] += 1
ans = 0
for dis in d:
ans = max(ans, dis)
print(ans)
f() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
k = [0] * n
k1 = [0] * n
k2 = [0] * n
for i in range(n):
k1[a[i] - 1] = i
k2[b[i] - 1] = i
for i in range(n):
k[(k1[i] - k2[i]) % n] += 1
print(max(k)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | import sys
input = lambda: sys.stdin.readline().strip("\r\n")
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
al = [0] * n
bl = [0] * n
for i in range(n):
al[a[i] - 1] = i + 1
bl[b[i] - 1] = i + 1
ans = [0] * n
for i in range(n):
ans[(al[i] - bl[i]) % n] += 1
print(max(ans)) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | n = int(input())
ai = list(map(int, input().split()))
bi = list(map(int, input().split()))
di = [0] * n
for i in range(n):
di[ai[i] - 1] = i
ci = [((di[bi[i] - 1] - i) % n) for i in range(n)]
ci.sort()
num = 1
ans = 1
for i in range(1, n):
num *= ci[i] == ci[i - 1]
num += 1
ans = max(ans, num)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | a = input()
b = input().split(" ")
c = input().split(" ")
list1 = [i for i in b]
list2 = [i for i in c]
list3 = []
dict = {}
dict2 = {}
for i in range(int(a)):
dict2[list2[i]] = i
for i in range(int(a)):
tag = i - dict2[list1[i]]
if tag not in dict:
dict[tag] = 1
else:
dict[tag] += 1
if tag - int(a) not in dict:
dict[tag - int(a)] = 1
else:
dict[tag - int(a)] += 1
if tag + int(a) not in dict:
dict[tag + int(a)] = 1
else:
dict[tag + int(a)] += 1
print(dict[max(dict, key=dict.get)]) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | from sys import stdin
A = int(stdin.readline())
C = stdin.readline().split()
D = stdin.readline().split()
E = dict()
G = dict()
S = set()
for j in range(0, A):
E[C[j]] = j
MAX = 0
for t in range(0, A):
if E[D[t]] >= t:
p = len(S)
S.add(int(E[D[t]]) - t)
if p == len(S):
G[int(E[D[t]]) - t] += 1
else:
G[int(E[D[t]]) - t] = 1
MAX = max(MAX, G[int(E[D[t]]) - t])
else:
p = len(S)
S.add(int(E[D[t]]) + A - t)
if p == len(S):
G[int(E[D[t]]) + A - t] += 1
else:
G[int(E[D[t]]) + A - t] = 1
MAX = max(MAX, G[int(E[D[t]]) + A - t])
print(MAX) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
dicA = {i: e for e, i in enumerate(a)}
dicB = {i: e for e, i in enumerate(b)}
rdata = [0] * N
for i in range(1, N + 1):
A = dicA[i]
B = dicB[i]
if B > A:
rdata[B - A] += 1
else:
rdata[(N - (A - B)) % N] += 1
ans = max(rdata)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | from sys import stdin, stdout
n = int(input())
a = list(map(int, stdin.readline().split()))
b = list(map(int, stdin.readline().split()))
if n == 2:
print(2)
else:
aind = [(0) for i in range(n)]
bind = [(0) for i in range(n)]
for i in range(n):
aind[a[i] - 1] = i
for i in range(n):
bind[b[i] - 1] = i
cnt = [(0) for i in range(n)]
for i in range(n):
cnt[i] = aind[i] - bind[i]
d = {}
maxi = -1
for i in cnt:
if i in d:
d[i] += 1
else:
d[i] = 1
if -(n - i) in d:
d[i - n] += 1
else:
d[i - n] = 1
for i in d:
if d[i] > maxi:
maxi = d[i]
print(maxi) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | n = int(input())
a = [(0) for _ in range(n + 5)]
i = 0
for x in input().split():
a[int(x)] = i
i += 1
ans = [(0) for _ in range(2 * n + 5)]
i = 0
for x in input().split():
offset = i - a[int(x)]
ans[offset if offset > 0 else offset + n] += 1
i += 1
print(max(ans)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | from sys import *
def solve():
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
idx = [(0) for _ in range(n + 1)]
for i in range(n):
idx[a[i]] = i
offcnt = {}
for i in range(n):
offset = (idx[b[i]] - i) % n
if not offset in offcnt:
offcnt[offset] = 1
else:
offcnt[offset] += 1
ans = 0
for _, v in offcnt.items():
ans = max(ans, v)
print(ans)
solve() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | n = int(input())
a = [(int(i) - 1) for i in input().split()]
b = [(int(i) - 1) for i in input().split()]
c = list(range(n))
for i in range(n):
c[a[i]] = i
for i in range(n):
b[i] = c[b[i]]
c = [0] * n
for i in range(n):
c[(b[i] - i + n) % n] += 1
print(max(c)) | 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 BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | from sys import stdin
n = int(stdin.readline().strip())
s = list(map(int, stdin.readline().strip().split()))
s1 = list(map(int, stdin.readline().strip().split()))
ans = [(0) for i in range(n)]
pos = [(0) for i in range(n + 1)]
for i in range(n):
pos[s1[i]] = i
for i in range(n):
x = pos[s[i]]
if x <= i:
ans[i - x] += 1
else:
ans[n + i - x] += 1
print(max(ans)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
dic = {}
for i in range(n):
dic[a[i]] = i
dc = {}
dac = {}
for i in range(n):
x = dic[b[i]]
c = 0
ac = 0
if x >= i:
c = x - i
ac = n - (x - i)
else:
ac = i - x
c = n - (i - x)
try:
dc[c] += 1
except:
dc[c] = 1
try:
dac[ac] += 1
except:
dac[ac] = 1
maxx = 0
for i in dc:
maxx = max(maxx, dc[i])
for i in dac:
maxx = max(maxx, dac[i])
print(maxx) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | def solve():
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
mIndex = {}
for i in range(n):
mIndex[b[i]] = i
m = {}
for i in range(n):
i1, i2 = i, mIndex[a[i]]
steps = n - i2 + i1
if i1 > i2:
steps = i1 - i2
m[steps] = m.get(steps, 0) + 1
res = 1
for key in m.keys():
res = max(res, m[key])
print(res)
solve() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | R1 = lambda: list(map(int, input().split()))
R2 = lambda: int(input())
n, a, b = R2(), R1(), R1()
c = [0] * n
d = [0] * n
for i in range(n):
c[a[i] - 1] += i
c[b[i] - 1] -= i
for i in c:
d[i] += 1
print(max(d)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
inda = {}
for i in range(n):
inda[a[i]] = i
indb = {}
for i in range(n):
indb[b[i]] = i
shift = [0] * n
for i in range(n):
if a[i] in indb:
ind = indb[a[i]]
if ind < i:
val = n - (i + 1) + ind + 1
else:
val = ind - i
shift[val] += 1
print(max(shift)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | def solve(n, seqA, seqB):
table = {x: index for index, x in enumerate(seqA, 0)}
diff = {}
for indexB in range(n):
res = table[seqB[indexB]] - indexB
if res < 0:
if res * -1 > n // 2:
res = n - res * -1
elif res > n // 2:
res = n - res
res *= -1
if diff.get(res) == None:
diff[res] = 1
else:
diff[res] += 1
return max(diff.values())
n = int(input())
seqA = input().split()
seqB = input().split()
print(solve(n, seqA, seqB)) | FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.
Note that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \ldots, a_n$, in which every number from $1$ to $n$ appears exactly once.
The message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$
His two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times.
A single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$ simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
-----Input-----
The first line of the input contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$Β β the size of the arrays.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le n)$ β the elements of the first permutation.
The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le n)$ β the elements of the second permutation.
-----Output-----
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
-----Examples-----
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
-----Note-----
For the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\{1, 2, 3, 4, 5\}$ and $\{1, 2, 3, 4, 5\}$.
For the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.
For the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\{1, 3, 2, 4\}$ and $\{2, 3, 1, 4\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$. | import sys
input = sys.stdin.readline
def inp():
return int(input())
def inlist():
return list(map(int, input().split()))
def instr():
s = input()
return list(s[: len(s) - 1])
def invr():
return map(int, input().split())
n = inp()
a = []
pos = [None] * n
shift = [0] * n
a = inlist()
for i in range(0, n):
pos[a[i] - 1] = i
b = inlist()
for i in range(0, n):
cur = pos[b[i] - 1] - i
if cur < 0:
cur = cur + n
shift[cur] = shift[cur] + 1
max = -1
for i in shift:
if max < i:
max = i
print(max) | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.