description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible.
In one operation, he can choose any subsequence of $s$ and rotate it clockwise once.
For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$.
A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters.
To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously.
Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible.
-----Input-----
The first line contains a single integer $n$ $(1 \le n \le 10^6)$Β β the length of the strings.
The second line contains the binary string $s$ of length $n$.
The third line contains the binary string $t$ of length $n$.
-----Output-----
If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$.
Otherwise, print the minimum number of operations required.
-----Examples-----
Input
6
010000
000001
Output
1
Input
10
1111100000
0000011111
Output
5
Input
8
10101010
01010101
Output
1
Input
10
1111100000
1111100001
Output
-1
-----Note-----
In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$.
In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations.
In the last test, it is impossible to convert $s$ into $t$. | m = int(input())
s = list(input())
t = list(input())
i = 0
a = c = d = 0
while i < m:
if s[i] != t[i]:
if s[i] == "0":
a += 1
else:
a -= 1
c = min(c, a)
d = max(d, a)
i += 1
if a == 0:
print(d - c)
else:
print(-1) | 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 NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER |
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible.
In one operation, he can choose any subsequence of $s$ and rotate it clockwise once.
For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$.
A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters.
To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously.
Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible.
-----Input-----
The first line contains a single integer $n$ $(1 \le n \le 10^6)$Β β the length of the strings.
The second line contains the binary string $s$ of length $n$.
The third line contains the binary string $t$ of length $n$.
-----Output-----
If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$.
Otherwise, print the minimum number of operations required.
-----Examples-----
Input
6
010000
000001
Output
1
Input
10
1111100000
0000011111
Output
5
Input
8
10101010
01010101
Output
1
Input
10
1111100000
1111100001
Output
-1
-----Note-----
In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$.
In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations.
In the last test, it is impossible to convert $s$ into $t$. | q = int(input())
w = input()
e = input()
r = []
if w.count("1") != e.count("1"):
print(-1)
exit()
if w.count("1") == q or w.count("1") == 0:
print(0)
exit()
for i in range(q):
if w[i] != e[i]:
r.append(e[i])
k = 0
l = []
for i in r:
if i == "1":
k += 1
else:
k -= 1
l.append(k)
print(max(l) + abs(min(l))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR STRING VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR |
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible.
In one operation, he can choose any subsequence of $s$ and rotate it clockwise once.
For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$.
A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters.
To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously.
Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible.
-----Input-----
The first line contains a single integer $n$ $(1 \le n \le 10^6)$Β β the length of the strings.
The second line contains the binary string $s$ of length $n$.
The third line contains the binary string $t$ of length $n$.
-----Output-----
If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$.
Otherwise, print the minimum number of operations required.
-----Examples-----
Input
6
010000
000001
Output
1
Input
10
1111100000
0000011111
Output
5
Input
8
10101010
01010101
Output
1
Input
10
1111100000
1111100001
Output
-1
-----Note-----
In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$.
In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations.
In the last test, it is impossible to convert $s$ into $t$. | n = int(input())
s = input()
t = input()
res = 0
counts0, counts1, countt0, countt1 = 0, 0, 0, 0
start0, start1 = 0, 0
for i in range(n):
if s[i] == t[i]:
continue
elif s[i] == "0":
counts0 += 1
countt1 += 1
if start1 == 0:
res += 1
start0 += 1
else:
start1 -= 1
start0 += 1
else:
counts1 += 1
countt0 += 1
if start0 == 0:
res += 1
start1 += 1
else:
start0 -= 1
start1 += 1
if counts0 != countt0 or counts1 != countt1:
print(-1)
else:
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible.
In one operation, he can choose any subsequence of $s$ and rotate it clockwise once.
For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$.
A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters.
To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously.
Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible.
-----Input-----
The first line contains a single integer $n$ $(1 \le n \le 10^6)$Β β the length of the strings.
The second line contains the binary string $s$ of length $n$.
The third line contains the binary string $t$ of length $n$.
-----Output-----
If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$.
Otherwise, print the minimum number of operations required.
-----Examples-----
Input
6
010000
000001
Output
1
Input
10
1111100000
0000011111
Output
5
Input
8
10101010
01010101
Output
1
Input
10
1111100000
1111100001
Output
-1
-----Note-----
In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$.
In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations.
In the last test, it is impossible to convert $s$ into $t$. | def maxx(a, x):
p = 0
count = 0
for i in a:
p += x * i
count = max(count, p)
if p < 0:
p = 0
return count
n = int(input())
s = str(input())
t = str(input())
a = []
to = 0
tno = 0
if s == t:
print(0)
else:
for i in range(n):
if s[i] == "1" and t[i] == "0":
a.append(1)
if s[i] == "0" and t[i] == "1":
a.append(-1)
if sum(a) != 0:
print(-1)
else:
print(max(maxx(a, -1), maxx(a, 1))) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN 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 LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER |
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible.
In one operation, he can choose any subsequence of $s$ and rotate it clockwise once.
For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$.
A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters.
To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously.
Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible.
-----Input-----
The first line contains a single integer $n$ $(1 \le n \le 10^6)$Β β the length of the strings.
The second line contains the binary string $s$ of length $n$.
The third line contains the binary string $t$ of length $n$.
-----Output-----
If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$.
Otherwise, print the minimum number of operations required.
-----Examples-----
Input
6
010000
000001
Output
1
Input
10
1111100000
0000011111
Output
5
Input
8
10101010
01010101
Output
1
Input
10
1111100000
1111100001
Output
-1
-----Note-----
In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$.
In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations.
In the last test, it is impossible to convert $s$ into $t$. | n = input()
s = input()
t = input()
zero = 0
mmax = 0
mmin = 0
for i in range(len(s)):
if s[i] != t[i]:
if s[i] == "0":
zero = zero + 1
else:
zero = zero - 1
mmax = max(zero, mmax)
mmin = min(zero, mmin)
if zero == 0:
print(mmax - mmin)
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER |
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible.
In one operation, he can choose any subsequence of $s$ and rotate it clockwise once.
For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$.
A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters.
To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously.
Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible.
-----Input-----
The first line contains a single integer $n$ $(1 \le n \le 10^6)$Β β the length of the strings.
The second line contains the binary string $s$ of length $n$.
The third line contains the binary string $t$ of length $n$.
-----Output-----
If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$.
Otherwise, print the minimum number of operations required.
-----Examples-----
Input
6
010000
000001
Output
1
Input
10
1111100000
0000011111
Output
5
Input
8
10101010
01010101
Output
1
Input
10
1111100000
1111100001
Output
-1
-----Note-----
In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$.
In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations.
In the last test, it is impossible to convert $s$ into $t$. | n, s, t, p = int(input()), input(), input(), [0]
for i in range(n):
if s[i] != t[i]:
p.append(p[-1] + (1 if s[i] == "1" else -1))
print(max(p) - min(p) if p[-1] == 0 else -1) | ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR STRING NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER |
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible.
In one operation, he can choose any subsequence of $s$ and rotate it clockwise once.
For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$.
A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters.
To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously.
Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible.
-----Input-----
The first line contains a single integer $n$ $(1 \le n \le 10^6)$Β β the length of the strings.
The second line contains the binary string $s$ of length $n$.
The third line contains the binary string $t$ of length $n$.
-----Output-----
If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$.
Otherwise, print the minimum number of operations required.
-----Examples-----
Input
6
010000
000001
Output
1
Input
10
1111100000
0000011111
Output
5
Input
8
10101010
01010101
Output
1
Input
10
1111100000
1111100001
Output
-1
-----Note-----
In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$.
In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations.
In the last test, it is impossible to convert $s$ into $t$. | n = int(input())
s = list(map(str, input()))
t = list(map(str, input()))
a, b = 0, 0
for i in s:
if i == "1":
a += 1
for i in t:
if i == "1":
b += 1
if a != b:
print(-1)
elif s == t:
print(0)
else:
ones, zeros = 0, 0
for i in range(n):
if s[i] == "1" and t[i] == "0":
if zeros:
zeros -= 1
ones += 1
elif s[i] == "0" and t[i] == "1":
if ones:
ones -= 1
zeros += 1
print(ones + zeros) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING IF VAR VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible.
In one operation, he can choose any subsequence of $s$ and rotate it clockwise once.
For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$.
A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters.
To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously.
Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible.
-----Input-----
The first line contains a single integer $n$ $(1 \le n \le 10^6)$Β β the length of the strings.
The second line contains the binary string $s$ of length $n$.
The third line contains the binary string $t$ of length $n$.
-----Output-----
If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$.
Otherwise, print the minimum number of operations required.
-----Examples-----
Input
6
010000
000001
Output
1
Input
10
1111100000
0000011111
Output
5
Input
8
10101010
01010101
Output
1
Input
10
1111100000
1111100001
Output
-1
-----Note-----
In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$.
In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations.
In the last test, it is impossible to convert $s$ into $t$. | n = int(input())
s = input()
t = input()
moves = []
s1_z = 0
s2_z = 0
def check(num):
ret = 0
mx = 0
for z in moves:
ret += num * z
mx = max(mx, ret)
if ret < 0:
ret = 0
return mx
for i in range(n):
if s[i] == "0":
s1_z += 1
if t[i] == "0":
s2_z += 1
if s[i] != t[i]:
if s[i] == "1":
moves.append(-1)
else:
moves.append(1)
if s1_z != s2_z:
print(-1)
else:
print(max(check(1), check(-1))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER |
Lee bought some food for dinner time, but Lee's friends eat dinner in a deadly way. Lee is so scared, he doesn't want to die, at least not before seeing Online IOI 2020...
There are $n$ different types of food and $m$ Lee's best friends. Lee has $w_i$ plates of the $i$-th type of food and each friend has two different favorite types of food: the $i$-th friend's favorite types of food are $x_i$ and $y_i$ ($x_i \ne y_i$).
Lee will start calling his friends one by one. Whoever is called will go to the kitchen and will try to eat one plate of each of his favorite food types. Each of the friends will go to the kitchen exactly once.
The only problem is the following: if a friend will eat at least one plate of food (in total) then he will be harmless. But if there is nothing left for him to eat (neither $x_i$ nor $y_i$), he will eat Lee instead $\times\_\times$.
Lee can choose the order of friends to call, so he'd like to determine if he can survive dinner or not. Also, he'd like to know the order itself.
-----Input-----
The first line contains two integers $n$ and $m$ ($2 \le n \le 10^5$; $1 \le m \le 2 \cdot 10^5$)Β β the number of different food types and the number of Lee's friends.
The second line contains $n$ integers $w_1, w_2, \ldots, w_n$ ($0 \le w_i \le 10^6$)Β β the number of plates of each food type.
The $i$-th line of the next $m$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$; $x_i \ne y_i$)Β β the favorite types of food of the $i$-th friend.
-----Output-----
If Lee can survive the dinner then print ALIVE (case insensitive), otherwise print DEAD (case insensitive).
Also, if he can survive the dinner, print the order Lee should call friends. If there are multiple valid orders, print any of them.
-----Examples-----
Input
3 3
1 2 1
1 2
2 3
1 3
Output
ALIVE
3 2 1
Input
3 2
1 1 0
1 2
1 3
Output
ALIVE
2 1
Input
4 4
1 2 0 1
1 3
1 2
2 3
2 4
Output
ALIVE
1 3 2 4
Input
5 5
1 1 1 2 1
3 4
1 2
2 3
4 5
4 5
Output
ALIVE
5 4 1 3 2
Input
4 10
2 4 1 4
3 2
4 2
4 1
3 1
4 1
1 3
3 2
2 1
3 1
2 4
Output
DEAD
-----Note-----
In the first example, any of the following orders of friends are correct : $[1, 3, 2]$, $[3, 1, 2]$, $[2, 3, 1]$, $[3, 2, 1]$.
In the second example, Lee should call the second friend first (the friend will eat a plate of food $1$) and then call the first friend (the friend will eat a plate of food $2$). If he calls the first friend sooner than the second one, then the first friend will eat one plate of food $1$ and food $2$ and there will be no food left for the second friend to eat. | n, m = map(int, input().split())
fs = [[] for i in range(n + 1)]
q, vis, req = [([0] * (n + 1)) for i in range(3)]
res, head, tail = [], 0, 0
used = [0] * (m + 1)
w = [0] + list(map(int, input().split()))
for i in range(1, m + 1):
x, y = map(int, input().split())
fs[x].append((i, x, y))
fs[y].append((i, x, y))
req[x] += 1
req[y] += 1
for i in range(1, n + 1):
if req[i] <= w[i]:
q[tail] = i
tail += 1
vis[i] = 1
while head < tail:
x = q[head]
head += 1
for u in fs[x]:
if not used[u[0]]:
used[u[0]] = 1
res.append(u[0])
if u[1] == x:
y = u[2]
else:
y = u[1]
req[y] -= 1
if not vis[y] and req[y] <= w[y]:
q[tail] = y
tail += 1
vis[y] = 1
res.reverse()
if len(res) < m:
print("DEAD")
else:
print("ALIVE")
for u in res:
print(u) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR |
Lee bought some food for dinner time, but Lee's friends eat dinner in a deadly way. Lee is so scared, he doesn't want to die, at least not before seeing Online IOI 2020...
There are $n$ different types of food and $m$ Lee's best friends. Lee has $w_i$ plates of the $i$-th type of food and each friend has two different favorite types of food: the $i$-th friend's favorite types of food are $x_i$ and $y_i$ ($x_i \ne y_i$).
Lee will start calling his friends one by one. Whoever is called will go to the kitchen and will try to eat one plate of each of his favorite food types. Each of the friends will go to the kitchen exactly once.
The only problem is the following: if a friend will eat at least one plate of food (in total) then he will be harmless. But if there is nothing left for him to eat (neither $x_i$ nor $y_i$), he will eat Lee instead $\times\_\times$.
Lee can choose the order of friends to call, so he'd like to determine if he can survive dinner or not. Also, he'd like to know the order itself.
-----Input-----
The first line contains two integers $n$ and $m$ ($2 \le n \le 10^5$; $1 \le m \le 2 \cdot 10^5$)Β β the number of different food types and the number of Lee's friends.
The second line contains $n$ integers $w_1, w_2, \ldots, w_n$ ($0 \le w_i \le 10^6$)Β β the number of plates of each food type.
The $i$-th line of the next $m$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$; $x_i \ne y_i$)Β β the favorite types of food of the $i$-th friend.
-----Output-----
If Lee can survive the dinner then print ALIVE (case insensitive), otherwise print DEAD (case insensitive).
Also, if he can survive the dinner, print the order Lee should call friends. If there are multiple valid orders, print any of them.
-----Examples-----
Input
3 3
1 2 1
1 2
2 3
1 3
Output
ALIVE
3 2 1
Input
3 2
1 1 0
1 2
1 3
Output
ALIVE
2 1
Input
4 4
1 2 0 1
1 3
1 2
2 3
2 4
Output
ALIVE
1 3 2 4
Input
5 5
1 1 1 2 1
3 4
1 2
2 3
4 5
4 5
Output
ALIVE
5 4 1 3 2
Input
4 10
2 4 1 4
3 2
4 2
4 1
3 1
4 1
1 3
3 2
2 1
3 1
2 4
Output
DEAD
-----Note-----
In the first example, any of the following orders of friends are correct : $[1, 3, 2]$, $[3, 1, 2]$, $[2, 3, 1]$, $[3, 2, 1]$.
In the second example, Lee should call the second friend first (the friend will eat a plate of food $1$) and then call the first friend (the friend will eat a plate of food $2$). If he calls the first friend sooner than the second one, then the first friend will eat one plate of food $1$ and food $2$ and there will be no food left for the second friend to eat. | import sys
input = sys.stdin.readline
def line_input():
return [int(x) for x in input().split()]
def main():
n, m = line_input()
w = line_input()
adj = [[] for _ in range(n)]
edges = []
deg = [0] * n
for i in range(m):
u, v = line_input()
u, v = u - 1, v - 1
adj[u].append(v)
adj[v].append(u)
edges.append((u, v))
deg[u] += 1
deg[v] += 1
vis = [False] * n
order = [0] * n
st = []
cur = 0
for i in range(n):
if deg[i] <= w[i]:
vis[i] = True
order[i] = cur
cur += 1
st.append(i)
while len(st):
u = st.pop()
for v in adj[u]:
deg[v] -= 1
if not vis[v] and deg[v] <= w[v]:
vis[v] = True
order[v] = cur
cur += 1
st.append(v)
if sum(vis) < n:
print("DEAD")
else:
print("ALIVE")
edge_order = [
sorted([order[edges[i][0]], order[edges[i][1]]]) for i in range(m)
]
idx = [x for x in range(m)]
print(
*[(x + 1) for x in sorted(idx, key=lambda x: edge_order[x], reverse=True)]
)
main() | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR |
Lee bought some food for dinner time, but Lee's friends eat dinner in a deadly way. Lee is so scared, he doesn't want to die, at least not before seeing Online IOI 2020...
There are $n$ different types of food and $m$ Lee's best friends. Lee has $w_i$ plates of the $i$-th type of food and each friend has two different favorite types of food: the $i$-th friend's favorite types of food are $x_i$ and $y_i$ ($x_i \ne y_i$).
Lee will start calling his friends one by one. Whoever is called will go to the kitchen and will try to eat one plate of each of his favorite food types. Each of the friends will go to the kitchen exactly once.
The only problem is the following: if a friend will eat at least one plate of food (in total) then he will be harmless. But if there is nothing left for him to eat (neither $x_i$ nor $y_i$), he will eat Lee instead $\times\_\times$.
Lee can choose the order of friends to call, so he'd like to determine if he can survive dinner or not. Also, he'd like to know the order itself.
-----Input-----
The first line contains two integers $n$ and $m$ ($2 \le n \le 10^5$; $1 \le m \le 2 \cdot 10^5$)Β β the number of different food types and the number of Lee's friends.
The second line contains $n$ integers $w_1, w_2, \ldots, w_n$ ($0 \le w_i \le 10^6$)Β β the number of plates of each food type.
The $i$-th line of the next $m$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$; $x_i \ne y_i$)Β β the favorite types of food of the $i$-th friend.
-----Output-----
If Lee can survive the dinner then print ALIVE (case insensitive), otherwise print DEAD (case insensitive).
Also, if he can survive the dinner, print the order Lee should call friends. If there are multiple valid orders, print any of them.
-----Examples-----
Input
3 3
1 2 1
1 2
2 3
1 3
Output
ALIVE
3 2 1
Input
3 2
1 1 0
1 2
1 3
Output
ALIVE
2 1
Input
4 4
1 2 0 1
1 3
1 2
2 3
2 4
Output
ALIVE
1 3 2 4
Input
5 5
1 1 1 2 1
3 4
1 2
2 3
4 5
4 5
Output
ALIVE
5 4 1 3 2
Input
4 10
2 4 1 4
3 2
4 2
4 1
3 1
4 1
1 3
3 2
2 1
3 1
2 4
Output
DEAD
-----Note-----
In the first example, any of the following orders of friends are correct : $[1, 3, 2]$, $[3, 1, 2]$, $[2, 3, 1]$, $[3, 2, 1]$.
In the second example, Lee should call the second friend first (the friend will eat a plate of food $1$) and then call the first friend (the friend will eat a plate of food $2$). If he calls the first friend sooner than the second one, then the first friend will eat one plate of food $1$ and food $2$ and there will be no food left for the second friend to eat. | from sys import stdin, stdout
def dead_lee(n, m, w, xy, s):
queue = []
for i in range(n):
if w[i] >= len(s[i]):
queue.append(i)
pl = []
while len(queue) > 0:
cur = queue.pop()
w[cur] = 0
for i in s[cur]:
pl.append(i + 1)
if xy[i][0] != cur:
s[xy[i][0]].remove(i)
if w[xy[i][0]] == len(s[xy[i][0]]) and w[xy[i][0]] > 0:
queue.append(xy[i][0])
else:
s[xy[i][1]].remove(i)
if w[xy[i][1]] == len(s[xy[i][1]]) and w[xy[i][1]] > 0:
queue.append(xy[i][1])
if len(pl) == m:
pl.reverse()
return ["ALIVE", pl]
else:
return ["DEAD"]
n, m = map(int, stdin.readline().split())
w = list(map(int, stdin.readline().split()))
xy = [None for i in range(m)]
s = [set() for i in range(n)]
for i in range(m):
xy[i] = list(map(int, stdin.readline().split()))
xy[i][0] -= 1
xy[i][1] -= 1
s[xy[i][0]].add(i)
s[xy[i][1]].add(i)
res = dead_lee(n, m, w, xy, s)
stdout.write(res[0] + "\n")
if len(res) > 1:
stdout.write(" ".join(map(str, res[1])) + "\n") | FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR RETURN LIST STRING VAR RETURN LIST STRING 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 NONE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER STRING |
Lee bought some food for dinner time, but Lee's friends eat dinner in a deadly way. Lee is so scared, he doesn't want to die, at least not before seeing Online IOI 2020...
There are $n$ different types of food and $m$ Lee's best friends. Lee has $w_i$ plates of the $i$-th type of food and each friend has two different favorite types of food: the $i$-th friend's favorite types of food are $x_i$ and $y_i$ ($x_i \ne y_i$).
Lee will start calling his friends one by one. Whoever is called will go to the kitchen and will try to eat one plate of each of his favorite food types. Each of the friends will go to the kitchen exactly once.
The only problem is the following: if a friend will eat at least one plate of food (in total) then he will be harmless. But if there is nothing left for him to eat (neither $x_i$ nor $y_i$), he will eat Lee instead $\times\_\times$.
Lee can choose the order of friends to call, so he'd like to determine if he can survive dinner or not. Also, he'd like to know the order itself.
-----Input-----
The first line contains two integers $n$ and $m$ ($2 \le n \le 10^5$; $1 \le m \le 2 \cdot 10^5$)Β β the number of different food types and the number of Lee's friends.
The second line contains $n$ integers $w_1, w_2, \ldots, w_n$ ($0 \le w_i \le 10^6$)Β β the number of plates of each food type.
The $i$-th line of the next $m$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$; $x_i \ne y_i$)Β β the favorite types of food of the $i$-th friend.
-----Output-----
If Lee can survive the dinner then print ALIVE (case insensitive), otherwise print DEAD (case insensitive).
Also, if he can survive the dinner, print the order Lee should call friends. If there are multiple valid orders, print any of them.
-----Examples-----
Input
3 3
1 2 1
1 2
2 3
1 3
Output
ALIVE
3 2 1
Input
3 2
1 1 0
1 2
1 3
Output
ALIVE
2 1
Input
4 4
1 2 0 1
1 3
1 2
2 3
2 4
Output
ALIVE
1 3 2 4
Input
5 5
1 1 1 2 1
3 4
1 2
2 3
4 5
4 5
Output
ALIVE
5 4 1 3 2
Input
4 10
2 4 1 4
3 2
4 2
4 1
3 1
4 1
1 3
3 2
2 1
3 1
2 4
Output
DEAD
-----Note-----
In the first example, any of the following orders of friends are correct : $[1, 3, 2]$, $[3, 1, 2]$, $[2, 3, 1]$, $[3, 2, 1]$.
In the second example, Lee should call the second friend first (the friend will eat a plate of food $1$) and then call the first friend (the friend will eat a plate of food $2$). If he calls the first friend sooner than the second one, then the first friend will eat one plate of food $1$ and food $2$ and there will be no food left for the second friend to eat. | n, m = list(map(int, input().split()))
w = list(map(int, input().split()))
w2guest = {}
for i in range(n):
w2guest[i] = set()
guests = []
for i in range(m):
a, b = list(map(int, input().split()))
guests.append((a - 1, b - 1))
w2guest[a - 1].add(i)
w2guest[b - 1].add(i)
ans = []
q = []
visited = set()
in_work = set()
for i in range(n):
if len(w2guest[i]) == 0:
visited.add(i)
elif len(w2guest[i]) <= w[i]:
q.append(i)
in_work.add(i)
while len(visited) != n and len(q) > 0:
a = q.pop()
gs = w2guest[a]
ans.extend(gs)
visited.add(a)
for g in gs:
i1 = guests[g][0]
i2 = guests[g][1]
if i1 != a:
w2guest[i1].remove(g)
if len(w2guest[i1]) == 0:
visited.add(i1)
elif len(w2guest[i1]) <= w[i1] and i1 not in in_work:
in_work.add(i1)
q.append(i1)
if i2 != a:
w2guest[i2].remove(g)
if len(w2guest[i2]) == 0:
visited.add(i2)
elif len(w2guest[i2]) <= w[i2] and i2 not in in_work:
in_work.add(i2)
q.append(i2)
if len(visited) != n:
print("DEAD")
else:
print("ALIVE")
print(*reversed([(x + 1) for x in ans])) | 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 DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR |
Lee bought some food for dinner time, but Lee's friends eat dinner in a deadly way. Lee is so scared, he doesn't want to die, at least not before seeing Online IOI 2020...
There are $n$ different types of food and $m$ Lee's best friends. Lee has $w_i$ plates of the $i$-th type of food and each friend has two different favorite types of food: the $i$-th friend's favorite types of food are $x_i$ and $y_i$ ($x_i \ne y_i$).
Lee will start calling his friends one by one. Whoever is called will go to the kitchen and will try to eat one plate of each of his favorite food types. Each of the friends will go to the kitchen exactly once.
The only problem is the following: if a friend will eat at least one plate of food (in total) then he will be harmless. But if there is nothing left for him to eat (neither $x_i$ nor $y_i$), he will eat Lee instead $\times\_\times$.
Lee can choose the order of friends to call, so he'd like to determine if he can survive dinner or not. Also, he'd like to know the order itself.
-----Input-----
The first line contains two integers $n$ and $m$ ($2 \le n \le 10^5$; $1 \le m \le 2 \cdot 10^5$)Β β the number of different food types and the number of Lee's friends.
The second line contains $n$ integers $w_1, w_2, \ldots, w_n$ ($0 \le w_i \le 10^6$)Β β the number of plates of each food type.
The $i$-th line of the next $m$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$; $x_i \ne y_i$)Β β the favorite types of food of the $i$-th friend.
-----Output-----
If Lee can survive the dinner then print ALIVE (case insensitive), otherwise print DEAD (case insensitive).
Also, if he can survive the dinner, print the order Lee should call friends. If there are multiple valid orders, print any of them.
-----Examples-----
Input
3 3
1 2 1
1 2
2 3
1 3
Output
ALIVE
3 2 1
Input
3 2
1 1 0
1 2
1 3
Output
ALIVE
2 1
Input
4 4
1 2 0 1
1 3
1 2
2 3
2 4
Output
ALIVE
1 3 2 4
Input
5 5
1 1 1 2 1
3 4
1 2
2 3
4 5
4 5
Output
ALIVE
5 4 1 3 2
Input
4 10
2 4 1 4
3 2
4 2
4 1
3 1
4 1
1 3
3 2
2 1
3 1
2 4
Output
DEAD
-----Note-----
In the first example, any of the following orders of friends are correct : $[1, 3, 2]$, $[3, 1, 2]$, $[2, 3, 1]$, $[3, 2, 1]$.
In the second example, Lee should call the second friend first (the friend will eat a plate of food $1$) and then call the first friend (the friend will eat a plate of food $2$). If he calls the first friend sooner than the second one, then the first friend will eat one plate of food $1$ and food $2$ and there will be no food left for the second friend to eat. | import sys
def input():
return sys.stdin.readline().rstrip()
def input_split():
return [int(i) for i in input().split()]
prefs = []
n, m = input_split()
desired = [(0) for i in range(n)]
eaters = [[] for i in range(n)]
w = input_split()
for friend in range(m):
x, y = input_split()
x -= 1
y -= 1
prefs.append((x, y))
desired[x] += 1
desired[y] += 1
eaters[x].append(friend)
eaters[y].append(friend)
isSafe = [(False) for i in range(n)]
safes = []
for i in range(n):
if w[i] >= desired[i]:
safes.append(i)
isSafe[i] = True
order = []
included = set()
while True:
new_safes = []
for dish in safes:
for friend in eaters[dish]:
if friend not in included:
included.add(friend)
order.append(friend)
p1, p2 = prefs[friend]
if not isSafe[p1] or not isSafe[p2]:
if not isSafe[p1]:
desired[p1] -= 1
if w[p1] >= desired[p1]:
isSafe[p1] = True
new_safes.append(p1)
else:
desired[p2] -= 1
if w[p2] >= desired[p2]:
isSafe[p2] = True
new_safes.append(p2)
if len(new_safes) == 0:
break
else:
safes = new_safes
if len(order) == m:
order.reverse()
order = [(i + 1) for i in order]
print("ALIVE")
print(*order, sep=" ")
else:
print("DEAD") | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR 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 VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR WHILE NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING |
Lee bought some food for dinner time, but Lee's friends eat dinner in a deadly way. Lee is so scared, he doesn't want to die, at least not before seeing Online IOI 2020...
There are $n$ different types of food and $m$ Lee's best friends. Lee has $w_i$ plates of the $i$-th type of food and each friend has two different favorite types of food: the $i$-th friend's favorite types of food are $x_i$ and $y_i$ ($x_i \ne y_i$).
Lee will start calling his friends one by one. Whoever is called will go to the kitchen and will try to eat one plate of each of his favorite food types. Each of the friends will go to the kitchen exactly once.
The only problem is the following: if a friend will eat at least one plate of food (in total) then he will be harmless. But if there is nothing left for him to eat (neither $x_i$ nor $y_i$), he will eat Lee instead $\times\_\times$.
Lee can choose the order of friends to call, so he'd like to determine if he can survive dinner or not. Also, he'd like to know the order itself.
-----Input-----
The first line contains two integers $n$ and $m$ ($2 \le n \le 10^5$; $1 \le m \le 2 \cdot 10^5$)Β β the number of different food types and the number of Lee's friends.
The second line contains $n$ integers $w_1, w_2, \ldots, w_n$ ($0 \le w_i \le 10^6$)Β β the number of plates of each food type.
The $i$-th line of the next $m$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$; $x_i \ne y_i$)Β β the favorite types of food of the $i$-th friend.
-----Output-----
If Lee can survive the dinner then print ALIVE (case insensitive), otherwise print DEAD (case insensitive).
Also, if he can survive the dinner, print the order Lee should call friends. If there are multiple valid orders, print any of them.
-----Examples-----
Input
3 3
1 2 1
1 2
2 3
1 3
Output
ALIVE
3 2 1
Input
3 2
1 1 0
1 2
1 3
Output
ALIVE
2 1
Input
4 4
1 2 0 1
1 3
1 2
2 3
2 4
Output
ALIVE
1 3 2 4
Input
5 5
1 1 1 2 1
3 4
1 2
2 3
4 5
4 5
Output
ALIVE
5 4 1 3 2
Input
4 10
2 4 1 4
3 2
4 2
4 1
3 1
4 1
1 3
3 2
2 1
3 1
2 4
Output
DEAD
-----Note-----
In the first example, any of the following orders of friends are correct : $[1, 3, 2]$, $[3, 1, 2]$, $[2, 3, 1]$, $[3, 2, 1]$.
In the second example, Lee should call the second friend first (the friend will eat a plate of food $1$) and then call the first friend (the friend will eat a plate of food $2$). If he calls the first friend sooner than the second one, then the first friend will eat one plate of food $1$ and food $2$ and there will be no food left for the second friend to eat. | import sys
def xmax(x, y):
if x[1] > y[1]:
return x
return y
class SegTree:
def __init__(self, init_val, n, ide_ele, seg_func):
self.segfunc = seg_func
self.num = 2 ** (n - 1).bit_length()
self.ide_ele = ide_ele
self.seg = [self.ide_ele] * 2 * self.num
for i in range(n):
self.seg[i + self.num - 1] = init_val[i]
for i in range(self.num - 2, -1, -1):
self.seg[i] = self.segfunc(self.seg[2 * i + 1], self.seg[2 * i + 2])
def update(self, k, x):
ll = k
k += self.num - 1
self.seg[k] = ll, self.seg[k][1] + x
while k + 1:
k = (k - 1) // 2
self.seg[k] = self.segfunc(self.seg[k * 2 + 1], self.seg[k * 2 + 2])
def update2(self, k, x):
k += self.num - 1
self.seg[k] = x
while k + 1:
k = (k - 1) // 2
self.seg[k] = self.segfunc(self.seg[k * 2 + 1], self.seg[k * 2 + 2])
def query(self, p, q):
if q <= p:
return self.ide_ele
p += self.num - 1
q += self.num - 2
res = self.ide_ele
while q - p > 1:
if p & 1 == 0:
res = self.segfunc(res, self.seg[p])
if q & 1 == 1:
res = self.segfunc(res, self.seg[q])
q -= 1
p = p // 2
q = (q - 1) // 2
if p == q:
res = self.segfunc(res, self.seg[p])
else:
res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q])
return res
input = sys.stdin.readline
N, M = map(int, input().split())
X = list(map(int, input().split()))
sts = [[] for _ in range(N)]
for i in range(1, M + 1):
a, b = map(int, input().split())
sts[a - 1].append((i, b - 1))
sts[b - 1].append((i, a - 1))
X[a - 1] -= 1
X[b - 1] -= 1
minf = -(10**18) - 1
ss = SegTree([(i, x) for i, x in enumerate(X)], N, (-1, minf), xmax)
f = False
R = []
vs = set()
while True:
j, mx = ss.query(0, N)
if mx < 0:
f = True
break
while sts[j]:
i, co = sts[j].pop()
if i in vs:
continue
vs.add(i)
ss.update(co, 1)
R.append(i)
if len(R) == M:
break
ss.update2(j, (j, minf))
if f or len(R) != M:
print("DEAD")
else:
print("ALIVE")
print(*R[::-1]) | IMPORT FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER VAR WHILE BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR WHILE NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER |
You are given a rooted tree, consisting of $n$ vertices. The vertices are numbered from $1$ to $n$, the root is the vertex $1$.
You can perform the following operation at most $k$ times:
choose an edge $(v, u)$ of the tree such that $v$ is a parent of $u$;
remove the edge $(v, u)$;
add an edge $(1, u)$ (i. e. make $u$ with its subtree a child of the root).
The height of a tree is the maximum depth of its vertices, and the depth of a vertex is the number of edges on the path from the root to it. For example, the depth of vertex $1$ is $0$, since it's the root, and the depth of all its children is $1$.
What's the smallest height of the tree that can be achieved?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of testcases.
The first line of each testcase contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $0 \le k \le n - 1$) β the number of vertices in the tree and the maximum number of operations you can perform.
The second line contains $n-1$ integers $p_2, p_3, \dots, p_n$ ($1 \le p_i < i$) β the parent of the $i$-th vertex. Vertex $1$ is the root.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print a single integer β the smallest height of the tree that can achieved by performing at most $k$ operations.
-----Examples-----
Input
5
5 1
1 1 2 2
5 2
1 1 2 2
6 0
1 2 3 4 5
6 1
1 2 3 4 5
4 3
1 1 1
Output
2
1
5
3
1
-----Note-----
None | def check(pars, dep, k):
_oper = 0
depths = [(1) for i in range(len(pars))]
for i in range(len(pars) - 1, 0, -1):
if depths[i] == dep and pars[i] != 0:
_oper += 1
else:
par = pars[i]
depths[par] = max(depths[par], depths[i] + 1)
return _oper <= k
t = int(input())
for _ in range(t):
n, k = input().split()
n, k = int(n), int(k)
pars = [0]
pars_temp = input().split()
pars += [(int(i) - 1) for i in pars_temp]
low, high = 1, n - 1
while low < high:
mid = (low + high) // 2
if check(pars, mid, k):
high = mid - 1
else:
low = mid + 1
if high >= 1 and check(pars, high, k):
print(high)
else:
print(high + 1) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given a rooted tree, consisting of $n$ vertices. The vertices are numbered from $1$ to $n$, the root is the vertex $1$.
You can perform the following operation at most $k$ times:
choose an edge $(v, u)$ of the tree such that $v$ is a parent of $u$;
remove the edge $(v, u)$;
add an edge $(1, u)$ (i. e. make $u$ with its subtree a child of the root).
The height of a tree is the maximum depth of its vertices, and the depth of a vertex is the number of edges on the path from the root to it. For example, the depth of vertex $1$ is $0$, since it's the root, and the depth of all its children is $1$.
What's the smallest height of the tree that can be achieved?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of testcases.
The first line of each testcase contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $0 \le k \le n - 1$) β the number of vertices in the tree and the maximum number of operations you can perform.
The second line contains $n-1$ integers $p_2, p_3, \dots, p_n$ ($1 \le p_i < i$) β the parent of the $i$-th vertex. Vertex $1$ is the root.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print a single integer β the smallest height of the tree that can achieved by performing at most $k$ operations.
-----Examples-----
Input
5
5 1
1 1 2 2
5 2
1 1 2 2
6 0
1 2 3 4 5
6 1
1 2 3 4 5
4 3
1 1 1
Output
2
1
5
3
1
-----Note-----
None | import sys
input = sys.stdin.readline
def solve():
n, k = map(int, input().split())
P = list(map(int, input().split()))
child = [[] for _ in range(n + 1)]
par = [-1] * (n + 1)
for i in range(1, n):
child[P[i - 1]].append(i + 1)
par[i + 1] = P[i - 1]
stack = [(0, 1)]
depth = [0] * (n + 1)
while stack:
d, node = stack.pop()
depth[node] = d
for ch in child[node]:
stack.append((d + 1, ch))
D = sorted([(v, i + 1) for i, v in enumerate(depth[1:])])
lo, hi = 1, n - 1
while lo < hi:
mid = (lo + hi) // 2
cnt = 0
vis = set()
i = n - 1
while i >= 0 and cnt <= k:
d, idx = D[i]
if d <= mid or idx in vis:
i -= 1
continue
curr = 0
while idx > 0 and curr < mid - 1:
idx = par[idx]
curr += 1
stack = [idx]
vis.add(idx)
while stack:
node = stack.pop()
for ch in child[node]:
if ch not in vis:
vis.add(ch)
stack.append(ch)
cnt += 1
i -= 1
if cnt <= k:
hi = mid
else:
lo = mid + 1
return hi
for _ in range(int(input())):
print(solve()) | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
You are given a rooted tree, consisting of $n$ vertices. The vertices are numbered from $1$ to $n$, the root is the vertex $1$.
You can perform the following operation at most $k$ times:
choose an edge $(v, u)$ of the tree such that $v$ is a parent of $u$;
remove the edge $(v, u)$;
add an edge $(1, u)$ (i. e. make $u$ with its subtree a child of the root).
The height of a tree is the maximum depth of its vertices, and the depth of a vertex is the number of edges on the path from the root to it. For example, the depth of vertex $1$ is $0$, since it's the root, and the depth of all its children is $1$.
What's the smallest height of the tree that can be achieved?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of testcases.
The first line of each testcase contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $0 \le k \le n - 1$) β the number of vertices in the tree and the maximum number of operations you can perform.
The second line contains $n-1$ integers $p_2, p_3, \dots, p_n$ ($1 \le p_i < i$) β the parent of the $i$-th vertex. Vertex $1$ is the root.
The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print a single integer β the smallest height of the tree that can achieved by performing at most $k$ operations.
-----Examples-----
Input
5
5 1
1 1 2 2
5 2
1 1 2 2
6 0
1 2 3 4 5
6 1
1 2 3 4 5
4 3
1 1 1
Output
2
1
5
3
1
-----Note-----
None | for _ in range(int(input())):
q, k = map(int, input().split())
arr = [-1, 0] + [int(i) for i in input().split()]
l, r = 1, q - 1
while l < r:
mid = (l + r) // 2
tot = 0
h = [0] * (q + 1)
for i in range(q, 1, -1):
if h[i] == mid - 1 and arr[i] != 1:
tot += 1
else:
h[arr[i]] = max(h[arr[i]], h[i] + 1)
if tot <= k:
r = mid
else:
l = mid + 1
print(l) | 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 NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
A positive number $x$ of length $n$ in base $p$ ($2 \le p \le 10^9$) is written on the blackboard. The number $x$ is given as a sequence $a_1, a_2, \dots, a_n$ ($0 \le a_i < p$) β the digits of $x$ in order from left to right (most significant to least significant).
Dmitry is very fond of all the digits of this number system, so he wants to see each of them at least once.
In one operation, he can:
take any number $x$ written on the board, increase it by $1$, and write the new value $x + 1$ on the board.
For example, $p=5$ and $x=234_5$.
Initially, the board contains the digits $2$, $3$ and $4$;
Dmitry increases the number $234_5$ by $1$ and writes down the number $240_5$. On the board there are digits $0, 2, 3, 4$;
Dmitry increases the number $240_5$ by $1$ and writes down the number $241_5$. Now the board contains all the digits from $0$ to $4$.
Your task is to determine the minimum number of operations required to make all the digits from $0$ to $p-1$ appear on the board at least once.
-----Input-----
The first line of the input contains a single integer $t$ ($1 \le t \le 2 \cdot 10^3$) β the number of test cases. The descriptions of the input test cases follow.
The first line of description of each test case contains two integers $n$ ($1 \le n \le 100$) and $p$ ($2 \le p \le 10^9$) β the length of the number and the base of the number system.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < p$) β digits of $x$ in number system with base $p$
It is guaranteed that the number $x$ does not contain leading zeros (that is, $a_1>0$).
-----Output-----
For each test case print a single integer β the minimum number of operations required for Dmitry to get all the digits on the board from $0$ to $p-1$.
It can be shown that this always requires a finite number of operations.
-----Examples-----
Input
11
2 3
1 2
4 2
1 1 1 1
6 6
1 2 3 4 5 0
5 2
1 0 1 0 1
3 10
1 2 3
5 1000
4 1 3 2 5
3 5
2 3 4
4 4
3 2 3 0
1 3
2
5 5
1 2 2 2 4
3 4
1 0 1
Output
1
1
0
0
7
995
2
1
1
1
2
-----Note-----
None | testcase = int(input())
for _ in range(testcase):
n, p = [int(num) for num in input().split()]
arr = [int(num) for num in input().split()]
arrs = set(arr)
arr.insert(0, 0)
first = arr[-1]
for i in range(n - 1, -1, -1):
if arr[i] == p - 1:
continue
comp = arr[i] + 1
break
tt = comp in arrs
i = (first - 1 + p) % p
while i in arrs and i != first:
i = (i - 1 + p) % p
if i == first:
print(0)
continue
elif i > first or tt:
ans = (i - first + p) % p
else:
arrs.add(comp)
i = (first - 1 + p) % p
while i in arrs and i != first:
i = (i - 1 + p) % p
ans = max((p - first) % p, (i - first + p) % p)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
A positive number $x$ of length $n$ in base $p$ ($2 \le p \le 10^9$) is written on the blackboard. The number $x$ is given as a sequence $a_1, a_2, \dots, a_n$ ($0 \le a_i < p$) β the digits of $x$ in order from left to right (most significant to least significant).
Dmitry is very fond of all the digits of this number system, so he wants to see each of them at least once.
In one operation, he can:
take any number $x$ written on the board, increase it by $1$, and write the new value $x + 1$ on the board.
For example, $p=5$ and $x=234_5$.
Initially, the board contains the digits $2$, $3$ and $4$;
Dmitry increases the number $234_5$ by $1$ and writes down the number $240_5$. On the board there are digits $0, 2, 3, 4$;
Dmitry increases the number $240_5$ by $1$ and writes down the number $241_5$. Now the board contains all the digits from $0$ to $4$.
Your task is to determine the minimum number of operations required to make all the digits from $0$ to $p-1$ appear on the board at least once.
-----Input-----
The first line of the input contains a single integer $t$ ($1 \le t \le 2 \cdot 10^3$) β the number of test cases. The descriptions of the input test cases follow.
The first line of description of each test case contains two integers $n$ ($1 \le n \le 100$) and $p$ ($2 \le p \le 10^9$) β the length of the number and the base of the number system.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < p$) β digits of $x$ in number system with base $p$
It is guaranteed that the number $x$ does not contain leading zeros (that is, $a_1>0$).
-----Output-----
For each test case print a single integer β the minimum number of operations required for Dmitry to get all the digits on the board from $0$ to $p-1$.
It can be shown that this always requires a finite number of operations.
-----Examples-----
Input
11
2 3
1 2
4 2
1 1 1 1
6 6
1 2 3 4 5 0
5 2
1 0 1 0 1
3 10
1 2 3
5 1000
4 1 3 2 5
3 5
2 3 4
4 4
3 2 3 0
1 3
2
5 5
1 2 2 2 4
3 4
1 0 1
Output
1
1
0
0
7
995
2
1
1
1
2
-----Note-----
None | t = int(input(""))
C = [0] * t
for i in range(t):
LRX = input("").split(" ")
n = int(LRX[0])
p = int(LRX[1])
A = [int(x) for x in input("").split(" ")]
if n == 1:
if A[0] == 2:
C[i] = p - 2
else:
C[i] = p - 1
continue
qq = n - 2
while qq >= 0 and A[qq] == p - 1:
qq -= 1
if qq >= 0:
sp = (A[qq] + 1) % p
else:
sp = 1
S = set(A)
q = (A[-1] - 1) % p
v = False
while q in S:
q = (q - 1) % p
if q == A[-1]:
v = True
break
if v:
continue
if q == 0:
C[i] = p - A[-1]
elif q < A[-1] and q == sp:
w = False
q = (q - 1) % p
while q in S:
q = (q - 1) % p
if q == p - 1:
w = True
break
if w:
C[i] = p - A[-1]
else:
C[i] = p + q - A[-1]
elif q < A[-1]:
C[i] = p + q - A[-1]
else:
C[i] = q - A[-1]
for j in C:
print(j) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING STRING IF VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR |
A positive number $x$ of length $n$ in base $p$ ($2 \le p \le 10^9$) is written on the blackboard. The number $x$ is given as a sequence $a_1, a_2, \dots, a_n$ ($0 \le a_i < p$) β the digits of $x$ in order from left to right (most significant to least significant).
Dmitry is very fond of all the digits of this number system, so he wants to see each of them at least once.
In one operation, he can:
take any number $x$ written on the board, increase it by $1$, and write the new value $x + 1$ on the board.
For example, $p=5$ and $x=234_5$.
Initially, the board contains the digits $2$, $3$ and $4$;
Dmitry increases the number $234_5$ by $1$ and writes down the number $240_5$. On the board there are digits $0, 2, 3, 4$;
Dmitry increases the number $240_5$ by $1$ and writes down the number $241_5$. Now the board contains all the digits from $0$ to $4$.
Your task is to determine the minimum number of operations required to make all the digits from $0$ to $p-1$ appear on the board at least once.
-----Input-----
The first line of the input contains a single integer $t$ ($1 \le t \le 2 \cdot 10^3$) β the number of test cases. The descriptions of the input test cases follow.
The first line of description of each test case contains two integers $n$ ($1 \le n \le 100$) and $p$ ($2 \le p \le 10^9$) β the length of the number and the base of the number system.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < p$) β digits of $x$ in number system with base $p$
It is guaranteed that the number $x$ does not contain leading zeros (that is, $a_1>0$).
-----Output-----
For each test case print a single integer β the minimum number of operations required for Dmitry to get all the digits on the board from $0$ to $p-1$.
It can be shown that this always requires a finite number of operations.
-----Examples-----
Input
11
2 3
1 2
4 2
1 1 1 1
6 6
1 2 3 4 5 0
5 2
1 0 1 0 1
3 10
1 2 3
5 1000
4 1 3 2 5
3 5
2 3 4
4 4
3 2 3 0
1 3
2
5 5
1 2 2 2 4
3 4
1 0 1
Output
1
1
0
0
7
995
2
1
1
1
2
-----Note-----
None | from sys import stdin, stdout
t = int(stdin.readline().strip())
def addWithCarry(digits, base):
carry = 1
result = set(digits)
result.add(0)
for i in range(len(digits) - 2, -1, -1):
curr = digits[i] + carry
result.add(curr % base)
carry = curr // base
while carry:
result.add(carry % base)
carry //= base
return result
for test in range(t):
n, p = map(int, stdin.readline().split())
digits = list(map(int, stdin.readline().split()))
upper_bound = p
ss = set(digits)
answer = 0
while upper_bound - 1 in ss:
upper_bound -= 1
if upper_bound == 0:
print(0)
continue
if upper_bound > digits[-1]:
answer += upper_bound - digits[-1] - 1
upper_bound, digits[-1] = digits[-1], upper_bound - 1
while upper_bound - 1 in ss:
upper_bound -= 1
if upper_bound == 0:
print(answer)
continue
answer += p - digits[-1]
ss = addWithCarry(digits, p)
while upper_bound - 1 in ss:
upper_bound -= 1
if upper_bound == 0:
print(answer)
continue
answer += upper_bound - 1
print(answer) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN 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 VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
A positive number $x$ of length $n$ in base $p$ ($2 \le p \le 10^9$) is written on the blackboard. The number $x$ is given as a sequence $a_1, a_2, \dots, a_n$ ($0 \le a_i < p$) β the digits of $x$ in order from left to right (most significant to least significant).
Dmitry is very fond of all the digits of this number system, so he wants to see each of them at least once.
In one operation, he can:
take any number $x$ written on the board, increase it by $1$, and write the new value $x + 1$ on the board.
For example, $p=5$ and $x=234_5$.
Initially, the board contains the digits $2$, $3$ and $4$;
Dmitry increases the number $234_5$ by $1$ and writes down the number $240_5$. On the board there are digits $0, 2, 3, 4$;
Dmitry increases the number $240_5$ by $1$ and writes down the number $241_5$. Now the board contains all the digits from $0$ to $4$.
Your task is to determine the minimum number of operations required to make all the digits from $0$ to $p-1$ appear on the board at least once.
-----Input-----
The first line of the input contains a single integer $t$ ($1 \le t \le 2 \cdot 10^3$) β the number of test cases. The descriptions of the input test cases follow.
The first line of description of each test case contains two integers $n$ ($1 \le n \le 100$) and $p$ ($2 \le p \le 10^9$) β the length of the number and the base of the number system.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < p$) β digits of $x$ in number system with base $p$
It is guaranteed that the number $x$ does not contain leading zeros (that is, $a_1>0$).
-----Output-----
For each test case print a single integer β the minimum number of operations required for Dmitry to get all the digits on the board from $0$ to $p-1$.
It can be shown that this always requires a finite number of operations.
-----Examples-----
Input
11
2 3
1 2
4 2
1 1 1 1
6 6
1 2 3 4 5 0
5 2
1 0 1 0 1
3 10
1 2 3
5 1000
4 1 3 2 5
3 5
2 3 4
4 4
3 2 3 0
1 3
2
5 5
1 2 2 2 4
3 4
1 0 1
Output
1
1
0
0
7
995
2
1
1
1
2
-----Note-----
None | for t in range(int(input())):
n, p = map(int, input().split())
A = list(map(int, input().split()))
last = A[-1]
T = set(A)
if len(T) == p:
print(0)
continue
S = list(T)
S.sort()
if len(S) > last and S[last] == last:
cur = p - 1
while True:
if cur not in T:
print(cur - last)
break
cur -= 1
else:
for i in range(n - 2, -1, -1):
if A[i] != p - 1:
T.add(A[i] + 1)
break
else:
T.add(1)
if len(T) == p:
print(p - last)
continue
cur = last - 1
while True:
if cur not in T:
print(max(p - last, p - last + cur))
break
cur -= 1 | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER |
A positive number $x$ of length $n$ in base $p$ ($2 \le p \le 10^9$) is written on the blackboard. The number $x$ is given as a sequence $a_1, a_2, \dots, a_n$ ($0 \le a_i < p$) β the digits of $x$ in order from left to right (most significant to least significant).
Dmitry is very fond of all the digits of this number system, so he wants to see each of them at least once.
In one operation, he can:
take any number $x$ written on the board, increase it by $1$, and write the new value $x + 1$ on the board.
For example, $p=5$ and $x=234_5$.
Initially, the board contains the digits $2$, $3$ and $4$;
Dmitry increases the number $234_5$ by $1$ and writes down the number $240_5$. On the board there are digits $0, 2, 3, 4$;
Dmitry increases the number $240_5$ by $1$ and writes down the number $241_5$. Now the board contains all the digits from $0$ to $4$.
Your task is to determine the minimum number of operations required to make all the digits from $0$ to $p-1$ appear on the board at least once.
-----Input-----
The first line of the input contains a single integer $t$ ($1 \le t \le 2 \cdot 10^3$) β the number of test cases. The descriptions of the input test cases follow.
The first line of description of each test case contains two integers $n$ ($1 \le n \le 100$) and $p$ ($2 \le p \le 10^9$) β the length of the number and the base of the number system.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < p$) β digits of $x$ in number system with base $p$
It is guaranteed that the number $x$ does not contain leading zeros (that is, $a_1>0$).
-----Output-----
For each test case print a single integer β the minimum number of operations required for Dmitry to get all the digits on the board from $0$ to $p-1$.
It can be shown that this always requires a finite number of operations.
-----Examples-----
Input
11
2 3
1 2
4 2
1 1 1 1
6 6
1 2 3 4 5 0
5 2
1 0 1 0 1
3 10
1 2 3
5 1000
4 1 3 2 5
3 5
2 3 4
4 4
3 2 3 0
1 3
2
5 5
1 2 2 2 4
3 4
1 0 1
Output
1
1
0
0
7
995
2
1
1
1
2
-----Note-----
None | t = int(input())
for _ in range(t):
n, p = map(int, input().split())
a = list(map(int, input().split()))
a.reverse()
i = 1
while i < n and a[i] == p - 1:
i += 1
loopx = a[i] + 1 if i < n else 1
free = set(a)
fin = p - 1
cur = (a[0] - 1) % p
while cur in free:
fin -= 1
if fin == 0:
break
cur = (cur - 1) % p
free.add(loopx)
free.add(0)
cur2 = (a[0] - 1) % p
fin2 = p - 1
while cur2 in free:
fin2 -= 1
if fin2 == 0:
break
cur2 = (cur2 - 1) % p
fin = min(fin, max(fin2, p - a[0]))
print(fin) | 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 EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR WHILE VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
A positive number $x$ of length $n$ in base $p$ ($2 \le p \le 10^9$) is written on the blackboard. The number $x$ is given as a sequence $a_1, a_2, \dots, a_n$ ($0 \le a_i < p$) β the digits of $x$ in order from left to right (most significant to least significant).
Dmitry is very fond of all the digits of this number system, so he wants to see each of them at least once.
In one operation, he can:
take any number $x$ written on the board, increase it by $1$, and write the new value $x + 1$ on the board.
For example, $p=5$ and $x=234_5$.
Initially, the board contains the digits $2$, $3$ and $4$;
Dmitry increases the number $234_5$ by $1$ and writes down the number $240_5$. On the board there are digits $0, 2, 3, 4$;
Dmitry increases the number $240_5$ by $1$ and writes down the number $241_5$. Now the board contains all the digits from $0$ to $4$.
Your task is to determine the minimum number of operations required to make all the digits from $0$ to $p-1$ appear on the board at least once.
-----Input-----
The first line of the input contains a single integer $t$ ($1 \le t \le 2 \cdot 10^3$) β the number of test cases. The descriptions of the input test cases follow.
The first line of description of each test case contains two integers $n$ ($1 \le n \le 100$) and $p$ ($2 \le p \le 10^9$) β the length of the number and the base of the number system.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < p$) β digits of $x$ in number system with base $p$
It is guaranteed that the number $x$ does not contain leading zeros (that is, $a_1>0$).
-----Output-----
For each test case print a single integer β the minimum number of operations required for Dmitry to get all the digits on the board from $0$ to $p-1$.
It can be shown that this always requires a finite number of operations.
-----Examples-----
Input
11
2 3
1 2
4 2
1 1 1 1
6 6
1 2 3 4 5 0
5 2
1 0 1 0 1
3 10
1 2 3
5 1000
4 1 3 2 5
3 5
2 3 4
4 4
3 2 3 0
1 3
2
5 5
1 2 2 2 4
3 4
1 0 1
Output
1
1
0
0
7
995
2
1
1
1
2
-----Note-----
None | def solve():
n, p = map(int, input().split())
digits = list(map(int, input().split()))
digit_set = set(digits)
digit_sorted = sorted(list(digit_set))
last = digits[-1]
lift = False
for i in range(len(digit_set)):
if i == last:
break
if i not in digit_set:
lift = True
else:
lift = True
if lift:
for i in range(n - 2, -1, -1):
if digits[i] + 1 != p:
digit_set.add(digits[i] + 1)
break
else:
digit_set.add(1)
digit_sorted = sorted(list(digit_set))
out = p - last
for i in range(len(digit_set) + 1):
if last - i == 0:
break
if last - i not in digit_set:
out += last - i
break
return out
else:
maxp = last
for ith in range(1, len(digit_sorted) + 1):
i = len(digit_sorted) - ith
if digit_sorted[i] != p - ith:
maxp = p - ith
break
return maxp - last
for _ in range(int(input())):
print(solve()) | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
Polycarp found $n$ segments on the street. A segment with the index $i$ is described by two integers $l_i$ and $r_i$ β coordinates of the beginning and end of the segment, respectively. Polycarp realized that he didn't need all the segments, so he wanted to delete some of them.
Polycarp believes that a set of $k$ segments is good if there is a segment $[l_i, r_i]$ ($1 \leq i \leq k$) from the set, such that it intersects every segment from the set (the intersection must be a point or segment). For example, a set of $3$ segments $[[1, 4], [2, 3], [3, 6]]$ is good, since the segment $[2, 3]$ intersects each segment from the set. Set of $4$ segments $[[1, 2], [2, 3], [3, 5], [4, 5]]$ is not good.
Polycarp wonders, what is the minimum number of segments he has to delete so that the remaining segments form a good set?
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^5$) β number of test cases. Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of segments. This is followed by $n$ lines describing the segments.
Each segment is described by two integers $l$ and $r$ ($1 \leq l \leq r \leq 10^9$) β coordinates of the beginning and end of the segment, respectively.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the minimum number of segments that need to be deleted in order for the set of remaining segments to become good.
-----Examples-----
Input
4
3
1 4
2 3
3 6
4
1 2
2 3
3 5
4 5
5
1 2
3 8
4 5
6 7
9 10
5
1 5
2 4
3 5
3 8
4 8
Output
0
1
2
0
-----Note-----
None | from sys import stdin, stdout
input = stdin.readline
def output(answer):
stdout.write(str(answer) + "\n")
def binarySearchfor(tp, target):
low, high = 0, n - 1
while low < high:
mid = low + (high - low + 1) // 2
if tp[mid][0] <= target:
low = mid
else:
high = mid - 1
return low
for _ in range(int(input())):
n = int(input())
tp = []
search = []
for i in range(n):
x, y = map(int, input().split())
tp.append((x, 1))
tp.append((y, -1))
search.append((x, y))
search.sort(key=lambda x: (x[0], x[1]))
tp.sort(key=lambda x: (x[0], -x[1]))
index = -1
curr = 0
mx = 1
for x, d in tp:
curr += d
index += max(d, 0)
if d == 1:
ind = binarySearchfor(search, search[index][1])
mx = max(curr + (ind - index), mx)
output(n - mx) | ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Polycarp found $n$ segments on the street. A segment with the index $i$ is described by two integers $l_i$ and $r_i$ β coordinates of the beginning and end of the segment, respectively. Polycarp realized that he didn't need all the segments, so he wanted to delete some of them.
Polycarp believes that a set of $k$ segments is good if there is a segment $[l_i, r_i]$ ($1 \leq i \leq k$) from the set, such that it intersects every segment from the set (the intersection must be a point or segment). For example, a set of $3$ segments $[[1, 4], [2, 3], [3, 6]]$ is good, since the segment $[2, 3]$ intersects each segment from the set. Set of $4$ segments $[[1, 2], [2, 3], [3, 5], [4, 5]]$ is not good.
Polycarp wonders, what is the minimum number of segments he has to delete so that the remaining segments form a good set?
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^5$) β number of test cases. Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of segments. This is followed by $n$ lines describing the segments.
Each segment is described by two integers $l$ and $r$ ($1 \leq l \leq r \leq 10^9$) β coordinates of the beginning and end of the segment, respectively.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the minimum number of segments that need to be deleted in order for the set of remaining segments to become good.
-----Examples-----
Input
4
3
1 4
2 3
3 6
4
1 2
2 3
3 5
4 5
5
1 2
3 8
4 5
6 7
9 10
5
1 5
2 4
3 5
3 8
4 8
Output
0
1
2
0
-----Note-----
None | import sys
input = sys.stdin.buffer.readline
def multiLineArrayPrint(arr):
print("\n".join([str(x) for x in arr]))
allAns = []
t = int(input())
for _ in range(t):
n = int(input())
sortedByL = []
sortedByR = []
lr = []
for __ in range(n):
l, r = [int(x) for x in input().split()]
sortedByL.append(l)
sortedByR.append(r)
lr.append([l, r])
sortedByL.sort()
sortedByR.sort()
ans = n
for l, r in lr:
deleteCnt = 0
i = -1
b = n
while b > 0:
while i + b < n and sortedByR[i + b] < l:
i += b
b //= 2
deleteCnt += i + 1
i = n
b = n
while b > 0:
while i - b >= 0 and sortedByL[i - b] > r:
i -= b
b //= 2
deleteCnt += n - i
ans = min(ans, deleteCnt)
allAns.append(ans)
multiLineArrayPrint(allAns) | IMPORT ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Polycarp found $n$ segments on the street. A segment with the index $i$ is described by two integers $l_i$ and $r_i$ β coordinates of the beginning and end of the segment, respectively. Polycarp realized that he didn't need all the segments, so he wanted to delete some of them.
Polycarp believes that a set of $k$ segments is good if there is a segment $[l_i, r_i]$ ($1 \leq i \leq k$) from the set, such that it intersects every segment from the set (the intersection must be a point or segment). For example, a set of $3$ segments $[[1, 4], [2, 3], [3, 6]]$ is good, since the segment $[2, 3]$ intersects each segment from the set. Set of $4$ segments $[[1, 2], [2, 3], [3, 5], [4, 5]]$ is not good.
Polycarp wonders, what is the minimum number of segments he has to delete so that the remaining segments form a good set?
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^5$) β number of test cases. Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of segments. This is followed by $n$ lines describing the segments.
Each segment is described by two integers $l$ and $r$ ($1 \leq l \leq r \leq 10^9$) β coordinates of the beginning and end of the segment, respectively.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the minimum number of segments that need to be deleted in order for the set of remaining segments to become good.
-----Examples-----
Input
4
3
1 4
2 3
3 6
4
1 2
2 3
3 5
4 5
5
1 2
3 8
4 5
6 7
9 10
5
1 5
2 4
3 5
3 8
4 8
Output
0
1
2
0
-----Note-----
None | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
a = []
b = []
mp = dict()
for i in range(n):
l, r = map(int, input().split())
a.append([l, r])
b.append(l)
b.append(r)
b.sort()
k = 1
for x in b:
if x not in mp:
mp[x] = k
k += 1
ct1 = [(0) for _ in range(k + 1)]
ct2 = [(0) for _ in range(k + 1)]
for l, r in a:
ct1[mp[r]] += 1
ct2[mp[l]] += 1
for i in range(2, k + 1):
ct1[i] += ct1[i - 1]
ct2[k - i] += ct2[k - i + 1]
ans = n
for l, r in a:
bad = ct1[mp[l] - 1] + ct2[mp[r] + 1]
ans = min(ans, bad)
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Polycarp found $n$ segments on the street. A segment with the index $i$ is described by two integers $l_i$ and $r_i$ β coordinates of the beginning and end of the segment, respectively. Polycarp realized that he didn't need all the segments, so he wanted to delete some of them.
Polycarp believes that a set of $k$ segments is good if there is a segment $[l_i, r_i]$ ($1 \leq i \leq k$) from the set, such that it intersects every segment from the set (the intersection must be a point or segment). For example, a set of $3$ segments $[[1, 4], [2, 3], [3, 6]]$ is good, since the segment $[2, 3]$ intersects each segment from the set. Set of $4$ segments $[[1, 2], [2, 3], [3, 5], [4, 5]]$ is not good.
Polycarp wonders, what is the minimum number of segments he has to delete so that the remaining segments form a good set?
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^5$) β number of test cases. Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of segments. This is followed by $n$ lines describing the segments.
Each segment is described by two integers $l$ and $r$ ($1 \leq l \leq r \leq 10^9$) β coordinates of the beginning and end of the segment, respectively.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the minimum number of segments that need to be deleted in order for the set of remaining segments to become good.
-----Examples-----
Input
4
3
1 4
2 3
3 6
4
1 2
2 3
3 5
4 5
5
1 2
3 8
4 5
6 7
9 10
5
1 5
2 4
3 5
3 8
4 8
Output
0
1
2
0
-----Note-----
None | from sys import stdin
t = int(stdin.readline())
for _ in range(t):
n = int(stdin.readline())
ls = []
rs = []
seg = set()
for i in range(n):
l, r = map(int, stdin.readline().split())
ls.append(l)
rs.append(r)
seg.add((l, r))
ls.sort()
rs.sort()
def fmaxi(k):
l1 = 0
r1 = n - 1
while l1 < r1:
mid = (l1 + r1) // 2
if ls[mid] > k:
r1 = mid - 1
else:
l1 = mid + 1
if ls[r1] <= k:
return r1
else:
return r1 - 1
def fmini(k):
l1 = 0
r1 = n - 1
while l1 < r1:
mid = (l1 + r1) // 2
if rs[mid] >= k:
r1 = mid - 1
else:
l1 = mid + 1
if rs[r1] >= k and r1 >= 0:
return r1 - 1
return r1
maxi = 0
for i in seg:
end = fmini(i[0]) + 1
start = fmaxi(i[1]) + 1
if start - end > maxi:
maxi = start - end
print(n - maxi) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR RETURN VAR RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Polycarp found $n$ segments on the street. A segment with the index $i$ is described by two integers $l_i$ and $r_i$ β coordinates of the beginning and end of the segment, respectively. Polycarp realized that he didn't need all the segments, so he wanted to delete some of them.
Polycarp believes that a set of $k$ segments is good if there is a segment $[l_i, r_i]$ ($1 \leq i \leq k$) from the set, such that it intersects every segment from the set (the intersection must be a point or segment). For example, a set of $3$ segments $[[1, 4], [2, 3], [3, 6]]$ is good, since the segment $[2, 3]$ intersects each segment from the set. Set of $4$ segments $[[1, 2], [2, 3], [3, 5], [4, 5]]$ is not good.
Polycarp wonders, what is the minimum number of segments he has to delete so that the remaining segments form a good set?
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^5$) β number of test cases. Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of segments. This is followed by $n$ lines describing the segments.
Each segment is described by two integers $l$ and $r$ ($1 \leq l \leq r \leq 10^9$) β coordinates of the beginning and end of the segment, respectively.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the minimum number of segments that need to be deleted in order for the set of remaining segments to become good.
-----Examples-----
Input
4
3
1 4
2 3
3 6
4
1 2
2 3
3 5
4 5
5
1 2
3 8
4 5
6 7
9 10
5
1 5
2 4
3 5
3 8
4 8
Output
0
1
2
0
-----Note-----
None | import sys
t = int(input())
for test in range(t):
n = int(input())
ans = [n] * n
arrl = [0] * n
arrr = [0] * n
for i in range(n):
arrl[i], arrr[i] = map(int, sys.stdin.readline().split())
vals = []
for i in range(n):
vals.append(arrl[i])
vals.append(arrr[i])
def lower_bound(vals, val):
low = int(0)
high = int(len(vals) - 1)
while low < high:
mid = int((low + high) / 2)
if vals[mid] < val:
low = mid + 1
else:
high = mid
return low
vals.sort()
ql = []
qr = []
for i in range(2 * n):
ql.append([])
qr.append([])
for i in range(n):
arrl[i] = lower_bound(vals, arrl[i])
arrr[i] = lower_bound(vals, arrr[i])
ql[arrl[i]].append((arrr[i], i))
qr[arrr[i]].append((arrl[i], i))
tr = [0] * (2 * n + 1)
def update(pos, val):
i = pos
while i <= 2 * n:
tr[i] += val
i += i & -i
def get_prefix(pos):
sum = 0
i = pos
while i > 0:
sum += tr[i]
i -= i & -i
return sum
def get_range(l, r):
if l > r:
return 0
else:
return get_prefix(r) - get_prefix(l - 1)
for key in range(2 * n):
for el in ql[key]:
ans[el[1]] -= get_prefix(key)
for el in ql[key]:
update(el[0] + 1, 1)
tr = [0] * (2 * n + 1)
for key in range(2 * n - 1, -1, -1):
for el in qr[key]:
ans[el[1]] -= get_range(key + 2, 2 * n)
for el in qr[key]:
update(el[0] + 1, 1)
maxans = 0
for el in ans:
maxans = max(maxans, el)
print(n - maxans) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR WHILE VAR BIN_OP NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Polycarp found $n$ segments on the street. A segment with the index $i$ is described by two integers $l_i$ and $r_i$ β coordinates of the beginning and end of the segment, respectively. Polycarp realized that he didn't need all the segments, so he wanted to delete some of them.
Polycarp believes that a set of $k$ segments is good if there is a segment $[l_i, r_i]$ ($1 \leq i \leq k$) from the set, such that it intersects every segment from the set (the intersection must be a point or segment). For example, a set of $3$ segments $[[1, 4], [2, 3], [3, 6]]$ is good, since the segment $[2, 3]$ intersects each segment from the set. Set of $4$ segments $[[1, 2], [2, 3], [3, 5], [4, 5]]$ is not good.
Polycarp wonders, what is the minimum number of segments he has to delete so that the remaining segments form a good set?
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^5$) β number of test cases. Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of segments. This is followed by $n$ lines describing the segments.
Each segment is described by two integers $l$ and $r$ ($1 \leq l \leq r \leq 10^9$) β coordinates of the beginning and end of the segment, respectively.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the minimum number of segments that need to be deleted in order for the set of remaining segments to become good.
-----Examples-----
Input
4
3
1 4
2 3
3 6
4
1 2
2 3
3 5
4 5
5
1 2
3 8
4 5
6 7
9 10
5
1 5
2 4
3 5
3 8
4 8
Output
0
1
2
0
-----Note-----
None | from sys import stdin, stdout
input = stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
arr = []
for i in range(n):
x, y = map(int, input().split())
arr.append((x, y))
arr = sorted(arr)
trr = sorted(arr, key=lambda x: (x[1], x[0]))
dict = {}
f = float("inf")
for i in arr:
s = i[0]
e = i[1]
l = 0
r = n - 1
ans1 = n
while l <= r:
m = (l + r) // 2
if arr[m][0] <= e:
l = m + 1
else:
ans1 = m
r = m - 1
l = 0
r = n - 1
ans2 = n
while l <= r:
m = (l + r) // 2
if trr[m][1] < s:
l = m + 1
else:
ans2 = m
r = m - 1
ans = ans1 - ans2
f = min(f, n - ans)
print(f) | ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Polycarp found $n$ segments on the street. A segment with the index $i$ is described by two integers $l_i$ and $r_i$ β coordinates of the beginning and end of the segment, respectively. Polycarp realized that he didn't need all the segments, so he wanted to delete some of them.
Polycarp believes that a set of $k$ segments is good if there is a segment $[l_i, r_i]$ ($1 \leq i \leq k$) from the set, such that it intersects every segment from the set (the intersection must be a point or segment). For example, a set of $3$ segments $[[1, 4], [2, 3], [3, 6]]$ is good, since the segment $[2, 3]$ intersects each segment from the set. Set of $4$ segments $[[1, 2], [2, 3], [3, 5], [4, 5]]$ is not good.
Polycarp wonders, what is the minimum number of segments he has to delete so that the remaining segments form a good set?
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^5$) β number of test cases. Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of segments. This is followed by $n$ lines describing the segments.
Each segment is described by two integers $l$ and $r$ ($1 \leq l \leq r \leq 10^9$) β coordinates of the beginning and end of the segment, respectively.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the minimum number of segments that need to be deleted in order for the set of remaining segments to become good.
-----Examples-----
Input
4
3
1 4
2 3
3 6
4
1 2
2 3
3 5
4 5
5
1 2
3 8
4 5
6 7
9 10
5
1 5
2 4
3 5
3 8
4 8
Output
0
1
2
0
-----Note-----
None | import sys
input = sys.stdin.readline
(T,) = map(int, input().split())
for _ in range(T):
(N,) = map(int, input().split())
X = []
for i in range(N):
l, r = map(int, input().split())
X.append((2 * l, i))
X.append((2 * r + 1, i))
X.sort()
R = 0
nn = 0
rr = 0
RR = [0] * N
kk = [0] * N
for c, i in X:
if c % 2:
R -= 1
rr = max(rr, nn - RR[i] + kk[i])
else:
RR[i] = nn
kk[i] = R
nn += 1
R += 1
print(N - rr) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Polycarp found $n$ segments on the street. A segment with the index $i$ is described by two integers $l_i$ and $r_i$ β coordinates of the beginning and end of the segment, respectively. Polycarp realized that he didn't need all the segments, so he wanted to delete some of them.
Polycarp believes that a set of $k$ segments is good if there is a segment $[l_i, r_i]$ ($1 \leq i \leq k$) from the set, such that it intersects every segment from the set (the intersection must be a point or segment). For example, a set of $3$ segments $[[1, 4], [2, 3], [3, 6]]$ is good, since the segment $[2, 3]$ intersects each segment from the set. Set of $4$ segments $[[1, 2], [2, 3], [3, 5], [4, 5]]$ is not good.
Polycarp wonders, what is the minimum number of segments he has to delete so that the remaining segments form a good set?
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^5$) β number of test cases. Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of segments. This is followed by $n$ lines describing the segments.
Each segment is described by two integers $l$ and $r$ ($1 \leq l \leq r \leq 10^9$) β coordinates of the beginning and end of the segment, respectively.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the minimum number of segments that need to be deleted in order for the set of remaining segments to become good.
-----Examples-----
Input
4
3
1 4
2 3
3 6
4
1 2
2 3
3 5
4 5
5
1 2
3 8
4 5
6 7
9 10
5
1 5
2 4
3 5
3 8
4 8
Output
0
1
2
0
-----Note-----
None | p = int(input())
for o in range(p):
n = int(input())
segments = []
for i in range(n):
l, r = map(int, input().strip().split())
segments.append([l, r])
byHead = segments[:]
byTail = segments[:]
byHead.sort()
byTail.sort(key=lambda x: x[1])
left = 0
right = 0
mx = 0
for i in range(n):
l = byHead[i][0]
r = byHead[i][1]
while byTail[left][1] < l:
left += 1
if right < n:
while byHead[right][0] <= r:
right += 1
if right == n:
break
mx = max(mx, right - left)
print(n - mx) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Polycarp found $n$ segments on the street. A segment with the index $i$ is described by two integers $l_i$ and $r_i$ β coordinates of the beginning and end of the segment, respectively. Polycarp realized that he didn't need all the segments, so he wanted to delete some of them.
Polycarp believes that a set of $k$ segments is good if there is a segment $[l_i, r_i]$ ($1 \leq i \leq k$) from the set, such that it intersects every segment from the set (the intersection must be a point or segment). For example, a set of $3$ segments $[[1, 4], [2, 3], [3, 6]]$ is good, since the segment $[2, 3]$ intersects each segment from the set. Set of $4$ segments $[[1, 2], [2, 3], [3, 5], [4, 5]]$ is not good.
Polycarp wonders, what is the minimum number of segments he has to delete so that the remaining segments form a good set?
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^5$) β number of test cases. Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of segments. This is followed by $n$ lines describing the segments.
Each segment is described by two integers $l$ and $r$ ($1 \leq l \leq r \leq 10^9$) β coordinates of the beginning and end of the segment, respectively.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the minimum number of segments that need to be deleted in order for the set of remaining segments to become good.
-----Examples-----
Input
4
3
1 4
2 3
3 6
4
1 2
2 3
3 5
4 5
5
1 2
3 8
4 5
6 7
9 10
5
1 5
2 4
3 5
3 8
4 8
Output
0
1
2
0
-----Note-----
None | import sys
input = sys.stdin.readline
class Bit:
def __init__(self, n):
self.size = n
self.tree = [0] * (n + 1)
def sum(self, i):
s = 0
while i > 0:
s += self.tree[i]
i -= i & -i
return s
def add(self, i, x):
while i <= self.size:
self.tree[i] += x
i += i & -i
t = int(input())
for _ in range(t):
n = int(input())
mm = 0
segs = []
gg = set()
for _ in range(n):
x, y = list(map(int, input().split()))
segs.append((x, y))
gg.add(x)
gg.add(y)
gg = list(set(gg))
gg.sort()
D = {}
for i in range(len(gg)):
D[gg[i]] = i + 1
for i in range(len(segs)):
segs[i] = D[segs[i][0]], D[segs[i][1]]
segs.sort()
fenwick_o = Bit(len(gg) + 3)
fenwick_c = Bit(len(gg) + 3)
for x, y in segs:
fenwick_o.add(x, 1)
fenwick_c.add(y + 1, 1)
best = 0
O = {}
C = {}
for x, y in segs:
if x not in C:
C[x] = fenwick_c.sum(x)
if y not in O:
O[y] = fenwick_o.sum(y)
best = max(best, O[y] - C[x])
if best == n:
break
print(n - best) | IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN 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 EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
count = defaultdict(int)
for i in barcodes:
count[i] += 1
heap = []
for key, val in list(count.items()):
heap.append([-val, key])
heapq.heapify(heap)
idx = 0
while len(heap) > 1:
one = heapq.heappop(heap)
two = heapq.heappop(heap)
barcodes[idx] = one[1]
idx += 1
barcodes[idx] = two[1]
idx += 1
one[0] += 1
two[0] += 1
if one[0] != 0:
heapq.heappush(heap, one)
if two[0] != 0:
heapq.heappush(heap, two)
if heap:
barcodes[idx] = heapq.heappop(heap)[1]
return barcodes | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
dicta = collections.defaultdict(int)
max_word = 0
for i in barcodes:
dicta[i] += 1
if dicta[i] > dicta[max_word]:
max_word = i
n = len(barcodes)
ans = [(0) for i in range(n)]
idx = 0
for i in range(dicta[max_word]):
ans[idx] = max_word
idx += 2
del dicta[max_word]
for let in dicta:
for i in range(dicta[let]):
if idx >= n:
idx = 1
ans[idx] = let
idx += 2
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
i, n = 0, len(barcodes)
res = [0] * n
for k, v in collections.Counter(barcodes).most_common():
for _ in range(v):
res[i] = k
i += 2
if i >= n:
i = 1
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def update(self, freq, key, heap):
if freq > 1:
heappush(heap, (-1 * (freq - 1), key))
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
freq_map = defaultdict(int)
heap = []
res = []
for n in barcodes:
freq_map[n] += 1
for key in list(freq_map.keys()):
heappush(heap, (-1 * freq_map[key], key))
while len(heap) > 0:
freq_a, key_a = heappop(heap)
res.append(key_a)
if len(heap) == 0:
break
freq_b, key_b = heappop(heap)
res.append(key_b)
self.update(-1 * freq_a, key_a, heap)
self.update(-1 * freq_b, key_b, heap)
return res | CLASS_DEF FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes):
cnt = dict()
for v in barcodes:
cnt[v] = cnt.get(v, 0) + 1
N = len(barcodes)
barcodes.sort(key=lambda v: (cnt[v], v), reverse=True)
A = [0] * N
A[::2], A[1::2] = barcodes[: (N + 1) // 2], barcodes[(N + 1) // 2 :]
return A | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
count = Counter(barcodes)
heap = [(-count[x], x) for x in list(count.keys())]
heapq.heapify(heap)
out = []
while len(heap) > 1:
count1, code1 = heapq.heappop(heap)
count2, code2 = heapq.heappop(heap)
out.extend([code1, code2])
count1 += 1
count2 += 1
if count1 < 0:
heapq.heappush(heap, (count1, code1))
if count2 < 0:
heapq.heappush(heap, (count2, code2))
if heap:
count1, code1 = heapq.heappop(heap)
out += [code1]
return out | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR LIST VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
freq = {}
for b in barcodes:
if b in freq:
freq[b] += 1
else:
freq[b] = 1
descBarcode = sorted(
[(k, v) for k, v in list(freq.items())], key=lambda i: i[1], reverse=True
)
newBarcode = []
maxIncrements = descBarcode[0][1]
curIndex, curIncrements = 1, 0
for num, count in descBarcode:
if not newBarcode:
newBarcode = [num] * count
else:
while count > 0:
newBarcode.insert(curIndex, num)
curIndex += 2
maxIncrements += 1
if curIncrements > maxIncrements or curIndex > len(newBarcode):
curIndex = 1
maxIncrements = 0
count -= 1
return newBarcode | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR ASSIGN VAR BIN_OP LIST VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
cb = collections.Counter(barcodes)
for kk, l in cb.most_common():
out = [[kk] for _ in range(l)]
break
i = 0
for k in cb:
if k != kk:
for v in range(cb[k]):
out[i % l].append(k)
i += 1
output = []
for o in out:
output += o
return output | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
queue = deque()
heap = []
d = {}
res = []
time = 0
for code in barcodes:
d[code] = d.get(code, 0) - 1
for k, v in d.items():
heapq.heappush(heap, (v, k))
while heap or queue:
if heap:
count, code = heapq.heappop(heap)
count += 1
res.append(code)
if count < 0:
queue.append([time + 1, (count, code)])
if queue and queue[0][0] <= time:
heapq.heappush(heap, queue.popleft()[1])
time += 1
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR VAR IF VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
count = collections.Counter(barcodes)
ans = [0] * len(barcodes)
index = 0
for key, freq in count.most_common():
for _ in range(freq):
if index >= len(barcodes):
index = 1
ans[index] = key
index += 2
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
h = []
d = defaultdict(int)
for ele in barcodes:
d[ele] += 1
for key, val in d.items():
heappush(h, (-1 * val, key))
res = []
prev = ()
while h:
ele = heappop(h)
freq = ele[0] * -1
res.append(ele[1])
if prev != ():
heappush(h, prev)
if freq != 1:
prev = -1 * (freq - 1), ele[1]
else:
prev = ()
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR LIST ASSIGN VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
mp = collections.defaultdict(int)
for b in barcodes:
mp[b] += 1
heap = []
for k, v in list(mp.items()):
heapq.heappush(heap, (-v, k))
idx = 0
n = len(barcodes)
ans = [0] * n
while heap:
f, v = heapq.heappop(heap)
f = -f
while f > 0:
ans[idx] = v
idx += 2
f -= 1
if idx >= n:
idx = 1
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
count = Counter(barcodes)
pq = []
for c in count:
heappush(pq, [-count[c], c])
output = []
prev = None
while pq:
c, v = heappop(pq)
c += 1
output.append(v)
if prev:
heappush(pq, prev)
prev = None
if c != 0:
prev = [c, v]
return output | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NONE WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NONE IF VAR NUMBER ASSIGN VAR LIST VAR VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
counts = {k: (0) for k in barcodes}
for n in barcodes:
counts[n] += 1
barcodes.sort()
barcode = []
heap = [(-y, x) for x, y in list(counts.items())]
heapq.heapify(heap)
print(heap)
while len(heap) > 0:
freq, digit = heapq.heappop(heap)
if len(barcode) == 0 or barcode[-1] != digit:
barcode.append(digit)
if freq < -1:
freq += 1
heapq.heappush(heap, (freq, digit))
else:
nextFreq, nextDig = heapq.heappop(heap)
barcode.append(nextDig)
heapq.heappush(heap, (freq, digit))
if nextFreq < -1:
nextFreq += 1
heapq.heappush(heap, (nextFreq, nextDig))
return barcode | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
count = defaultdict(int)
for i in barcodes:
count[i] -= 1
h = []
for i in count:
heappush(h, (count[i], i))
out = [(0) for _ in range(len(barcodes))]
freq, code = heappop(h)
for i in range(0, len(barcodes), 2):
out[i] = code
freq += 1
if not freq and h:
freq, code = heappop(h)
if not freq and h:
freq, code = heappop(h)
for i in range(1, len(barcodes), 2):
out[i] = code
freq += 1
if not freq and h:
freq, code = heappop(h)
return out | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
cnt = Counter(barcodes)
M = 0
for k, v in cnt.items():
if v > M:
mark = k
M = v
barcodes.sort()
pos = defaultdict(list)
j = 0
for i in range(len(barcodes)):
if barcodes[i] != mark:
pos[j].append(barcodes[i])
j = (j + 1) % M
ans = []
for i in range(M):
ans += [mark] + pos[i]
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR BIN_OP LIST VAR VAR VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
dicta = [0] * 10001
max_word = 0
for i in barcodes:
dicta[i] += 1
if dicta[i] > dicta[max_word]:
max_word = i
ans = [(0) for i in range(len(barcodes))]
idx = 0
for i in range(dicta[max_word]):
ans[idx] = max_word
idx += 2
dicta[max_word] = 0
for j in range(10001):
if dicta[j] > 0:
for i in range(dicta[j]):
if idx >= len(barcodes):
idx = 1
ans[idx] = j
idx += 2
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
freq = [0] * 10001
maxFreqCode = 0
for code in barcodes:
freq[code] += 1
if freq[code] > freq[maxFreqCode]:
maxFreqCode = code
i, n = 0, len(barcodes)
ans = [0] * n
for code in range(10001):
if code == 0:
code = maxFreqCode
for _ in range(freq[code]):
if i >= n:
i = 1
ans[i] = code
i += 2
freq[code] = 0
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
counter = Counter(barcodes)
keys = sorted(list(counter.keys()), key=lambda k: counter[k], reverse=True)
numbers = []
for key in keys:
numbers.extend([key] * counter[key])
ans = [0] * len(numbers)
N = len(numbers)
ans[::2] = numbers[: (N + 1) // 2]
ans[1::2] = numbers[(N + 1) // 2 :]
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
d = Counter(barcodes)
cand = barcodes[0]
maxi = d[cand]
for idx, (k, v) in enumerate(d.items()):
if v > maxi:
maxi = v
cand = k
res = [None for i in range(len(barcodes))]
pos = 0
for i in range(maxi):
res[pos] = cand
pos += 2
del d[cand]
for idx, (k, v) in enumerate(d.items()):
for j in range(v):
if pos >= len(res):
pos = 1
res[pos] = k
pos += 2
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
if len(barcodes) < 2:
return barcodes
counter = collections.Counter(barcodes)
heap = []
for k, v in counter.items():
heapq.heappush(heap, (-v, k))
result = []
while len(heap) > 1:
count1, key1 = heapq.heappop(heap)
count2, key2 = heapq.heappop(heap)
result.append(key1)
result.append(key2)
count1 += 1
count2 += 1
if count1 < 0:
heapq.heappush(heap, (count1, key1))
if count2 < 0:
heapq.heappush(heap, (count2, key2))
print(heap)
if len(heap) > 0:
count, key = heapq.heappop(heap)
result.append(key)
return result
else:
return result | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
dt = collections.Counter(barcodes)
hp = []
for code, freq in list(dt.items()):
heapq.heappush(hp, (-freq, code))
res = []
while len(hp) > 1:
freq1, bar1 = heapq.heappop(hp)
freq2, bar2 = heapq.heappop(hp)
res.append(bar1)
res.append(bar2)
if freq1 < -1:
heapq.heappush(hp, (freq1 + 1, bar1))
if freq2 < -1:
heapq.heappush(hp, (freq2 + 1, bar2))
if hp:
res.append(heapq.heappop(hp)[1])
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
n = len(barcodes)
if n == 1:
return barcodes
ans = []
barcodes.sort()
if n % 2 == 0:
for x, y in zip(barcodes[: n // 2], barcodes[n // 2 :]):
ans.extend([y, x])
else:
mid = barcodes[n // 2]
added, x_last = 0, -1
for x, y in zip(barcodes[: n // 2], barcodes[n // 2 + 1 :]):
if not added and x_last != mid and y != mid:
ans.append(mid)
added = 1
ans.extend([y, x])
x_last = x
if not added:
ans.append(mid)
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
freq = dict()
for i in range(len(barcodes)):
freq[barcodes[i]] = freq.get(barcodes[i], 0) + 1
heap = list()
reverse = collections.defaultdict(list)
for el in freq:
heap.append(-freq[el])
reverse[freq[el]].append(el)
heapq.heapify(heap)
temp_list = list()
answer = list()
while True:
if len(temp_list) > 1:
temp = temp_list.pop(0)
if temp[0] > 0:
heapq.heappush(heap, -temp[0])
reverse[temp[0]].append(temp[1])
if len(heap) > 0:
cur_freq = -heapq.heappop(heap)
cur_el = reverse[cur_freq].pop(0)
answer.append(cur_el)
temp_list.append([cur_freq - 1, cur_el])
else:
break
while len(temp_list) > 0:
temp = temp_list.pop(0)
if temp[0] > 0:
answer.append(temp[1])
return answer | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
if len(barcodes) <= 1:
return barcodes
counts = defaultdict(int)
for b in barcodes:
counts[b] += 1
max_count = 0
max_b = 0
for b, c in counts.items():
if c > max_count:
max_count = c
max_b = b
everything_else = [b for b in barcodes if b != max_b]
arranged = [max_b] * max_count
i = 1
while i <= len(arranged) and len(everything_else) != 0:
arranged.insert(i, everything_else.pop(0))
i += 2
def can_add_at(i, b):
if i == 0:
return arranged[i] != b
elif i == len(arranged):
return arranged[-1] != b
else:
return arranged[i] != b and arranged[i - 1] != b
i = 0
while len(everything_else) != 0:
if can_add_at(i, everything_else[0]):
arranged.insert(i, everything_else.pop(0))
i += 1
return arranged | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR RETURN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, S: List[int]) -> List[int]:
if not S:
return []
d = collections.Counter(S)
heap = []
for key, value in list(d.items()):
heapq.heappush(heap, [-value, key])
res = []
pre = heapq.heappop(heap)
res.append(pre[1])
while heap:
cur = heapq.heappop(heap)
res.append(cur[1])
pre[0] += 1
if pre[0] < 0:
heapq.heappush(heap, pre)
pre = cur
if len(res) != len(S):
return []
else:
return res | CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN LIST RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
hash_map = dict()
heap = list()
result = [0] * len(barcodes)
index = 0
for bar_code in barcodes:
hash_map[bar_code] = hash_map.get(bar_code, 0) + 1
print(hash_map)
for code, freq in hash_map.items():
heap.append((-freq, code))
heapq.heapify(heap)
while heap:
freq, code = heapq.heappop(heap)
freq = abs(freq)
while freq > 0:
if index >= len(barcodes):
index = 1
result[index] = code
index += 2
freq -= 1
return result | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
h = collections.Counter(barcodes)
heap = [(-value, key) for key, value in list(h.items())]
heapq.heapify(heap)
a = []
while heap:
x = heapq.heappop(heap)
if heap == []:
a.append(x[1])
return a
y = heapq.heappop(heap)
a.append(x[1])
a.append(y[1])
if x[0] + 1 != 0:
heapq.heappush(heap, (x[0] + 1, x[1]))
if y[0] + 1 != 0:
heapq.heappush(heap, (y[0] + 1, y[1]))
return a | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
counter_dict = collections.Counter(barcodes)
kk_heap = []
for it in list(counter_dict.items()):
heapq.heappush(kk_heap, [-it[1], it[0]])
last_kk = None
res = []
while kk_heap:
if len(kk_heap) == 1:
res.append(kk_heap[0][1])
break
choose_from = [heapq.heappop(kk_heap), heapq.heappop(kk_heap)]
idx = 0 if last_kk != choose_from[0][1] else 1
kk = choose_from[idx][1]
res.append(kk)
choose_from[idx][0] += 1
for cf in choose_from:
if cf[0]:
heapq.heappush(kk_heap, cf)
last_kk = kk
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR NONE ASSIGN VAR LIST WHILE VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
len_ = len(barcodes)
freq = collections.Counter()
maxFreqCode = 0
for code in barcodes:
freq[code] += 1
if freq[code] > freq[maxFreqCode]:
maxFreqCode = code
i = 0
ans = [0] * len_
for _ in range(freq[maxFreqCode]):
ans[i] = maxFreqCode
i += 2
freq[maxFreqCode] = 0
for code in freq:
for _ in range(freq[code]):
if i >= len_:
i = 1
ans[i] = code
i += 2
freq[code] = 0
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
l = barcodes[:]
n = len(l)
c = Counter(l)
c = list(sorted(list(c.items()), key=lambda x: -x[1]))
nl = [0] * n
y = 0
x = c[y][1]
val = c[y][0]
for i in range(0, n, 2):
nl[i] = val
x -= 1
if x == 0:
y += 1
if y < len(c):
x = c[y][1]
val = c[y][0]
for i in range(1, n, 2):
nl[i] = val
x -= 1
if x == 0:
y += 1
if y < len(c):
x = c[y][1]
val = c[y][0]
return nl | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
cnts = collections.defaultdict(int)
for n in barcodes:
cnts[n] += 1
barcodes.sort(key=lambda x: (cnts[x], x))
n = len(barcodes)
first, second = barcodes[: n // 2], barcodes[n // 2 :]
res = []
f, s = 0, 0
while f < len(first) or s < len(second):
if s < len(second):
res.append(second[s])
s += 1
if f < len(first):
res.append(first[f])
f += 1
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
cnts = collections.Counter(barcodes)
heap = [(-v, k) for k, v in list(cnts.items())]
heapq.heapify(heap)
res = []
while heap:
cnt1, num1 = heapq.heappop(heap)
if res and res[-1] == num1:
cnt, num = heapq.heappop(heap)
res.append(num)
cnt += 1
if cnt != 0:
heapq.heappush(heap, (cnt, num))
res.append(num1)
cnt1 += 1
if cnt1 != 0:
heapq.heappush(heap, (cnt1, num1))
else:
res.append(num1)
cnt1 += 1
if cnt1 != 0:
heapq.heappush(heap, (cnt1, num1))
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
count = collections.Counter(barcodes)
barcodes.sort(key=lambda a: (count[a], a))
barcodes[1::2], barcodes[::2] = (
barcodes[0 : len(barcodes) // 2],
barcodes[len(barcodes) // 2 :],
)
return barcodes | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
if len(barcodes) <= 1:
return barcodes
def helper(barcodes, k):
freqCount = collections.defaultdict(int)
for ele in barcodes:
freqCount[ele] += 1
result = []
cooldown = collections.deque()
heap = []
for key, value in freqCount.items():
heapq.heappush(heap, (-value, key))
while len(heap) > 0:
count, element = heapq.heappop(heap)
count *= -1
result.append(element)
cooldown.append((count - 1, element))
if len(cooldown) < k:
continue
cooldownCount, cooldownElement = cooldown.popleft()
if cooldownCount > 0:
heapq.heappush(heap, (-cooldownCount, cooldownElement))
return result
return helper(barcodes, 2) | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
record = dict()
for bar in barcodes:
record[bar] = record.get(bar, 0) + 1
record = sorted(
[(k, v) for k, v in record.items()], key=lambda x: x[1], reverse=True
)
max_count = record[0][1]
read = []
for k, v in record:
read += [k] * v
L = len(read)
res = [[] for _ in range(max_count)]
for idx in range(L):
res[idx % max_count].append(read[idx])
ress = []
for r in res:
ress += r
return ress | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR BIN_OP LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
if len(barcodes) < 2:
return barcodes
sorted_codes = sorted(barcodes)
halfway = len(barcodes) // 2
ans = [0] * (halfway * 2)
ans[::2], ans[1::2] = sorted_codes[-halfway:], sorted_codes[:halfway]
if len(barcodes) % 2 == 1:
prev = None
mid = sorted_codes[halfway]
for i in range(len(ans) - 1):
if ans[i] == mid:
i += 1
elif ans[i] != prev:
ans.insert(i, mid)
break
prev = ans[i]
return ans | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NONE ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
if barcodes is None or len(barcodes) == 0:
return []
n = len(barcodes)
dict_t = {}
for i in range(n):
temp = barcodes[i]
dict_t[temp] = dict_t.get(temp, 0) + 1
hp = []
for key, value in dict_t.items():
heapq.heappush(hp, (-value, key))
result = []
count1 = 0
count2 = 0
while hp:
count1, key1 = heapq.heappop(hp)
if hp:
count2, key2 = heapq.heappop(hp)
if count1 < 0 and count2 < 0:
result.append(key1)
result.append(key2)
count1 += 1
count2 += 1
elif count1 < 0 and count2 == 0:
result.append(key1)
count1 += 1
if count1 < 0:
heapq.heappush(hp, (count1, key1))
if count2 < 0:
heapq.heappush(hp, (count2, key2))
return result | CLASS_DEF FUNC_DEF VAR VAR IF VAR NONE FUNC_CALL VAR VAR NUMBER RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
count = {}
for code in barcodes:
if code in count:
count[code] += 1
else:
count[code] = 1
count = {
k: v
for k, v in sorted(count.items(), key=lambda item: item[1], reverse=True)
}
it = iter(count.keys())
cur = next(it)
i = 0
while i < len(barcodes):
while count[cur] == 0:
cur = next(it)
barcodes[i] = cur
count[cur] -= 1
i += 2
i = 1
while i < len(barcodes):
while count[cur] == 0:
cur = next(it)
barcodes[i] = cur
count[cur] -= 1
i += 2
return barcodes | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
c = Counter(barcodes)
heap = []
ans = []
for k, v in c.items():
heapq.heappush(heap, (-v, k))
while heap:
most, d1 = heapq.heappop(heap)
if ans and ans[-1] == d1:
more, d2 = heapq.heappop(heap)
ans.append(d2)
if more < -1:
heapq.heappush(heap, (more + 1, d2))
heapq.heappush(heap, (most, d1))
continue
ans.append(d1)
if most < -1:
heapq.heappush(heap, (most + 1, d1))
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
c = collections.Counter(barcodes)
heap = []
for cha, time in list(c.items()):
heapq.heappush(heap, (-time, cha))
res = []
while heap:
n = len(heap)
if n >= 2:
t1, c1 = heapq.heappop(heap)
res.append(c1)
t2, c2 = heapq.heappop(heap)
res.append(c2)
if t1 + 1 < 0:
heapq.heappush(heap, (t1 + 1, c1))
if t2 + 1 < 0:
heapq.heappush(heap, (t2 + 1, c2))
else:
t, c = heapq.heappop(heap)
res.append(c)
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
n = len(barcodes)
counter = Counter(barcodes)
sorted_barcodes = sorted(barcodes, key=lambda x: (counter[x], x))
res = [0] * n
res[1::2], res[::2] = sorted_barcodes[: n // 2], sorted_barcodes[n // 2 :]
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
m = [(0) for _ in range(10001)]
max_cnt = 0
max_n = 0
for n in barcodes:
m[n] += 1
if m[n] > max_cnt:
max_cnt = m[n]
max_n = n
res = [(0) for _ in range(len(barcodes))]
pos = 0
for i in range(10001):
n = max_n if i == 0 else i
while m[n] > 0:
m[n] -= 1
res[pos] = n
pos = pos + 2 if pos + 2 < len(barcodes) else 1
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
q = [[-v, k] for k, v in collections.Counter(barcodes).items()]
heapq.heapify(q)
result = []
while q:
if result and result[-1] == q[0][1]:
tmp = heapq.heappop(q)
item = heapq.heappop(q)
result.append(item[1])
item[0] += 1
if item[0] < 0:
heapq.heappush(q, item)
heapq.heappush(q, tmp)
else:
item = heapq.heappop(q)
result.append(item[1])
item[0] += 1
if item[0] < 0:
heapq.heappush(q, item)
return result | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR IF VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
d = collections.Counter(barcodes)
res = []
pq = []
for no, f in list(d.items()):
heapq.heappush(pq, [-f, no])
temp = []
while len(pq) != 0 or temp != 0:
if len(pq) != 0:
f, no = heapq.heappop(pq)
if temp != []:
heapq.heappush(pq, temp)
temp = []
elif temp != []:
f, no = temp
temp = []
else:
break
f = f * -1
f = f - 1
res.append(no)
if f != 0:
if len(pq) == 0:
heapq.heappush(pq, [-f, no])
elif -f > pq[0][0]:
heapq.heappush(pq, [-f, no])
else:
temp = [-f, no]
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST IF VAR LIST ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR LIST VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
if len(barcodes) <= 1:
return barcodes
heap, res = [], []
counter = collections.Counter(barcodes)
for val, cnt in counter.items():
heapq.heappush(heap, (-cnt, val))
res = []
while len(heap) > 1:
cnt1, val1 = heapq.heappop(heap)
cnt2, val2 = heapq.heappop(heap)
res.extend([val1, val2])
cnt1 += 1
cnt2 += 1
if cnt1:
heapq.heappush(heap, (cnt1, val1))
if cnt2:
heapq.heappush(heap, (cnt2, val2))
if heap:
res.append(heap[0][1])
return res | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
hq = [(-v, i) for i, v in collections.Counter(barcodes).items()]
heapq.heapify(hq)
res = []
while hq:
temp = []
for _ in range(min(len(hq), 2)):
val, number = heapq.heappop(hq)
temp.append((val, number))
res.append(number)
for _ in range(len(temp)):
val, number = temp.pop()
if val < -1:
heapq.heappush(hq, (val + 1, number))
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
counter = collections.Counter(barcodes)
data = [[] for i in range(len(barcodes) + 1)]
for i in counter:
data[counter[i]].append(i)
res = [None for k in range(len(barcodes))]
temp = 0
for j in range(len(data) - 1, 0, -1):
if len(data[j]) == 0:
continue
for char in data[j]:
times = j
while times > 0:
res[temp] = char
temp += 2
times -= 1
if temp >= len(res):
temp = 1
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
counter = Counter(barcodes)
heap = [(-v, c) for c, v in counter.items()]
heapq.heapify(heap)
res = []
while heap:
popped = []
for _ in range(2):
if not heap:
return res
v, c = heapq.heappop(heap)
res.append(c)
if v < -1:
popped.append([v + 1, c])
for v, c in popped:
heapq.heappush(heap, (v, c))
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
freq = {}
for b in barcodes:
if b in freq:
freq[b] += 1
else:
freq[b] = 1
frqBarcode = [(k, v) for k, v in list(freq.items())]
maxNum, maxIncrements = max(frqBarcode, key=lambda i: i[1])
newBarcode = [maxNum] * maxIncrements
curIndex, curIncrements = 1, 0
for num, count in frqBarcode:
if num == maxNum:
continue
while count > 0:
newBarcode.insert(curIndex, num)
curIndex += 2
maxIncrements += 1
if curIncrements > maxIncrements or curIndex > len(newBarcode):
curIndex = 1
maxIncrements = 0
count -= 1
return newBarcode | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
h = []
c = collections.Counter(barcodes)
for i in c:
heapq.heappush(h, (-c[i], i))
res = []
if len(barcodes) < 2:
return str(barcodes[0])
while h:
val1, char1 = heapq.heappop(h)
char2 = ""
if h:
val2, char2 = heapq.heappop(h)
res.append(char1)
res.append(char2)
if val1 * -1 > 1:
heapq.heappush(h, (val1 + 1, char1))
if val2 * -1 > 1:
heapq.heappush(h, (val2 + 1, char2))
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
d = defaultdict(lambda: 0)
for b in barcodes:
d[b] += 1
counter = 0
heap = []
for b in d:
heap.append((-d[b], counter, b))
counter += 1
heapify(heap)
result = []
while heap:
count1, tmp, b1 = heappop(heap)
result.append(b1)
if not heap:
return result
count2, tmp, b2 = heappop(heap)
result.append(b2)
count1 += 1
count2 += 1
if count1:
heappush(heap, (count1, counter, b1))
counter += 1
if count2:
heappush(heap, (count2, counter, b2))
counter += 1
return result | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
freq = collections.Counter(barcodes)
heap = []
for k, v in freq.items():
heapq.heappush(heap, (-v, k))
ans = []
while heap:
freq, num = heapq.heappop(heap)
if not ans:
ans.append(num)
freq += 1
if freq < 0:
heapq.heappush(heap, (freq, num))
elif num == ans[-1]:
nextFreq, nextNum = heapq.heappop(heap)
ans.append(nextNum)
nextFreq += 1
if nextFreq < 0:
heapq.heappush(heap, (nextFreq, nextNum))
heapq.heappush(heap, (freq, num))
else:
ans.append(num)
freq += 1
if freq < 0:
heapq.heappush(heap, (freq, num))
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
if not barcodes:
return []
res = [None] * len(barcodes)
barCount = collections.defaultdict(int)
for barcode in barcodes:
barCount[barcode] += 1
mostFreq = max(barCount, key=barCount.get)
i = 0
for _ in range(barCount[mostFreq]):
res[i] = mostFreq
i += 2
if i >= len(barcodes):
i = 1
barSet = set(barcodes)
barSet.remove(mostFreq)
for key in barSet:
if key == mostFreq:
continue
for _ in range(barCount[key]):
res[i] = key
i += 2
if i >= len(barcodes):
i = 1
return res | CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN LIST ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR VAR |
In a warehouse, there is a row of barcodes, where the i-th barcode isΒ barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal.Β You may return any answer, and it is guaranteed an answer exists.
Β
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Β
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000 | class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
counter = collections.Counter(barcodes)
pq = []
for key, value in counter.items():
heapq.heappush(pq, (-value, key))
temp_list = []
result = []
while pq:
k = 2
while pq and k > 0:
count, curr = heapq.heappop(pq)
count = count * -1
result.append(curr)
if count > 1:
temp_list.append((count - 1, curr))
k -= 1
if len(temp_list) == 0 and len(pq) == 0:
break
while temp_list:
count, curr = temp_list.pop()
heapq.heappush(pq, (-count, curr))
return result | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR |
Consider an array A consisting of N positive elements. The *frequency array* of A is the array B of size N such that B_{i} = *frequency* of element A_{i} in A.
For example, if A = [4, 7, 4, 11, 2, 7, 7], the *frequency array* B = [2, 3, 2, 1, 1, 3, 3].
You have lost the array A, but fortunately you have the array B.
Your task is to construct the lexicographically smallest array A such that:
1β€ A_{i} β€ 10^{5};
The frequency array of A is equal to B.
If no such array A exists, print -1.
Note: Array X is lexicographically smaller than array Y, if X_{i} < Y_{i}, where i is the first index where X and Y differ.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains a single integer N β the size of the array.
- The next line contains N space-separated integers - B_{1}, B_{2}, \ldots, B_{N}, the frequency array.
------ Output Format ------
For each test case, output on a new line, N space separated integers - A_{1}, A_{2}, \ldots, A_{N}, the lexicographically smallest array A. If no such array A exists, print -1.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€ B_{i} β€ 10^{5}$
- The sum of $N$ over all test cases won't exceed $10^{6}$.
----- Sample Input 1 ------
5
5
2 3 3 3 2
5
1 1 1 1 1
5
5 5 5 5 5
3
1 2 4
8
1 3 2 3 2 2 2 3
----- Sample Output 1 ------
1 2 2 2 1
1 2 3 4 5
1 1 1 1 1
-1
1 2 3 2 3 4 4 2
----- explanation 1 ------
Test case $1$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 2, 2, 1]$. The element $A_{1}$ and $A_{5}$ have frequency $2$ while $A_{2}, A_{3},$ and $A_{4}$ have frequency $3$.
Test case $2$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 3, 4, 5]$. Each element in $A$ has frequency $1$.
Test case $3$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 1, 1, 1, 1]$. Each element in $A$ has frequency $5$.
Test case $4$: No possible array $A$ exists having the given frequency array. | t = int(input())
for _ in range(t):
n = int(input())
B = list(map(int, input().split()))
countRecord = {}
for freq in B:
countRecord[freq] = countRecord.get(freq, 0) + 1
flag = False
for key, value in countRecord.items():
if value % key != 0:
flag = True
print(-1)
break
if flag:
continue
record = {}
A = [0] * n
for i in range(n):
if B[i] in record:
record[B[i]].append(i)
else:
record[B[i]] = [i]
count = 1
visited = set()
for i in range(n):
if i not in visited:
c2 = 0
flag = False
while record[B[i]] and c2 != B[i]:
flag = True
idx = record[B[i]].pop(0)
visited.add(idx)
A[idx] = count
c2 += 1
if flag:
count += 1
print(*A) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Consider an array A consisting of N positive elements. The *frequency array* of A is the array B of size N such that B_{i} = *frequency* of element A_{i} in A.
For example, if A = [4, 7, 4, 11, 2, 7, 7], the *frequency array* B = [2, 3, 2, 1, 1, 3, 3].
You have lost the array A, but fortunately you have the array B.
Your task is to construct the lexicographically smallest array A such that:
1β€ A_{i} β€ 10^{5};
The frequency array of A is equal to B.
If no such array A exists, print -1.
Note: Array X is lexicographically smaller than array Y, if X_{i} < Y_{i}, where i is the first index where X and Y differ.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains a single integer N β the size of the array.
- The next line contains N space-separated integers - B_{1}, B_{2}, \ldots, B_{N}, the frequency array.
------ Output Format ------
For each test case, output on a new line, N space separated integers - A_{1}, A_{2}, \ldots, A_{N}, the lexicographically smallest array A. If no such array A exists, print -1.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€ B_{i} β€ 10^{5}$
- The sum of $N$ over all test cases won't exceed $10^{6}$.
----- Sample Input 1 ------
5
5
2 3 3 3 2
5
1 1 1 1 1
5
5 5 5 5 5
3
1 2 4
8
1 3 2 3 2 2 2 3
----- Sample Output 1 ------
1 2 2 2 1
1 2 3 4 5
1 1 1 1 1
-1
1 2 3 2 3 4 4 2
----- explanation 1 ------
Test case $1$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 2, 2, 1]$. The element $A_{1}$ and $A_{5}$ have frequency $2$ while $A_{2}, A_{3},$ and $A_{4}$ have frequency $3$.
Test case $2$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 3, 4, 5]$. Each element in $A$ has frequency $1$.
Test case $3$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 1, 1, 1, 1]$. Each element in $A$ has frequency $5$.
Test case $4$: No possible array $A$ exists having the given frequency array. | def abc():
n = int(input())
b = list(map(int, input().split()))
freq = {}
for i in b:
if i not in freq.keys():
freq[i] = 0
freq[i] += 1
for i in b:
if freq[i] % i != 0:
print("-1")
return
m, f = {}, {}
j = 1
for i in b:
if i not in m.keys() or i not in f.keys() or f[i] == 0:
m[i] = j
f[i] = i
j += 1
print(m[i], end=" ")
f[i] -= 1
print()
for _ in range(int(input())):
abc() | 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 FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR VAR DICT DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Consider an array A consisting of N positive elements. The *frequency array* of A is the array B of size N such that B_{i} = *frequency* of element A_{i} in A.
For example, if A = [4, 7, 4, 11, 2, 7, 7], the *frequency array* B = [2, 3, 2, 1, 1, 3, 3].
You have lost the array A, but fortunately you have the array B.
Your task is to construct the lexicographically smallest array A such that:
1β€ A_{i} β€ 10^{5};
The frequency array of A is equal to B.
If no such array A exists, print -1.
Note: Array X is lexicographically smaller than array Y, if X_{i} < Y_{i}, where i is the first index where X and Y differ.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains a single integer N β the size of the array.
- The next line contains N space-separated integers - B_{1}, B_{2}, \ldots, B_{N}, the frequency array.
------ Output Format ------
For each test case, output on a new line, N space separated integers - A_{1}, A_{2}, \ldots, A_{N}, the lexicographically smallest array A. If no such array A exists, print -1.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€ B_{i} β€ 10^{5}$
- The sum of $N$ over all test cases won't exceed $10^{6}$.
----- Sample Input 1 ------
5
5
2 3 3 3 2
5
1 1 1 1 1
5
5 5 5 5 5
3
1 2 4
8
1 3 2 3 2 2 2 3
----- Sample Output 1 ------
1 2 2 2 1
1 2 3 4 5
1 1 1 1 1
-1
1 2 3 2 3 4 4 2
----- explanation 1 ------
Test case $1$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 2, 2, 1]$. The element $A_{1}$ and $A_{5}$ have frequency $2$ while $A_{2}, A_{3},$ and $A_{4}$ have frequency $3$.
Test case $2$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 3, 4, 5]$. Each element in $A$ has frequency $1$.
Test case $3$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 1, 1, 1, 1]$. Each element in $A$ has frequency $5$.
Test case $4$: No possible array $A$ exists having the given frequency array. | def Solve(freq, n):
d, ans = {}, []
next_element, size = 1, 0
for i in range(n):
if freq[i] == 1:
size += 1
ans.append(next_element)
next_element += 1
elif freq[i] not in d:
size += freq[i]
d[freq[i]] = [1, next_element]
ans.append(next_element)
next_element += 1
else:
value = d[freq[i]][1]
ans.append(value)
d[freq[i]][0] += 1
if d[freq[i]][0] >= freq[i]:
del d[freq[i]]
if size > n:
return [-1]
else:
return ans
for _ in range(int(input())):
n = int(input())
freq = list(map(int, input().split()))
print(*Solve(freq, n)) | FUNC_DEF ASSIGN VAR VAR DICT LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR RETURN LIST NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Consider an array A consisting of N positive elements. The *frequency array* of A is the array B of size N such that B_{i} = *frequency* of element A_{i} in A.
For example, if A = [4, 7, 4, 11, 2, 7, 7], the *frequency array* B = [2, 3, 2, 1, 1, 3, 3].
You have lost the array A, but fortunately you have the array B.
Your task is to construct the lexicographically smallest array A such that:
1β€ A_{i} β€ 10^{5};
The frequency array of A is equal to B.
If no such array A exists, print -1.
Note: Array X is lexicographically smaller than array Y, if X_{i} < Y_{i}, where i is the first index where X and Y differ.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains a single integer N β the size of the array.
- The next line contains N space-separated integers - B_{1}, B_{2}, \ldots, B_{N}, the frequency array.
------ Output Format ------
For each test case, output on a new line, N space separated integers - A_{1}, A_{2}, \ldots, A_{N}, the lexicographically smallest array A. If no such array A exists, print -1.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€ B_{i} β€ 10^{5}$
- The sum of $N$ over all test cases won't exceed $10^{6}$.
----- Sample Input 1 ------
5
5
2 3 3 3 2
5
1 1 1 1 1
5
5 5 5 5 5
3
1 2 4
8
1 3 2 3 2 2 2 3
----- Sample Output 1 ------
1 2 2 2 1
1 2 3 4 5
1 1 1 1 1
-1
1 2 3 2 3 4 4 2
----- explanation 1 ------
Test case $1$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 2, 2, 1]$. The element $A_{1}$ and $A_{5}$ have frequency $2$ while $A_{2}, A_{3},$ and $A_{4}$ have frequency $3$.
Test case $2$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 3, 4, 5]$. Each element in $A$ has frequency $1$.
Test case $3$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 1, 1, 1, 1]$. Each element in $A$ has frequency $5$.
Test case $4$: No possible array $A$ exists having the given frequency array. | for hm in range(int(input())):
a = int(input())
b = list(map(int, input().strip().split(" ")))
c = 0
e = set(b)
k = {}
for hm in b:
if hm in k:
k[hm] += 1
else:
k[hm] = 1
for hm in e:
c += k[hm] // hm * hm
if c != a:
print(-1)
continue
m = 1
n = {}
v = set(b)
for hm in range(a):
if b[hm] in n and n[b[hm]][1] > 0:
n[b[hm]][1] -= 1
b[hm] = n[b[hm]][0]
else:
n[b[hm]] = [m, b[hm] - 1]
b[hm] = m
m += 1
print(*b) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR LIST VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Consider an array A consisting of N positive elements. The *frequency array* of A is the array B of size N such that B_{i} = *frequency* of element A_{i} in A.
For example, if A = [4, 7, 4, 11, 2, 7, 7], the *frequency array* B = [2, 3, 2, 1, 1, 3, 3].
You have lost the array A, but fortunately you have the array B.
Your task is to construct the lexicographically smallest array A such that:
1β€ A_{i} β€ 10^{5};
The frequency array of A is equal to B.
If no such array A exists, print -1.
Note: Array X is lexicographically smaller than array Y, if X_{i} < Y_{i}, where i is the first index where X and Y differ.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains a single integer N β the size of the array.
- The next line contains N space-separated integers - B_{1}, B_{2}, \ldots, B_{N}, the frequency array.
------ Output Format ------
For each test case, output on a new line, N space separated integers - A_{1}, A_{2}, \ldots, A_{N}, the lexicographically smallest array A. If no such array A exists, print -1.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€ B_{i} β€ 10^{5}$
- The sum of $N$ over all test cases won't exceed $10^{6}$.
----- Sample Input 1 ------
5
5
2 3 3 3 2
5
1 1 1 1 1
5
5 5 5 5 5
3
1 2 4
8
1 3 2 3 2 2 2 3
----- Sample Output 1 ------
1 2 2 2 1
1 2 3 4 5
1 1 1 1 1
-1
1 2 3 2 3 4 4 2
----- explanation 1 ------
Test case $1$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 2, 2, 1]$. The element $A_{1}$ and $A_{5}$ have frequency $2$ while $A_{2}, A_{3},$ and $A_{4}$ have frequency $3$.
Test case $2$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 3, 4, 5]$. Each element in $A$ has frequency $1$.
Test case $3$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 1, 1, 1, 1]$. Each element in $A$ has frequency $5$.
Test case $4$: No possible array $A$ exists having the given frequency array. | t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
ans = [0] * n
d = {}
for item in arr:
if item in d:
d[item] += 1
else:
d[item] = 1
start = 1
curr_val = {}
for i in range(n):
if d[arr[i]] % arr[i] == 0:
curr_val[arr[i]] = start
start += 1
if arr[i] not in curr_val:
break
d[arr[i]] -= 1
ans[i] = curr_val[arr[i]]
if min(ans) == 0:
print(-1)
else:
print(*ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
Consider an array A consisting of N positive elements. The *frequency array* of A is the array B of size N such that B_{i} = *frequency* of element A_{i} in A.
For example, if A = [4, 7, 4, 11, 2, 7, 7], the *frequency array* B = [2, 3, 2, 1, 1, 3, 3].
You have lost the array A, but fortunately you have the array B.
Your task is to construct the lexicographically smallest array A such that:
1β€ A_{i} β€ 10^{5};
The frequency array of A is equal to B.
If no such array A exists, print -1.
Note: Array X is lexicographically smaller than array Y, if X_{i} < Y_{i}, where i is the first index where X and Y differ.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains a single integer N β the size of the array.
- The next line contains N space-separated integers - B_{1}, B_{2}, \ldots, B_{N}, the frequency array.
------ Output Format ------
For each test case, output on a new line, N space separated integers - A_{1}, A_{2}, \ldots, A_{N}, the lexicographically smallest array A. If no such array A exists, print -1.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€ B_{i} β€ 10^{5}$
- The sum of $N$ over all test cases won't exceed $10^{6}$.
----- Sample Input 1 ------
5
5
2 3 3 3 2
5
1 1 1 1 1
5
5 5 5 5 5
3
1 2 4
8
1 3 2 3 2 2 2 3
----- Sample Output 1 ------
1 2 2 2 1
1 2 3 4 5
1 1 1 1 1
-1
1 2 3 2 3 4 4 2
----- explanation 1 ------
Test case $1$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 2, 2, 1]$. The element $A_{1}$ and $A_{5}$ have frequency $2$ while $A_{2}, A_{3},$ and $A_{4}$ have frequency $3$.
Test case $2$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 3, 4, 5]$. Each element in $A$ has frequency $1$.
Test case $3$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 1, 1, 1, 1]$. Each element in $A$ has frequency $5$.
Test case $4$: No possible array $A$ exists having the given frequency array. | import sys
R = lambda: map(int, next(sys.stdin).split())
T = int(input())
for i in range(T):
length = int(input())
B = list(R())
maxB = max(B)
valid = []
numberFrequency = []
for i in range(maxB + 1):
numberFrequency.append(0)
numberCount = numberFrequency.copy()
unique = 1
for i in range(length):
if numberFrequency[B[i]] == 0:
numberFrequency[B[i]] = unique
numberCount[B[i]] = B[i]
unique += 1
numberCount[B[i]] -= 1
valid.append(str(numberFrequency[B[i]]))
if numberCount[B[i]] == 0:
numberFrequency[B[i]] = 0
empty = 1
for i in range(len(numberFrequency)):
if numberFrequency[i] != 0:
empty = 0
if empty == 1:
print(" ".join(valid))
else:
print(-1) | IMPORT ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER |
Consider an array A consisting of N positive elements. The *frequency array* of A is the array B of size N such that B_{i} = *frequency* of element A_{i} in A.
For example, if A = [4, 7, 4, 11, 2, 7, 7], the *frequency array* B = [2, 3, 2, 1, 1, 3, 3].
You have lost the array A, but fortunately you have the array B.
Your task is to construct the lexicographically smallest array A such that:
1β€ A_{i} β€ 10^{5};
The frequency array of A is equal to B.
If no such array A exists, print -1.
Note: Array X is lexicographically smaller than array Y, if X_{i} < Y_{i}, where i is the first index where X and Y differ.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains a single integer N β the size of the array.
- The next line contains N space-separated integers - B_{1}, B_{2}, \ldots, B_{N}, the frequency array.
------ Output Format ------
For each test case, output on a new line, N space separated integers - A_{1}, A_{2}, \ldots, A_{N}, the lexicographically smallest array A. If no such array A exists, print -1.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€ B_{i} β€ 10^{5}$
- The sum of $N$ over all test cases won't exceed $10^{6}$.
----- Sample Input 1 ------
5
5
2 3 3 3 2
5
1 1 1 1 1
5
5 5 5 5 5
3
1 2 4
8
1 3 2 3 2 2 2 3
----- Sample Output 1 ------
1 2 2 2 1
1 2 3 4 5
1 1 1 1 1
-1
1 2 3 2 3 4 4 2
----- explanation 1 ------
Test case $1$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 2, 2, 1]$. The element $A_{1}$ and $A_{5}$ have frequency $2$ while $A_{2}, A_{3},$ and $A_{4}$ have frequency $3$.
Test case $2$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 3, 4, 5]$. Each element in $A$ has frequency $1$.
Test case $3$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 1, 1, 1, 1]$. Each element in $A$ has frequency $5$.
Test case $4$: No possible array $A$ exists having the given frequency array. | def reconstruct_array(B):
a = []
using = {}
j = 1
k = 0
s = 0
for i in B:
if i in using:
if using[i][0][1] == 0:
using[i] = [[j, i]]
j += 1
s += i
a.append(using[i][0][0])
using[i][0][1] -= 1
else:
using[i] = [[j, i]]
s += i
j += 1
a.append(using[i][0][0])
using[i][0][1] -= 1
if s > len(B):
return [-1]
return a
for _ in range(int(input())):
n = int(input())
B = list(map(int, input().split()))
print(*reconstruct_array(B)) | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR LIST LIST VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR LIST LIST VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER IF VAR FUNC_CALL VAR VAR RETURN LIST NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Consider an array A consisting of N positive elements. The *frequency array* of A is the array B of size N such that B_{i} = *frequency* of element A_{i} in A.
For example, if A = [4, 7, 4, 11, 2, 7, 7], the *frequency array* B = [2, 3, 2, 1, 1, 3, 3].
You have lost the array A, but fortunately you have the array B.
Your task is to construct the lexicographically smallest array A such that:
1β€ A_{i} β€ 10^{5};
The frequency array of A is equal to B.
If no such array A exists, print -1.
Note: Array X is lexicographically smaller than array Y, if X_{i} < Y_{i}, where i is the first index where X and Y differ.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains a single integer N β the size of the array.
- The next line contains N space-separated integers - B_{1}, B_{2}, \ldots, B_{N}, the frequency array.
------ Output Format ------
For each test case, output on a new line, N space separated integers - A_{1}, A_{2}, \ldots, A_{N}, the lexicographically smallest array A. If no such array A exists, print -1.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€ B_{i} β€ 10^{5}$
- The sum of $N$ over all test cases won't exceed $10^{6}$.
----- Sample Input 1 ------
5
5
2 3 3 3 2
5
1 1 1 1 1
5
5 5 5 5 5
3
1 2 4
8
1 3 2 3 2 2 2 3
----- Sample Output 1 ------
1 2 2 2 1
1 2 3 4 5
1 1 1 1 1
-1
1 2 3 2 3 4 4 2
----- explanation 1 ------
Test case $1$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 2, 2, 1]$. The element $A_{1}$ and $A_{5}$ have frequency $2$ while $A_{2}, A_{3},$ and $A_{4}$ have frequency $3$.
Test case $2$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 3, 4, 5]$. Each element in $A$ has frequency $1$.
Test case $3$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 1, 1, 1, 1]$. Each element in $A$ has frequency $5$.
Test case $4$: No possible array $A$ exists having the given frequency array. | for _ in range(int(input())):
n = int(input())
freq = list(map(int, input().split()))
x = 1
for i in set(freq):
if freq.count(i) % i != 0:
print(-1)
break
else:
res = [0] * n
dic = {}
check = {}
for i in range(n):
p = freq[i]
if p in check and check[p] == p:
check.pop(p)
dic.pop(p)
if p == 1:
res[i] = x
x = x + 1
elif p in dic:
res[i] = dic[p]
check[p] += 1
elif p not in dic:
dic[p] = x
res[i] = x
check[p] = 1
x += 1
print(*res) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Consider an array A consisting of N positive elements. The *frequency array* of A is the array B of size N such that B_{i} = *frequency* of element A_{i} in A.
For example, if A = [4, 7, 4, 11, 2, 7, 7], the *frequency array* B = [2, 3, 2, 1, 1, 3, 3].
You have lost the array A, but fortunately you have the array B.
Your task is to construct the lexicographically smallest array A such that:
1β€ A_{i} β€ 10^{5};
The frequency array of A is equal to B.
If no such array A exists, print -1.
Note: Array X is lexicographically smaller than array Y, if X_{i} < Y_{i}, where i is the first index where X and Y differ.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains a single integer N β the size of the array.
- The next line contains N space-separated integers - B_{1}, B_{2}, \ldots, B_{N}, the frequency array.
------ Output Format ------
For each test case, output on a new line, N space separated integers - A_{1}, A_{2}, \ldots, A_{N}, the lexicographically smallest array A. If no such array A exists, print -1.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€ B_{i} β€ 10^{5}$
- The sum of $N$ over all test cases won't exceed $10^{6}$.
----- Sample Input 1 ------
5
5
2 3 3 3 2
5
1 1 1 1 1
5
5 5 5 5 5
3
1 2 4
8
1 3 2 3 2 2 2 3
----- Sample Output 1 ------
1 2 2 2 1
1 2 3 4 5
1 1 1 1 1
-1
1 2 3 2 3 4 4 2
----- explanation 1 ------
Test case $1$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 2, 2, 1]$. The element $A_{1}$ and $A_{5}$ have frequency $2$ while $A_{2}, A_{3},$ and $A_{4}$ have frequency $3$.
Test case $2$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 3, 4, 5]$. Each element in $A$ has frequency $1$.
Test case $3$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 1, 1, 1, 1]$. Each element in $A$ has frequency $5$.
Test case $4$: No possible array $A$ exists having the given frequency array. | import sys
input = sys.stdin.readline
def inp():
return int(input())
def inlt():
return list(map(int, input().split()))
def insr():
return input().strip()
def invr():
return map(int, input().split())
def outp(n):
sys.stdout.write(str(n) + "\n")
def outlt(lst):
sys.stdout.write(" ".join(map(str, lst)) + "\n")
def outplt(lst):
sys.stdout.write("\n".join(map(str, lst)))
def outpltlt(lst):
sys.stdout.write("\n".join(map(str, (" ".join(map(str, a)) for a in lst))))
ans = []
for _ in range(inp()):
N = inp()
B = inlt()
an = []
mx = 10**5
x = 1
dic = {B[0]: [x] * B[0]}
mx -= B[0]
for b in B:
if b not in dic or not dic[b]:
x += 1
dic[b] = [x] * b
mx -= b
if mx < 0:
break
an.append(dic[b].pop())
for x in dic:
if dic[x] or mx < 0:
an = [-1]
break
ans.append(an)
outpltlt(ans) | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT VAR NUMBER BIN_OP LIST VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP LIST VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Consider an array A consisting of N positive elements. The *frequency array* of A is the array B of size N such that B_{i} = *frequency* of element A_{i} in A.
For example, if A = [4, 7, 4, 11, 2, 7, 7], the *frequency array* B = [2, 3, 2, 1, 1, 3, 3].
You have lost the array A, but fortunately you have the array B.
Your task is to construct the lexicographically smallest array A such that:
1β€ A_{i} β€ 10^{5};
The frequency array of A is equal to B.
If no such array A exists, print -1.
Note: Array X is lexicographically smaller than array Y, if X_{i} < Y_{i}, where i is the first index where X and Y differ.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains a single integer N β the size of the array.
- The next line contains N space-separated integers - B_{1}, B_{2}, \ldots, B_{N}, the frequency array.
------ Output Format ------
For each test case, output on a new line, N space separated integers - A_{1}, A_{2}, \ldots, A_{N}, the lexicographically smallest array A. If no such array A exists, print -1.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€ B_{i} β€ 10^{5}$
- The sum of $N$ over all test cases won't exceed $10^{6}$.
----- Sample Input 1 ------
5
5
2 3 3 3 2
5
1 1 1 1 1
5
5 5 5 5 5
3
1 2 4
8
1 3 2 3 2 2 2 3
----- Sample Output 1 ------
1 2 2 2 1
1 2 3 4 5
1 1 1 1 1
-1
1 2 3 2 3 4 4 2
----- explanation 1 ------
Test case $1$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 2, 2, 1]$. The element $A_{1}$ and $A_{5}$ have frequency $2$ while $A_{2}, A_{3},$ and $A_{4}$ have frequency $3$.
Test case $2$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 3, 4, 5]$. Each element in $A$ has frequency $1$.
Test case $3$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 1, 1, 1, 1]$. Each element in $A$ has frequency $5$.
Test case $4$: No possible array $A$ exists having the given frequency array. | testcases = int(input())
for eachcase in range(testcases):
length = int(input())
array = list(map(int, input().split()))
value = 0
hashmapA = {}
hashmapB = {}
finalarray = []
for nums in array:
hashmapA[nums] = 1 + hashmapA.get(nums, 0)
for nums in array:
if hashmapA[nums] % nums == 0:
value += 1
hashmapB[nums] = value
finalarray.append(value)
hashmapA[nums] -= 1
elif nums in hashmapB:
finalarray.append(hashmapB[nums])
hashmapA[nums] -= 1
else:
finalarray.append(-1)
break
if finalarray[-1] == -1:
print(-1)
else:
print(*finalarray) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
Consider an array A consisting of N positive elements. The *frequency array* of A is the array B of size N such that B_{i} = *frequency* of element A_{i} in A.
For example, if A = [4, 7, 4, 11, 2, 7, 7], the *frequency array* B = [2, 3, 2, 1, 1, 3, 3].
You have lost the array A, but fortunately you have the array B.
Your task is to construct the lexicographically smallest array A such that:
1β€ A_{i} β€ 10^{5};
The frequency array of A is equal to B.
If no such array A exists, print -1.
Note: Array X is lexicographically smaller than array Y, if X_{i} < Y_{i}, where i is the first index where X and Y differ.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains a single integer N β the size of the array.
- The next line contains N space-separated integers - B_{1}, B_{2}, \ldots, B_{N}, the frequency array.
------ Output Format ------
For each test case, output on a new line, N space separated integers - A_{1}, A_{2}, \ldots, A_{N}, the lexicographically smallest array A. If no such array A exists, print -1.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€ B_{i} β€ 10^{5}$
- The sum of $N$ over all test cases won't exceed $10^{6}$.
----- Sample Input 1 ------
5
5
2 3 3 3 2
5
1 1 1 1 1
5
5 5 5 5 5
3
1 2 4
8
1 3 2 3 2 2 2 3
----- Sample Output 1 ------
1 2 2 2 1
1 2 3 4 5
1 1 1 1 1
-1
1 2 3 2 3 4 4 2
----- explanation 1 ------
Test case $1$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 2, 2, 1]$. The element $A_{1}$ and $A_{5}$ have frequency $2$ while $A_{2}, A_{3},$ and $A_{4}$ have frequency $3$.
Test case $2$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 3, 4, 5]$. Each element in $A$ has frequency $1$.
Test case $3$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 1, 1, 1, 1]$. Each element in $A$ has frequency $5$.
Test case $4$: No possible array $A$ exists having the given frequency array. | for _ in range(int(input())):
n = int(input())
b = list(map(int, input().split()))
a = list()
ref = dict()
j = 1
for i in b:
if i == 1:
a.append(j)
j += 1
else:
if i not in ref.keys():
ref[i] = [j, i]
j += 1
a.append(ref[i][0])
ref[i][1] -= 1
if not ref[i][1]:
ref.pop(i)
if ref:
print(-1)
else:
print(*a) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR VAR LIST VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
Consider an array A consisting of N positive elements. The *frequency array* of A is the array B of size N such that B_{i} = *frequency* of element A_{i} in A.
For example, if A = [4, 7, 4, 11, 2, 7, 7], the *frequency array* B = [2, 3, 2, 1, 1, 3, 3].
You have lost the array A, but fortunately you have the array B.
Your task is to construct the lexicographically smallest array A such that:
1β€ A_{i} β€ 10^{5};
The frequency array of A is equal to B.
If no such array A exists, print -1.
Note: Array X is lexicographically smaller than array Y, if X_{i} < Y_{i}, where i is the first index where X and Y differ.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains a single integer N β the size of the array.
- The next line contains N space-separated integers - B_{1}, B_{2}, \ldots, B_{N}, the frequency array.
------ Output Format ------
For each test case, output on a new line, N space separated integers - A_{1}, A_{2}, \ldots, A_{N}, the lexicographically smallest array A. If no such array A exists, print -1.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€ B_{i} β€ 10^{5}$
- The sum of $N$ over all test cases won't exceed $10^{6}$.
----- Sample Input 1 ------
5
5
2 3 3 3 2
5
1 1 1 1 1
5
5 5 5 5 5
3
1 2 4
8
1 3 2 3 2 2 2 3
----- Sample Output 1 ------
1 2 2 2 1
1 2 3 4 5
1 1 1 1 1
-1
1 2 3 2 3 4 4 2
----- explanation 1 ------
Test case $1$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 2, 2, 1]$. The element $A_{1}$ and $A_{5}$ have frequency $2$ while $A_{2}, A_{3},$ and $A_{4}$ have frequency $3$.
Test case $2$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 3, 4, 5]$. Each element in $A$ has frequency $1$.
Test case $3$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 1, 1, 1, 1]$. Each element in $A$ has frequency $5$.
Test case $4$: No possible array $A$ exists having the given frequency array. | for _ in range(int(input())):
n = int(input())
B = list(map(int, input().split()))
B_arr = {}
for i in B:
if i in B_arr:
B_arr[i] += 1
else:
B_arr[i] = 1
count = 0
for i in B_arr:
if B_arr[i] % i != 0:
count = 1
if count == 1:
print(-1)
else:
count_arr = {}
assign_arr = {}
A = []
j = 1
for i in B:
if i in count_arr:
A.append(assign_arr[i])
count_arr[i] += 1
if count_arr[i] == i:
del count_arr[i]
else:
count_arr[i] = 1
assign_arr[i] = j
A.append(j)
if count_arr[i] == i:
del count_arr[i]
j = j + 1
print(*A) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Consider an array A consisting of N positive elements. The *frequency array* of A is the array B of size N such that B_{i} = *frequency* of element A_{i} in A.
For example, if A = [4, 7, 4, 11, 2, 7, 7], the *frequency array* B = [2, 3, 2, 1, 1, 3, 3].
You have lost the array A, but fortunately you have the array B.
Your task is to construct the lexicographically smallest array A such that:
1β€ A_{i} β€ 10^{5};
The frequency array of A is equal to B.
If no such array A exists, print -1.
Note: Array X is lexicographically smaller than array Y, if X_{i} < Y_{i}, where i is the first index where X and Y differ.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains a single integer N β the size of the array.
- The next line contains N space-separated integers - B_{1}, B_{2}, \ldots, B_{N}, the frequency array.
------ Output Format ------
For each test case, output on a new line, N space separated integers - A_{1}, A_{2}, \ldots, A_{N}, the lexicographically smallest array A. If no such array A exists, print -1.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€ B_{i} β€ 10^{5}$
- The sum of $N$ over all test cases won't exceed $10^{6}$.
----- Sample Input 1 ------
5
5
2 3 3 3 2
5
1 1 1 1 1
5
5 5 5 5 5
3
1 2 4
8
1 3 2 3 2 2 2 3
----- Sample Output 1 ------
1 2 2 2 1
1 2 3 4 5
1 1 1 1 1
-1
1 2 3 2 3 4 4 2
----- explanation 1 ------
Test case $1$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 2, 2, 1]$. The element $A_{1}$ and $A_{5}$ have frequency $2$ while $A_{2}, A_{3},$ and $A_{4}$ have frequency $3$.
Test case $2$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 2, 3, 4, 5]$. Each element in $A$ has frequency $1$.
Test case $3$: The lexicographically smallest array $A$ having the given frequency array $B$ is $A = [1, 1, 1, 1, 1]$. Each element in $A$ has frequency $5$.
Test case $4$: No possible array $A$ exists having the given frequency array. | for i in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
l1 = []
d = {}
count = 1
s = set(l)
for j in s:
d[j] = [0, 0]
for k in l:
if d[k][1] == 0:
d[k][1] = k
d[k][0] = count
count += 1
if d[k][1] != 0:
l1.append(d[k][0])
d[k][1] -= 1
for q in d.values():
if q[1] != 0:
print(-1)
break
else:
for w in l1:
print(w, end=" ")
print() | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR LIST NUMBER NUMBER FOR VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.