description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1.
You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix).
Note that both steps are mandatory in each operation, and their order cannot be changed.
For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01.
You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β the length of the string $s$.
The second line contains string $s$ of $n$ characters. Each character is either 0 or 1.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print a single integerΒ β the maximum number of operations you can perform.
-----Example-----
Input
5
6
111010
1
0
1
1
2
11
6
101010
Output
3
1
1
1
3
-----Note-----
In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
|
for test in range(int(input())):
n = int(input())
s = input()
sl = []
ko = 0
c = 1
prev = s[0]
for r in s[1:]:
if r == prev:
c += 1
else:
sl.append(c)
c = 1
prev = r
sl.append(c)
if len(sl) == 1:
print(1)
else:
res = 1
z = sl[-1] - 1
for i in reversed(range(len(sl) - 1)):
if sl[i] == 1:
if z > 0:
res += 1
z -= 1
else:
z += 1
else:
res += 1
z = z + sl[i] - 2
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 ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Chef has two arrays A and B of the same size N.
In one operation, Chef can:
Choose an index i (1 β€ i β€ N) and swap the elements A_{i} and B_{i}.
Chef came up with a task to find the minimum possible value of (A_{max} - A_{min}) after performing the swap operation any (possibly zero) number of times.
Since Chef is busy, can you help him solve this task?
Note that A_{max} and A_{min} denote the maximum and minimum elements of the array A respectively.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains one integer N β the number of elements in each array.
- The second line consists of N space-separated integers A_{1}, A_{2},\ldots ,A_{N} denoting the elements of the array A.
- The third line consists of N space-separated integers B_{1}, B_{2}, \ldots ,B_{N} denoting the elements of the array B.
------ Output Format ------
For each test case, output on a new line, the minimum possible value of (A_{max} - A_{min}) in the array A after doing swap operation any number of times.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2\cdot 10^{5}$
$1 β€ A_{i}, B_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$.
----- Sample Input 1 ------
3
2
1 2
2 1
3
1 5 3
2 3 1
4
4 2 5 1
5 3 4 1
----- Sample Output 1 ------
0
1
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
By doing the above operations, array $A$ becomes $[2, 2]$. Here $(A_{max} - A_{min}) = 0$. It can be shown that this is the minimum value possible.
Test case $2$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
- Operation $2$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
By doing the above operations, array $A$ becomes $[2, 3, 3]$. Here $(A_{max} - A_{min}) = 1$. It can be shown that this is the minimum value possible.
Test case $3$: Chef can make the following operations:
- Operation $1$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
- Operation $2$: Choose $i=3$ and swap $A_{3}$ with $B_{3}$.
By doing the above operations, array $A$ becomes $[4, 3, 4, 1]$. Here $(A_{max} - A_{min}) = 3$. It can be shown that this is the minimum value possible.
|
t = int(input())
for k in range(t):
n = int(input())
a = [int(numa) for numa in input().split()]
b = [int(numb) for numb in input().split()]
for j in range(n):
if a[j] < b[j]:
temp = a[j]
a[j] = b[j]
b[j] = temp
c = []
for j in range(n):
l = []
l.append(a[j])
l.append(b[j])
c.append(l)
c.sort()
maximum = c[n - 1][0]
minimum = c[0][0]
ans = maximum - minimum
fmin = [0] * n
fmax = [0] * n
lmin = [0] * n
lmax = [0] * n
fmin[0] = c[0][0]
lmin[n - 1] = c[n - 1][1]
fmax[0] = c[0][0]
lmax[n - 1] = c[n - 1][1]
for i in range(1, n):
fmin[i] = min(fmin[i - 1], c[i][0])
fmax[i] = max(fmax[i - 1], c[i][0])
for i in range(n - 2, -1, -1):
lmin[i] = min(lmin[i + 1], c[i][1])
lmax[i] = max(lmax[i + 1], c[i][1])
for i in range(n - 1, -1, -1):
temp = c[i][0]
c[i][0] = c[i][1]
c[i][1] = temp
if i == 0:
maximum = lmax[0]
minimum = lmin[0]
else:
maximum = max(fmax[i - 1], lmax[i])
minimum = min(fmin[i - 1], lmin[i])
ans = min(ans, maximum - minimum)
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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Chef has two arrays A and B of the same size N.
In one operation, Chef can:
Choose an index i (1 β€ i β€ N) and swap the elements A_{i} and B_{i}.
Chef came up with a task to find the minimum possible value of (A_{max} - A_{min}) after performing the swap operation any (possibly zero) number of times.
Since Chef is busy, can you help him solve this task?
Note that A_{max} and A_{min} denote the maximum and minimum elements of the array A respectively.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains one integer N β the number of elements in each array.
- The second line consists of N space-separated integers A_{1}, A_{2},\ldots ,A_{N} denoting the elements of the array A.
- The third line consists of N space-separated integers B_{1}, B_{2}, \ldots ,B_{N} denoting the elements of the array B.
------ Output Format ------
For each test case, output on a new line, the minimum possible value of (A_{max} - A_{min}) in the array A after doing swap operation any number of times.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2\cdot 10^{5}$
$1 β€ A_{i}, B_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$.
----- Sample Input 1 ------
3
2
1 2
2 1
3
1 5 3
2 3 1
4
4 2 5 1
5 3 4 1
----- Sample Output 1 ------
0
1
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
By doing the above operations, array $A$ becomes $[2, 2]$. Here $(A_{max} - A_{min}) = 0$. It can be shown that this is the minimum value possible.
Test case $2$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
- Operation $2$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
By doing the above operations, array $A$ becomes $[2, 3, 3]$. Here $(A_{max} - A_{min}) = 1$. It can be shown that this is the minimum value possible.
Test case $3$: Chef can make the following operations:
- Operation $1$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
- Operation $2$: Choose $i=3$ and swap $A_{3}$ with $B_{3}$.
By doing the above operations, array $A$ becomes $[4, 3, 4, 1]$. Here $(A_{max} - A_{min}) = 3$. It can be shown that this is the minimum value possible.
|
for _ in range(int(input())):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list()
for i in range(N):
C.append([A[i], i])
C.append([B[i], i])
C.sort(key=lambda x: x[0])
ans = 10**10
last = [-1] * N
p = 0
num = 0
for i in range(2 * N):
val, ind = C[i]
if last[ind] == -1:
num += 1
last[ind] = i
if num < N:
continue
while True:
if last[C[p][1]] == p:
break
p += 1
ans = min(ans, C[i][0] - C[p][0])
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR WHILE NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Chef has two arrays A and B of the same size N.
In one operation, Chef can:
Choose an index i (1 β€ i β€ N) and swap the elements A_{i} and B_{i}.
Chef came up with a task to find the minimum possible value of (A_{max} - A_{min}) after performing the swap operation any (possibly zero) number of times.
Since Chef is busy, can you help him solve this task?
Note that A_{max} and A_{min} denote the maximum and minimum elements of the array A respectively.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains one integer N β the number of elements in each array.
- The second line consists of N space-separated integers A_{1}, A_{2},\ldots ,A_{N} denoting the elements of the array A.
- The third line consists of N space-separated integers B_{1}, B_{2}, \ldots ,B_{N} denoting the elements of the array B.
------ Output Format ------
For each test case, output on a new line, the minimum possible value of (A_{max} - A_{min}) in the array A after doing swap operation any number of times.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2\cdot 10^{5}$
$1 β€ A_{i}, B_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$.
----- Sample Input 1 ------
3
2
1 2
2 1
3
1 5 3
2 3 1
4
4 2 5 1
5 3 4 1
----- Sample Output 1 ------
0
1
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
By doing the above operations, array $A$ becomes $[2, 2]$. Here $(A_{max} - A_{min}) = 0$. It can be shown that this is the minimum value possible.
Test case $2$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
- Operation $2$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
By doing the above operations, array $A$ becomes $[2, 3, 3]$. Here $(A_{max} - A_{min}) = 1$. It can be shown that this is the minimum value possible.
Test case $3$: Chef can make the following operations:
- Operation $1$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
- Operation $2$: Choose $i=3$ and swap $A_{3}$ with $B_{3}$.
By doing the above operations, array $A$ becomes $[4, 3, 4, 1]$. Here $(A_{max} - A_{min}) = 3$. It can be shown that this is the minimum value possible.
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(n):
if a[i] < b[i]:
a[i], b[i] = b[i], a[i]
co = list(sorted(zip(a, b)))
c = [list(i) for i in co]
res = c[n - 1][0] - c[0][0]
firstMin = [c[0][0]] * n
firstMax = [c[0][0]] * n
lastMin = [c[n - 1][1]] * n
lastMax = [c[n - 1][1]] * n
for i in range(1, n):
firstMin[i] = min(firstMin[i - 1], c[i][0])
firstMax[i] = max(firstMax[i - 1], c[i][0])
for i in range(n - 2, -1, -1):
lastMin[i] = min(lastMin[i + 1], c[i][1])
lastMax[i] = max(lastMax[i + 1], c[i][1])
for i in range(n - 1, -1, -1):
if i == 0:
mx = lastMax[0]
mn = lastMin[0]
else:
mx = max(firstMax[i - 1], lastMax[i])
mn = min(firstMin[i - 1], lastMin[i])
res = min(res, mx - mn)
print(res)
|
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP LIST VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Chef has two arrays A and B of the same size N.
In one operation, Chef can:
Choose an index i (1 β€ i β€ N) and swap the elements A_{i} and B_{i}.
Chef came up with a task to find the minimum possible value of (A_{max} - A_{min}) after performing the swap operation any (possibly zero) number of times.
Since Chef is busy, can you help him solve this task?
Note that A_{max} and A_{min} denote the maximum and minimum elements of the array A respectively.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains one integer N β the number of elements in each array.
- The second line consists of N space-separated integers A_{1}, A_{2},\ldots ,A_{N} denoting the elements of the array A.
- The third line consists of N space-separated integers B_{1}, B_{2}, \ldots ,B_{N} denoting the elements of the array B.
------ Output Format ------
For each test case, output on a new line, the minimum possible value of (A_{max} - A_{min}) in the array A after doing swap operation any number of times.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2\cdot 10^{5}$
$1 β€ A_{i}, B_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$.
----- Sample Input 1 ------
3
2
1 2
2 1
3
1 5 3
2 3 1
4
4 2 5 1
5 3 4 1
----- Sample Output 1 ------
0
1
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
By doing the above operations, array $A$ becomes $[2, 2]$. Here $(A_{max} - A_{min}) = 0$. It can be shown that this is the minimum value possible.
Test case $2$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
- Operation $2$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
By doing the above operations, array $A$ becomes $[2, 3, 3]$. Here $(A_{max} - A_{min}) = 1$. It can be shown that this is the minimum value possible.
Test case $3$: Chef can make the following operations:
- Operation $1$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
- Operation $2$: Choose $i=3$ and swap $A_{3}$ with $B_{3}$.
By doing the above operations, array $A$ becomes $[4, 3, 4, 1]$. Here $(A_{max} - A_{min}) = 3$. It can be shown that this is the minimum value possible.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ad = a
bd = b
maxa = max(a)
mina = min(a)
maxai = a.index(maxa)
minai = a.index(mina)
d = a[maxai] - a[minai]
while True:
if bd[minai] > ad[minai]:
temp = ad[minai]
ad[minai] = bd[minai]
bd[minai] = temp
maxad = max(ad)
minad = min(ad)
if maxad - minad < d:
d = maxad - minad
if ad.index(minad) != minai:
minai = ad.index(minad)
else:
break
while True:
if bd[maxai] < ad[maxai]:
temp = ad[maxai]
ad[maxai] = bd[maxai]
bd[maxai] = temp
maxad = max(ad)
minad = min(ad)
if maxad - minad < d:
d = maxad - minad
if ad.index(maxad) != maxai:
maxai = ad.index(maxad)
else:
break
print(d)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR WHILE NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Chef has two arrays A and B of the same size N.
In one operation, Chef can:
Choose an index i (1 β€ i β€ N) and swap the elements A_{i} and B_{i}.
Chef came up with a task to find the minimum possible value of (A_{max} - A_{min}) after performing the swap operation any (possibly zero) number of times.
Since Chef is busy, can you help him solve this task?
Note that A_{max} and A_{min} denote the maximum and minimum elements of the array A respectively.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains one integer N β the number of elements in each array.
- The second line consists of N space-separated integers A_{1}, A_{2},\ldots ,A_{N} denoting the elements of the array A.
- The third line consists of N space-separated integers B_{1}, B_{2}, \ldots ,B_{N} denoting the elements of the array B.
------ Output Format ------
For each test case, output on a new line, the minimum possible value of (A_{max} - A_{min}) in the array A after doing swap operation any number of times.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2\cdot 10^{5}$
$1 β€ A_{i}, B_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$.
----- Sample Input 1 ------
3
2
1 2
2 1
3
1 5 3
2 3 1
4
4 2 5 1
5 3 4 1
----- Sample Output 1 ------
0
1
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
By doing the above operations, array $A$ becomes $[2, 2]$. Here $(A_{max} - A_{min}) = 0$. It can be shown that this is the minimum value possible.
Test case $2$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
- Operation $2$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
By doing the above operations, array $A$ becomes $[2, 3, 3]$. Here $(A_{max} - A_{min}) = 1$. It can be shown that this is the minimum value possible.
Test case $3$: Chef can make the following operations:
- Operation $1$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
- Operation $2$: Choose $i=3$ and swap $A_{3}$ with $B_{3}$.
By doing the above operations, array $A$ becomes $[4, 3, 4, 1]$. Here $(A_{max} - A_{min}) = 3$. It can be shown that this is the minimum value possible.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = []
for i in range(n):
if a[i] < b[i]:
a[i], b[i] = b[i], a[i]
c.append([a[i], b[i]])
c.sort()
fMin = [0] * n
fMax = [0] * n
lMin = [0] * n
lMax = [0] * n
fMax[0] = c[0][0]
fMin[0] = c[0][0]
lMax[-1] = c[-1][1]
lMin[-1] = c[-1][1]
for i in range(1, n):
fMin[i] = min(fMin[i - 1], c[i][0])
fMax[i] = max(fMax[i - 1], c[i][0])
for i in range(n - 2, -1, -1):
lMin[i] = min(lMin[i + 1], c[i][1])
lMax[i] = max(lMax[i + 1], c[i][1])
mx = c[-1][0]
mn = c[0][0]
ans = mx - mn
for i in range(n - 1, -1, -1):
if not i:
mx = lMax[0]
mn = lMin[0]
else:
mx = max(fMax[i - 1], lMax[i])
mn = min(fMin[i - 1], lMin[i])
ans = min(ans, mx - mn)
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Chef has two arrays A and B of the same size N.
In one operation, Chef can:
Choose an index i (1 β€ i β€ N) and swap the elements A_{i} and B_{i}.
Chef came up with a task to find the minimum possible value of (A_{max} - A_{min}) after performing the swap operation any (possibly zero) number of times.
Since Chef is busy, can you help him solve this task?
Note that A_{max} and A_{min} denote the maximum and minimum elements of the array A respectively.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains one integer N β the number of elements in each array.
- The second line consists of N space-separated integers A_{1}, A_{2},\ldots ,A_{N} denoting the elements of the array A.
- The third line consists of N space-separated integers B_{1}, B_{2}, \ldots ,B_{N} denoting the elements of the array B.
------ Output Format ------
For each test case, output on a new line, the minimum possible value of (A_{max} - A_{min}) in the array A after doing swap operation any number of times.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2\cdot 10^{5}$
$1 β€ A_{i}, B_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$.
----- Sample Input 1 ------
3
2
1 2
2 1
3
1 5 3
2 3 1
4
4 2 5 1
5 3 4 1
----- Sample Output 1 ------
0
1
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
By doing the above operations, array $A$ becomes $[2, 2]$. Here $(A_{max} - A_{min}) = 0$. It can be shown that this is the minimum value possible.
Test case $2$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
- Operation $2$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
By doing the above operations, array $A$ becomes $[2, 3, 3]$. Here $(A_{max} - A_{min}) = 1$. It can be shown that this is the minimum value possible.
Test case $3$: Chef can make the following operations:
- Operation $1$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
- Operation $2$: Choose $i=3$ and swap $A_{3}$ with $B_{3}$.
By doing the above operations, array $A$ becomes $[4, 3, 4, 1]$. Here $(A_{max} - A_{min}) = 3$. It can be shown that this is the minimum value possible.
|
for tcase in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = [(ai + bi, min(ai, bi), max(ai, bi)) for ai, bi in zip(a, b)]
c.sort()
mnb = [10**9]
for ci, ai, bi in c:
mnb.append(min(mnb[-1], bi))
mna = [10**9]
for ci, ai, bi in reversed(c):
mna.append(min(mna[-1], ai))
mna.reverse()
mn = [min(x, y) for x, y in zip(mna, mnb)]
mxb = [0]
for ci, ai, bi in c:
mxb.append(max(mxb[-1], bi))
mxa = [0]
for ci, ai, bi in reversed(c):
mxa.append(max(mxa[-1], ai))
mxa.reverse()
mx = [max(x, y) for x, y in zip(mxa, mxb)]
ans = [(mxi - mni) for mni, mxi in zip(mn, mx)]
print(min(ans))
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST BIN_OP NUMBER NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST BIN_OP NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Chef has two arrays A and B of the same size N.
In one operation, Chef can:
Choose an index i (1 β€ i β€ N) and swap the elements A_{i} and B_{i}.
Chef came up with a task to find the minimum possible value of (A_{max} - A_{min}) after performing the swap operation any (possibly zero) number of times.
Since Chef is busy, can you help him solve this task?
Note that A_{max} and A_{min} denote the maximum and minimum elements of the array A respectively.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains one integer N β the number of elements in each array.
- The second line consists of N space-separated integers A_{1}, A_{2},\ldots ,A_{N} denoting the elements of the array A.
- The third line consists of N space-separated integers B_{1}, B_{2}, \ldots ,B_{N} denoting the elements of the array B.
------ Output Format ------
For each test case, output on a new line, the minimum possible value of (A_{max} - A_{min}) in the array A after doing swap operation any number of times.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2\cdot 10^{5}$
$1 β€ A_{i}, B_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$.
----- Sample Input 1 ------
3
2
1 2
2 1
3
1 5 3
2 3 1
4
4 2 5 1
5 3 4 1
----- Sample Output 1 ------
0
1
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
By doing the above operations, array $A$ becomes $[2, 2]$. Here $(A_{max} - A_{min}) = 0$. It can be shown that this is the minimum value possible.
Test case $2$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
- Operation $2$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
By doing the above operations, array $A$ becomes $[2, 3, 3]$. Here $(A_{max} - A_{min}) = 1$. It can be shown that this is the minimum value possible.
Test case $3$: Chef can make the following operations:
- Operation $1$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
- Operation $2$: Choose $i=3$ and swap $A_{3}$ with $B_{3}$.
By doing the above operations, array $A$ becomes $[4, 3, 4, 1]$. Here $(A_{max} - A_{min}) = 3$. It can be shown that this is the minimum value possible.
|
t = int(input())
for i in range(t):
n = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
for i in range(n):
if A[i] < B[i]:
tmp = A[i]
A[i] = B[i]
B[i] = tmp
AB = [[(-1) for i in range(2)] for i in range(n)]
for i in range(n):
AB[i][0] = A[i]
AB[i][1] = B[i]
AB.sort(key=lambda row: row[0])
L, R = [], []
for i in range(len(AB)):
L.append(AB[i][0])
R.append(AB[i][1])
R_max, R_min = max(R), min(R)
maxi = float("-inf")
mini = float("inf")
for i in reversed(range(len(R))):
maxi = max(maxi, R[i])
mini = min(mini, R[i])
R[i] = [mini, maxi]
diff = min(max(L) - min(L), R_max - R_min)
for i in range(0, n - 1):
L_max, R_max = L[n - i - 2], R[-i - 1][1]
L_min, R_min = L[0], R[-i - 1][0]
A_max = max(L_max, R_max)
A_min = min(L_min, R_min)
diff = min(diff, A_max - A_min)
print(diff)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Chef has two arrays A and B of the same size N.
In one operation, Chef can:
Choose an index i (1 β€ i β€ N) and swap the elements A_{i} and B_{i}.
Chef came up with a task to find the minimum possible value of (A_{max} - A_{min}) after performing the swap operation any (possibly zero) number of times.
Since Chef is busy, can you help him solve this task?
Note that A_{max} and A_{min} denote the maximum and minimum elements of the array A respectively.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains one integer N β the number of elements in each array.
- The second line consists of N space-separated integers A_{1}, A_{2},\ldots ,A_{N} denoting the elements of the array A.
- The third line consists of N space-separated integers B_{1}, B_{2}, \ldots ,B_{N} denoting the elements of the array B.
------ Output Format ------
For each test case, output on a new line, the minimum possible value of (A_{max} - A_{min}) in the array A after doing swap operation any number of times.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2\cdot 10^{5}$
$1 β€ A_{i}, B_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$.
----- Sample Input 1 ------
3
2
1 2
2 1
3
1 5 3
2 3 1
4
4 2 5 1
5 3 4 1
----- Sample Output 1 ------
0
1
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
By doing the above operations, array $A$ becomes $[2, 2]$. Here $(A_{max} - A_{min}) = 0$. It can be shown that this is the minimum value possible.
Test case $2$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
- Operation $2$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
By doing the above operations, array $A$ becomes $[2, 3, 3]$. Here $(A_{max} - A_{min}) = 1$. It can be shown that this is the minimum value possible.
Test case $3$: Chef can make the following operations:
- Operation $1$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
- Operation $2$: Choose $i=3$ and swap $A_{3}$ with $B_{3}$.
By doing the above operations, array $A$ becomes $[4, 3, 4, 1]$. Here $(A_{max} - A_{min}) = 3$. It can be shown that this is the minimum value possible.
|
for i in range(int(input())):
N = int(input())
arr_A = list(map(int, input().split()))
arr_B = list(map(int, input().split()))
for i in range(N):
if arr_A[i] < arr_B[i]:
arr_A[i], arr_B[i] = arr_B[i], arr_A[i]
c = [[arr_A[i], arr_B[i]] for i in range(N)]
c.sort()
maxB = c[N - 1][0]
minB = c[0][0]
soln = maxB - minB
fminB = [(0) for i in range(N)]
fmaxB = [(0) for i in range(N)]
lminB = [(0) for i in range(N)]
lmaxB = [(0) for i in range(N)]
fminB[0] = c[0][0]
lminB[N - 1] = c[N - 1][1]
fmaxB[0] = c[0][0]
lmaxB[N - 1] = c[N - 1][1]
for i in range(1, N):
fminB[i] = min(fminB[i - 1], c[i][0])
fmaxB[i] = max(fmaxB[i - 1], c[i][0])
for i in range(N - 2, -1, -1):
lminB[i] = min(lminB[i + 1], c[i][1])
lmaxB[i] = max(lmaxB[i + 1], c[i][1])
for i in range(N - 1, -1, -1):
c[i][0], c[i][1] = c[i][1], c[i][0]
if i == 0:
maxB = lmaxB[0]
minB = lminB[0]
else:
maxB = max(fmaxB[i - 1], lmaxB[i])
minB = min(fminB[i - 1], lminB[i])
ss = maxB - minB
soln = min(soln, ss)
print(soln)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Chef has two arrays A and B of the same size N.
In one operation, Chef can:
Choose an index i (1 β€ i β€ N) and swap the elements A_{i} and B_{i}.
Chef came up with a task to find the minimum possible value of (A_{max} - A_{min}) after performing the swap operation any (possibly zero) number of times.
Since Chef is busy, can you help him solve this task?
Note that A_{max} and A_{min} denote the maximum and minimum elements of the array A respectively.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains one integer N β the number of elements in each array.
- The second line consists of N space-separated integers A_{1}, A_{2},\ldots ,A_{N} denoting the elements of the array A.
- The third line consists of N space-separated integers B_{1}, B_{2}, \ldots ,B_{N} denoting the elements of the array B.
------ Output Format ------
For each test case, output on a new line, the minimum possible value of (A_{max} - A_{min}) in the array A after doing swap operation any number of times.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2\cdot 10^{5}$
$1 β€ A_{i}, B_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$.
----- Sample Input 1 ------
3
2
1 2
2 1
3
1 5 3
2 3 1
4
4 2 5 1
5 3 4 1
----- Sample Output 1 ------
0
1
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
By doing the above operations, array $A$ becomes $[2, 2]$. Here $(A_{max} - A_{min}) = 0$. It can be shown that this is the minimum value possible.
Test case $2$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
- Operation $2$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
By doing the above operations, array $A$ becomes $[2, 3, 3]$. Here $(A_{max} - A_{min}) = 1$. It can be shown that this is the minimum value possible.
Test case $3$: Chef can make the following operations:
- Operation $1$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
- Operation $2$: Choose $i=3$ and swap $A_{3}$ with $B_{3}$.
By doing the above operations, array $A$ becomes $[4, 3, 4, 1]$. Here $(A_{max} - A_{min}) = 3$. It can be shown that this is the minimum value possible.
|
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split(" ")))
brr = list(map(int, input().split(" ")))
for idx, (val1, val2) in enumerate(zip(arr, brr)):
if val1 < val2:
arr[idx] = val2
brr[idx] = val1
combined = []
for idx, (val1, val2) in enumerate(zip(arr, brr)):
curr = [val1, val2]
combined.append(curr)
combined.sort()
firstMn = [(0) for _ in range(n)]
firstMx = [(0) for _ in range(n)]
lastMn = [(0) for _ in range(n)]
lastMx = [(0) for _ in range(n)]
firstMn[0] = combined[0][0]
firstMx[0] = combined[0][0]
lastMn[n - 1] = combined[n - 1][1]
lastMx[n - 1] = combined[n - 1][1]
for i in range(1, n):
firstMn[i] = min(firstMn[i - 1], combined[i][0])
firstMx[i] = max(firstMx[i - 1], combined[i][0])
for i in range(n - 2, -1, -1):
lastMn[i] = min(lastMn[i + 1], combined[i][1])
lastMx[i] = max(lastMx[i + 1], combined[i][1])
ans = combined[n - 1][0] - combined[0][0]
for i in range(n - 1, -1, -1):
if i == 0:
mx = lastMx[i]
mn = lastMn[i]
else:
mx = max(firstMx[i - 1], lastMx[i])
mn = min(firstMn[i - 1], lastMn[i])
ans = min(ans, mx - mn)
print(ans)
|
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 STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Chef has two arrays A and B of the same size N.
In one operation, Chef can:
Choose an index i (1 β€ i β€ N) and swap the elements A_{i} and B_{i}.
Chef came up with a task to find the minimum possible value of (A_{max} - A_{min}) after performing the swap operation any (possibly zero) number of times.
Since Chef is busy, can you help him solve this task?
Note that A_{max} and A_{min} denote the maximum and minimum elements of the array A respectively.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains one integer N β the number of elements in each array.
- The second line consists of N space-separated integers A_{1}, A_{2},\ldots ,A_{N} denoting the elements of the array A.
- The third line consists of N space-separated integers B_{1}, B_{2}, \ldots ,B_{N} denoting the elements of the array B.
------ Output Format ------
For each test case, output on a new line, the minimum possible value of (A_{max} - A_{min}) in the array A after doing swap operation any number of times.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2\cdot 10^{5}$
$1 β€ A_{i}, B_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$.
----- Sample Input 1 ------
3
2
1 2
2 1
3
1 5 3
2 3 1
4
4 2 5 1
5 3 4 1
----- Sample Output 1 ------
0
1
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
By doing the above operations, array $A$ becomes $[2, 2]$. Here $(A_{max} - A_{min}) = 0$. It can be shown that this is the minimum value possible.
Test case $2$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
- Operation $2$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
By doing the above operations, array $A$ becomes $[2, 3, 3]$. Here $(A_{max} - A_{min}) = 1$. It can be shown that this is the minimum value possible.
Test case $3$: Chef can make the following operations:
- Operation $1$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
- Operation $2$: Choose $i=3$ and swap $A_{3}$ with $B_{3}$.
By doing the above operations, array $A$ becomes $[4, 3, 4, 1]$. Here $(A_{max} - A_{min}) = 3$. It can be shown that this is the minimum value possible.
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
if n == 1:
print(0)
elif n == 2:
print(
min(abs(a[1] - a[0]), abs(b[1] - a[0]), abs(a[1] - b[0]), abs(b[1] - b[0]))
)
else:
els = []
for i in range(n):
els.append((a[i], i))
els.append((b[i], i))
els.sort()
doubs = {}
elids = {}
for i in range(2 * n):
e = els[i][1]
if e in doubs:
doubs[e].append(i)
else:
doubs[e] = [i]
elids[i] = e
mindist = float("inf")
ri = 0
li = 0
en = set()
while li < 2 * n:
while len(en) < n and ri < 2 * n:
en.add(els[ri][1])
ri += 1
if els[ri - 1][0] - els[li][0] < mindist and len(en) == n:
mindist = els[ri - 1][0] - els[li][0]
if doubs[els[li][1]][1] >= ri or doubs[els[li][1]][1] <= li:
en.remove(els[li][1])
li += 1
print(mindist)
|
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR BIN_OP NUMBER VAR WHILE FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Chef has two arrays A and B of the same size N.
In one operation, Chef can:
Choose an index i (1 β€ i β€ N) and swap the elements A_{i} and B_{i}.
Chef came up with a task to find the minimum possible value of (A_{max} - A_{min}) after performing the swap operation any (possibly zero) number of times.
Since Chef is busy, can you help him solve this task?
Note that A_{max} and A_{min} denote the maximum and minimum elements of the array A respectively.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains one integer N β the number of elements in each array.
- The second line consists of N space-separated integers A_{1}, A_{2},\ldots ,A_{N} denoting the elements of the array A.
- The third line consists of N space-separated integers B_{1}, B_{2}, \ldots ,B_{N} denoting the elements of the array B.
------ Output Format ------
For each test case, output on a new line, the minimum possible value of (A_{max} - A_{min}) in the array A after doing swap operation any number of times.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2\cdot 10^{5}$
$1 β€ A_{i}, B_{i} β€ 10^{9}$
- The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$.
----- Sample Input 1 ------
3
2
1 2
2 1
3
1 5 3
2 3 1
4
4 2 5 1
5 3 4 1
----- Sample Output 1 ------
0
1
3
----- explanation 1 ------
Test case $1$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
By doing the above operations, array $A$ becomes $[2, 2]$. Here $(A_{max} - A_{min}) = 0$. It can be shown that this is the minimum value possible.
Test case $2$: Chef can make the following operations:
- Operation $1$: Choose $i=1$ and swap $A_{1}$ with $B_{1}$.
- Operation $2$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
By doing the above operations, array $A$ becomes $[2, 3, 3]$. Here $(A_{max} - A_{min}) = 1$. It can be shown that this is the minimum value possible.
Test case $3$: Chef can make the following operations:
- Operation $1$: Choose $i=2$ and swap $A_{2}$ with $B_{2}$.
- Operation $2$: Choose $i=3$ and swap $A_{3}$ with $B_{3}$.
By doing the above operations, array $A$ becomes $[4, 3, 4, 1]$. Here $(A_{max} - A_{min}) = 3$. It can be shown that this is the minimum value possible.
|
t = int(input())
while t > 0:
n = int(input())
s = input()
arr = [int(ele) for ele in s.strip().split()]
s = input()
brr = [int(ele) for ele in s.strip().split()]
arr = arr + brr
arr = [(arr[i], i % n) for i in range(2 * n)]
arr.sort()
dic = {}
mn = 1000000001
j = 0
for i in range(2 * n):
if dic.get(arr[i][1], None) == None:
dic[arr[i][1]] = 0
dic[arr[i][1]] += 1
if len(dic) == n:
while len(dic) == n:
dic[arr[j][1]] -= 1
if dic[arr[j][1]] == 0:
del dic[arr[j][1]]
j += 1
mn = min(mn, arr[i][0] - arr[j - 1][0])
print(mn)
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER NONE NONE ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
|
You have an initial power P, an initial score of 0 points, and a bag of tokens.
Each token can be used at most once, has a value token[i], and has potentially two ways to use it.
If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point.
If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point.
Return the largest number of points we can have after playing any number of tokens.
Β
Example 1:
Input: tokens = [100], P = 50
Output: 0
Example 2:
Input: tokens = [100,200], P = 150
Output: 1
Example 3:
Input: tokens = [100,200,300,400], P = 200
Output: 2
Β
Note:
tokens.length <= 1000
0 <= tokens[i] < 10000
0 <= P < 10000
|
class Solution:
def bagOfTokensScore(self, tokens: List[int], P: int) -> int:
tokens = sorted(tokens)
left = 0
right = len(tokens) - 1
points = 0
if len(tokens) == 1:
if tokens[0] <= P:
return 1
if len(tokens) == 0:
return 0
while left < right:
if tokens[left] <= P:
P -= tokens[left]
left += 1
points += 1
elif tokens[left] > P and points > 0:
P += tokens[right]
points -= 1
right -= 1
elif points == 0 and tokens[left] > P:
break
if P >= tokens[left]:
points += 1
return points
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR NUMBER RETURN VAR VAR
|
You have an initial power P, an initial score of 0 points, and a bag of tokens.
Each token can be used at most once, has a value token[i], and has potentially two ways to use it.
If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point.
If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point.
Return the largest number of points we can have after playing any number of tokens.
Β
Example 1:
Input: tokens = [100], P = 50
Output: 0
Example 2:
Input: tokens = [100,200], P = 150
Output: 1
Example 3:
Input: tokens = [100,200,300,400], P = 200
Output: 2
Β
Note:
tokens.length <= 1000
0 <= tokens[i] < 10000
0 <= P < 10000
|
class Solution:
def bagOfTokensScore(self, tokens: List[int], P: int) -> int:
n = len(tokens)
tokens.sort()
i = 0
j = n - 1
points = 0
result = 0
while i <= j:
if tokens[i] <= P:
points += 1
result = max(result, points)
P -= tokens[i]
i += 1
else:
if points == 0:
break
P += tokens[j]
points -= 1
j -= 1
return result
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
You have an initial power P, an initial score of 0 points, and a bag of tokens.
Each token can be used at most once, has a value token[i], and has potentially two ways to use it.
If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point.
If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point.
Return the largest number of points we can have after playing any number of tokens.
Β
Example 1:
Input: tokens = [100], P = 50
Output: 0
Example 2:
Input: tokens = [100,200], P = 150
Output: 1
Example 3:
Input: tokens = [100,200,300,400], P = 200
Output: 2
Β
Note:
tokens.length <= 1000
0 <= tokens[i] < 10000
0 <= P < 10000
|
class Solution:
def bagOfTokensScore(self, tokens: List[int], P: int) -> int:
max_points, points = 0, 0
tokens.sort()
i, j = 0, len(tokens) - 1
while i <= j:
if P < tokens[i]:
if i == j or points == 0:
break
P += tokens[j]
points -= 1
j -= 1
points += 1
P -= tokens[i]
i += 1
max_points = max(max_points, points)
return max(max_points, points)
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
|
You have an initial power P, an initial score of 0 points, and a bag of tokens.
Each token can be used at most once, has a value token[i], and has potentially two ways to use it.
If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point.
If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point.
Return the largest number of points we can have after playing any number of tokens.
Β
Example 1:
Input: tokens = [100], P = 50
Output: 0
Example 2:
Input: tokens = [100,200], P = 150
Output: 1
Example 3:
Input: tokens = [100,200,300,400], P = 200
Output: 2
Β
Note:
tokens.length <= 1000
0 <= tokens[i] < 10000
0 <= P < 10000
|
class Solution:
def bagOfTokensScore(self, tokens: List[int], P: int) -> int:
if not len(tokens):
return 0
tokens.sort()
res = 0
deque = collections.deque(tokens)
while len(deque) > 1 and (P >= deque[0] or res):
while deque and P > deque[0]:
res += 1
P -= deque.popleft()
if len(deque) > 1 and res:
P += deque.pop()
res -= 1
if deque and P >= deque[0]:
res += 1
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR WHILE VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
You have an initial power P, an initial score of 0 points, and a bag of tokens.
Each token can be used at most once, has a value token[i], and has potentially two ways to use it.
If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point.
If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point.
Return the largest number of points we can have after playing any number of tokens.
Β
Example 1:
Input: tokens = [100], P = 50
Output: 0
Example 2:
Input: tokens = [100,200], P = 150
Output: 1
Example 3:
Input: tokens = [100,200,300,400], P = 200
Output: 2
Β
Note:
tokens.length <= 1000
0 <= tokens[i] < 10000
0 <= P < 10000
|
class Solution:
def bagOfTokensScore(self, tokens: List[int], P: int) -> int:
tokens.sort()
def twoPointer(tokens, P):
i = 0
j = len(tokens) - 1
score = 0
maxScore = 0
while i <= j:
flag = False
while i < len(tokens) and P >= tokens[i]:
score += 1
P -= tokens[i]
i += 1
flag = True
if flag:
maxScore = max(score, maxScore)
continue
if score > 0:
score -= 1
P += tokens[j]
j -= 1
elif P < tokens[i]:
return score
return maxScore
def helper(tokens, P, score):
maxVal = score
if score > 0 and len(tokens) > 0:
maxVal = max(
helper(
tokens[: len(tokens) - 1],
P + tokens[len(tokens) - 1],
score - 1,
),
maxVal,
)
for i in range(len(tokens) - 1, -1, -1):
if P >= tokens[i]:
maxVal = max(
helper(tokens[:i] + tokens[i + 1 :], P - tokens[i], score + 1),
maxVal,
)
return maxVal
return twoPointer(tokens, P)
|
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR
|
You have an initial power P, an initial score of 0 points, and a bag of tokens.
Each token can be used at most once, has a value token[i], and has potentially two ways to use it.
If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point.
If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point.
Return the largest number of points we can have after playing any number of tokens.
Β
Example 1:
Input: tokens = [100], P = 50
Output: 0
Example 2:
Input: tokens = [100,200], P = 150
Output: 1
Example 3:
Input: tokens = [100,200,300,400], P = 200
Output: 2
Β
Note:
tokens.length <= 1000
0 <= tokens[i] < 10000
0 <= P < 10000
|
class Solution:
def bagOfTokensScore(self, tokens: List[int], P: int) -> int:
tokens.sort()
max_points = 0
points = 0
i = 0
j = len(tokens) - 1
while i <= j:
if P >= tokens[i]:
points += 1
P -= tokens[i]
max_points = max(points, max_points)
i += 1
elif points > 0:
points -= 1
P += tokens[j]
j -= 1
else:
return max_points
return max_points
|
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR RETURN VAR VAR
|
You have an initial power P, an initial score of 0 points, and a bag of tokens.
Each token can be used at most once, has a value token[i], and has potentially two ways to use it.
If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point.
If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point.
Return the largest number of points we can have after playing any number of tokens.
Β
Example 1:
Input: tokens = [100], P = 50
Output: 0
Example 2:
Input: tokens = [100,200], P = 150
Output: 1
Example 3:
Input: tokens = [100,200,300,400], P = 200
Output: 2
Β
Note:
tokens.length <= 1000
0 <= tokens[i] < 10000
0 <= P < 10000
|
class Solution:
def bagOfTokensScore(self, tokens: List[int], P: int) -> int:
tokens.sort()
if not tokens or P < tokens[0]:
return 0
l, r = 0, len(tokens) - 1
points = 0
while l <= r:
if P >= tokens[l]:
points += 1
P -= tokens[l]
l += 1
elif r - l > 1:
points -= 1
P += tokens[r]
r -= 1
else:
break
return points
|
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR VAR
|
You have an initial power P, an initial score of 0 points, and a bag of tokens.
Each token can be used at most once, has a value token[i], and has potentially two ways to use it.
If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point.
If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point.
Return the largest number of points we can have after playing any number of tokens.
Β
Example 1:
Input: tokens = [100], P = 50
Output: 0
Example 2:
Input: tokens = [100,200], P = 150
Output: 1
Example 3:
Input: tokens = [100,200,300,400], P = 200
Output: 2
Β
Note:
tokens.length <= 1000
0 <= tokens[i] < 10000
0 <= P < 10000
|
class Solution:
def bagOfTokensScore(self, tokens: List[int], P: int) -> int:
if not tokens:
return 0
tokens.sort()
point = 0
while tokens:
if P < tokens[0]:
if point and len(tokens) > 1:
P += tokens.pop()
point -= 1
else:
break
else:
P -= tokens.pop(0)
point += 1
return point
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR IF VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR NUMBER RETURN VAR VAR
|
You have an initial power P, an initial score of 0 points, and a bag of tokens.
Each token can be used at most once, has a value token[i], and has potentially two ways to use it.
If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point.
If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point.
Return the largest number of points we can have after playing any number of tokens.
Β
Example 1:
Input: tokens = [100], P = 50
Output: 0
Example 2:
Input: tokens = [100,200], P = 150
Output: 1
Example 3:
Input: tokens = [100,200,300,400], P = 200
Output: 2
Β
Note:
tokens.length <= 1000
0 <= tokens[i] < 10000
0 <= P < 10000
|
class Solution:
def bagOfTokensScore(self, tokens: List[int], P: int) -> int:
tokens.sort()
buyer, seller = 0, len(tokens) - 1
max_items = current_items = 0
while buyer <= seller:
if P >= tokens[buyer]:
P -= tokens[buyer]
current_items += 1
buyer += 1
elif current_items == 0:
break
else:
P += tokens[seller]
current_items -= 1
seller -= 1
max_items = max(max_items, current_items)
return max_items
|
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
|
You have an initial power P, an initial score of 0 points, and a bag of tokens.
Each token can be used at most once, has a value token[i], and has potentially two ways to use it.
If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point.
If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point.
Return the largest number of points we can have after playing any number of tokens.
Β
Example 1:
Input: tokens = [100], P = 50
Output: 0
Example 2:
Input: tokens = [100,200], P = 150
Output: 1
Example 3:
Input: tokens = [100,200,300,400], P = 200
Output: 2
Β
Note:
tokens.length <= 1000
0 <= tokens[i] < 10000
0 <= P < 10000
|
class Solution:
def bagOfTokensScore(self, tokens: List[int], P: int) -> int:
tokens.sort()
maxPoints = 0
points = 0
left, right = 0, len(tokens) - 1
while left <= right and P >= 0:
if P - tokens[left] >= 0:
P -= tokens[left]
points += 1
maxPoints = max(maxPoints, points)
left += 1
elif points > 0:
P += tokens[right]
points -= 1
right -= 1
else:
break
return max(maxPoints, points)
|
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR
|
You have an initial power P, an initial score of 0 points, and a bag of tokens.
Each token can be used at most once, has a value token[i], and has potentially two ways to use it.
If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point.
If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point.
Return the largest number of points we can have after playing any number of tokens.
Β
Example 1:
Input: tokens = [100], P = 50
Output: 0
Example 2:
Input: tokens = [100,200], P = 150
Output: 1
Example 3:
Input: tokens = [100,200,300,400], P = 200
Output: 2
Β
Note:
tokens.length <= 1000
0 <= tokens[i] < 10000
0 <= P < 10000
|
class Solution:
def bagOfTokensScore(self, tokens: List[int], P: int) -> int:
startIndex = 0
endIndex = len(tokens) - 1
points = 0
tokens.sort()
while True:
print(startIndex, endIndex, P)
if startIndex > endIndex or endIndex < startIndex:
return points
if P >= tokens[startIndex]:
points += 1
P -= tokens[startIndex]
startIndex += 1
elif points == 0:
return points
elif startIndex == endIndex:
return points
else:
P += tokens[endIndex]
endIndex -= 1
points -= 1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR WHILE NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR IF VAR VAR RETURN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR
|
Today you are going to lead a group of elven archers to defend the castle that is attacked by an army of angry orcs. Three sides of the castle are protected by impassable mountains and the remaining side is occupied by a long wall that is split into n sections. At this moment there are exactly ai archers located at the i-th section of this wall. You know that archer who stands at section i can shoot orcs that attack section located at distance not exceeding r, that is all such sections j that |i - j| β€ r. In particular, r = 0 means that archers are only capable of shooting at orcs who attack section i.
Denote as defense level of section i the total number of archers who can shoot at the orcs attacking this section. Reliability of the defense plan is the minimum value of defense level of individual wall section.
There is a little time left till the attack so you can't redistribute archers that are already located at the wall. However, there is a reserve of k archers that you can distribute among wall sections in arbitrary way. You would like to achieve maximum possible reliability of the defence plan.
Input
The first line of the input contains three integers n, r and k (1 β€ n β€ 500 000, 0 β€ r β€ n, 0 β€ k β€ 1018) β the number of sections of the wall, the maximum distance to other section archers can still shoot and the number of archers yet to be distributed along the wall. The second line contains n integers a1, a2, ..., an (0 β€ ai β€ 109) β the current number of archers at each section.
Output
Print one integer β the maximum possible value of defense plan reliability, i.e. the maximum possible value of minimum defense level if we distribute k additional archers optimally.
Examples
Input
5 0 6
5 4 3 4 9
Output
5
Input
4 2 0
1 2 3 4
Output
6
Input
5 1 1
2 1 2 1 2
Output
3
|
import sys
border = 1 << 30
def build(x=0):
res = [0, 0]
i = 0
while x and i < 30:
if x & 1:
res[1] += 1 << i
x >>= 1
i += 1
i = 0
while x:
if x & 1:
res[0] += 1 << i
x >>= 1
i += 1
return res
n, r, k = map(int, sys.stdin.buffer.readline().decode("utf-8").split())
a = list(map(int, sys.stdin.buffer.readline().decode("utf-8").split()))
k_big, k_small = build(k)
left = build(sum(a[:r]))
minus_big, minus_small = [0] * n, [0] * n
w = 2 * r + 1
def solve(level_big, level_small):
used_big = used_small = 0
acc_big, acc_small = left
for i in range(n):
if minus_small[i] or minus_big[i]:
acc_big -= minus_big[i]
acc_small -= minus_small[i]
if acc_small < 0:
acc_small += border
acc_big -= 1
elif acc_small >= border:
acc_small -= border
acc_big += 1
minus_big[i] = minus_small[i] = 0
acc_small += (a[i + r] if i + r < n else 0) - (
a[i - r - 1] if i - r >= 1 else 0
)
if acc_small >= border:
acc_small -= border
acc_big += 1
elif acc_small < 0:
acc_small += border
acc_big -= 1
if acc_big < level_big or acc_big == level_big and acc_small < level_small:
used_big += level_big - acc_big
used_small += level_small - acc_small
if used_small >= border:
used_small -= border
used_big += 1
elif used_small < 0:
used_small += border
used_big -= 1
if used_big > k_big or used_big == k_big and used_small > k_small:
break
if i + w < n:
minus_big[i + w] = level_big - acc_big
minus_small[i + w] = level_small - acc_small
acc_big = level_big
acc_small = level_small
for i in range(i + 1, n):
if minus_small[i] or minus_big[i]:
minus_small[i] = minus_big[i] = 0
return used_big < k_big or used_big == k_big and used_small <= k_small
ok, ng = min(a), k + sum(a) + 1
while abs(ok - ng) > 1:
mid = ok + ng >> 1
if solve(*build(mid)):
ok = mid
else:
ng = mid
print(ok)
|
IMPORT ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
There is a deck of $n$ cards. The $i$-th card has a number $a_i$ on the front and a number $b_i$ on the back. Every integer between $1$ and $2n$ appears exactly once on the cards.
A deck is called sorted if the front values are in increasing order and the back values are in decreasing order. That is, if $a_i< a_{i+1}$ and $b_i> b_{i+1}$ for all $1\le i<n$.
To flip a card $i$ means swapping the values of $a_i$ and $b_i$. You must flip some subset of cards (possibly, none), then put all the cards in any order you like. What is the minimum number of cards you must flip in order to sort the deck?
-----Input-----
The first line contains a single integer $n$ ($1\le n\le 2\cdot 10^5$) β the number of cards.
The next $n$ lines describe the cards. The $i$-th of these lines contains two integers $a_i, b_i$ ($1\le a_i, b_i\le 2n$). Every integer between $1$ and $2n$ appears exactly once.
-----Output-----
If it is impossible to sort the deck, output "-1". Otherwise, output the minimum number of flips required to sort the deck.
-----Examples-----
Input
5
3 10
6 4
1 9
5 8
2 7
Output
2
Input
2
1 2
3 4
Output
-1
Input
3
1 2
3 6
4 5
Output
-1
-----Note-----
In the first test case, we flip the cards $(1, 9)$ and $(2, 7)$. The deck is then ordered $(3,10), (5,8), (6,4), (7,2), (9,1)$. It is sorted because $3<5<6<7<9$ and $10>8>4>2>1$.
In the second test case, it is impossible to sort the deck.
|
from sys import stdin
def solve():
n = int(stdin.readline())
dic = {}
cards = set()
for i in range(n):
a, b = map(int, stdin.readline().split())
a -= 1
b -= 1
dic[a] = b
dic[b] = a
cards.add((a, b))
top = []
bot = []
used = [False] * (2 * n)
left = 0
right = 2 * n - 1
left_target = 0
right_target = 2 * n
count = 0
final_ans = 0
try:
while count != n:
flag = True
while left < left_target:
flag = False
while used[left]:
left += 1
if left == left_target:
break
else:
a, b = left, dic[left]
top.append((a, b))
used[a] = True
used[b] = True
right_target = min(right_target, b)
count += 1
left += 1
if left == left_target:
break
if len(top) + len(bot) == n:
continue
while right > right_target:
flag = False
while used[right]:
right -= 1
if right == right_target:
break
else:
a, b = right, dic[right]
bot.append((a, b))
used[a] = True
used[b] = True
left_target = max(left_target, b)
count += 1
right -= 1
if right == right_target:
break
if flag:
srt = top + bot[::-1]
for i in range(len(srt) - 1):
if not (srt[i][0] < srt[i + 1][0] and srt[i][1] > srt[i + 1][1]):
return -1
else:
ans = 0
for c in top + bot:
if c in cards:
ans += 1
final_ans += min(ans, len(top) + len(bot) - ans)
left_target += 1
right_target -= 1
top = []
bot = []
if count == n:
srt = top + bot[::-1]
for i in range(len(srt) - 1):
if not (srt[i][0] < srt[i + 1][0] and srt[i][1] > srt[i + 1][1]):
return -1
else:
ans = 0
for c in top + bot:
if c in cards:
ans += 1
final_ans += min(ans, len(top) + len(bot) - ans)
return final_ans
except:
return -1
print(solve())
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR IF VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST IF VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR
|
There is a deck of $n$ cards. The $i$-th card has a number $a_i$ on the front and a number $b_i$ on the back. Every integer between $1$ and $2n$ appears exactly once on the cards.
A deck is called sorted if the front values are in increasing order and the back values are in decreasing order. That is, if $a_i< a_{i+1}$ and $b_i> b_{i+1}$ for all $1\le i<n$.
To flip a card $i$ means swapping the values of $a_i$ and $b_i$. You must flip some subset of cards (possibly, none), then put all the cards in any order you like. What is the minimum number of cards you must flip in order to sort the deck?
-----Input-----
The first line contains a single integer $n$ ($1\le n\le 2\cdot 10^5$) β the number of cards.
The next $n$ lines describe the cards. The $i$-th of these lines contains two integers $a_i, b_i$ ($1\le a_i, b_i\le 2n$). Every integer between $1$ and $2n$ appears exactly once.
-----Output-----
If it is impossible to sort the deck, output "-1". Otherwise, output the minimum number of flips required to sort the deck.
-----Examples-----
Input
5
3 10
6 4
1 9
5 8
2 7
Output
2
Input
2
1 2
3 4
Output
-1
Input
3
1 2
3 6
4 5
Output
-1
-----Note-----
In the first test case, we flip the cards $(1, 9)$ and $(2, 7)$. The deck is then ordered $(3,10), (5,8), (6,4), (7,2), (9,1)$. It is sorted because $3<5<6<7<9$ and $10>8>4>2>1$.
In the second test case, it is impossible to sort the deck.
|
import sys
sys.setrecursionlimit(10**5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.buffer.readline())
def LI():
return list(map(int, sys.stdin.buffer.readline().split()))
def LI1():
return list(map(int1, sys.stdin.buffer.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def LLI1(rows_number):
return [LI1() for _ in range(rows_number)]
def BI():
return sys.stdin.buffer.readline().rstrip()
def SI():
return sys.stdin.buffer.readline().rstrip().decode()
dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = 10**16
md = 10**9 + 7
n = II()
rev = [0] * 2 * n
inc = [False] * 2 * n
ab = []
for _ in range(n):
a, b = LI1()
if a < b:
ab.append((a, b, 0))
else:
ab.append((b, a, 1))
inc = sorted(ab, key=lambda x: -x[0])
dec = sorted(ab, key=lambda x: x[1])
al = []
bl = [2 * n]
ar = []
br = [-1]
ans = 0
fin = [False] * 2 * n
while inc and dec:
c0 = c1 = 0
a, b, t = inc.pop()
if fin[a]:
continue
fin[a] = fin[b] = True
al.append(a)
bl.append(b)
if t == 0:
c0 += 1
else:
c1 += 1
up = True
while up:
up = False
while inc and inc[-1][0] < br[-1]:
a, b, t = inc.pop()
if fin[a]:
continue
fin[a] = fin[b] = True
al.append(a)
bl.append(b)
if t == 0:
c0 += 1
else:
c1 += 1
up = True
while dec and dec[-1][1] > bl[-1]:
a, b, t = dec.pop()
if fin[a]:
continue
fin[a] = fin[b] = True
ar.append(b)
br.append(a)
if t == 0:
c1 += 1
else:
c0 += 1
up = True
ans += min(c0, c1)
def ok():
bb = br + bl[::-1]
return bb == sorted(bb)
if ok():
print(ans)
else:
print(-1)
|
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST BIN_OP NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER VAR WHILE VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
There is a deck of $n$ cards. The $i$-th card has a number $a_i$ on the front and a number $b_i$ on the back. Every integer between $1$ and $2n$ appears exactly once on the cards.
A deck is called sorted if the front values are in increasing order and the back values are in decreasing order. That is, if $a_i< a_{i+1}$ and $b_i> b_{i+1}$ for all $1\le i<n$.
To flip a card $i$ means swapping the values of $a_i$ and $b_i$. You must flip some subset of cards (possibly, none), then put all the cards in any order you like. What is the minimum number of cards you must flip in order to sort the deck?
-----Input-----
The first line contains a single integer $n$ ($1\le n\le 2\cdot 10^5$) β the number of cards.
The next $n$ lines describe the cards. The $i$-th of these lines contains two integers $a_i, b_i$ ($1\le a_i, b_i\le 2n$). Every integer between $1$ and $2n$ appears exactly once.
-----Output-----
If it is impossible to sort the deck, output "-1". Otherwise, output the minimum number of flips required to sort the deck.
-----Examples-----
Input
5
3 10
6 4
1 9
5 8
2 7
Output
2
Input
2
1 2
3 4
Output
-1
Input
3
1 2
3 6
4 5
Output
-1
-----Note-----
In the first test case, we flip the cards $(1, 9)$ and $(2, 7)$. The deck is then ordered $(3,10), (5,8), (6,4), (7,2), (9,1)$. It is sorted because $3<5<6<7<9$ and $10>8>4>2>1$.
In the second test case, it is impossible to sort the deck.
|
def sv():
N = int(input())
lf = [False] * N
mt = [False] * N
for n in range(N):
a, b = map(int, input().split())
sp = False
if a > N:
a, b = b, a
sp = True
if a > N or b <= N:
return -1
a -= 1
b = 2 * N - b
lf[a] = b
if sp:
mt[a] = True
g0 = -1
g1 = -1
gm = -1
cc = 0
cs = 0
ans = 0
for n in range(N):
cc += 1
if lf[n] > g0 and (g0 >= g1 or lf[n] <= g1):
g0 = lf[n]
if mt[n]:
cs += 1
elif lf[n] > g1:
g1 = lf[n]
if not mt[n]:
cs += 1
else:
return -1
gm = max(gm, lf[n])
if gm == n:
ans += min(cs, cc - cs)
cc = 0
cs = 0
g0 = n
g1 = n
return ans
print(sv())
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
There is a deck of $n$ cards. The $i$-th card has a number $a_i$ on the front and a number $b_i$ on the back. Every integer between $1$ and $2n$ appears exactly once on the cards.
A deck is called sorted if the front values are in increasing order and the back values are in decreasing order. That is, if $a_i< a_{i+1}$ and $b_i> b_{i+1}$ for all $1\le i<n$.
To flip a card $i$ means swapping the values of $a_i$ and $b_i$. You must flip some subset of cards (possibly, none), then put all the cards in any order you like. What is the minimum number of cards you must flip in order to sort the deck?
-----Input-----
The first line contains a single integer $n$ ($1\le n\le 2\cdot 10^5$) β the number of cards.
The next $n$ lines describe the cards. The $i$-th of these lines contains two integers $a_i, b_i$ ($1\le a_i, b_i\le 2n$). Every integer between $1$ and $2n$ appears exactly once.
-----Output-----
If it is impossible to sort the deck, output "-1". Otherwise, output the minimum number of flips required to sort the deck.
-----Examples-----
Input
5
3 10
6 4
1 9
5 8
2 7
Output
2
Input
2
1 2
3 4
Output
-1
Input
3
1 2
3 6
4 5
Output
-1
-----Note-----
In the first test case, we flip the cards $(1, 9)$ and $(2, 7)$. The deck is then ordered $(3,10), (5,8), (6,4), (7,2), (9,1)$. It is sorted because $3<5<6<7<9$ and $10>8>4>2>1$.
In the second test case, it is impossible to sort the deck.
|
import sys
mod = 1000000007
eps = 10**-9
def main():
import sys
input = sys.stdin.buffer.readline
N = int(input())
AB = [-1] * (2 * N + 1)
BA = [-1] * (2 * N + 1)
for _ in range(N):
a, b = map(int, input().split())
AB[a] = b
BA[b] = a
seen = [0] * (2 * N + 1)
small = 0
large = 2 * N + 1
top = []
bottom = []
ans = 0
seen_sum = 0
while True:
cnt_all = 0
cnt_flipped = 0
small += 1
cnt_all += 1
if BA[small] != -1:
cnt_flipped += 1
L = BA[small]
else:
L = AB[small]
top.append((small, L))
seen[small] = 1
seen[L] = 1
small_new = small
large_new = L
seen_sum += 2
while small != small_new or large_new != large:
if large_new != large:
for l in range(large - 1, large_new, -1):
if seen[l]:
continue
cnt_all += 1
if BA[l] != -1:
cnt_flipped += 1
s = BA[l]
else:
s = AB[l]
bottom.append((l, s))
seen[s] = 1
seen[l] = 1
seen_sum += 2
small_new = max(small_new, s)
large = large_new
elif small_new != small:
for s in range(small + 1, small_new):
if seen[s]:
continue
cnt_all += 1
if BA[s] != -1:
cnt_flipped += 1
l = BA[s]
else:
l = AB[s]
top.append((s, l))
seen[s] = 1
seen[l] = 1
large_new = min(large_new, l)
seen_sum += 2
small = small_new
if seen_sum == 2 * N:
break
ans += min(cnt_flipped, cnt_all - cnt_flipped)
if seen_sum == 2 * N:
break
bottom.reverse()
top.extend(bottom)
ok = 1
for i in range(N - 1):
if top[i][0] > top[i + 1][0] or top[i][1] < top[i + 1][1]:
ok = 0
break
if ok:
print(ans)
else:
print(-1)
main()
|
IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
|
There is a deck of $n$ cards. The $i$-th card has a number $a_i$ on the front and a number $b_i$ on the back. Every integer between $1$ and $2n$ appears exactly once on the cards.
A deck is called sorted if the front values are in increasing order and the back values are in decreasing order. That is, if $a_i< a_{i+1}$ and $b_i> b_{i+1}$ for all $1\le i<n$.
To flip a card $i$ means swapping the values of $a_i$ and $b_i$. You must flip some subset of cards (possibly, none), then put all the cards in any order you like. What is the minimum number of cards you must flip in order to sort the deck?
-----Input-----
The first line contains a single integer $n$ ($1\le n\le 2\cdot 10^5$) β the number of cards.
The next $n$ lines describe the cards. The $i$-th of these lines contains two integers $a_i, b_i$ ($1\le a_i, b_i\le 2n$). Every integer between $1$ and $2n$ appears exactly once.
-----Output-----
If it is impossible to sort the deck, output "-1". Otherwise, output the minimum number of flips required to sort the deck.
-----Examples-----
Input
5
3 10
6 4
1 9
5 8
2 7
Output
2
Input
2
1 2
3 4
Output
-1
Input
3
1 2
3 6
4 5
Output
-1
-----Note-----
In the first test case, we flip the cards $(1, 9)$ and $(2, 7)$. The deck is then ordered $(3,10), (5,8), (6,4), (7,2), (9,1)$. It is sorted because $3<5<6<7<9$ and $10>8>4>2>1$.
In the second test case, it is impossible to sort the deck.
|
import sys
input = sys.stdin.readline
n = int(input())
front = [-1] * n
back = [-1] * n
rev = [-1] * (2 * n)
used = [False] * n
opp = [-1] * (2 * n)
for c in range(n):
a, b = map(lambda x: int(x) - 1, input().split())
front[c] = a
back[c] = b
rev[a] = c
rev[b] = c
opp[a] = b
opp[b] = a
left = -1
right = 2 * n
out = 0
works = True
while right - left > 1:
s_l = left + 1
s_r = right
curr = 0
curr_f = 0
while (s_l > left or s_r < right) and right - left > 1:
if s_l > left:
left += 1
card = rev[left]
if not used[card]:
used[card] = True
curr += 1
if front[card] == left:
curr_f += 1
if opp[left] < s_r:
s_r = opp[left]
else:
works = False
elif s_r < right:
right -= 1
card = rev[right]
if not used[card]:
used[card] = True
curr += 1
if front[card] == right:
curr_f += 1
if opp[right] > s_l:
s_l = opp[right]
else:
works = False
if s_l > s_r:
works = False
if not works:
break
out += min(curr_f, curr - curr_f)
if works:
print(out)
else:
print(-1)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
There is a deck of $n$ cards. The $i$-th card has a number $a_i$ on the front and a number $b_i$ on the back. Every integer between $1$ and $2n$ appears exactly once on the cards.
A deck is called sorted if the front values are in increasing order and the back values are in decreasing order. That is, if $a_i< a_{i+1}$ and $b_i> b_{i+1}$ for all $1\le i<n$.
To flip a card $i$ means swapping the values of $a_i$ and $b_i$. You must flip some subset of cards (possibly, none), then put all the cards in any order you like. What is the minimum number of cards you must flip in order to sort the deck?
-----Input-----
The first line contains a single integer $n$ ($1\le n\le 2\cdot 10^5$) β the number of cards.
The next $n$ lines describe the cards. The $i$-th of these lines contains two integers $a_i, b_i$ ($1\le a_i, b_i\le 2n$). Every integer between $1$ and $2n$ appears exactly once.
-----Output-----
If it is impossible to sort the deck, output "-1". Otherwise, output the minimum number of flips required to sort the deck.
-----Examples-----
Input
5
3 10
6 4
1 9
5 8
2 7
Output
2
Input
2
1 2
3 4
Output
-1
Input
3
1 2
3 6
4 5
Output
-1
-----Note-----
In the first test case, we flip the cards $(1, 9)$ and $(2, 7)$. The deck is then ordered $(3,10), (5,8), (6,4), (7,2), (9,1)$. It is sorted because $3<5<6<7<9$ and $10>8>4>2>1$.
In the second test case, it is impossible to sort the deck.
|
number_of_cards = int(input())
opposite_side = {}
cards = set()
result = 0
top = []
bottom = []
highest_taken = 2 * number_of_cards + 1
lowest_taken = 0
for _ in range(number_of_cards):
front, back = map(int, input().split())
opposite_side[front] = back
opposite_side[back] = front
cards.add((front, back))
def end():
print(-1)
exit(0)
for i in range(1, 2 * number_of_cards + 1):
if opposite_side[i] == -1:
continue
a, b = i, opposite_side[i]
opposite_side[a], opposite_side[b] = -1, -1
top.append((a, b))
counter = 1
cost = 0 if (a, b) in cards else 1
should_repeat = True
candidate_max = b
candidate_min = a
while should_repeat:
should_repeat = False
while highest_taken > candidate_max:
highest_taken -= 1
if opposite_side[highest_taken] == -1:
continue
should_repeat = True
tmp_a, tmp_b = highest_taken, opposite_side[highest_taken]
opposite_side[tmp_a], opposite_side[tmp_b] = -1, -1
if (
len(bottom) == 0 or tmp_a < bottom[-1][0] and tmp_b > bottom[-1][1]
) and (tmp_a > top[-1][0] and tmp_b < top[-1][1]):
bottom.append((tmp_a, tmp_b))
candidate_min = max(candidate_min, tmp_b)
counter += 1
if (tmp_a, tmp_b) not in cards:
cost += 1
else:
end()
while lowest_taken < candidate_min:
lowest_taken += 1
if opposite_side[lowest_taken] == -1:
continue
should_repeat = True
tmp_a, tmp_b = lowest_taken, opposite_side[lowest_taken]
opposite_side[tmp_a], opposite_side[tmp_b] = -1, -1
if (len(top) == 0 or tmp_a > top[-1][0] and tmp_b < top[-1][1]) and (
len(bottom) == 0 or tmp_a < bottom[-1][0] and tmp_b > bottom[-1][1]
):
top.append((tmp_a, tmp_b))
candidate_max = min(candidate_max, tmp_b)
counter += 1
if (tmp_a, tmp_b) not in cards:
cost += 1
else:
end()
result += min(cost, counter - cost)
print(result)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
def oper(l, m):
q, r = 0, 0
for i in range(len(l)):
if m[i] == "c":
q = max(i, q)
while q < len(l):
if l[q] == "c":
l[i], l[q] = l[q], l[i]
q += 1
break
elif l[q] == "a":
return "NO"
q += 1
if m[i] == "b":
r = max(i, r)
while r < len(l):
if l[r] == "b":
l[i], l[r] = l[r], l[i]
r += 1
break
elif l[r] == "c":
return "NO"
r += 1
if l == m:
return "YES"
return "NO"
t = int(input())
for vmlskv in range(t):
n = int(input())
a = input()
b = input()
l = []
m = []
for i in range(n):
l.append(a[i])
m.append(b[i])
print(oper(l, m))
|
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR STRING RETURN STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR STRING RETURN STRING VAR NUMBER IF VAR VAR RETURN STRING RETURN STRING 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
q = int(input())
while q:
n = int(input())
s = input()
t = input()
flag = 1
if (
s.count("a") != t.count("a")
or s.count("b") != t.count("b")
or s.count("c") != t.count("c")
):
flag = 0
else:
i = 0
j = 0
while i < n:
if s[i] == "b":
i += 1
continue
while t[j] == "b":
j += 1
if s[i] != t[j] or s[i] == "a" and i > j or s[i] == "c" and i < j:
flag = 0
break
i += 1
j += 1
if flag:
print("YES")
else:
print("NO")
q -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR NUMBER WHILE VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR VAR VAR STRING VAR VAR VAR VAR STRING VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
I, P, N = input, print, "NO"
for i in range(int(I())):
n = int(I())
s = I()
t = I()
if s.count("b") != t.count("b"):
P(N)
continue
j, z = 0, "YES"
for i in range(n):
x = s[i]
if x == "b":
continue
while t[j] == "b":
j += 1
y = t[j]
if x != y or x == "a" and i > j or x == "c" and i < j:
z = N
break
j += 1
P(z)
|
ASSIGN VAR VAR VAR VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING WHILE VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR STRING VAR VAR VAR STRING VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
import sys
input = sys.stdin.readline
q = int(input())
for _ in range(q):
n = int(input().strip("\n"))
s, t = input().strip("\n"), input().strip("\n")
s = list(s)
ap, bp, cp = 0, 0, 0
for j in range(n):
ap = max(ap, j + 1)
while ap < n and s[ap] != "a":
ap += 1
bp = max(bp, j + 1)
while bp < n and s[bp] != "b":
bp += 1
cp = max(cp, j + 1)
while cp < n and s[cp] != "c":
cp += 1
if s[j] != t[j]:
if s[j] == "a":
if t[j] == "b":
if bp == n or cp < bp:
print("NO")
break
else:
s[bp], s[j] = s[j], s[bp]
bp += 1
while bp < n and s[bp] != "b":
bp += 1
else:
print("NO")
break
elif s[j] == "b":
if t[j] == "c":
if cp == n or ap < cp:
print("NO")
break
else:
s[cp], s[j] = s[j], s[cp]
cp += 1
while cp < n and s[cp] != "c":
cp += 1
else:
print("NO")
break
else:
print("NO")
break
else:
print("YES")
|
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 FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR STRING IF VAR VAR STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR STRING IF VAR VAR STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
x = int(input())
def cal(n, s, t):
if s == t:
return True
p, q = list(s), list(t)
i = n - 1
a, b = n - 1, n - 1
while i >= 0:
if q[i] == p[i]:
i -= 1
continue
if q[i] == "c":
return False
if q[i] == "b":
ck = 0
for j in range(min(i, b), -1, -1):
if p[j] == "a":
return False
elif p[j] == "b":
b = j
p[j], p[i] = p[i], p[j]
ck = 1
break
if ck == 0:
return False
else:
ck = 0
for j in range(min(i, a), -1, -1):
if p[j] == "c":
return False
elif p[j] == "a":
a = j
p[j], p[i] = p[i], p[j]
ck = 1
break
if ck == 0:
return False
i -= 1
return True
for jj in range(x):
n = int(input())
s, t = input(), input()
if cal(n, s, t):
print("YES")
else:
print("NO")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR STRING RETURN NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR STRING RETURN NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR STRING RETURN NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
TT = int(input())
while TT:
n = int(input())
s1 = input()
s2 = input()
SCA1 = []
SCA2 = []
for i in range(n):
if s1[i] != "b":
SCA1.append([s1[i], i])
if s2[i] != "b":
SCA2.append([s2[i], i])
flag = 1
if len(SCA1) != len(SCA2):
flag = 0
else:
for i in range(len(SCA1)):
if SCA1[i][0] != SCA2[i][0]:
flag = 0
break
elif SCA1[i][0] == "a":
if SCA1[i][1] > SCA2[i][1]:
flag = 0
break
elif SCA1[i][1] < SCA2[i][1]:
flag = 0
break
if flag:
print("YES")
else:
print("NO")
TT -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER STRING IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
a = int(input())
for y in range(a):
b = int(input())
c = input()
d = input()
if (
c.count("b") == d.count("b")
and c.count("a") == d.count("a")
and c.count("c") == d.count("c")
):
j = []
h = []
i = []
k = []
f = 0
for y in range(b):
if c[y] == "a" or c[y] == "c":
j.append([c[y], y])
i.append(c[y])
if d[y] == "a" or d[y] == "c":
h.append([d[y], y])
k.append(d[y])
if i != k:
print("NO")
else:
s = len(j)
for y in range(s):
if j[y][0] == "a":
if j[y][1] > h[y][1]:
f = -1
break
elif j[y][1] < h[y][1]:
f = -1
break
if f == -1:
print("NO")
else:
print("YES")
else:
print("NO")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
def solve():
n = int(input())
p = input()
q = input()
i, j = 0, 0
while i < n or j < n:
while i < n and p[i] == "b":
i += 1
while j < n and q[j] == "b":
j += 1
if i == n and j == n:
break
if i < n and j == n or j < n and i == n:
print("NO")
return
if p[i] != q[j] or p[i] == "a" and j < i or p[i] == "c" and j > i:
print("NO")
return
i += 1
j += 1
print("YES")
t = int(input())
for _ in range(t):
solve()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR VAR VAR VAR VAR STRING VAR VAR VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR STRING RETURN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
x = int(input())
while x > 0:
x -= 1
n = int(input())
s = str(input())
t = str(input())
s = list(s)
t = list(t)
y, u = 0, 0
ans = "YES"
if (
s.count("a") != t.count("a")
or s.count("b") != t.count("b")
or s.count("c") != t.count("c")
):
print("NO")
continue
for i in range(0, n):
if s[i] == t[i]:
continue
elif s[i] == "a":
if i == n - 1 or t[i] != "b":
ans = "NO"
break
else:
j = max(y, i + 1)
while j < n and s[j] == "a":
j += 1
y = j
if j == n or s[j] != "b":
ans = "NO"
break
s[i], s[j] = s[j], s[i]
elif s[i] == "b":
if i == n - 1 or t[i] != "c":
ans = "NO"
break
else:
j = max(u, i + 1)
while j < n and s[j] == "b":
j += 1
u = j
if j == n or s[j] != "c":
ans = "NO"
break
s[i], s[j] = s[j], s[i]
else:
ans = "NO"
break
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER 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 FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR STRING IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR STRING ASSIGN VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR STRING ASSIGN VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
for _ in range(int(input())):
n = int(input())
s = input()
t = input()
sac = []
sa, sc, ta, tc = 0, 0, 0, 0
tac = []
ans = "YES"
for i in range(n):
if s[i] != "b":
sac.append(s[i])
if t[i] != "b":
tac.append(t[i])
if s[i] == "a":
sa += 1
if s[i] == "c":
sc += 1
if t[i] == "a":
ta += 1
if t[i] == "c":
tc += 1
if ta > sa or sc > tc:
ans = "NO"
break
if sac == tac and sa == ta and ans == "YES":
print(ans)
else:
print("NO")
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR STRING IF VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
def pp(s: str, t: str, n: int) -> str:
if (
s.count("a") != t.count("a")
or s.count("b") != t.count("b")
or s.count("c") != t.count("c")
):
return "NO"
p1, p2 = 0, 0
while p1 < n and p2 < n:
while p1 < n and s[p1] == "b":
p1 += 1
if p1 == n:
break
while p2 < n and t[p2] == "b":
p2 += 1
if s[p1] != t[p2]:
return "NO"
if s[p1] == "a":
if p1 > p2:
return "NO"
elif p1 < p2:
return "NO"
p1 += 1
p2 += 1
return "YES"
n = int(input())
for i in range(n):
tt = int(input())
s, t = input(), input()
print(pp(s, t, tt))
|
FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING RETURN STRING ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR RETURN STRING IF VAR VAR STRING IF VAR VAR RETURN STRING IF VAR VAR RETURN STRING VAR NUMBER VAR NUMBER RETURN STRING 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 VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
t = int(input())
for i in range(t):
n = int(input())
sa = input()
sb = input()
lsta = []
lstb = []
for j in range(n):
if sa[j] != "b":
lsta.append((sa[j], j))
if sb[j] != "b":
lstb.append((sb[j], j))
e = 0
if len(lsta) != len(lstb):
print("NO")
else:
l = len(lsta)
for j in range(l):
if lsta[j][0] == "a":
if lstb[j][0] == "a":
if lstb[j][1] < lsta[j][1]:
e += 1
break
else:
e += 1
break
elif lsta[j][0] == "c":
if lsta[j][0] == "c":
if lstb[j][0] == "c":
if lstb[j][1] > lsta[j][1]:
e += 1
break
else:
e += 1
break
else:
e += 1
break
if e == 0:
print("YES")
else:
print("NO")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING IF VAR VAR NUMBER STRING IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER STRING IF VAR VAR NUMBER STRING IF VAR VAR NUMBER STRING IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
def solve(arr_a, arr_b):
a, b, c = 0, 0, 0
ba, bb = [], []
ca, cb = [], []
cba, cbb = [], []
for i in arr_a:
if i == "a":
a += 1
ba.append(b)
ca.append(c)
if i == "b":
b += 1
cba.append(c)
if i == "c":
c += 1
a2, b2, c2 = 0, 0, 0
for i in arr_b:
if i == "a":
a2 += 1
bb.append(b2)
cb.append(c2)
if i == "b":
b2 += 1
cbb.append(c2)
if i == "c":
c2 += 1
if a != a2 or b != b2 or c != c2:
return False
for i in range(len(ba)):
if ba[i] > bb[i] or ca[i] != cb[i]:
return False
for i in range(len(cba)):
if cba[i] > cbb[i]:
return False
return True
t = int(input())
for i in range(t):
n = int(input())
arr_a = input()
arr_b = input()
if solve(arr_a, arr_b):
print("YES")
else:
print("NO")
|
FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR LIST LIST FOR VAR VAR IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER 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 ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
iter = int(input())
for z in range(iter):
n = int(input())
a = input()
b = input()
top = 0
ans = 1
for i in range(n):
if b[i] == "a":
top -= 1
if a[i] == "a":
top += 1
if top < 0:
ans = 0
break
if top and a[i] == "c":
ans = 0
break
if top > 0:
ans = 0
if ans:
top = 0
for i in range(n - 1, -1, -1):
if b[i] == "c":
top -= 1
if a[i] == "c":
top += 1
if top < 0:
ans = 0
break
if top and b[i] == "a":
ans = 0
break
if top > 0:
ans = 0
if ans:
print("YES")
else:
print("NO")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
import sys
input = sys.stdin.readline
def solve():
n = int(input())
s = list(input().strip())
t = input().strip()
idx1 = start = 0
while idx1 < n:
c1, c2 = s[idx1], t[idx1]
if c1 == c2:
idx1 += 1
continue
if c1 == "c":
return "NO"
elif c1 == "a":
if c2 == "c":
return "NO"
j = max(start, idx1 + 1)
while j < n and s[j] != "b":
if s[j] == "c":
return "NO"
j += 1
if j == n:
return "NO"
s[idx1], s[j] = s[j], s[idx1]
start = j + 1
else:
if c2 == "a":
return "NO"
j = max(start, idx1 + 1)
while j < n and s[j] != "c":
if s[j] == "a":
return "NO"
j += 1
if j == n:
return "NO"
s[idx1], s[j] = s[j], s[idx1]
start = j + 1
idx1 += 1
if s[-1] != t[-1]:
return "NO"
return "YES"
for _ in range(int(input())):
print(solve())
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR STRING RETURN STRING IF VAR STRING IF VAR STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR STRING IF VAR VAR STRING RETURN STRING VAR NUMBER IF VAR VAR RETURN STRING ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR STRING IF VAR VAR STRING RETURN STRING VAR NUMBER IF VAR VAR RETURN STRING ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
for _ in range(int(input())):
n = int(input())
s = input()
t = input()
s1 = sorted(s)
t1 = sorted(t)
if s1 != t1:
print("NO")
elif s == t:
print("YES")
else:
a = 0
c = 0
flag = True
for x in range(n):
if s[x] == t[x] and s[x] == "b":
continue
else:
if a != 0 and t[x] == "c" or c != 0 and s[x] == "a":
flag = False
break
if s[x] == "a" and t[x] == "b":
a += 1
elif s[x] == "b" and t[x] == "c":
c += 1
elif s[x] == "b" and t[x] == "a":
a -= 1
if a < 0:
flag = False
break
elif s[x] == "c" and t[x] == "b":
c -= 1
if c < 0:
flag = False
break
elif s[x] == "a" and t[x] == "c" or s[x] == "c" and t[x] == "a":
flag = False
break
if flag:
print("YES")
else:
print("NO")
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR STRING IF VAR NUMBER VAR VAR STRING VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
def solve():
n = int(input())
s = input()
t = input()
if (
t.count("a") != s.count("a")
or t.count("b") != s.count("b")
or t.count("c") != s.count("c")
):
print("NO\t")
return
j = 0
for i in range(n):
if s[i] == "b":
continue
while t[j] == "b":
j += 1
if s[i] != t[j] or s[i] == "a" and i > j or s[i] == "c" and i < j:
print("NO\t")
return
j += 1
print("YES\t")
for i in range(int(input())):
solve()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING WHILE VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR VAR VAR STRING VAR VAR VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR STRING RETURN VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
import itertools
for _ in range(int(input())):
n = int(input())
s = input()
t = input()
ls = []
lt = []
at_count = 0
ct_count = 0
as_count = 0
cs_count = 0
flag = False
for chars, chart in zip(s, t):
if chars == "a":
ls.append(0)
as_count += 1
elif chars == "c":
ls.append(1)
cs_count += 1
if chart == "a":
lt.append(0)
at_count += 1
elif chart == "c":
lt.append(1)
ct_count += 1
if at_count > as_count or cs_count > ct_count:
flag = True
if not flag:
if ls == lt:
print("YES")
else:
print("NO")
else:
print("NO")
|
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
import sys
def do_count(s, char):
counter = 0
arr = []
for c in s:
if c == char:
arr.append(counter)
counter = 0
else:
counter += 1
arr.append(counter)
return arr
for _ in range(int(input())):
input()
s = input()
t = input()
valid = True
for c in "abc":
if s.count(c) != t.count(c):
valid = False
if not valid:
print("NO")
continue
if "".join(c for c in s if c != "b") != "".join(c for c in t if c != "b"):
print("NO")
continue
arrsa = do_count(s, "a")
arrta = do_count(t, "a")
arrsc = do_count(s, "c")
arrtc = do_count(t, "c")
print(arrsa, arrta, file=sys.stderr)
print(arrsc, arrtc, file=sys.stderr)
sums = 0
sumt = 0
for i in range(len(arrsa)):
sums += arrsa[i]
sumt += arrta[i]
if sums > sumt:
valid = False
sums = 0
sumt = 0
for i in range(len(arrtc)):
sums += arrsc[i]
sumt += arrtc[i]
if sums < sumt:
valid = False
if not valid:
print("NO")
else:
print("YES")
|
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL STRING VAR VAR VAR VAR STRING FUNC_CALL STRING VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
for _ in range(int(input())):
n = int(input())
s = input()
t = input()
if s.count("b") != t.count("b"):
print("NO")
continue
j = 0
for i in range(n):
if s[i] == "b":
continue
while t[j] == "b":
j += 1
if s[i] != t[j] or s[i] == "a" and i > j or s[i] == "c" and i < j:
print("NO")
break
j += 1
else:
print("YES")
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING WHILE VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR VAR VAR STRING VAR VAR VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
g = int(input())
def logic(n, s, t):
s2 = [c for c in s if c != "b"]
t2 = [c for c in t if c != "b"]
if s2 != t2:
return "NO"
s_a = []
s_c = []
t_a = []
t_c = []
for i in range(n):
if s[i] == "a":
s_a.append(i)
elif s[i] == "c":
s_c.append(i)
if t[i] == "a":
t_a.append(i)
elif t[i] == "c":
t_c.append(i)
for i in range(len(s_a)):
if s_a[i] > t_a[i]:
return "NO"
for i in range(len(s_c)):
if s_c[i] < t_c[i]:
return "NO"
return "YES"
for a in range(g):
n = int(input())
s = input()
t = input()
print(logic(n, s, t))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR STRING IF VAR VAR RETURN STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
q = int(input())
for _ in range(q):
n = int(input())
s = input().strip()
t = input().strip()
stack_s = 0
stack_t = 0
for s_c, t_c in zip(s, t):
if s_c == "a":
if stack_t == 0:
stack_s += 1
else:
break
if t_c == "a":
if stack_s > 0 and stack_t == 0:
stack_s -= 1
else:
break
if t_c == "c":
if stack_s == 0:
stack_t += 1
else:
break
if s_c == "c":
if stack_s == 0 and stack_t > 0:
stack_t -= 1
else:
break
else:
if stack_s == stack_t == 0:
print("YES")
continue
print("NO")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR STRING IF VAR NUMBER VAR NUMBER IF VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR STRING IF VAR NUMBER VAR NUMBER IF VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
def solve():
n = int(input())
s, t = input(), input()
A, B = "", ""
aa, ac, ba, bc = 0, 0, 0, 0
for i in range(n):
if s[i] == "a":
aa += 1
if s[i] == "c":
ac += 1
if t[i] == "a":
ba += 1
if t[i] == "c":
bc += 1
if ba > aa or bc < ac:
print("NO")
return
if s[i] != "b":
A += s[i]
if t[i] != "b":
B += t[i]
print(("YES", "NO")[A != B])
for T in range(int(input())):
solve()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR STRING STRING ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR STRING VAR VAR VAR IF VAR VAR STRING VAR VAR VAR EXPR FUNC_CALL VAR STRING STRING VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
def check(s):
coun = [0, 0, 0]
for i in s:
coun[ord(i) - ord("a")] += 1
return coun
q = int(input())
for test in range(q):
n = int(input())
s = list(input())
t = list(input())
flag = 0
b = 0
c = 0
if check(s) != check(t):
flag = 1
print("NO")
else:
i = 0
while i < n:
if s[i] == t[i]:
i += 1
continue
else:
if i == n - 1:
flag = 1
print("NO")
break
elif s[i] == "a" and t[i] == "b":
start = max(i, b)
while start < n and s[start] == "a":
start += 1
if start == n or s[start] == "c":
flag = 1
print("NO")
break
s[start] = "a"
b = start + 1
elif s[i] == "b" and t[i] == "c":
start = max(i, c)
while start < n and s[start] == "b":
start += 1
if start == n or s[start] == "a":
flag = 1
print("NO")
break
s[start] = "b"
c = start + 1
else:
flag = 1
print("NO")
break
i += 1
if flag == 0:
print("YES")
|
FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER RETURN 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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR STRING VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
def mi():
return map(int, input().split())
def li():
return list(mi())
def si():
return str(input())
def ni():
return int(input())
for T in range(int(input())):
n = ni()
s = list(si())
t = list(si())
flag = True
i = 0
a1 = 0
a2 = 0
c1 = 0
c2 = 0
flag = True
d1 = ""
d2 = ""
for i in s:
if i != "b":
d1 += i
for i in t:
if i != "b":
d2 += i
if d1 != d2:
flag = False
for i in range(n):
if c1 > c2:
flag = False
break
if s[i] == "c":
c1 += 1
if t[i] == "c":
c2 += 1
s.reverse()
t.reverse()
for i in range(n):
if a1 > a2:
flag = False
break
if s[i] == "a":
a1 += 1
if t[i] == "a":
a2 += 1
if flag:
print("YES")
else:
print("NO")
|
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR VAR FOR VAR VAR IF VAR STRING VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
Z = int(input())
while Z != 0:
N = int(input())
S = str(input())
T = str(input())
result = True
test1 = ""
test2 = ""
for i in range(len(S)):
if S[i] != "b":
test1 += S[i]
if T[i] != "b":
test2 += T[i]
count_c_S = 0
count_c_T = 0
for i in range(len(S)):
if count_c_S > count_c_T:
result = False
break
if S[i] == "c":
count_c_S += 1
if T[i] == "c":
count_c_T += 1
S = S[::-1]
T = T[::-1]
count_a_S = 0
count_a_T = 0
for i in range(len(S)):
if count_a_S > count_a_T:
result = False
break
if S[i] == "a":
count_a_S += 1
if T[i] == "a":
count_a_T += 1
if result == True and test2 == test1:
print("YES")
else:
print("NO")
Z -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER 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 STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR IF VAR VAR STRING VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
for _ in range(int(input())):
n = int(input())
s = input()
t = input()
numA = 0
numC = 0
ans = s.replace("b", "") == t.replace("b", "")
for i in range(n):
numA += (s[i] == "a") - (t[i] == "a")
numC += (t[i] == "c") - (s[i] == "c")
if numA < 0 or numC < 0:
ans = False
print("YES" if ans else "NO")
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING STRING FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR VAR STRING VAR VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
class Solution:
def solve(self, n, s, t):
si_list = []
ti_list = []
for i in range(n):
if s[i] != "b":
si_list.append(s[i])
if t[i] != "b":
ti_list.append(t[i])
if si_list != ti_list:
return False
csi_index = []
cti_index = []
asi_index = []
ati_index = []
for i in range(n):
if s[i] == "c":
csi_index.append(i)
elif s[i] == "a":
asi_index.append(i)
if t[i] == "c":
cti_index.append(i)
elif t[i] == "a":
ati_index.append(i)
for i in range(len(csi_index)):
if csi_index[i] < cti_index[i]:
return False
for i in range(len(asi_index)):
if ati_index[i] < asi_index[i]:
return False
return True
sol = Solution()
c = int(input())
while c > 0:
n = int(input())
s = input()
t = input()
flag = sol.solve(n, s, t)
if flag == True:
print("YES")
else:
print("NO")
c -= 1
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
def solve():
n = int(input())
s = input()
t = input()
s1 = []
t1 = []
ss1 = []
tt1 = []
for i in range(n):
if s[i] == "a" or s[i] == "c":
s1.append(s[i])
ss1.append(i)
if t[i] == "a" or t[i] == "c":
t1.append(t[i])
tt1.append(i)
if len(s1) != len(t1):
print("NO")
return
for i in range(len(s1)):
if s1[i] != t1[i]:
print("NO")
return
if s1[i] == "a" and ss1[i] > tt1[i]:
print("NO")
return
if s1[i] == "c" and ss1[i] < tt1[i]:
print("NO")
return
print("YES")
q = int(input())
while q:
solve()
q -= 1
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR STRING VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR STRING VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
q = int(input())
for _ in range(q):
n = int(input())
s = input()
t = input()
ans = "YES"
cur = 0
for i in range(n - 1):
if s[i] == t[i]:
continue
if t[i] == "a":
ans = "NO"
break
elif t[i] == "b":
try:
ind = s.index("b", i + 1)
except ValueError:
ans = "NO"
break
if s[i:ind] == "a" * (ind - i):
s = s[:i] + "b" + "a" * (ind - i) + s[ind + 1 :]
else:
ans = "NO"
break
else:
try:
ind = s.index("c", i + 1)
except ValueError:
ans = "NO"
break
if s[i:ind] == "b" * (ind - i):
s = s[:i] + "c" + "b" * (ind - i) + s[ind + 1 :]
else:
ans = "NO"
break
if s[n - 1] != t[n - 1]:
ans = "NO"
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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR STRING IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR ASSIGN VAR STRING IF VAR VAR VAR BIN_OP STRING BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR STRING BIN_OP STRING BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR ASSIGN VAR STRING IF VAR VAR VAR BIN_OP STRING BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR STRING BIN_OP STRING BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
|
You are given two strings $s$ and $t$, both of length $n$. Each character in both string is 'a', 'b' or 'c'.
In one move, you can perform one of the following actions:
choose an occurrence of "ab" in $s$ and replace it with "ba";
choose an occurrence of "bc" in $s$ and replace it with "cb".
You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $s$ to make it equal to string $t$?
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 10^5$) β the length of strings $s$ and $t$.
The second line contains string $s$ of length $n$. Each character is 'a', 'b' or 'c'.
The third line contains string $t$ of length $n$. Each character is 'a', 'b' or 'c'.
The sum of $n$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase, print "YES" if you can change string $s$ to make it equal to string $t$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".
-----Examples-----
Input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
Output
YES
NO
YES
YES
NO
-----Note-----
None
|
T = int(input())
while T > 0:
n = int(input())
s = input()
t = input()
ptr1 = ptr2 = 0
ans = True
while True:
while ptr1 < n and s[ptr1] == "b":
ptr1 += 1
while ptr2 < n and t[ptr2] == "b":
ptr2 += 1
if ptr1 == n and ptr2 == n:
break
if ptr1 == n or ptr2 == n or s[ptr1] != t[ptr2]:
ans = False
break
if s[ptr1] == "a" and ptr1 > ptr2 or s[ptr1] == "c" and ptr1 < ptr2:
ans = False
break
ptr1 += 1
ptr2 += 1
if ptr1 != n or ptr2 != n:
ans = False
if ans:
print("YES")
else:
print("NO")
T -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR VAR VAR STRING VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER
|
You are given an array a consisting of n integers.
Let min(l, r) be the minimum value among a_l, a_{l + 1}, β¦, a_r and max(l, r) be the maximum value among a_l, a_{l + 1}, β¦, a_r.
Your task is to choose three positive (greater than 0) integers x, y and z such that:
* x + y + z = n;
* max(1, x) = min(x + 1, x + y) = max(x + y + 1, n).
In other words, you have to split the array a into three consecutive non-empty parts that cover the whole array and the maximum in the first part equals the minimum in the second part and equals the maximum in the third part (or determine it is impossible to find such a partition).
Among all such triples (partitions), you can choose any.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (3 β€ n β€ 2 β
10^5) β the length of a.
The second line of the test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9), where a_i is the i-th element of a.
It is guaranteed that the sum of n does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each test case, print the answer: NO in the only line if there is no such partition of a that satisfies the conditions from the problem statement. Otherwise, print YES in the first line and three integers x, y and z (x + y + z = n) in the second line.
If there are several answers, you can print any.
Example
Input
6
11
1 2 3 3 3 4 4 3 4 2 1
8
2 9 1 7 3 9 4 1
9
2 1 4 2 4 3 3 1 2
7
4 2 1 1 4 1 4
5
1 1 1 1 1
7
4 3 4 3 3 3 4
Output
YES
6 1 4
NO
YES
2 5 2
YES
4 1 2
YES
1 1 3
YES
2 1 4
|
import sys
input = sys.stdin.readline
def format(a, b=0, n=0):
if a == -1:
return "NO"
else:
return "YES\n{} {} {}".format(a, b - a, n - b)
def solve():
n = int(input())
ar = [int(t) for t in input().split()]
rmq = RangeQuery(ar)
fmax = max(ar)
idxs = [i for i in range(n) if ar[i] == fmax]
if len(idxs) >= 3:
mid = idxs[1]
return format(mid, mid + 1, n)
front = []
cmax, event = 0, 0
for i in range(n):
x = ar[i]
if x > cmax:
front += [(cmax, event)]
cmax = x
event = i
elif x < cmax:
event = i
back = []
cmax, event = 0, n
for i in reversed(range(n)):
x = ar[i]
if x > cmax:
back += [(cmax, event)]
cmax = x
event = i
elif x < cmax:
event = i
fit = iter(front)
bit = iter(back)
f = next(fit, False)
b = next(bit, False)
while f and b:
if f[0] == b[0]:
if f[0] == rmq.query(f[1] + 1, b[1]):
return format(f[1] + 1, b[1], n)
else:
f = next(fit, False)
b = next(bit, False)
elif f[0] < b[0]:
f = next(fit, False)
else:
b = next(bit, False)
return format(-1)
def main():
T = int(input())
ans = []
for _ in range(T):
ans += [solve()]
print(*ans, sep="\n")
class RangeQuery:
def __init__(self, data, func=min):
self.func = func
self._data = _data = [list(data)]
i, n = 1, len(_data[0])
while 2 * i <= n:
prev = _data[-1]
_data.append([func(prev[j], prev[j + i]) for j in range(n - 2 * i + 1)])
i <<= 1
def query(self, start, stop):
depth = (stop - start).bit_length() - 1
return self.func(
self._data[depth][start], self._data[depth][stop - (1 << depth)]
)
def __getitem__(self, idx):
return self._data[0][idx]
main()
|
IMPORT ASSIGN VAR VAR FUNC_DEF NUMBER NUMBER IF VAR NUMBER RETURN STRING RETURN FUNC_CALL STRING VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR ASSIGN VAR VAR LIST FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER WHILE BIN_OP NUMBER VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR FUNC_DEF RETURN VAR NUMBER VAR EXPR FUNC_CALL VAR
|
You are given an array a consisting of n integers.
Let min(l, r) be the minimum value among a_l, a_{l + 1}, β¦, a_r and max(l, r) be the maximum value among a_l, a_{l + 1}, β¦, a_r.
Your task is to choose three positive (greater than 0) integers x, y and z such that:
* x + y + z = n;
* max(1, x) = min(x + 1, x + y) = max(x + y + 1, n).
In other words, you have to split the array a into three consecutive non-empty parts that cover the whole array and the maximum in the first part equals the minimum in the second part and equals the maximum in the third part (or determine it is impossible to find such a partition).
Among all such triples (partitions), you can choose any.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (3 β€ n β€ 2 β
10^5) β the length of a.
The second line of the test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9), where a_i is the i-th element of a.
It is guaranteed that the sum of n does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each test case, print the answer: NO in the only line if there is no such partition of a that satisfies the conditions from the problem statement. Otherwise, print YES in the first line and three integers x, y and z (x + y + z = n) in the second line.
If there are several answers, you can print any.
Example
Input
6
11
1 2 3 3 3 4 4 3 4 2 1
8
2 9 1 7 3 9 4 1
9
2 1 4 2 4 3 3 1 2
7
4 2 1 1 4 1 4
5
1 1 1 1 1
7
4 3 4 3 3 3 4
Output
YES
6 1 4
NO
YES
2 5 2
YES
4 1 2
YES
1 1 3
YES
2 1 4
|
import sys
from sys import stdin
class SegTree:
def __init__(self, N, first):
self.NO = 2 ** (N - 1).bit_length()
self.First = first
self.data = [first] * (2 * self.NO)
def calc(self, l, r):
return min(l, r)
def update(self, ind, x):
ind += self.NO - 1
self.data[ind] = x
while ind >= 0:
ind = (ind - 1) // 2
self.data[ind] = self.calc(self.data[2 * ind + 1], self.data[2 * ind + 2])
def query(self, l, r):
L = l + self.NO
R = r + self.NO
s = self.First
while L < R:
if R & 1:
R -= 1
s = self.calc(s, self.data[R - 1])
if L & 1:
s = self.calc(s, self.data[L - 1])
L += 1
L >>= 1
R >>= 1
return s
def get(self, ind):
ind += self.NO - 1
return self.data[ind]
tt = int(stdin.readline())
for loop in range(tt):
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
lmax = [float("-inf")]
for i in range(n - 1):
lmax.append(max(lmax[-1], a[i]))
rmax = [float("-inf")]
for i in range(n - 2, -1, -1):
rmax.append(max(rmax[-1], a[i]))
rmax.reverse()
ST = SegTree(n, float("inf"))
for i in range(n):
ST.update(i, a[i])
dic = {}
lis = []
for i in a:
if i not in dic:
dic[i] = 0
dic[i] += 1
if dic[i] == 3:
lis.append(i)
lis.sort()
lind = 0
rind = n - 1
for X in lis:
LX = 0
RX = 0
while lind < n and a[lind] <= X:
if a[lind] == X:
LX += 1
lind += 1
while rind >= 0 and a[rind] <= X:
if a[rind] == X:
RX += 1
rind -= 1
if lind > rind:
for i in range(1, n - 1):
if a[i] == lmax[i] == rmax[i] == X:
print("YES")
print(i, 1, n - i - 1)
break
else:
print("NO")
break
mmin = ST.query(lind, rind + 1)
if mmin < X or LX == 0 or RX == 0:
continue
elif mmin == X:
print("YES")
print(lind, rind - lind + 1, n - rind - 1)
break
elif LX >= 2 and a[lind - 1] == X:
print("YES")
print(lind - 1, rind - lind + 2, n - rind - 1)
break
elif RX >= 2 and a[rind + 1] == X:
print("YES")
print(lind, rind - lind + 2, n - rind - 2)
break
else:
print("NO")
|
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_DEF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF VAR BIN_OP VAR NUMBER RETURN 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR LIST FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
|
You are given an array a consisting of n integers.
Let min(l, r) be the minimum value among a_l, a_{l + 1}, β¦, a_r and max(l, r) be the maximum value among a_l, a_{l + 1}, β¦, a_r.
Your task is to choose three positive (greater than 0) integers x, y and z such that:
* x + y + z = n;
* max(1, x) = min(x + 1, x + y) = max(x + y + 1, n).
In other words, you have to split the array a into three consecutive non-empty parts that cover the whole array and the maximum in the first part equals the minimum in the second part and equals the maximum in the third part (or determine it is impossible to find such a partition).
Among all such triples (partitions), you can choose any.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (3 β€ n β€ 2 β
10^5) β the length of a.
The second line of the test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9), where a_i is the i-th element of a.
It is guaranteed that the sum of n does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each test case, print the answer: NO in the only line if there is no such partition of a that satisfies the conditions from the problem statement. Otherwise, print YES in the first line and three integers x, y and z (x + y + z = n) in the second line.
If there are several answers, you can print any.
Example
Input
6
11
1 2 3 3 3 4 4 3 4 2 1
8
2 9 1 7 3 9 4 1
9
2 1 4 2 4 3 3 1 2
7
4 2 1 1 4 1 4
5
1 1 1 1 1
7
4 3 4 3 3 3 4
Output
YES
6 1 4
NO
YES
2 5 2
YES
4 1 2
YES
1 1 3
YES
2 1 4
|
for _ in range(int(input())):
N = int(input())
X = list(map(int, input().split()))
M = [-1] * N
mx = 0
INF = 10
for i in range(N - 1, -1, -1):
mx = max(X[i], mx)
M[i] = mx
M2 = [-1] * N
mx = 0
d = dict()
Y = [-1] * N
Z = [-1] * N
stack = []
for i in range(N):
mx = max(X[i], mx)
M2[i] = mx
for i in range(N - 1, -1, -1):
if M2[i] in d:
Y[i] = d[M2[i]]
d[X[i]] = i
for i in range(N - 1, -1, -1):
while stack and stack[-1][1] >= X[i]:
stack.pop()
if stack:
Z[i] = stack[-1][0]
stack.append((i, X[i]))
Z2 = [-1] * N
stack = []
R = []
for i in range(N - 1, -1, -1):
while stack and stack[-1][1] >= M2[i]:
stack.pop()
if stack:
Z2[i] = stack[-1][0]
stack.append((i, X[i]))
for i in range(N):
if Y[i] == -1:
continue
if i < Z2[i] < Y[i]:
continue
if Z[Y[i]] == -1:
if M2[i] == X[-1] and Y[i] != N - 1:
R.append((i + 1, N - (i + 2), 1))
continue
if M[Z[Y[i]]] == M2[i]:
R.append((i + 1, Z[Y[i]] - i - 1, N - Z[Y[i]]))
if Y[i] != Z[Y[i]] - 1 and M[Z[Y[i]] - 1] == M2[i]:
R.append((i + 1, Z[Y[i]] - i - 2, N - Z[Y[i]] + 1))
if R:
print("YES")
print(*R[-1])
else:
print("NO")
|
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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
|
You are given an array a consisting of n integers.
Let min(l, r) be the minimum value among a_l, a_{l + 1}, β¦, a_r and max(l, r) be the maximum value among a_l, a_{l + 1}, β¦, a_r.
Your task is to choose three positive (greater than 0) integers x, y and z such that:
* x + y + z = n;
* max(1, x) = min(x + 1, x + y) = max(x + y + 1, n).
In other words, you have to split the array a into three consecutive non-empty parts that cover the whole array and the maximum in the first part equals the minimum in the second part and equals the maximum in the third part (or determine it is impossible to find such a partition).
Among all such triples (partitions), you can choose any.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (3 β€ n β€ 2 β
10^5) β the length of a.
The second line of the test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9), where a_i is the i-th element of a.
It is guaranteed that the sum of n does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each test case, print the answer: NO in the only line if there is no such partition of a that satisfies the conditions from the problem statement. Otherwise, print YES in the first line and three integers x, y and z (x + y + z = n) in the second line.
If there are several answers, you can print any.
Example
Input
6
11
1 2 3 3 3 4 4 3 4 2 1
8
2 9 1 7 3 9 4 1
9
2 1 4 2 4 3 3 1 2
7
4 2 1 1 4 1 4
5
1 1 1 1 1
7
4 3 4 3 3 3 4
Output
YES
6 1 4
NO
YES
2 5 2
YES
4 1 2
YES
1 1 3
YES
2 1 4
|
import sys
rmq = []
def query(a, b):
ii = len(bin(b - a)) - 3
return min(rmq[ii][a], rmq[ii][b - (1 << ii) + 1])
for ttt in range(int(sys.stdin.readline())):
n = int(sys.stdin.readline())
d = [int(i) for i in sys.stdin.readline().split()]
lgn = len(bin(n - 1)) - 2
rmq = [([0] * n) for i in range(lgn)]
for j in range(n):
rmq[0][j] = d[j]
for i in range(1, lgn):
for j in range(n):
rmq[i][j] = min(rmq[i - 1][j], rmq[i - 1][min(n - 1, j + (1 << i - 1))])
mx1 = [0] * n
mx1[0] = d[0]
for i in range(1, n):
mx1[i] = max(d[i], mx1[i - 1])
mx2 = [0] * n
mx2[n - 1] = d[n - 1]
for i in range(n - 2, -1, -1):
mx2[i] = max(d[i], mx2[i + 1])
ans = [-1, -1]
for x in range(n - 2):
h = mx1[x]
lo = x + 2
hi = n - 1
while lo < hi:
mid = (lo + hi) // 2
qq1 = query(x + 1, mid - 1)
qq2 = mx2[mid]
go_up = qq1 > h or qq2 > h
go_down = qq1 < h or qq2 < h
if go_up == go_down:
break
if go_up:
lo = mid + 1
else:
hi = mid - 1
y = (lo + hi) // 2
if lo <= hi and h == mx2[y] and h == query(x + 1, y - 1):
ans[0] = x
ans[1] = y
break
if ans[0] == -1:
sys.stdout.write("NO\n")
else:
sys.stdout.write(f"YES\n{ans[0] + 1} {ans[1] - ans[0] - 1} {n - ans[1]}\n")
|
IMPORT ASSIGN VAR LIST FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING BIN_OP VAR NUMBER NUMBER STRING BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER STRING BIN_OP VAR VAR NUMBER STRING
|
You are given an array a consisting of n integers.
Let min(l, r) be the minimum value among a_l, a_{l + 1}, β¦, a_r and max(l, r) be the maximum value among a_l, a_{l + 1}, β¦, a_r.
Your task is to choose three positive (greater than 0) integers x, y and z such that:
* x + y + z = n;
* max(1, x) = min(x + 1, x + y) = max(x + y + 1, n).
In other words, you have to split the array a into three consecutive non-empty parts that cover the whole array and the maximum in the first part equals the minimum in the second part and equals the maximum in the third part (or determine it is impossible to find such a partition).
Among all such triples (partitions), you can choose any.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (3 β€ n β€ 2 β
10^5) β the length of a.
The second line of the test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9), where a_i is the i-th element of a.
It is guaranteed that the sum of n does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each test case, print the answer: NO in the only line if there is no such partition of a that satisfies the conditions from the problem statement. Otherwise, print YES in the first line and three integers x, y and z (x + y + z = n) in the second line.
If there are several answers, you can print any.
Example
Input
6
11
1 2 3 3 3 4 4 3 4 2 1
8
2 9 1 7 3 9 4 1
9
2 1 4 2 4 3 3 1 2
7
4 2 1 1 4 1 4
5
1 1 1 1 1
7
4 3 4 3 3 3 4
Output
YES
6 1 4
NO
YES
2 5 2
YES
4 1 2
YES
1 1 3
YES
2 1 4
|
from itertools import accumulate
from sys import gettrace, stdin
if gettrace():
inputi = input
else:
def input():
return next(stdin)[:-1]
def inputi():
return stdin.buffer.readline()
def solve():
n = int(inputi())
aa = [int(a) for a in inputi().split()]
maa = max(aa)
mpos = [i for i, a in enumerate(aa) if a == maa]
if len(mpos) >= 3:
print("YES")
print(mpos[1], 1, n - mpos[1] - 1)
return
x = mpos[0]
z = n - mpos[-1] - 1
lmx = list(accumulate(aa[0:x], lambda a, b: max(a, b)))
rmx = list(accumulate(aa[n - 1 : n - z - 1 : -1], lambda a, b: max(a, b)))
cmn = min(aa[x : n - z])
while x > 0 and z > 0:
if lmx[x - 1] == rmx[z - 1]:
if lmx[x - 1] == cmn:
print("YES")
print(x, n - x - z, z)
return
elif cmn < lmx[x - 1]:
x -= 1
z -= 1
elif x > 1 and lmx[x - 2] == lmx[x - 1] and aa[x - 1] == lmx[x - 1]:
x -= 1
else:
z -= 1
elif lmx[x - 1] > rmx[z - 1]:
x -= 1
else:
z -= 1
cmn = min(cmn, aa[x], aa[n - z - 1])
print("NO")
def main():
t = int(inputi())
for _ in range(t):
solve()
main()
|
IF FUNC_CALL VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR WHILE VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR RETURN IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
import sys
from sys import stdin
class SegTree:
def __init__(self, N, first):
self.NO = 2 ** (N - 1).bit_length()
self.First = first
self.data = [first] * (2 * self.NO)
def calc(self, l, r):
return min(l, r)
def update(self, ind, x):
ind += self.NO - 1
self.data[ind] = x
while ind >= 0:
ind = (ind - 1) // 2
self.data[ind] = self.calc(self.data[2 * ind + 1], self.data[2 * ind + 2])
def query(self, l, r):
L = l + self.NO
R = r + self.NO
s = self.First
while L < R:
if R & 1:
R -= 1
s = self.calc(s, self.data[R - 1])
if L & 1:
s = self.calc(s, self.data[L - 1])
L += 1
L >>= 1
R >>= 1
return s
def get(self, ind):
ind += self.NO - 1
return self.data[ind]
tt = int(stdin.readline())
for loop in range(tt):
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
ST = SegTree(n, float("inf"))
for i in range(n):
ST.update(i, a[i])
li = 0
ri = n - 1
ans = [0] * n
for i in range(n - 1):
if ST.query(li, ri + 1) == i + 1:
ans[n - 1 - i] = 1
if a[li] == i + 1:
li += 1
elif a[ri] == i + 1:
ri -= 1
else:
break
tmp = [0] * n
for i in range(n):
tmp[a[i] - 1] += 1
if max(tmp) == 1 and min(tmp) == 1:
ans[0] += 1
print("".join(map(str, ans)))
|
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_DEF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF VAR BIN_OP VAR NUMBER RETURN 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
for t in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
visited = [(False) for i in range(n + 1)]
i = 0
j = n - 1
flag, count = 0, 0
k = 1
mini = 9999999999
while i <= j:
if a[i] == k:
i += 1
k += 1
count += 1
elif a[j] == k:
j -= 1
k += 1
count += 1
else:
while i <= j:
mini = min(mini, a[i])
i += 1
if mini == k:
count += 1
elif mini < k:
count = mini
for i in range(n):
if visited[a[i]] == True:
flag = -1
break
visited[a[i]] = True
for i in range(n - count):
if i == flag:
print(1, end="")
else:
print(0, end="")
for i in range(n - count, n):
print(1, 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 NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
cnt = [0] * (n + 1)
numless = 0
for i in a:
cnt[i] += 1
print(int(set(a) == set([i for i in range(1, n + 1)])), end="")
if min(a) == 1:
l = 0
r = n - 1
ans = n
for i in range(n, 0, -1):
if a[l] < n - i + 1:
break
if a[r] < n - i + 1:
break
if cnt[n - i + 1] == 0 or numless > 0:
break
ans = i
if a[l] != n - i + 1 and a[r] != n - i + 1:
break
if a[l] == n - i + 1 and a[r] == n - i + 1:
break
if a[l] == n - i + 1:
l += 1
cnt[n - i + 1] -= 1
if a[r] == n - i + 1:
r -= 1
cnt[n - i + 1] -= 1
numless += cnt[n - i + 1]
if ans == 1:
ans += 1
print("".join([str(0) for i in range(ans - 2)]), end="")
print("".join([str(1) for i in range(n - ans + 1)]))
else:
print("".join([str(0) for i in range(n - 1)]))
|
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 BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
from sys import stdin
input = stdin.readline
q = int(input())
for _ in range(q):
n = int(input())
l = list(map(int, input().split()))
ile = [0] * (n + 1)
for i in l:
ile[i] += 1
lewy = 0
prawy = n - 1
step = 1
while True:
if lewy == prawy:
break
if l[lewy] != step and l[prawy] != step:
break
elif l[lewy] == step:
lewy += 1
else:
prawy -= 1
step += 1
poss = step - 1
if ile[poss + 1] > 0:
poss += 1
ogr = 10**9
for i in range(1, n + 1):
if ile[i] > 1:
ogr = i
break
poss = min(ogr, poss)
word = (n - poss) * "0" + poss * "1"
if ile == [0] + [1] * n:
print("1" + word[1:])
else:
print(word)
|
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR STRING BIN_OP VAR STRING IF VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
t = int(input())
for tt in range(t):
n = int(input())
array = [int(x) for x in input().split()]
cnt = [0] * (n + 1)
ans = []
for e in array:
cnt[e] += 1
flag = True
l = 0
r = n - 1
for i in range(1, n):
if flag and cnt[i]:
ans.append(1)
else:
ans.append(0)
if cnt[i] != 1:
flag = False
elif array[l] == i:
l += 1
elif array[r] == i:
r -= 1
else:
flag = False
aux = set(array)
if len(aux) == n:
ans.append(1)
else:
ans.append(0)
ans.reverse()
for e in ans:
print(e, end="")
print()
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
import sys
def input():
return sys.stdin.readline().strip()
def list2d(a, b, c):
return [([c] * b) for i in range(a)]
def list3d(a, b, c, d):
return [[([d] * c) for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e):
return [[[([e] * d) for k in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1):
return int(-(-x // y))
def INT():
return int(input())
def MAP():
return map(int, input().split())
def LIST(N=None):
return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes():
print("Yes")
def No():
print("No")
def YES():
print("YES")
def NO():
print("NO")
INF = 10**19
MOD = 10**9 + 7
EPS = 10**-10
class BIT:
def __init__(self, n):
self.n = n
n += 1
nv = 1
while nv < n:
nv *= 2
self.size = nv
self.tree = [0] * nv
def sum(self, i):
s = 0
i += 1
while i > 0:
s += self.tree[i - 1]
i -= i & -i
return s
def add(self, i, x):
i += 1
while i <= self.size:
self.tree[i - 1] += x
i += i & -i
def query(self, l, r):
return self.sum(r - 1) - self.sum(l - 1)
def get(self, i):
return self.query(i, i + 1)
def update(self, i, x):
self.add(i, x - self.get(i))
def print(self):
for i in range(self.n):
print(self.get(i), end=" ")
print()
def bisearch_fore(self, l, r, x):
l_sm = self.sum(l - 1)
ok = r + 1
ng = l - 1
while ng + 1 < ok:
mid = (ok + ng) // 2
if self.sum(mid) - l_sm >= x:
ok = mid
else:
ng = mid
if ok != r + 1:
return ok
else:
return -1
def bisearch_back(self, l, r, x):
r_sm = self.sum(r)
ok = l - 1
ng = r + 1
while ok + 1 < ng:
mid = (ok + ng) // 2
if r_sm - self.sum(mid - 1) >= x:
ok = mid
else:
ng = mid
if ok != l - 1:
return ok
else:
return -1
for _ in range(INT()):
N = INT()
A = [(a - 1) for a in LIST()]
C = [0] * N
B = [[] for i in range(N)]
for i in range(N):
C[A[i]] += 1
B[A[i]].append(i)
bit = BIT(N)
ans = [0] * N
for a in range(N):
if not C[a]:
break
ln = N - a - 1
se = set()
for i in B[a]:
l = bit.bisearch_back(max(i - ln, 0), i - 1, 1)
if l == -1:
l = max(i - ln, 0)
else:
l += 1
r = bit.bisearch_fore(i + 1, min(i + ln, N - 1), 1)
if r == -1:
r = min(i + ln, N - 1)
else:
r -= 1
if r - l == ln:
se.add((l, r))
bit.add(i, 1)
if len(se) != 1:
break
ans[ln] = 1
if len(set(A)) == N:
ans[0] = 1
print("".join(map(str, ans)))
|
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER RETURN VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER RETURN VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
from sys import stdin
ip = stdin.readline
for _ in range(int(ip())):
n = int(ip())
a = [(int(i) - 1) for i in ip().split()]
cnt = [0] * n
ans = [0] * n
for i in a:
cnt[i] += 1
ans[0] = 0 if cnt.count(0) > 0 else 1
ans[n - 1] = int(cnt[0] > 0)
l, r = 0, n - 1
for i in range(1, n):
j = i - 1
if cnt[j] == 1 and cnt[i]:
if a[l] == j:
l += 1
elif a[r] == j:
r -= 1
else:
break
ans[n - i - 1] = 1
else:
break
print(*ans, sep="")
|
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
a = [int(i) for i in input().split()]
b = a[:]
b.sort(reverse=1)
l = 0
r = n - 1
ans = [0] * n
for i in range(1, n + 1):
if b[-1] == i:
ans[n - i] = 1
if a[l] == i:
l += 1
b.pop()
elif a[r] == i:
r -= 1
b.pop()
else:
break
if len(a) == len(set(a)):
ans[0] = 1
for i in ans:
print(i, end="")
print()
|
IMPORT ASSIGN VAR 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
def solve(n, arr):
if n == 1:
return ["1"]
ans = ["0" for i in range(n)]
pos = [set() for i in range(n + 1)]
for i, a in enumerate(arr):
pos[a].add(i)
start = 0
end = n - 1
for a in range(1, n + 1):
if not pos[a]:
return ans
b1 = start in pos[a]
b2 = end in pos[a]
ans[n - a] = "1"
if len(pos[a]) >= 2:
return ans
if b1:
start += 1
elif b2:
end -= 1
else:
for i in range(1, n + 1):
if not len(pos[i]) == 1:
return ans
ans[0] = "1"
return ans
return ans
t = int(input())
for i in range(t):
n = int(input())
arr = list(map(int, input().split()))
print("".join(solve(n, arr)))
|
FUNC_DEF IF VAR NUMBER RETURN LIST STRING ASSIGN VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING IF FUNC_CALL VAR VAR VAR NUMBER RETURN VAR IF VAR VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER STRING RETURN VAR RETURN 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 VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
inx = {}
temp = 0
for i in range(n):
if l[i] in inx:
temp = 1
inx[l[i]].append(i + 1)
else:
inx[l[i]] = [i + 1]
ans = []
ch = 0
for i in range(1, n + 1):
if i not in inx:
ans.append("0" * (n + 1 - i))
break
if len(inx[i]) > 1:
ans.append("1")
ans.append("0" * (n - i))
break
curr = inx[i][0] - 1
if curr > 0 and curr < n - 1:
if l[curr - 1] > i and l[curr + 1] > i:
ans.append("1")
ans.append("0" * (n - i - 1))
ch = 1
break
else:
ans.append("1")
else:
ans.append("1")
if ch == 1:
if temp == 0:
ans.append("1")
else:
ans.append("0")
print("".join(ans[::-1]))
|
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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
import sys
input = sys.stdin.readline
def check(A):
LEN = len(A)
USE = [0] * (LEN + 1)
for a in A:
USE[a] += 1
flag = 1
OK = 1 << 30
for i in range(1, LEN + 1):
if USE[i] != 1:
flag = 0
OK = min(OK, i)
for i in range(1, LEN - 1):
if A[i - 1] >= A[i] and A[i] <= A[i + 1]:
OK = min(OK, A[i])
ANS = []
USE = [0] * (LEN + 1)
for a in A:
if USE[a] == 0 and a <= OK:
ANS.append(a)
USE[a] = 1
return flag, ANS
t = int(input())
for tests in range(t):
n = int(input())
A = list(map(int, input().split()))
ANS = ["0"] * n
f, B = check(A)
if f == 1:
ANS[0] = "1"
for i in range(-len(B), 0):
ANS[i] = "1"
print("".join(ANS))
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
import sys
input = sys.stdin.readline
def fun(l, r, val, che, ar):
while l <= r:
mid = l + (r - l) // 2
if fun1(mid, ar) < val:
l = mid + 1
elif che[mid] == 1:
return mid
else:
r = mid - 1
def update(inp, add, ar):
global n
while inp < n:
ar[inp] += add
inp += inp & -inp
def fun1(inp, ar):
global n
ans = 0
while inp:
ans += ar[inp]
inp -= inp & -inp
return ans
for _ in range(int(input())):
nn = int(input())
n = nn + 1
br = list(map(int, input().split()))
ar = [0] * n
arb = [0] * n
ch = [0] * n
chb = [0] * n
dic = {}
ans = [-1] * n
for i in range(nn):
if br[i] in dic:
dic[br[i]].append(i)
else:
dic[br[i]] = [i]
for i in range(1, n):
if i in dic:
for j in dic[i]:
temval = fun1(j + 1, ar)
if temval == 0:
fl = 0
else:
fl = fun(0, j + 1, fun1(j + 1, ar), ch, ar)
j = nn - 1 - j
temval = fun1(j + 1, arb)
if temval == 0:
rl = 0
else:
rl = fun(0, j + 1, fun1(j + 1, arb), chb, arb)
ans[i] = max(ans[i], nn - (rl + fl))
for j in dic[i]:
ch[j + 1] = 1
chb[nn - j] = 1
update(j + 1, 1, ar)
update(nn - j, 1, arb)
mi = [0]
va = ans[1]
for i in range(1, n):
va = min(va, ans[i])
mi.append(va)
final = [0] * n
for i in range(1, n):
req = n - i
if mi[i] >= req:
final[req] = 1
for i in range(1, n):
print(final[i], end="")
print()
|
IMPORT ASSIGN VAR VAR FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER 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 FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
import sys
input = sys.stdin.readline
(T,) = map(int, input().split())
for _ in range(T):
(N,) = map(int, input().split())
X = [0] + list(map(int, input().split()))
d = [0] * (N + 1)
for i, x in enumerate(X):
d[x] += 1
vs = set(X)
R = [0] * N
mn, mx = 0, N
for i in range(1, N + 1):
if X[mn] == i - 1 and d[i - 1] == 1 and i in vs:
mn += 1
R[N - i] = 1
elif X[mx] == i - 1 and d[i - 1] == 1 and i in vs:
mx -= 1
R[N - i] = 1
else:
break
if len(set(X)) == N + 1:
R[0] = 1
print("".join(str(c) for c in R))
|
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 BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
def is_permutatio3n(l):
se = set()
n = len(l)
for e in l:
if e in se or e > n:
return False
se.add(e)
return True
def main():
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
is_permutation = True
d = {}
M = 0
for e in a:
M = max(M, e)
if e in d:
is_permutation = False
d[e] += 1
else:
d[e] = 1
if M > n:
is_permutation = False
left = 0
right = n - 1
z = 0
last_work = 0
while z < n:
z += 1
if z not in d:
break
last_work = z
if d[z] > 1:
break
else:
last_work = z
if a[left] == z:
left += 1
elif a[right] == z:
right -= 1
else:
break
last_work = min(last_work, n - 1)
l = [str(int(is_permutation))]
for i in range(1, n - last_work):
l.append("0")
for j in range(n - last_work, n):
l.append("1")
print("".join(l))
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
import sys
input = sys.stdin.readline
t = int(input())
for you in range(t):
n = int(input())
l = input().split()
li = [int(i) for i in l]
curr = 1
start = 0
end = n - 1
while start <= end:
if li[start] == curr:
start += 1
curr += 1
elif li[end] == curr:
end -= 1
curr += 1
else:
break
ok = [(0) for i in range(n)]
for i in range(n):
ok[li[i] - 1] += 1
poss = 1
for i in range(n):
if ok[i] == 0:
poss = 0
break
lol = [(0) for i in range(n)]
lol[0] = poss
for i in range(n - curr + 1, n):
lol[i] = 1
done = 0
if end < start:
done = 1
else:
z = min(li[start : end + 1])
if z == curr:
done = 1
else:
done = 0
lol[n - curr] = done
li.sort()
ans = n + 1
for i in range(1, n):
if li[i] == li[i - 1]:
ans = li[i]
break
for i in range(n - ans):
lol[i] = 0
for i in lol:
print(i, end="")
print()
|
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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
right = [(i + 1) for i in range(n)]
left = [(i - 1) for i in range(n)]
idx = [[] for i in range(n)]
for i in range(n):
idx[a[i] - 1].append(i)
t = []
for i in range(n - 1, -1, -1):
t += idx[i]
idx = t
memo = [(0) for i in range(n)]
for i in idx:
L, R = left[i] + 1, right[i] - 1
memo[a[i] - 1] = max(memo[a[i] - 1], R - L + 1)
if left[i] == -1:
if right[i] != n:
left[right[i]] = -1
elif right[i] != n:
right[left[i]], left[right[i]] = right[i], left[i]
else:
right[left[i]] = n
Mini = [memo[i] for i in range(n)]
for i in range(1, n):
Mini[i] = min(Mini[i - 1], Mini[i])
ans = []
for i in range(n - 1, -1, -1):
if Mini[i] >= n - i:
ans.append("1")
else:
ans.append("0")
print("".join(ans))
|
IMPORT ASSIGN VAR 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 ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER 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 NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
import sys
input = lambda: sys.stdin.readline().rstrip()
T = int(input())
for _ in range(T):
N = int(input())
A = [(int(a) - 1) for a in input().split()]
C = [0] * N
for a in A:
C[a] += 1
l, r = 0, N - 1
for i in range(N):
if C[i] != 1:
break
if A[l] == i:
l += 1
elif A[r] == i:
r -= 1
else:
break
i = min(i, N - 1)
a0 = min(C)
k = min(i + 1, N - 1) if C[i] else i
ans = str(a0) + "0" * (N - k - 1) + "1" * k
print(ans)
|
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL 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 BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP STRING BIN_OP BIN_OP VAR VAR NUMBER BIN_OP STRING VAR EXPR FUNC_CALL VAR VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
import sys
t = int(input())
for test in range(t):
n = int(input())
a = list(map(int, input().split(" ")))
s = [[] for i in range(n + 1)]
res = ["1" for i in range(n)]
for i in range(n):
if s[a[i]] != []:
res[0] = "0"
s[a[i]].append(i)
k = 0
for i in range(1, n + 1):
if len(s[i]) == 0:
for j in range(0, n - i + 1):
res[j] = "0"
break
elif len(s[i]) > 1:
for j in range(0, n - i):
res[j] = "0"
break
elif s[i][0] == k:
k += 1
elif s[i][0] != n - i + k:
for j in range(1, n - i):
res[j] = "0"
break
print("".join(res))
|
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR LIST ASSIGN VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR STRING IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR STRING IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
import sys
input = sys.stdin.buffer.readline
T = int(input())
for _ in range(T):
n, a = int(input()), list(map(int, input().split()))
p1 = 1 if n == len(set(a)) == max(a) else 0
up = 1
for u in range(n - 2, -1, -1):
u1, u2 = a[u], a[u + 1]
if u1 > u2 and not up:
a[u] = u2
elif u1 < u2 and up:
up = 0
a.sort()
res, ok = [], 1
for i, u in enumerate(a[: n - 1]):
if i + 1 != u:
ok = 0
res.append(ok)
res.append(p1)
print("".join(map(str, reversed(res))))
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
gans = []
for _ in range(int(input())):
n = int(input())
u = list(map(int, input().split())) + [0]
d = [0] * n
for i in range(1, n):
c = i - 1
while u[c] >= u[i]:
c -= d[c] + 1
d[i] = i - c - 1
p = [0] * n
for i in range(n - 2, -1, -1):
c = i + 1
while u[c] >= u[i]:
c += p[c] + 1
p[i] = c - i - 1
ans = [0] * n
u.pop()
s = [0] * n
z = [0] * n
for i in range(n):
z[u[i] - 1] = max(z[u[i] - 1], d[i] + p[i])
s[u[i] - 1] += 1
for i in range(n):
if s[i] == 0:
break
if i > 0:
z[i] = min(z[i], z[i - 1])
if z[i] + i + 1 >= n:
ans[i] = 1
ans.reverse()
gans.append("".join(map(str, ans)))
print("\n".join(gans))
|
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
import sys
sys.setrecursionlimit(10**5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.buffer.readline())
def MI():
return map(int, sys.stdin.buffer.readline().split())
def LI():
return list(map(int, sys.stdin.buffer.readline().split()))
def LI1():
return list(map(int1, sys.stdin.buffer.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def BI():
return sys.stdin.buffer.readline().rstrip()
def SI():
return sys.stdin.buffer.readline().rstrip().decode()
inf = 10**16
md = 10**9 + 7
class SparseTableMin:
def __init__(self, aa):
w = len(aa)
h = w.bit_length()
table = [aa] + [([-1] * w) for _ in range(h - 1)]
tablei1 = table[0]
for i in range(1, h):
tablei = table[i]
for j in range(w - (1 << i) + 1):
rj = j + (1 << i - 1)
tablei[j] = min(tablei1[j], tablei1[rj])
tablei1 = tablei
self.table = table
def min(self, l, r):
i = (r - l).bit_length() - 1
tablei = self.table[i]
Lmin = tablei[l]
Rmin = tablei[r - (1 << i)]
if Lmin < Rmin:
Rmin = Lmin
return Rmin
for _ in range(II()):
n = II()
aa = LI1()
ans = [0] * n
ex = [False] * n
for a in aa:
if a < n:
ex[a] = True
if ex[0]:
ans[n - 1] = 1
if sum(ex) == n:
ans[0] = 1
sp = SparseTableMin(aa)
l, r = 0, n - 1
for i in range(n - 2):
if aa[l] == i and sp.min(l + 1, r + 1) == i + 1:
ans[n - 2 - i] = 1
l += 1
elif aa[r] == i and sp.min(l, r) == i + 1:
ans[n - 2 - i] = 1
r -= 1
else:
break
print(*ans, sep="")
|
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
from sys import stdin, stdout
input = stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
a = [int(x) for x in input().split()]
arr = [(0) for x in range(n)]
l = 0
r = n - 1
dict = {}
for k in range(n):
i = a[k]
if i in dict:
dict[i].append(k)
else:
dict[i] = [k]
if 1 not in dict:
for i in arr:
print(i, end="")
print()
continue
arr[0] = 1
for i in range(2, n + 1):
if i not in dict:
break
if len(dict[i - 1]) == 1:
if dict[i - 1][0] == l:
l += 1
arr[i - 1] = 1
elif dict[i - 1][0] == r:
r -= 1
arr[i - 1] = 1
else:
break
else:
break
arr = arr[::-1]
for i in range(1, n + 1):
if i not in dict:
break
else:
arr[0] = 1
for i in arr:
print(i, end="")
print()
|
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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR IF NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
from sys import stdin
T = int(stdin.readline().strip())
for casos in range(T):
n = int(stdin.readline().strip())
s = list(map(int, stdin.readline().strip().split()))
if n == 1:
print("1")
continue
cnt = [(0) for i in range(n + 2)]
for i in s:
cnt[i] += 1
ans = "1"
for i in range(1, n + 1):
if cnt[i] != 1:
ans = "0"
break
aux = ""
if cnt[1] > 0:
aux += "1"
next = 2
pos1 = 0
pos2 = n - 1
for i in range(2, n):
if s[pos1] == i - 1 and cnt[i - 1] == 1 and cnt[i] > 0:
pos1 += 1
aux += "1"
elif s[pos2] == i - 1 and cnt[i - 1] == 1 and cnt[i] > 0:
pos2 -= 1
aux += "1"
else:
while len(aux) < n - 1:
aux += "0"
break
else:
aux = "0" * (n - 1)
ans += aux[::-1]
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING IF VAR NUMBER NUMBER VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR STRING IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR STRING WHILE FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP STRING BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
from sys import stdin
input = stdin.readline
def rating(arr):
arr = list(map(lambda s: s - 1, lst))
cnt = [0] * len(arr)
ans = [0] * len(arr)
for i in arr:
cnt[i] += 1
ans[0] = 0 if cnt.count(0) > 0 else 1
ans[-1] = int(cnt[0] > 0)
l = 0
r = len(arr) - 1
for i in range(1, len(arr)):
j = i - 1
if cnt[j] == 1 and cnt[i]:
if arr[l] == j:
l += 1
elif arr[r] == j:
r -= 1
else:
break
ans[len(arr) - i - 1] = 1
else:
break
return "".join(list(map(str, ans)))
for i in range(int(input())):
a = input()
lst = list(map(int, input().strip().split()))
print(rating(lst))
|
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
for nt in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
ans = ["0"] * n
if sorted(a) == [i for i in range(1, n + 1)]:
ans[0] = "1"
else:
ans[0] = "0"
count = [0] * n
for i in a:
count[i - 1] += 1
l = 0
r = n - 1
curr = 1
ind = -1
for i in range(n):
if count[i]:
minn = i + 1
break
if min(a) == 1:
ans[-1] = "1"
while curr < n:
if a[l] == curr and count[curr - 1] == 1 and count[curr] >= 1:
l += 1
ans[n - 1 - curr] = "1"
elif a[r] == curr and count[curr - 1] == 1 and count[curr] >= 1:
r -= 1
ans[n - 1 - curr] = "1"
else:
ind = curr
break
curr += 1
print("".join(ans))
|
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 BIN_OP LIST STRING VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER STRING WHILE VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR STRING IF VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR STRING ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
s = ["0" for i in range(n)]
c = [0] * n
for i in range(n):
c[arr[i] - 1] += 1
if c[0] > 0:
s[-1] = "1"
i = 0
j = n - 1
p = 1
s[0] = "1" if len(set(arr)) == n else "0"
while p < n:
if (arr[i] == p or arr[j] == p) and c[p - 1] == 1 and c[p] >= 1:
if arr[i] == p:
i += 1
s[n - 1 - p] = "1"
elif arr[j] == p:
j -= 1
s[n - 1 - p] = "1"
p += 1
continue
break
print("".join(s))
|
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 STRING VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING STRING WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR STRING IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
import sys
input = sys.stdin.buffer.readline
def prog():
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
mn = min(a)
counts = [0] * (n + 1)
for i in a:
counts[i] += 1
output = ["0" for i in range(n)]
if sorted(a) == [i for i in range(1, n + 1)]:
output[0] = "1"
l = 0
r = n - 1
for i in range(1, n):
if mn != i:
break
if a[l] == i:
counts[a[l]] -= 1
l += 1
output[n - i] = "1"
while counts[mn] == 0:
mn += 1
elif a[r] == i:
counts[a[r]] -= 1
r -= 1
output[n - i] = "1"
while counts[mn] == 0:
mn += 1
else:
output[n - i] = "1"
break
print("".join(output))
prog()
|
IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING WHILE VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
def read_ints():
return map(int, input().split())
(t_n,) = read_ints()
for i_t in range(t_n):
(n,) = read_ints()
a_seq = tuple(read_ints())
result = [None] * n
result[0] = sorted(a_seq) == list(range(1, n + 1))
a_need = 1
i, j = 0, n - 1
has_analogue = False
while i <= j and (a_seq[i] == a_need or a_seq[j] == a_need):
if a_seq[i] == a_need and a_seq[j] == a_need and i != j:
i += 1
a_need += 1
has_analogue = True
break
else:
if a_seq[i] == a_need:
i += 1
else:
j -= 1
a_need += 1
wrong_segment_length = j - i + 1
if not has_analogue:
min_k = wrong_segment_length
wrong_segment_min = min(a_seq[i : j + 1], default=n + 1)
else:
min_k = wrong_segment_length + 1
wrong_segment_min = min(a_seq[i : j + 1])
if wrong_segment_min == a_need:
min_k = max(min_k, wrong_segment_length)
elif wrong_segment_min > a_need:
min_k = max(min_k, wrong_segment_length + 1)
else:
while wrong_segment_min != a_need:
a_need -= 1
wrong_segment_length += 1
min_k = max(min_k, wrong_segment_length)
for k in range(min_k, n + 1):
result[k - 1] = True
print("".join([("1" if result[i] else "0") for i in range(n)]))
|
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR STRING STRING VAR FUNC_CALL VAR VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
ans = ["0"] * n
a = list(map(int, input().split()))
if sorted(a) == list(range(1, n + 1)):
ans[0] = "1"
c = [0] * (n + 1)
ind = [0] * (n + 1)
for i in range(n):
c[a[i]] += 1
ind[a[i]] = i
s, e = 0, n - 1
pos = 1
for i in range(1, n + 1):
if i == 1 and c[i] or c[i] and (pos == s - 1 or pos == e + 1):
ans[n - i] = "1"
pos = ind[i]
if pos == s:
s += 1
else:
e -= 1
if c[i] > 1:
break
else:
break
print("".join(ans))
|
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
d = dict()
if a[-1] == 1:
a = a[::-1]
for i, x in enumerate(a):
if x in d:
d[x].append(i)
else:
d[x] = list()
d[x].append(i)
ans = []
flag = True
L = -1
R = n
for i in range(1, n):
if i == 1:
if 1 in d:
ans.append("1")
else:
ans.append("0")
elif ans[-1] == "1" and len(d[i - 1]) == 1:
if a[L + 1] == i - 1:
L += 1
elif a[R - 1] == i - 1:
R -= 1
else:
ans.append("0")
continue
if i in d:
ans.append("1")
else:
ans.append("0")
else:
ans.append("0")
continue
flag = True
for i in range(1, n + 1):
if i in d and len(d[i]) == 1:
continue
else:
flag = False
break
if flag:
ans.append("1")
else:
ans.append("0")
print("".join(ans[::-1]))
|
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 FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
import sys
for _ in range(int(sys.stdin.readline())):
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().strip().split(" ")))
i = 0
j = n - 1
onec = 0
oc = 0
iso = "ΓΆ"
d = 0
for k in range(1, n):
if a[i] < k or a[j] < k:
d = k - 1
break
elif a[i] == k:
onec += 1
i += 1
elif a[j] == k:
onec += 1
j -= 1
else:
d = k
break
if i < j + 1:
m = min(a[i : j + 1])
if m <= d:
onec = m * 1
oc = n - 1 - onec
r = []
for i in range(1, n + 1):
r.append(0)
for i in a:
if r[i - 1] == 0:
r[i - 1] += 1
else:
iso = "0"
break
else:
iso = "1"
sys.stdout.write(iso + "0" * oc + "1" * onec + "\n")
|
IMPORT 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 BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP STRING VAR BIN_OP STRING VAR STRING
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
def gift():
for _ in range(t):
n = int(input())
A = [(int(a) - 1) for a in input().split()]
C = [0] * n
for a in A:
C[a] += 1
l, r = 0, n - 1
for i in range(n):
if C[i] != 1:
break
if A[l] == i:
l += 1
elif A[r] == i:
r -= 1
else:
break
a0 = min(C)
k = min(i + 1, n - 1) if C[i] else i
ans = str(a0) + "0" * (n - k - 1) + "1" * k
yield ans
t = int(input())
ans = gift()
print(*ans, sep="\n")
|
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP STRING BIN_OP BIN_OP VAR VAR NUMBER BIN_OP STRING VAR EXPR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING
|
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers $a$ of length $n$. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter $k$, the program takes the minimum of each contiguous subarray of length $k$ in $a$.
More formally, for an array $a$ of length $n$ and an integer $k$, define the $k$-compression array of $a$ as an array $b$ of length $n-k+1$, such that $$b_j =\min_{j\le i\le j+k-1}a_i$$
For example, the $3$-compression array of $[1, 3, 4, 5, 2]$ is $[\min\{1, 3, 4\}, \min\{3, 4, 5\}, \min\{4, 5, 2\}]=[1, 3, 2].$
A permutation of length $m$ is an array consisting of $m$ distinct integers from $1$ to $m$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($m=3$ but there is $4$ in the array).
A $k$-compression array will make CodeCook users happy if it will be a permutation. Given an array $a$, determine for all $1\leq k\leq n$ if CodeCook users will be happy after a $k$-compression of this array or not.
-----Input-----
The first line contains a single integer $t$ ($1\leq t\leq 10^4$) β the number of test cases.
The first line of the description of each test case contains a single integer $n$ ($1\leq n\leq 3\cdot 10^5$) β the length of the array.
The second line of the description of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\leq a_i\leq n$) β the elements of the array.
It is guaranteed, that the sum of $n$ for all test cases does not exceed $3\cdot 10^5$.
-----Output-----
For each test case, print a binary string of length $n$.
The $k$-th character of the string should be $1$ if CodeCook users will be happy after a $k$-compression of the array $a$, and $0$ otherwise.
-----Examples-----
Input
5
5
1 5 3 4 2
4
1 3 2 1
5
1 3 3 3 2
10
1 2 3 4 5 6 7 8 9 10
3
3 3 2
Output
10111
0001
00111
1111111111
000
-----Note-----
In the first test case, $a=[1, 5, 3, 4, 2]$.
The $1$-compression of $a$ is $[1, 5, 3, 4, 2]$ and it is a permutation.
The $2$-compression of $a$ is $[1, 3, 3, 2]$ and it is not a permutation, since $3$ appears twice.
The $3$-compression of $a$ is $[1, 3, 2]$ and it is a permutation.
The $4$-compression of $a$ is $[1, 2]$ and it is a permutation.
The $5$-compression of $a$ is $[1]$ and it is a permutation.
|
import sys
input = sys.stdin.readline
class Bit:
def __init__(self, n):
self.size = n
self.tree = [0] * (n + 1)
def range_sum(self, l, r):
return self.sum(r) - self.sum(l - 1)
def sum(self, i):
i += 1
s = 0
while i > 0:
s += self.tree[i]
i -= i & -i
return s
def get(self, i):
return self.sum(i) - self.sum(i - 1)
def add(self, i, x):
i += 1
while i <= self.size:
self.tree[i] += x
i += i & -i
def main():
n = int(input())
alst = list(map(int, input().split()))
bit1 = Bit(n + 1)
bit2 = Bit(n + 1)
for a in alst:
bit1.add(a, 1)
ans = []
l = 0
r = n - 1
pos = 1
while r != l:
if (
bit1.get(pos) >= 1
and bit1.sum(pos - 1) == 0
and bit2.sum(pos - 1) == pos - 1
):
ans.append("1")
else:
ans += ["0"] * (n - len(ans) - 1)
break
if alst[l] <= alst[r]:
num = alst[l]
l += 1
else:
num = alst[r]
r -= 1
bit1.add(num, -1)
if bit2.get(num) == 0:
bit2.add(num, 1)
pos += 1
lst = [i for i in range(1, n + 1)]
alst.sort()
for a, b in zip(alst, lst):
if a != b:
ans.append("0")
print("".join(ans[::-1]))
return
ans.append("1")
print("".join(ans[::-1]))
for _ in range(int(input())):
main()
|
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING VAR BIN_OP LIST STRING BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + β¦. + arr1[j] = arr2[i] + arr2[i+1] + β¦. + arr2[j].
Example 1:
Input:
N = 6
Arr1[] = {0, 1, 0, 0, 0, 0}
Arr2[] = {1, 0, 1, 0, 0, 1}
Output: 4
Explanation: The longest span with same
sum is from index 1 to 4 following zero
based indexing.
Your Task:
You don't need to read input or print anything. Complete the function longestCommonSum() which takes two arrays arr1, arr2 and integer n as input parameters and returns the length of the longest common span.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5}
0 <= Arr1[i], Arr2[i] <= 1
|
class Solution:
def longestCommonSum(self, arr1, arr2, n):
arr = []
for i in range(n):
arr.append(arr1[i] - arr2[i])
for i in range(1, n):
arr[i] += arr[i - 1]
dic = {}
dic[0] = -1
l = 0
for i in range(n):
if arr[i] in dic.keys():
l = max(l, i - dic[arr[i]])
else:
dic[arr[i]] = i
return l
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.