description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | for u in range(int(input())):
n, m, x = map(int, input().split())
h = list(map(int, input().split()))
ans = [0] * n
y = [[h[i], i] for i in range(n)]
y = sorted(y)
print("YES")
for i in range(n):
ans[y[i][1]] = i % m + 1
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | for _ in range(int(input())):
n, m, x = map(int, input().split())
h = list(map(int, input().split()))
l = []
j = 0
for i in h:
l.append([j, i])
j += 1
l.sort(key=lambda x: x[1])
j = 0
for k in l:
k[1] = j % m + 1
j += 1
l.sort(key=lambda x: x[0])
print("YES")
print(*(i[1] for i in l)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER VAR VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | for _ in range(int(input())):
n, m, x = map(int, input().split())
a = list(map(int, input().split()))
b = [[a[i], i] for i in range(n)]
b.sort()
b.reverse()
res = [0] * n
c = 0
for i in range(n):
k = b[i][1]
res[k] = c % m + 1
c += 1
print("YES")
print(*res) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | n = int(input())
for i in range(n):
n, m, x = map(int, input().split())
s = list(map(int, input().split()))
li = []
for i in range(len(s)):
li.append([s[i], i])
li.sort()
sort_index = []
for x in li:
sort_index.append(x[1])
Ans = [0] * n
cnt = -1
for j in sort_index:
cnt += 1
Ans[j] = cnt % m + 1
print("YES")
print(" ".join(str(i) for i in Ans)) | 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | t = int(input())
for i in range(t):
n, m, x = map(int, input().split())
arr = list(map(int, input().split()))
ans = [(0) for j in range(n)]
ind = []
for i in range(n):
ind.append([i, arr[i]])
ind.sort(key=lambda x: x[1])
print("YES")
for i in range(n):
if (i + 1) % m == 0:
ans[ind[i][0]] = m
else:
ans[ind[i][0]] = (i + 1) % m
print(*ans) | 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 FUNC_CALL 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 EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | def solve(n, m, x, h):
ans = [-1] * n
arr = sorted((hi, i) for i, hi in enumerate(h))
for ind in range(n):
ans[arr[ind][1]] = ind % m + 1
print("YES")
print(*ans)
T = int(input())
for t in range(T):
n, m, x = map(int, input().split())
h = list(map(int, input().split()))
solve(n, m, x, h) | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | for _ in range(int(input())):
print("YES")
n, h, k = map(int, input().split())
s = map(int, input().split())
ar = [[] for i in range(h)]
dc = sorted(enumerate(s), key=lambda x: (x[1], x[0]), reverse=True)
x = 0
c = 0
while c < n:
if x % 2 == 0:
for i in range(h):
ar[i].append(dc[c][0])
c += 1
if c == n:
break
else:
for i in range(h - 1, -1, -1):
ar[i].append(dc[c][0])
c += 1
if c == n:
break
x += 1
l = [(0) for i in range(n)]
for i in range(h):
for j in range(len(ar[i])):
l[ar[i][j]] = i + 1
print(*l) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | for iii in range(int(input())):
def solution(n, m, x, s):
e = sorted([(s[i], i) for i in range(n)])
d = [(0) for i in range(n)]
j = f = 1
for i in range(n - 1, -1, -1):
if j > m and f:
j = 1
f = 0
elif j > m:
j = 1
f = 1
if f:
d[e[i][1]] = j
else:
d[e[i][1]] = m - j + 1
j += 1
return d
n, m, x = map(int, input().split())
s = list(map(int, input().split()))
print("YES")
print(*solution(n, m, x, s)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | def ii():
return int(input())
def li():
return [int(i) for i in input().split()]
for t in range(ii()):
n, m, x = li()
a = li()
h = []
for i in range(n):
h.append([a[i], i])
h.sort()
ans = [(0) for i in range(n)]
for i in range(n):
ans[h[i][1]] = i % m + 1
print("YES")
print(*ans) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | T = int(input())
for t in range(T):
n, m, x = map(int, input().split())
hs = list(map(int, input().split()))
hs = [(hs[i], i) for i in range(n)]
ord = [0] * n
hs.sort()
i, j = 0, 0
arr = [0] * m
while i < n:
arr[j] += hs[i][0]
if abs(arr[j] - arr[j - 1]) > x:
break
ord[hs[i][1]] = j + 1
j = (j + 1) % m
i += 1
if i == n:
print("YES")
for i in ord:
print(i, end=" ")
print()
else:
print("NO") | 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | def answer(arr1, tower, x):
arr1.sort(key=lambda x: x[0])
result = [(0) for i in range(tower)]
result1 = [(0) for i in range(len(arr))]
i = 0
j = 0
while i < len(arr1):
result[j] += arr1[i][0]
result1[arr1[i][1]] = j + 1
j += 1
if j == tower:
j = 0
i += 1
i = 0
ind = True
while i + 1 < tower:
if abs(result[i] - result[i + 1]) > x:
print("NO")
ind = False
break
i += 1
if ind == True:
print("YES")
for i in result1:
print(i, end=" ")
print()
t = int(input())
for w in range(t):
inp = input().split()
tower = int(inp[1])
x = int(inp[2])
inp = input().split()
arr = []
j = 0
for i in inp:
arr.append([int(i), j])
j += 1
answer(arr, tower, x) | FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | def read():
number = eval(input())
return number
def gets():
s = input()
return s
def cout(x):
print(x)
def main():
T = eval(input())
while T:
T = T - 1
n, m, x = map(int, input().split())
k = list(map(int, input().split()))
dj = sorted(range(n), key=lambda i: k[i])
ans = [0] * n
cnt = 0
for i in dj:
ans[i] = cnt + 1
cnt = (cnt + 1) % m
cout("YES")
print(*ans)
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | for _ in range(int(input())):
n, m, k = map(int, input().split())
lst = list(map(int, input().split()))
ans = [0] * n
lst = sorted(range(n), key=lst.__getitem__)
count = 1
for i in range(n):
if count > m:
count = 1
ans[lst[i]] = count
count += 1
print("YES")
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | t = int(input())
for i in range(t):
a = [int(x) for x in input().split()]
n = a[0]
m = a[1]
x = a[2]
a = [int(x) for x in input().split()]
bl = []
for ind in range(len(a)):
bl.append((a[ind], ind))
bl.sort()
to = []
for ind in range(len(bl)):
eff = ind % m
if len(to) - 1 < eff:
to.append([bl[ind]])
else:
to[eff].append(bl[ind])
su = []
res = [(-1) for x in range(n)]
ind = 1
for el in to:
summ = 0
for k in el:
summ += k[0]
res[k[1]] = str(ind)
su.append(summ)
ind += 1
minn = min(su)
maxx = max(su)
if maxx - minn > x:
print("NO")
else:
print("YES")
print(" ".join(res)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | T = int(input())
for ii in range(T):
n, m, x = map(int, input().split())
a = list(map(int, input().split()))
arr = []
for i in range(n):
arr.append([i, a[i]])
arr = sorted(arr, key=lambda x: x[1])
ar = [[] for i in range(m)]
ans = [0] * n
for i in range(n):
ar[i % m].append(arr[i][1])
ans[arr[i][0]] = i % m + 1
maxx = -1
minn = 1e20
l = 0
for i in ar:
if len(i) > 0:
l += 1
maxx = max(sum(i), maxx)
minn = min(sum(i), minn)
if maxx - minn > x or l < m:
print("NO")
else:
print("YES")
print(*ans) | 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | def task():
n, m, x = [int(x) for x in input().split()]
s = [int(x) for x in input().split()]
d = sorted(range(n), key=lambda t: s[t])
ans = [0] * n
print("YES")
a = 0
for i in d:
ans[i] = a + 1
a = (a + 1) % m
print(" ".join([str(x) for x in ans]))
for _ in range(int(input())):
task() | FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | import itertools
def readline():
return map(int, input().split())
def solve_clever():
n, m, x = readline()
h = sorted((hi, i) for i, hi in enumerate(readline()))
answer = sorted(
(i, tower_idx)
for (hi, i), tower_idx in zip(h, itertools.cycle(range(1, m + 1)))
)
print("YES")
print(*(ti for i, ti in answer))
def main():
t = int(input())
for __ in range(t):
solve_clever()
main() | IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | t = int(input())
for case in range(1, t + 1):
n, m, x = (int(i) for i in input().split())
h = [int(i) for i in input().split()]
blocks = [[h[i], i, 0] for i in range(n)]
blocks.sort(key=lambda x: x[0], reverse=True)
towerHeights = [(0) for _ in range(m)]
t = 0
for b in blocks:
b[2] = t + 1
towerHeights[t] += b[0]
t = (t + 1) % m
blocks.sort(key=lambda x: x[1])
if max(towerHeights) - min(towerHeights) <= x:
print("YES")
for b in blocks:
print(b[2], end=" ")
print()
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | t = int(input())
for _ in range(t):
n, m, x = map(int, input().split())
a = list(map(int, input().split()))
ans = [0] * n
arr = []
for i in range(n):
arr.append([a[i], i])
arr.sort()
for i in range(n):
ans[arr[i][1]] = i % m + 1
print("YES")
print(*ans) | 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | R = lambda: list(map(int, input().split()))
(t,) = R()
for _ in range(t):
n, m, x = R()
h = R()
h = [(h[i], i) for i in range(n)]
h.sort()
ans = [0] * n
for i in range(n):
ans[h[i][1]] = i % m + 1
print("YES")
print(*ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | for _ in range(int(input())):
n, m, x = map(int, input().split())
a = list(map(int, input().split()))
ans = [-1] * n
h = []
tower = [0] * n
for i in range(n):
h.append((a[i], i))
h.sort()
for i in range(n):
tower[i % m] += h[i][0]
ans[h[i][1]] = i % m + 1
tower.sort()
idx = -1
for i in range(n):
if tower[i] != 0:
idx = i
break
if tower[i] - tower[idx] > x:
print("NO")
else:
print("YES")
for i in ans:
print(i, end=" ")
print("") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | for _ in range(int(input())):
n, m, x = map(int, input().split())
h = list(map(int, input().split()))
h = [(h[i], i) for i in range(n)]
h.sort(reverse=True)
tow = [0] * m
rev = False
cur = 0
ans = [0] * n
for hi, i in h:
tow[cur] += hi
ans[i] = cur + 1
if cur == 0 and rev:
rev = False
elif cur == m - 1 and not rev:
rev = True
elif rev:
cur -= 1
else:
cur += 1
if max(tow) - min(tow) > x:
print("NO")
else:
print("YES")
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | for i in range(int(input())):
n, m, x = map(int, input().split())
l = list(map(int, input().split()))
pair = [[l[i], i] for i in range(n)]
final = [-1] * n
pair.sort()
towers = [0] * m
m1 = 0
for j in range(n):
if m1 == m:
m1 = 0
towers[m1] += pair[j][0]
final[pair[j][1]] = m1
m1 += 1
if max(towers) - min(towers) <= x:
print("YES")
for k in final:
print(k + 1, end=" ")
print()
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | for _ in range(int(input())):
n, m, x = map(int, input().split())
arr = list(map(int, input().split()))
new = []
for i in range(n):
new.append([arr[i], i])
new.sort(reverse=True)
res = [0] * n
for i in range(0, n, 2 * m):
c = 0
while i + c < n and c < m:
res[new[i + c][1]] = c + 1
c += 1
for i in range(m, n, 2 * m):
c = 0
while i + c < n and c < m:
res[new[i + c][1]] = m - c
c += 1
print("YES")
print(*res) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | def solve():
n, m, x = map(int, input().split())
h = list(map(int, input().split()))
i = 0
b = [0] * m
ind = [-1] * n
hai = [(h[i], i) for i in range(n)]
hai.sort(key=lambda z: z[0])
i = 0
j = 0
while j < n:
while b[i] <= b[(i + 1) % m]:
b[i] += hai[j][0]
ind[hai[j][1]] = i + 1
j += 1
if j >= n:
break
i += 1
i %= m
for i in range(1, m):
if b[i] - b[i - 1] > x:
print("NO")
return
print("YES")
for i in range(n):
print(ind[i], end=" ")
print()
def main():
for _ in range(int(input())):
solve()
main() | FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | from sys import stdin, stdout
def readInt():
return int(stdin.readline())
def readInts():
return map(int, stdin.readline().strip().split())
def readString():
return stdin.readline().strip()
def writeLine(s):
stdout.write(str(s) + "\n")
return
def main():
t = readInt()
for _ in range(t):
n, m, x = readInts()
h = list(readInts())
si = sorted(range(len(h)), key=lambda k: h[k])
output = [0] * n
for i in range(len(si)):
output[si[i]] = i % m + 1
writeLine("YES")
writeLine(" ".join(map(str, output)))
main() | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$.
Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) β the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.
The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) β the heights of the blocks.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$.
-----Output-----
For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in.
If there are multiple solutions, print any of them.
-----Examples-----
Input
2
5 2 3
1 2 3 1 2
4 3 3
1 1 2 3
Output
YES
1 1 1 2 2
YES
1 2 2 3
-----Note-----
In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful.
In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful. | for _ in range(int(input())):
n, m, x = map(int, input().split())
a = list(map(int, input().split()))
b = []
for i in range(n):
b.append([a[i], i])
b.sort(reverse=True)
ans = [1] * n
while b:
for i in range(m):
if b:
r = b.pop()
ans[r[1]] = i + 1
print("YES")
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR FOR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
A binary tree of n nodes is given. Nodes of the tree are numbered from 1 to n and the root is the node 1. Each node can have no child, only one left child, only one right child, or both children. For convenience, let's denote l_u and r_u as the left and the right child of the node u respectively, l_u = 0 if u does not have the left child, and r_u = 0 if the node u does not have the right child.
Each node has a string label, initially is a single character c_u. Let's define the string representation of the binary tree as the concatenation of the labels of the nodes in the in-order. Formally, let f(u) be the string representation of the tree rooted at the node u. f(u) is defined as follows: $$$ f(u) = \begin{cases} <empty string>, & if u = 0; \\\ f(l_u) + c_u + f(r_u) & otherwise, \end{cases} where +$$$ denotes the string concatenation operation.
This way, the string representation of the tree is f(1).
For each node, we can duplicate its label at most once, that is, assign c_u with c_u + c_u, but only if u is the root of the tree, or if its parent also has its label duplicated.
You are given the tree and an integer k. What is the lexicographically smallest string representation of the tree, if we can duplicate labels of at most k nodes?
A string a is lexicographically smaller than a string b if and only if one of the following holds:
* a is a prefix of b, but a β b;
* in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The second line contains a string c of n lower-case English letters, where c_i is the initial label of the node i for 1 β€ i β€ n. Note that the given string c is not the initial string representation of the tree.
The i-th of the next n lines contains two integers l_i and r_i (0 β€ l_i, r_i β€ n). If the node i does not have the left child, l_i = 0, and if the node i does not have the right child, r_i = 0.
It is guaranteed that the given input forms a binary tree, rooted at 1.
Output
Print a single line, containing the lexicographically smallest string representation of the tree if at most k nodes have their labels duplicated.
Examples
Input
4 3
abab
2 3
0 0
0 4
0 0
Output
baaaab
Input
8 2
kadracyn
2 5
3 4
0 0
0 0
6 8
0 7
0 0
0 0
Output
daarkkcyan
Input
8 3
kdaracyn
2 5
0 3
0 4
0 0
6 8
0 7
0 0
0 0
Output
darkcyan
Note
The images below present the tree for the examples. The number in each node is the node number, while the subscripted letter is its label. To the right is the string representation of the tree, with each letter having the same color as the corresponding node.
Here is the tree for the first example. Here we duplicated the labels of nodes 1 and 3. We should not duplicate the label of node 2 because it would give us the string "bbaaab", which is lexicographically greater than "baaaab".
<image>
In the second example, we can duplicate the labels of nodes 1 and 2. Note that only duplicating the label of the root will produce a worse result than the initial string.
<image>
In the third example, we should not duplicate any character at all. Even though we would want to duplicate the label of the node 3, by duplicating it we must also duplicate the label of the node 2, which produces a worse result.
<image>
There is no way to produce string "darkkcyan" from a tree with the initial string representation "darkcyan" :(. | import sys
I = lambda: [*map(int, sys.stdin.readline().split())]
left = []
right = []
n, k = I()
parents = [-1] * n
s = input()
for i in range(n):
l, r = I()
l -= 1
r -= 1
left.append(l)
right.append(r)
if l >= 0:
parents[l] = i
if r >= 0:
parents[r] = i
covered = [0] * n
covered.append(1)
order = []
curr = 0
while len(order) < n:
if covered[left[curr]]:
if covered[curr]:
if covered[right[curr]]:
curr = parents[curr]
else:
curr = right[curr]
else:
covered[curr] = 1
order.append(curr)
else:
curr = left[curr]
after = "a"
want = [0] * n
curr = s[order[-1]]
for i in range(n - 2, -1, -1):
new = s[order[i]]
if new != curr:
after = curr
curr = new
if curr < after:
want[order[i]] = 1
dist = [float("inf")] * n
for v in order:
if want[v]:
dist[v] = 0
elif left[v] >= 0:
dist[v] = dist[left[v]] + 1
dupe = [0] * n
checked = [0] * n
curr = 0
lef = k
while lef > 0 and curr != -1:
if dupe[curr]:
if left[curr] >= 0 and dupe[left[curr]] == 0 and checked[left[curr]] == 0:
curr = left[curr]
elif right[curr] >= 0 and dupe[right[curr]] == 0 and checked[right[curr]] == 0:
curr = right[curr]
else:
curr = parents[curr]
elif dist[curr] < lef:
lef -= dist[curr] + 1
dupe[curr] = 1
for i in range(dist[curr]):
curr = left[curr]
dupe[curr] = 1
else:
checked[curr] = 1
curr = parents[curr]
out = []
for guy in order:
if dupe[guy]:
out.append(2 * s[guy])
else:
out.append(s[guy])
print("".join(out)) | IMPORT ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR IF VAR VAR VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You are given a tree (connected graph without cycles) consisting of $n$ vertices. The tree is unrooted β it is just a connected undirected graph without cycles.
In one move, you can choose exactly $k$ leaves (leaf is such a vertex that is connected to only one another vertex) connected to the same vertex and remove them with edges incident to them. I.e. you choose such leaves $u_1, u_2, \dots, u_k$ that there are edges $(u_1, v)$, $(u_2, v)$, $\dots$, $(u_k, v)$ and remove these leaves and these edges.
Your task is to find the maximum number of moves you can perform if you remove leaves optimally.
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $1 \le k < n$) β the number of vertices in the tree and the number of leaves you remove in one move, respectively. The next $n-1$ lines describe edges. The $i$-th edge is represented as two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$), where $x_i$ and $y_i$ are vertices the $i$-th edge connects. It is guaranteed that the given set of edges forms a tree.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer β the maximum number of moves you can perform if you remove leaves optimally.
-----Example-----
Input
4
8 3
1 2
1 5
7 6
6 8
3 1
6 4
6 1
10 3
1 2
1 10
2 3
1 5
1 6
2 4
7 10
10 9
8 10
7 2
3 1
4 5
3 6
7 4
1 2
1 4
5 1
1 2
2 3
4 3
5 3
Output
2
3
3
4
-----Note-----
The picture corresponding to the first test case of the example:
[Image]
There you can remove vertices $2$, $5$ and $3$ during the first move and vertices $1$, $7$ and $4$ during the second move.
The picture corresponding to the second test case of the example:
[Image]
There you can remove vertices $7$, $8$ and $9$ during the first move, then vertices $5$, $6$ and $10$ during the second move and vertices $1$, $3$ and $4$ during the third move.
The picture corresponding to the third test case of the example:
$\text{of}$
There you can remove vertices $5$ and $7$ during the first move, then vertices $2$ and $4$ during the second move and vertices $1$ and $6$ during the third move. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, k = map(int, input().split())
edge = [[] for i in range(n)]
for i in range(n - 1):
x, y = map(int, input().split())
edge[x - 1].append(y - 1)
edge[y - 1].append(x - 1)
leaf = [[] for i in range(n)]
for i in range(n):
if len(edge[i]) == 1:
leaf[edge[i][0]].append(i)
que = [i for i in range(n) if len(leaf[i]) >= k]
deg = [len(edge[i]) for i in range(n)]
delete = set([])
ans = 0
while que:
v = que.pop()
if v in delete:
continue
q = len(leaf[v]) // k
count = q * k
ans += q
while count:
dv = leaf[v].pop()
delete.add(dv)
count -= 1
deg[v] -= q * k
if deg[v] == 1:
for nv in edge[v]:
if nv not in delete:
leaf[nv].append(v)
if len(leaf[nv]) == k:
que.append(nv)
break
print(ans) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a tree (connected graph without cycles) consisting of $n$ vertices. The tree is unrooted β it is just a connected undirected graph without cycles.
In one move, you can choose exactly $k$ leaves (leaf is such a vertex that is connected to only one another vertex) connected to the same vertex and remove them with edges incident to them. I.e. you choose such leaves $u_1, u_2, \dots, u_k$ that there are edges $(u_1, v)$, $(u_2, v)$, $\dots$, $(u_k, v)$ and remove these leaves and these edges.
Your task is to find the maximum number of moves you can perform if you remove leaves optimally.
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $1 \le k < n$) β the number of vertices in the tree and the number of leaves you remove in one move, respectively. The next $n-1$ lines describe edges. The $i$-th edge is represented as two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$), where $x_i$ and $y_i$ are vertices the $i$-th edge connects. It is guaranteed that the given set of edges forms a tree.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer β the maximum number of moves you can perform if you remove leaves optimally.
-----Example-----
Input
4
8 3
1 2
1 5
7 6
6 8
3 1
6 4
6 1
10 3
1 2
1 10
2 3
1 5
1 6
2 4
7 10
10 9
8 10
7 2
3 1
4 5
3 6
7 4
1 2
1 4
5 1
1 2
2 3
4 3
5 3
Output
2
3
3
4
-----Note-----
The picture corresponding to the first test case of the example:
[Image]
There you can remove vertices $2$, $5$ and $3$ during the first move and vertices $1$, $7$ and $4$ during the second move.
The picture corresponding to the second test case of the example:
[Image]
There you can remove vertices $7$, $8$ and $9$ during the first move, then vertices $5$, $6$ and $10$ during the second move and vertices $1$, $3$ and $4$ during the third move.
The picture corresponding to the third test case of the example:
$\text{of}$
There you can remove vertices $5$ and $7$ during the first move, then vertices $2$ and $4$ during the second move and vertices $1$ and $6$ during the third move. | from sys import stdin
input = stdin.readline
def solve():
n, k = map(int, input().split())
adj = [[] for _ in range(n)]
frd = [set() for _ in range(n)]
cnt = [0] * n
is_leaf = [False] * n
for _ in range(n - 1):
u, v = map(int, input().split())
u -= 1
v -= 1
adj[u].append(v)
adj[v].append(u)
if k == 1:
print(n - 1)
return
for i in range(n):
if len(adj[i]) == 1:
is_leaf[i] = True
cnt[adj[i][0]] += 1
q = []
for u in range(n):
if cnt[u] >= k:
q.append(u)
for v in adj[u]:
if not is_leaf[v]:
frd[u].add(v)
ans = 0
while len(q) > 0:
u = q.pop()
ans += cnt[u] // k
cnt[u] %= k
if cnt[u] == 0 and len(frd[u]) == 1:
p = frd[u].pop()
frd[p].remove(u)
cnt[p] += 1
if cnt[p] >= k:
q.append(p)
print(ans)
def main():
t = int(input())
for _ in range(t):
solve()
main() | ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You are given a tree (connected graph without cycles) consisting of $n$ vertices. The tree is unrooted β it is just a connected undirected graph without cycles.
In one move, you can choose exactly $k$ leaves (leaf is such a vertex that is connected to only one another vertex) connected to the same vertex and remove them with edges incident to them. I.e. you choose such leaves $u_1, u_2, \dots, u_k$ that there are edges $(u_1, v)$, $(u_2, v)$, $\dots$, $(u_k, v)$ and remove these leaves and these edges.
Your task is to find the maximum number of moves you can perform if you remove leaves optimally.
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $1 \le k < n$) β the number of vertices in the tree and the number of leaves you remove in one move, respectively. The next $n-1$ lines describe edges. The $i$-th edge is represented as two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$), where $x_i$ and $y_i$ are vertices the $i$-th edge connects. It is guaranteed that the given set of edges forms a tree.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer β the maximum number of moves you can perform if you remove leaves optimally.
-----Example-----
Input
4
8 3
1 2
1 5
7 6
6 8
3 1
6 4
6 1
10 3
1 2
1 10
2 3
1 5
1 6
2 4
7 10
10 9
8 10
7 2
3 1
4 5
3 6
7 4
1 2
1 4
5 1
1 2
2 3
4 3
5 3
Output
2
3
3
4
-----Note-----
The picture corresponding to the first test case of the example:
[Image]
There you can remove vertices $2$, $5$ and $3$ during the first move and vertices $1$, $7$ and $4$ during the second move.
The picture corresponding to the second test case of the example:
[Image]
There you can remove vertices $7$, $8$ and $9$ during the first move, then vertices $5$, $6$ and $10$ during the second move and vertices $1$, $3$ and $4$ during the third move.
The picture corresponding to the third test case of the example:
$\text{of}$
There you can remove vertices $5$ and $7$ during the first move, then vertices $2$ and $4$ during the second move and vertices $1$ and $6$ during the third move. | import sys
input = sys.stdin.readline
f = lambda: map(int, input().split())
for _ in range(int(input())):
n, k = f()
g = [[] for i in range(n + 1)]
deg = [0] * (n + 1)
isLeaf = [0] * (n + 1)
numLeaves = [0] * (n + 1)
q = []
ans = 0
for __ in range(n - 1):
a, b = f()
g[a].append(b)
g[b].append(a)
deg[a] += 1
deg[b] += 1
if k == 1:
print(n - 1)
continue
for i in range(1, n + 1):
if deg[i] == 1:
isLeaf[i] = True
numLeaves[g[i][0]] += 1
for i in range(1, n + 1):
if numLeaves[i] >= k:
q.append(i)
while len(q):
u = q.pop()
ans += 1
numLeaves[u] -= k
if numLeaves[u]:
if numLeaves[u] >= k:
q.append(u)
continue
numP, v = 0, 0
for i in g[u]:
if not isLeaf[i]:
numP += 1
v = i
if numP == 1:
numLeaves[v] += 1
isLeaf[u] = True
if numLeaves[v] == k:
q.append(v)
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a tree (connected graph without cycles) consisting of $n$ vertices. The tree is unrooted β it is just a connected undirected graph without cycles.
In one move, you can choose exactly $k$ leaves (leaf is such a vertex that is connected to only one another vertex) connected to the same vertex and remove them with edges incident to them. I.e. you choose such leaves $u_1, u_2, \dots, u_k$ that there are edges $(u_1, v)$, $(u_2, v)$, $\dots$, $(u_k, v)$ and remove these leaves and these edges.
Your task is to find the maximum number of moves you can perform if you remove leaves optimally.
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $1 \le k < n$) β the number of vertices in the tree and the number of leaves you remove in one move, respectively. The next $n-1$ lines describe edges. The $i$-th edge is represented as two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$), where $x_i$ and $y_i$ are vertices the $i$-th edge connects. It is guaranteed that the given set of edges forms a tree.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer β the maximum number of moves you can perform if you remove leaves optimally.
-----Example-----
Input
4
8 3
1 2
1 5
7 6
6 8
3 1
6 4
6 1
10 3
1 2
1 10
2 3
1 5
1 6
2 4
7 10
10 9
8 10
7 2
3 1
4 5
3 6
7 4
1 2
1 4
5 1
1 2
2 3
4 3
5 3
Output
2
3
3
4
-----Note-----
The picture corresponding to the first test case of the example:
[Image]
There you can remove vertices $2$, $5$ and $3$ during the first move and vertices $1$, $7$ and $4$ during the second move.
The picture corresponding to the second test case of the example:
[Image]
There you can remove vertices $7$, $8$ and $9$ during the first move, then vertices $5$, $6$ and $10$ during the second move and vertices $1$, $3$ and $4$ during the third move.
The picture corresponding to the third test case of the example:
$\text{of}$
There you can remove vertices $5$ and $7$ during the first move, then vertices $2$ and $4$ during the second move and vertices $1$ and $6$ during the third move. | def crop_set(s, n):
new = set()
it = iter(s)
for i in range(n):
new.add(next(it))
return new
class Vertex:
__slots__ = "vertexes", "leaves", "graph"
def __init__(self, graph):
self.vertexes = set()
self.leaves = 0
self.graph = graph
def try_to_leave(self):
if len(self.vertexes) + self.leaves == 1:
if self.vertexes:
parent = self.vertexes.pop()
parent.vertexes.remove(self)
parent.leaves += 1
parent.update()
self.vertexes.add(parent)
def update(self):
self.graph[self] = self.leaves
class Tree:
def __init__(self, n, k):
self.k = k
self.lc = [set() for _ in range(n // k + 1)]
self.dlc = dict()
self.max = 0
def add_vertex(self, v):
self.dlc[v] = 0
self.lc[0].add(v)
v.update()
def __setitem__(self, key, value):
value = value // self.k
if self.max == self.dlc[key] > value:
self.lc[self.max].discard(key)
while not self.lc[self.max]:
self.max -= 1
else:
self.lc[self.dlc[key]].discard(key)
if value > self.max:
self.max = value
self.dlc[key] = value
self.lc[value].add(key)
def to_int_decrement(s):
return int(s) - 1
def solve():
n, k = list(map(int, input().split()))
tree = Tree(n, k)
graph = [Vertex(tree) for _ in range(n)]
for _ in range(n - 1):
a, b = list(map(to_int_decrement, input().split()))
graph[a].vertexes.add(graph[b])
graph[b].vertexes.add(graph[a])
if k == 1:
print(n - 1)
return
for v in graph:
tree.add_vertex(v)
for v in graph:
v.try_to_leave()
c = 0
while tree.max > 0:
v = tree.lc[tree.max].pop()
c += v.leaves // k
v.leaves -= v.leaves // k * k
v.try_to_leave()
v.update()
print(c)
for _ in range(int(input())):
solve() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR CLASS_DEF ASSIGN VAR STRING STRING STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You are given a tree (connected graph without cycles) consisting of $n$ vertices. The tree is unrooted β it is just a connected undirected graph without cycles.
In one move, you can choose exactly $k$ leaves (leaf is such a vertex that is connected to only one another vertex) connected to the same vertex and remove them with edges incident to them. I.e. you choose such leaves $u_1, u_2, \dots, u_k$ that there are edges $(u_1, v)$, $(u_2, v)$, $\dots$, $(u_k, v)$ and remove these leaves and these edges.
Your task is to find the maximum number of moves you can perform if you remove leaves optimally.
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $1 \le k < n$) β the number of vertices in the tree and the number of leaves you remove in one move, respectively. The next $n-1$ lines describe edges. The $i$-th edge is represented as two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$), where $x_i$ and $y_i$ are vertices the $i$-th edge connects. It is guaranteed that the given set of edges forms a tree.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer β the maximum number of moves you can perform if you remove leaves optimally.
-----Example-----
Input
4
8 3
1 2
1 5
7 6
6 8
3 1
6 4
6 1
10 3
1 2
1 10
2 3
1 5
1 6
2 4
7 10
10 9
8 10
7 2
3 1
4 5
3 6
7 4
1 2
1 4
5 1
1 2
2 3
4 3
5 3
Output
2
3
3
4
-----Note-----
The picture corresponding to the first test case of the example:
[Image]
There you can remove vertices $2$, $5$ and $3$ during the first move and vertices $1$, $7$ and $4$ during the second move.
The picture corresponding to the second test case of the example:
[Image]
There you can remove vertices $7$, $8$ and $9$ during the first move, then vertices $5$, $6$ and $10$ during the second move and vertices $1$, $3$ and $4$ during the third move.
The picture corresponding to the third test case of the example:
$\text{of}$
There you can remove vertices $5$ and $7$ during the first move, then vertices $2$ and $4$ during the second move and vertices $1$ and $6$ during the third move. | import sys
def rs():
return sys.stdin.readline().rstrip()
def ri():
return int(sys.stdin.readline())
def ria():
return list(map(int, sys.stdin.readline().split()))
def ws(s):
sys.stdout.write(s)
sys.stdout.write("\n")
def wi(n):
sys.stdout.write(str(n))
sys.stdout.write("\n")
def wia(a, sep=" "):
sys.stdout.write(sep.join([str(x) for x in a]))
sys.stdout.write("\n")
def solve(n, k, ee):
g = [[] for _ in range(n)]
for e in ee:
g[e[0]].append(e[1])
g[e[1]].append(e[0])
leafs = [[] for _ in range(n)]
for i in range(n):
if len(g[i]) == 1:
leafs[g[i][0]].append(i)
deg = [len(g[i]) for i in range(n)]
deleted = set()
ans = 0
q = [i for i in range(n) if len(leafs[i]) >= k]
while q:
v = q.pop()
if v in deleted:
continue
x = len(leafs[v]) // k
cnt = x * k
ans += x
while cnt > 0:
w = leafs[v].pop()
deleted.add(w)
cnt -= 1
deg[v] -= x * k
if deg[v] == 1:
for w in g[v]:
if w not in deleted:
leafs[w].append(v)
if len(leafs[w]) == k:
q.append(w)
return ans
def main():
for _ in range(ri()):
n, k = ria()
e = []
for i in range(n - 1):
xi, yi = ria()
xi -= 1
yi -= 1
e.append([xi, yi])
wi(solve(n, k, e))
main() | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL 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 EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING FUNC_DEF STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR |
You are given a tree (connected graph without cycles) consisting of $n$ vertices. The tree is unrooted β it is just a connected undirected graph without cycles.
In one move, you can choose exactly $k$ leaves (leaf is such a vertex that is connected to only one another vertex) connected to the same vertex and remove them with edges incident to them. I.e. you choose such leaves $u_1, u_2, \dots, u_k$ that there are edges $(u_1, v)$, $(u_2, v)$, $\dots$, $(u_k, v)$ and remove these leaves and these edges.
Your task is to find the maximum number of moves you can perform if you remove leaves optimally.
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $1 \le k < n$) β the number of vertices in the tree and the number of leaves you remove in one move, respectively. The next $n-1$ lines describe edges. The $i$-th edge is represented as two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$), where $x_i$ and $y_i$ are vertices the $i$-th edge connects. It is guaranteed that the given set of edges forms a tree.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer β the maximum number of moves you can perform if you remove leaves optimally.
-----Example-----
Input
4
8 3
1 2
1 5
7 6
6 8
3 1
6 4
6 1
10 3
1 2
1 10
2 3
1 5
1 6
2 4
7 10
10 9
8 10
7 2
3 1
4 5
3 6
7 4
1 2
1 4
5 1
1 2
2 3
4 3
5 3
Output
2
3
3
4
-----Note-----
The picture corresponding to the first test case of the example:
[Image]
There you can remove vertices $2$, $5$ and $3$ during the first move and vertices $1$, $7$ and $4$ during the second move.
The picture corresponding to the second test case of the example:
[Image]
There you can remove vertices $7$, $8$ and $9$ during the first move, then vertices $5$, $6$ and $10$ during the second move and vertices $1$, $3$ and $4$ during the third move.
The picture corresponding to the third test case of the example:
$\text{of}$
There you can remove vertices $5$ and $7$ during the first move, then vertices $2$ and $4$ during the second move and vertices $1$ and $6$ during the third move. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, k = map(int, input().split())
adj = [[] for _ in range(n)]
for _ in range(n - 1):
a, b = map(lambda s: int(s) - 1, input().split())
adj[a].append(b)
adj[b].append(a)
if k == 1:
print(n - 1)
continue
V = [False] * n
C = [0] * n
Q = []
def is_leaf(i):
return len(adj[i]) == 1
def enq(i):
if V[i] or sum(is_leaf(j) for j in adj[i]) < k:
return
V[i] = True
Q.append(i)
for i, a in enumerate(adj):
if len(a) == 1:
j = a[0]
C[j] += 1
if C[j] >= k and not V[j]:
V[j] = True
Q.append(j)
res = 0
for i in Q:
V[i] = False
nadj = []
leafc = []
for j in adj[i]:
if is_leaf(j):
leafc.append(j)
else:
nadj.append(j)
while len(leafc) >= k:
res += 1
for _ in range(k):
leafc.pop()
nadj.extend(leafc)
adj[i] = nadj
if is_leaf(i):
enq(nadj[0])
print(res) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a tree (connected graph without cycles) consisting of $n$ vertices. The tree is unrooted β it is just a connected undirected graph without cycles.
In one move, you can choose exactly $k$ leaves (leaf is such a vertex that is connected to only one another vertex) connected to the same vertex and remove them with edges incident to them. I.e. you choose such leaves $u_1, u_2, \dots, u_k$ that there are edges $(u_1, v)$, $(u_2, v)$, $\dots$, $(u_k, v)$ and remove these leaves and these edges.
Your task is to find the maximum number of moves you can perform if you remove leaves optimally.
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $1 \le k < n$) β the number of vertices in the tree and the number of leaves you remove in one move, respectively. The next $n-1$ lines describe edges. The $i$-th edge is represented as two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$), where $x_i$ and $y_i$ are vertices the $i$-th edge connects. It is guaranteed that the given set of edges forms a tree.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer β the maximum number of moves you can perform if you remove leaves optimally.
-----Example-----
Input
4
8 3
1 2
1 5
7 6
6 8
3 1
6 4
6 1
10 3
1 2
1 10
2 3
1 5
1 6
2 4
7 10
10 9
8 10
7 2
3 1
4 5
3 6
7 4
1 2
1 4
5 1
1 2
2 3
4 3
5 3
Output
2
3
3
4
-----Note-----
The picture corresponding to the first test case of the example:
[Image]
There you can remove vertices $2$, $5$ and $3$ during the first move and vertices $1$, $7$ and $4$ during the second move.
The picture corresponding to the second test case of the example:
[Image]
There you can remove vertices $7$, $8$ and $9$ during the first move, then vertices $5$, $6$ and $10$ during the second move and vertices $1$, $3$ and $4$ during the third move.
The picture corresponding to the third test case of the example:
$\text{of}$
There you can remove vertices $5$ and $7$ during the first move, then vertices $2$ and $4$ during the second move and vertices $1$ and $6$ during the third move. | for i in range(int(input())):
n, k = list(map(int, input().split()))
a = [[] for i in range(n)]
b = [0] * n
c = [0] * n
d = [0] * n
e = []
for i in range(n - 1):
u, v = list(map(int, input().split()))
a[u - 1].append(v)
a[v - 1].append(u)
b[u - 1] += 1
b[v - 1] += 1
if k == 1:
print(n - 1)
else:
for i in range(n):
if b[i] == 1:
d[i] = 1
for j in a[i]:
c[j - 1] += 1
z = 0
f = 0
for i in range(n):
if c[i] >= k:
e.append(i + 1)
f += 1
i = 0
while i < f:
z += c[e[i] - 1] // k
y = c[e[i] - 1] // k * k
for j in a[e[i] - 1]:
if b[j - 1] == 1:
if y == 0:
break
y -= 1
d[j - 1] = 2
c[e[i] - 1] -= 1
b[e[i] - 1] -= 1
if b[e[i] - 1] == 1:
d[e[i] - 1] = 1
for j in a[e[i] - 1]:
if d[j - 1] != 2:
c[j - 1] += 1
if c[j - 1] == k:
e.append(j)
f += 1
i += 1
print(z) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR FOR VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a tree (connected graph without cycles) consisting of $n$ vertices. The tree is unrooted β it is just a connected undirected graph without cycles.
In one move, you can choose exactly $k$ leaves (leaf is such a vertex that is connected to only one another vertex) connected to the same vertex and remove them with edges incident to them. I.e. you choose such leaves $u_1, u_2, \dots, u_k$ that there are edges $(u_1, v)$, $(u_2, v)$, $\dots$, $(u_k, v)$ and remove these leaves and these edges.
Your task is to find the maximum number of moves you can perform if you remove leaves optimally.
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $1 \le k < n$) β the number of vertices in the tree and the number of leaves you remove in one move, respectively. The next $n-1$ lines describe edges. The $i$-th edge is represented as two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$), where $x_i$ and $y_i$ are vertices the $i$-th edge connects. It is guaranteed that the given set of edges forms a tree.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer β the maximum number of moves you can perform if you remove leaves optimally.
-----Example-----
Input
4
8 3
1 2
1 5
7 6
6 8
3 1
6 4
6 1
10 3
1 2
1 10
2 3
1 5
1 6
2 4
7 10
10 9
8 10
7 2
3 1
4 5
3 6
7 4
1 2
1 4
5 1
1 2
2 3
4 3
5 3
Output
2
3
3
4
-----Note-----
The picture corresponding to the first test case of the example:
[Image]
There you can remove vertices $2$, $5$ and $3$ during the first move and vertices $1$, $7$ and $4$ during the second move.
The picture corresponding to the second test case of the example:
[Image]
There you can remove vertices $7$, $8$ and $9$ during the first move, then vertices $5$, $6$ and $10$ during the second move and vertices $1$, $3$ and $4$ during the third move.
The picture corresponding to the third test case of the example:
$\text{of}$
There you can remove vertices $5$ and $7$ during the first move, then vertices $2$ and $4$ during the second move and vertices $1$ and $6$ during the third move. | t = int(input())
Maxn = 210000
while t > 0:
t -= 1
n, k = map(int, input().split())
d = [0] * (n + 1)
bj = [0] * (n + 1)
flag = [0] * (n + 1)
to = [[]]
for i in range(0, n):
to.append([])
for i in range(1, n):
u, v = map(int, input().split())
to[u].append(v)
to[v].append(u)
d[u] += 1
d[v] += 1
if k == 1:
print(n - 1)
continue
que = []
for i in range(1, n + 1):
if d[i] == 1:
bj[to[i][0]] += 1
flag[i] = 1
for i in range(1, n + 1):
if bj[i] >= k:
que.append(i)
ans = 0
while len(que):
now = que.pop()
ans += 1
bj[now] -= k
if bj[now] >= k:
que.append(now)
continue
if bj[now]:
continue
sxz = 0
nh = 0
for i in to[now]:
if flag[i] == 0:
sxz += 1
nh = i
if sxz == 1:
flag[now] = 1
bj[nh] += 1
if bj[nh] == k:
que.append(nh)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a tree (connected graph without cycles) consisting of $n$ vertices. The tree is unrooted β it is just a connected undirected graph without cycles.
In one move, you can choose exactly $k$ leaves (leaf is such a vertex that is connected to only one another vertex) connected to the same vertex and remove them with edges incident to them. I.e. you choose such leaves $u_1, u_2, \dots, u_k$ that there are edges $(u_1, v)$, $(u_2, v)$, $\dots$, $(u_k, v)$ and remove these leaves and these edges.
Your task is to find the maximum number of moves you can perform if you remove leaves optimally.
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $1 \le k < n$) β the number of vertices in the tree and the number of leaves you remove in one move, respectively. The next $n-1$ lines describe edges. The $i$-th edge is represented as two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$), where $x_i$ and $y_i$ are vertices the $i$-th edge connects. It is guaranteed that the given set of edges forms a tree.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer β the maximum number of moves you can perform if you remove leaves optimally.
-----Example-----
Input
4
8 3
1 2
1 5
7 6
6 8
3 1
6 4
6 1
10 3
1 2
1 10
2 3
1 5
1 6
2 4
7 10
10 9
8 10
7 2
3 1
4 5
3 6
7 4
1 2
1 4
5 1
1 2
2 3
4 3
5 3
Output
2
3
3
4
-----Note-----
The picture corresponding to the first test case of the example:
[Image]
There you can remove vertices $2$, $5$ and $3$ during the first move and vertices $1$, $7$ and $4$ during the second move.
The picture corresponding to the second test case of the example:
[Image]
There you can remove vertices $7$, $8$ and $9$ during the first move, then vertices $5$, $6$ and $10$ during the second move and vertices $1$, $3$ and $4$ during the third move.
The picture corresponding to the third test case of the example:
$\text{of}$
There you can remove vertices $5$ and $7$ during the first move, then vertices $2$ and $4$ during the second move and vertices $1$ and $6$ during the third move. | import sys
def input():
return sys.stdin.readline().rstrip()
def input_split():
return [int(i) for i in input().split()]
testCases = int(input())
answers = []
for _ in range(testCases):
n, k = input_split()
present = [(True) for i in range(n)]
edges = [set() for _ in range(n)]
for _ in range(n - 1):
x, y = input_split()
x -= 1
y -= 1
edges[x].add(y)
edges[y].add(x)
deletions = 0
deleted_till_now = set()
leaves = [(0) for i in range(n)]
for i in range(n):
for node in edges[i]:
if len(edges[node]) == 1:
leaves[i] += 1
pending = []
for i in range(n):
if leaves[i] >= k:
pending.append(i)
while len(pending) != 0:
last = pending.pop()
num = leaves[last]
if num < k or not present[last]:
pass
else:
leaves_removed = num - num % k
dels = num // k
deletions += dels
to_remove = set()
for node in edges[last]:
if len(edges[node]) == 1:
to_remove.add(node)
present[node] = False
if len(to_remove) == leaves_removed:
break
edges[last] = edges[last].difference(to_remove)
leaves[last] = num % k
if len(edges[last]) == 1:
parent = list(edges[last])[0]
leaves[parent] += 1
if leaves[parent] >= k:
pending.append(parent)
answers.append(deletions)
print(*answers, sep="\n") | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING |
You are given a tree (connected graph without cycles) consisting of $n$ vertices. The tree is unrooted β it is just a connected undirected graph without cycles.
In one move, you can choose exactly $k$ leaves (leaf is such a vertex that is connected to only one another vertex) connected to the same vertex and remove them with edges incident to them. I.e. you choose such leaves $u_1, u_2, \dots, u_k$ that there are edges $(u_1, v)$, $(u_2, v)$, $\dots$, $(u_k, v)$ and remove these leaves and these edges.
Your task is to find the maximum number of moves you can perform if you remove leaves optimally.
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $1 \le k < n$) β the number of vertices in the tree and the number of leaves you remove in one move, respectively. The next $n-1$ lines describe edges. The $i$-th edge is represented as two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$), where $x_i$ and $y_i$ are vertices the $i$-th edge connects. It is guaranteed that the given set of edges forms a tree.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer β the maximum number of moves you can perform if you remove leaves optimally.
-----Example-----
Input
4
8 3
1 2
1 5
7 6
6 8
3 1
6 4
6 1
10 3
1 2
1 10
2 3
1 5
1 6
2 4
7 10
10 9
8 10
7 2
3 1
4 5
3 6
7 4
1 2
1 4
5 1
1 2
2 3
4 3
5 3
Output
2
3
3
4
-----Note-----
The picture corresponding to the first test case of the example:
[Image]
There you can remove vertices $2$, $5$ and $3$ during the first move and vertices $1$, $7$ and $4$ during the second move.
The picture corresponding to the second test case of the example:
[Image]
There you can remove vertices $7$, $8$ and $9$ during the first move, then vertices $5$, $6$ and $10$ during the second move and vertices $1$, $3$ and $4$ during the third move.
The picture corresponding to the third test case of the example:
$\text{of}$
There you can remove vertices $5$ and $7$ during the first move, then vertices $2$ and $4$ during the second move and vertices $1$ and $6$ during the third move. | from sys import gettrace, stdin
if gettrace():
inputi = input
else:
def input():
return next(stdin)[:-1]
def inputi():
return stdin.buffer.readline()
def readGraph(n, m):
adj = [set() for _ in range(n)]
for _ in range(m):
u, v = map(int, input().split())
adj[u - 1].add(v - 1)
adj[v - 1].add(u - 1)
return adj
def readTree(n):
return readGraph(n, n - 1)
def solve():
n, k = map(int, input().split())
adj = readTree(n)
if k == 1:
return n - 1, n, k
leaves = {}
lc = [0] * n
for i in range(n):
if len(adj[i]) == 1:
lc[adj[i].pop()] += 1
lc[i] = -1
st = [i for i in range(n) if lc[i] >= k]
res = 0
while st:
i = st.pop()
res += lc[i] // k
if len(adj[i]) == lc[i] + 1 and lc[i] % k == 0:
for a in adj[i]:
if lc[a] != -1:
lc[a] += 1
if lc[a] == k:
st.append(a)
lc[i] = -1
else:
lc[i] %= k
return res, n, k
def main():
printproblem = False
t = int(input())
for i in range(t):
res, _, _ = solve()
print(res)
main() | IF FUNC_CALL VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given a tree (connected graph without cycles) consisting of $n$ vertices. The tree is unrooted β it is just a connected undirected graph without cycles.
In one move, you can choose exactly $k$ leaves (leaf is such a vertex that is connected to only one another vertex) connected to the same vertex and remove them with edges incident to them. I.e. you choose such leaves $u_1, u_2, \dots, u_k$ that there are edges $(u_1, v)$, $(u_2, v)$, $\dots$, $(u_k, v)$ and remove these leaves and these edges.
Your task is to find the maximum number of moves you can perform if you remove leaves optimally.
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $1 \le k < n$) β the number of vertices in the tree and the number of leaves you remove in one move, respectively. The next $n-1$ lines describe edges. The $i$-th edge is represented as two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$), where $x_i$ and $y_i$ are vertices the $i$-th edge connects. It is guaranteed that the given set of edges forms a tree.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer β the maximum number of moves you can perform if you remove leaves optimally.
-----Example-----
Input
4
8 3
1 2
1 5
7 6
6 8
3 1
6 4
6 1
10 3
1 2
1 10
2 3
1 5
1 6
2 4
7 10
10 9
8 10
7 2
3 1
4 5
3 6
7 4
1 2
1 4
5 1
1 2
2 3
4 3
5 3
Output
2
3
3
4
-----Note-----
The picture corresponding to the first test case of the example:
[Image]
There you can remove vertices $2$, $5$ and $3$ during the first move and vertices $1$, $7$ and $4$ during the second move.
The picture corresponding to the second test case of the example:
[Image]
There you can remove vertices $7$, $8$ and $9$ during the first move, then vertices $5$, $6$ and $10$ during the second move and vertices $1$, $3$ and $4$ during the third move.
The picture corresponding to the third test case of the example:
$\text{of}$
There you can remove vertices $5$ and $7$ during the first move, then vertices $2$ and $4$ during the second move and vertices $1$ and $6$ during the third move. | import sys
input = sys.stdin.readline
for f in range(int(input())):
n, k = map(int, input().split())
neig = [0] * n
for i in range(n):
neig[i] = [0]
for i in range(n - 1):
a, b = map(int, input().split())
a -= 1
b -= 1
neig[a][0] += 1
neig[b][0] += 1
neig[a].append(b)
neig[b].append(a)
conleaves = [0] * n
for i in range(n):
conleaves[i] = [0]
goodvertices = []
mx = 0
for i in range(n):
if neig[i][0] == 1:
if neig[neig[i][1]][0] > 0:
conleaves[neig[i][1]][0] += 1
conleaves[neig[i][1]].append(i)
neig[i][0] = 0
if conleaves[neig[i][1]][0] == k:
goodvertices.append(neig[i][1])
while len(goodvertices) > 0:
v = goodvertices.pop()
rem = conleaves[v][0] // k
mx += rem
rest = conleaves[v][0] % k
conleaves[v] = conleaves[v][0 : rest + 1]
conleaves[v][0] = rest
neig[v][0] -= rem * k
if neig[v][0] == 1:
for i in range(1, len(neig[v])):
if neig[neig[v][i]][0] > 0:
neig[v][0] = 0
conleaves[neig[v][i]][0] += 1
conleaves[neig[v][i]].append(v)
if conleaves[neig[v][i]][0] == k:
goodvertices.append(neig[v][i])
print(mx) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR VAR IF VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a tree (connected graph without cycles) consisting of $n$ vertices. The tree is unrooted β it is just a connected undirected graph without cycles.
In one move, you can choose exactly $k$ leaves (leaf is such a vertex that is connected to only one another vertex) connected to the same vertex and remove them with edges incident to them. I.e. you choose such leaves $u_1, u_2, \dots, u_k$ that there are edges $(u_1, v)$, $(u_2, v)$, $\dots$, $(u_k, v)$ and remove these leaves and these edges.
Your task is to find the maximum number of moves you can perform if you remove leaves optimally.
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $1 \le k < n$) β the number of vertices in the tree and the number of leaves you remove in one move, respectively. The next $n-1$ lines describe edges. The $i$-th edge is represented as two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$), where $x_i$ and $y_i$ are vertices the $i$-th edge connects. It is guaranteed that the given set of edges forms a tree.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer β the maximum number of moves you can perform if you remove leaves optimally.
-----Example-----
Input
4
8 3
1 2
1 5
7 6
6 8
3 1
6 4
6 1
10 3
1 2
1 10
2 3
1 5
1 6
2 4
7 10
10 9
8 10
7 2
3 1
4 5
3 6
7 4
1 2
1 4
5 1
1 2
2 3
4 3
5 3
Output
2
3
3
4
-----Note-----
The picture corresponding to the first test case of the example:
[Image]
There you can remove vertices $2$, $5$ and $3$ during the first move and vertices $1$, $7$ and $4$ during the second move.
The picture corresponding to the second test case of the example:
[Image]
There you can remove vertices $7$, $8$ and $9$ during the first move, then vertices $5$, $6$ and $10$ during the second move and vertices $1$, $3$ and $4$ during the third move.
The picture corresponding to the third test case of the example:
$\text{of}$
There you can remove vertices $5$ and $7$ during the first move, then vertices $2$ and $4$ during the second move and vertices $1$ and $6$ during the third move. | 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 MI1():
return map(int1, 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]
for _ in range(II()):
n, k = MI()
to = [[] for _ in range(n)]
for _ in range(n - 1):
u, v = MI1()
to[u].append(v)
to[v].append(u)
if k == 1:
print(n - 1)
continue
dp = [0] * n
beleaf = [False] * n
leaves = [0] * n
stack = [(0, -1)]
first = [True] * n
while stack:
u, pu = stack.pop()
if first[u]:
first[u] = False
stack.append((u, pu))
for v in to[u]:
if v == pu:
continue
stack.append((v, u))
else:
dp[u] = leaves[u] // k
if pu != -1 and dp[u] * k + 1 == len(to[u]):
leaves[pu] += 1
beleaf[u] = True
for v in to[u]:
if v == pu:
continue
dp[u] += dp[v]
stack = [(0, -1)]
while stack:
u, pu = stack.pop()
for v in to[u]:
if v == pu:
continue
dp[v] = dp[u]
if (leaves[u] - beleaf[v]) % k == 0 and leaves[u] - beleaf[v] + 1 == len(
to[u]
):
leaves[v] += 1
if leaves[v] % k == 0:
dp[v] += 1
stack.append((v, u))
print(max(dp)) | 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 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 FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Nezzar has a binary string s of length n that he wants to share with his best friend, Nanako. Nanako will spend q days inspecting the binary string. At the same time, Nezzar wants to change the string s into string f during these q days, because it looks better.
It is known that Nanako loves consistency so much. On the i-th day, Nanako will inspect a segment of string s from position l_i to position r_i inclusive. If the segment contains both characters '0' and '1', Nanako becomes unhappy and throws away the string.
After this inspection, at the i-th night, Nezzar can secretly change strictly less than half of the characters in the segment from l_i to r_i inclusive, otherwise the change will be too obvious.
Now Nezzar wonders, if it is possible to avoid Nanako being unhappy and at the same time have the string become equal to the string f at the end of these q days and nights.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each test case contains two integers n,q (1 β€ n β€ 2 β
10^5, 0 β€ q β€ 2 β
10^5).
The second line of each test case contains a binary string s of length n.
The third line of each test case contains a binary string f of length n.
Then q lines follow, i-th of them contains two integers l_i,r_i (1 β€ l_i β€ r_i β€ n) β bounds of the segment, that Nanako will inspect on the i-th day.
It is guaranteed that the sum of n for all test cases doesn't exceed 2 β
10^5, and the sum of q for all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print "YES" on the single line if it is possible to avoid Nanako being unhappy and have the string f at the end of q days and nights. Otherwise, print "NO".
You can print each letter in any case (upper or lower).
Example
Input
4
5 2
00000
00111
1 5
1 3
2 1
00
01
1 2
10 6
1111111111
0110001110
1 10
5 9
7 10
1 7
3 5
6 10
5 2
10000
11000
2 5
1 3
Output
YES
NO
YES
NO
Note
In the first test case, \underline{00000} β \underline{000}11 β 00111 is one of the possible sequences of string changes.
In the second test case, it can be shown that it is impossible to have the string f at the end. | import sys
input = sys.stdin.readline
def indexes(L, R):
INDLIST = []
R -= 1
L >>= 1
R >>= 1
while L != R:
if L > R:
INDLIST.append(L)
L >>= 1
else:
INDLIST.append(R)
R >>= 1
while L != 0:
INDLIST.append(L)
L >>= 1
return INDLIST
def updates(l, r, x):
L = l + seg_el
R = r + seg_el
L //= L & -L
R //= R & -R
UPIND = indexes(L, R)
for ind in UPIND[::-1]:
if LAZY[ind] != None:
update_lazy = LAZY[ind] * (1 << seg_height - 1 - ind.bit_length())
LAZY[ind << 1] = LAZY[1 + (ind << 1)] = LAZY[ind]
SEG[ind << 1] = SEG[1 + (ind << 1)] = update_lazy
LAZY[ind] = None
while L != R:
if L > R:
SEG[L] = x * (1 << seg_height - L.bit_length())
LAZY[L] = x
L += 1
L //= L & -L
else:
R -= 1
SEG[R] = x * (1 << seg_height - R.bit_length())
LAZY[R] = x
R //= R & -R
for ind in UPIND:
SEG[ind] = SEG[ind << 1] + SEG[1 + (ind << 1)]
def getvalues(l, r):
L = l + seg_el
R = r + seg_el
L //= L & -L
R //= R & -R
UPIND = indexes(L, R)
for ind in UPIND[::-1]:
if LAZY[ind] != None:
update_lazy = LAZY[ind] * (1 << seg_height - 1 - ind.bit_length())
LAZY[ind << 1] = LAZY[1 + (ind << 1)] = LAZY[ind]
SEG[ind << 1] = SEG[1 + (ind << 1)] = update_lazy
LAZY[ind] = None
ANS = 0
while L != R:
if L > R:
ANS += SEG[L]
L += 1
L //= L & -L
else:
R -= 1
ANS += SEG[R]
R //= R & -R
return ANS
t = int(input())
for tests in range(t):
n, q = map(int, input().split())
S = input().strip()
F = input().strip()
Q = [tuple(map(int, input().split())) for i in range(q)]
seg_el = 1 << n.bit_length()
seg_height = 1 + n.bit_length()
SEG = [0] * (2 * seg_el)
LAZY = [None] * (2 * seg_el)
for i in range(n):
SEG[i + seg_el] = int(F[i])
for i in range(seg_el - 1, 0, -1):
SEG[i] = SEG[i * 2] + SEG[i * 2 + 1]
for l, r in Q[::-1]:
SUM = r - l + 1
xx = getvalues(l - 1, r)
if xx * 2 == SUM:
print("NO")
break
elif xx * 2 > SUM:
updates(l - 1, r, 1)
else:
updates(l - 1, r, 0)
else:
for i in range(n):
if getvalues(i, i + 1) == int(S[i]):
True
else:
print("NO")
break
else:
print("YES") | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR NUMBER IF VAR VAR NONE ASSIGN VAR BIN_OP VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NONE WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR NUMBER IF VAR VAR NONE ASSIGN VAR BIN_OP VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NONE ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL 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 NUMBER FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR EXPR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Nezzar has a binary string s of length n that he wants to share with his best friend, Nanako. Nanako will spend q days inspecting the binary string. At the same time, Nezzar wants to change the string s into string f during these q days, because it looks better.
It is known that Nanako loves consistency so much. On the i-th day, Nanako will inspect a segment of string s from position l_i to position r_i inclusive. If the segment contains both characters '0' and '1', Nanako becomes unhappy and throws away the string.
After this inspection, at the i-th night, Nezzar can secretly change strictly less than half of the characters in the segment from l_i to r_i inclusive, otherwise the change will be too obvious.
Now Nezzar wonders, if it is possible to avoid Nanako being unhappy and at the same time have the string become equal to the string f at the end of these q days and nights.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each test case contains two integers n,q (1 β€ n β€ 2 β
10^5, 0 β€ q β€ 2 β
10^5).
The second line of each test case contains a binary string s of length n.
The third line of each test case contains a binary string f of length n.
Then q lines follow, i-th of them contains two integers l_i,r_i (1 β€ l_i β€ r_i β€ n) β bounds of the segment, that Nanako will inspect on the i-th day.
It is guaranteed that the sum of n for all test cases doesn't exceed 2 β
10^5, and the sum of q for all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print "YES" on the single line if it is possible to avoid Nanako being unhappy and have the string f at the end of q days and nights. Otherwise, print "NO".
You can print each letter in any case (upper or lower).
Example
Input
4
5 2
00000
00111
1 5
1 3
2 1
00
01
1 2
10 6
1111111111
0110001110
1 10
5 9
7 10
1 7
3 5
6 10
5 2
10000
11000
2 5
1 3
Output
YES
NO
YES
NO
Note
In the first test case, \underline{00000} β \underline{000}11 β 00111 is one of the possible sequences of string changes.
In the second test case, it can be shown that it is impossible to have the string f at the end. | import sys
t = [0] * (4 * 200000)
lazy = [-1] * (4 * 200000)
def build(a, v, tl, tr):
if tl == tr:
t[v] = a[tl]
else:
tm = (tl + tr) // 2
build(a, v * 2, tl, tm)
build(a, v * 2 + 1, tm + 1, tr)
t[v] = t[v * 2] + t[v * 2 + 1]
return
def push(v, tl, tr):
if lazy[v] == -1:
return
tm = (tl + tr) // 2
t[v * 2] = lazy[v] * (tm - tl + 1)
lazy[v * 2] = lazy[v]
t[v * 2 + 1] = lazy[v] * (tr - tm)
lazy[v * 2 + 1] = lazy[v]
lazy[v] = -1
return
def update(v, tl, tr, l, r, setval):
if r < tl or l > tr:
return
if l <= tl and tr <= r:
t[v] = (tr - tl + 1) * setval
lazy[v] = setval
else:
push(v, tl, tr)
tm = (tl + tr) // 2
update(v * 2, tl, tm, l, r, setval)
update(v * 2 + 1, tm + 1, tr, l, r, setval)
t[v] = t[v * 2] + t[v * 2 + 1]
return
def query(v, tl, tr, l, r):
if r < tl or l > tr:
return 0
if l <= tl and tr <= r:
return t[v]
push(v, tl, tr)
tm = (tl + tr) // 2
returnVal = query(v * 2, tl, tm, l, r) + query(v * 2 + 1, tm + 1, tr, l, r)
return returnVal
def reconstruct(v, tl, tr, f2):
if tl == tr:
f2.append(t[v])
else:
push(v, tl, tr)
tm = (tl + tr) // 2
reconstruct(v * 2, tl, tm, f2)
reconstruct(v * 2 + 1, tm + 1, tr, f2)
return
def main():
z = int(input())
allans = []
for _ in range(z):
n, q = readIntArr()
sf = input()
ss = input()
lr = [None for __ in range(q)]
for i in range(q - 1, -1, -1):
l, r = readIntArr()
l -= 1
r -= 1
lr[i] = [l, r]
s = [(c - ord("0")) for c in ss]
f = [(c - ord("0")) for c in sf]
for i in range(4 * n):
t[i] = 0
lazy[i] = -1
build(s, 1, 0, n - 1)
ok = True
for i in range(q):
l, r = lr[i]
nElems = r - l + 1
total = query(1, 0, n - 1, l, r)
if total * 2 < nElems:
updateVal = 0
elif total * 2 > nElems:
updateVal = 1
else:
ok = False
break
update(1, 0, n - 1, l, r, updateVal)
if not ok:
allans.append("NO")
continue
f2 = []
reconstruct(1, 0, n - 1, f2)
ok = True
for i in range(n):
if f[i] != f2[i]:
ok = False
break
if ok:
allans.append("YES")
else:
allans.append("NO")
multiLineArrayPrint(allans)
return
input = sys.stdin.buffer.readline
def oneLineArrayPrint(arr):
print(" ".join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print("\n".join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print("\n".join([" ".join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
def makeArr(defaultValFactory, dimensionArr):
dv = defaultValFactory
da = dimensionArr
if len(da) == 1:
return [dv() for _ in range(da[0])]
else:
return [makeArr(dv, da[1:]) for _ in range(da[0])]
def queryInteractive(x, y):
print("? {} {}".format(x, y))
sys.stdout.flush()
return int(input())
def answerInteractive(ans):
print("! {}".format(ans))
sys.stdout.flush()
inf = float("inf")
MOD = 10**9 + 7
for _abc in range(1):
main() | IMPORT ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN FUNC_DEF IF VAR VAR NUMBER RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_DEF IF VAR VAR VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR LIST VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR STRING VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR |
Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.
Note:
The length of num is less than 10002 and will be β₯ k.
The given num does not contain any leading zero.
Example 1:
Input: num = "1432219", k = 3
Output: "1219"
Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
Example 2:
Input: num = "10200", k = 1
Output: "200"
Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
Example 3:
Input: num = "10", k = 2
Output: "0"
Explanation: Remove all the digits from the number and it is left with nothing which is 0. | class Solution(object):
def removeKdigits(self, num, k):
if k == len(num):
return "0"
stack = []
cnt = 0
for i, ch in enumerate(num):
if cnt == k:
for j in range(i, len(num)):
stack.append(num[j])
break
if not stack or ch >= stack[-1]:
stack.append(ch)
else:
while stack and ch < stack[-1] and cnt < k:
stack.pop()
cnt += 1
stack.append(ch)
while cnt < k:
stack.pop()
cnt += 1
i = 0
while i < len(stack) and stack[i] == "0":
i += 1
if i == len(stack):
return "0"
return "".join(stack[i:]) | CLASS_DEF VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN STRING RETURN FUNC_CALL STRING VAR VAR |
Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.
Note:
The length of num is less than 10002 and will be β₯ k.
The given num does not contain any leading zero.
Example 1:
Input: num = "1432219", k = 3
Output: "1219"
Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
Example 2:
Input: num = "10200", k = 1
Output: "200"
Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
Example 3:
Input: num = "10", k = 2
Output: "0"
Explanation: Remove all the digits from the number and it is left with nothing which is 0. | class Solution:
def removeKdigits(self, num, k):
stack = []
i = 0
num += "0"
while i < len(num):
while stack and stack[-1] > num[i] and k > 0:
stack.pop()
k -= 1
if (stack or num[i] != "0") and i < len(num) - 1:
stack.append(num[i])
i += 1
res = "".join(stack)
return res if res else "0" | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER VAR STRING WHILE VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR STRING VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR RETURN VAR VAR STRING |
Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.
Note:
The length of num is less than 10002 and will be β₯ k.
The given num does not contain any leading zero.
Example 1:
Input: num = "1432219", k = 3
Output: "1219"
Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
Example 2:
Input: num = "10200", k = 1
Output: "200"
Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
Example 3:
Input: num = "10", k = 2
Output: "0"
Explanation: Remove all the digits from the number and it is left with nothing which is 0. | class Solution:
def removeKdigits(self, num, k):
if num == "":
return ""
arr = [i for i in list(num)]
stack = [arr[0]]
out = 0
for i in range(1, len(arr)):
while out < k and len(stack) > 0 and arr[i] < stack[-1]:
stack.pop()
out += 1
stack.append(arr[i])
diff = k - out
for i in range(diff):
stack.pop()
res = "".join(stack).lstrip("0")
if res == "":
return "0"
return res | CLASS_DEF FUNC_DEF IF VAR STRING RETURN STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL STRING VAR STRING IF VAR STRING RETURN STRING RETURN VAR |
Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.
Note:
The length of num is less than 10002 and will be β₯ k.
The given num does not contain any leading zero.
Example 1:
Input: num = "1432219", k = 3
Output: "1219"
Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
Example 2:
Input: num = "10200", k = 1
Output: "200"
Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
Example 3:
Input: num = "10", k = 2
Output: "0"
Explanation: Remove all the digits from the number and it is left with nothing which is 0. | class Solution:
def removeKdigits(self, num, k):
out = []
for digit in num:
while k and out and out[-1] > digit:
out.pop()
k -= 1
out.append(digit)
return "".join(out[: -k or None]).lstrip("0") or "0" | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL FUNC_CALL STRING VAR VAR NONE STRING STRING |
Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.
Note:
The length of num is less than 10002 and will be β₯ k.
The given num does not contain any leading zero.
Example 1:
Input: num = "1432219", k = 3
Output: "1219"
Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
Example 2:
Input: num = "10200", k = 1
Output: "200"
Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
Example 3:
Input: num = "10", k = 2
Output: "0"
Explanation: Remove all the digits from the number and it is left with nothing which is 0. | class Solution:
def removeKdigits(self, num, k):
while True:
if k == 0:
return num if num != "" else "0"
if num == "":
return "0"
index = 0
num_len = len(num)
while True:
if index == num_len - 1:
num = num[:-1]
k -= 1
break
if num[index] > num[index + 1]:
new_num = num[:index] + num[index + 1 :]
num = new_num.lstrip("0")
k -= 1
break
index += 1 | CLASS_DEF FUNC_DEF WHILE NUMBER IF VAR NUMBER RETURN VAR STRING VAR STRING IF VAR STRING RETURN STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER |
Polycarp has a rectangular field of $n \times m$ cells (the size of the $n \cdot m$ field does not exceed $10^6$ cells, $m \ge 2$), in each cell of which there can be candy. There are $n$ rows and $m$ columns in the field.
Let's denote a cell with coordinates $x$ vertically and $y$ horizontally by $(x, y)$. Then the top-left cell will be denoted as $(1, 1)$, and the bottom-right cell will be denoted as $(n, m)$.
If there is candy in the cell, then the cell is marked with the symbol '1', otherwise β with the symbol '0'.
Polycarp made a Robot that can collect candy. The Robot can move from $(x, y)$ either to $(x+1, y+1)$, or to $(x+1, y-1)$. If the Robot is in a cell that contains candy, it takes it.
While there is at least one candy on the field, the following procedure is executed:
Polycarp puts the Robot in an arbitrary cell on the topmost row of the field. He himself chooses in which cell to place the Robot. It is allowed to put the Robot in the same cell multiple times.
The Robot moves across the field and collects candies. He controls the Robot.
When the Robot leaves the field, Polycarp takes it. If there are still candies left, Polycarp repeats the procedure.
Find the minimum number of times Polycarp needs to put the Robot on the topmost row of the field in order to collect all the candies. It is guaranteed that Polycarp can always collect all the candies.
-----Input-----
The first line of input data contains an integer $t$ ($1 \le t \le 10^4$) β the number of input data sets in the test.
Before each input data, there is a blank line in the test. Next is a line that contains integers $n$ and $m$ ($2 \le m$, $2 \le n \cdot m \le 10^6$) β field sizes. This is followed by $n$ lines, $i$-th of which describes the $i$-th line of the field. Each of them is a string of size $m$ characters: the symbol '1' corresponds to a cell with candy, the symbol '0' β an empty cell.
It is guaranteed that the sum of $n \cdot m$ values for all input data sets in the test does not exceed $10^6$.
-----Output-----
Print $t$ lines, each line should contain the answer to the corresponding set of input data: the minimum number of times Polycarpus needs to put the Robot on the topmost row of the field in order to collect all the candies.
-----Examples-----
Input
4
2 2
00
00
3 3
100
000
101
4 5
01000
00001
00010
10000
3 3
111
111
111
Output
0
2
2
4
-----Note-----
In the first set Polycarp may not put the Robot on the field at all, so the answer "0"
In the second set, Polycarp will need to place the robot on the field twice. The Robot can collect candies like this: for the first time Polycarp puts the Robot in the cell $(1, 1)$ and collects candies at the positions $(1, 1)$ and $(3, 3)$. The second time Polycarp can again put the Robot in $(1, 1)$, and then the Robot will move first to $(2,2)$, then to $(3, 1)$ and collect the last candy.
In the fourth set, you can show that the Robot cannot collect all the candies in three passes. | def f(s, e, l):
while s <= e:
if l[s]:
return s
s += 2
for _ in range(int(input())):
input()
n, m = map(int, input().split())
s = [input() for i in range(n)]
n -= 1
d = [0] * (n + m)
for i in range(n + m):
r, c, e = min(i, n), max(0, i - n), min(i, m - 1)
while r >= 0 and c < m:
if s[r][c] > "0":
p = f(c - r, e, d)
if p == None:
d[c - r] = 1
break
else:
d[p], d[c - r], c, r = 0, 1, (i + p) // 2, (i - p) // 2
r -= 1
c += 1
print(sum(d)) | FUNC_DEF WHILE VAR VAR IF VAR VAR RETURN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NONE ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
"Out of 6 billion humans, the troublemakers are just a handful." - Dalai Lama
Nikitasha and Mansi are best friends. They have a binary sequence $A_{1}, A_{2}, \ldots, A_{N}$ (each element of this sequence is $0$ or $1$). Their friend Sakshi always finds ways to trouble them. This time, Sakshi altered their sequence by performing the following operation $Z$ times:
Create a new sequence $B_{1}, B_{2}, \ldots, B_{N}$. Initially, $A_{i} = B_{i}$ for each valid $i$.
For each valid $i$ such that $A_{i} = 0$ and $A_{i+1} = 1$, swap $B_{i}$ and $B_{i+1}$, i.e. set $B_{i} = 1$ and $B_{i+1} = 0$. Note that the order of swaps does not matter.
Replace the sequence $A$ by the sequence $B$.
Now, Sakshi challenges Nikitasha And Mansi to find the altered sequence (the resulting sequence $A$ after $Z$ steps). Help Nikitasha and Mansi find this sequence.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $Z$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing $N$ space-separated integers β the altered sequence.
------ Constraints ------
$1 β€ T β€ 1,000$
$1 β€ N, Z β€ 10^{6}$
$0 β€ A_{i} β€ 1$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (10 points):
$1 β€ N, Z β€ 3,000$
the sum of $N$ over all test cases does not exceed $3,000$
Subtask #2 (90 points): original constraints
----- Sample Input 1 ------
2
6 2
0 1 1 0 1 1
4 4
0 1 0 1
----- Sample Output 1 ------
1 1 0 1 1 0
1 1 0 0
----- explanation 1 ------
Example case 1: After the first step, the sequence becomes $(1, 0, 1, 1, 0, 1)$. After the second step, it becomes $(1, 1, 0, 1, 1, 0)$.
Example case 2: After the first step, the sequence becomes $(1, 0, 1, 0)$. After the second step, the sequence becomes $(1, 1, 0, 0)$. Applying more steps does not alter the sequence anymore, so after the third and fourth step, the sequence remains $(1, 1, 0, 0)$. | for _ in range(int(input())):
n, z = map(int, input().split())
a = list(map(int, input().split()))
dp = []
temp = []
record = []
total = 0
ones = 0
zeros = 0
for i in range(n):
if a[i] == 1:
if len(temp) >= z and temp[-z] == 1:
total -= 1
total += 1
temp.append(1)
record.append(len(temp) - 1)
if zeros != 0:
total -= zeros
counter = min(len(temp), zeros)
for v in record[: -z - 1 : -1]:
temp[v] = 0
counter -= 1
del record[-1]
if counter == 0:
break
zeros = 0
total = max(0, total)
dp.append(total)
else:
zeros += 1
ans = [0] * n
ind = 0
for i in range(n):
if a[i] == 1:
shift = i - (z - dp[ind])
ans[max(shift, ind)] = 1
ind += 1
print(" ".join(str(v) for v in ans)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
"Out of 6 billion humans, the troublemakers are just a handful." - Dalai Lama
Nikitasha and Mansi are best friends. They have a binary sequence $A_{1}, A_{2}, \ldots, A_{N}$ (each element of this sequence is $0$ or $1$). Their friend Sakshi always finds ways to trouble them. This time, Sakshi altered their sequence by performing the following operation $Z$ times:
Create a new sequence $B_{1}, B_{2}, \ldots, B_{N}$. Initially, $A_{i} = B_{i}$ for each valid $i$.
For each valid $i$ such that $A_{i} = 0$ and $A_{i+1} = 1$, swap $B_{i}$ and $B_{i+1}$, i.e. set $B_{i} = 1$ and $B_{i+1} = 0$. Note that the order of swaps does not matter.
Replace the sequence $A$ by the sequence $B$.
Now, Sakshi challenges Nikitasha And Mansi to find the altered sequence (the resulting sequence $A$ after $Z$ steps). Help Nikitasha and Mansi find this sequence.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $Z$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing $N$ space-separated integers β the altered sequence.
------ Constraints ------
$1 β€ T β€ 1,000$
$1 β€ N, Z β€ 10^{6}$
$0 β€ A_{i} β€ 1$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (10 points):
$1 β€ N, Z β€ 3,000$
the sum of $N$ over all test cases does not exceed $3,000$
Subtask #2 (90 points): original constraints
----- Sample Input 1 ------
2
6 2
0 1 1 0 1 1
4 4
0 1 0 1
----- Sample Output 1 ------
1 1 0 1 1 0
1 1 0 0
----- explanation 1 ------
Example case 1: After the first step, the sequence becomes $(1, 0, 1, 1, 0, 1)$. After the second step, it becomes $(1, 1, 0, 1, 1, 0)$.
Example case 2: After the first step, the sequence becomes $(1, 0, 1, 0)$. After the second step, the sequence becomes $(1, 1, 0, 0)$. Applying more steps does not alter the sequence anymore, so after the third and fourth step, the sequence remains $(1, 1, 0, 0)$. | test = int(input())
while test != 0:
n, z = map(int, input().split())
a = list(map(int, input().split()))
ones = []
for i in range(n):
if a[i] == 1:
ones.append(i)
while z > 0:
flag = True
flag2 = False
cluster = False
for i in range(len(ones) - 1):
if ones[i + 1] - ones[i] > 1:
flag2 = True
continue
if flag2:
if ones[i + 1] - ones[i] == 1:
cluster = True
break
if cluster == False:
break
for i in range(len(ones)):
if i == len(ones) - 1:
if flag:
if ones[i] - 1 < i:
ones[i] = i
else:
ones[i] -= 1
elif flag:
if ones[i + 1] - ones[i] == 1:
flag = False
if ones[i] - 1 < i:
ones[i] = i
else:
ones[i] -= 1
elif ones[i + 1] - ones[i] > 1:
flag = True
z -= 1
if z > 0:
for i in range(len(ones)):
if ones[i] - z < i:
ones[i] = i
else:
ones[i] -= z
ptr = 0
for i in range(n):
if i == ones[ptr]:
print("1 ", end="")
if ptr + 1 < len(ones):
ptr += 1
else:
print("0 ", end="")
print("")
test -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING STRING IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING VAR NUMBER |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
"Out of 6 billion humans, the troublemakers are just a handful." - Dalai Lama
Nikitasha and Mansi are best friends. They have a binary sequence $A_{1}, A_{2}, \ldots, A_{N}$ (each element of this sequence is $0$ or $1$). Their friend Sakshi always finds ways to trouble them. This time, Sakshi altered their sequence by performing the following operation $Z$ times:
Create a new sequence $B_{1}, B_{2}, \ldots, B_{N}$. Initially, $A_{i} = B_{i}$ for each valid $i$.
For each valid $i$ such that $A_{i} = 0$ and $A_{i+1} = 1$, swap $B_{i}$ and $B_{i+1}$, i.e. set $B_{i} = 1$ and $B_{i+1} = 0$. Note that the order of swaps does not matter.
Replace the sequence $A$ by the sequence $B$.
Now, Sakshi challenges Nikitasha And Mansi to find the altered sequence (the resulting sequence $A$ after $Z$ steps). Help Nikitasha and Mansi find this sequence.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $Z$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing $N$ space-separated integers β the altered sequence.
------ Constraints ------
$1 β€ T β€ 1,000$
$1 β€ N, Z β€ 10^{6}$
$0 β€ A_{i} β€ 1$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (10 points):
$1 β€ N, Z β€ 3,000$
the sum of $N$ over all test cases does not exceed $3,000$
Subtask #2 (90 points): original constraints
----- Sample Input 1 ------
2
6 2
0 1 1 0 1 1
4 4
0 1 0 1
----- Sample Output 1 ------
1 1 0 1 1 0
1 1 0 0
----- explanation 1 ------
Example case 1: After the first step, the sequence becomes $(1, 0, 1, 1, 0, 1)$. After the second step, it becomes $(1, 1, 0, 1, 1, 0)$.
Example case 2: After the first step, the sequence becomes $(1, 0, 1, 0)$. After the second step, the sequence becomes $(1, 1, 0, 0)$. Applying more steps does not alter the sequence anymore, so after the third and fourth step, the sequence remains $(1, 1, 0, 0)$. | t = int(input())
for i in range(t):
n, z = map(int, input().split(" "))
bin_seq = list(map(int, input().split(" ")))
req1 = []
curr = 0
while curr <= n:
num = 0
for j in range(curr, n):
if bin_seq[j] == 0:
num += 1
else:
break
req1.append(num)
curr += num + 1
req2 = []
curr = 0
len_req1 = len(req1)
while curr < len_req1 - 1:
inx1 = -1
inx2 = -1
while curr < len_req1 - 1:
if req1[curr] == 0:
curr += 1
else:
inx1 = curr
curr += 1
break
while curr < len_req1 - 1:
if req1[curr] != 0:
curr += 1
else:
inx2 = curr
curr += 1
break
if inx1 != -1 and inx2 == -1:
inx2 = len_req1 - 1
req2.append([inx1, inx2])
elif inx1 != -1 and inx2 != -1:
req2.append([inx1, inx2])
for j in range(z):
if len(req2) == 0:
break
for k in req2:
req1[k[0]] -= 1
req1[k[1]] += 1
if k[1] != len_req1 - 1:
k[1] += 1
prev_req2 = req2
req2 = []
cons = 0
while cons < len(prev_req2):
inx1 = prev_req2[cons][0]
inx2 = prev_req2[cons][1]
if req1[inx1] == 0:
if inx1 != len(req1) - 2:
inx1 += 1
else:
cons += 1
continue
if req1[inx2] != 0 and inx2 != len(req1) - 1:
try:
while req1[inx2] != 0:
cons += 1
inx2 = prev_req2[cons][1]
except:
inx2 = len_req1 - 1
cons += 1
req2.append([inx1, inx2])
ans_str = ""
for j in req1:
for k in range(j):
ans_str += "0 "
ans_str += "1 "
print(ans_str[:-3]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR STRING FOR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR NUMBER |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
"Out of 6 billion humans, the troublemakers are just a handful." - Dalai Lama
Nikitasha and Mansi are best friends. They have a binary sequence $A_{1}, A_{2}, \ldots, A_{N}$ (each element of this sequence is $0$ or $1$). Their friend Sakshi always finds ways to trouble them. This time, Sakshi altered their sequence by performing the following operation $Z$ times:
Create a new sequence $B_{1}, B_{2}, \ldots, B_{N}$. Initially, $A_{i} = B_{i}$ for each valid $i$.
For each valid $i$ such that $A_{i} = 0$ and $A_{i+1} = 1$, swap $B_{i}$ and $B_{i+1}$, i.e. set $B_{i} = 1$ and $B_{i+1} = 0$. Note that the order of swaps does not matter.
Replace the sequence $A$ by the sequence $B$.
Now, Sakshi challenges Nikitasha And Mansi to find the altered sequence (the resulting sequence $A$ after $Z$ steps). Help Nikitasha and Mansi find this sequence.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $Z$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing $N$ space-separated integers β the altered sequence.
------ Constraints ------
$1 β€ T β€ 1,000$
$1 β€ N, Z β€ 10^{6}$
$0 β€ A_{i} β€ 1$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (10 points):
$1 β€ N, Z β€ 3,000$
the sum of $N$ over all test cases does not exceed $3,000$
Subtask #2 (90 points): original constraints
----- Sample Input 1 ------
2
6 2
0 1 1 0 1 1
4 4
0 1 0 1
----- Sample Output 1 ------
1 1 0 1 1 0
1 1 0 0
----- explanation 1 ------
Example case 1: After the first step, the sequence becomes $(1, 0, 1, 1, 0, 1)$. After the second step, it becomes $(1, 1, 0, 1, 1, 0)$.
Example case 2: After the first step, the sequence becomes $(1, 0, 1, 0)$. After the second step, the sequence becomes $(1, 1, 0, 0)$. Applying more steps does not alter the sequence anymore, so after the third and fourth step, the sequence remains $(1, 1, 0, 0)$. | def find(arr):
if arr == []:
return []
elif arr[-1] == 0:
return find(arr[:-1])
else:
aux = []
count = 0
for i in range(1, len(arr) + 1):
if arr[-i] == 0:
aux += [arr[-i]]
count += 1
elif arr[-i] == 1:
if count > 0:
count -= 1
else:
aux += [1]
aux = aux[::-1]
return aux
t = int(input())
for _ in range(t):
n, z = list(map(int, input().split()))
arr = list(map(int, input().split()))
onespos = {i: float("inf") for i in range(1, len(arr) + 1)}
jumps = []
currentones = 0
currentzeros = 0
numones = 0
pos = 0
count = 0
for i in range(len(arr)):
if arr[i] == 1:
pos = i
numones = 1
currentones = 1
currentzeros = min(i, z)
jumps += [min(i, z)]
onespos[1] = i
break
for i in range(pos + 1, len(arr)):
if arr[i] == 0:
count += 1
else:
currentones += 1
numones -= count
if numones <= 0:
pos = i - currentones + 1
jumps += [min(z, pos)]
numones = 1
onespos[1] = pos
count = 0
currentzeros = min(z, pos)
else:
pos += 1
till = onespos[numones]
if pos - z <= 0:
currentzeros = currentzeros + count
else:
numone = z - currentzeros
if count <= numone:
for i in range(1, len(arr) + 1):
if onespos[i] > pos - z - 1:
currentzeros = currentzeros + count - 1
break
elif onespos[i] == pos - z - 1:
currentzeros = currentzeros + count
break
else:
count = numone
currentzeros += count
jumps += [currentzeros]
numones += 1
onespos[numones] = pos
count = 0
ans = [(0) for i in range(len(arr))]
for i in range(len(arr)):
if arr[i] == 1:
ans[i - jumps[0]] = 1
jumps.pop(0)
ans = list(map(str, ans))
print(" ".join(ans)) | FUNC_DEF IF VAR LIST RETURN LIST IF VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR LIST VAR VAR VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR LIST NUMBER ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR LIST FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR LIST FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR LIST VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
"Out of 6 billion humans, the troublemakers are just a handful." - Dalai Lama
Nikitasha and Mansi are best friends. They have a binary sequence $A_{1}, A_{2}, \ldots, A_{N}$ (each element of this sequence is $0$ or $1$). Their friend Sakshi always finds ways to trouble them. This time, Sakshi altered their sequence by performing the following operation $Z$ times:
Create a new sequence $B_{1}, B_{2}, \ldots, B_{N}$. Initially, $A_{i} = B_{i}$ for each valid $i$.
For each valid $i$ such that $A_{i} = 0$ and $A_{i+1} = 1$, swap $B_{i}$ and $B_{i+1}$, i.e. set $B_{i} = 1$ and $B_{i+1} = 0$. Note that the order of swaps does not matter.
Replace the sequence $A$ by the sequence $B$.
Now, Sakshi challenges Nikitasha And Mansi to find the altered sequence (the resulting sequence $A$ after $Z$ steps). Help Nikitasha and Mansi find this sequence.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $Z$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing $N$ space-separated integers β the altered sequence.
------ Constraints ------
$1 β€ T β€ 1,000$
$1 β€ N, Z β€ 10^{6}$
$0 β€ A_{i} β€ 1$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (10 points):
$1 β€ N, Z β€ 3,000$
the sum of $N$ over all test cases does not exceed $3,000$
Subtask #2 (90 points): original constraints
----- Sample Input 1 ------
2
6 2
0 1 1 0 1 1
4 4
0 1 0 1
----- Sample Output 1 ------
1 1 0 1 1 0
1 1 0 0
----- explanation 1 ------
Example case 1: After the first step, the sequence becomes $(1, 0, 1, 1, 0, 1)$. After the second step, it becomes $(1, 1, 0, 1, 1, 0)$.
Example case 2: After the first step, the sequence becomes $(1, 0, 1, 0)$. After the second step, the sequence becomes $(1, 1, 0, 0)$. Applying more steps does not alter the sequence anymore, so after the third and fourth step, the sequence remains $(1, 1, 0, 0)$. | t = int(input())
for naman in range(t):
n, z = map(int, input().split())
l = list(map(int, input().split()))
listof1 = []
ans = [0] * n
for i in range(len(l)):
if l[i] == 1:
listof1.append(i)
n1 = len(listof1)
for i in range(n1 - 1):
if listof1[i + 1] - listof1[i] == 1:
cont = True
break
k = z
while cont and k > 0:
flag2 = 0
flag = 0
flag3 = 0
for i in range(n1):
if flag == 1:
if i != n1 - 1:
if listof1[i + 1] - listof1[i] != 1:
flag = 0
elif i != n1 - 1 and i != 0:
if listof1[i + 1] - listof1[i] == 1:
listof1[i] = max(listof1[i - 1] + 1, listof1[i] - 1)
flag = 1
else:
listof1[i] = max(listof1[i - 1] + 1, listof1[i] - 1)
flag = 0
elif i == 0:
if listof1[i + 1] - listof1[i] == 1:
listof1[i] = max(listof1[i] - 1, 0)
flag = 1
else:
listof1[i] = max(listof1[i] - 1, 0)
flag = 0
else:
listof1[i] = max(listof1[i - 1] + 1, listof1[i] - 1)
k = k - 1
for i in range(n1 - 1):
if flag3 == 0:
if listof1[i + 1] - listof1[i] != 1:
flag3 = 1
elif listof1[i + 1] - listof1[i] == 1:
cont = True
flag2 = 1
break
if flag2 == 0:
cont = False
if k > 0:
for i in range(n1):
if i == 0:
listof1[i] = max(0, listof1[i] - k)
else:
listof1[i] = max(listof1[i - 1] + 1, listof1[i] - k)
for i in listof1:
ans[i] = 1
for i in ans:
print(i, end=" ")
else:
for i in listof1:
ans[i] = 1
for i in ans:
print(i, end=" ")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Alice has a very important message M consisting of some non-negative integers that she wants to keep secret from Eve. Alice knows that the only theoretically secure cipher is one-time pad. Alice generates a random key K of the length equal to the message's length. Alice computes the bitwise xor of each element of the message and the key (<image>, where <image> denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)) and stores this encrypted message A. Alice is smart. Be like Alice.
For example, Alice may have wanted to store a message M = (0, 15, 9, 18). She generated a key K = (16, 7, 6, 3). The encrypted message is thus A = (16, 8, 15, 17).
Alice realised that she cannot store the key with the encrypted message. Alice sent her key K to Bob and deleted her own copy. Alice is smart. Really, be like Alice.
Bob realised that the encrypted message is only secure as long as the key is secret. Bob thus randomly permuted the key before storing it. Bob thinks that this way, even if Eve gets both the encrypted message and the key, she will not be able to read the message. Bob is not smart. Don't be like Bob.
In the above example, Bob may have, for instance, selected a permutation (3, 4, 1, 2) and stored the permuted key P = (6, 3, 16, 7).
One year has passed and Alice wants to decrypt her message. Only now Bob has realised that this is impossible. As he has permuted the key randomly, the message is lost forever. Did we mention that Bob isn't smart?
Bob wants to salvage at least some information from the message. Since he is not so smart, he asks for your help. You know the encrypted message A and the permuted key P. What is the lexicographically smallest message that could have resulted in the given encrypted text?
More precisely, for given A and P, find the lexicographically smallest message O, for which there exists a permutation Ο such that <image> for every i.
Note that the sequence S is lexicographically smaller than the sequence T, if there is an index i such that Si < Ti and for all j < i the condition Sj = Tj holds.
Input
The first line contains a single integer N (1 β€ N β€ 300000), the length of the message.
The second line contains N integers A1, A2, ..., AN (0 β€ Ai < 230) representing the encrypted message.
The third line contains N integers P1, P2, ..., PN (0 β€ Pi < 230) representing the permuted encryption key.
Output
Output a single line with N integers, the lexicographically smallest possible message O. Note that all its elements should be non-negative.
Examples
Input
3
8 4 13
17 2 7
Output
10 3 28
Input
5
12 7 87 22 11
18 39 9 12 16
Output
0 14 69 6 44
Input
10
331415699 278745619 998190004 423175621 42983144 166555524 843586353 802130100 337889448 685310951
226011312 266003835 342809544 504667531 529814910 684873393 817026985 844010788 993949858 1031395667
Output
128965467 243912600 4281110 112029883 223689619 76924724 429589 119397893 613490433 362863284
Note
In the first case, the solution is (10, 3, 28), since <image>, <image> and <image>. Other possible permutations of key yield messages (25, 6, 10), (25, 3, 15), (10, 21, 10), (15, 21, 15) and (15, 6, 28), which are all lexicographically larger than the solution. | n = int(input())
ai = list(map(int, input().split()))
pi = list(map(int, input().split()))
oi = []
class node:
def __init__(self, data):
self.data = data
self.right = None
self.left = None
self.val = None
self.count = 0
class trie:
def __init__(self):
self.root = node("0")
def insert(self, data):
self.active = self.root
for i in data:
if i == "1":
if self.active.right:
self.active = self.active.right
else:
self.active.right = node("1")
self.active = self.active.right
if i == "0":
if self.active.left:
self.active = self.active.left
else:
self.active.left = node("0")
self.active = self.active.left
self.active.count += 1
self.active.val = int(data, 2)
def func(self, data):
self.active = self.root
for i in data:
if i == "0":
if self.active.left and self.active.left.count > 0:
self.active = self.active.left
else:
self.active = self.active.right
elif self.active.right and self.active.right.count > 0:
self.active = self.active.right
else:
self.active = self.active.left
self.active.count -= 1
return int(data, 2) ^ self.active.val
t = trie()
for i in pi:
t.insert(bin(i)[2:].rjust(31, "0"))
for i in ai:
oi.append(t.func(bin(i)[2:].rjust(31, "0")))
print(*oi) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR VAR FOR VAR VAR IF VAR STRING IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR IF VAR STRING IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR FOR VAR VAR IF VAR STRING IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER STRING FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR VAR |
Alice has a very important message M consisting of some non-negative integers that she wants to keep secret from Eve. Alice knows that the only theoretically secure cipher is one-time pad. Alice generates a random key K of the length equal to the message's length. Alice computes the bitwise xor of each element of the message and the key (<image>, where <image> denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)) and stores this encrypted message A. Alice is smart. Be like Alice.
For example, Alice may have wanted to store a message M = (0, 15, 9, 18). She generated a key K = (16, 7, 6, 3). The encrypted message is thus A = (16, 8, 15, 17).
Alice realised that she cannot store the key with the encrypted message. Alice sent her key K to Bob and deleted her own copy. Alice is smart. Really, be like Alice.
Bob realised that the encrypted message is only secure as long as the key is secret. Bob thus randomly permuted the key before storing it. Bob thinks that this way, even if Eve gets both the encrypted message and the key, she will not be able to read the message. Bob is not smart. Don't be like Bob.
In the above example, Bob may have, for instance, selected a permutation (3, 4, 1, 2) and stored the permuted key P = (6, 3, 16, 7).
One year has passed and Alice wants to decrypt her message. Only now Bob has realised that this is impossible. As he has permuted the key randomly, the message is lost forever. Did we mention that Bob isn't smart?
Bob wants to salvage at least some information from the message. Since he is not so smart, he asks for your help. You know the encrypted message A and the permuted key P. What is the lexicographically smallest message that could have resulted in the given encrypted text?
More precisely, for given A and P, find the lexicographically smallest message O, for which there exists a permutation Ο such that <image> for every i.
Note that the sequence S is lexicographically smaller than the sequence T, if there is an index i such that Si < Ti and for all j < i the condition Sj = Tj holds.
Input
The first line contains a single integer N (1 β€ N β€ 300000), the length of the message.
The second line contains N integers A1, A2, ..., AN (0 β€ Ai < 230) representing the encrypted message.
The third line contains N integers P1, P2, ..., PN (0 β€ Pi < 230) representing the permuted encryption key.
Output
Output a single line with N integers, the lexicographically smallest possible message O. Note that all its elements should be non-negative.
Examples
Input
3
8 4 13
17 2 7
Output
10 3 28
Input
5
12 7 87 22 11
18 39 9 12 16
Output
0 14 69 6 44
Input
10
331415699 278745619 998190004 423175621 42983144 166555524 843586353 802130100 337889448 685310951
226011312 266003835 342809544 504667531 529814910 684873393 817026985 844010788 993949858 1031395667
Output
128965467 243912600 4281110 112029883 223689619 76924724 429589 119397893 613490433 362863284
Note
In the first case, the solution is (10, 3, 28), since <image>, <image> and <image>. Other possible permutations of key yield messages (25, 6, 10), (25, 3, 15), (10, 21, 10), (15, 21, 15) and (15, 6, 28), which are all lexicographically larger than the solution. | from sys import stdin
input = stdin.readline
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
self.count = 0
class Trie:
def __init__(self):
self.root = Node(0)
def insert(self, preXor):
self.temp = self.root
for i in range(31, -1, -1):
val = preXor & 1 << i
if val:
if not self.temp.right:
self.temp.right = Node(0)
self.temp = self.temp.right
self.temp.count += 1
else:
if not self.temp.left:
self.temp.left = Node(0)
self.temp = self.temp.left
self.temp.count += 1
self.temp.data = preXor
def query(self, val):
self.temp = self.root
for i in range(31, -1, -1):
active = val & 1 << i
if active:
if self.temp.right and self.temp.right.count > 0:
self.temp = self.temp.right
elif self.temp.left:
self.temp = self.temp.left
elif self.temp.left and self.temp.left.count > 0:
self.temp = self.temp.left
elif self.temp.right:
self.temp = self.temp.right
self.temp.count -= 1
return val ^ self.temp.data
n = input()
l1 = list(map(int, input().strip().split()))
l2 = list(map(int, input().strip().split()))
trie = Trie()
for i in l2:
trie.insert(i)
for i in l1:
print(trie.query(i), end=" ") | ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR IF VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING |
Alice has a very important message M consisting of some non-negative integers that she wants to keep secret from Eve. Alice knows that the only theoretically secure cipher is one-time pad. Alice generates a random key K of the length equal to the message's length. Alice computes the bitwise xor of each element of the message and the key (<image>, where <image> denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)) and stores this encrypted message A. Alice is smart. Be like Alice.
For example, Alice may have wanted to store a message M = (0, 15, 9, 18). She generated a key K = (16, 7, 6, 3). The encrypted message is thus A = (16, 8, 15, 17).
Alice realised that she cannot store the key with the encrypted message. Alice sent her key K to Bob and deleted her own copy. Alice is smart. Really, be like Alice.
Bob realised that the encrypted message is only secure as long as the key is secret. Bob thus randomly permuted the key before storing it. Bob thinks that this way, even if Eve gets both the encrypted message and the key, she will not be able to read the message. Bob is not smart. Don't be like Bob.
In the above example, Bob may have, for instance, selected a permutation (3, 4, 1, 2) and stored the permuted key P = (6, 3, 16, 7).
One year has passed and Alice wants to decrypt her message. Only now Bob has realised that this is impossible. As he has permuted the key randomly, the message is lost forever. Did we mention that Bob isn't smart?
Bob wants to salvage at least some information from the message. Since he is not so smart, he asks for your help. You know the encrypted message A and the permuted key P. What is the lexicographically smallest message that could have resulted in the given encrypted text?
More precisely, for given A and P, find the lexicographically smallest message O, for which there exists a permutation Ο such that <image> for every i.
Note that the sequence S is lexicographically smaller than the sequence T, if there is an index i such that Si < Ti and for all j < i the condition Sj = Tj holds.
Input
The first line contains a single integer N (1 β€ N β€ 300000), the length of the message.
The second line contains N integers A1, A2, ..., AN (0 β€ Ai < 230) representing the encrypted message.
The third line contains N integers P1, P2, ..., PN (0 β€ Pi < 230) representing the permuted encryption key.
Output
Output a single line with N integers, the lexicographically smallest possible message O. Note that all its elements should be non-negative.
Examples
Input
3
8 4 13
17 2 7
Output
10 3 28
Input
5
12 7 87 22 11
18 39 9 12 16
Output
0 14 69 6 44
Input
10
331415699 278745619 998190004 423175621 42983144 166555524 843586353 802130100 337889448 685310951
226011312 266003835 342809544 504667531 529814910 684873393 817026985 844010788 993949858 1031395667
Output
128965467 243912600 4281110 112029883 223689619 76924724 429589 119397893 613490433 362863284
Note
In the first case, the solution is (10, 3, 28), since <image>, <image> and <image>. Other possible permutations of key yield messages (25, 6, 10), (25, 3, 15), (10, 21, 10), (15, 21, 15) and (15, 6, 28), which are all lexicographically larger than the solution. | def add(x):
global tree
now = 0
tree[now][2] += 1
for i in range(29, -1, -1):
bit = x >> i & 1
if tree[now][bit] == 0:
tree[now][bit] = len(tree)
tree.append([0, 0, 0])
now = tree[now][bit]
tree[now][2] += 1
def find_min(x):
global tree
now = ans = 0
for i in range(29, -1, -1):
bit = x >> i & 1
if tree[now][bit] and tree[tree[now][bit]][2]:
now = tree[now][bit]
else:
now = tree[now][bit ^ 1]
ans |= 1 << i
tree[now][2] -= 1
return ans
tree = [[0, 0, 0]]
n = int(input())
a = list(map(int, input().split()))
list(map(add, map(int, input().split())))
[print(x, end=" ") for x in list(map(find_min, a))] | FUNC_DEF ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR NUMBER NUMBER RETURN VAR ASSIGN VAR LIST LIST 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You are given two arrays: an array $a$ consisting of $n$ zeros and an array $b$ consisting of $n$ integers.
You can apply the following operation to the array $a$ an arbitrary number of times: choose some subsegment of $a$ of length $k$ and add the arithmetic progression $1, 2, \ldots, k$ to this subsegment β i. e. add $1$ to the first element of the subsegment, $2$ to the second element, and so on. The chosen subsegment should be inside the borders of the array $a$ (i.e., if the left border of the chosen subsegment is $l$, then the condition $1 \le l \le l + k - 1 \le n$ should be satisfied). Note that the progression added is always $1, 2, \ldots, k$ but not the $k, k - 1, \ldots, 1$ or anything else (i.e., the leftmost element of the subsegment always increases by $1$, the second element always increases by $2$ and so on).
Your task is to find the minimum possible number of operations required to satisfy the condition $a_i \ge b_i$ for each $i$ from $1$ to $n$. Note that the condition $a_i \ge b_i$ should be satisfied for all elements at once.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 3 \cdot 10^5$) β the number of elements in both arrays and the length of the subsegment, respectively.
The second line of the input contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 10^{12}$), where $b_i$ is the $i$-th element of the array $b$.
-----Output-----
Print one integer β the minimum possible number of operations required to satisfy the condition $a_i \ge b_i$ for each $i$ from $1$ to $n$.
-----Examples-----
Input
3 3
5 4 6
Output
5
Input
6 3
1 2 3 2 2 3
Output
3
Input
6 3
1 2 4 1 2 3
Output
3
Input
7 3
50 17 81 25 42 39 96
Output
92
-----Note-----
Consider the first example. In this test, we don't really have any choice, so we need to add at least five progressions to make the first element equals $5$. The array $a$ becomes $[5, 10, 15]$.
Consider the second example. In this test, let's add one progression on the segment $[1; 3]$ and two progressions on the segment $[4; 6]$. Then, the array $a$ becomes $[1, 2, 3, 2, 4, 6]$. | n, k = map(int, input().split())
b = list(map(int, input().split()))
ans, sum, cnt = 0, 0, 0
closed = [0] * n
for i in range(n - 1, -1, -1):
sum -= cnt
b[i] -= sum
cnt -= closed[i]
if b[i] <= 0:
continue
delta = min(i + 1, k)
x = (b[i] + delta - 1) // delta
cnt += x
sum += x * delta
ans += x
closed[i - k] += x
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given two arrays: an array $a$ consisting of $n$ zeros and an array $b$ consisting of $n$ integers.
You can apply the following operation to the array $a$ an arbitrary number of times: choose some subsegment of $a$ of length $k$ and add the arithmetic progression $1, 2, \ldots, k$ to this subsegment β i. e. add $1$ to the first element of the subsegment, $2$ to the second element, and so on. The chosen subsegment should be inside the borders of the array $a$ (i.e., if the left border of the chosen subsegment is $l$, then the condition $1 \le l \le l + k - 1 \le n$ should be satisfied). Note that the progression added is always $1, 2, \ldots, k$ but not the $k, k - 1, \ldots, 1$ or anything else (i.e., the leftmost element of the subsegment always increases by $1$, the second element always increases by $2$ and so on).
Your task is to find the minimum possible number of operations required to satisfy the condition $a_i \ge b_i$ for each $i$ from $1$ to $n$. Note that the condition $a_i \ge b_i$ should be satisfied for all elements at once.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 3 \cdot 10^5$) β the number of elements in both arrays and the length of the subsegment, respectively.
The second line of the input contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 10^{12}$), where $b_i$ is the $i$-th element of the array $b$.
-----Output-----
Print one integer β the minimum possible number of operations required to satisfy the condition $a_i \ge b_i$ for each $i$ from $1$ to $n$.
-----Examples-----
Input
3 3
5 4 6
Output
5
Input
6 3
1 2 3 2 2 3
Output
3
Input
6 3
1 2 4 1 2 3
Output
3
Input
7 3
50 17 81 25 42 39 96
Output
92
-----Note-----
Consider the first example. In this test, we don't really have any choice, so we need to add at least five progressions to make the first element equals $5$. The array $a$ becomes $[5, 10, 15]$.
Consider the second example. In this test, let's add one progression on the segment $[1; 3]$ and two progressions on the segment $[4; 6]$. Then, the array $a$ becomes $[1, 2, 3, 2, 4, 6]$. | n, k = map(int, input().split())
b = list(map(int, input().split()))
ans, a = 0, 0
d2 = [0] * n
d = 0
for i in range(n - 1, -1, -1):
d += d2[i]
a += d
if a >= b[i]:
continue
v = min(k, i + 1)
x = (b[i] - a + v - 1) // v
a += v * x
ans += x
if i > 0:
d2[i - 1] -= x
if i - k - 1 >= 0:
d2[i - 1 - k] += x
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are given two arrays: an array $a$ consisting of $n$ zeros and an array $b$ consisting of $n$ integers.
You can apply the following operation to the array $a$ an arbitrary number of times: choose some subsegment of $a$ of length $k$ and add the arithmetic progression $1, 2, \ldots, k$ to this subsegment β i. e. add $1$ to the first element of the subsegment, $2$ to the second element, and so on. The chosen subsegment should be inside the borders of the array $a$ (i.e., if the left border of the chosen subsegment is $l$, then the condition $1 \le l \le l + k - 1 \le n$ should be satisfied). Note that the progression added is always $1, 2, \ldots, k$ but not the $k, k - 1, \ldots, 1$ or anything else (i.e., the leftmost element of the subsegment always increases by $1$, the second element always increases by $2$ and so on).
Your task is to find the minimum possible number of operations required to satisfy the condition $a_i \ge b_i$ for each $i$ from $1$ to $n$. Note that the condition $a_i \ge b_i$ should be satisfied for all elements at once.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 3 \cdot 10^5$) β the number of elements in both arrays and the length of the subsegment, respectively.
The second line of the input contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 10^{12}$), where $b_i$ is the $i$-th element of the array $b$.
-----Output-----
Print one integer β the minimum possible number of operations required to satisfy the condition $a_i \ge b_i$ for each $i$ from $1$ to $n$.
-----Examples-----
Input
3 3
5 4 6
Output
5
Input
6 3
1 2 3 2 2 3
Output
3
Input
6 3
1 2 4 1 2 3
Output
3
Input
7 3
50 17 81 25 42 39 96
Output
92
-----Note-----
Consider the first example. In this test, we don't really have any choice, so we need to add at least five progressions to make the first element equals $5$. The array $a$ becomes $[5, 10, 15]$.
Consider the second example. In this test, let's add one progression on the segment $[1; 3]$ and two progressions on the segment $[4; 6]$. Then, the array $a$ becomes $[1, 2, 3, 2, 4, 6]$. | if True:
n, k = list(map(int, input().split()))
b = list(map(int, input().split()))
closed = [0] * len(b)
sum = 0
cnt = 0
ans = 0
for i in range(len(b) - 1, -1, -1):
sum -= cnt
cnt -= closed[i]
b[i] -= sum
if b[i] <= 0:
continue
el = min(k, i + 1)
f = b[i] // el + (1 if b[i] % el != 0 else 0)
sum += f * el
cnt += f
ans += f
if i >= k:
closed[i - k] += f
print(ans) | IF NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given two arrays: an array $a$ consisting of $n$ zeros and an array $b$ consisting of $n$ integers.
You can apply the following operation to the array $a$ an arbitrary number of times: choose some subsegment of $a$ of length $k$ and add the arithmetic progression $1, 2, \ldots, k$ to this subsegment β i. e. add $1$ to the first element of the subsegment, $2$ to the second element, and so on. The chosen subsegment should be inside the borders of the array $a$ (i.e., if the left border of the chosen subsegment is $l$, then the condition $1 \le l \le l + k - 1 \le n$ should be satisfied). Note that the progression added is always $1, 2, \ldots, k$ but not the $k, k - 1, \ldots, 1$ or anything else (i.e., the leftmost element of the subsegment always increases by $1$, the second element always increases by $2$ and so on).
Your task is to find the minimum possible number of operations required to satisfy the condition $a_i \ge b_i$ for each $i$ from $1$ to $n$. Note that the condition $a_i \ge b_i$ should be satisfied for all elements at once.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 3 \cdot 10^5$) β the number of elements in both arrays and the length of the subsegment, respectively.
The second line of the input contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 10^{12}$), where $b_i$ is the $i$-th element of the array $b$.
-----Output-----
Print one integer β the minimum possible number of operations required to satisfy the condition $a_i \ge b_i$ for each $i$ from $1$ to $n$.
-----Examples-----
Input
3 3
5 4 6
Output
5
Input
6 3
1 2 3 2 2 3
Output
3
Input
6 3
1 2 4 1 2 3
Output
3
Input
7 3
50 17 81 25 42 39 96
Output
92
-----Note-----
Consider the first example. In this test, we don't really have any choice, so we need to add at least five progressions to make the first element equals $5$. The array $a$ becomes $[5, 10, 15]$.
Consider the second example. In this test, let's add one progression on the segment $[1; 3]$ and two progressions on the segment $[4; 6]$. Then, the array $a$ becomes $[1, 2, 3, 2, 4, 6]$. | n, k = [*map(int, input().split())]
b = [*map(int, input().split())]
d = [0] * n
s = total = 0
for i in range(n - 1, -1, -1):
b[i] -= total
if b[i] > 0:
dd = min(k, i + 1)
d[i] = (b[i] + dd - 1) // dd
s += d[i] - (d[i + k] if i + k < n else 0)
total += d[i] * dd - s
print(sum(d)) | ASSIGN VAR VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given two arrays: an array $a$ consisting of $n$ zeros and an array $b$ consisting of $n$ integers.
You can apply the following operation to the array $a$ an arbitrary number of times: choose some subsegment of $a$ of length $k$ and add the arithmetic progression $1, 2, \ldots, k$ to this subsegment β i. e. add $1$ to the first element of the subsegment, $2$ to the second element, and so on. The chosen subsegment should be inside the borders of the array $a$ (i.e., if the left border of the chosen subsegment is $l$, then the condition $1 \le l \le l + k - 1 \le n$ should be satisfied). Note that the progression added is always $1, 2, \ldots, k$ but not the $k, k - 1, \ldots, 1$ or anything else (i.e., the leftmost element of the subsegment always increases by $1$, the second element always increases by $2$ and so on).
Your task is to find the minimum possible number of operations required to satisfy the condition $a_i \ge b_i$ for each $i$ from $1$ to $n$. Note that the condition $a_i \ge b_i$ should be satisfied for all elements at once.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 3 \cdot 10^5$) β the number of elements in both arrays and the length of the subsegment, respectively.
The second line of the input contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 10^{12}$), where $b_i$ is the $i$-th element of the array $b$.
-----Output-----
Print one integer β the minimum possible number of operations required to satisfy the condition $a_i \ge b_i$ for each $i$ from $1$ to $n$.
-----Examples-----
Input
3 3
5 4 6
Output
5
Input
6 3
1 2 3 2 2 3
Output
3
Input
6 3
1 2 4 1 2 3
Output
3
Input
7 3
50 17 81 25 42 39 96
Output
92
-----Note-----
Consider the first example. In this test, we don't really have any choice, so we need to add at least five progressions to make the first element equals $5$. The array $a$ becomes $[5, 10, 15]$.
Consider the second example. In this test, let's add one progression on the segment $[1; 3]$ and two progressions on the segment $[4; 6]$. Then, the array $a$ becomes $[1, 2, 3, 2, 4, 6]$. | def main():
n, k = map(int, input().split())
b = list(map(int, input().split()))
temp = 0
operations = [0] * n
last = 0
for i in range(n - 1, k - 1, -1):
now = 0
if i != n - 1:
last -= temp
now = last
if i + k >= n:
pass
else:
temp -= operations[i + k]
delta = b[i] - now
if delta <= 0:
continue
else:
operations[i] = (delta + k - 1) // k
temp += operations[i]
last = operations[i] * k + now
m = 0
need = [0] * k
for i in range(k - 1, -1, -1):
if i != n - 1:
last -= temp
need[i] = max(0, b[i] - last)
if i + k >= n:
pass
else:
temp -= operations[i + k]
else:
need[i] = max(0, b[i])
for i in range(k):
m = max(m, (need[i] + i) // (i + 1))
print(sum(operations) + m)
main() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...
There are two arrays $a$ and $b$ of length $n$. Initially, an $ans$ is equal to $0$ and the following operation is defined: Choose position $i$ ($1 \le i \le n$); Add $a_i$ to $ans$; If $b_i \neq -1$ then add $a_i$ to $a_{b_i}$.
What is the maximum $ans$ you can get by performing the operation on each $i$ ($1 \le i \le n$) exactly once?
Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.
-----Input-----
The first line contains the integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β the length of arrays $a$ and $b$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($β10^6 \le a_i \le 10^6$).
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$ or $b_i = -1$).
Additional constraint: it's guaranteed that for any $i$ ($1 \le i \le n$) the sequence $b_i, b_{b_i}, b_{b_{b_i}}, \ldots$ is not cyclic, in other words it will always end with $-1$.
-----Output-----
In the first line, print the maximum $ans$ you can get.
In the second line, print the order of operations: $n$ different integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). The $p_i$ is the position which should be chosen at the $i$-th step. If there are multiple orders, print any of them.
-----Examples-----
Input
3
1 2 3
2 3 -1
Output
10
1 2 3
Input
2
-1 100
2 -1
Output
99
2 1
Input
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9
Output
-9
3 5 6 1 9 4 10 7 8 2 | import sys
input = sys.stdin.readline
(N,) = map(int, input().split())
X = [0] + list(map(int, input().split()))
Y = [0] + list(map(int, input().split()))
st = []
G = [set() for _ in range(N + 1)]
for i in range(1, N + 1):
if Y[i] < 0:
st.append(i)
else:
G[Y[i]].add(i)
ato = []
R = []
W = [0] * (N + 1)
for s in st:
stack = [s]
vs_dfs = list()
while stack:
v = stack.pop()
vs_dfs.append(v)
for u in G[v]:
stack.append(u)
for v in vs_dfs[::-1]:
for u in G[v]:
X[v] += max(0, X[u])
if X[v] < 0:
ato.append(v)
else:
R.append(v)
ato = ato[::-1]
print(sum(X))
print(*(R + ato)) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR NUMBER FOR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...
There are two arrays $a$ and $b$ of length $n$. Initially, an $ans$ is equal to $0$ and the following operation is defined: Choose position $i$ ($1 \le i \le n$); Add $a_i$ to $ans$; If $b_i \neq -1$ then add $a_i$ to $a_{b_i}$.
What is the maximum $ans$ you can get by performing the operation on each $i$ ($1 \le i \le n$) exactly once?
Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.
-----Input-----
The first line contains the integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β the length of arrays $a$ and $b$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($β10^6 \le a_i \le 10^6$).
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$ or $b_i = -1$).
Additional constraint: it's guaranteed that for any $i$ ($1 \le i \le n$) the sequence $b_i, b_{b_i}, b_{b_{b_i}}, \ldots$ is not cyclic, in other words it will always end with $-1$.
-----Output-----
In the first line, print the maximum $ans$ you can get.
In the second line, print the order of operations: $n$ different integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). The $p_i$ is the position which should be chosen at the $i$-th step. If there are multiple orders, print any of them.
-----Examples-----
Input
3
1 2 3
2 3 -1
Output
10
1 2 3
Input
2
-1 100
2 -1
Output
99
2 1
Input
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9
Output
-9
3 5 6 1 9 4 10 7 8 2 | n = int(input())
val = [0] + [int(x) for x in input().split()]
par = [-1] + [int(x) for x in input().split()]
chil = [[] for _ in range(n + 1)]
vis = [False] * (n + 1)
for i, x in enumerate(par):
if x == -1:
continue
chil[x].append(i)
first, second = [], []
ans = 0
for i in range(1, n + 1):
if vis[i]:
continue
stack = [i]
post = []
while stack:
node = stack.pop()
vis[node] = True
post.append(node)
for other in chil[node]:
if not vis[other]:
stack.append(other)
for node in reversed(post):
ans += val[node]
if val[node] > 0 and par[node] != -1:
val[par[node]] += val[node]
if val[node] > 0:
first.append(node)
else:
second.append(node)
print(ans)
print(" ".join(map(str, first + second[::-1]))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER |
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...
There are two arrays $a$ and $b$ of length $n$. Initially, an $ans$ is equal to $0$ and the following operation is defined: Choose position $i$ ($1 \le i \le n$); Add $a_i$ to $ans$; If $b_i \neq -1$ then add $a_i$ to $a_{b_i}$.
What is the maximum $ans$ you can get by performing the operation on each $i$ ($1 \le i \le n$) exactly once?
Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.
-----Input-----
The first line contains the integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β the length of arrays $a$ and $b$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($β10^6 \le a_i \le 10^6$).
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$ or $b_i = -1$).
Additional constraint: it's guaranteed that for any $i$ ($1 \le i \le n$) the sequence $b_i, b_{b_i}, b_{b_{b_i}}, \ldots$ is not cyclic, in other words it will always end with $-1$.
-----Output-----
In the first line, print the maximum $ans$ you can get.
In the second line, print the order of operations: $n$ different integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). The $p_i$ is the position which should be chosen at the $i$-th step. If there are multiple orders, print any of them.
-----Examples-----
Input
3
1 2 3
2 3 -1
Output
10
1 2 3
Input
2
-1 100
2 -1
Output
99
2 1
Input
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9
Output
-9
3 5 6 1 9 4 10 7 8 2 | import sys
input = sys.stdin.readline
n = int(input())
ar = list(map(int, input().split()))
br = list(map(int, input().split()))
dic = {}
revtree = {}
for i in range(1, n + 1):
revtree[i] = []
dic[i] = [ar[i - 1], br[i - 1]]
root = set({})
for i in range(1, n + 1):
ind = dic[i][1]
if ind != -1:
revtree[ind].append(i)
else:
root.add(i)
tree = []
dis = []
for i in root:
tree.append([i])
dis.append(dict({}))
dis[-1][i] = 0
level = 1
ne = {i}
while ne:
tem = set({})
for j in ne:
xx = revtree[j]
for k in xx:
tem.add(k)
tree[-1].append(k)
dis[-1][k] = level
ne = tem.copy()
level += 1
for i in range(len(root)):
tree[i].sort(key=lambda x: dis[i][x], reverse=True)
ans = 0
li = []
last = []
for i in tree:
for j in i:
ind = dic[j][1]
val = dic[j][0]
if val < 0:
ans += val
last.append(j)
else:
ans += val
if ind != -1:
dic[ind][0] += val
li.append(j)
print(ans)
print(*(li + last[::-1])) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST ASSIGN VAR VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR EXPR FUNC_CALL VAR FUNC_CALL VAR DICT ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER |
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...
There are two arrays $a$ and $b$ of length $n$. Initially, an $ans$ is equal to $0$ and the following operation is defined: Choose position $i$ ($1 \le i \le n$); Add $a_i$ to $ans$; If $b_i \neq -1$ then add $a_i$ to $a_{b_i}$.
What is the maximum $ans$ you can get by performing the operation on each $i$ ($1 \le i \le n$) exactly once?
Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.
-----Input-----
The first line contains the integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β the length of arrays $a$ and $b$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($β10^6 \le a_i \le 10^6$).
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$ or $b_i = -1$).
Additional constraint: it's guaranteed that for any $i$ ($1 \le i \le n$) the sequence $b_i, b_{b_i}, b_{b_{b_i}}, \ldots$ is not cyclic, in other words it will always end with $-1$.
-----Output-----
In the first line, print the maximum $ans$ you can get.
In the second line, print the order of operations: $n$ different integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). The $p_i$ is the position which should be chosen at the $i$-th step. If there are multiple orders, print any of them.
-----Examples-----
Input
3
1 2 3
2 3 -1
Output
10
1 2 3
Input
2
-1 100
2 -1
Output
99
2 1
Input
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9
Output
-9
3 5 6 1 9 4 10 7 8 2 | n = int(input())
val = list(map(int, input().split()))
b = list(map(int, input().split()))
neig = [0] * n
for i in range(n):
neig[i] = [0]
inedges = [0] * n
for i in range(n):
if b[i] != -1:
neig[i][0] += 1
neig[i].append(b[i] - 1)
inedges[b[i] - 1] += 1
ans = 0
beg = []
en = []
tod = []
for i in range(n):
if inedges[i] == 0:
tod.append(i)
while len(tod) > 0:
x = tod.pop()
ans += val[x]
if val[x] > 0:
beg.append(x + 1)
else:
en.append(x + 1)
if neig[x][0] == 1:
inedges[neig[x][1]] -= 1
if inedges[neig[x][1]] == 0:
tod.append(neig[x][1])
if val[x] > 0:
val[neig[x][1]] += val[x]
print(ans)
print(*beg, end=" ")
en.reverse()
print(*en) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...
There are two arrays $a$ and $b$ of length $n$. Initially, an $ans$ is equal to $0$ and the following operation is defined: Choose position $i$ ($1 \le i \le n$); Add $a_i$ to $ans$; If $b_i \neq -1$ then add $a_i$ to $a_{b_i}$.
What is the maximum $ans$ you can get by performing the operation on each $i$ ($1 \le i \le n$) exactly once?
Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.
-----Input-----
The first line contains the integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β the length of arrays $a$ and $b$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($β10^6 \le a_i \le 10^6$).
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$ or $b_i = -1$).
Additional constraint: it's guaranteed that for any $i$ ($1 \le i \le n$) the sequence $b_i, b_{b_i}, b_{b_{b_i}}, \ldots$ is not cyclic, in other words it will always end with $-1$.
-----Output-----
In the first line, print the maximum $ans$ you can get.
In the second line, print the order of operations: $n$ different integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). The $p_i$ is the position which should be chosen at the $i$-th step. If there are multiple orders, print any of them.
-----Examples-----
Input
3
1 2 3
2 3 -1
Output
10
1 2 3
Input
2
-1 100
2 -1
Output
99
2 1
Input
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9
Output
-9
3 5 6 1 9 4 10 7 8 2 | import sys
def rs():
return sys.stdin.readline().rstrip()
def ri():
return int(sys.stdin.readline())
def ria():
return list(map(int, sys.stdin.readline().split()))
def ws(s):
sys.stdout.write(s)
sys.stdout.write("\n")
def wi(n):
sys.stdout.write(str(n))
sys.stdout.write("\n")
def wia(a, sep=" "):
sys.stdout.write(sep.join([str(x) for x in a]))
sys.stdout.write("\n")
def solve(n, a, b):
for i in range(n):
if b[i] > 0:
b[i] -= 1
in_deg = [0] * n
for i in range(n):
if b[i] != -1:
in_deg[b[i]] += 1
q = [i for i in range(n) if in_deg[i] == 0]
ans = 0
res = []
end = []
while q:
v = q.pop()
if a[v] < 0:
end.append(v)
else:
res.append(v)
ans += a[v]
if b[v] != -1:
a[b[v]] += a[v]
if b[v] != -1:
in_deg[b[v]] -= 1
if in_deg[b[v]] == 0:
q.append(b[v])
for v in reversed(end):
res.append(v)
ans += a[v]
if b[v] != -1:
a[b[v]] += a[v]
wi(ans)
wia([(i + 1) for i in res])
def main():
n = ri()
a = ria()
b = ria()
solve(n, a, b)
main() | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL 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 EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING FUNC_DEF STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR |
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...
There are two arrays $a$ and $b$ of length $n$. Initially, an $ans$ is equal to $0$ and the following operation is defined: Choose position $i$ ($1 \le i \le n$); Add $a_i$ to $ans$; If $b_i \neq -1$ then add $a_i$ to $a_{b_i}$.
What is the maximum $ans$ you can get by performing the operation on each $i$ ($1 \le i \le n$) exactly once?
Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.
-----Input-----
The first line contains the integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β the length of arrays $a$ and $b$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($β10^6 \le a_i \le 10^6$).
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$ or $b_i = -1$).
Additional constraint: it's guaranteed that for any $i$ ($1 \le i \le n$) the sequence $b_i, b_{b_i}, b_{b_{b_i}}, \ldots$ is not cyclic, in other words it will always end with $-1$.
-----Output-----
In the first line, print the maximum $ans$ you can get.
In the second line, print the order of operations: $n$ different integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). The $p_i$ is the position which should be chosen at the $i$-th step. If there are multiple orders, print any of them.
-----Examples-----
Input
3
1 2 3
2 3 -1
Output
10
1 2 3
Input
2
-1 100
2 -1
Output
99
2 1
Input
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9
Output
-9
3 5 6 1 9 4 10 7 8 2 | 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 LI1():
return list(map(int1, 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()
bb = LI1()
to = [[] for _ in range(n)]
ot = [[] for _ in range(n)]
for u, b in enumerate(bb):
if b == -2:
continue
to[u].append(b)
ot[b].append(u)
def dfs(u):
stack = [u]
while stack:
u = stack.pop()
if fin[u]:
topo.append(u)
else:
fin[u] = True
stack.append(u)
for v in to[u]:
if fin[v]:
continue
stack.append(v)
topo = []
fin = [False] * n
for u in range(n):
if fin[u]:
continue
dfs(u)
ans = 0
pp = []
fin = [-1] * n
for u in topo[::-1]:
if aa[u] >= 0:
ans += aa[u]
if bb[u] != -2:
aa[bb[u]] += aa[u]
pp.append(u + 1)
fin[u] = 1
for u in topo:
if fin[u] == 1:
continue
ans += aa[u]
pp.append(u + 1)
print(ans)
print(*pp) | 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 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 FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...
There are two arrays $a$ and $b$ of length $n$. Initially, an $ans$ is equal to $0$ and the following operation is defined: Choose position $i$ ($1 \le i \le n$); Add $a_i$ to $ans$; If $b_i \neq -1$ then add $a_i$ to $a_{b_i}$.
What is the maximum $ans$ you can get by performing the operation on each $i$ ($1 \le i \le n$) exactly once?
Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.
-----Input-----
The first line contains the integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β the length of arrays $a$ and $b$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($β10^6 \le a_i \le 10^6$).
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$ or $b_i = -1$).
Additional constraint: it's guaranteed that for any $i$ ($1 \le i \le n$) the sequence $b_i, b_{b_i}, b_{b_{b_i}}, \ldots$ is not cyclic, in other words it will always end with $-1$.
-----Output-----
In the first line, print the maximum $ans$ you can get.
In the second line, print the order of operations: $n$ different integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). The $p_i$ is the position which should be chosen at the $i$-th step. If there are multiple orders, print any of them.
-----Examples-----
Input
3
1 2 3
2 3 -1
Output
10
1 2 3
Input
2
-1 100
2 -1
Output
99
2 1
Input
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9
Output
-9
3 5 6 1 9 4 10 7 8 2 | import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int, input().split()))
B = map(int, input().split())
IncomList = [[] for _ in range(n)]
weight = [None] * n
endpoints = list()
for i, d in enumerate(B):
if d == -1:
endpoints.append(i)
else:
IncomList[d - 1].append(i)
serial, last = list(), list()
ans = 0
while endpoints:
e = endpoints[-1]
flag = True
for h in IncomList[e]:
if weight[h] is None:
endpoints.append(h)
flag = False
if flag:
endpoints.pop()
weight[e] = A[e] + sum(map(lambda x: max(0, weight[x]), IncomList[e]))
ans += weight[e]
if weight[e] >= 0:
serial.append(e + 1)
else:
last.append(e + 1)
serial.extend(reversed(last))
print(ans)
print(" ".join(map(str, serial))) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...
There are two arrays $a$ and $b$ of length $n$. Initially, an $ans$ is equal to $0$ and the following operation is defined: Choose position $i$ ($1 \le i \le n$); Add $a_i$ to $ans$; If $b_i \neq -1$ then add $a_i$ to $a_{b_i}$.
What is the maximum $ans$ you can get by performing the operation on each $i$ ($1 \le i \le n$) exactly once?
Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.
-----Input-----
The first line contains the integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β the length of arrays $a$ and $b$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($β10^6 \le a_i \le 10^6$).
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$ or $b_i = -1$).
Additional constraint: it's guaranteed that for any $i$ ($1 \le i \le n$) the sequence $b_i, b_{b_i}, b_{b_{b_i}}, \ldots$ is not cyclic, in other words it will always end with $-1$.
-----Output-----
In the first line, print the maximum $ans$ you can get.
In the second line, print the order of operations: $n$ different integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). The $p_i$ is the position which should be chosen at the $i$-th step. If there are multiple orders, print any of them.
-----Examples-----
Input
3
1 2 3
2 3 -1
Output
10
1 2 3
Input
2
-1 100
2 -1
Output
99
2 1
Input
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9
Output
-9
3 5 6 1 9 4 10 7 8 2 | import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
B = [(b - 1) for b in B]
D = [0] * n
for b in B:
if b == -2:
continue
D[b] += 1
E = [[] for i in range(n)]
for i in range(n):
if B[i] != -2:
E[B[i]].append(i)
Q = [i for i in range(n) if B[i] == -2]
TOP_SORT = []
while Q:
x = Q.pop()
TOP_SORT.append(x)
for to in E[x]:
Q.append(to)
C = [0] * n
ANS = 0
ALIST = []
ELIST = []
USE = [0] * n
for x in TOP_SORT[::-1]:
ANS += C[x] + A[x]
if B[x] == -2:
ELIST.append(x)
continue
if C[x] + A[x] > 0:
C[B[x]] += C[x] + A[x]
ALIST.append(x)
else:
ELIST.append(x)
print(ANS)
print(*[(i + 1) for i in ALIST + ELIST[::-1]]) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER |
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...
There are two arrays $a$ and $b$ of length $n$. Initially, an $ans$ is equal to $0$ and the following operation is defined: Choose position $i$ ($1 \le i \le n$); Add $a_i$ to $ans$; If $b_i \neq -1$ then add $a_i$ to $a_{b_i}$.
What is the maximum $ans$ you can get by performing the operation on each $i$ ($1 \le i \le n$) exactly once?
Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.
-----Input-----
The first line contains the integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β the length of arrays $a$ and $b$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($β10^6 \le a_i \le 10^6$).
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$ or $b_i = -1$).
Additional constraint: it's guaranteed that for any $i$ ($1 \le i \le n$) the sequence $b_i, b_{b_i}, b_{b_{b_i}}, \ldots$ is not cyclic, in other words it will always end with $-1$.
-----Output-----
In the first line, print the maximum $ans$ you can get.
In the second line, print the order of operations: $n$ different integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). The $p_i$ is the position which should be chosen at the $i$-th step. If there are multiple orders, print any of them.
-----Examples-----
Input
3
1 2 3
2 3 -1
Output
10
1 2 3
Input
2
-1 100
2 -1
Output
99
2 1
Input
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9
Output
-9
3 5 6 1 9 4 10 7 8 2 | import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
edges = [[] for i in range(n)]
parent = [(-1) for i in range(n)]
for i in range(len(b)):
if b[i] != -1:
edges[b[i] - 1].append(i)
parent[i] = b[i] - 1
done = [(0) for i in range(n)]
first = []
last = []
ans = 0
for i in range(n):
if done[i] == 0:
dfs = [i]
while len(dfs) > 0:
curr = dfs[-1]
cont = 1
for i in edges[curr]:
if done[i] == 0:
cont = 0
dfs.append(i)
break
if cont == 1:
if a[curr] > 0:
first.append(curr + 1)
if parent[curr] != -1:
a[parent[curr]] += a[curr]
else:
last.append(curr + 1)
ans += a[curr]
done[curr] = 1
dfs.pop(-1)
print(ans)
print(" ".join(map(str, first)) + " " + " ".join(map(str, last[::-1]))) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER |
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...
There are two arrays $a$ and $b$ of length $n$. Initially, an $ans$ is equal to $0$ and the following operation is defined: Choose position $i$ ($1 \le i \le n$); Add $a_i$ to $ans$; If $b_i \neq -1$ then add $a_i$ to $a_{b_i}$.
What is the maximum $ans$ you can get by performing the operation on each $i$ ($1 \le i \le n$) exactly once?
Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.
-----Input-----
The first line contains the integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β the length of arrays $a$ and $b$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($β10^6 \le a_i \le 10^6$).
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$ or $b_i = -1$).
Additional constraint: it's guaranteed that for any $i$ ($1 \le i \le n$) the sequence $b_i, b_{b_i}, b_{b_{b_i}}, \ldots$ is not cyclic, in other words it will always end with $-1$.
-----Output-----
In the first line, print the maximum $ans$ you can get.
In the second line, print the order of operations: $n$ different integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). The $p_i$ is the position which should be chosen at the $i$-th step. If there are multiple orders, print any of them.
-----Examples-----
Input
3
1 2 3
2 3 -1
Output
10
1 2 3
Input
2
-1 100
2 -1
Output
99
2 1
Input
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9
Output
-9
3 5 6 1 9 4 10 7 8 2 | import sys
input = sys.stdin.readline
def topological_sort(graph):
n = len(graph)
degree = [0] * n
for v in graph:
for nxt_v in v:
degree[nxt_v] += 1
ans = []
for i in range(n):
if degree[i] == 0:
ans.append(i)
stack = ans[:]
while stack:
pos = stack.pop()
for next_v in graph[pos]:
degree[next_v] -= 1
if degree[next_v] == 0:
stack.append(next_v)
ans.append(next_v)
return ans
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
graph = [[] for _ in range(n)]
inv_graph = [[] for _ in range(n)]
for i, nxt_v in enumerate(b):
if nxt_v == -1:
continue
nxt_v -= 1
graph[i].append(nxt_v)
inv_graph[nxt_v].append(i)
ans = 0
res = []
used = [False] * n
tp_sort = topological_sort(graph)
for v in tp_sort:
if used[v]:
continue
if a[v] > -1:
ans += a[v]
for nxt_v in graph[v]:
a[nxt_v] += a[v]
res.append(v + 1)
used[v] = True
tp_sort = topological_sort(inv_graph)
for v in tp_sort:
if used[v]:
continue
ans += a[v]
res.append(v + 1)
used[v] = True
print(ans)
print(*res) | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR IF VAR VAR NUMBER VAR VAR VAR FOR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...
There are two arrays $a$ and $b$ of length $n$. Initially, an $ans$ is equal to $0$ and the following operation is defined: Choose position $i$ ($1 \le i \le n$); Add $a_i$ to $ans$; If $b_i \neq -1$ then add $a_i$ to $a_{b_i}$.
What is the maximum $ans$ you can get by performing the operation on each $i$ ($1 \le i \le n$) exactly once?
Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.
-----Input-----
The first line contains the integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β the length of arrays $a$ and $b$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($β10^6 \le a_i \le 10^6$).
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$ or $b_i = -1$).
Additional constraint: it's guaranteed that for any $i$ ($1 \le i \le n$) the sequence $b_i, b_{b_i}, b_{b_{b_i}}, \ldots$ is not cyclic, in other words it will always end with $-1$.
-----Output-----
In the first line, print the maximum $ans$ you can get.
In the second line, print the order of operations: $n$ different integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). The $p_i$ is the position which should be chosen at the $i$-th step. If there are multiple orders, print any of them.
-----Examples-----
Input
3
1 2 3
2 3 -1
Output
10
1 2 3
Input
2
-1 100
2 -1
Output
99
2 1
Input
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9
Output
-9
3 5 6 1 9 4 10 7 8 2 | from sys import stdin, stdout
def captain_flint_and_treasure(n, a_a, b_a):
ind = [(0) for i in range(n + 1)]
r_s = set(i + 1 for i in range(n))
for i in range(n):
if b_a[i] != -1:
ind[b_a[i]] += 1
r_s.discard(b_a[i])
queue = list(r_s)
ans1 = 0
ans2 = []
ans3 = []
while queue:
cnt = len(queue)
for i in range(cnt):
j = queue.pop()
if b_a[j - 1] != -1:
if a_a[j - 1] > 0:
a_a[b_a[j - 1] - 1] += a_a[j - 1]
ind[b_a[j - 1]] -= 1
if ind[b_a[j - 1]] == 0:
queue.append(b_a[j - 1])
ans1 += a_a[j - 1]
if a_a[j - 1] > 0:
ans2.append(j)
else:
ans3.append(j)
ans3.reverse()
return [ans1, ans2 + ans3]
n = int(stdin.readline())
a_a = list(map(int, stdin.readline().split()))
b_a = list(map(int, stdin.readline().split()))
ans = captain_flint_and_treasure(n, a_a, b_a)
stdout.write(str(ans[0]) + "\n")
stdout.write(" ".join(map(str, ans[1])) + "\n") | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN LIST VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER STRING |
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...
There are two arrays $a$ and $b$ of length $n$. Initially, an $ans$ is equal to $0$ and the following operation is defined: Choose position $i$ ($1 \le i \le n$); Add $a_i$ to $ans$; If $b_i \neq -1$ then add $a_i$ to $a_{b_i}$.
What is the maximum $ans$ you can get by performing the operation on each $i$ ($1 \le i \le n$) exactly once?
Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.
-----Input-----
The first line contains the integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β the length of arrays $a$ and $b$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($β10^6 \le a_i \le 10^6$).
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$ or $b_i = -1$).
Additional constraint: it's guaranteed that for any $i$ ($1 \le i \le n$) the sequence $b_i, b_{b_i}, b_{b_{b_i}}, \ldots$ is not cyclic, in other words it will always end with $-1$.
-----Output-----
In the first line, print the maximum $ans$ you can get.
In the second line, print the order of operations: $n$ different integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). The $p_i$ is the position which should be chosen at the $i$-th step. If there are multiple orders, print any of them.
-----Examples-----
Input
3
1 2 3
2 3 -1
Output
10
1 2 3
Input
2
-1 100
2 -1
Output
99
2 1
Input
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9
Output
-9
3 5 6 1 9 4 10 7 8 2 | n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
G = [[] for _ in range(n)]
for i in range(n):
if b[i] != -1:
b[i] -= 1
G[b[i]].append(i)
best = a[:]
visited = [False] * n
topo = []
for x in range(n):
if visited[x]:
continue
stk = [x]
while stk:
x = stk[-1]
if not visited[x]:
visited[x] = True
for y in G[x]:
if not visited[y]:
stk.append(y)
else:
stk.pop()
for y in G[x]:
best[x] += max(0, best[y])
topo.append(x)
ans = 0
order = []
bad = []
for x in topo:
if best[x] >= 0:
ans += best[x]
order.append(x)
else:
bad.append(x)
X = order + bad[::-1]
X = [(x + 1) for x in X]
while bad:
ans += best[bad.pop()]
print(ans)
print(*X) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR WHILE VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...
There are two arrays $a$ and $b$ of length $n$. Initially, an $ans$ is equal to $0$ and the following operation is defined: Choose position $i$ ($1 \le i \le n$); Add $a_i$ to $ans$; If $b_i \neq -1$ then add $a_i$ to $a_{b_i}$.
What is the maximum $ans$ you can get by performing the operation on each $i$ ($1 \le i \le n$) exactly once?
Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.
-----Input-----
The first line contains the integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β the length of arrays $a$ and $b$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($β10^6 \le a_i \le 10^6$).
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$ or $b_i = -1$).
Additional constraint: it's guaranteed that for any $i$ ($1 \le i \le n$) the sequence $b_i, b_{b_i}, b_{b_{b_i}}, \ldots$ is not cyclic, in other words it will always end with $-1$.
-----Output-----
In the first line, print the maximum $ans$ you can get.
In the second line, print the order of operations: $n$ different integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). The $p_i$ is the position which should be chosen at the $i$-th step. If there are multiple orders, print any of them.
-----Examples-----
Input
3
1 2 3
2 3 -1
Output
10
1 2 3
Input
2
-1 100
2 -1
Output
99
2 1
Input
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9
Output
-9
3 5 6 1 9 4 10 7 8 2 | def put():
return map(int, input().split())
def dfs():
s = []
sum = 0
newgraph = [[] for i in range(n)]
newinedge = [0] * n
for i in range(n):
if inedge[i] == 0:
s.append(i)
while s:
i = s.pop()
sum += a[i]
for j in graph[i]:
if a[i] > 0:
a[j] += a[i]
newgraph[i].append(j)
newinedge[j] += 1
else:
newgraph[j].append(i)
newinedge[i] += 1
inedge[j] -= 1
if inedge[j] == 0:
s.append(j)
print(sum)
for i in range(n):
if newinedge[i] == 0:
s.append(i)
ans = []
while s:
i = s.pop()
ans.append(i + 1)
for j in newgraph[i]:
newinedge[j] -= 1
if newinedge[j] == 0:
s.append(j)
print(*ans)
n = int(input())
a = list(put())
b = list(put())
visited = [0] * n
graph = [[] for i in range(n)]
inedge = [0] * (n + 1)
for i in range(n):
if b[i] != -1:
inedge[b[i] - 1] += 1
graph[i].append(b[i] - 1)
dfs() | FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR |
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...
There are two arrays $a$ and $b$ of length $n$. Initially, an $ans$ is equal to $0$ and the following operation is defined: Choose position $i$ ($1 \le i \le n$); Add $a_i$ to $ans$; If $b_i \neq -1$ then add $a_i$ to $a_{b_i}$.
What is the maximum $ans$ you can get by performing the operation on each $i$ ($1 \le i \le n$) exactly once?
Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.
-----Input-----
The first line contains the integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β the length of arrays $a$ and $b$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($β10^6 \le a_i \le 10^6$).
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$ or $b_i = -1$).
Additional constraint: it's guaranteed that for any $i$ ($1 \le i \le n$) the sequence $b_i, b_{b_i}, b_{b_{b_i}}, \ldots$ is not cyclic, in other words it will always end with $-1$.
-----Output-----
In the first line, print the maximum $ans$ you can get.
In the second line, print the order of operations: $n$ different integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). The $p_i$ is the position which should be chosen at the $i$-th step. If there are multiple orders, print any of them.
-----Examples-----
Input
3
1 2 3
2 3 -1
Output
10
1 2 3
Input
2
-1 100
2 -1
Output
99
2 1
Input
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9
Output
-9
3 5 6 1 9 4 10 7 8 2 | from sys import *
def main():
n = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]
b = [int(x) for x in stdin.readline().rstrip().split()]
countLink = dict()
for i in b:
countLink[i - 1] = countLink.get(i - 1, 0) + 1
preselect = []
for i in range(n):
if countLink.get(i, 0) == 0:
preselect.append(i)
selected = []
stack = []
result = 0
while len(preselect) > 0:
i = preselect.pop()
if countLink.get(i, 0) == 0:
result += a[i]
if a[i] > 0:
selected.append(i + 1)
if b[i] != -1:
a[b[i] - 1] += a[i]
else:
stack.append(i + 1)
countLink[b[i] - 1] = countLink.get(b[i] - 1, 0) - 1
countLink[i] = -1
if countLink[b[i] - 1] == 0 and b[i] - 1 >= 0:
preselect.append(b[i] - 1)
print(result)
for i in selected:
print(i, end=" ")
for i in reversed(stack):
print(i, end=" ")
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...
There are two arrays $a$ and $b$ of length $n$. Initially, an $ans$ is equal to $0$ and the following operation is defined: Choose position $i$ ($1 \le i \le n$); Add $a_i$ to $ans$; If $b_i \neq -1$ then add $a_i$ to $a_{b_i}$.
What is the maximum $ans$ you can get by performing the operation on each $i$ ($1 \le i \le n$) exactly once?
Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.
-----Input-----
The first line contains the integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β the length of arrays $a$ and $b$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($β10^6 \le a_i \le 10^6$).
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$ or $b_i = -1$).
Additional constraint: it's guaranteed that for any $i$ ($1 \le i \le n$) the sequence $b_i, b_{b_i}, b_{b_{b_i}}, \ldots$ is not cyclic, in other words it will always end with $-1$.
-----Output-----
In the first line, print the maximum $ans$ you can get.
In the second line, print the order of operations: $n$ different integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). The $p_i$ is the position which should be chosen at the $i$-th step. If there are multiple orders, print any of them.
-----Examples-----
Input
3
1 2 3
2 3 -1
Output
10
1 2 3
Input
2
-1 100
2 -1
Output
99
2 1
Input
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9
Output
-9
3 5 6 1 9 4 10 7 8 2 | ans = [[], []]
ans_num = 0
def dfs(v):
queue = [v]
while queue:
v = queue[-1]
if not vis[v]:
vis[v] = True
for u in G_up[v]:
if not vis[u]:
queue.append(u)
else:
queue.pop()
global ans_num
ans_num += a[v]
ans[a[v] < 0].append(v)
if b[v] != -2 and a[v] > 0:
a[b[v]] += a[v]
def up_down(G, n):
G_new = {i: [] for i in range(n)}
for i in range(n):
if G[i] >= 0:
G_new[G[i]].append(i)
return G_new
n = int(input())
a = list(map(int, input().split()))
b = list(map(lambda el: int(el) - 1, input().split()))
G_up = up_down(b, n)
vis = [(False) for i in range(n)]
for i in range(n):
if not vis[i]:
dfs(i)
print(ans_num)
print(*[(el + 1) for el in ans[0]], *reversed([(el + 1) for el in ans[1]])) | ASSIGN VAR LIST LIST LIST ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER |
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...
There are two arrays $a$ and $b$ of length $n$. Initially, an $ans$ is equal to $0$ and the following operation is defined: Choose position $i$ ($1 \le i \le n$); Add $a_i$ to $ans$; If $b_i \neq -1$ then add $a_i$ to $a_{b_i}$.
What is the maximum $ans$ you can get by performing the operation on each $i$ ($1 \le i \le n$) exactly once?
Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.
-----Input-----
The first line contains the integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β the length of arrays $a$ and $b$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($β10^6 \le a_i \le 10^6$).
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$ or $b_i = -1$).
Additional constraint: it's guaranteed that for any $i$ ($1 \le i \le n$) the sequence $b_i, b_{b_i}, b_{b_{b_i}}, \ldots$ is not cyclic, in other words it will always end with $-1$.
-----Output-----
In the first line, print the maximum $ans$ you can get.
In the second line, print the order of operations: $n$ different integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). The $p_i$ is the position which should be chosen at the $i$-th step. If there are multiple orders, print any of them.
-----Examples-----
Input
3
1 2 3
2 3 -1
Output
10
1 2 3
Input
2
-1 100
2 -1
Output
99
2 1
Input
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9
Output
-9
3 5 6 1 9 4 10 7 8 2 | n = int(input())
a = list(map(int, input().split()))
b = list(int(x) - 1 for x in input().split())
indeg = [0] * n
for x in b:
if x >= 0:
indeg[x] += 1
q = [i for i in range(n) if indeg[i] == 0]
ans = 0
res = []
end = []
while q:
top = q.pop()
if a[top] < 0:
end.append(top)
else:
res.append(top)
ans += a[top]
if b[top] >= 0:
a[b[top]] += a[top]
if b[top] >= 0:
nei = b[top]
indeg[nei] -= 1
if indeg[nei] == 0:
q.append(nei)
for i in end[::-1]:
ans += a[i]
if b[i] >= 0:
a[b[i]] += a[i]
res.append(i)
print(ans)
print(*[(x + 1) for x in res]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR |
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...
There are two arrays $a$ and $b$ of length $n$. Initially, an $ans$ is equal to $0$ and the following operation is defined: Choose position $i$ ($1 \le i \le n$); Add $a_i$ to $ans$; If $b_i \neq -1$ then add $a_i$ to $a_{b_i}$.
What is the maximum $ans$ you can get by performing the operation on each $i$ ($1 \le i \le n$) exactly once?
Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.
-----Input-----
The first line contains the integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β the length of arrays $a$ and $b$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($β10^6 \le a_i \le 10^6$).
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$ or $b_i = -1$).
Additional constraint: it's guaranteed that for any $i$ ($1 \le i \le n$) the sequence $b_i, b_{b_i}, b_{b_{b_i}}, \ldots$ is not cyclic, in other words it will always end with $-1$.
-----Output-----
In the first line, print the maximum $ans$ you can get.
In the second line, print the order of operations: $n$ different integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). The $p_i$ is the position which should be chosen at the $i$-th step. If there are multiple orders, print any of them.
-----Examples-----
Input
3
1 2 3
2 3 -1
Output
10
1 2 3
Input
2
-1 100
2 -1
Output
99
2 1
Input
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9
Output
-9
3 5 6 1 9 4 10 7 8 2 | import sys
sys.setrecursionlimit(2 * 10**5)
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
def construct_graph(A, B):
graph = {}
for i, j in enumerate(B):
if a[i] is not None:
graph[i + 1] = j
return graph
def dfs(G, x, FINISHED_ORDER, VISITED):
if x in VISITED:
return
if x in G and G[x] != -1:
dfs(G, G[x], FINISHED_ORDER, VISITED)
FINISHED_ORDER.append(x)
VISITED[x] = True
def get_topological_sort(G):
visited = {}
finished_order = []
for x in G:
dfs(G, x, finished_order, visited)
finished_order.reverse()
return finished_order
def get_iterative_topological_sort(G):
finished_order = []
already_removed = {}
for x in G:
if x in already_removed:
continue
stack = [x]
while len(stack) != 0:
x = stack.pop()
if x in already_removed:
finished_order.append(x)
continue
stack.append(x)
if x in G and G[x] != -1 and G[x] not in already_removed:
stack.append(G[x])
already_removed[x] = True
finished_order.reverse()
return finished_order
g = construct_graph(a, b)
take_first = []
take_last = []
ans = 0
TOP = get_iterative_topological_sort(g)
TOP.reverse()
while len(TOP) != 0:
node_to_select = TOP.pop()
if a[node_to_select - 1] is None:
continue
if a[node_to_select - 1] >= 0:
take_first.append(node_to_select)
ans += a[node_to_select - 1]
if b[node_to_select - 1] != -1:
a[b[node_to_select - 1] - 1] += a[node_to_select - 1]
else:
take_last.append(node_to_select)
ans += a[node_to_select - 1]
a[node_to_select - 1] = None
print(ans)
sol = take_first + take_last[::-1]
sol = list(map(str, sol))
print(" ".join(sol)) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NONE ASSIGN VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR LIST VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER NONE IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...
There are two arrays $a$ and $b$ of length $n$. Initially, an $ans$ is equal to $0$ and the following operation is defined: Choose position $i$ ($1 \le i \le n$); Add $a_i$ to $ans$; If $b_i \neq -1$ then add $a_i$ to $a_{b_i}$.
What is the maximum $ans$ you can get by performing the operation on each $i$ ($1 \le i \le n$) exactly once?
Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.
-----Input-----
The first line contains the integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β the length of arrays $a$ and $b$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($β10^6 \le a_i \le 10^6$).
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$ or $b_i = -1$).
Additional constraint: it's guaranteed that for any $i$ ($1 \le i \le n$) the sequence $b_i, b_{b_i}, b_{b_{b_i}}, \ldots$ is not cyclic, in other words it will always end with $-1$.
-----Output-----
In the first line, print the maximum $ans$ you can get.
In the second line, print the order of operations: $n$ different integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). The $p_i$ is the position which should be chosen at the $i$-th step. If there are multiple orders, print any of them.
-----Examples-----
Input
3
1 2 3
2 3 -1
Output
10
1 2 3
Input
2
-1 100
2 -1
Output
99
2 1
Input
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9
Output
-9
3 5 6 1 9 4 10 7 8 2 | import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
for i in range(n):
if B[i] != -1:
B[i] -= 1
ans = 0
P = [0] * n
V = [0] * n
M = [[] for i in range(n)]
for i in range(n):
if B[i] != -1:
M[i].append(B[i])
P[B[i]] += 1
Q = []
for i in range(n):
if P[i] == 0:
Q.append(i)
V[i] = 1
s = 0
ANS = []
MINUS = []
while s < len(Q):
i = Q[s]
ans += A[i]
if A[i] > 0:
ANS.append(i)
else:
MINUS.append(i)
for x in M[i]:
if V[x] == 0:
if A[i] > 0:
A[x] += A[i]
P[x] -= 1
if P[x] == 0:
Q.append(x)
V[x] = 1
s += 1
print(ans)
print(
*([(i + 1) for i in ANS] + [(MINUS[i] + 1) for i in range(len(MINUS) - 1, -1, -1)])
) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER |
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?
Example 1:
Input: [1,0,2]
Output: 5
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input: [1,2,2]
Output: 4
Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it satisfies the above two conditions. | class Solution:
def candy(self, ratings):
candies = [0] * len(ratings)
from_left = [1] * len(ratings)
cur_less_than = 1
for idx in range(1, len(ratings)):
if ratings[idx - 1] < ratings[idx]:
cur_less_than += 1
else:
cur_less_than = 1
from_left[idx] = cur_less_than
from_right = [1] * len(ratings)
cur_less_than = 1
idx = len(ratings) - 2
while idx >= 0:
if ratings[idx + 1] < ratings[idx]:
cur_less_than += 1
else:
cur_less_than = 1
from_right[idx] = cur_less_than
idx -= 1
for idx in range(0, len(ratings)):
candies[idx] = max(from_left[idx], from_right[idx])
return sum(candies) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR |
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?
Example 1:
Input: [1,0,2]
Output: 5
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input: [1,2,2]
Output: 4
Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it satisfies the above two conditions. | class Solution:
def candy(self, ratings):
n = len(ratings)
result = [1] * n
for i in range(n - 1):
if ratings[i + 1] > ratings[i]:
result[i + 1] = result[i] + 1
for i in range(n - 1, 0, -1):
if ratings[i - 1] > ratings[i]:
result[i - 1] = max(result[i] + 1, result[i - 1])
return sum(result) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR |
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?
Example 1:
Input: [1,0,2]
Output: 5
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input: [1,2,2]
Output: 4
Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it satisfies the above two conditions. | class Solution:
def candy(self, ratings):
if not ratings:
return 0
if len(ratings) == 1:
return 1
l2r = [(1) for _ in ratings]
for i in range(1, len(ratings)):
if ratings[i] > ratings[i - 1]:
l2r[i] = l2r[i - 1] + 1
res = l2r[-1]
for i in range(len(ratings) - 2, -1, -1):
if ratings[i] > ratings[i + 1]:
l2r[i] = max(l2r[i], l2r[i + 1] + 1)
res += l2r[i]
return res | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR RETURN VAR |
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?
Example 1:
Input: [1,0,2]
Output: 5
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input: [1,2,2]
Output: 4
Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it satisfies the above two conditions. | class Solution:
def candy(self, ratings):
current_min = 1
current_max = 1
desc_len = 1
cnt = 1
for i in range(1, len(ratings)):
if ratings[i] < ratings[i - 1]:
if current_min == 1:
if desc_len >= current_max:
cnt += desc_len + 1
else:
cnt += desc_len
else:
current_max = current_min
current_min = 1
cnt += 1
desc_len += 1
elif ratings[i] > ratings[i - 1]:
current_min += 1
current_max = current_min
cnt += current_min
desc_len = 1
else:
cnt += 1
current_min = 1
current_max = 1
desc_len = 1
return cnt | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?
Example 1:
Input: [1,0,2]
Output: 5
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input: [1,2,2]
Output: 4
Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it satisfies the above two conditions. | class Solution:
def candy(self, ratings):
candy = [(1) for i in range(len(ratings))]
for i in range(1, len(ratings)):
if ratings[i] > ratings[i - 1]:
candy[i] = candy[i - 1] + 1
for i in range(len(ratings) - 1)[::-1]:
if ratings[i] > ratings[i + 1]:
candy[i] = max(candy[i], candy[i + 1] + 1)
return sum(candy) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR |
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?
Example 1:
Input: [1,0,2]
Output: 5
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input: [1,2,2]
Output: 4
Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it satisfies the above two conditions. | class Solution:
def candy(self, ratings):
if not ratings:
return 0
total, pre, decrease = 1, 1, 0
for i in range(1, len(ratings)):
if ratings[i] >= ratings[i - 1]:
if decrease > 0:
total += (1 + decrease) * decrease // 2
if pre <= decrease:
total += decrease + 1 - pre
decrease, pre = 0, 1
if ratings[i] == ratings[i - 1]:
total += 1
pre = 1
else:
pre += 1
total += pre
else:
decrease += 1
if decrease > 0:
total += (1 + decrease) * decrease // 2
if pre <= decrease:
total += decrease + 1 - pre
return total | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR |
This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).
Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has $n$ planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format:
$+$ $x$: the storehouse received a plank with length $x$ $-$ $x$: one plank with length $x$ was removed from the storehouse (it is guaranteed that the storehouse had some planks with length $x$).
Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!
We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$): the initial amount of planks at the company's storehouse, the second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$): the lengths of the planks.
The third line contains a single integer $q$ ($1 \le q \le 10^5$): the number of events in the company. Each of the next $q$ lines contains a description of the events in a given format: the type of the event (a symbol $+$ or $-$) is given first, then goes the integer $x$ ($1 \le x \le 10^5$).
-----Output-----
After every event in the company, print "YES" if two storages of the required shape can be built from the planks of that company's set, and print "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
6
1 1 1 2 1 1
6
+ 2
+ 1
- 1
+ 2
- 1
+ 2
Output
NO
YES
NO
NO
NO
YES
-----Note-----
After the second event Applejack can build a rectangular storage using planks with lengths $1$, $2$, $1$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$.
After the sixth event Applejack can build a rectangular storage using planks with lengths $2$, $2$, $2$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$. | n = int(input())
lis = list(map(int, input().split()))
f = {}
ff = {}
for i in range(100000 + 5):
f[i] = 0
ff[i] = 0
for i in lis:
if i not in f:
f[i] = 1
if f[i] not in ff:
ff[f[i]] = 1
else:
ff[f[i]] += 1
else:
f[i] += 1
if f[i] not in ff:
ff[f[i]] = 1
else:
ff[f[i]] += 1
q = int(input())
flag = 0
for gamesense in range(q):
c, j = input().split()
j = int(j)
if c == "+":
f[j] += 1
ff[f[j]] += 1
else:
ff[f[j]] -= 1
f[j] -= 1
if ff[4] > 1 or ff[4] > 0 and ff[2] > 2 or ff[6] > 0 and ff[2] > 1 or ff[8] > 0:
print("YES")
flag = 1
continue
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING |
This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).
Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has $n$ planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format:
$+$ $x$: the storehouse received a plank with length $x$ $-$ $x$: one plank with length $x$ was removed from the storehouse (it is guaranteed that the storehouse had some planks with length $x$).
Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!
We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$): the initial amount of planks at the company's storehouse, the second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$): the lengths of the planks.
The third line contains a single integer $q$ ($1 \le q \le 10^5$): the number of events in the company. Each of the next $q$ lines contains a description of the events in a given format: the type of the event (a symbol $+$ or $-$) is given first, then goes the integer $x$ ($1 \le x \le 10^5$).
-----Output-----
After every event in the company, print "YES" if two storages of the required shape can be built from the planks of that company's set, and print "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
6
1 1 1 2 1 1
6
+ 2
+ 1
- 1
+ 2
- 1
+ 2
Output
NO
YES
NO
NO
NO
YES
-----Note-----
After the second event Applejack can build a rectangular storage using planks with lengths $1$, $2$, $1$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$.
After the sixth event Applejack can build a rectangular storage using planks with lengths $2$, $2$, $2$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$. | n = int(input())
a = list(map(int, input().split()))
a_dict = {}
for i in a:
a_dict[i] = a_dict.get(i, 0) + 1
res = {"sqr": 0, "rect": 0}
def is_good():
global a_dict, res
for v in a_dict.values():
v2 = v // 2
if v2 >= 2:
res["sqr"] += 1
v2 -= 2
if v2 >= 2:
res["sqr"] += 1
v2 -= 2
if v2 >= 1:
res["rect"] += 1
v2 -= 1
q = int(input())
ANS = []
is_good()
for i in range(q):
sign, num = input().split()
num = int(num)
if num not in a_dict:
a_dict[num] = 0
if sign == "+":
a_dict[int(num)] += 1
v = a_dict[int(num)]
if v % 2 == 0:
if v % 4 == 0:
res["sqr"] += 1
res["rect"] -= 1
else:
res["rect"] += 1
else:
a_dict[int(num)] -= 1
v = a_dict[int(num)]
if (v + 1) % 2 == 0:
if (v + 1) % 4 == 0:
res["sqr"] -= 1
res["rect"] += 1
else:
res["rect"] -= 1
if res["sqr"] >= 1 and res["rect"] >= 2 or res["sqr"] >= 2:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR DICT STRING STRING NUMBER NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR STRING NUMBER VAR NUMBER IF VAR NUMBER VAR STRING NUMBER VAR NUMBER IF VAR NUMBER VAR STRING NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR STRING VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR STRING NUMBER VAR STRING NUMBER VAR STRING NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR STRING NUMBER VAR STRING NUMBER VAR STRING NUMBER IF VAR STRING NUMBER VAR STRING NUMBER VAR STRING NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).
Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has $n$ planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format:
$+$ $x$: the storehouse received a plank with length $x$ $-$ $x$: one plank with length $x$ was removed from the storehouse (it is guaranteed that the storehouse had some planks with length $x$).
Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!
We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$): the initial amount of planks at the company's storehouse, the second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$): the lengths of the planks.
The third line contains a single integer $q$ ($1 \le q \le 10^5$): the number of events in the company. Each of the next $q$ lines contains a description of the events in a given format: the type of the event (a symbol $+$ or $-$) is given first, then goes the integer $x$ ($1 \le x \le 10^5$).
-----Output-----
After every event in the company, print "YES" if two storages of the required shape can be built from the planks of that company's set, and print "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
6
1 1 1 2 1 1
6
+ 2
+ 1
- 1
+ 2
- 1
+ 2
Output
NO
YES
NO
NO
NO
YES
-----Note-----
After the second event Applejack can build a rectangular storage using planks with lengths $1$, $2$, $1$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$.
After the sixth event Applejack can build a rectangular storage using planks with lengths $2$, $2$, $2$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$. | def check(fr, tw):
if fr > 1:
return "YES"
elif fr == 1:
if tw > 1:
return "YES"
else:
return "NO"
else:
return "NO"
n = int(input())
a = list(map(int, input().split()))
sq = [0]
rect = [0, 0]
fr = 0
tw = 0
d = {}
for x in a:
d[x] = d.get(x, [0, 0, 0])
d[x][0] += 1
d[x][1] = d[x][0] // 4
d[x][2] = d[x][0] % 4 // 2
for x in d:
fr += d[x][1]
tw += d[x][2]
for q in range(int(input())):
a = input().split()
sn = a[0]
x = int(a[1])
if sn == "+":
d[x] = d.get(x, [0, 0, 0])
fr -= d[x][1]
tw -= d[x][2]
d[x][0] += 1
else:
fr -= d[x][1]
tw -= d[x][2]
d[x][0] -= 1
d[x][1] = d[x][0] // 4
d[x][2] = d[x][0] % 4 // 2
fr += d[x][1]
tw += d[x][2]
print(check(fr, tw)) | FUNC_DEF IF VAR NUMBER RETURN STRING IF VAR NUMBER IF VAR NUMBER RETURN STRING RETURN STRING RETURN 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 LIST NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR LIST NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR LIST NUMBER NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).
Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has $n$ planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format:
$+$ $x$: the storehouse received a plank with length $x$ $-$ $x$: one plank with length $x$ was removed from the storehouse (it is guaranteed that the storehouse had some planks with length $x$).
Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!
We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$): the initial amount of planks at the company's storehouse, the second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$): the lengths of the planks.
The third line contains a single integer $q$ ($1 \le q \le 10^5$): the number of events in the company. Each of the next $q$ lines contains a description of the events in a given format: the type of the event (a symbol $+$ or $-$) is given first, then goes the integer $x$ ($1 \le x \le 10^5$).
-----Output-----
After every event in the company, print "YES" if two storages of the required shape can be built from the planks of that company's set, and print "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
6
1 1 1 2 1 1
6
+ 2
+ 1
- 1
+ 2
- 1
+ 2
Output
NO
YES
NO
NO
NO
YES
-----Note-----
After the second event Applejack can build a rectangular storage using planks with lengths $1$, $2$, $1$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$.
After the sixth event Applejack can build a rectangular storage using planks with lengths $2$, $2$, $2$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$. | def process(planks, events):
results = []
tows = []
fours = []
for elem in planks.keys():
if planks[elem] == 1:
pass
elif 1 < planks[elem] < 4:
tows.append(elem)
else:
fours.append(elem)
for event in events:
if event[0] == "+":
if event[1] not in planks.keys() or planks[event[1]] == 0:
planks[event[1]] = 1
elif planks[event[1]] == 1:
tows.append(event[1])
planks[event[1]] += 1
elif planks[event[1]] == 3:
fours.append(event[1])
tows.remove(event[1])
planks[event[1]] += 1
else:
planks[event[1]] += 1
elif planks[event[1]] == 1:
planks[event[1]] = 0
elif planks[event[1]] == 2:
planks[event[1]] = 1
tows.remove(event[1])
elif planks[event[1]] == 4:
fours.remove(event[1])
tows.append(event[1])
planks[event[1]] -= 1
else:
planks[event[1]] -= 1
if len(fours) >= 2:
results.append("YES")
elif len(fours) == 1 and planks[fours[0]] >= 6 and len(tows) >= 1:
results.append("YES")
elif len(fours) == 1 and len(tows) >= 2:
results.append("YES")
elif len(fours) == 1 and planks[fours[0]] >= 8:
results.append("YES")
else:
results.append("NO")
return results
n = int(input())
planks = {}
l = [int(x) for x in input().split()]
for num in range(n):
if l[num] in planks.keys():
planks[l[num]] += 1
else:
planks[l[num]] = 1
s = int(input())
events = []
for num in range(s):
q = input().split()
events.append([q[0], int(q[1])])
results = process(planks, events)
for result in results:
print(result) | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER IF NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER STRING IF VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).
Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has $n$ planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format:
$+$ $x$: the storehouse received a plank with length $x$ $-$ $x$: one plank with length $x$ was removed from the storehouse (it is guaranteed that the storehouse had some planks with length $x$).
Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!
We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$): the initial amount of planks at the company's storehouse, the second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$): the lengths of the planks.
The third line contains a single integer $q$ ($1 \le q \le 10^5$): the number of events in the company. Each of the next $q$ lines contains a description of the events in a given format: the type of the event (a symbol $+$ or $-$) is given first, then goes the integer $x$ ($1 \le x \le 10^5$).
-----Output-----
After every event in the company, print "YES" if two storages of the required shape can be built from the planks of that company's set, and print "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
6
1 1 1 2 1 1
6
+ 2
+ 1
- 1
+ 2
- 1
+ 2
Output
NO
YES
NO
NO
NO
YES
-----Note-----
After the second event Applejack can build a rectangular storage using planks with lengths $1$, $2$, $1$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$.
After the sixth event Applejack can build a rectangular storage using planks with lengths $2$, $2$, $2$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$. | def solve():
_ = int(input())
a = list(map(int, input().split()))
d = {}
for x in a:
if x in d:
d[x] += 1
else:
d[x] = 1
k = {(2): 0, (4): 0}
for key, val in d.items():
if val >= 4:
k[4] += 1
k[2] += (val - 4) // 2
else:
k[2] += val // 2
for _ in range(int(input())):
char, key = input().split()
key = int(key)
if char == "+":
if key not in d:
d[key] = 1
else:
val = d[key]
if val >= 4:
k[4] -= 1
k[2] -= (val - 4) // 2
else:
k[2] -= val // 2
d[key] += 1
val = d[key]
if val >= 4:
k[4] += 1
k[2] += (val - 4) // 2
else:
k[2] += val // 2
else:
val = d[key]
if val >= 4:
k[4] -= 1
k[2] -= (val - 4) // 2
else:
k[2] -= val // 2
d[key] -= 1
val = d[key]
if val >= 4:
k[4] += 1
k[2] += (val - 4) // 2
else:
k[2] += val // 2
if k[4] > 1 or k[4] == 1 and k[2] > 1:
print("YES")
else:
print("NO")
solve() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR |
This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).
Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has $n$ planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format:
$+$ $x$: the storehouse received a plank with length $x$ $-$ $x$: one plank with length $x$ was removed from the storehouse (it is guaranteed that the storehouse had some planks with length $x$).
Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!
We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$): the initial amount of planks at the company's storehouse, the second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$): the lengths of the planks.
The third line contains a single integer $q$ ($1 \le q \le 10^5$): the number of events in the company. Each of the next $q$ lines contains a description of the events in a given format: the type of the event (a symbol $+$ or $-$) is given first, then goes the integer $x$ ($1 \le x \le 10^5$).
-----Output-----
After every event in the company, print "YES" if two storages of the required shape can be built from the planks of that company's set, and print "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
6
1 1 1 2 1 1
6
+ 2
+ 1
- 1
+ 2
- 1
+ 2
Output
NO
YES
NO
NO
NO
YES
-----Note-----
After the second event Applejack can build a rectangular storage using planks with lengths $1$, $2$, $1$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$.
After the sixth event Applejack can build a rectangular storage using planks with lengths $2$, $2$, $2$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$. | n = int(input())
arr = [int(i) for i in input().split()]
md = dict([])
sq = 0
rect = 0
for i in arr:
if i in md:
md[i] += 1
if md[i] % 4 == 0:
sq += 1
rect += 1
elif md[i] % 2 == 0:
rect += 1
else:
md[i] = 1
q = int(input())
i = 0
for j in range(q):
qu = input().split()
if qu[0] == "+":
i = int(qu[1])
if i in md:
md[i] += 1
if md[i] % 4 == 0:
sq += 1
rect += 1
elif md[i] % 2 == 0:
rect += 1
else:
md[i] = 1
else:
i = int(qu[1])
if md[i] % 4 == 0:
sq -= 1
rect -= 1
elif md[i] % 2 == 0:
rect -= 1
md[i] -= 1
if sq >= 2:
print("YES")
elif sq == 1 and rect - 2 >= 2:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).
Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has $n$ planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format:
$+$ $x$: the storehouse received a plank with length $x$ $-$ $x$: one plank with length $x$ was removed from the storehouse (it is guaranteed that the storehouse had some planks with length $x$).
Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!
We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$): the initial amount of planks at the company's storehouse, the second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$): the lengths of the planks.
The third line contains a single integer $q$ ($1 \le q \le 10^5$): the number of events in the company. Each of the next $q$ lines contains a description of the events in a given format: the type of the event (a symbol $+$ or $-$) is given first, then goes the integer $x$ ($1 \le x \le 10^5$).
-----Output-----
After every event in the company, print "YES" if two storages of the required shape can be built from the planks of that company's set, and print "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
6
1 1 1 2 1 1
6
+ 2
+ 1
- 1
+ 2
- 1
+ 2
Output
NO
YES
NO
NO
NO
YES
-----Note-----
After the second event Applejack can build a rectangular storage using planks with lengths $1$, $2$, $1$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$.
After the sixth event Applejack can build a rectangular storage using planks with lengths $2$, $2$, $2$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$. | import sys
input = sys.stdin.readline
def ceil(x):
if x != int(x):
x = int(x) + 1
return x
def swaparr(arr, a, b):
temp = arr[a]
arr[a] = arr[b]
arr[b] = temp
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
def nCr(n, k):
if k > n - k:
k = n - k
res = 1
for i in range(k):
res = res * (n - i)
res = res / (i + 1)
return int(res)
def upper_bound(a, x, lo=0, hi=None):
if hi == None:
hi = len(a)
while lo < hi:
mid = (lo + hi) // 2
if a[mid] < x:
lo = mid + 1
else:
hi = mid
return lo
def primefs(n):
primes = {}
while n % 2 == 0 and n > 0:
primes[2] = primes.get(2, 0) + 1
n = n // 2
for i in range(3, int(n**0.5) + 2, 2):
while n % i == 0 and n > 0:
primes[i] = primes.get(i, 0) + 1
n = n // i
if n > 2:
primes[n] = primes.get(n, 0) + 1
return primes
def power(x, y, p):
res = 1
x = x % p
if x == 0:
return 0
while y > 0:
if y & 1 == 1:
res = res * x % p
y = y >> 1
x = x * x % p
return res
def swap(a, b):
temp = a
a = b
b = temp
return a, b
def find(x, link):
p = x
while p != link[p]:
p = link[p]
while x != p:
nex = link[x]
link[x] = p
x = nex
return p
def union(x, y, link, size):
x = find(x, link)
y = find(y, link)
if size[x] < size[y]:
x, y = swap(x, y)
if x != y:
size[x] += size[y]
link[y] = x
def sieve(n):
prime = [(True) for i in range(n + 1)]
p = 2
while p * p <= n:
if prime[p] == True:
for i in range(p * p, n + 1, p):
prime[i] = False
p += 1
return prime
MAXN = int(100000.0 + 5)
def spf_sieve():
spf[1] = 1
for i in range(2, MAXN):
spf[i] = i
for i in range(4, MAXN, 2):
spf[i] = 2
for i in range(3, ceil(MAXN**0.5), 2):
if spf[i] == i:
for j in range(i * i, MAXN, i):
if spf[j] == j:
spf[j] = i
def factoriazation(x):
ret = {}
while x != 1:
ret[spf[x]] = ret.get(spf[x], 0) + 1
x = x // spf[x]
return ret
def int_array():
return list(map(int, input().strip().split()))
def float_array():
return list(map(float, input().strip().split()))
def str_array():
return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float("inf")
NINF = -float("inf")
n = int(input())
a = int_array()
dick = {}
for i in a:
dick[i] = dick.get(i, 0) + 1
twos = fours = 0
for i in dick:
twos += dick[i] // 2
fours += dick[i] // 4
q = int(input())
for _ in range(q):
s = input().strip()
x = int(s[2:])
if x in dick:
tb = dick[x] // 2
fb = dick[x] // 4
else:
tb = fb = 0
if s[0] == "+":
dick[x] = dick.get(x, 0) + 1
else:
dick[x] -= 1
tn = dick[x] // 2
fn = dick[x] // 4
fours += fn - fb
twos += tn - tb
t = twos - 2 * fours
if fours >= 2:
print("YES")
elif fours == 1 and t >= 2:
print("YES")
else:
print("NO") | IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF NUMBER NONE IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR DICT WHILE VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).
Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has $n$ planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format:
$+$ $x$: the storehouse received a plank with length $x$ $-$ $x$: one plank with length $x$ was removed from the storehouse (it is guaranteed that the storehouse had some planks with length $x$).
Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!
We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$): the initial amount of planks at the company's storehouse, the second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$): the lengths of the planks.
The third line contains a single integer $q$ ($1 \le q \le 10^5$): the number of events in the company. Each of the next $q$ lines contains a description of the events in a given format: the type of the event (a symbol $+$ or $-$) is given first, then goes the integer $x$ ($1 \le x \le 10^5$).
-----Output-----
After every event in the company, print "YES" if two storages of the required shape can be built from the planks of that company's set, and print "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
6
1 1 1 2 1 1
6
+ 2
+ 1
- 1
+ 2
- 1
+ 2
Output
NO
YES
NO
NO
NO
YES
-----Note-----
After the second event Applejack can build a rectangular storage using planks with lengths $1$, $2$, $1$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$.
After the sixth event Applejack can build a rectangular storage using planks with lengths $2$, $2$, $2$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$. | 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]
def add(a):
cnt[a] += 1
if cnt[a] == 8:
ss[3] += 1
ss[2] -= 1
if cnt[a] == 6:
ss[2] += 1
ss[1] -= 1
if cnt[a] == 4:
ss[1] += 1
ss[0] -= 1
if cnt[a] == 2:
ss[0] += 1
def sub(a):
cnt[a] -= 1
if cnt[a] == 7:
ss[3] -= 1
ss[2] += 1
if cnt[a] == 5:
ss[2] -= 1
ss[1] += 1
if cnt[a] == 3:
ss[1] -= 1
ss[0] += 1
if cnt[a] == 1:
ss[0] -= 1
n = II()
aa = LI()
ss = [0] * 4
cnt = [0] * 100005
for a in aa:
add(a)
for _ in range(II()):
op, a = SI().split()
if op == "+":
add(int(a))
else:
sub(int(a))
if ss[3] or ss[2] and sum(ss[:3]) > 1 or ss[1] and sum(ss[:2]) > 2 or ss[1] > 1:
print("YES")
else:
print("NO") | 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 FUNC_DEF VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER NUMBER FUNC_DEF VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).
Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has $n$ planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format:
$+$ $x$: the storehouse received a plank with length $x$ $-$ $x$: one plank with length $x$ was removed from the storehouse (it is guaranteed that the storehouse had some planks with length $x$).
Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!
We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$): the initial amount of planks at the company's storehouse, the second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$): the lengths of the planks.
The third line contains a single integer $q$ ($1 \le q \le 10^5$): the number of events in the company. Each of the next $q$ lines contains a description of the events in a given format: the type of the event (a symbol $+$ or $-$) is given first, then goes the integer $x$ ($1 \le x \le 10^5$).
-----Output-----
After every event in the company, print "YES" if two storages of the required shape can be built from the planks of that company's set, and print "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
6
1 1 1 2 1 1
6
+ 2
+ 1
- 1
+ 2
- 1
+ 2
Output
NO
YES
NO
NO
NO
YES
-----Note-----
After the second event Applejack can build a rectangular storage using planks with lengths $1$, $2$, $1$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$.
After the sixth event Applejack can build a rectangular storage using planks with lengths $2$, $2$, $2$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$. | T = int(input())
init = [int(x) for x in input().split()]
storage = {}
pairs = 0
quads = 0
octs = 0
for i in init:
try:
storage[i] += 1
except:
storage[i] = 1
if storage[i] % 2 == 0:
pairs += 1
if storage[i] % 4 == 0:
quads += 1
Q = int(input())
for q in range(Q):
ls = [x for x in input().split()]
op = ls[0]
l = int(ls[1])
if op == "+":
try:
storage[l] += 1
except:
storage[l] = 1
if storage[l] % 2 == 0:
pairs += 1
if storage[l] % 4 == 0:
quads += 1
else:
storage[l] -= 1
if storage[l] % 2 == 1:
pairs -= 1
if storage[l] % 4 == 3:
quads -= 1
if quads >= 2:
print("YES")
elif quads and pairs >= 4:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR STRING VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).
Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has $n$ planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format:
$+$ $x$: the storehouse received a plank with length $x$ $-$ $x$: one plank with length $x$ was removed from the storehouse (it is guaranteed that the storehouse had some planks with length $x$).
Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!
We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$): the initial amount of planks at the company's storehouse, the second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$): the lengths of the planks.
The third line contains a single integer $q$ ($1 \le q \le 10^5$): the number of events in the company. Each of the next $q$ lines contains a description of the events in a given format: the type of the event (a symbol $+$ or $-$) is given first, then goes the integer $x$ ($1 \le x \le 10^5$).
-----Output-----
After every event in the company, print "YES" if two storages of the required shape can be built from the planks of that company's set, and print "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
6
1 1 1 2 1 1
6
+ 2
+ 1
- 1
+ 2
- 1
+ 2
Output
NO
YES
NO
NO
NO
YES
-----Note-----
After the second event Applejack can build a rectangular storage using planks with lengths $1$, $2$, $1$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$.
After the sixth event Applejack can build a rectangular storage using planks with lengths $2$, $2$, $2$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$. | n = int(input())
a = list(map(int, input().split()))
q = int(input())
d = {}
for i in range(n):
if a[i] not in d:
d[a[i]] = 1
else:
d[a[i]] += 1
sides = 0
chk = False
chk2 = 0
for x in d.keys():
chk2 += d[x] // 4
sides += d[x] // 2
for j in range(q):
s = list(input().split())
if s[0] == "+":
if int(s[1]) not in d:
d[int(s[1])] = 1
else:
if d[int(s[1])] % 4 == 3:
chk2 += 1
if d[int(s[1])] % 2 == 1:
sides += 1
d[int(s[1])] += 1
n += 1
else:
if d[int(s[1])] % 2 == 0:
sides -= 1
if d[int(s[1])] % 4 == 0:
chk2 -= 1
d[int(s[1])] -= 1
n -= 1
if n < 8:
print("NO")
continue
if sides >= 4:
chk = True
else:
chk = False
if chk and chk2 > 0:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING IF FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).
Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has $n$ planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format:
$+$ $x$: the storehouse received a plank with length $x$ $-$ $x$: one plank with length $x$ was removed from the storehouse (it is guaranteed that the storehouse had some planks with length $x$).
Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!
We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$): the initial amount of planks at the company's storehouse, the second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$): the lengths of the planks.
The third line contains a single integer $q$ ($1 \le q \le 10^5$): the number of events in the company. Each of the next $q$ lines contains a description of the events in a given format: the type of the event (a symbol $+$ or $-$) is given first, then goes the integer $x$ ($1 \le x \le 10^5$).
-----Output-----
After every event in the company, print "YES" if two storages of the required shape can be built from the planks of that company's set, and print "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
6
1 1 1 2 1 1
6
+ 2
+ 1
- 1
+ 2
- 1
+ 2
Output
NO
YES
NO
NO
NO
YES
-----Note-----
After the second event Applejack can build a rectangular storage using planks with lengths $1$, $2$, $1$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$.
After the sixth event Applejack can build a rectangular storage using planks with lengths $2$, $2$, $2$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$. | n = int(input())
a = list(map(int, input().split()))
b = {}
for el in a:
if el in b:
b[el] += 1
else:
b[el] = 1
a = b
q = int(input())
n2, n4, n6, n8 = 0, 0, 0, 0
for i in a.values():
if i >= 2:
n2 += 1
if i >= 4:
n4 += 1
if i >= 6:
n6 += 1
if i >= 8:
n8 += 1
for _ in range(q):
s, k = input().split()
k = int(k)
if s == "+":
if k not in a:
a[k] = 0
a[k] += 1
if a[k] == 2:
n2 += 1
elif a[k] == 4:
n4 += 1
elif a[k] == 6:
n6 += 1
elif a[k] == 8:
n8 += 1
else:
a[k] -= 1
if a[k] == 1:
n2 -= 1
elif a[k] == 3:
n4 -= 1
elif a[k] == 5:
n6 -= 1
elif a[k] == 7:
n8 -= 1
if n8 > 0:
print("YES")
continue
if n6 > 0 and n2 >= 2:
print("YES")
continue
if n4 > 0 and n2 >= 3:
print("YES")
continue
if n4 >= 2:
print("YES")
continue
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).
Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has $n$ planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format:
$+$ $x$: the storehouse received a plank with length $x$ $-$ $x$: one plank with length $x$ was removed from the storehouse (it is guaranteed that the storehouse had some planks with length $x$).
Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!
We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$): the initial amount of planks at the company's storehouse, the second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$): the lengths of the planks.
The third line contains a single integer $q$ ($1 \le q \le 10^5$): the number of events in the company. Each of the next $q$ lines contains a description of the events in a given format: the type of the event (a symbol $+$ or $-$) is given first, then goes the integer $x$ ($1 \le x \le 10^5$).
-----Output-----
After every event in the company, print "YES" if two storages of the required shape can be built from the planks of that company's set, and print "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
6
1 1 1 2 1 1
6
+ 2
+ 1
- 1
+ 2
- 1
+ 2
Output
NO
YES
NO
NO
NO
YES
-----Note-----
After the second event Applejack can build a rectangular storage using planks with lengths $1$, $2$, $1$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$.
After the sixth event Applejack can build a rectangular storage using planks with lengths $2$, $2$, $2$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$. | def fu(l):
if l[8] >= 1:
print("Yes")
return
if sum(l[4:]) >= 2:
print("YES")
return
if sum(l[6:]) >= 1:
if sum(l[2:6]) >= 1:
print("YES")
return
if l[4] == 1 or l[5] == 1:
if sum(l[2:4]) >= 2:
print("YES")
return
print("NO")
return
n = int(input())
l = list(map(int, input().split()))
d = {}
cou = [0] * 9
for i in l:
if d.get(i, 0) != 0:
if d[i] >= 8:
d[i] += 1
else:
cou[d[i]] -= 1
d[i] += 1
cou[d[i]] += 1
else:
d[i] = 1
cou[1] += 1
q = int(input())
for i in range(q):
temp = input().split()
if temp[0] == "-":
t = int(temp[1])
d[t] = d.get(t, 0)
if d[t] <= 8:
cou[d[t]] -= 1
d[t] -= 1
cou[d[t]] += 1
else:
d[t] -= 1
else:
t = int(temp[1])
d[t] = d.get(t, 0)
if d[t] >= 8:
d[t] += 1
else:
cou[d[t]] -= 1
d[t] += 1
cou[d[t]] += 1
fu(cou) | FUNC_DEF IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN IF FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).
Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has $n$ planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format:
$+$ $x$: the storehouse received a plank with length $x$ $-$ $x$: one plank with length $x$ was removed from the storehouse (it is guaranteed that the storehouse had some planks with length $x$).
Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!
We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$): the initial amount of planks at the company's storehouse, the second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$): the lengths of the planks.
The third line contains a single integer $q$ ($1 \le q \le 10^5$): the number of events in the company. Each of the next $q$ lines contains a description of the events in a given format: the type of the event (a symbol $+$ or $-$) is given first, then goes the integer $x$ ($1 \le x \le 10^5$).
-----Output-----
After every event in the company, print "YES" if two storages of the required shape can be built from the planks of that company's set, and print "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
6
1 1 1 2 1 1
6
+ 2
+ 1
- 1
+ 2
- 1
+ 2
Output
NO
YES
NO
NO
NO
YES
-----Note-----
After the second event Applejack can build a rectangular storage using planks with lengths $1$, $2$, $1$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$.
After the sixth event Applejack can build a rectangular storage using planks with lengths $2$, $2$, $2$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$. | n = int(input())
a = [int(i) for i in input().split()]
q = int(input())
count = [0] * 100006
for i in a:
count[i] += 1
four = 0
two = 0
six = 0
eight = 0
for i in count:
if i >= 4:
four += 1
if i >= 2:
two += 1
if i >= 6:
six += 1
if i >= 8:
eight += 1
for _ in range(q):
ss = input().split()
if ss[0] == "+":
sz = int(ss[1])
count[sz] += 1
if count[sz] == 2:
two += 1
if count[sz] == 4:
four += 1
if count[sz] == 6:
six += 1
if count[sz] == 8:
eight += 1
else:
sz = int(ss[1])
count[sz] -= 1
if count[sz] == 1:
two -= 1
if count[sz] == 3:
four -= 1
if count[sz] == 5:
six -= 1
if count[sz] == 7:
eight -= 1
if two >= 3 and four >= 1:
print("YES")
elif eight >= 1:
print("YES")
elif two == 2 and six >= 1:
print("YES")
elif four == 2:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).
Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has $n$ planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format:
$+$ $x$: the storehouse received a plank with length $x$ $-$ $x$: one plank with length $x$ was removed from the storehouse (it is guaranteed that the storehouse had some planks with length $x$).
Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!
We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$): the initial amount of planks at the company's storehouse, the second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$): the lengths of the planks.
The third line contains a single integer $q$ ($1 \le q \le 10^5$): the number of events in the company. Each of the next $q$ lines contains a description of the events in a given format: the type of the event (a symbol $+$ or $-$) is given first, then goes the integer $x$ ($1 \le x \le 10^5$).
-----Output-----
After every event in the company, print "YES" if two storages of the required shape can be built from the planks of that company's set, and print "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
6
1 1 1 2 1 1
6
+ 2
+ 1
- 1
+ 2
- 1
+ 2
Output
NO
YES
NO
NO
NO
YES
-----Note-----
After the second event Applejack can build a rectangular storage using planks with lengths $1$, $2$, $1$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$.
After the sixth event Applejack can build a rectangular storage using planks with lengths $2$, $2$, $2$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$. | def add(e):
if e in dic:
dic[e] += 1
else:
dic[e] = 1
if dic[e] % 4 == 0:
return 1, -1
elif dic[e] % 2 == 0:
return 0, 1
else:
return 0, 0
def remove(e):
dic[e] -= 1
if (dic[e] + 1) % 4 == 0:
return -1, +1
elif (dic[e] + 1) % 2 == 0:
return 0, -1
else:
return 0, 0
n = int(input())
l = map(int, input().split(" "))
dic = {}
for e in l:
add(e)
square = 0
rec = 0
for e in dic:
square += dic[e] // 4
rec += dic[e] % 4 // 2
n = int(input())
for i in range(n):
line = input().split(" ")
if line[0] == "+":
a, b = add(int(line[1]))
square += a
rec += b
else:
a, b = remove(int(line[1]))
square += a
rec += b
if square >= 2 or square >= 1 and rec >= 2:
print("YES")
else:
print("NO") | FUNC_DEF IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER NUMBER RETURN NUMBER NUMBER FUNC_DEF VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER RETURN NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER RETURN NUMBER NUMBER RETURN NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER STRING ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).
Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has $n$ planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format:
$+$ $x$: the storehouse received a plank with length $x$ $-$ $x$: one plank with length $x$ was removed from the storehouse (it is guaranteed that the storehouse had some planks with length $x$).
Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!
We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$): the initial amount of planks at the company's storehouse, the second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$): the lengths of the planks.
The third line contains a single integer $q$ ($1 \le q \le 10^5$): the number of events in the company. Each of the next $q$ lines contains a description of the events in a given format: the type of the event (a symbol $+$ or $-$) is given first, then goes the integer $x$ ($1 \le x \le 10^5$).
-----Output-----
After every event in the company, print "YES" if two storages of the required shape can be built from the planks of that company's set, and print "NO" otherwise. You can print each letter in any case (upper or lower).
-----Example-----
Input
6
1 1 1 2 1 1
6
+ 2
+ 1
- 1
+ 2
- 1
+ 2
Output
NO
YES
NO
NO
NO
YES
-----Note-----
After the second event Applejack can build a rectangular storage using planks with lengths $1$, $2$, $1$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$.
After the sixth event Applejack can build a rectangular storage using planks with lengths $2$, $2$, $2$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$. | for _ in range(0, 1):
n = int(input())
liStr = input().split()
arr = []
for i in liStr:
arr.append(int(i))
di = {}
for i in arr:
di[i] = di.get(i, 0) + 1
tp1 = 0
tp2 = 0
tp3 = 0
tp4 = 0
tp5 = 0
for i in di:
a = di[i]
if a >= 8:
tp1 += 1
if a >= 6 and a <= 7:
tp2 += 1
if a >= 4 and a <= 5:
tp3 += 1
if a >= 2 and a <= 3:
tp4 += 1
if a < 2:
tp5 += 1
T = int(input())
for _ in range(0, T):
s = input()
ch = s[0:1]
x = int(s[2:])
if ch == "+":
oldFreq = di.get(x, 0)
newFreq = oldFreq + 1
di[x] = newFreq
if newFreq == 2:
tp5 -= 1
tp4 += 1
if newFreq == 4:
tp4 -= 1
tp3 += 1
if newFreq == 6:
tp3 -= 1
tp2 += 1
if newFreq == 8:
tp2 -= 1
tp1 += 1
if ch == "-":
oldFreq = di.get(x, 0)
newFreq = oldFreq - 1
di[x] = newFreq
if newFreq == 1:
tp4 -= 1
tp5 += 1
if newFreq == 3:
tp3 -= 1
tp4 += 1
if newFreq == 5:
tp2 -= 1
tp3 += 1
if newFreq == 7:
tp1 -= 1
tp2 += 1
if tp1 > 0:
print("YES")
continue
if tp2 > 0 and (tp3 > 0 or tp4 > 0):
print("YES")
continue
if tp2 > 1:
print("YES")
continue
if tp3 > 1:
print("YES")
continue
if tp3 > 0 and tp4 > 1:
print("YES")
continue
print("NO") | FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER 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.