description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
input = sys.stdin.readline
q, x = map(int, input().split())
seg_el = 1 << (x + 1).bit_length()
SEG = [0] * (2 * seg_el)
def add1(n, seg_el):
i = n + seg_el
SEG[i] += 1
i >>= 1
while i != 0:
SEG[i] = min(SEG[i * 2], SEG[i * 2 + 1])
i >>= 1
def getvalues(l, r):
L = l + seg_el
R = r + seg_el
ANS = 1 << 30
while L < R:
if L & 1:
ANS = min(ANS, SEG[L])
L += 1
if R & 1:
R -= 1
ANS = min(ANS, SEG[R])
L >>= 1
R >>= 1
return ANS
for qu in range(q):
t = int(input())
add1(t % x, seg_el)
MIN = getvalues(0, x)
OK = 0
NG = x
while NG - OK > 1:
mid = (OK + NG) // 2
if getvalues(0, mid) > MIN:
OK = mid
else:
NG = mid
print(MIN * x + OK) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | def f(x, mex, mod_count):
while mod_count.get(mex % x, False):
mod_count[mex % x] -= 1
mex += 1
return mex
q, x = list(map(int, input().split()))
mod_count = {}
mex = 0
result = ""
for _ in range(q):
if x != 1:
mod = int(input()) % x
mod_count[mod] = mod_count.get(mod, 0) + 1
mex = f(x, mex, mod_count)
else:
mex = _ + 1
result += str(mex)
result += "\n"
print(result) | FUNC_DEF WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
input = sys.stdin.readline
q, x = list(map(int, input().split()))
arr = [(0) for i in range(x)]
cur = 0
for i in range(q):
n = int(input())
temp = n % x
arr[temp] += 1
while arr[cur % x] > 0:
arr[cur % x] -= 1
cur += 1
sys.stdout.write(str(cur) + "\n") | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | q, x = map(int, input().split())
mx = 4 * 10**5 + 1
t_reszt = [0] * mx
t_wyn = [0] * mx
t_out = []
curr_ind = 0
out_curr_ind = 0
for y in range(q):
curr_val = int(input())
m = curr_val % x
ii = t_reszt[m] * x + m
if ii < mx - 1:
t_wyn[ii] = 1
t_reszt[m] += 1
curr_ind = out_curr_ind
for z in range(curr_ind, len(t_wyn), +1):
if t_wyn[z] == 0:
t_out.append(z)
out_curr_ind = z
break
print("\n".join(map(str, t_out))) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
def read2():
reader = (tuple(map(int, line.split())) for line in sys.stdin)
_, base = next(reader)
mex = 0
pack = [(0) for _ in range(base)]
cmds = list(reader)
for cmd in cmds:
value = cmd[0] % base
pack[value] += 1
while pack[mex % base]:
pack[mex % base] -= 1
mex += 1
print(mex)
read2() | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | from sys import stdin, stdout
class Solve:
q = x = ans = n = 0
d = {}
ANS = []
def bsearch(self, l, r, rem, ret):
if l > r:
return ret
mid = (l + r) // 2
number = rem + self.x * mid
if number not in self.d:
ret = number
return self.bsearch(l, mid - 1, rem, ret)
else:
return self.bsearch(mid + 1, r, rem, ret)
def solve(self):
self.q, self.x = map(int, stdin.readline().split())
l, r = 0, 1000000000 // self.x
for _ in range(self.q):
self.n = int(stdin.readline())
ret = self.bsearch(l, r, self.n % self.x, self.n)
self.d[ret] = 1
while self.ans in self.d:
self.ans += 1
self.ANS.append(str(self.ans))
stdout.write("\n".join(self.ANS))
return 0
def main():
s = Solve()
s.solve()
main() | CLASS_DEF ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR LIST FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
inp = sys.stdin.readline
input = lambda: inp().strip()
flush = sys.stdout.flush
def iin():
return int(input())
def lin():
return list(map(int, input().split()))
def main():
q, x = lin()
mx = 4 * 10**5 + 1 + x
ch = 0
fact = [[] for i in range(x)]
for _ in range(q):
y = iin()
fact[y % x].append(y)
while ch < mx:
if fact[ch % x]:
fact[ch % x].pop()
ch += 1
else:
break
print(ch)
main() | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR 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 VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR WHILE VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | q, x = map(int, input().split())
y = [0] * x
ans = list()
zero_ind = set()
zero = [1] * x
d = 1
for kek in range(q):
a = int(input())
a %= x
y[a] += 1
if len(zero) >= x - a:
zero[x - a - 1] = 0
while len(zero) > 0 and zero[-1] == 0:
zero.pop()
if len(zero) == 0:
d += 1
zero = [1] * x
for i in range(x):
y[i] -= 1
if y[i] != 0:
zero[x - i - 1] = 0
while len(zero) > 0 and zero[-1] == 0:
zero.pop()
ans.append(d * x - len(zero))
print(*ans, sep="\n") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__
class SegmTree:
def __init__(self, size):
N = 1
while N < size:
N <<= 1
self.N = N
self.tree = [(float("inf"), -1) for _ in range(2 * N)]
for i in range(size):
self.tree[i + N] = 0, i
self.build()
def build(self):
for i in range(self.N - 1, 0, -1):
self.tree[i] = min(self.tree[i << 1], self.tree[i << 1 | 1])
def inc(self, i):
tree = self.tree
i += self.N
occ, rem = tree[i]
tree[i] = occ + 1, rem
while i > 1:
tree[i >> 1] = min(tree[i], tree[i ^ 1])
i >>= 1
def query_range(self, l, r):
tree = self.tree
l += self.N
r += self.N
result = float("inf"), -1
while l < r:
if l & 1:
result = min(result, tree[l])
l += 1
if r & 1:
r -= 1
result = min(result, tree[r])
l >>= 1
r >>= 1
return result
q, x = map(int, input().split())
st = SegmTree(x)
for _ in range(q):
val = int(input())
st.inc(val % x)
minOcc, rem = st.tree[1]
mex = minOcc * x + rem
print(mex) | IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
def get():
global x, now_min
res = [0, 1000000000]
token = 0
for i in range(x):
c = list_cycle[i]
if c == now_min:
res = [i, c]
token = 1
break
if c < res[1]:
res = [i, c]
if not token:
now_min += 1
return res[1] * x + res[0]
q, x = map(int, sys.stdin.readline().split())
list_cycle = [(0) for _ in range(x)]
now_min, pointer = 0, 0
for i in range(q):
now = int(sys.stdin.readline())
list_cycle[now % x] += 1
while list_cycle[pointer % x] > pointer // x:
pointer += 1
print(pointer) | IMPORT FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR LIST VAR VAR IF VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
input = sys.stdin.buffer.readline
def main():
Q, X = map(int, input().split())
A = [0] * X
def segfunc(x, y):
if x[0] < y[0]:
return x
elif x[0] == y[0]:
if x[1] <= y[1]:
return x
else:
return y
else:
return y
def init(init_val):
for i in range(X):
seg[i + num - 1] = [init_val[i], i]
for i in range(num - 2, -1, -1):
seg[i] = segfunc(seg[2 * i + 1], seg[2 * i + 2])
def update(k, x):
k += num - 1
seg[k][0] += x
while k:
k = (k - 1) // 2
seg[k] = segfunc(seg[2 * k + 1], seg[2 * k + 2])
def query(p, q):
if q <= p:
return ide_ele
p += num - 1
q += num - 2
res = [ide_ele, X]
while q - p > 1:
if p & 1 == 0:
res = segfunc(res, seg[p])
if q & 1 == 1:
res = segfunc(res, seg[q])
q -= 1
p = p // 2
q = (q - 1) // 2
if p == q:
res = segfunc(res, seg[p])
else:
res = segfunc(segfunc(res, seg[p]), seg[q])
return res
ide_ele = 10**18
num = 2 ** (X - 1).bit_length()
seg = [[ide_ele, X] for _ in range(2 * num)]
init(A)
for i in range(Q):
y = int(input())
y %= X
update(y, 1)
c, idx = query(0, X)
print(c * X + idx)
main() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR RETURN VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER LIST VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR WHILE VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR VAR WHILE BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR LIST VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | q, x = map(int, input().split())
a = [0] * (x + 1)
for i in range(x):
a[i] = 0
mex, base = 0, 0
ans = ""
while q > 0:
q -= 1
val = int(input())
a[val % x] += 1
while a[mex] > 0:
mex += 1
if mex == x:
base += 1
mex = 0
for i in range(x):
a[i] -= 1
ans += str(base * x + mex) + "\n"
print(ans) | ASSIGN VAR VAR FUNC_CALL 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 NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR STRING WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
input = sys.stdin.readline
q, x = map(int, input().split())
ar = [0] * x
ct = 0
for i in range(q):
y = int(input())
if y == ct:
ct += 1
else:
ar[y % x] += 1
while True:
if ar[ct % x] > 0:
ar[ct % x] -= 1
ct += 1
else:
break
print(ct) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER WHILE NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | from sys import stdin, stdout
res = 0
d = [(0) for i in range(400005)]
q, x = map(int, stdin.readline().split())
for _ in range(q):
n = int(stdin.readline())
d[n % x] += 1
while d[res % x] > res / x:
res += 1
stdout.write("%s" % res + "\n") | ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR STRING |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
input = sys.stdin.buffer.readline
def solution():
cnt = [0] * (4 * 10**5 + 2)
q, x = map(int, input().split())
i = 0
s = set()
for _ in range(q):
n = int(input())
z = n % x
cnt[z] += 1
while cnt[i % x]:
cnt[i % x] -= 1
i += 1
print(i)
solution() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | def ii():
a = int(input())
return a
def ai():
a = list(map(int, input().split()))
return a
def mi():
a = map(int, input().split())
return a
q, x = mi()
mex = 0
freq = [0] * x
ans = []
for i in range(q):
freq[ii() % x] += 1
while freq[mex % x] > 0:
freq[mex % x] -= 1
mex += 1
ans.append(mex)
print("\n".join(map(str, ans))) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
input = sys.stdin.readline
q, x = map(int, input().split())
d = {}
for i in range(x):
d[i] = 0
mex = 0
done = {}
for i in range(q):
n = int(input())
n = n % x
d[n] += 1
n = n + (d[n] - 1) * x
done[n] = 1
while mex in done:
mex += 1
print(mex) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
q, x = map(int, sys.stdin.readline().strip().split())
mxN = 4 * 10**5 + 1
mex = 0
a = {i: (0) for i in range(x)}
need_to_mex = 0
mn = 0
needCheck = False
for query in range(q):
n = int(sys.stdin.readline().strip()) % x
a[n] += 1
if n == need_to_mex:
if needCheck:
mn = mxN
mx = -1
pos_mn = 0
for k in a:
if a[k] < mn:
mn = a[k]
pos_mn = k
need_to_mex = pos_mn
mex = mn * x + pos_mn
if pos_mn > 0 and mn < a[pos_mn - 1] or pos_mn < x and mn < a[pos_mn + 1]:
needCheck = True
else:
needCheck = False
else:
mex += 1
need_to_mex += 1
if need_to_mex >= x:
need_to_mex = 0
else:
needCheck = True
print(mex) | IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | from sys import stdin
def solve():
q, x = map(int, stdin.readline().split())
d = {}
mx, mex = 0, 0
for _ in range(q):
mx += 1
n = int(stdin.readline())
d.setdefault(n % x, 0)
d[n % x] += 1
while mex < mx:
if mex % x in d and d[mex % x] >= 1:
d[mex % x] -= 1
mex += 1
else:
break
print(mex)
solve() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
input = sys.stdin.buffer.readline
q, x = map(int, input().split())
a = [(0) for i in range(x)]
b = []
for i in range(q):
n = int(input())
a[n % x] += 1
b.append(n)
dict = {}
for i in range(x):
dict[i] = a[i]
s = []
store = 0
mini = 9999999999
for i in range(x):
if a[i] < mini:
mini = a[i]
store = i
y = x * a[store] + store
pre_i = store
pre_j = a[store]
for i in range(q - 1, 0, -1):
dict[b[i] % x] -= 1
if dict[b[i] % x] == pre_j and b[i] % x <= pre_i:
pre_i = b[i] % x
pre_j = dict[b[i] % x]
elif dict[b[i] % x] < pre_j:
pre_i = b[i] % x
pre_j = dict[b[i] % x]
s.append(pre_j * x + pre_i)
for i in range(q - 2, -1, -1):
print(s[i])
print(y) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
class MinHeap:
class Node:
def __init__(self, i, key, priority):
self.i = i
self.key = key
self.priority = priority
def __repr__(self):
return "Node[i:{} k:{} p:{}]".format(self.i, self.key, self.priority)
def sift_up(self, i):
while i > 0:
p = (i - 1) // 2
if self.heap[i].priority < self.heap[p].priority:
self.heap[i].i, self.heap[p].i = p, i
self.heap[i], self.heap[p] = self.heap[p], self.heap[i]
i = p
else:
break
def sift_down(self, i):
while 2 * i + 1 < len(self.heap):
m = i
l = 2 * i + 1
if self.heap[l].priority < self.heap[m].priority:
m = l
r = l + 1
if r < len(self.heap) and self.heap[r].priority < self.heap[m].priority:
m = r
if m != i:
self.heap[i].i, self.heap[m].i = m, i
self.heap[i], self.heap[m] = self.heap[m], self.heap[i]
i = m
else:
break
def lowest_priority(self):
return self.heap[0].priority
def lowest_priority_key(self):
return self.heap[0].key
def get_priority(self, key):
return self.mapping[key].priority
def set_priority(self, key, new_priority):
node = self.mapping[key]
if new_priority < node.priority:
node.priority = new_priority
self.sift_up(node.i)
elif new_priority > node.priority:
node.priority = new_priority
self.sift_down(node.i)
def add_priority(self, key, priority):
node = self.Node(len(self.heap), key, priority)
self.mapping[key] = node
self.heap.append(node)
self.sift_up(len(self.heap) - 1)
def remove_priority(self, key):
node = self.mapping[key]
if node.i < len(self.heap) - 1:
replacement_node = self.heap.pop()
self.heap[node.i] = replacement_node
replacement_node.i = node.i
if replacement_node.priority < node.priority:
self.sift_up(node.i)
elif replacement_node.priority > node.priority:
self.sift_down(node.i)
else:
self.heap.pop()
del self.mapping[key]
def __init__(self, priorities={}):
if priorities is not None:
self.mapping = {}
self.heap = []
for key, priority in priorities.items():
node = self.Node(len(self.heap), key, priority)
self.mapping[key] = node
self.heap.append(node)
for i in range(len(self.heap) - 1, -1, -1):
self.sift_up(i)
def __len__(self):
return len(self.heap)
def __repr__(self):
return repr(self.heap)
q, x = (int(v) for v in sys.stdin.readline().split())
H = MinHeap({m: (0, m) for m in range(x)})
for i in range(q):
n = int(sys.stdin.readline())
m = n % x
mis_n, mis_m = H.get_priority(m)
H.set_priority(m, (mis_n + 1, mis_m))
mis_n, mis_m = H.lowest_priority()
print(mis_n * x + mis_m) | IMPORT CLASS_DEF CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL STRING VAR VAR VAR FUNC_DEF WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_DEF WHILE BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN VAR NUMBER FUNC_DEF RETURN VAR NUMBER FUNC_DEF RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF DICT IF VAR NONE ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
def main():
import sys
q, x = map(int, input().split())
s = [0] * x
m = 0
for i in range(q):
if x == 1:
print(i + 1)
else:
c = int(sys.stdin.readline())
s[c % x] += 1
while s[m % x] > 0:
s[m % x] -= 1
m = m + 1
sys.stdout.write("\n" + str(m))
main() | IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
input = sys.stdin.readline
q, x = map(int, input().split())
d = {}
ans = 0
for _ in range(q):
y = int(input())
c = y % x
if c in d:
d[c] += 1
else:
d[c] = 1
while True:
v = ans % x
if v in d:
ans += 1
d[v] -= 1
if d[v] == 0:
del d[v]
else:
break
sys.stdout.write(str(ans) + "\n") | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
input = sys.stdin.readline
def segtree(i, l, r, arr):
if l == r:
tree[i] = arr[l]
return tree[i]
m = (l + r) // 2
tree[i] = min(segtree(2 * i + 1, l, m, arr), segtree(2 * i + 2, m + 1, r, arr))
return tree[i]
def update(i, l, r, num, ind):
if ind < l or ind > r:
return
elif l == r:
tree[i] = num
return
m = (l + r) // 2
update(2 * i + 1, l, m, num, ind)
update(2 * i + 2, m + 1, r, num, ind)
tree[i] = min(tree[2 * i + 1], tree[2 * i + 2])
return
def search(i, l, r, num):
if l == r:
return l
m = (l + r) // 2
if tree[2 * i + 1] == num:
return search(2 * i + 1, l, m, num)
else:
return search(2 * i + 2, m + 1, r, num)
q, x = map(int, input().split())
arr = [0] * x
i = 0
while 2**i < x:
i = i + 1
tree = [0] * (2 ** (i + 1) - 1)
segtree(0, 0, x - 1, arr)
for _ in range(q):
y = int(input())
arr[y % x] += 1
update(0, 0, x - 1, arr[y % x], y % x)
i = search(0, 0, x - 1, tree[0])
if tree[0] == 0:
print(i)
elif i == 0:
sys.stdout.write(str(x * tree[0]) + "\n")
else:
sys.stdout.write(str(x * tree[0] + i) + "\n") | IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN IF VAR VAR ASSIGN VAR VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR STRING |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | q, x = map(int, input().split())
sachin = []
count1 = 0
count2 = 0
arr = []
out = []
for i in range(x):
arr.append(0)
for i in range(q):
val = int(input())
arr[val % x] += 1
while arr[count2 % x] > count1:
count2 += 1
if count2 % x == 0:
count1 += 1
out.append(count2)
print("\n".join(map(str, out))) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
input = sys.stdin.readline
m, maxn = {}, int(400000.0)
t, x = list(map(int, input().split()))
Mex = 0
for _ in range(t):
n = int(input())
ans = int((Mex - n) / x)
l, r = ans, int((maxn - n) / x)
mid = l
while l <= r:
mid = int((l + r) / 2)
if n + mid * x in m:
l = mid + 1
else:
r = mid - 1
m[n + l * x] = "Lin"
while Mex in m:
Mex += 1
print(Mex) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR DICT FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR STRING WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | n, x = map(int, input().split())
mlook = 0
available = [0] * x
mex = -1
ans = [0] * n
for _ in range(n):
num = int(input())
if num == mex + 1 or mlook == num % x:
mex += 1
mlook = (mlook + 1) % x
while available[mlook] > 0:
available[mlook] -= 1
mex += 1
mlook = (mlook + 1) % x
else:
available[num % x] += 1
ans[_] = mex + 1
print("\n".join(str(ans[k]) for k in range(n))) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
input = sys.stdin.buffer.readline
q, x = [int(x) for x in input().split()]
nIsCovered = [(False) for _ in range(4 * 10**5 + 1)]
multipleNotCovered = [(0) for _ in range(x)]
currMEX = 0
for _ in range(q):
n = int(input())
m = n % x
z = multipleNotCovered[m]
if m + x * z < len(nIsCovered):
nIsCovered[m + x * z] = True
multipleNotCovered[m] += 1
while nIsCovered[currMEX] == True:
currMEX += 1
print(currMEX) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | from sys import stdin, stdout
def main():
arr = list(map(int, stdin.readline().split()))
N = arr[0]
X = arr[1]
counter = dict()
for i in range(X):
counter[i] = 0
maxer = 0
for i in range(N):
new_num = int(stdin.readline())
counter[new_num % X] += 1
while counter[maxer % X] > 0:
counter[maxer % X] -= 1
maxer += 1
stdout.write(str(maxer) + "\n")
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
import time
input = sys.stdin.readline
start_time = time.time()
MAX_N = int(400000.0 + 50)
q, x = map(int, input().split())
ans = 0
visited = [(0) for _ in range(x)]
for _ in range(q):
y = int(input())
visited[y % x] += 1
while visited[ans % x] != 0:
visited[ans % x] -= 1
ans += 1
print(ans) | IMPORT IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | ls = list(map(int, input().split()))
arri = []
x = ls[1]
nls = [0] * x
mexi = 0
for i in range(ls[0]):
nls[int(input()) % x] += 1
if x == 1:
mexi = i + 1
else:
while nls[mexi % x]:
nls[mexi % x] -= 1
mexi += 1
arri.append(mexi)
print("\n".join(map(str, arri))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | q, x = map(int, input().split(" "))
my_list = []
for i in range(q):
my_list.append(int(input()))
count = [0] * x
mex = 0
for a in my_list:
count[a % x] += 1
while True:
if count[mex % x] > 0:
count[mex % x] -= 1
mex += 1
else:
break
print(mex) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR NUMBER WHILE NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
input = sys.stdin.readline
q, x = map(int, input().split())
LIST = [0] * x
mex = 0
for qu in range(q):
LIST[int(input()) % x] += 1
while LIST[mex % x] > mex // x:
mex += 1
print(mex) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | [n, x] = list(map(int, input().split(" ")))
arr = [(0) for i in range(x)]
index = 0
ans = []
if 0:
none = 0
else:
for i in range(n):
k = int(input())
arr[k % x] += 1
while arr[index % x] != 0:
arr[index % x] -= 1
index += 1
ans.append(index)
for i in ans:
print(i) | ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST IF NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
q, x = [int(i) for i in input().split()]
d = [0] * x
mx = -1
for i in range(q):
qqq = int(sys.stdin.readline())
d[qqq % x] += 1
while True:
mm = (mx + 1) % x
if d[mm] >= (mx + 1) // x + 1:
mx += 1
else:
break
print(mx + 1) | IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | def solve(x, queries):
surplus = [0] * x
mex = 0
for q in queries:
surplus[q % x] += 1
while surplus[mex % x] > 0:
surplus[mex % x] -= 1
mex += 1
yield mex
q, x = map(int, input().split())
for result in solve(x, [int(input()) for i in range(q)]):
print(result) | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
def main():
input = sys.stdin.readline
q, x = [int(i) for i in input().split()]
A = [(0) for i in range(x)]
m = 0
c = 0
ans = []
for a in range(q):
n = int(input())
A[n % x] += 1
while A[c] >= m // x + 1:
c = (c + 1) % x
m += 1
ans.append(str(m))
print("\n".join(ans))
def __starting_point():
main()
__starting_point() | IMPORT FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER WHILE VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) β the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query β for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | MAX_N = 400005
query, x = map(int, input().strip().split())
count = [(0) for _ in range(MAX_N)]
mex = 0
ans = ""
for i in range(query):
y = int(input().strip())
count[y % x] += 1
while True:
if count[mex % x] > 0:
count[mex % x] -= 1
mex += 1
else:
break
ans += str(mex) + "\n"
print(ans) | ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER WHILE NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | from sys import stdin, stdout
def three_sequences(n, a_a, q_a):
res_a = []
dif_a = [0] * n
K = 0
for i in range(1, n):
K += max(0, a_a[i] - a_a[i - 1])
dif_a[i] = a_a[i] - a_a[i - 1]
res_a.append(calc(K, a_a[0]))
for q in q_a:
l, r, x = q
if l > 1:
K -= max(0, dif_a[l - 1])
dif_a[l - 1] += x
K += max(0, dif_a[l - 1])
if r < n:
K -= max(0, dif_a[r])
dif_a[r] -= x
K += max(0, dif_a[r])
if l == 1:
a_a[0] += x
res_a.append(calc(K, a_a[0]))
return res_a
def calc(K, a0):
b0 = (a0 - K) // 2
bn = b0 + K
c0 = a_a[0] - b0
return max(bn, c0)
n = int(stdin.readline())
a_a = list(map(int, stdin.readline().split()))
q = int(stdin.readline())
q_a = []
for _ in range(q):
l, r, x = map(int, stdin.readline().split())
q_a.append([l, r, x])
res_a = three_sequences(n, a_a, q_a)
for res in res_a:
stdout.write(str(res) + "\n") | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = [0] * n
c = [0] * n
difn = 0
diff = [0] * n
for i in range(1, n):
if a[i] - a[i - 1] > 0:
difn += a[i] - a[i - 1]
diff[i] = a[i] - a[i - 1]
bo = (a[0] - difn) // 2
ans = max(bo + difn, a[0] - bo)
print(ans)
td = difn
for i in range(q):
l, r, x = map(int, input().split())
if l != 1:
if diff[l - 1] + x > 0:
td -= max(0, diff[l - 1])
diff[l - 1] = diff[l - 1] + x
td += diff[l - 1]
else:
td = td - max(0, diff[l - 1])
diff[l - 1] = diff[l - 1] + x
elif l == 1:
a[0] += x
if r != n:
if diff[r] - x > 0:
td -= max(0, diff[r])
diff[r] = diff[r] - x
td += diff[r]
else:
td = td - max(0, diff[r])
diff[r] = diff[r] - x
bo = (a[0] - td) // 2
ans = max(bo + td, a[0] - bo)
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 ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER VAR IF VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
diff = [(0) for i in range(n)]
for i in range(1, n):
diff[i] = a[i] - a[i - 1]
S = 0
for i in range(1, n):
if diff[i] >= 0:
S += diff[i]
first = a[0]
ans = []
res = S + first
ans.append((res + 1) // 2)
Q = int(input())
for _ in range(Q):
l, r, x = map(int, input().split())
l = l - 1
r = r - 1
if l == 0:
first += x
else:
if diff[l] >= 0:
S -= diff[l]
diff[l] += x
if diff[l] >= 0:
S += diff[l]
if r + 1 < n:
if diff[r + 1] >= 0:
S -= diff[r + 1]
diff[r + 1] -= x
if diff[r + 1] >= 0:
S += diff[r + 1]
res = S + first
ans.append((res + 1) // 2)
for a in ans:
print(a) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR IF BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | n = int(input())
a = list(map(int, input().split()))
pre_delta = [0] * n
up_delta = 0
for i in range(1, n):
pre_delta[i] = a[i] - a[i - 1]
if pre_delta[i] > 0:
up_delta += pre_delta[i]
ans = (up_delta + a[0] + 1) // 2
print(ans)
q = int(input())
for _ in range(q):
l, r, x = map(int, input().split())
if l == 1:
a[0] += x
else:
if pre_delta[l - 1] > 0:
up_delta -= pre_delta[l - 1]
pre_delta[l - 1] += x
if pre_delta[l - 1] > 0:
up_delta += pre_delta[l - 1]
if r < n:
if pre_delta[r] > 0:
up_delta -= pre_delta[r]
pre_delta[r] -= x
if pre_delta[r] > 0:
up_delta += pre_delta[r]
ans = (up_delta + a[0] + 1) // 2
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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | import sys
def calc(a0, db, dc):
b0 = (a0 - db) // 2
c0 = a0 - b0
c1 = (a0 - dc) // 2
b1 = a0 - c1
return min(max(b0 + db, c0), max(b1 + db, c1))
def main():
n = int(input())
A = [int(a) for a in input().split()]
D = [(A[i + 1] - A[i]) for i in range(n - 1)]
a0 = A[0]
db = sum(filter(lambda d: d > 0, D))
dc = sum(filter(lambda d: d < 0, D))
q = int(input())
ans = [calc(a0, db, dc)]
for i in range(q):
l, r, x = map(int, input().split())
if l > 1:
if D[l - 2] < 0:
dc -= D[l - 2]
else:
db -= D[l - 2]
D[l - 2] += x
if D[l - 2] < 0:
dc += D[l - 2]
else:
db += D[l - 2]
if r < n:
if D[r - 1] < 0:
dc -= D[r - 1]
else:
db -= D[r - 1]
D[r - 1] -= x
if D[r - 1] < 0:
dc += D[r - 1]
else:
db += D[r - 1]
if l == 1:
a0 += x
ans.append(calc(a0, db, dc))
print("\n".join(map(str, ans)))
main() | IMPORT FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR 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 BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | import sys
sys.setrecursionlimit(10**5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.readline())
def MI():
return map(int, sys.stdin.readline().split())
def LI():
return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def SI():
return sys.stdin.readline()[:-1]
n = II()
aa = LI()
dd = [0] * n
s = 0
for i in range(n - 1):
dd[i + 1] = aa[i + 1] - aa[i]
for d in dd:
if d > 0:
s += d
b0 = (aa[0] - s) // 2
print(max(b0 + s, aa[0] - b0))
def add(i, a, s):
if i == 0:
aa[0] += a
return s
if dd[i] > 0:
s -= dd[i]
dd[i] += a
if dd[i] > 0:
s += dd[i]
return s
for _ in range(II()):
l, r, x = MI()
l -= 1
s = add(l, x, s)
if r < n:
s = add(r, -x, s)
b0 = (aa[0] - s) // 2
print(max(b0 + s, aa[0] - b0)) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR RETURN VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | import sys
inp = sys.stdin.buffer.readline
inar = lambda: list(map(int, inp().split()))
inin = lambda: int(inp())
inst = lambda: inp().decode().rstrip("\n\r")
_T_ = 1
for _t_ in range(_T_):
n = inin()
a = inar()
d = [(0) for i in range(n - 1)]
k = 0
for i in range(1, n):
d[i - 1] += a[i] - a[i - 1]
k += max(0, a[i] - a[i - 1])
x = (a[0] + k + 1) // 2
print(x)
q = inin()
for _ in range(q):
l, r, c = inar()
l -= 1
r -= 1
if l == 0:
a[0] += c
else:
if d[l - 1] >= 0:
k -= d[l - 1]
d[l - 1] += c
if d[l - 1] >= 0:
k += d[l - 1]
if r == n - 1:
pass
else:
if d[r] >= 0:
k -= d[r]
d[r] -= c
if d[r] >= 0:
k += d[r]
x = (a[0] + k + 1) // 2
print(x) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int, input().split()))
PLUS = [0] * (n - 1)
for i in range(n - 1):
PLUS[i] = A[i + 1] - A[i]
S = 0
for p in PLUS:
if p > 0:
S += p
print((S + A[0] + 1) // 2)
Q = int(input())
for queries in range(Q):
l, r, x = map(int, input().split())
if l >= 2:
ptemp = PLUS[l - 2]
PLUS[l - 2] += x
if x > 0:
if PLUS[l - 2] > 0:
S += PLUS[l - 2] - max(0, ptemp)
elif ptemp > 0:
S -= ptemp - max(0, PLUS[l - 2])
else:
A[0] += x
if r < n:
ptemp = PLUS[r - 1]
PLUS[r - 1] -= x
if x < 0:
if PLUS[r - 1] > 0:
S += PLUS[r - 1] - max(0, ptemp)
elif ptemp > 0:
S -= ptemp - max(0, PLUS[r - 1])
print((S + A[0] + 1) // 2) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | input = __import__("sys").stdin.readline
def query():
return (ans + s[0] + 1) // 2
n = int(input())
s = list(map(int, input().split()))
ans = 0
diffs = [0] * (n - 1)
for i in range(n - 1):
diffs[i] = s[i + 1] - s[i]
if diffs[i] > 0:
ans += diffs[i]
print(query())
for _ in range(int(input())):
l, r, x = map(int, input().split())
l -= 2
r -= 1
if l < 0:
s[0] += x
if l >= 0:
if diffs[l] > 0:
ans -= diffs[l]
diffs[l] += x
if diffs[l] > 0:
ans += diffs[l]
if r < n - 1:
if diffs[r] > 0:
ans -= diffs[r]
diffs[r] -= x
if diffs[r] > 0:
ans += diffs[r]
print(query()) | ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR IF VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
start = a[0]
diffs = []
for i in range(n - 1):
diffs.append(a[i + 1] - a[i])
pos = 0
for v in diffs:
if v > 0:
pos += v
final = []
def out(ans):
final.append((ans + 1) // 2)
out(pos + start)
q = int(input())
for _ in range(q):
l, r, x = map(int, input().split())
if l > 1:
if diffs[l - 2] >= 0:
pos -= diffs[l - 2]
diffs[l - 2] += x
if diffs[l - 2] >= 0:
pos += diffs[l - 2]
else:
start += x
if r < n:
if diffs[r - 1] >= 0:
pos -= diffs[r - 1]
diffs[r - 1] -= x
if diffs[r - 1] >= 0:
pos += diffs[r - 1]
out(pos + start)
print("\n".join(map(str, final))) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | import sys
class Fastio:
def __init__(self):
self.ibuf = b""
self.pil = 0
self.pir = 0
self.sz = 1 << 17
def load(self):
self.ibuf = self.ibuf[self.pil :]
self.ibuf += sys.stdin.buffer.read(self.sz)
self.pil = 0
self.pir = len(self.ibuf)
def fastin(self):
if self.pir - self.pil < 32:
self.load()
minus = 0
x = 0
while self.ibuf[self.pil] < ord("-"):
self.pil += 1
if self.ibuf[self.pil] == ord("-"):
minus = 1
self.pil += 1
while True:
if self.ibuf[self.pil] < ord("0"):
break
x = x * 10 + int(self.ibuf[self.pil] & 15)
self.pil += 1
if minus:
x = -x
return x
fastio = Fastio()
fastin = fastio.fastin
N = fastin()
a = [0] * N
for i in range(N):
a[i] = fastin()
for i in range(N - 1, 0, -1):
a[i] -= a[i - 1]
p = 0
for i in range(1, N):
p += max(0, a[i])
print((a[0] + p + 1) // 2)
Q = fastin()
for _ in range(Q):
l, r, x = fastin(), fastin(), fastin()
l -= 1
if l != 0:
p -= max(a[l], 0)
a[l] += x
if l != 0:
p += max(a[l], 0)
if r != N:
p -= max(a[r], 0)
a[r] -= x
p += max(a[r], 0)
print((a[0] + p + 1) // 2) | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR UNKNOWN ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR NUMBER WHILE NUMBER IF VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | import sys
Case = 1
def main():
n = inin()
a = inar()
d = [(0) for i in range(n - 1)]
k = 0
for i in range(1, n):
d[i - 1] += a[i] - a[i - 1]
k += max(0, a[i] - a[i - 1])
x = (a[0] + k + 1) // 2
pr(x)
q = inin()
for _ in range(q):
l, r, c = inar()
l -= 1
r -= 1
if l == 0:
a[0] += c
else:
if d[l - 1] >= 0:
k -= d[l - 1]
d[l - 1] += c
if d[l - 1] >= 0:
k += d[l - 1]
if r == n - 1:
pass
else:
if d[r] >= 0:
k -= d[r]
d[r] -= c
if d[r] >= 0:
k += d[r]
x = (a[0] + k + 1) // 2
pr(x)
inp = sys.stdin.buffer.readline
inar = lambda: list(map(int, inp().split()))
inin = lambda: int(inp())
inst = lambda: inp().decode().strip()
wrt = sys.stdout.write
pr = lambda *args, end="\n": wrt(" ".join([str(x) for x in args]) + end)
enum = enumerate
inf = float("inf")
for __Case__ in range(Case):
main() | IMPORT ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR STRING FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int, input().split()))
dif = [(b - a) for a, b in zip([0] + A, A)]
K = sum(d for i, d in enumerate(dif) if i == 0 or d > 0)
print((K + 1) // 2)
def add(x, v):
global K
if x == 0 or dif[x] > 0:
K -= dif[x]
dif[x] += v
if x == 0 or dif[x] > 0:
K += dif[x]
for _ in range(int(input())):
l, r, x = map(int, input().split())
l -= 1
add(l, x)
if r < n:
add(r, -x)
print((K + 1) // 2) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP LIST NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | import sys
n = int(sys.stdin.readline().strip())
a = list(map(int, sys.stdin.readline().strip().split()))
d = []
b = 0
c = 0
F = a[0]
L = a[-1]
for i in range(1, n):
x = a[i] - a[i - 1]
if x >= 0:
b = b + x
else:
c = c - x
d.append(x)
Q = int(sys.stdin.readline().strip())
for q in range(0, Q + 1):
if q > 0:
l, r, x = list(map(int, sys.stdin.readline().strip().split()))
else:
l, r, x = 1, 1, 0
if x != 0 and l > 1:
if x >= 0 and d[l - 2] >= 0:
b = b + x
elif x >= 0 and d[l - 2] < 0:
b = b + x - min(-d[l - 2], x)
c = c - min(-d[l - 2], x)
elif x < 0 and d[l - 2] >= 0:
c = c - min(d[l - 2], -x) - x
b = b - min(d[l - 2], -x)
elif x < 0 and d[l - 2] < 0:
c = c - x
d[l - 2] = d[l - 2] + x
elif x != 0 and l == 1:
F = F + x
if x != 0 and r < n:
if x <= 0 and d[r - 1] >= 0:
b = b - x
elif x <= 0 and d[r - 1] < 0:
b = b - x - min(-x, -d[r - 1])
c = c - min(-x, -d[r - 1])
elif x > 0 and d[r - 1] >= 0:
c = c - min(x, d[r - 1]) + x
b = b - min(x, d[r - 1])
elif x > 0 and d[r - 1] < 0:
c = c + x
d[r - 1] = d[r - 1] - x
elif x != 0 and r == n:
L = L + x
print((L + F + b + c + 3) // 4) | IMPORT 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 LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | import sys
rd = sys.stdin.readline
N = int(rd())
a = list(map(int, rd().split()))
for i in range(N - 1, 0, -1):
a[i] -= a[i - 1]
p = 0
for i in range(1, N):
p += max(0, a[i])
print((a[0] + p + 1) // 2)
Q = int(rd())
for _ in range(Q):
l, r, x = map(int, rd().split())
l -= 1
if l != 0:
p -= max(a[l], 0)
a[l] += x
if l != 0:
p += max(a[l], 0)
if r != N:
p -= max(a[r], 0)
a[r] -= x
p += max(a[r], 0)
print((a[0] + p + 1) // 2) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | n = int(input())
aa = list(map(int, input().split()))
dd = [0] * n
s = 0
for i in range(n - 1):
dd[i + 1] = aa[i + 1] - aa[i]
for d in dd:
if d > 0:
s += d
b0 = (aa[0] - s) // 2
print(max(b0 + s, aa[0] - b0))
def add(i, a, s):
if i == 0:
aa[0] += a
return s
if dd[i] > 0:
s -= dd[i]
dd[i] += a
if dd[i] > 0:
s += dd[i]
return s
for _ in range(int(input())):
l, r, x = map(int, input().split())
l -= 1
s = add(l, x, s)
if r < n:
s = add(r, -x, s)
b0 = (aa[0] - s) // 2
print(max(b0 + s, aa[0] - b0)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR RETURN VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | from sys import stdin
n = int(stdin.readline())
a = [int(x) for x in stdin.readline().split()]
prev = a[0]
diff = [prev]
bsum = prev
for el in a[1:]:
d = el - prev
diff.append(d)
if d > 0:
bsum += d
prev = el
print((bsum + 1) // 2)
q = int(stdin.readline())
for _ in range(q):
l, r, w = [int(x) for x in stdin.readline().split()]
if l == 1:
bsum += w
diff[l - 1] += w
else:
if diff[l - 1] > 0:
bsum -= diff[l - 1]
diff[l - 1] += w
if diff[l - 1] > 0:
bsum += diff[l - 1]
if r < n:
if diff[r] > 0:
bsum -= diff[r]
diff[r] -= w
if diff[r] > 0:
bsum += diff[r]
print((bsum + 1) // 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR VAR FOR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | n = int(input())
a = [int(i) for i in input().split()]
d = [(a[i + 1] - a[i]) for i in range(n - 1)]
dif = sum([max(v, 0) for v in d])
print((a[0] - dif + 1) // 2 + dif)
for k in range(int(input())):
l, r, x = [int(i) for i in input().split()]
l, r = l - 1, r - 1
if l == 0:
a[0] += x
if l > 0:
dif -= max(0, d[l - 1])
d[l - 1] += x
dif += max(0, d[l - 1])
if r < n - 1:
dif -= max(0, d[r])
d[r] -= x
dif += max(0, d[r])
print((a[0] - dif + 1) // 2 + dif) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR IF VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | import sys
_ord, inp, num, neg, _Index = lambda x: x, [], 0, False, 0
i, s = 0, sys.stdin.buffer.read()
try:
while True:
if s[i] >= b"0"[0]:
num = 10 * num + _ord(s[i]) - 48
elif s[i] == b"-"[0]:
neg = True
elif s[i] != b"\r"[0]:
inp.append(-num if neg else num)
num, neg = 0, False
i += 1
except IndexError:
pass
if s and s[-1] >= b"0"[0]:
inp.append(-num if neg else num)
def inin(size=None):
global _Index
if size == None:
ni = _Index
_Index += 1
return inp[ni]
else:
ni = _Index
_Index += size
return inp[ni : ni + size]
_T_ = 1
for _t_ in range(_T_):
n = inin()
a = inin(n)
d = [(0) for i in range(n - 1)]
k = 0
for i in range(1, n):
d[i - 1] += a[i] - a[i - 1]
k += max(0, a[i] - a[i - 1])
x = (a[0] + k + 1) // 2
print(x)
q = inin()
for _ in range(q):
l, r, c = inin(3)
l -= 1
r -= 1
if l == 0:
a[0] += c
else:
if d[l - 1] >= 0:
k -= d[l - 1]
d[l - 1] += c
if d[l - 1] >= 0:
k += d[l - 1]
if r == n - 1:
pass
else:
if d[r] >= 0:
k -= d[r]
d[r] -= c
if d[r] >= 0:
k += d[r]
x = (a[0] + k + 1) // 2
print(x) | IMPORT ASSIGN VAR VAR VAR VAR VAR VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR WHILE NUMBER IF VAR VAR UNKNOWN NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR UNKNOWN NUMBER ASSIGN VAR NUMBER IF VAR VAR UNKNOWN NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER VAR IF VAR VAR NUMBER UNKNOWN NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF NONE IF VAR NONE ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | import atexit
import sys
class Fastio:
def __init__(self):
self.ibuf = bytes()
self.obuf = bytearray()
self.pil = 0
self.pir = 0
self.buf = bytearray(20)
def load(self):
self.ibuf = self.ibuf[self.pil :]
self.ibuf += sys.stdin.buffer.read(131072)
self.pil = 0
self.pir = len(self.ibuf)
def flush(self):
sys.stdout.buffer.write(self.obuf)
self.obuf = bytearray()
def fastin(self):
if self.pir - self.pil < 32:
self.load()
minus = 0
x = 0
while self.ibuf[self.pil] < ord("-"):
self.pil += 1
if self.ibuf[self.pil] == ord("-"):
minus = 1
self.pil += 1
while self.ibuf[self.pil] >= ord("0"):
x = x * 10 + (self.ibuf[self.pil] & 15)
self.pil += 1
if minus:
x = -x
return x
def fastout(self, x, end=b"\n"):
i = 19
if x == 0:
self.buf[i] = 48
i -= 1
else:
if x < 0:
self.obuf.append(45)
x = -x
while x != 0:
x, r = x // 10, x % 10
self.buf[i] = 48 + r
i -= 1
self.obuf.extend(self.buf[i + 1 :])
self.obuf.extend(end)
if len(self.obuf) > 131072:
self.flush()
fastio = Fastio()
rd = fastio.fastin
wtn = fastio.fastout
atexit.register(fastio.flush)
N = rd()
a = [0] * N
for i in range(N):
a[i] = rd()
for i in range(N - 1, 0, -1):
a[i] -= a[i - 1]
p = 0
for i in range(1, N):
p += max(0, a[i])
wtn((a[0] + p + 1) // 2)
Q = rd()
for _ in range(Q):
l, r, x = rd(), rd(), rd()
l -= 1
if l != 0:
p -= max(a[l], 0)
a[l] += x
if l != 0:
p += max(a[l], 0)
if r != N:
p -= max(a[r], 0)
a[r] -= x
p += max(a[r], 0)
wtn((a[0] + p + 1) // 2) | IMPORT IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF UNKNOWN ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | n = int(input())
aaa = list(map(int, input().split()))
diff = []
score = 0
prv = 0
for a in aaa:
d = a - prv
diff.append(d)
if d > 0:
score += d
prv = a
buf = [(score + min(0, diff[0]) + 1) // 2]
q = int(input())
for _ in range(q):
l, r, x = list(map(int, input().split()))
l -= 1
dl1 = diff[l]
diff[l] += x
dl2 = diff[l]
score += max(0, dl2) - max(0, dl1)
if r < n:
dr1 = diff[r]
diff[r] -= x
dr2 = diff[r]
score += max(0, dr2) - max(0, dr1)
buf.append((score + min(0, diff[0]) + 1) // 2)
print("\n".join(map(str, buf))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | import sys
input = sys.stdin.readline
def solve():
n = int(input())
a = list(map(int, input().split()))
b = [0] * (n + 2)
sp = 0
sn = 0
for i in range(1, n):
b[i] = a[i] - a[i - 1]
if b[i] < 0:
sn += b[i]
else:
sp += b[i]
b[0] = a[0]
print((b[0] + sp + 1) // 2)
for q in range(int(input())):
l, r, x = map(int, input().split())
if l > 1:
if b[l - 1] < 0:
sn -= b[l - 1]
else:
sp -= b[l - 1]
b[l - 1] += x
if b[l - 1] < 0:
sn += b[l - 1]
else:
sp += b[l - 1]
else:
b[l - 1] += x
if r < n:
if b[r] < 0:
sn -= b[r]
else:
sp -= b[r]
b[r] -= x
if b[r] < 0:
sn += b[r]
else:
sp += b[r]
else:
b[r] -= x
print((b[0] + sp + 1) // 2)
solve() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | import sys
mod = 1000000007
eps = 10**-9
def main():
import sys
input = sys.stdin.buffer.readline
class Bit:
def __init__(self, n):
self.size = n
self.tree = [0] * (n + 1)
def sum(self, i):
s = 0
while i > 0:
s += self.tree[i]
i -= i & -i
return s
def add(self, i, x):
while i <= self.size:
self.tree[i] += x
i += i & -i
def lower_bound(self, w):
if w <= 0:
return 0
x = 0
k = 1 << self.size.bit_length() - 1
while k:
if x + k <= self.size and self.tree[x + k] < w:
w -= self.tree[x + k]
x += k
k >>= 1
return x + 1
N = int(input())
A = list(map(int, input().split()))
bit = Bit(N)
bit.add(1, A[0])
b = A[0]
ans = []
for i in range(1, N):
bit.add(i + 1, A[i] - A[i - 1])
if A[i] - A[i - 1] > 0:
b += A[i] - A[i - 1]
ans.append((b + 1) // 2)
for _ in range(int(input())):
l, r, x = map(int, input().split())
if l != 1:
dl_ori = bit.sum(l) - bit.sum(l - 1)
dl_new = dl_ori + x
dl_ori = max(0, dl_ori)
dl_new = max(0, dl_new)
b += dl_new - dl_ori
else:
b += x
if r != N:
dr_ori = bit.sum(r + 1) - bit.sum(r)
dr_new = dr_ori - x
dr_ori = max(0, dr_ori)
dr_new = max(0, dr_new)
b += dr_new - dr_ori
bit.add(l, x)
if r != N:
bit.add(r + 1, -x)
ans.append((b + 1) // 2)
print(*ans, sep="\n")
main() | IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR NUMBER WHILE VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | import sys
input = sys.stdin.buffer.readline
n, ar = int(input()), list(map(int, input().split()))
sm = ar[0]
d = [0] * n
d[0] = ar[0]
for i in range(1, n):
d[i] = ar[i] - ar[i - 1]
sm += max(d[i], 0)
print((sm + 1) // 2)
q = int(input())
for _ in range(q):
l, r, x = map(int, input().split())
sm -= max(d[l - 1], d[l - 1] if l - 1 == 0 else 0)
d[l - 1] += x
sm += max(d[l - 1], d[l - 1] if l - 1 == 0 else 0)
if r < n:
sm -= max(d[r], 0)
d[r] -= x
sm += max(d[r], 0)
print((sm + 1) // 2) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | import sys
rd = sys.stdin.readline
n = int(rd())
a = list(map(int, rd().split()))
pos = 0
sa = [0] * (n - 1)
for i in range(n - 1):
sa[i] = a[i + 1] - a[i]
if sa[i] > 0:
pos += sa[i]
print((a[0] + pos + 1) // 2)
q = int(rd())
for i in range(q):
l, r, x = map(int, rd().split())
if l == 1:
a[0] += x
if x > 0:
if l >= 2:
if sa[l - 2] >= 0:
pos += x
else:
pos += max(0, x + sa[l - 2])
sa[l - 2] += x
if r <= n - 1:
if sa[r - 1] >= x:
pos -= x
else:
pos -= max(0, sa[r - 1])
sa[r - 1] -= x
elif x < 0:
x = -x
if l >= 2:
if sa[l - 2] >= x:
pos -= x
else:
pos -= max(0, sa[l - 2])
sa[l - 2] -= x
if r <= n - 1:
if sa[r - 1] >= 0:
pos += x
else:
pos += max(0, x + sa[r - 1])
sa[r - 1] += x
print((a[0] + pos + 1) // 2) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR IF VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | import sys
input = sys.stdin.buffer.readline
n = int(input())
a = list(map(int, input().split()))
start = a[0]
d = [0] * n
R = 0
for i in range(n - 1):
d[i] = a[i + 1] - a[i]
if d[i] > 0:
R += d[i]
print((start + R + 1) // 2)
q = int(input())
for _ in range(q):
l, r, x = map(int, input().split())
l, r = l - 1, r - 1
if l == 0:
start += x
else:
if d[l - 1] > 0:
R -= d[l - 1]
d[l - 1] += x
if d[l - 1] > 0:
R += d[l - 1]
if r < n - 1:
if d[r] > 0:
R -= d[r]
d[r] -= x
if d[r] > 0:
R += d[r]
print((start + R + 1) // 2) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | import sys
input = sys.stdin.buffer.readline
class BIT:
def __init__(self, n):
self.size = n
self.bit = [0] * (n + 1)
def build(self, array):
for i in range(1, self.size + 1):
if i - 1 + (i & -i) >= self.size:
self.bit[i] = array[i - 1]
else:
self.bit[i] = array[i - 1] - array[i - 1 + (i & -i)]
def _add(self, i, val):
while i > 0:
self.bit[i] += val
i -= i & -i
def __getitem__(self, i):
i = i + 1
s = 0
while i <= self.size:
s += self.bit[i]
i += i & -i
return s
def add(self, l, r, val):
self._add(r, val)
self._add(l, -val)
n = int(input())
a = list(map(int, input().split()))
q = int(input())
queries = [list(map(int, input().split())) for i in range(q)]
diff = [(a[i + 1] - a[i]) for i in range(n - 1)]
diff_sum = 0
for d in diff:
if d > 0:
diff_sum += d
bit = BIT(n)
bit.build(a)
val0 = bit[0]
ans = []
ans.append((diff_sum + val0 + 1) // 2)
for l, r, val in queries:
l -= 1
bit.add(l, r, val)
d1, d2 = -1, -1
d1_new, d2_new = -1, -1
if l - 1 >= 0:
d1 = diff[l - 1]
d1_new = bit[l] - bit[l - 1]
diff[l - 1] = d1_new
if r - 1 < n - 1:
d2 = diff[r - 1]
d2_new = bit[r] - bit[r - 1]
diff[r - 1] = d2_new
diff_sum -= max(d1, 0) + max(d2, 0)
diff_sum += max(d1_new, 0) + max(d2_new, 0)
if l == 0:
val0 += val
ans.append((diff_sum + val0 + 1) // 2)
print("\n".join(map(str, ans))) | IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_DEF WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | import sys
input = sys.stdin.readline
(N,) = map(int, input().split())
X = list(map(int, input().split()))
Y = [0]
for i in range(N - 1):
Y.append(X[i + 1] - X[i])
Y.append(0)
t = X[0]
for y in Y:
if y > 0:
t += y
print((t + 1) // 2)
(M,) = map(int, input().split())
for i in range(M):
l, r, x = map(int, input().split())
b = max(Y[l - 1], 0)
Y[l - 1] += x
n = max(Y[l - 1], 0)
dd = 0
if l != 1:
dd = n - b
b2 = max(Y[r], 0)
Y[r] -= x
n2 = max(Y[r], 0)
if r != N:
dd += n2 - b2
if l == 1:
t += x
t += dd
print((t + 1) // 2) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER |
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice. | import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
lst = []
su_pos, su_neg = 0, 0
for i in range(1, n):
x = a[i] - a[i - 1]
lst.append(x)
if x > 0:
su_pos += x
else:
su_neg -= x
print((a[0] + su_pos + 1) // 2)
for _ in range(int(input())):
x, y, z = map(int, input().split())
x -= 1
y -= 1
if not x:
a[0] += z
if y != n - 1:
if lst[y] < 0:
su_neg += lst[y]
else:
su_pos -= lst[y]
lst[y] -= z
if lst[y] < 0:
su_neg -= lst[y]
else:
su_pos += lst[y]
if x:
if lst[x - 1] < 0:
su_neg += lst[x - 1]
else:
su_pos -= lst[x - 1]
lst[x - 1] += z
if lst[x - 1] < 0:
su_neg -= lst[x - 1]
else:
su_pos += lst[x - 1]
print((a[0] + su_pos + 1) // 2) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER |
It's Galactik Football time! The Galactik Football Assosiation (GFA) has announced a football tournament between all the teams of all the planets in the galaxy.
Each planet of the galaxy has a government. Some governments have a mutual agreement between them. If planet $A$ has mutual agreement with planet $B$, then there is a bidirectional spaceway between $A$ and $B$, using which anybody can go from $A$ to $B$ and vice-versa. People can use these spaceways to travel from one planet to another, if there exists a path between them using some of the spaceways.
Each planet has its own football ground. The GFA has planned the matches in such a way that a team can have a match at any of these grounds. The GFA has come across some problems in the execution of their plan. They have found out that there are many pairs of planets between which there does not exist any path, so the football team of one of those planets can't reach the other planet. They requested the corresponding governments to make a spaceway between them, but they canβt because of the absence of a mutual agreement. So the GFA suggested that they will make teleportation devices between some pairs of planets which will be used only by the football teams to travel.
But there are two types of governments in the galaxy
1. Some of the governments are greedy and they want to make money through the GFA. Each of these government has asked the GFA for a tax value which it has to pay if it wants to make a teleport ending at their planet.
2. Others want to sponsor the event, so they will give money to the GFA if they make a teleport ending at their planet. The GFA would always avoid such governments no matter what the consequences are, because these kind of governments always have some dirty plans up their sleeves.
Now, the GFA wants to make bi-directional teleports between planets such that the football teams of any planet can reach any other planet to play a football match, using spaceways between the planets and/or teleports made by the GFA.
The GFA also has financial problems and want to spend as little money as possible. They have come to you so that you can help them calculate the minimum amount of money needed to fulfil their plan.
------ Input ------
The first line of the input consists of two space separated integers - $N$ and $M$. $N$ is the number of planets and $M$ is the number of spaceways. The description of the spaceways follows.
The next $M$ lines, each contain two space separated integers $A$ and $B$, denoting a mutual agreement and hence a spaceway to travel, between planet $A$ and planet $B$.
The next $N$ lines each contain a single integer, the integer on the $i^{th}$ line representing $C_{i}$. If $C_{i} β₯ 0$, then it represents the tax value which the GFA has to pay to the government of planet $i$ (it's a type $1$ government). If $C_{i} < 0$, then it represents the money that the $i^{th}$ government will pay to the GFA (it's a type $2$ government).
------ Output ------
Print the minimum amount needed for the GFA to fulfil their plan. If there is no way to do so, print "-1" (without quotes).
------ Constraints ------
$1 β€ N β€ 10^{5}$
$0 β€ M β€ 10^{6}$
$0 β€ |C| β€ 10^{4}$
$1 β€ A,B β€ N$
$A \neq B$
----- Sample Input 1 ------
6 6
1 2
2 3
1 3
4 5
5 6
4 6
1
3
5
2
4
6
----- Sample Output 1 ------
3
----- explanation 1 ------
----- Sample Input 2 ------
3 1
2 3
1
-1
-1
----- Sample Output 2 ------
-1
----- explanation 2 ------ | def get_root(i, root):
if root[i] == i:
return i
return get_root(root[i], root)
n, m = map(int, input().split())
edges, cost = [], [0]
root = list(i for i in range(n + 1))
for i in range(m):
edges.append(tuple(map(int, input().split())))
for i in range(n):
cost.append(int(input()))
for edge in edges:
x, y, xroot, yroot = (
edge[0],
edge[1],
get_root(edge[0], root),
get_root(edge[1], root),
)
if xroot != yroot:
if cost[xroot] >= 0 and cost[yroot] >= 0:
if cost[xroot] <= cost[yroot]:
root[y] = xroot
root[yroot] = xroot
else:
root[x] = yroot
root[xroot] = yroot
elif cost[xroot] <= 0 and cost[yroot] <= 0:
root[y] = xroot
root[yroot] = xroot
elif cost[xroot] < 0:
root[x] = yroot
root[xroot] = yroot
else:
root[y] = xroot
root[yroot] = xroot
for i in range(1, n + 1):
root[i] = get_root(i, root)
d = {}
for i in range(1, n + 1):
d[root[i]] = cost[root[i]]
d = list(d.values())
minn = min(d)
if minn < 0 and len(d) > 1:
print(-1)
else:
print(sum(d) + (len(d) - 2) * min(d)) | FUNC_DEF IF VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR |
It's Galactik Football time! The Galactik Football Assosiation (GFA) has announced a football tournament between all the teams of all the planets in the galaxy.
Each planet of the galaxy has a government. Some governments have a mutual agreement between them. If planet $A$ has mutual agreement with planet $B$, then there is a bidirectional spaceway between $A$ and $B$, using which anybody can go from $A$ to $B$ and vice-versa. People can use these spaceways to travel from one planet to another, if there exists a path between them using some of the spaceways.
Each planet has its own football ground. The GFA has planned the matches in such a way that a team can have a match at any of these grounds. The GFA has come across some problems in the execution of their plan. They have found out that there are many pairs of planets between which there does not exist any path, so the football team of one of those planets can't reach the other planet. They requested the corresponding governments to make a spaceway between them, but they canβt because of the absence of a mutual agreement. So the GFA suggested that they will make teleportation devices between some pairs of planets which will be used only by the football teams to travel.
But there are two types of governments in the galaxy
1. Some of the governments are greedy and they want to make money through the GFA. Each of these government has asked the GFA for a tax value which it has to pay if it wants to make a teleport ending at their planet.
2. Others want to sponsor the event, so they will give money to the GFA if they make a teleport ending at their planet. The GFA would always avoid such governments no matter what the consequences are, because these kind of governments always have some dirty plans up their sleeves.
Now, the GFA wants to make bi-directional teleports between planets such that the football teams of any planet can reach any other planet to play a football match, using spaceways between the planets and/or teleports made by the GFA.
The GFA also has financial problems and want to spend as little money as possible. They have come to you so that you can help them calculate the minimum amount of money needed to fulfil their plan.
------ Input ------
The first line of the input consists of two space separated integers - $N$ and $M$. $N$ is the number of planets and $M$ is the number of spaceways. The description of the spaceways follows.
The next $M$ lines, each contain two space separated integers $A$ and $B$, denoting a mutual agreement and hence a spaceway to travel, between planet $A$ and planet $B$.
The next $N$ lines each contain a single integer, the integer on the $i^{th}$ line representing $C_{i}$. If $C_{i} β₯ 0$, then it represents the tax value which the GFA has to pay to the government of planet $i$ (it's a type $1$ government). If $C_{i} < 0$, then it represents the money that the $i^{th}$ government will pay to the GFA (it's a type $2$ government).
------ Output ------
Print the minimum amount needed for the GFA to fulfil their plan. If there is no way to do so, print "-1" (without quotes).
------ Constraints ------
$1 β€ N β€ 10^{5}$
$0 β€ M β€ 10^{6}$
$0 β€ |C| β€ 10^{4}$
$1 β€ A,B β€ N$
$A \neq B$
----- Sample Input 1 ------
6 6
1 2
2 3
1 3
4 5
5 6
4 6
1
3
5
2
4
6
----- Sample Output 1 ------
3
----- explanation 1 ------
----- Sample Input 2 ------
3 1
2 3
1
-1
-1
----- Sample Output 2 ------
-1
----- explanation 2 ------ | from sys import stdin
class Node:
def __init__(self, data, price):
self.data = data
self.rank = 0
self.networkPrice = price
self.parent = self
def find_root(node):
if node != node.parent:
node.parent = find_root(node.parent)
return node.parent
return node.parent
def calculate_price(parent_1, parent_2):
if parent_2.networkPrice < parent_1.networkPrice:
if parent_2.networkPrice >= 0:
return parent_2.networkPrice
else:
return parent_1.networkPrice
elif parent_1.networkPrice >= 0:
return parent_1.networkPrice
else:
return parent_2.networkPrice
def union(node_1, node_2):
parent_1, parent_2 = find_root(node_1), find_root(node_2)
min_price = calculate_price(parent_1, parent_2)
if parent_1 == parent_2:
return
if parent_1.rank > parent_2.rank:
parent_2.parent = parent_1
parent_1.networkPrice = min_price
else:
parent_1.parent = parent_2
parent_2.networkPrice = min_price
if parent_1.rank == parent_2.rank:
parent_2.rank += 1
N, M = map(int, stdin.readline().strip().split(" "))
queries = []
for i in range(M):
queries.append(tuple(map(int, stdin.readline().strip().split(" "))))
prices = []
for i in range(N):
prices.append(int(stdin.readline()))
vertex = [Node(i, j) for i, j in zip(range(1, N + 1), prices)]
for i in queries:
union(vertex[i[0] - 1], vertex[i[1] - 1])
usage_dict = {}
for i in range(1, N + 1):
usage_dict[i] = False
sum = 0
possible = True
min_cost = 10**5
countDict = {}
for i in vertex:
networkRoot = find_root(i)
if networkRoot.data not in countDict:
countDict[networkRoot.data] = True
if networkRoot.networkPrice < 0:
possible = False
break
if usage_dict[networkRoot.data] == False and networkRoot.networkPrice >= 0:
if min_cost > networkRoot.networkPrice:
min_cost = networkRoot.networkPrice
sum += networkRoot.networkPrice
usage_dict[networkRoot.data] = True
count = 0
for i in countDict:
count += 1
if count == 1:
print("0")
elif not possible:
print("-1")
else:
print(sum + (count - 2) * min_cost) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR RETURN VAR FUNC_DEF IF VAR VAR IF VAR NUMBER RETURN VAR RETURN VAR IF VAR NUMBER RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR |
It's Galactik Football time! The Galactik Football Assosiation (GFA) has announced a football tournament between all the teams of all the planets in the galaxy.
Each planet of the galaxy has a government. Some governments have a mutual agreement between them. If planet $A$ has mutual agreement with planet $B$, then there is a bidirectional spaceway between $A$ and $B$, using which anybody can go from $A$ to $B$ and vice-versa. People can use these spaceways to travel from one planet to another, if there exists a path between them using some of the spaceways.
Each planet has its own football ground. The GFA has planned the matches in such a way that a team can have a match at any of these grounds. The GFA has come across some problems in the execution of their plan. They have found out that there are many pairs of planets between which there does not exist any path, so the football team of one of those planets can't reach the other planet. They requested the corresponding governments to make a spaceway between them, but they canβt because of the absence of a mutual agreement. So the GFA suggested that they will make teleportation devices between some pairs of planets which will be used only by the football teams to travel.
But there are two types of governments in the galaxy
1. Some of the governments are greedy and they want to make money through the GFA. Each of these government has asked the GFA for a tax value which it has to pay if it wants to make a teleport ending at their planet.
2. Others want to sponsor the event, so they will give money to the GFA if they make a teleport ending at their planet. The GFA would always avoid such governments no matter what the consequences are, because these kind of governments always have some dirty plans up their sleeves.
Now, the GFA wants to make bi-directional teleports between planets such that the football teams of any planet can reach any other planet to play a football match, using spaceways between the planets and/or teleports made by the GFA.
The GFA also has financial problems and want to spend as little money as possible. They have come to you so that you can help them calculate the minimum amount of money needed to fulfil their plan.
------ Input ------
The first line of the input consists of two space separated integers - $N$ and $M$. $N$ is the number of planets and $M$ is the number of spaceways. The description of the spaceways follows.
The next $M$ lines, each contain two space separated integers $A$ and $B$, denoting a mutual agreement and hence a spaceway to travel, between planet $A$ and planet $B$.
The next $N$ lines each contain a single integer, the integer on the $i^{th}$ line representing $C_{i}$. If $C_{i} β₯ 0$, then it represents the tax value which the GFA has to pay to the government of planet $i$ (it's a type $1$ government). If $C_{i} < 0$, then it represents the money that the $i^{th}$ government will pay to the GFA (it's a type $2$ government).
------ Output ------
Print the minimum amount needed for the GFA to fulfil their plan. If there is no way to do so, print "-1" (without quotes).
------ Constraints ------
$1 β€ N β€ 10^{5}$
$0 β€ M β€ 10^{6}$
$0 β€ |C| β€ 10^{4}$
$1 β€ A,B β€ N$
$A \neq B$
----- Sample Input 1 ------
6 6
1 2
2 3
1 3
4 5
5 6
4 6
1
3
5
2
4
6
----- Sample Output 1 ------
3
----- explanation 1 ------
----- Sample Input 2 ------
3 1
2 3
1
-1
-1
----- Sample Output 2 ------
-1
----- explanation 2 ------ | import sys
sys.setrecursionlimit(10**6)
def dfs(vi, n, ed, c, mi):
vi[n] = True
if c[n] >= 0:
mi[0] = min(mi[0], c[n])
for l in ed[n]:
if vi[l] == False:
dfs(vi, l, ed, c, mi)
n, m = [int(a) for a in input().split()]
ed = []
for l in range(n + 1):
ed.append([])
for l in range(m):
u, v = [int(a) for a in input().split()]
ed[u].append(v)
ed[v].append(u)
c = [0]
for l in range(n):
c.append(int(input()))
ans = []
vi = []
for l in range(n + 1):
vi.append(False)
co = 0
for v in range(1, n + 1):
mi = [float("inf")]
f = False
if vi[v] == False:
dfs(vi, v, ed, c, mi)
co = co + 1
if mi[0] == float("inf"):
if co >= 2:
f = True
print(-1)
break
else:
ans.append(0)
else:
ans.append(mi[0])
if f:
break
if not f:
to = 0
ans.sort()
for l in range(1, len(ans)):
to = to + ans[l] + ans[0]
print(to) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR IF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
It's Galactik Football time! The Galactik Football Assosiation (GFA) has announced a football tournament between all the teams of all the planets in the galaxy.
Each planet of the galaxy has a government. Some governments have a mutual agreement between them. If planet $A$ has mutual agreement with planet $B$, then there is a bidirectional spaceway between $A$ and $B$, using which anybody can go from $A$ to $B$ and vice-versa. People can use these spaceways to travel from one planet to another, if there exists a path between them using some of the spaceways.
Each planet has its own football ground. The GFA has planned the matches in such a way that a team can have a match at any of these grounds. The GFA has come across some problems in the execution of their plan. They have found out that there are many pairs of planets between which there does not exist any path, so the football team of one of those planets can't reach the other planet. They requested the corresponding governments to make a spaceway between them, but they canβt because of the absence of a mutual agreement. So the GFA suggested that they will make teleportation devices between some pairs of planets which will be used only by the football teams to travel.
But there are two types of governments in the galaxy
1. Some of the governments are greedy and they want to make money through the GFA. Each of these government has asked the GFA for a tax value which it has to pay if it wants to make a teleport ending at their planet.
2. Others want to sponsor the event, so they will give money to the GFA if they make a teleport ending at their planet. The GFA would always avoid such governments no matter what the consequences are, because these kind of governments always have some dirty plans up their sleeves.
Now, the GFA wants to make bi-directional teleports between planets such that the football teams of any planet can reach any other planet to play a football match, using spaceways between the planets and/or teleports made by the GFA.
The GFA also has financial problems and want to spend as little money as possible. They have come to you so that you can help them calculate the minimum amount of money needed to fulfil their plan.
------ Input ------
The first line of the input consists of two space separated integers - $N$ and $M$. $N$ is the number of planets and $M$ is the number of spaceways. The description of the spaceways follows.
The next $M$ lines, each contain two space separated integers $A$ and $B$, denoting a mutual agreement and hence a spaceway to travel, between planet $A$ and planet $B$.
The next $N$ lines each contain a single integer, the integer on the $i^{th}$ line representing $C_{i}$. If $C_{i} β₯ 0$, then it represents the tax value which the GFA has to pay to the government of planet $i$ (it's a type $1$ government). If $C_{i} < 0$, then it represents the money that the $i^{th}$ government will pay to the GFA (it's a type $2$ government).
------ Output ------
Print the minimum amount needed for the GFA to fulfil their plan. If there is no way to do so, print "-1" (without quotes).
------ Constraints ------
$1 β€ N β€ 10^{5}$
$0 β€ M β€ 10^{6}$
$0 β€ |C| β€ 10^{4}$
$1 β€ A,B β€ N$
$A \neq B$
----- Sample Input 1 ------
6 6
1 2
2 3
1 3
4 5
5 6
4 6
1
3
5
2
4
6
----- Sample Output 1 ------
3
----- explanation 1 ------
----- Sample Input 2 ------
3 1
2 3
1
-1
-1
----- Sample Output 2 ------
-1
----- explanation 2 ------ | def find(child):
if child == parents[child]:
return parents[child]
parents[child] = find(parents[child])
return parents[child]
def union(root1, root2):
real_root1, real_root2 = find(root1), find(root2)
if real_root1 == real_root2:
return
if rank[real_root1] <= rank[real_root2]:
parents[real_root1] = real_root2
if rank[real_root1] == rank[real_root2]:
rank[real_root2] += 1
else:
parents[real_root2] = real_root1
n, m = map(int, input().strip().split())
parents = list(range(n))
rank = [(0) for i in range(n)]
for i in range(m):
root1, root2 = map(int, input().strip().split())
union(root1 - 1, root2 - 1)
cost_dict = {}
for i in range(n):
if i == parents[i]:
cost_dict[i] = 10**5
cost = [(10**5) for i in range(n)]
min_cost = 10**5
for i in range(n):
c = int(input().strip())
if c >= 0:
cost[i] = c
grand_parent = find(i)
cost_dict[grand_parent] = min(c, cost_dict[grand_parent])
min_cost = min(c, min_cost)
possible = True
total_cost = 0
for root in cost_dict:
cst = cost_dict[root]
if cst == 10**5 or cst > 10**4:
possible = False
break
else:
total_cost += cst
if len(cost_dict) == 1:
print(0)
elif not possible:
print(-1)
else:
total_cost = total_cost + (len(cost_dict) - 2) * min_cost
print(total_cost) | FUNC_DEF IF VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
It's Galactik Football time! The Galactik Football Assosiation (GFA) has announced a football tournament between all the teams of all the planets in the galaxy.
Each planet of the galaxy has a government. Some governments have a mutual agreement between them. If planet $A$ has mutual agreement with planet $B$, then there is a bidirectional spaceway between $A$ and $B$, using which anybody can go from $A$ to $B$ and vice-versa. People can use these spaceways to travel from one planet to another, if there exists a path between them using some of the spaceways.
Each planet has its own football ground. The GFA has planned the matches in such a way that a team can have a match at any of these grounds. The GFA has come across some problems in the execution of their plan. They have found out that there are many pairs of planets between which there does not exist any path, so the football team of one of those planets can't reach the other planet. They requested the corresponding governments to make a spaceway between them, but they canβt because of the absence of a mutual agreement. So the GFA suggested that they will make teleportation devices between some pairs of planets which will be used only by the football teams to travel.
But there are two types of governments in the galaxy
1. Some of the governments are greedy and they want to make money through the GFA. Each of these government has asked the GFA for a tax value which it has to pay if it wants to make a teleport ending at their planet.
2. Others want to sponsor the event, so they will give money to the GFA if they make a teleport ending at their planet. The GFA would always avoid such governments no matter what the consequences are, because these kind of governments always have some dirty plans up their sleeves.
Now, the GFA wants to make bi-directional teleports between planets such that the football teams of any planet can reach any other planet to play a football match, using spaceways between the planets and/or teleports made by the GFA.
The GFA also has financial problems and want to spend as little money as possible. They have come to you so that you can help them calculate the minimum amount of money needed to fulfil their plan.
------ Input ------
The first line of the input consists of two space separated integers - $N$ and $M$. $N$ is the number of planets and $M$ is the number of spaceways. The description of the spaceways follows.
The next $M$ lines, each contain two space separated integers $A$ and $B$, denoting a mutual agreement and hence a spaceway to travel, between planet $A$ and planet $B$.
The next $N$ lines each contain a single integer, the integer on the $i^{th}$ line representing $C_{i}$. If $C_{i} β₯ 0$, then it represents the tax value which the GFA has to pay to the government of planet $i$ (it's a type $1$ government). If $C_{i} < 0$, then it represents the money that the $i^{th}$ government will pay to the GFA (it's a type $2$ government).
------ Output ------
Print the minimum amount needed for the GFA to fulfil their plan. If there is no way to do so, print "-1" (without quotes).
------ Constraints ------
$1 β€ N β€ 10^{5}$
$0 β€ M β€ 10^{6}$
$0 β€ |C| β€ 10^{4}$
$1 β€ A,B β€ N$
$A \neq B$
----- Sample Input 1 ------
6 6
1 2
2 3
1 3
4 5
5 6
4 6
1
3
5
2
4
6
----- Sample Output 1 ------
3
----- explanation 1 ------
----- Sample Input 2 ------
3 1
2 3
1
-1
-1
----- Sample Output 2 ------
-1
----- explanation 2 ------ | def find_root(arr, n):
while arr[n] != n:
n = arr[n]
return n
def union(arr, a, b, size):
root_a = find_root(arr, a)
root_b = find_root(arr, b)
if root_a == root_b:
return
if size[root_a] < size[root_b]:
arr[root_a] = arr[root_b]
size[root_b] += arr[root_a]
else:
arr[root_b] = arr[root_a]
size[root_a] += size[root_b]
def same_root(arr, i, cost):
root = find_root(arr, i)
if cost[i] < cost[root] and cost[i] > -1 or cost[root] < 0 and cost[i] > -1:
cost[root] = cost[i]
arr[i] = root
N, M = [int(x) for x in input().split()]
size = [1] * (N + 1)
arr = [x for x in range(N + 1)]
cost = [0]
while M > 0:
a, b = [int(x) for x in input().split()]
union(arr, a, b, size)
M -= 1
temp = N
while temp > 0:
cost.append(int(input()))
temp -= 1
for i in range(1, N + 1):
same_root(arr, i, cost)
roots_dict = {}
sum = 0
cnt = 0
min = 1000000
flag = True
for i in range(1, N + 1):
if arr[i] not in roots_dict:
if cost[arr[i]] < 0:
flag = False
sum += cost[arr[i]]
if cost[arr[i]] < min:
min = cost[arr[i]]
roots_dict[arr[i]] = True
cnt += 1
if cnt == 1:
print("0")
elif flag:
print(sum + (cnt - 2) * min)
else:
print("-1") | FUNC_DEF WHILE VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING |
It's Galactik Football time! The Galactik Football Assosiation (GFA) has announced a football tournament between all the teams of all the planets in the galaxy.
Each planet of the galaxy has a government. Some governments have a mutual agreement between them. If planet $A$ has mutual agreement with planet $B$, then there is a bidirectional spaceway between $A$ and $B$, using which anybody can go from $A$ to $B$ and vice-versa. People can use these spaceways to travel from one planet to another, if there exists a path between them using some of the spaceways.
Each planet has its own football ground. The GFA has planned the matches in such a way that a team can have a match at any of these grounds. The GFA has come across some problems in the execution of their plan. They have found out that there are many pairs of planets between which there does not exist any path, so the football team of one of those planets can't reach the other planet. They requested the corresponding governments to make a spaceway between them, but they canβt because of the absence of a mutual agreement. So the GFA suggested that they will make teleportation devices between some pairs of planets which will be used only by the football teams to travel.
But there are two types of governments in the galaxy
1. Some of the governments are greedy and they want to make money through the GFA. Each of these government has asked the GFA for a tax value which it has to pay if it wants to make a teleport ending at their planet.
2. Others want to sponsor the event, so they will give money to the GFA if they make a teleport ending at their planet. The GFA would always avoid such governments no matter what the consequences are, because these kind of governments always have some dirty plans up their sleeves.
Now, the GFA wants to make bi-directional teleports between planets such that the football teams of any planet can reach any other planet to play a football match, using spaceways between the planets and/or teleports made by the GFA.
The GFA also has financial problems and want to spend as little money as possible. They have come to you so that you can help them calculate the minimum amount of money needed to fulfil their plan.
------ Input ------
The first line of the input consists of two space separated integers - $N$ and $M$. $N$ is the number of planets and $M$ is the number of spaceways. The description of the spaceways follows.
The next $M$ lines, each contain two space separated integers $A$ and $B$, denoting a mutual agreement and hence a spaceway to travel, between planet $A$ and planet $B$.
The next $N$ lines each contain a single integer, the integer on the $i^{th}$ line representing $C_{i}$. If $C_{i} β₯ 0$, then it represents the tax value which the GFA has to pay to the government of planet $i$ (it's a type $1$ government). If $C_{i} < 0$, then it represents the money that the $i^{th}$ government will pay to the GFA (it's a type $2$ government).
------ Output ------
Print the minimum amount needed for the GFA to fulfil their plan. If there is no way to do so, print "-1" (without quotes).
------ Constraints ------
$1 β€ N β€ 10^{5}$
$0 β€ M β€ 10^{6}$
$0 β€ |C| β€ 10^{4}$
$1 β€ A,B β€ N$
$A \neq B$
----- Sample Input 1 ------
6 6
1 2
2 3
1 3
4 5
5 6
4 6
1
3
5
2
4
6
----- Sample Output 1 ------
3
----- explanation 1 ------
----- Sample Input 2 ------
3 1
2 3
1
-1
-1
----- Sample Output 2 ------
-1
----- explanation 2 ------ | class DisjointSet:
def __init__(self, n, cost):
self.parent = [i for i in range(n + 1)]
self.rank = [1] * (n + 1)
self.cost = cost
self.visited = [0] * (n + 1)
def find(self, x):
if self.parent[x] != x:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def union(self, x, y):
x_repr = self.find(x)
y_repr = self.find(y)
if x_repr == y_repr:
return
elif self.rank[x_repr] < self.rank[y_repr]:
self.parent[x_repr] = y_repr
elif self.rank[y_repr] < self.rank[x_repr]:
self.parent[y_repr] = x_repr
else:
self.parent[x_repr] = y_repr
self.rank[y_repr] += 1
def populate_representatives(self, x):
root = self.find(x)
if (
0 <= self.cost[x] < self.cost[root]
or self.cost[root] < 0
and self.cost[x] >= 0
):
self.cost[root] = self.cost[x]
n, m = map(int, input().strip().split())
connections = []
cost = [0] * (n + 1)
summ = 0
minimum = 100005
count = 0
not_possible = 0
for _ in range(m):
x, y = map(int, input().strip().split())
connections.append((x, y))
for _ in range(1, n + 1):
cost[_] = int(input())
ds = DisjointSet(n, cost)
for i in connections:
ds.union(i[0], i[1])
for _ in range(1, n + 1):
ds.populate_representatives(_)
for i in range(1, n + 1):
if not ds.visited[ds.parent[i]]:
ds.visited[ds.parent[i]] = 1
if ds.cost[ds.parent[i]] < 0:
not_possible = 1
summ += ds.cost[ds.parent[i]]
minimum = min(minimum, ds.cost[ds.parent[i]])
count += 1
if count != 1:
if not_possible:
print(-1)
else:
count = (count - 2) * minimum
print(summ + count)
else:
print(0) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER |
It's Galactik Football time! The Galactik Football Assosiation (GFA) has announced a football tournament between all the teams of all the planets in the galaxy.
Each planet of the galaxy has a government. Some governments have a mutual agreement between them. If planet $A$ has mutual agreement with planet $B$, then there is a bidirectional spaceway between $A$ and $B$, using which anybody can go from $A$ to $B$ and vice-versa. People can use these spaceways to travel from one planet to another, if there exists a path between them using some of the spaceways.
Each planet has its own football ground. The GFA has planned the matches in such a way that a team can have a match at any of these grounds. The GFA has come across some problems in the execution of their plan. They have found out that there are many pairs of planets between which there does not exist any path, so the football team of one of those planets can't reach the other planet. They requested the corresponding governments to make a spaceway between them, but they canβt because of the absence of a mutual agreement. So the GFA suggested that they will make teleportation devices between some pairs of planets which will be used only by the football teams to travel.
But there are two types of governments in the galaxy
1. Some of the governments are greedy and they want to make money through the GFA. Each of these government has asked the GFA for a tax value which it has to pay if it wants to make a teleport ending at their planet.
2. Others want to sponsor the event, so they will give money to the GFA if they make a teleport ending at their planet. The GFA would always avoid such governments no matter what the consequences are, because these kind of governments always have some dirty plans up their sleeves.
Now, the GFA wants to make bi-directional teleports between planets such that the football teams of any planet can reach any other planet to play a football match, using spaceways between the planets and/or teleports made by the GFA.
The GFA also has financial problems and want to spend as little money as possible. They have come to you so that you can help them calculate the minimum amount of money needed to fulfil their plan.
------ Input ------
The first line of the input consists of two space separated integers - $N$ and $M$. $N$ is the number of planets and $M$ is the number of spaceways. The description of the spaceways follows.
The next $M$ lines, each contain two space separated integers $A$ and $B$, denoting a mutual agreement and hence a spaceway to travel, between planet $A$ and planet $B$.
The next $N$ lines each contain a single integer, the integer on the $i^{th}$ line representing $C_{i}$. If $C_{i} β₯ 0$, then it represents the tax value which the GFA has to pay to the government of planet $i$ (it's a type $1$ government). If $C_{i} < 0$, then it represents the money that the $i^{th}$ government will pay to the GFA (it's a type $2$ government).
------ Output ------
Print the minimum amount needed for the GFA to fulfil their plan. If there is no way to do so, print "-1" (without quotes).
------ Constraints ------
$1 β€ N β€ 10^{5}$
$0 β€ M β€ 10^{6}$
$0 β€ |C| β€ 10^{4}$
$1 β€ A,B β€ N$
$A \neq B$
----- Sample Input 1 ------
6 6
1 2
2 3
1 3
4 5
5 6
4 6
1
3
5
2
4
6
----- Sample Output 1 ------
3
----- explanation 1 ------
----- Sample Input 2 ------
3 1
2 3
1
-1
-1
----- Sample Output 2 ------
-1
----- explanation 2 ------ | from sys import stdin, stdout
def find(a):
if par[a] == a:
return a
else:
par[a] = find(par[a])
return par[a]
def union(a, b):
a, b = find(a), find(b)
if a != b:
if rank[a] > rank[b]:
par[b] = a
elif rank[b] > rank[a]:
par[a] = b
else:
par[a] = b
rank[b] += 1
n, m = stdin.readline().strip().split(" ")
n, m = int(n), int(m)
par = [i for i in range(n)]
rank = [(0) for i in range(n)]
cost = [(-1) for i in range(n)]
for i in range(m):
u, v = stdin.readline().strip().split(" ")
u, v = int(u) - 1, int(v) - 1
union(u, v)
up = set()
for i in range(n):
c = int(stdin.readline().strip())
up.add(find(i))
if c >= 0:
if cost[find(i)] == -1:
cost[find(i)] = c
else:
cost[find(i)] = min(cost[find(i)], c)
if len(up) == 1:
print(0)
else:
flag = True
for i in up:
if cost[i] == -1:
print(-1)
flag = False
break
if flag:
t1 = [cost[x] for x in up]
t1.sort()
print(sum(t1) + (len(t1) - 2) * t1[0]) | FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER |
It's Galactik Football time! The Galactik Football Assosiation (GFA) has announced a football tournament between all the teams of all the planets in the galaxy.
Each planet of the galaxy has a government. Some governments have a mutual agreement between them. If planet $A$ has mutual agreement with planet $B$, then there is a bidirectional spaceway between $A$ and $B$, using which anybody can go from $A$ to $B$ and vice-versa. People can use these spaceways to travel from one planet to another, if there exists a path between them using some of the spaceways.
Each planet has its own football ground. The GFA has planned the matches in such a way that a team can have a match at any of these grounds. The GFA has come across some problems in the execution of their plan. They have found out that there are many pairs of planets between which there does not exist any path, so the football team of one of those planets can't reach the other planet. They requested the corresponding governments to make a spaceway between them, but they canβt because of the absence of a mutual agreement. So the GFA suggested that they will make teleportation devices between some pairs of planets which will be used only by the football teams to travel.
But there are two types of governments in the galaxy
1. Some of the governments are greedy and they want to make money through the GFA. Each of these government has asked the GFA for a tax value which it has to pay if it wants to make a teleport ending at their planet.
2. Others want to sponsor the event, so they will give money to the GFA if they make a teleport ending at their planet. The GFA would always avoid such governments no matter what the consequences are, because these kind of governments always have some dirty plans up their sleeves.
Now, the GFA wants to make bi-directional teleports between planets such that the football teams of any planet can reach any other planet to play a football match, using spaceways between the planets and/or teleports made by the GFA.
The GFA also has financial problems and want to spend as little money as possible. They have come to you so that you can help them calculate the minimum amount of money needed to fulfil their plan.
------ Input ------
The first line of the input consists of two space separated integers - $N$ and $M$. $N$ is the number of planets and $M$ is the number of spaceways. The description of the spaceways follows.
The next $M$ lines, each contain two space separated integers $A$ and $B$, denoting a mutual agreement and hence a spaceway to travel, between planet $A$ and planet $B$.
The next $N$ lines each contain a single integer, the integer on the $i^{th}$ line representing $C_{i}$. If $C_{i} β₯ 0$, then it represents the tax value which the GFA has to pay to the government of planet $i$ (it's a type $1$ government). If $C_{i} < 0$, then it represents the money that the $i^{th}$ government will pay to the GFA (it's a type $2$ government).
------ Output ------
Print the minimum amount needed for the GFA to fulfil their plan. If there is no way to do so, print "-1" (without quotes).
------ Constraints ------
$1 β€ N β€ 10^{5}$
$0 β€ M β€ 10^{6}$
$0 β€ |C| β€ 10^{4}$
$1 β€ A,B β€ N$
$A \neq B$
----- Sample Input 1 ------
6 6
1 2
2 3
1 3
4 5
5 6
4 6
1
3
5
2
4
6
----- Sample Output 1 ------
3
----- explanation 1 ------
----- Sample Input 2 ------
3 1
2 3
1
-1
-1
----- Sample Output 2 ------
-1
----- explanation 2 ------ | def find(l, ind):
if l[ind] < 0:
return l[ind], ind
else:
return find(l, l[ind])
n, m = map(int, input().split())
l = [-1] * (n + 1)
for x in range(m):
a, b = map(int, input().split())
a1 = find(l, a)
b1 = find(l, b)
if a1[1] != b1[1]:
if a1[0] == b1[0]:
l[a1[1]] += a1[0]
l[b1[1]] = a1[1]
elif a1[0] < b1[0]:
l[a1[1]] += b1[0]
l[b1[1]] = a1[1]
else:
l[b1[1]] += a1[0]
l[a1[1]] = b1[1]
else:
if a != a1[1]:
l[a] = a1[1]
if b != a1[1]:
l[b] = a1[1]
l1 = [0] * n
for x in range(n):
l1[x] = int(input())
d = dict()
for x in range(1, len(l)):
if l[x] < 0:
d[x] = [x]
for x in range(1, len(l)):
if l[x] > 0:
e = find(l, l[x])
d[e[1]].append(x)
ans = 0
global_min = 100000
if len(d) == 1:
print(0)
else:
for x in d:
min_cost = 100000
b = 0
for y in d[x]:
if l1[y - 1] < 0:
continue
if l1[y - 1] < global_min:
global_min = l1[y - 1]
if l1[y - 1] < min_cost:
min_cost = l1[y - 1]
b = 1
if b == 0:
ans = -1
break
else:
ans += min_cost
if b == 0:
print(-1)
else:
print(ans + (len(d) - 2) * global_min) | FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR |
It's Galactik Football time! The Galactik Football Assosiation (GFA) has announced a football tournament between all the teams of all the planets in the galaxy.
Each planet of the galaxy has a government. Some governments have a mutual agreement between them. If planet $A$ has mutual agreement with planet $B$, then there is a bidirectional spaceway between $A$ and $B$, using which anybody can go from $A$ to $B$ and vice-versa. People can use these spaceways to travel from one planet to another, if there exists a path between them using some of the spaceways.
Each planet has its own football ground. The GFA has planned the matches in such a way that a team can have a match at any of these grounds. The GFA has come across some problems in the execution of their plan. They have found out that there are many pairs of planets between which there does not exist any path, so the football team of one of those planets can't reach the other planet. They requested the corresponding governments to make a spaceway between them, but they canβt because of the absence of a mutual agreement. So the GFA suggested that they will make teleportation devices between some pairs of planets which will be used only by the football teams to travel.
But there are two types of governments in the galaxy
1. Some of the governments are greedy and they want to make money through the GFA. Each of these government has asked the GFA for a tax value which it has to pay if it wants to make a teleport ending at their planet.
2. Others want to sponsor the event, so they will give money to the GFA if they make a teleport ending at their planet. The GFA would always avoid such governments no matter what the consequences are, because these kind of governments always have some dirty plans up their sleeves.
Now, the GFA wants to make bi-directional teleports between planets such that the football teams of any planet can reach any other planet to play a football match, using spaceways between the planets and/or teleports made by the GFA.
The GFA also has financial problems and want to spend as little money as possible. They have come to you so that you can help them calculate the minimum amount of money needed to fulfil their plan.
------ Input ------
The first line of the input consists of two space separated integers - $N$ and $M$. $N$ is the number of planets and $M$ is the number of spaceways. The description of the spaceways follows.
The next $M$ lines, each contain two space separated integers $A$ and $B$, denoting a mutual agreement and hence a spaceway to travel, between planet $A$ and planet $B$.
The next $N$ lines each contain a single integer, the integer on the $i^{th}$ line representing $C_{i}$. If $C_{i} β₯ 0$, then it represents the tax value which the GFA has to pay to the government of planet $i$ (it's a type $1$ government). If $C_{i} < 0$, then it represents the money that the $i^{th}$ government will pay to the GFA (it's a type $2$ government).
------ Output ------
Print the minimum amount needed for the GFA to fulfil their plan. If there is no way to do so, print "-1" (without quotes).
------ Constraints ------
$1 β€ N β€ 10^{5}$
$0 β€ M β€ 10^{6}$
$0 β€ |C| β€ 10^{4}$
$1 β€ A,B β€ N$
$A \neq B$
----- Sample Input 1 ------
6 6
1 2
2 3
1 3
4 5
5 6
4 6
1
3
5
2
4
6
----- Sample Output 1 ------
3
----- explanation 1 ------
----- Sample Input 2 ------
3 1
2 3
1
-1
-1
----- Sample Output 2 ------
-1
----- explanation 2 ------ | from sys import setrecursionlimit, stdin, stdout
setrecursionlimit(100000)
class Graph:
def __init__(self, vertices):
self.graph = [(-1) for i in range(vertices + 1)]
def findParent(self, x):
if self.graph[x] < 0:
return x
else:
self.graph[x] = self.findParent(self.graph[x])
return self.graph[x]
def union(self, x, y):
if self.graph[x] <= self.graph[y]:
self.graph[x] += self.graph[y]
self.graph[y] = x
else:
self.graph[y] += self.graph[x]
self.graph[x] = y
def addEdge(self, x, y):
x = self.findParent(x)
y = self.findParent(y)
if x != y:
self.union(x, y)
n, m = map(int, input().split())
planets = n
g = Graph(n)
cost = []
for _ in range(m):
a, b = map(int, input().split())
g.addEdge(a, b)
for i in range(n):
c = int(input())
index = i + 1
if c >= 0:
cost.append((c, index))
cost.sort()
total = 0
n = len(cost)
if n >= 2:
e1 = cost.pop(0)
for i in range(n - 1):
e2 = cost[i]
p1 = g.findParent(e1[1])
p2 = g.findParent(e2[1])
if p1 != p2:
total = total + (e2[0] + e1[0])
g.union(p1, p2)
s = set()
for i in range(1, planets + 1):
p = g.findParent(i)
s.add(p)
if len(s) == 1:
print(str(total))
else:
print("-1") | EXPR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
Given an array arr[] denoting heights of N towers and a positive integer K.
For each tower, you must perform exactly one of the following operations exactly once.
Increase the height of the tower by K
Decrease the height of the tower by K
Find out the minimum possible difference between the height of the shortest and tallest towers after you have modified each tower.
You can find a slight modification of the problem here.
Note: It is compulsory to increase or decrease the height by K for each tower. After the operation, the resultant array should not contain any negative integers.
Example 1:
Input:
K = 2, N = 4
Arr[] = {1, 5, 8, 10}
Output:
5
Explanation:
The array can be modified as
{1+k, 5-k, 8-k, 10-k} = {3, 3, 6, 8}.
The difference between
the largest and the smallest is 8-3 = 5.
Example 2:
Input:
K = 3, N = 5
Arr[] = {3, 9, 12, 16, 20}
Output:
11
Explanation:
The array can be modified as
{3+k, 9+k, 12-k, 16-k, 20-k} -> {6, 12, 9, 13, 17}.
The difference between
the largest and the smallest is 17-6 = 11.
Your Task:
You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n, and k as input parameters and returns an integer denoting the minimum difference.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints
1 β€ K β€ 10^{4}
1 β€ N β€ 10^{5}
1 β€ Arr[i] β€ 10^{5} | class Solution:
def getMinDiff(self, arr, n, k):
arr.sort()
res = arr[n - 1] - arr[0]
mintemp = arr[0]
maxtemp = arr[n - 1]
for i in range(1, n):
if arr[i] < k or arr[n - 1] < k:
continue
mintemp = min(arr[0] + k, arr[i] - k)
maxtemp = max(arr[i - 1] + k, arr[n - 1] - k)
res = min(res, maxtemp - mintemp)
return res | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR |
Given an array arr[] denoting heights of N towers and a positive integer K.
For each tower, you must perform exactly one of the following operations exactly once.
Increase the height of the tower by K
Decrease the height of the tower by K
Find out the minimum possible difference between the height of the shortest and tallest towers after you have modified each tower.
You can find a slight modification of the problem here.
Note: It is compulsory to increase or decrease the height by K for each tower. After the operation, the resultant array should not contain any negative integers.
Example 1:
Input:
K = 2, N = 4
Arr[] = {1, 5, 8, 10}
Output:
5
Explanation:
The array can be modified as
{1+k, 5-k, 8-k, 10-k} = {3, 3, 6, 8}.
The difference between
the largest and the smallest is 8-3 = 5.
Example 2:
Input:
K = 3, N = 5
Arr[] = {3, 9, 12, 16, 20}
Output:
11
Explanation:
The array can be modified as
{3+k, 9+k, 12-k, 16-k, 20-k} -> {6, 12, 9, 13, 17}.
The difference between
the largest and the smallest is 17-6 = 11.
Your Task:
You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n, and k as input parameters and returns an integer denoting the minimum difference.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints
1 β€ K β€ 10^{4}
1 β€ N β€ 10^{5}
1 β€ Arr[i] β€ 10^{5} | class Solution:
def getMinDiff(self, arr, n, k):
arr.sort()
l = arr[0] + k
r = arr[n - 1] - k
res = arr[n - 1] - arr[0]
for i in range(1, n):
if arr[i] - k < 0:
continue
maxi = max(r, arr[i - 1] + k)
mini = min(l, arr[i] - k)
res = min(res, maxi - mini)
return res | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR |
Given an array arr[] denoting heights of N towers and a positive integer K.
For each tower, you must perform exactly one of the following operations exactly once.
Increase the height of the tower by K
Decrease the height of the tower by K
Find out the minimum possible difference between the height of the shortest and tallest towers after you have modified each tower.
You can find a slight modification of the problem here.
Note: It is compulsory to increase or decrease the height by K for each tower. After the operation, the resultant array should not contain any negative integers.
Example 1:
Input:
K = 2, N = 4
Arr[] = {1, 5, 8, 10}
Output:
5
Explanation:
The array can be modified as
{1+k, 5-k, 8-k, 10-k} = {3, 3, 6, 8}.
The difference between
the largest and the smallest is 8-3 = 5.
Example 2:
Input:
K = 3, N = 5
Arr[] = {3, 9, 12, 16, 20}
Output:
11
Explanation:
The array can be modified as
{3+k, 9+k, 12-k, 16-k, 20-k} -> {6, 12, 9, 13, 17}.
The difference between
the largest and the smallest is 17-6 = 11.
Your Task:
You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n, and k as input parameters and returns an integer denoting the minimum difference.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints
1 β€ K β€ 10^{4}
1 β€ N β€ 10^{5}
1 β€ Arr[i] β€ 10^{5} | class Solution:
def getMinDiff(self, arr, n, k):
arr.sort()
ans = arr[n - 1] - arr[0]
for i in range(1, n):
if arr[i] < k:
continue
small_tower = min(arr[0] + k, arr[i] - k)
high_tower = max(arr[n - 1] - k, arr[i - 1] + k)
ans = min(ans, high_tower - small_tower)
return ans | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR |
Given an array arr[] denoting heights of N towers and a positive integer K.
For each tower, you must perform exactly one of the following operations exactly once.
Increase the height of the tower by K
Decrease the height of the tower by K
Find out the minimum possible difference between the height of the shortest and tallest towers after you have modified each tower.
You can find a slight modification of the problem here.
Note: It is compulsory to increase or decrease the height by K for each tower. After the operation, the resultant array should not contain any negative integers.
Example 1:
Input:
K = 2, N = 4
Arr[] = {1, 5, 8, 10}
Output:
5
Explanation:
The array can be modified as
{1+k, 5-k, 8-k, 10-k} = {3, 3, 6, 8}.
The difference between
the largest and the smallest is 8-3 = 5.
Example 2:
Input:
K = 3, N = 5
Arr[] = {3, 9, 12, 16, 20}
Output:
11
Explanation:
The array can be modified as
{3+k, 9+k, 12-k, 16-k, 20-k} -> {6, 12, 9, 13, 17}.
The difference between
the largest and the smallest is 17-6 = 11.
Your Task:
You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n, and k as input parameters and returns an integer denoting the minimum difference.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints
1 β€ K β€ 10^{4}
1 β€ N β€ 10^{5}
1 β€ Arr[i] β€ 10^{5} | class Solution:
def getMinDiff(self, arr, n, k):
arr.sort()
if n == 1:
return 0
ans = arr[-1] - arr[0]
lar = arr[-1] - k
sm = arr[0] + k
for i in range(n - 1):
a = min(sm, arr[i + 1] - k)
b = max(lar, arr[i] + k)
if a < 0:
continue
ans = min(ans, b - a)
return ans | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR |
Given an array arr[] denoting heights of N towers and a positive integer K.
For each tower, you must perform exactly one of the following operations exactly once.
Increase the height of the tower by K
Decrease the height of the tower by K
Find out the minimum possible difference between the height of the shortest and tallest towers after you have modified each tower.
You can find a slight modification of the problem here.
Note: It is compulsory to increase or decrease the height by K for each tower. After the operation, the resultant array should not contain any negative integers.
Example 1:
Input:
K = 2, N = 4
Arr[] = {1, 5, 8, 10}
Output:
5
Explanation:
The array can be modified as
{1+k, 5-k, 8-k, 10-k} = {3, 3, 6, 8}.
The difference between
the largest and the smallest is 8-3 = 5.
Example 2:
Input:
K = 3, N = 5
Arr[] = {3, 9, 12, 16, 20}
Output:
11
Explanation:
The array can be modified as
{3+k, 9+k, 12-k, 16-k, 20-k} -> {6, 12, 9, 13, 17}.
The difference between
the largest and the smallest is 17-6 = 11.
Your Task:
You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n, and k as input parameters and returns an integer denoting the minimum difference.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints
1 β€ K β€ 10^{4}
1 β€ N β€ 10^{5}
1 β€ Arr[i] β€ 10^{5} | class Solution:
def getMinDiff(self, arr, n, k):
arr.sort()
ans = arr[-1] - arr[0]
for i in range(1, n):
x = max(arr[0] + k, arr[i - 1] + k, arr[i] - k, arr[-1] - k)
y = min(arr[0] + k, arr[i - 1] + k, arr[i] - k, arr[-1] - k)
if y >= 0:
ans = min(ans, x - y)
return ans | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR |
Given an array arr[] denoting heights of N towers and a positive integer K.
For each tower, you must perform exactly one of the following operations exactly once.
Increase the height of the tower by K
Decrease the height of the tower by K
Find out the minimum possible difference between the height of the shortest and tallest towers after you have modified each tower.
You can find a slight modification of the problem here.
Note: It is compulsory to increase or decrease the height by K for each tower. After the operation, the resultant array should not contain any negative integers.
Example 1:
Input:
K = 2, N = 4
Arr[] = {1, 5, 8, 10}
Output:
5
Explanation:
The array can be modified as
{1+k, 5-k, 8-k, 10-k} = {3, 3, 6, 8}.
The difference between
the largest and the smallest is 8-3 = 5.
Example 2:
Input:
K = 3, N = 5
Arr[] = {3, 9, 12, 16, 20}
Output:
11
Explanation:
The array can be modified as
{3+k, 9+k, 12-k, 16-k, 20-k} -> {6, 12, 9, 13, 17}.
The difference between
the largest and the smallest is 17-6 = 11.
Your Task:
You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n, and k as input parameters and returns an integer denoting the minimum difference.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints
1 β€ K β€ 10^{4}
1 β€ N β€ 10^{5}
1 β€ Arr[i] β€ 10^{5} | class Solution:
def getMinDiff(self, arr, n, k):
arr = sorted(arr)
minDiff = arr[n - 1] - arr[0]
if arr[n - 1] - k <= 0:
return minDiff
for i in range(1, n):
if arr[i] - k >= 0:
minH = min(arr[0] + k, arr[i] - k)
maxH = max(arr[n - 1] - k, arr[i - 1] + k)
minDiff = min(minDiff, maxH - minH)
return minDiff | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR |
Given an array arr[] denoting heights of N towers and a positive integer K.
For each tower, you must perform exactly one of the following operations exactly once.
Increase the height of the tower by K
Decrease the height of the tower by K
Find out the minimum possible difference between the height of the shortest and tallest towers after you have modified each tower.
You can find a slight modification of the problem here.
Note: It is compulsory to increase or decrease the height by K for each tower. After the operation, the resultant array should not contain any negative integers.
Example 1:
Input:
K = 2, N = 4
Arr[] = {1, 5, 8, 10}
Output:
5
Explanation:
The array can be modified as
{1+k, 5-k, 8-k, 10-k} = {3, 3, 6, 8}.
The difference between
the largest and the smallest is 8-3 = 5.
Example 2:
Input:
K = 3, N = 5
Arr[] = {3, 9, 12, 16, 20}
Output:
11
Explanation:
The array can be modified as
{3+k, 9+k, 12-k, 16-k, 20-k} -> {6, 12, 9, 13, 17}.
The difference between
the largest and the smallest is 17-6 = 11.
Your Task:
You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n, and k as input parameters and returns an integer denoting the minimum difference.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints
1 β€ K β€ 10^{4}
1 β€ N β€ 10^{5}
1 β€ Arr[i] β€ 10^{5} | class Solution:
def getMinDiff(self, arr, n, k):
if n == 1:
return 0
arr.sort()
initialDiff = arr[-1] - arr[0]
for i in range(1, n):
if arr[i] < k:
continue
newMin = min(arr[0] + k, arr[i] - k)
newMax = max(arr[i - 1] + k, arr[-1] - k)
newDiff = newMax - newMin
if newDiff < initialDiff:
initialDiff = newDiff
return initialDiff | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR |
Given an array arr[] denoting heights of N towers and a positive integer K.
For each tower, you must perform exactly one of the following operations exactly once.
Increase the height of the tower by K
Decrease the height of the tower by K
Find out the minimum possible difference between the height of the shortest and tallest towers after you have modified each tower.
You can find a slight modification of the problem here.
Note: It is compulsory to increase or decrease the height by K for each tower. After the operation, the resultant array should not contain any negative integers.
Example 1:
Input:
K = 2, N = 4
Arr[] = {1, 5, 8, 10}
Output:
5
Explanation:
The array can be modified as
{1+k, 5-k, 8-k, 10-k} = {3, 3, 6, 8}.
The difference between
the largest and the smallest is 8-3 = 5.
Example 2:
Input:
K = 3, N = 5
Arr[] = {3, 9, 12, 16, 20}
Output:
11
Explanation:
The array can be modified as
{3+k, 9+k, 12-k, 16-k, 20-k} -> {6, 12, 9, 13, 17}.
The difference between
the largest and the smallest is 17-6 = 11.
Your Task:
You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n, and k as input parameters and returns an integer denoting the minimum difference.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints
1 β€ K β€ 10^{4}
1 β€ N β€ 10^{5}
1 β€ Arr[i] β€ 10^{5} | class Solution:
def getMinDiff(self, arr, n, k):
arr.sort()
min_element = arr[0]
max_element = arr[n - 1]
result = max_element - min_element
for i in range(1, n):
if arr[i] - k < 0:
continue
max_element = max(arr[i - 1] + k, arr[n - 1] - k)
min_element = min(arr[i] - k, arr[0] + k)
ans = max_element - min_element
result = min(result, ans)
return result | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array arr[] denoting heights of N towers and a positive integer K.
For each tower, you must perform exactly one of the following operations exactly once.
Increase the height of the tower by K
Decrease the height of the tower by K
Find out the minimum possible difference between the height of the shortest and tallest towers after you have modified each tower.
You can find a slight modification of the problem here.
Note: It is compulsory to increase or decrease the height by K for each tower. After the operation, the resultant array should not contain any negative integers.
Example 1:
Input:
K = 2, N = 4
Arr[] = {1, 5, 8, 10}
Output:
5
Explanation:
The array can be modified as
{1+k, 5-k, 8-k, 10-k} = {3, 3, 6, 8}.
The difference between
the largest and the smallest is 8-3 = 5.
Example 2:
Input:
K = 3, N = 5
Arr[] = {3, 9, 12, 16, 20}
Output:
11
Explanation:
The array can be modified as
{3+k, 9+k, 12-k, 16-k, 20-k} -> {6, 12, 9, 13, 17}.
The difference between
the largest and the smallest is 17-6 = 11.
Your Task:
You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n, and k as input parameters and returns an integer denoting the minimum difference.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints
1 β€ K β€ 10^{4}
1 β€ N β€ 10^{5}
1 β€ Arr[i] β€ 10^{5} | class Solution:
def getMinDiff(self, arr, n, k):
arr.sort()
diff = arr[n - 1] - arr[0]
if n == 1:
return 0
for i in range(1, n):
if arr[i] - k < 0:
continue
Max = max(arr[i - 1] + k, arr[n - 1] - k)
Min = min(arr[0] + k, arr[i] - k)
diff = min(diff, Max - Min)
return diff | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR |
Given an array arr[] denoting heights of N towers and a positive integer K.
For each tower, you must perform exactly one of the following operations exactly once.
Increase the height of the tower by K
Decrease the height of the tower by K
Find out the minimum possible difference between the height of the shortest and tallest towers after you have modified each tower.
You can find a slight modification of the problem here.
Note: It is compulsory to increase or decrease the height by K for each tower. After the operation, the resultant array should not contain any negative integers.
Example 1:
Input:
K = 2, N = 4
Arr[] = {1, 5, 8, 10}
Output:
5
Explanation:
The array can be modified as
{1+k, 5-k, 8-k, 10-k} = {3, 3, 6, 8}.
The difference between
the largest and the smallest is 8-3 = 5.
Example 2:
Input:
K = 3, N = 5
Arr[] = {3, 9, 12, 16, 20}
Output:
11
Explanation:
The array can be modified as
{3+k, 9+k, 12-k, 16-k, 20-k} -> {6, 12, 9, 13, 17}.
The difference between
the largest and the smallest is 17-6 = 11.
Your Task:
You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n, and k as input parameters and returns an integer denoting the minimum difference.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints
1 β€ K β€ 10^{4}
1 β€ N β€ 10^{5}
1 β€ Arr[i] β€ 10^{5} | class Solution:
def getMinDiff(self, arr, n, k):
if n == 1:
return 0
arr.sort()
smallest = arr[0]
largest = arr[n - 1]
ans = arr[n - 1] - arr[0]
for i in range(1, len(arr)):
if arr[i] - k < 0:
continue
mi = min(arr[0] + k, arr[i] - k)
ma = max(arr[i - 1] + k, arr[n - 1] - k)
ans = min(ans, ma - mi)
return ans | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR |
Given an array arr[] denoting heights of N towers and a positive integer K.
For each tower, you must perform exactly one of the following operations exactly once.
Increase the height of the tower by K
Decrease the height of the tower by K
Find out the minimum possible difference between the height of the shortest and tallest towers after you have modified each tower.
You can find a slight modification of the problem here.
Note: It is compulsory to increase or decrease the height by K for each tower. After the operation, the resultant array should not contain any negative integers.
Example 1:
Input:
K = 2, N = 4
Arr[] = {1, 5, 8, 10}
Output:
5
Explanation:
The array can be modified as
{1+k, 5-k, 8-k, 10-k} = {3, 3, 6, 8}.
The difference between
the largest and the smallest is 8-3 = 5.
Example 2:
Input:
K = 3, N = 5
Arr[] = {3, 9, 12, 16, 20}
Output:
11
Explanation:
The array can be modified as
{3+k, 9+k, 12-k, 16-k, 20-k} -> {6, 12, 9, 13, 17}.
The difference between
the largest and the smallest is 17-6 = 11.
Your Task:
You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n, and k as input parameters and returns an integer denoting the minimum difference.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints
1 β€ K β€ 10^{4}
1 β€ N β€ 10^{5}
1 β€ Arr[i] β€ 10^{5} | class Solution:
def getMinDiff(self, arr, n, k):
arr.sort()
sm = arr[0] + k
lg = arr[-1] - k
diff = arr[-1] - arr[0]
for i in range(n - 1):
if arr[i + 1] >= k:
mx = max(lg, arr[i] + k)
mn = min(sm, arr[i + 1] - k)
diff = min(mx - mn, diff)
return diff | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Today is Devu's birthday. He has obtained few colored balloons from his friends. You are given this information by a string s consisting of lower case English Latin letters. Each letter (from 'a' to 'z') denotes a color. e.g. if s = "aab", then it means that he has received two balloons of color 'a' whereas one balloon of color 'b'.
Now, Devu wants to decorate the cake by arranging all the balloons linearly from left to right on the cake such that no same colored balloons are nearby/ adjacent to each other.
Now Devu wonders whether it is possible to do so or not? Please help him in this. If it is not possible to do so, print -1. Otherwise, print any one of arrangements of the balloons on the cake. If there are more than one possible ways of doing so, you can print any one of them.
------ Input ------
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
First line of each test case will contain string s
------ Output ------
Print a single line corresponding to the answer of the problem.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ size of string s β€ 10^{5}$
$Sum of size of string s over all test cases will be less than or equal to β€ 10^{6}$
----- Sample Input 1 ------
3
aab
ab
aa
----- Sample Output 1 ------
aba
ab
-1
----- explanation 1 ------
Example case 1. He can arrange the balloons in the order given by the following string "aba".
Example case 2. He can arrange the balloons in the order given by the following string "ab"
Example case 3. There is no valid way of decorating cakes with balloon in the desired way. So we print -1. | t = int(input())
for _ in range(t):
s = input()
hash = {}
yo = ""
maxi = -10
for i in s:
try:
hash[i]
except:
hash[i] = 1
else:
hash[i] += 1
ans = []
j = 0
for i in hash.keys():
ans.append(list(i * hash[i]))
j += 1
k = []
i = 0
j = 0
count = 0
while True:
if i < len(ans[j]):
k.append(ans[j][i])
count += 1
if count == len(s):
break
if j + 1 >= len(ans):
j = 0
i += 1
else:
j += 1
bo = []
ha = 0
ka = ""
for i in range(len(k) - 1):
if k[i] == k[i + 1]:
ha = i
ka = k[i]
break
count = len(k[i:])
bo = k[:i]
yo = []
la = 0
for i in range(len(bo) - 1):
if bo[i] != ka and bo[i + 1] != ka and la < count:
yo.append(bo[i])
yo.append(ka)
la += 1
else:
yo.append(bo[i])
if bo != []:
yo.append(bo[-1])
if len(yo) < len(k):
if ka != yo[0]:
yo.insert(0, ka)
if len(yo) < len(k):
if ka != yo[-1]:
yo.insert(len(yo), ka)
flag = 0
flag1 = 0
for i in range(len(k) - 1):
if k[i] != k[i + 1]:
continue
else:
flag = 1
break
if flag == 1:
for i in range(len(yo) - 1):
if yo[i] != yo[i + 1]:
continue
else:
flag1 = 1
break
if flag1 == 1 or len(yo) != len(k):
print(-1)
else:
print("".join(yo))
else:
print("".join(k)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR EXPR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Today is Devu's birthday. He has obtained few colored balloons from his friends. You are given this information by a string s consisting of lower case English Latin letters. Each letter (from 'a' to 'z') denotes a color. e.g. if s = "aab", then it means that he has received two balloons of color 'a' whereas one balloon of color 'b'.
Now, Devu wants to decorate the cake by arranging all the balloons linearly from left to right on the cake such that no same colored balloons are nearby/ adjacent to each other.
Now Devu wonders whether it is possible to do so or not? Please help him in this. If it is not possible to do so, print -1. Otherwise, print any one of arrangements of the balloons on the cake. If there are more than one possible ways of doing so, you can print any one of them.
------ Input ------
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
First line of each test case will contain string s
------ Output ------
Print a single line corresponding to the answer of the problem.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ size of string s β€ 10^{5}$
$Sum of size of string s over all test cases will be less than or equal to β€ 10^{6}$
----- Sample Input 1 ------
3
aab
ab
aa
----- Sample Output 1 ------
aba
ab
-1
----- explanation 1 ------
Example case 1. He can arrange the balloons in the order given by the following string "aba".
Example case 2. He can arrange the balloons in the order given by the following string "ab"
Example case 3. There is no valid way of decorating cakes with balloon in the desired way. So we print -1. | for _ in range(int(input())):
s = list(input())
s.sort()
n = len(s)
s.append("0")
l = []
a, c = s[0], 1
for i in range(1, n + 1):
if s[i] != s[i - 1]:
l.append([a, c])
a, c = s[i], 1
else:
c += 1
s1 = ""
a = max(l, key=lambda x: x[1])
a1 = l.index(a)
while len(l) > 1:
p = a[:]
s1 += p[0]
p[1] -= 1
del l[a1]
a = max(l, key=lambda x: x[1])
a1 = l.index(a)
if p[1] > 0:
l.append(p)
if l[0][1] == 1:
s1 += l[0][0]
print(s1)
else:
print(-1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Ivan has n different boxes. The first of them contains some balls of n different colors.
Ivan wants to play a strange game. He wants to distribute the balls into boxes in such a way that for every i (1 β€ i β€ n) i-th box will contain all balls with color i.
In order to do this, Ivan will make some turns. Each turn he does the following: Ivan chooses any non-empty box and takes all balls from this box; Then Ivan chooses any k empty boxes (the box from the first step becomes empty, and Ivan is allowed to choose it), separates the balls he took on the previous step into k non-empty groups and puts each group into one of the boxes. He should put each group into a separate box. He can choose either k = 2 or k = 3.
The penalty of the turn is the number of balls Ivan takes from the box during the first step of the turn. And penalty of the game is the total penalty of turns made by Ivan until he distributes all balls to corresponding boxes.
Help Ivan to determine the minimum possible penalty of the game!
-----Input-----
The first line contains one integer number n (1 β€ n β€ 200000) β the number of boxes and colors.
The second line contains n integer numbers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9), where a_{i} is the number of balls with color i.
-----Output-----
Print one number β the minimum possible penalty of the game.
-----Examples-----
Input
3
1 2 3
Output
6
Input
4
2 3 4 5
Output
19
-----Note-----
In the first example you take all the balls from the first box, choose k = 3 and sort all colors to corresponding boxes. Penalty is 6.
In the second example you make two turns: Take all the balls from the first box, choose k = 3, put balls of color 3 to the third box, of color 4 β to the fourth box and the rest put back into the first box. Penalty is 14; Take all the balls from the first box, choose k = 2, put balls of color 1 to the first box, of color 2 β to the second box. Penalty is 5.
Total penalty is 19. | class heap:
def __init__(self, maxn):
self.a = [0] * maxn
self.size = 0
def shift_down(self, i):
while 2 * i + 1 < self.size:
l = 2 * i + 1
r = 2 * i + 2
j = l
if r < self.size and self.a[r] < self.a[l]:
j = r
if self.a[i] <= self.a[j]:
break
self.a[i], self.a[j] = self.a[j], self.a[i]
i = j
def shift_up(self, i):
while i and self.a[i] < self.a[(i - 1) // 2]:
self.a[i], self.a[(i - 1) // 2] = self.a[(i - 1) // 2], self.a[i]
i = (i - 1) // 2
def erase_min(self):
mn = self.a[0]
self.a[0] = self.a[self.size - 1]
self.size -= 1
self.shift_down(0)
return mn
def insert(self, val):
self.size += 1
self.a[self.size - 1] = val
self.shift_up(self.size - 1)
n = int(input())
ans = 0
s = heap(400000 + 100)
for i in [int(j) for j in input().split()]:
s.insert(i)
if s.size % 2 == 0:
s.insert(0)
while s.size > 1:
t = s.erase_min() + s.erase_min()
if s.size:
t += s.erase_min()
ans += t
s.insert(t)
print(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FUNC_DEF WHILE BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_DEF WHILE VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR FUNC_DEF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Vasya is pressing the keys on the keyboard reluctantly, squeezing out his ideas on the classical epos depicted in Homer's Odysseus... How can he explain to his literature teacher that he isn't going to become a writer? In fact, he is going to become a programmer. So, he would take great pleasure in writing a program, but none β in writing a composition.
As Vasya was fishing for a sentence in the dark pond of his imagination, he suddenly wondered: what is the least number of times he should push a key to shift the cursor from one position to another one?
Let's describe his question more formally: to type a text, Vasya is using the text editor. He has already written n lines, the i-th line contains a_{i} characters (including spaces). If some line contains k characters, then this line overall contains (k + 1) positions where the cursor can stand: before some character or after all characters (at the end of the line). Thus, the cursor's position is determined by a pair of integers (r, c), where r is the number of the line and c is the cursor's position in the line (the positions are indexed starting from one from the beginning of the line).
Vasya doesn't use the mouse to move the cursor. He uses keys "Up", "Down", "Right" and "Left". When he pushes each of these keys, the cursor shifts in the needed direction. Let's assume that before the corresponding key is pressed, the cursor was located in the position (r, c), then Vasya pushed key: "Up": if the cursor was located in the first line (r = 1), then it does not move. Otherwise, it moves to the previous line (with number r - 1), to the same position. At that, if the previous line was short, that is, the cursor couldn't occupy position c there, the cursor moves to the last position of the line with number r - 1; "Down": if the cursor was located in the last line (r = n), then it does not move. Otherwise, it moves to the next line (with number r + 1), to the same position. At that, if the next line was short, that is, the cursor couldn't occupy position c there, the cursor moves to the last position of the line with number r + 1; "Right": if the cursor can move to the right in this line (c < a_{r} + 1), then it moves to the right (to position c + 1). Otherwise, it is located at the end of the line and doesn't move anywhere when Vasya presses the "Right" key; "Left": if the cursor can move to the left in this line (c > 1), then it moves to the left (to position c - 1). Otherwise, it is located at the beginning of the line and doesn't move anywhere when Vasya presses the "Left" key.
You've got the number of lines in the text file and the number of characters, written in each line of this file. Find the least number of times Vasya should push the keys, described above, to shift the cursor from position (r_1, c_1) to position (r_2, c_2).
-----Input-----
The first line of the input contains an integer n (1 β€ n β€ 100) β the number of lines in the file. The second line contains n integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 10^5), separated by single spaces. The third line contains four integers r_1, c_1, r_2, c_2 (1 β€ r_1, r_2 β€ n, 1 β€ c_1 β€ a_{r}_1 + 1, 1 β€ c_2 β€ a_{r}_2 + 1).
-----Output-----
Print a single integer β the minimum number of times Vasya should push a key to move the cursor from position (r_1, c_1) to position (r_2, c_2).
-----Examples-----
Input
4
2 1 6 4
3 4 4 2
Output
3
Input
4
10 5 6 4
1 11 4 2
Output
6
Input
3
10 1 10
1 10 1 1
Output
3
-----Note-----
In the first sample the editor contains four lines. Let's represent the cursor's possible positions in the line as numbers. Letter s represents the cursor's initial position, letter t represents the last one. Then all possible positions of the cursor in the text editor are described by the following table.
123
12
123s567
1t345
One of the possible answers in the given sample is: "Left", "Down", "Left". | import sys
sys.stdin = open("input.txt")
sys.stdout = open("output.txt", "w")
n = int(input())
a = [int(x) for x in input().split()]
[r1, c1, r2, c2] = [int(x) for x in input().split()]
r1 -= 1
r2 -= 1
c1 -= 1
c2 -= 1
dr = r2 - r1
ddr = dr // abs(dr) if dr != 0 else 1
c = c1
for i in range(abs(dr) + 1):
r = r1 + ddr * i
c = min(c, a[r])
pen1 = 0
for i in range(min(r1, r2)):
pen = (min(r1, r2) - i) * 2
if c > c2 and a[i] < c and a[i] <= min(a[i : min(r1, r2)]):
pen -= c - c2 - abs(a[i] - c2)
pen1 = min(pen1, pen)
pen2 = 0
for i in range(max(r1, r2) + 1, n):
pen = (i - max(r1, r2)) * 2
if c > c2 and a[i] < c and a[i] <= min(a[max(r1, r2) : i]):
pen -= c - c2 - abs(a[i] - c2)
pen2 = min(pen2, pen)
pen = abs(dr) + abs(c - c2)
print(min(pen + pen1, pen + pen2)) | IMPORT ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN LIST VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR |
Vasya is pressing the keys on the keyboard reluctantly, squeezing out his ideas on the classical epos depicted in Homer's Odysseus... How can he explain to his literature teacher that he isn't going to become a writer? In fact, he is going to become a programmer. So, he would take great pleasure in writing a program, but none β in writing a composition.
As Vasya was fishing for a sentence in the dark pond of his imagination, he suddenly wondered: what is the least number of times he should push a key to shift the cursor from one position to another one?
Let's describe his question more formally: to type a text, Vasya is using the text editor. He has already written n lines, the i-th line contains a_{i} characters (including spaces). If some line contains k characters, then this line overall contains (k + 1) positions where the cursor can stand: before some character or after all characters (at the end of the line). Thus, the cursor's position is determined by a pair of integers (r, c), where r is the number of the line and c is the cursor's position in the line (the positions are indexed starting from one from the beginning of the line).
Vasya doesn't use the mouse to move the cursor. He uses keys "Up", "Down", "Right" and "Left". When he pushes each of these keys, the cursor shifts in the needed direction. Let's assume that before the corresponding key is pressed, the cursor was located in the position (r, c), then Vasya pushed key: "Up": if the cursor was located in the first line (r = 1), then it does not move. Otherwise, it moves to the previous line (with number r - 1), to the same position. At that, if the previous line was short, that is, the cursor couldn't occupy position c there, the cursor moves to the last position of the line with number r - 1; "Down": if the cursor was located in the last line (r = n), then it does not move. Otherwise, it moves to the next line (with number r + 1), to the same position. At that, if the next line was short, that is, the cursor couldn't occupy position c there, the cursor moves to the last position of the line with number r + 1; "Right": if the cursor can move to the right in this line (c < a_{r} + 1), then it moves to the right (to position c + 1). Otherwise, it is located at the end of the line and doesn't move anywhere when Vasya presses the "Right" key; "Left": if the cursor can move to the left in this line (c > 1), then it moves to the left (to position c - 1). Otherwise, it is located at the beginning of the line and doesn't move anywhere when Vasya presses the "Left" key.
You've got the number of lines in the text file and the number of characters, written in each line of this file. Find the least number of times Vasya should push the keys, described above, to shift the cursor from position (r_1, c_1) to position (r_2, c_2).
-----Input-----
The first line of the input contains an integer n (1 β€ n β€ 100) β the number of lines in the file. The second line contains n integers a_1, a_2, ..., a_{n} (0 β€ a_{i} β€ 10^5), separated by single spaces. The third line contains four integers r_1, c_1, r_2, c_2 (1 β€ r_1, r_2 β€ n, 1 β€ c_1 β€ a_{r}_1 + 1, 1 β€ c_2 β€ a_{r}_2 + 1).
-----Output-----
Print a single integer β the minimum number of times Vasya should push a key to move the cursor from position (r_1, c_1) to position (r_2, c_2).
-----Examples-----
Input
4
2 1 6 4
3 4 4 2
Output
3
Input
4
10 5 6 4
1 11 4 2
Output
6
Input
3
10 1 10
1 10 1 1
Output
3
-----Note-----
In the first sample the editor contains four lines. Let's represent the cursor's possible positions in the line as numbers. Letter s represents the cursor's initial position, letter t represents the last one. Then all possible positions of the cursor in the text editor are described by the following table.
123
12
123s567
1t345
One of the possible answers in the given sample is: "Left", "Down", "Left". | import sys
sys.stdin = open("input.txt")
sys.stdout = open("output.txt", "w")
n = int(input())
a = list(map(int, input().split()))
r1, c1, r2, c2 = (i - 1 for i in map(int, input().split()))
x, y = (r1, r2) if r1 < r2 else (r2, r1)
if r1 < r2:
c1 = min(c1, min(a[i] for i in range(r1 + 1, r2 + 1)))
elif r2 < r1:
c1 = min(c1, min(a[i] for i in range(r2, r1)))
d = abs(c1 - c2)
c = c1
for i in range(x - 1, -1, -1):
if c > a[i]:
c = a[i]
q = abs(c2 - c) + 2 * abs(x - i)
if q < d:
d = q
c = c1
for i in range(y + 1, n):
if c > a[i]:
c = a[i]
q = abs(c2 - c) + 2 * abs(i - y)
if q < d:
d = q
print(d + abs(r2 - r1)) | IMPORT ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING 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 VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR |
The only difference between easy and hard versions is the length of the string. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems.
Kirk has a binary string $s$ (a string which consists of zeroes and ones) of length $n$ and he is asking you to find a binary string $t$ of the same length which satisfies the following conditions: For any $l$ and $r$ ($1 \leq l \leq r \leq n$) the length of the longest non-decreasing subsequence of the substring $s_{l}s_{l+1} \ldots s_{r}$ is equal to the length of the longest non-decreasing subsequence of the substring $t_{l}t_{l+1} \ldots t_{r}$; The number of zeroes in $t$ is the maximum possible.
A non-decreasing subsequence of a string $p$ is a sequence of indices $i_1, i_2, \ldots, i_k$ such that $i_1 < i_2 < \ldots < i_k$ and $p_{i_1} \leq p_{i_2} \leq \ldots \leq p_{i_k}$. The length of the subsequence is $k$.
If there are multiple substrings which satisfy the conditions, output any.
-----Input-----
The first line contains a binary string of length not more than $10^5$.
-----Output-----
Output a binary string which satisfied the above conditions. If there are many such strings, output any of them.
-----Examples-----
Input
110
Output
010
Input
010
Output
010
Input
0001111
Output
0000000
Input
0111001100111011101000
Output
0011001100001011101000
-----Note-----
In the first example: For the substrings of the length $1$ the length of the longest non-decreasing subsequnce is $1$; For $l = 1, r = 2$ the longest non-decreasing subsequnce of the substring $s_{1}s_{2}$ is $11$ and the longest non-decreasing subsequnce of the substring $t_{1}t_{2}$ is $01$; For $l = 1, r = 3$ the longest non-decreasing subsequnce of the substring $s_{1}s_{3}$ is $11$ and the longest non-decreasing subsequnce of the substring $t_{1}t_{3}$ is $00$; For $l = 2, r = 3$ the longest non-decreasing subsequnce of the substring $s_{2}s_{3}$ is $1$ and the longest non-decreasing subsequnce of the substring $t_{2}t_{3}$ is $1$;
The second example is similar to the first one. | import sys
INF = 10**10
def main():
print = out.append
st = list(input())
stk = []
for index, i in enumerate(st):
if i == "0" and len(stk) > 0 and stk[-1][0] == "1":
stk.pop()
else:
stk.append([i, index])
for li in stk:
st[li[1]] = "0"
print("".join(st))
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
out = []
get_int = lambda: int(input())
get_list = lambda: list(map(int, input().split()))
main()
print(*out, sep="\n") | IMPORT ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING |
The only difference between easy and hard versions is the length of the string. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems.
Kirk has a binary string $s$ (a string which consists of zeroes and ones) of length $n$ and he is asking you to find a binary string $t$ of the same length which satisfies the following conditions: For any $l$ and $r$ ($1 \leq l \leq r \leq n$) the length of the longest non-decreasing subsequence of the substring $s_{l}s_{l+1} \ldots s_{r}$ is equal to the length of the longest non-decreasing subsequence of the substring $t_{l}t_{l+1} \ldots t_{r}$; The number of zeroes in $t$ is the maximum possible.
A non-decreasing subsequence of a string $p$ is a sequence of indices $i_1, i_2, \ldots, i_k$ such that $i_1 < i_2 < \ldots < i_k$ and $p_{i_1} \leq p_{i_2} \leq \ldots \leq p_{i_k}$. The length of the subsequence is $k$.
If there are multiple substrings which satisfy the conditions, output any.
-----Input-----
The first line contains a binary string of length not more than $10^5$.
-----Output-----
Output a binary string which satisfied the above conditions. If there are many such strings, output any of them.
-----Examples-----
Input
110
Output
010
Input
010
Output
010
Input
0001111
Output
0000000
Input
0111001100111011101000
Output
0011001100001011101000
-----Note-----
In the first example: For the substrings of the length $1$ the length of the longest non-decreasing subsequnce is $1$; For $l = 1, r = 2$ the longest non-decreasing subsequnce of the substring $s_{1}s_{2}$ is $11$ and the longest non-decreasing subsequnce of the substring $t_{1}t_{2}$ is $01$; For $l = 1, r = 3$ the longest non-decreasing subsequnce of the substring $s_{1}s_{3}$ is $11$ and the longest non-decreasing subsequnce of the substring $t_{1}t_{3}$ is $00$; For $l = 2, r = 3$ the longest non-decreasing subsequnce of the substring $s_{2}s_{3}$ is $1$ and the longest non-decreasing subsequnce of the substring $t_{2}t_{3}$ is $1$;
The second example is similar to the first one. | S = list(map(int, input().strip()))
N = len(S)
stack = []
for i in range(N):
s = S[i]
if s == 0 and stack and stack[-1][0] == 1:
stack.pop()
else:
stack.append((s, i))
T = S[:]
if stack:
for i in tuple(map(list, zip(*stack)))[1]:
T[i] = 0
print("".join(map(str, T))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given an array $a$ of length $n$, which initially is a permutation of numbers from $1$ to $n$. In one operation, you can choose an index $i$ ($1 \leq i < n$) such that $a_i < a_{i + 1}$, and remove either $a_i$ or $a_{i + 1}$ from the array (after the removal, the remaining parts are concatenated).
For example, if you have the array $[1, 3, 2]$, you can choose $i = 1$ (since $a_1 = 1 < a_2 = 3$), then either remove $a_1$ which gives the new array $[3, 2]$, or remove $a_2$ which gives the new array $[1, 2]$.
Is it possible to make the length of this array equal to $1$ with these operations?
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 3 \cdot 10^5$) Β β the length of the array.
The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \leq a_i \leq n$, $a_i$ are pairwise distinct)Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each test case, output on a single line the word "YES" if it is possible to reduce the array to a single element using the aforementioned operation, or "NO" if it is impossible to do so.
-----Example-----
Input
4
3
1 2 3
4
3 1 2 4
3
2 3 1
6
2 4 6 1 3 5
Output
YES
YES
NO
YES
-----Note-----
For the first two test cases and the fourth test case, we can operate as follow (the bolded elements are the pair chosen for that operation):
$[\text{1}, \textbf{2}, \textbf{3}] \rightarrow [\textbf{1}, \textbf{2}] \rightarrow [\text{1}]$
$[\text{3}, \textbf{1}, \textbf{2}, \text{4}] \rightarrow [\text{3}, \textbf{1}, \textbf{4}] \rightarrow [\textbf{3}, \textbf{4}] \rightarrow [\text{4}]$
$[\textbf{2}, \textbf{4}, \text{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\textbf{4}, \textbf{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\text{4}, \text{1}, \textbf{3}, \textbf{5}] \rightarrow [\text{4}, \textbf{1}, \textbf{5}] \rightarrow [\textbf{4}, \textbf{5}] \rightarrow [\text{4}]$ | t = int(input())
while t:
t -= 1
n = int(input())
a = [int(i) for i in input().split()]
b = [[]]
for i in range(n - 1, 0, -1):
if a[i] < a[i - 1]:
b[-1].append(a[i])
b.append([])
else:
b[-1].append(a[i])
b[-1].append(a[0])
b.reverse()
st = [b[0][-1]]
flag = 0
for i in b[1:]:
if len(st) == 1 and i[0] > st[-1]:
continue
elif len(st) > 1 and i[0] > st[-1]:
while len(st) > 1 and st[-1] < i[0]:
st.pop()
if len(st) == 1 and st[0] > i[0]:
st.append(i[0])
else:
st.append(i[-1])
if len(st) > 1:
print("NO")
else:
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of length $n$, which initially is a permutation of numbers from $1$ to $n$. In one operation, you can choose an index $i$ ($1 \leq i < n$) such that $a_i < a_{i + 1}$, and remove either $a_i$ or $a_{i + 1}$ from the array (after the removal, the remaining parts are concatenated).
For example, if you have the array $[1, 3, 2]$, you can choose $i = 1$ (since $a_1 = 1 < a_2 = 3$), then either remove $a_1$ which gives the new array $[3, 2]$, or remove $a_2$ which gives the new array $[1, 2]$.
Is it possible to make the length of this array equal to $1$ with these operations?
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 3 \cdot 10^5$) Β β the length of the array.
The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \leq a_i \leq n$, $a_i$ are pairwise distinct)Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each test case, output on a single line the word "YES" if it is possible to reduce the array to a single element using the aforementioned operation, or "NO" if it is impossible to do so.
-----Example-----
Input
4
3
1 2 3
4
3 1 2 4
3
2 3 1
6
2 4 6 1 3 5
Output
YES
YES
NO
YES
-----Note-----
For the first two test cases and the fourth test case, we can operate as follow (the bolded elements are the pair chosen for that operation):
$[\text{1}, \textbf{2}, \textbf{3}] \rightarrow [\textbf{1}, \textbf{2}] \rightarrow [\text{1}]$
$[\text{3}, \textbf{1}, \textbf{2}, \text{4}] \rightarrow [\text{3}, \textbf{1}, \textbf{4}] \rightarrow [\textbf{3}, \textbf{4}] \rightarrow [\text{4}]$
$[\textbf{2}, \textbf{4}, \text{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\textbf{4}, \textbf{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\text{4}, \text{1}, \textbf{3}, \textbf{5}] \rightarrow [\text{4}, \textbf{1}, \textbf{5}] \rightarrow [\textbf{4}, \textbf{5}] \rightarrow [\text{4}]$ | for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
m = max(l)
mi = min(l)
if l[0] == m or l[-1] == mi or l[-1] < l[0]:
print("NO")
elif l[-1] == m or l[-1] > l[0] or l[0] == mi:
print("YES") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of length $n$, which initially is a permutation of numbers from $1$ to $n$. In one operation, you can choose an index $i$ ($1 \leq i < n$) such that $a_i < a_{i + 1}$, and remove either $a_i$ or $a_{i + 1}$ from the array (after the removal, the remaining parts are concatenated).
For example, if you have the array $[1, 3, 2]$, you can choose $i = 1$ (since $a_1 = 1 < a_2 = 3$), then either remove $a_1$ which gives the new array $[3, 2]$, or remove $a_2$ which gives the new array $[1, 2]$.
Is it possible to make the length of this array equal to $1$ with these operations?
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 3 \cdot 10^5$) Β β the length of the array.
The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \leq a_i \leq n$, $a_i$ are pairwise distinct)Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each test case, output on a single line the word "YES" if it is possible to reduce the array to a single element using the aforementioned operation, or "NO" if it is impossible to do so.
-----Example-----
Input
4
3
1 2 3
4
3 1 2 4
3
2 3 1
6
2 4 6 1 3 5
Output
YES
YES
NO
YES
-----Note-----
For the first two test cases and the fourth test case, we can operate as follow (the bolded elements are the pair chosen for that operation):
$[\text{1}, \textbf{2}, \textbf{3}] \rightarrow [\textbf{1}, \textbf{2}] \rightarrow [\text{1}]$
$[\text{3}, \textbf{1}, \textbf{2}, \text{4}] \rightarrow [\text{3}, \textbf{1}, \textbf{4}] \rightarrow [\textbf{3}, \textbf{4}] \rightarrow [\text{4}]$
$[\textbf{2}, \textbf{4}, \text{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\textbf{4}, \textbf{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\text{4}, \text{1}, \textbf{3}, \textbf{5}] \rightarrow [\text{4}, \textbf{1}, \textbf{5}] \rightarrow [\textbf{4}, \textbf{5}] \rightarrow [\text{4}]$ | tc = int(input())
for _ in range(tc):
_ = int(input())
xs = [int(x) for x in input().split()]
print("YES" if xs[0] < xs[-1] else "NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING STRING |
You are given an array $a$ of length $n$, which initially is a permutation of numbers from $1$ to $n$. In one operation, you can choose an index $i$ ($1 \leq i < n$) such that $a_i < a_{i + 1}$, and remove either $a_i$ or $a_{i + 1}$ from the array (after the removal, the remaining parts are concatenated).
For example, if you have the array $[1, 3, 2]$, you can choose $i = 1$ (since $a_1 = 1 < a_2 = 3$), then either remove $a_1$ which gives the new array $[3, 2]$, or remove $a_2$ which gives the new array $[1, 2]$.
Is it possible to make the length of this array equal to $1$ with these operations?
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 3 \cdot 10^5$) Β β the length of the array.
The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \leq a_i \leq n$, $a_i$ are pairwise distinct)Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each test case, output on a single line the word "YES" if it is possible to reduce the array to a single element using the aforementioned operation, or "NO" if it is impossible to do so.
-----Example-----
Input
4
3
1 2 3
4
3 1 2 4
3
2 3 1
6
2 4 6 1 3 5
Output
YES
YES
NO
YES
-----Note-----
For the first two test cases and the fourth test case, we can operate as follow (the bolded elements are the pair chosen for that operation):
$[\text{1}, \textbf{2}, \textbf{3}] \rightarrow [\textbf{1}, \textbf{2}] \rightarrow [\text{1}]$
$[\text{3}, \textbf{1}, \textbf{2}, \text{4}] \rightarrow [\text{3}, \textbf{1}, \textbf{4}] \rightarrow [\textbf{3}, \textbf{4}] \rightarrow [\text{4}]$
$[\textbf{2}, \textbf{4}, \text{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\textbf{4}, \textbf{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\text{4}, \text{1}, \textbf{3}, \textbf{5}] \rightarrow [\text{4}, \textbf{1}, \textbf{5}] \rightarrow [\textbf{4}, \textbf{5}] \rightarrow [\text{4}]$ | t = int(input())
for i in range(t):
n = int(input())
inp = [int(j) for j in input().split()]
diff = 0
for j in range(1, n):
diff = diff + (inp[j] - inp[j - 1])
if diff < 0:
print("NO")
else:
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of length $n$, which initially is a permutation of numbers from $1$ to $n$. In one operation, you can choose an index $i$ ($1 \leq i < n$) such that $a_i < a_{i + 1}$, and remove either $a_i$ or $a_{i + 1}$ from the array (after the removal, the remaining parts are concatenated).
For example, if you have the array $[1, 3, 2]$, you can choose $i = 1$ (since $a_1 = 1 < a_2 = 3$), then either remove $a_1$ which gives the new array $[3, 2]$, or remove $a_2$ which gives the new array $[1, 2]$.
Is it possible to make the length of this array equal to $1$ with these operations?
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 3 \cdot 10^5$) Β β the length of the array.
The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \leq a_i \leq n$, $a_i$ are pairwise distinct)Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each test case, output on a single line the word "YES" if it is possible to reduce the array to a single element using the aforementioned operation, or "NO" if it is impossible to do so.
-----Example-----
Input
4
3
1 2 3
4
3 1 2 4
3
2 3 1
6
2 4 6 1 3 5
Output
YES
YES
NO
YES
-----Note-----
For the first two test cases and the fourth test case, we can operate as follow (the bolded elements are the pair chosen for that operation):
$[\text{1}, \textbf{2}, \textbf{3}] \rightarrow [\textbf{1}, \textbf{2}] \rightarrow [\text{1}]$
$[\text{3}, \textbf{1}, \textbf{2}, \text{4}] \rightarrow [\text{3}, \textbf{1}, \textbf{4}] \rightarrow [\textbf{3}, \textbf{4}] \rightarrow [\text{4}]$
$[\textbf{2}, \textbf{4}, \text{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\textbf{4}, \textbf{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\text{4}, \text{1}, \textbf{3}, \textbf{5}] \rightarrow [\text{4}, \textbf{1}, \textbf{5}] \rightarrow [\textbf{4}, \textbf{5}] \rightarrow [\text{4}]$ | t = int(input())
for i in range(0, t):
impossible = 0
n = int(input())
a = []
arr = []
a = input().split(" ")
for j in range(0, n):
arr.append(int(a[j]))
if arr[0] < arr[n - 1]:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of length $n$, which initially is a permutation of numbers from $1$ to $n$. In one operation, you can choose an index $i$ ($1 \leq i < n$) such that $a_i < a_{i + 1}$, and remove either $a_i$ or $a_{i + 1}$ from the array (after the removal, the remaining parts are concatenated).
For example, if you have the array $[1, 3, 2]$, you can choose $i = 1$ (since $a_1 = 1 < a_2 = 3$), then either remove $a_1$ which gives the new array $[3, 2]$, or remove $a_2$ which gives the new array $[1, 2]$.
Is it possible to make the length of this array equal to $1$ with these operations?
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 3 \cdot 10^5$) Β β the length of the array.
The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \leq a_i \leq n$, $a_i$ are pairwise distinct)Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each test case, output on a single line the word "YES" if it is possible to reduce the array to a single element using the aforementioned operation, or "NO" if it is impossible to do so.
-----Example-----
Input
4
3
1 2 3
4
3 1 2 4
3
2 3 1
6
2 4 6 1 3 5
Output
YES
YES
NO
YES
-----Note-----
For the first two test cases and the fourth test case, we can operate as follow (the bolded elements are the pair chosen for that operation):
$[\text{1}, \textbf{2}, \textbf{3}] \rightarrow [\textbf{1}, \textbf{2}] \rightarrow [\text{1}]$
$[\text{3}, \textbf{1}, \textbf{2}, \text{4}] \rightarrow [\text{3}, \textbf{1}, \textbf{4}] \rightarrow [\textbf{3}, \textbf{4}] \rightarrow [\text{4}]$
$[\textbf{2}, \textbf{4}, \text{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\textbf{4}, \textbf{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\text{4}, \text{1}, \textbf{3}, \textbf{5}] \rightarrow [\text{4}, \textbf{1}, \textbf{5}] \rightarrow [\textbf{4}, \textbf{5}] \rightarrow [\text{4}]$ | t = int(input())
for _ in range(t):
n = int(input())
l = list(map(int, input().split()))
stack = []
for i in range(n):
if not stack:
stack.append(l[i])
elif l[i] > stack[-1]:
while stack and l[i] > stack[-1]:
t = stack.pop()
stack.append(t)
else:
stack.append(l[i])
if len(stack) == 1:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.