description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 β€ i β€ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y β₯ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€A_{i} β€10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
def palindrom(a):
count = 0
while len(a) > 1:
if a[0] == a[-1]:
del a[0]
del a[-1]
else:
if a[0] > a[-1]:
a[0] -= a[-1]
del a[-1]
else:
a[-1] -= a[0]
del a[0]
count += 1
print(count)
for i in range(int(input())):
n = int(input())
num = list(map(int, input().split(" ")))
palindrom(num)
|
FUNC_DEF ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL 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 STRING EXPR FUNC_CALL VAR VAR
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 β€ i β€ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y β₯ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€A_{i} β€10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
t = int(input())
for _ in range(t):
input()
l = list(map(int, input().split()))
count = 0
while len(l) > 1:
if l[0] == l[-1]:
l.pop(0)
l.pop()
count -= 1
elif l[0] > l[-1]:
l[0] -= l[-1]
l.pop()
else:
l[-1] -= l[0]
l.pop(0)
count += 1
print(count)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 β€ i β€ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y β₯ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€A_{i} β€10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
t = int(input())
for i in range(t):
n = int(input())
A = list(map(int, input().split()))[:n]
count = 0
for i in range(len(A)):
N = len(A) - 1
temp = 0
if i == N - i:
break
if A[i] < A[N - i]:
temp = A[N - i]
A[N - i] = A[i]
A.insert(N - i, temp - A[i])
count += 1
elif A[i] > A[N - i]:
temp = A[i]
A[i] = A[N - i]
A.insert(i + 1, temp - A[N - i])
count += 1
print(count)
|
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 VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 β€ i β€ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y β₯ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€A_{i} β€10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
x = 0
y = n - 1
ans = 0
while len(a) > 1:
if a[0] == a[-1]:
del a[0]
del a[-1]
elif a[0] < a[-1]:
x = a[0]
y = a[-1]
del a[0]
del a[-1]
a.append(y - x)
ans += 1
else:
a.reverse()
x = a[0]
y = a[-1]
del a[0]
del a[-1]
a.append(y - x)
a.reverse()
ans += 1
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 NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 β€ i β€ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y β₯ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€A_{i} β€10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
a = []
for t in range(int(input())):
n = input()
a = list(map(int, input().split()))
c = 0
while len(a) > 1:
if a[0] == a[-1]:
del a[0]
del a[-1]
else:
if a[0] > a[-1]:
a[0] -= a[-1]
del a[-1]
else:
a[-1] -= a[0]
del a[0]
c += 1
print(c)
|
ASSIGN VAR LIST 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 VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 β€ i β€ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y β₯ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€A_{i} β€10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
l = 0
r = n - 1
s = 0
while True:
if a[l] < a[r]:
a[r] -= a[l]
l += 1
s += 1
elif a[r] < a[l]:
a[l] -= a[r]
r -= 1
s += 1
else:
l += 1
r -= 1
if l >= r:
break
print(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 NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 β€ i β€ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y β₯ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€A_{i} β€10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
t = int(input())
while t:
n = int(input())
l = list(map(int, input().split()))
i, j, count = 0, 0, 0
h = n // 2
if n % 2 == 0:
pass
else:
h += 1
j = n - 1
while i < j:
if l[i] == l[j]:
i += 1
j -= 1
elif l[i] > l[j]:
count += 1
l[i] = l[i] - l[j]
j -= 1
else:
count += 1
l[j] = l[j] - l[i]
i += 1
print(count)
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 β€ i β€ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y β₯ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€A_{i} β€10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
for _ in range(int(input())):
r = int(input()) - 1
s = list(map(int, input().split()))
cout = 0
l = 0
while l < r:
if s[l] != s[r]:
cout = cout + 1
if s[l] > s[r]:
s[l] = s[l] - s[r]
r = r - 1
else:
s[r] = s[r] - s[l]
l = l + 1
else:
r = r - 1
l = l + 1
print(cout)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 β€ i β€ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y β₯ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€A_{i} β€10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
t = int(input())
for i in range(t):
n = int(input())
z = list(map(int, input().split()))
ans = 0
j = 0
k = n - 1
while j < k:
if z[j] == z[k]:
j += 1
k -= 1
elif z[j] > z[k]:
ans += 1
z[j] = z[j] - z[k]
k -= 1
else:
ans += 1
z[k] -= z[j]
j += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 β€ i β€ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y β₯ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€A_{i} β€10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
t = int(input())
for i in range(t):
n = int(input())
l = list(map(int, input().split()))
i = 0
r = n - 1
count = 0
while i < r:
x = l[i]
y = l[r]
if x < y:
b = y - x
l[r] = b
count += 1
i = i + 1
elif x > y:
a = x - y
l[i] = a
count += 1
r -= 1
else:
r = r - 1
i = i + 1
print(count)
|
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 BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 β€ i β€ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y β₯ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€A_{i} β€10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
t = int(input())
for i in range(t):
a = int(input())
b = list(map(int, input().split()))
c = 0
while len(b) > 1:
if b[0] == b[a - 1]:
del b[0]
del b[len(b) - 1]
a = a - 2
elif b[0] < b[a - 1]:
b[a - 1] = b[a - 1] - b[0]
del b[0]
a = a - 1
c = c + 1
else:
b[0] = b[0] - b[a - 1]
del b[a - 1]
a = a - 1
c = c + 1
print(c)
|
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 WHILE FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 β€ i β€ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y β₯ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€A_{i} β€10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
t = int(input())
test = 0
while test < t:
j = 0
n = int(input())
count = 0
arr = list(map(int, input().strip().split()))[:n]
length = len(arr)
while length > 1:
if arr[0] == arr[length - 1]:
arr.pop(length - 1)
length = len(arr)
arr.pop(0)
length = len(arr)
elif arr[0] > arr[length - 1]:
x = arr[length - 1]
y = arr[0] - x
arr.pop(0)
length = len(arr)
arr.insert(0, y)
length = len(arr)
arr.pop(length - 1)
length = len(arr)
count += 1
else:
y = arr[0]
x = arr[length - 1] - y
arr.pop(0)
length = len(arr)
arr.pop(length - 1)
length = len(arr)
arr.append(x)
length = len(arr)
count += 1
print(count)
test += 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 β€ i β€ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y β₯ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€A_{i} β€10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
t = int(input())
while t > 0:
n = int(input())
arr = list(map(int, input().split()))
c = 0
i = 0
j = n - 1
while i <= j:
if arr[i] == arr[j]:
i += 1
j -= 1
continue
c += 1
if arr[i] > arr[j]:
l = arr[i] - arr[j]
arr[i] = l
j -= 1
else:
l = arr[j] - arr[i]
arr[j] = l
i += 1
print(c)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 β€ i β€ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y β₯ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€A_{i} β€10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
for i in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
c = 0
while len(l) > 1:
if l[0] == l[-1]:
del l[0]
del l[-1]
elif l[0] < l[-1]:
c += 1
x = l[-1] - l[0]
del l[0]
del l[-1]
l.append(x)
elif l[0] > l[-1]:
c += 1
y = l[0] - l[-1]
del l[0]
del l[-1]
l.insert(0, y)
print(c)
|
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 WHILE FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 β€ i β€ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y β₯ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€A_{i} β€10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
t = int(input())
for i in range(t):
n = int(input())
l = [int(i) for i in input().split()]
c = 0
if n == 0 or n == 1:
print(0)
else:
while len(l) != 0 and len(l) != 1:
if l[0] == l[-1]:
l.pop()
l.pop(0)
else:
c += 1
if l[0] > l[-1]:
l[0] = l[0] - l[-1]
l.pop()
elif l[0] < l[-1]:
l[-1] = l[-1] - l[0]
l.pop(0)
print(c)
|
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 IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 β€ i β€ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y β₯ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$1 β€A_{i} β€10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
t = int(input())
for i in range(t):
n = int(input())
arr = list(map(int, input().split()))
i, j = 0, n - 1
count = 0
while i < j:
a = min(arr[i], arr[j])
b = max(arr[i], arr[j])
if arr[i] == arr[j]:
i += 1
j -= 1
elif arr[i] < arr[j]:
count += 1
arr[j] -= arr[i]
i += 1
else:
count += 1
arr[i] -= arr[j]
j -= 1
print(count)
|
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 VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Window:
def __init__(self):
self.counter = collections.Counter()
def add(self, x):
self.counter[x] += 1
def remove(self, x):
self.counter[x] -= 1
if self.counter[x] == 0:
self.counter.pop(x)
def __len__(self):
return len(self.counter)
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
window1 = Window()
window2 = Window()
left1, left2 = 0, 0
result = 0
for right, num in enumerate(A):
window1.add(num)
window2.add(num)
while len(window1) > K:
window1.remove(A[left1])
left1 += 1
while len(window2) >= K:
window2.remove(A[left2])
left2 += 1
result += left2 - left1
return result
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF VAR VAR NUMBER FUNC_DEF VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
return self.at_most_K(A, K) - self.at_most_K(A, K - 1)
def at_most_K(self, A, k):
left = 0
counter = collections.Counter()
diff = 0
result = 0
for right in range(len(A)):
counter[A[right]] += 1
if counter[A[right]] == 1:
diff += 1
while diff > k:
counter[A[left]] -= 1
if counter[A[left]] == 0:
diff -= 1
left += 1
result += right - left + 1
return result
|
CLASS_DEF FUNC_DEF VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
last_seen = {}
start = end = 0
ans = 0
for i, x in enumerate(A):
last_seen[x] = i
while len(last_seen) > K:
if last_seen[A[start]] == start:
del last_seen[A[start]]
start += 1
while A[end] not in last_seen or last_seen[A[end]] != end:
end += 1
if len(last_seen) == K:
ans += end - start + 1
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
def helper(A, K):
d = defaultdict(int)
begin = 0
end = 0
ans = float("-inf")
count = 0
res = 0
while end < len(A):
char = A[end]
d[char] += 1
if d[char] == 1:
count += 1
end += 1
while count > K:
temp = A[begin]
d[temp] -= 1
if d[temp] == 0:
count -= 1
begin += 1
res += end - begin + 1
return res
return helper(A, K) - helper(A, K - 1)
|
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
m1, m2 = collections.Counter(), collections.Counter()
def remove(m, n):
m[n] -= 1
if not m[n]:
del m[n]
res = 0
left = right = -1
for i, n in enumerate(A):
while left < len(A) - 1 and len(m1) < K:
left += 1
m1[A[left]] += 1
if len(m1) < K:
return res
while right < len(A) - 1 and len(m2) <= K:
right += 1
m2[A[right]] += 1
res += right - left + (len(m2) == K)
remove(m1, n)
remove(m2, n)
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
n = len(A)
if n < 1:
return 0
result = 0
fc = Counter()
c = Counter()
l = 0
fr = 0
r = 0
while l < n:
while fr < n and len(fc) < K:
fc[A[fr]] += 1
fr += 1
while r < n:
if A[r] not in c and len(c) >= K:
break
c[A[r]] += 1
r += 1
if len(c) == K:
result += r - fr + 1
fc[A[l]] -= 1
if fc[A[l]] == 0:
del fc[A[l]]
c[A[l]] -= 1
if c[A[l]] == 0:
del c[A[l]]
l += 1
print("c", l, r)
return result
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR VAR RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
def atmost(k):
i = 0
res = 0
d = defaultdict(int)
for j, a in enumerate(A):
if d[a] == 0:
k -= 1
d[a] += 1
while k < 0:
d[A[i]] -= 1
if d[A[i]] == 0:
k += 1
i += 1
res += j - i + 1
return res
return atmost(K) - atmost(K - 1)
|
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Accumulator:
def __init__(self):
self.KeyToCount = {}
self.q = deque()
def append(self, value):
self.q.append(value)
self.KeyToCount[value] = self.KeyToCount.get(value, 0) + 1
def popleft(self):
v = self.q.popleft()
if self.KeyToCount[v] == 1:
del self.KeyToCount[v]
else:
self.KeyToCount[v] -= 1
def distinctCount(self):
return len(self.KeyToCount)
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
n = len(A)
nextHiIdx = 0
nextLoIdx = 0
loAcc = Accumulator()
hiAcc = Accumulator()
lo = [None] * n
hi = [None] * n
for i in range(n):
if i >= 1:
hiAcc.popleft()
loAcc.popleft()
while nextLoIdx < n and loAcc.distinctCount() < K:
loAcc.append(A[nextLoIdx])
nextLoIdx += 1
while nextHiIdx < n and hiAcc.distinctCount() <= K:
hiAcc.append(A[nextHiIdx])
nextHiIdx += 1
if loAcc.distinctCount() == K:
lo[i] = nextLoIdx - 1
if hiAcc.distinctCount() == K + 1:
hi[i] = nextHiIdx - 1
elif hiAcc.distinctCount() == K and nextHiIdx == n:
hi[i] = nextHiIdx
return sum(hi[i] - lo[i] for i in range(n) if hi[i] != None)
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NONE VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
if not A:
return 0
l = len(A)
end = 0
left1 = 0
left2 = 0
map1 = collections.defaultdict(int)
map2 = collections.defaultdict(int)
ans = 0
while end < l:
ch = A[end]
map1[ch] += 1
map2[ch] += 1
while len(map1) > K and left1 < l:
temp1 = A[left1]
map1[temp1] -= 1
if map1[temp1] == 0:
del map1[temp1]
left1 += 1
while len(map2) >= K and left2 < l:
temp2 = A[left2]
map2[temp2] -= 1
if map2[temp2] == 0:
del map2[temp2]
left2 += 1
ans += left2 - left1
end += 1
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
return self.atMostK(A, K) - self.atMostK(A, K - 1)
def atMostK(self, A, k):
start = 0
end = 0
res = 0
chars = collections.Counter()
distinct = 0
while end < len(A):
if chars[A[end]] == 0:
distinct += 1
chars[A[end]] += 1
while distinct > k:
chars[A[start]] -= 1
if chars[A[start]] == 0:
distinct -= 1
start += 1
res += end - start + 1
end += 1
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A, K):
return self.atMostK(A, K) - self.atMostK(A, K - 1)
def atMostK(self, s, k):
if not s:
return 0
N = len(s)
left, right = 0, 0
ret = 0
counter = collections.Counter()
counter[s[right]] += 1
while right < N:
if len(counter) > k:
counter[s[left]] -= 1
if counter[s[left]] == 0:
del counter[s[left]]
left += 1
else:
right += 1
ret += right - left
if right < N:
counter[s[right]] += 1
return ret
|
CLASS_DEF FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
def at_most_k(arr, k):
start = 0
counter = Counter()
uniques = 0
res = 0
for i in range(len(A)):
counter[A[i]] += 1
if counter[A[i]] == 1:
uniques += 1
while uniques > k:
counter[A[start]] -= 1
if counter[A[start]] == 0:
uniques -= 1
start += 1
res += i - start
return res
return at_most_k(A, K) - at_most_k(A, K - 1)
|
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], k: int) -> int:
dic = {}
n = len(A)
left = 0
right = 0
cnt = 0
for i in range(n):
if A[i] in dic:
dic[A[i]] += 1
else:
dic[A[i]] = 1
l = len(dic)
if l == k + 1:
del dic[A[right]]
right += 1
left = right
l -= 1
if l == k:
while dic[A[right]] > 1:
dic[A[right]] -= 1
right += 1
cnt += right - left + 1
return cnt
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
int_dict = {}
int_sorted_list = []
current = 0
l = len(A)
count = 0
while current < l:
if int_dict.get(A[current]) != None:
int_sorted_list.remove(A[current])
int_dict[A[current]] = current
int_sorted_list.append(A[current])
if len(int_sorted_list) > K + 1:
del int_dict[int_sorted_list[0]]
del int_sorted_list[0]
if len(int_sorted_list) > K:
count += (
int_dict[int_sorted_list[-K]] - int_dict[int_sorted_list[-K - 1]]
)
elif len(int_sorted_list) == K:
count += int_dict[int_sorted_list[-K]] + 1
current += 1
return count
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR NONE EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
sum = 0
begin = 0
end = 0
s = collections.defaultdict(int)
lA = len(A)
for i in range(lA):
if len(s) >= K:
s[A[i]] += 1
if s[A[i]] > 1:
newend = end
while s[A[newend]] > 1:
s[A[newend]] -= 1
newend += 1
end = newend
else:
begin = end
while s[A[begin]] > 1:
s[A[begin]] -= 1
begin += 1
s[A[begin]] -= 1
begin += 1
end = begin
while s[A[end]] > 1:
s[A[end]] -= 1
end += 1
sum += end - begin + 1
else:
s[A[i]] += 1
if len(s) == K:
while s[A[end]] > 1:
s[A[end]] -= 1
end += 1
sum += end - begin + 1
return sum
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Window:
def __init__(self):
self.count = collections.Counter()
self.nonzero = 0
def add(self, x):
self.count[x] += 1
if self.count[x] == 1:
self.nonzero += 1
def remove(self, x):
self.count[x] -= 1
if self.count[x] == 0:
self.nonzero -= 1
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
window1 = Window()
window2 = Window()
ans = left1 = left2 = 0
for x in A:
window1.add(x)
window2.add(x)
while window1.nonzero > K:
window1.remove(A[left1])
left1 += 1
while window2.nonzero >= K:
window2.remove(A[left2])
left2 += 1
ans += left2 - left1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER FUNC_DEF VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A, K):
cnt1, cnt2 = dict(), dict()
res, i1, i2 = 0, 0, 0
for v in A:
cnt1[v] = cnt1.get(v, 0) + 1
cnt2[v] = cnt2.get(v, 0) + 1
while len(cnt1) > K:
cnt1[A[i1]] -= 1
if cnt1[A[i1]] == 0:
del cnt1[A[i1]]
i1 += 1
while len(cnt2) >= K:
cnt2[A[i2]] -= 1
if cnt2[A[i2]] == 0:
del cnt2[A[i2]]
i2 += 1
res += i2 - i1
return res
class Solution:
def subarraysWithKDistinct(self, A, K):
cnt1, cnt2 = dict(), dict()
res, i1, i2 = 0, 0, 0
for v in A:
cnt1[v] = cnt1.get(v, 0) + 1
cnt2[v] = cnt2.get(v, 0) + 1
while len(cnt1) > K:
X = A[i1]
cnt1[X] -= 1
if cnt1[X] == 0:
del cnt1[X]
i1 += 1
while len(cnt2) > K - 1:
X = A[i2]
cnt2[X] -= 1
if cnt2[X] == 0:
del cnt2[X]
i2 += 1
res += i2 - i1
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
n = len(A)
if n < K:
return 0
left = 0
right = 0
totalCount = 0
dp = [(0) for i in range(20001)]
result = 0
for right in range(n):
if dp[A[right]] == 0:
totalCount += 1
dp[A[right]] += 1
while totalCount >= K:
if totalCount == K:
result += 1
dp[A[left]] -= 1
if dp[A[left]] == 0:
totalCount -= 1
left += 1
while totalCount <= K and left > 0:
left -= 1
if dp[A[left]] == 0:
totalCount += 1
dp[A[left]] += 1
return result
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
class UniqueCounter(object):
def __init__(self):
self.non_zero = 0
self.counts = collections.defaultdict(int)
def add(self, x):
if self.counts[x] == 0:
self.non_zero += 1
self.counts[x] += 1
def remove(self, x):
self.counts[x] -= 1
if self.counts[x] == 0:
self.non_zero -= 1
def count(self):
return self.non_zero
def subarrays_with_max_K(A, K):
j = 0
uc = UniqueCounter()
uc.add(A[0])
answer = 0
for i in range(len(A)):
while j < len(A) and uc.count() <= K:
j += 1
if j < len(A):
uc.add(A[j])
answer += j - i
uc.remove(A[i])
return answer
return subarrays_with_max_K(A, K) - subarrays_with_max_K(A, K - 1)
|
CLASS_DEF FUNC_DEF VAR VAR VAR CLASS_DEF VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER FUNC_DEF VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER FUNC_DEF RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Window:
def __init__(self):
self.num = 0
self.dic = collections.defaultdict(int)
def add(self, v: int) -> int:
if self.dic[v] == 0:
self.num += 1
self.dic[v] += 1
return self.num
def remove(self, v: int) -> int:
self.dic[v] -= 1
if self.dic[v] == 0:
self.num -= 1
return self.num
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
wk = Window()
wm = Window()
sk = 0
sm = 0
e = 0
ans = 0
while e < len(A):
ce = A[e]
nk = wk.add(ce)
nm = wm.add(ce)
if nk < K:
e += 1
elif nk == K:
while nm != K - 1:
nm = wm.remove(A[sm])
sm += 1
ans += sm - sk
e += 1
else:
while nk != K:
nk = wk.remove(A[sk])
sk += 1
while nm != K - 1:
nm = wm.remove(A[sm])
sm += 1
ans += sm - sk
e += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER RETURN VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
return self.atMost(A, K) - self.atMost(A, K - 1)
def atMost(self, A, K):
if K == 0:
return 0
hi, res = 0, 0
n = len(A)
count = collections.defaultdict(int)
for lo in range(n):
while hi < n and (len(count) < K or A[hi] in count):
count[A[hi]] += 1
hi += 1
res += hi - lo
count[A[lo]] -= 1
if count[A[lo]] == 0:
count.pop(A[lo])
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
d, t = defaultdict(int), defaultdict(int)
a, b = K, K
left, right = 0, 0
res = 0
for i, ele in enumerate(A):
a -= 1 if d[ele] == 0 else 0
b -= 1 if t[ele] == 0 else 0
d[ele] += 1
t[ele] += 1
while a < 0:
a += 1 if d[A[left]] == 1 else 0
d[A[left]] -= 1
left += 1
while b <= 0:
b += 1 if t[A[right]] == 1 else 0
t[A[right]] -= 1
right += 1
res += right - left
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
keyCounter = defaultdict(int)
ALen = len(A)
l = 0
r = 0
ans = 0
KNow = 0
while r <= ALen:
if KNow == K:
ans += 1
temp = 0
while r + temp < ALen and keyCounter[A[r + temp]] > 0:
ans += 1
temp += 1
if keyCounter[A[l]] > 0:
keyCounter[A[l]] -= 1
if keyCounter[A[l]] == 0:
KNow -= 1
l += 1
elif KNow < K:
if r == ALen:
return ans
if keyCounter[A[r]] == 0:
KNow += 1
keyCounter[A[r]] += 1
r += 1
else:
if keyCounter[A[l]] > 0:
keyCounter[A[l]] -= 1
if keyCounter[A[l]] == 0:
KNow -= 1
l += 1
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR RETURN VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: "List[int]", K: "int") -> "int":
return self.subarraysWithAtMostKDistinct(
A, K
) - self.subarraysWithAtMostKDistinct(A, K - 1)
def subarraysWithAtMostKDistinct(self, s, k):
lookup = collections.defaultdict(int)
l, r, counter, res = 0, 0, 0, 0
while r < len(s):
lookup[s[r]] += 1
if lookup[s[r]] == 1:
counter += 1
r += 1
while l < r and counter > k:
lookup[s[l]] -= 1
if lookup[s[l]] == 0:
counter -= 1
l += 1
res += r - l
return res
|
CLASS_DEF FUNC_DEF STRING STRING RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A, K):
return self.atMostK(A, K) - self.atMostK(A, K - 1)
def atMostK(self, A, K):
count = collections.Counter()
res = left = right = distinct = 0
while right < len(A):
count[A[right]] += 1
if count[A[right]] == 1:
distinct += 1
while distinct > K:
count[A[left]] -= 1
if count[A[left]] == 0:
distinct -= 1
left += 1
res += right - left + 1
right += 1
return res
|
CLASS_DEF FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def helper(self, A, B):
count = 0
left = 0
right = 0
d = {}
while right < len(A):
if A[right] not in d:
d[A[right]] = 0
d[A[right]] += 1
while len(d) > B:
d[A[left]] -= 1
if d[A[left]] == 0:
d.pop(A[left])
left += 1
count += right - left + 1
right += 1
return count
def subarraysWithKDistinct(self, A: List[int], B: int) -> int:
return self.helper(A, B) - self.helper(A, B - 1)
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
n = len(A)
hashmap = {}
l = 0
count = 0
ans = 0
sums = 1
for r in range(n):
if A[r] in hashmap:
hashmap[A[r]] += 1
else:
hashmap[A[r]] = 1
if hashmap[A[r]] == 1:
count += 1
while count > K or hashmap[A[l]] > 1:
if count > K:
sums = 1
else:
sums += 1
hashmap[A[l]] -= 1
if hashmap[A[l]] == 0:
count -= 1
l += 1
if count == K:
ans += sums
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
start = 0
first_uni = 0
re = 0
dd = {}
for i, cur in enumerate(A):
dd[cur] = dd.get(cur, 0) + 1
if len(dd) == K + 1:
dd.pop(A[first_uni])
first_uni += 1
start = first_uni
if len(dd) == K:
while first_uni <= i and dd[A[first_uni]] != 1:
dd[A[first_uni]] -= 1
first_uni += 1
re += first_uni - start + 1
return re
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
def atMost(K):
N = len(A)
ct = collections.Counter()
ans = 0
i = 0
for j in range(N):
ct[A[j]] += 1
if len(ct) <= K:
ans += j - i + 1
else:
while len(ct) > K:
ct[A[i]] -= 1
if ct[A[i]] == 0:
del ct[A[i]]
i += 1
ans += j - i + 1
return ans
return atMost(K) - atMost(K - 1)
|
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
counter1, counter2 = collections.Counter(), collections.Counter()
slow = fast = res = 0
for a in A:
counter1[a], counter2[a] = counter1[a] + 1, counter2[a] + 1
while len(counter2) == K:
counter2[A[fast]] -= 1
if not counter2[A[fast]]:
del counter2[A[fast]]
fast += 1
while len(counter1) > K:
counter1[A[slow]] -= 1
if not counter1[A[slow]]:
del counter1[A[slow]]
slow += 1
res += fast - slow
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Window:
def __init__(self):
self.count = {}
def add(self, elem):
self.count.setdefault(elem, 0)
self.count[elem] += 1
def remove(self, elem):
self.count[elem] -= 1
if self.count[elem] == 0:
del self.count[elem]
def added_size(self, elem):
return len(self.count) + bool(elem not in self.count)
def count_below(arr, k):
if k == 0:
return 0
else:
right = 0
satisfactory = 0
window = Window()
for left in range(len(arr)):
while right < len(arr) and window.added_size(arr[right]) <= k:
window.add(arr[right])
right += 1
satisfactory += right - left
window.remove(arr[left])
return satisfactory
def subarrays_with_k_distinct(arr, k):
assert k >= 1
return count_below(arr, k) - count_below(arr, k - 1)
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
return subarrays_with_k_distinct(A, K)
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
checkSet = OrderedDict()
windowStart = 0
count = 0
ans = []
for i, n in enumerate(A):
checkSet[n] = i
checkSet.move_to_end(n)
while len(checkSet) > K:
windowStart = checkSet.popitem(last=False)[1] + 1
if len(checkSet) == K:
count += next(iter(list(checkSet.items())))[1] - windowStart + 1
return count
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def _identify_limit(self, A, current_limit, K, char_count_after_limit):
char_count_after_limit = {k: v for k, v in char_count_after_limit.items()}
unique_chars = {
i for i in char_count_after_limit if char_count_after_limit[i] > 0
}
while len(unique_chars) == K:
temp_char = A[current_limit]
char_count_after_limit[temp_char] = char_count_after_limit[temp_char] - 1
if char_count_after_limit[temp_char] == 0:
unique_chars.remove(temp_char)
current_limit += 1
char_count_after_limit[A[current_limit - 1]] = (
char_count_after_limit[A[current_limit - 1]] + 1
)
return current_limit - 1, char_count_after_limit
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
counter1, counter2 = collections.Counter(), collections.Counter()
slow = fast = res = 0
for _, a in enumerate(A):
counter1[a], counter2[a] = counter1[a] + 1, counter2[a] + 1
while len(counter2) == K:
counter2[A[fast]] -= 1
if not counter2[A[fast]]:
del counter2[A[fast]]
fast += 1
while len(counter1) > K:
counter1[A[slow]] -= 1
if not counter1[A[slow]]:
del counter1[A[slow]]
slow += 1
res += fast - slow
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER VAR FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithAtMostK(self, A: List[int], K: int) -> int:
result = i = 0
count = collections.Counter()
for j in range(len(A)):
if count[A[j]] == 0:
K -= 1
count[A[j]] += 1
while K < 0:
count[A[i]] -= 1
if count[A[i]] == 0:
K += 1
i += 1
result += j - i + 1
return result
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
return self.subarraysWithAtMostK(A, K) - self.subarraysWithAtMostK(A, K - 1)
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A, K):
def atMostK(A, K):
count = collections.Counter()
res, l, window_count = 0, 0, 0
for r, c in enumerate(A):
if count[c] == 0:
window_count += 1
count[c] += 1
while window_count > K:
count[A[l]] -= 1
if count[A[l]] == 0:
window_count -= 1
l += 1
res += r - l + 1
return res
return atMostK(A, K) - atMostK(A, K - 1)
|
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
return self.at_most(A, K) - self.at_most(A, K - 1)
def at_most(self, A, K):
if K == 0:
return 0
window = {}
left = 0
ret = 0
for right in range(len(A)):
window[A[right]] = window.get(A[right], 0) + 1
while left < right and len(window) > K:
window[A[left]] -= 1
if window[A[left]] == 0:
del window[A[left]]
left += 1
ret += right - left + 1
return ret
|
CLASS_DEF FUNC_DEF VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
def help(k):
res = 0
left = 0
d = {}
for i, ele in enumerate(A):
d[ele] = d.get(ele, 0) + 1
while len(d) > k:
d[A[left]] -= 1
if d[A[left]] == 0:
del d[A[left]]
left += 1
res += i - left
return res
return help(K) - help(K - 1)
|
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
length = len(A)
class counter:
def __init__(self):
self.c = Counter()
def addValue(self, num):
self.c[num] += 1
def removeValue(self, num):
self.c[num] -= 1
if self.c[num] == 0:
del self.c[num]
def subarraysWithAtLeast(k):
start = 0
c = counter()
ret = 0
for i in range(length):
cur = A[i]
c.addValue(cur)
if len(c.c) < k:
ret += i - start + 1
continue
while len(c.c) > k:
tmp = A[start]
c.removeValue(tmp)
start += 1
assert len(c.c) == k
ret += i - start + 1
return ret
return subarraysWithAtLeast(K) - subarraysWithAtLeast(K - 1)
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF VAR VAR NUMBER FUNC_DEF VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
ans, ctr, lb, count = 0, {}, 0, 0
for i, val in enumerate(A):
if val not in ctr:
ctr[val] = 0
ctr[val] += 1
while len(ctr) > K:
ctr[A[lb]] -= 1
if ctr[A[lb]] == 0:
del ctr[A[lb]]
lb += 1
if len(ctr) == K:
p2, count, ctr1 = lb, 0, collections.Counter()
while len(ctr) == K:
count += 1
ctr[A[p2]] -= 1
if ctr[A[p2]] == 0:
del ctr[A[p2]]
ctr1[A[p2]] += 1
p2 += 1
ans += count
for k, v in ctr1.items():
ctr[k] = ctr.get(k, 0) + v
return ans
class Window:
def __init__(self):
self.count = collections.Counter()
self.nonzero = 0
def add(self, x):
self.count[x] += 1
if self.count[x] == 1:
self.nonzero += 1
def remove(self, x):
self.count[x] -= 1
if self.count[x] == 0:
self.nonzero -= 1
class Solution(object):
def subarraysWithKDistinct(self, A, K):
window1 = Window()
window2 = Window()
ans = left1 = left2 = 0
for right, x in enumerate(A):
window1.add(x)
window2.add(x)
while window1.nonzero > K:
window1.remove(A[left1])
left1 += 1
while window2.nonzero >= K:
window2.remove(A[left2])
left2 += 1
ans += left2 - left1
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER DICT NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FUNC_CALL VAR WHILE FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER FUNC_DEF VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER CLASS_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def _identify_limit(self, A, current_limit, K, char_count_after_limit):
temp_char = A[current_limit]
while char_count_after_limit.get(temp_char, 0) > 1:
char_count_after_limit[temp_char] = char_count_after_limit[temp_char] - 1
current_limit += 1
temp_char = A[current_limit]
return current_limit, char_count_after_limit
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
char_info = {item: [] for item in set(A)}
unique_char_count = {}
unique_char = set()
start_idx = 0
end_idx = 0
num_substr = 0
current_subarray = []
current_valid_count = 0
limit_idx = 0
while end_idx < len(A) + 1:
if len(unique_char_count) == K:
limit_idx, unique_char_count = self._identify_limit(
A, limit_idx, K, unique_char_count
)
num_substr += limit_idx - start_idx + 1
if end_idx < len(A):
current_char = A[end_idx]
unique_char_count[current_char] = (
unique_char_count.get(current_char, 0) + 1
)
end_idx += 1
elif len(unique_char_count) > K:
current_char = A[limit_idx]
unique_char_count.pop(current_char)
start_idx = limit_idx + 1
limit_idx = start_idx
else:
if end_idx < len(A):
current_char = A[end_idx]
unique_char_count[current_char] = (
unique_char_count.get(current_char, 0) + 1
)
end_idx += 1
return num_substr
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
def remove(m, n):
if m[n] == 1:
del m[n]
else:
m[n] -= 1
m1, m2 = collections.Counter(), collections.Counter()
res = 0
i1 = i2 = -1
for i0, n in enumerate(A):
while i1 + 1 < len(A) and len(m1) < K:
i1 += 1
m1[A[i1]] += 1
if len(m1) < K:
return res
while i2 + 1 < len(A) and len(m2) < K + 1:
i2 += 1
m2[A[i2]] += 1
res += i2 - i1 + int(len(m2) == K)
remove(m1, n)
remove(m2, n)
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
return self.helper(A, K) - self.helper(A, K - 1)
def helper(self, A, K):
counter = collections.Counter()
p1 = p2 = 0
res = 0
while p2 < len(A):
if counter[A[p2]] == 0:
K -= 1
counter[A[p2]] += 1
while K < 0:
counter[A[p1]] -= 1
if counter[A[p1]] == 0:
K += 1
p1 += 1
p2 += 1
res += p2 - p1
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
if K > len(A):
return 0
result = checker = windowStart = 0
hashMap = {}
a_length = len(A)
for windowEnd in range(a_length):
curr_str = A[windowEnd]
if curr_str in hashMap:
hashMap[curr_str] += 1
else:
hashMap[curr_str] = 1
if len(hashMap) > K:
del hashMap[A[checker]]
checker += 1
windowStart = checker
if len(hashMap) == K:
while hashMap[A[checker]] > 1:
hashMap[A[checker]] -= 1
checker += 1
result += checker - windowStart + 1
return result
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
left1 = 0
left2 = 0
w1 = {}
w2 = {}
count = 0
for right, x in enumerate(A):
if x in w1:
w1[x] += 1
else:
w1[x] = 1
if x in w2:
w2[x] += 1
else:
w2[x] = 1
while len(w1) > K:
if w1[A[left1]] == 1:
w1.pop(A[left1])
elif w1[A[left1]] > 1:
w1[A[left1]] -= 1
left1 += 1
while len(w2) >= K:
if w2[A[left2]] == 1:
w2.pop(A[left2])
elif w2[A[left2]] > 1:
w2[A[left2]] -= 1
left2 += 1
count += left2 - left1
return count
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
result = 0
i, j = 0, 0
cnt = collections.Counter()
l = 0
n = len(A)
while j < n:
if len(cnt) != K:
cnt[A[j]] += 1
if len(cnt) == K:
l = 1
while cnt[A[i]] > 1:
cnt[A[i]] -= 1
i += 1
l += 1
result += l
elif A[j] in cnt:
cnt[A[j]] += 1
while cnt[A[i]] > 1:
cnt[A[i]] -= 1
i += 1
l += 1
result += l
else:
cnt[A[i]] -= 1
if cnt[A[i]] == 0:
cnt.pop(A[i])
cnt[A[j]] += 1
i += 1
l = 1
while cnt[A[i]] > 1:
cnt[A[i]] -= 1
i += 1
l += 1
result += l
j += 1
return result
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR
|
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
Return the number of good subarrays of A.
Β
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Β
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
|
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
good_start, bad_start = -1, -1
good_count, bad_count = {}, {}
def add(count, element):
if element not in count:
count[element] = 0
count[element] += 1
def remove(count, element):
count[element] -= 1
if count[element] == 0:
del count[element]
def find_good_start(A, K, cur_pos, index, good_count):
if len(good_count) == K:
return index
cur = index
while cur < cur_pos and len(good_count) > K:
cur += 1
remove(good_count, A[cur])
return cur
total = 0
for i in range(len(A)):
cur = A[i]
add(good_count, cur)
add(bad_count, cur)
if len(good_count) >= K:
good_start = find_good_start(A, K, i, good_start, good_count)
bad_start = find_good_start(A, K - 1, i, bad_start, bad_count)
if len(good_count) == K and len(bad_count) == K - 1:
total += bad_start - good_start
return total
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR DICT DICT FUNC_DEF IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
str1 = "First"
str2 = "Second"
def rev(c, cnt):
if cnt % 2 == 0:
return c
if c == "0":
c = "1"
else:
c = "0"
return c
def solve():
n = int(input())
a = input()
b = input()
ans = list()
l, r = 0, n - 1
for i in range(n):
if i % 2 == 0:
if rev(a[l], i) == b[n - i - 1]:
ans.append(1)
l += 1
else:
if rev(a[r], i) == b[n - i - 1]:
ans.append(1)
r -= 1
ans.append(n - i)
print(len(ans), sep=" ", end=" ")
for num in ans:
print(num, sep=" ", end=" ")
print()
def main():
t = int(input())
for i in range(t):
solve()
main()
|
ASSIGN VAR STRING ASSIGN VAR STRING FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN VAR IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
a = list(input())
b = list(input())
ans = []
l = 0
r = n - 1
for i in range(n - 1, -1, -1):
if a[i] != b[i]:
r = i
break
else:
r = 0
if l == r:
if a[0] == b[0]:
print(0)
else:
print(1, 1)
continue
else:
flip = False
count = r
while l != r:
if flip == False:
if a[l] == b[count]:
if a[l] == "0":
a[l] = "1"
else:
a[l] = "0"
ans.append(1)
ans.append(count + 1)
else:
ans.append(count + 1)
flip = True
else:
if a[r] != b[count]:
if a[r] == "0":
a[r] = "1"
else:
a[r] = "0"
ans.append(1)
ans.append(count + 1)
else:
ans.append(count + 1)
flip = False
for i in range(count, -1, -1):
if flip == True:
if a[l + count - i] == b[i]:
l = l + count - i
count = i
break
elif a[r - count + i] != b[i]:
r = r - count + i
count = i
break
else:
if flip == False:
l = r
else:
r = l
if flip == False:
if a[l] != b[0]:
ans.append(1)
elif a[r] == b[0]:
ans.append(1)
ans = [len(ans)] + ans
print(*ans)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
def order(s):
init = s[0]
res = []
for i in range(len(s) - 1):
if s[i] != s[i + 1]:
init = s[i + 1]
res.append(i + 1)
if init == "1":
res.append(len(s))
return res
for _ in range(int(input())):
n = int(input())
a = input()
b = input()
res = order(a) + order(b)[::-1]
print(len(res), *res)
|
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
t = int(input())
while t:
t -= 1
n = int(input())
l1 = input()
l2 = input()
ans = []
revert = False
index = -1
for i in range(n):
if i % 2 == 0:
index += 1
c1 = index
else:
c1 = n - index - 1
if l1[c1] != l2[n - i - 1]:
if i % 2 == 0:
ans.append(n - i)
else:
ans.append(1)
ans.append(n - i)
elif i % 2 == 0:
ans.append(1)
ans.append(n - i)
else:
ans.append(n - i)
print(len(ans), end=" ")
print(*ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
for _ in range(int(input())):
n = int(input())
s = input()
s += "0"
t = input()
t += "0"
t = t[::-1]
result = []
for i in range(n):
if s[i] != s[i + 1]:
result.append(i + 1)
for j in range(n):
if t[j] != t[j + 1]:
result.append(n - j)
print(len(result), *result)
|
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 STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
t = int(input())
for i in range(t):
n = int(input())
a = str(input())
b = str(input())
a = a + "0"
b = b + "0"
ans1 = []
ans2 = []
for j in range(1, n + 1):
if a[j] != a[j - 1]:
ans1.append(j)
if b[j] != b[j - 1]:
ans2.append(j)
l = len(ans2)
for j in range(1, l + 1):
ans1.append(ans2[-j])
k = len(ans1)
lis = [k]
for j in range(k):
lis.append(ans1[j])
print(" ".join(str(x) for x in lis))
|
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 BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
from _collections import deque
for _ in range(int(input())):
n = int(input())
a = input()
b = input()
c = []
ans = 0
for i in range(1, n):
if a[i - 1] is not a[i]:
c.append(i)
ans += 1
if a[-1] is not b[-1]:
c.append(n)
ans += 1
for i in range(n - 2, -1, -1):
if b[i] is not b[i + 1]:
c.append(i + 1)
ans += 1
print(ans, end=" ")
for i in c:
print(i, 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
from sys import stdin
t = int(stdin.readline().strip())
for _ in range(t):
n = int(stdin.readline().strip())
a = stdin.readline().strip()
b = stdin.readline().strip()
out = []
for i in range(n - 1):
if a[i] != a[i + 1]:
out.append(i + 1)
current = a[-1]
for i in range(n - 1, -1, -1):
if b[i] != current:
out.append(i + 1)
current = "0" if current == "1" else "1"
print(len(out), end=" ")
print(*out)
|
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING STRING STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
def flip(a, flip):
if flip % 2 == 0:
return a
elif a == 1:
return 0
else:
return 1
for _ in range(int(input())):
n = int(input())
a = list(map(int, list(input())))
b = list(map(int, list(input())))
i = 0
j = n - 1
ans = []
flipp = 0
while j >= 0:
if flip(a[i], flipp) != b[j]:
ans.append(j + 1)
else:
ans.append(1)
ans.append(j + 1)
flipp += 1
if i == 0:
a.pop(0)
i = j - 1
else:
a.pop()
i = 0
j -= 1
ans.insert(0, len(ans))
print(*ans)
|
FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN VAR IF VAR NUMBER RETURN NUMBER RETURN 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 FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
n = int(stdin.readline())
a = stdin.readline().strip()
b = stdin.readline().strip()
ans = []
idx = 0
flip = False
for i in range(n - 1, -1, -1):
if not flip and a[idx] == b[i] or flip and a[idx] != b[i]:
ans.append(1)
ans.append(i + 1)
if flip:
idx -= i
else:
idx += i
flip = not flip
stdout.write(str(len(ans)) + " ")
if len(ans) > 0:
stdout.write(" ".join(map(str, ans)) + "\n")
|
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 LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
t = int(input())
for _ in range(t):
n = int(input())
a = input()
b = input()
aToAllOnesOrZeros = []
for i in range(n - 1):
if a[i] != a[i + 1]:
aToAllOnesOrZeros.append(i + 1)
bToAllOnesOrZeros = []
for i in range(n - 1):
if b[i] != b[i + 1]:
bToAllOnesOrZeros.append(i + 1)
res = aToAllOnesOrZeros
if a[-1] != b[-1]:
res.append(n)
for i in range(len(bToAllOnesOrZeros) - 1, -1, -1):
res.append(bToAllOnesOrZeros[i])
l = len(res)
res.insert(0, l)
print(" ".join([str(x) for x in 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
for _ in range(int(input())):
n = int(input())
a = input()
l1 = list(a)
b = input()
l2 = list(b)
t1 = []
t2 = []
i = 0
while i < n:
while i < n and l1[i] == "0":
i += 1
if i == n:
break
if i > 0:
t1.append(i - 1)
while i < n and l1[i] == "1":
l1[i] = "0"
i += 1
t1.append(i - 1)
i = 0
while i < n:
while i < n and l2[i] == "0":
i += 1
if i == n:
break
if i > 0:
t2.append(i - 1)
while i < n and l2[i] == "1":
l2[i] = "0"
i += 1
t2.append(i - 1)
t2.reverse()
t = t1 + t2
print(len(t), end=" ")
for i in range(len(t)):
print(t[i] + 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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
for _ in range(int(input())):
n = int(input())
a = input()
b = input()
flag = 0
lft = 0
rht = n - 1
res = []
for i in range(n - 1, -1, -1):
if flag == 0:
if b[i] == a[lft]:
res.append(1)
lft += 1
else:
if b[i] != a[rht]:
res.append(1)
rht -= 1
res.append(i + 1)
flag = 1 - flag
print(len(res), *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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
def main(r):
n = int(input())
a = input()
b = input()
st = 0
end = n - 1
ln = 0
swaps = ""
if len(a) == 1:
if a == b:
print(0)
else:
print("1 1")
return
else:
for i in range(0, n):
if i % 2 == 0:
if a[st] == b[-(1 + i)]:
ln += 1
swaps += "1 "
st += 1
else:
if a[end] != b[-(1 + i)]:
swaps += "1 "
ln += 1
end -= 1
ln += 1
swaps += str(n - i) + " "
print(ln, swaps)
t = int(input())
for i in range(t):
main(i)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR STRING VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
def get_flipped(x, flips):
if flips % 2 == 0:
return x
else:
return "0" if x == "1" else "1"
def solve():
n = int(input().strip())
a = input().strip()
b = input().strip()
ar = []
for i in range(n):
this_bit = b[n - i - 1]
first_bit = get_flipped(a[i // 2] if i % 2 == 0 else a[n - 1 - i // 2], i)
if first_bit == this_bit:
ar.append(1)
ar.append(n - i)
print(len(ar), *ar)
t = int(input().strip())
for _ in range(t):
solve()
|
FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN VAR RETURN VAR STRING STRING STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
for _ in range(int(input())):
n = int(input())
a = input()
b = input()
a += "0"
b += "0"
l1, l2 = [], []
for i in range(1, n + 1):
if a[i] != a[i - 1]:
l1.append(i)
if b[i] != b[i - 1]:
l2.append(i)
l2.reverse()
l3 = l1 + l2
print(len(l3), sep=" ", end=" ")
for i in l3:
print(i, sep=" ", end=" ")
print(sep="")
|
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 VAR STRING VAR STRING ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING EXPR FUNC_CALL VAR STRING
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
for _ in range(int(input())):
n = int(input())
a = [int(x) for x in input()]
b = [int(x) for x in input()]
a.append(0)
b.append(0)
ans = []
for i in range(n):
if a[i] != a[i + 1]:
ans.append(i + 1)
for i in range(n - 1, -1, -1):
if b[i] != b[i + 1]:
ans.append(i + 1)
print(len(ans), end=" ")
for x in ans:
print(x, 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 VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
for _ in range(int(input())):
n = int(input())
a = []
b = []
a1 = list(input())
b1 = list(input())
for i in a1:
a.append(int(i))
for i in b1:
b.append(int(i))
pos1 = 0
pos2 = n - 1
reverse = 0
ans = []
for i in range(n - 1, -1, -1):
if not reverse:
if a[pos1] == b[i]:
ans.append(1)
ans.append(i + 1)
reverse = 1
pos1 += 1
else:
if a[pos2] != b[i]:
ans.append(1)
ans.append(i + 1)
reverse = 0
pos2 -= 1
print(len(ans), end=" ")
for i in ans:
print(i, end=" ")
print()
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
t = int(input())
for _ in range(t):
n = int(input())
a = input()
b = input()
al = 0
ah = len(a) - 1
ac = len(a) - 1
ao = 0
is_flipped = False
res = []
for i, c in enumerate(reversed(b)):
if (c == a[ac]) ^ is_flipped:
if ac == ah:
ah -= 1
ac -= 1
else:
al += 1
ac += 1
continue
if (c != a[ao]) ^ is_flipped:
res.append(ah - al + 1)
if ac == ah:
ac, ao = ao, ac
ac += 1
al += 1
else:
ac, ao = ao, ac
ac -= 1
ah -= 1
is_flipped = not is_flipped
continue
else:
res.append(1)
res.append(ah - al + 1)
if ac == ah:
ac, ao = ao, ac
ac += 1
al += 1
else:
ac, ao = ao, ac
ac -= 1
ah -= 1
is_flipped = not is_flipped
continue
print(len(res), *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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input()))
b = list(map(int, input()))
ans = []
f = 0
l, r = 0, n - 1
i = n - 1
while l <= r:
if f == 0:
if a[r] ^ f != b[i]:
if a[l] ^ f == b[i]:
ans.append(1)
ans.append(r - l + 1)
f ^= 1
l += 1
else:
r -= 1
elif a[l] ^ f != b[i]:
if a[r] ^ f == b[i]:
ans.append(1)
ans.append(r - l + 1)
f ^= 1
r -= 1
else:
l += 1
i -= 1
print(len(ans), *ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
def convert_to_1(s):
L = []
for i in range(len(s) - 1):
if s[i] != s[i + 1]:
L.append(i + 1)
if s[-1] == "0":
L.append(len(s))
return L
for i in " " * int(input()):
n = int(input())
s1 = input()
s2 = input()
L1 = convert_to_1(s1)
L2 = convert_to_1(s2)
L = L1 + L2[::-1]
print(len(L), end=" ")
for i in L:
print(i, end=" ")
print()
|
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR FOR VAR BIN_OP STRING 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 ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
t = int(input())
for i in range(t):
n = int(input())
a = list(input())
a = list(map(int, a))
b = list(input())
b = list(map(int, b))
c = []
lst1 = []
i = 0
j = n - 1
idx = 0
while len(c) < n:
if idx % 2 == 0:
c.append(a[i])
i += 1
idx += 1
else:
if a[j] == 0:
c.append(1)
else:
c.append(0)
j -= 1
idx += 1
ans = 0
for i in range(n):
if c[i] == b[n - i - 1]:
ans += 2
lst1.append(1)
lst1.append(n - i)
else:
ans += 1
lst1.append(n - i)
print(ans, end=" ")
for i in range(len(lst1)):
print(lst1[i], 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
for _ in range(int(input())):
n = int(input())
l1 = list(map(int, input()))
l2 = list(map(int, input()))
flip = False
idx = 0
ans = []
for i in range(n - 1, -1, -1):
if flip ^ (l1[idx] == l2[i]):
ans.append(1)
ans.append(i + 1)
if flip:
idx -= i
else:
idx += i
flip = not flip
print(len(ans), *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 VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
t = int(input())
for _ in range(t):
n = int(input())
list1 = list(map(int, input()))
list2 = list(map(int, input()))
ans = []
start = 0
invert = 0
for i in range(n - 1, 0 - 1, -1):
aim = list2[i]
if (
invert % 2 == 0
and list1[start + i] == aim
or invert % 2 != 0
and list1[start - i] != aim
):
continue
if (
invert % 2 == 0
and list1[start] == aim
or invert % 2 != 0
and list1[start] != aim
):
ans.append(1)
ans.append(i + 1)
else:
ans.append(i + 1)
if invert % 2 == 0:
start = start + i
else:
start = start - i
invert += 1
print(len(ans), end=" ")
for val in ans:
print(val, 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 FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
t = int(input())
while t > 0:
t -= 1
n = int(input())
s1 = input()
s2 = input()
l = 0
r = n - 1
rev = False
ans = []
for x in s2[-1::-1]:
if not rev:
if s1[r] == x:
r -= 1
continue
if s1[l] == x:
ans.append(1)
ans.append(r - l + 1)
l += 1
rev = True
else:
if s1[l] != x:
l += 1
continue
if s1[r] != x:
ans.append(1)
ans.append(r - l + 1)
r -= 1
rev = False
print(len(ans))
for x in ans:
print(x, end=" ")
print()
|
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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR NUMBER NUMBER IF VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, list(input())))
b = list(map(int, list(input())))
ans = []
a0 = a[0]
j = n - 1
k = 1
for i in range(n - 1, -1, -1):
if b[i] == a0:
ans.append(1)
ans.append(i + 1)
if (n - i) % 2 == 1:
a0 = (a[j] + 1) % 2
j -= 1
else:
a0 = a[k]
k += 1
print(len(ans), *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 VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
def find(s1, s2):
if len(s1) == 1:
if s1 == s2:
return []
else:
return [1]
indices = [-1] * len(s1)
cur = len(s1) - 1
val = 0
for i in range(len(s1)):
indices[cur] = val
cur -= 2
val += 1
if cur < 0:
break
if indices[0] == -1:
cur = 0
for i in range(len(s1)):
indices[cur] = val
val += 1
cur += 2
if cur > len(s1) - 1:
break
else:
cur = 1
for i in range(len(s1)):
indices[cur] = val
val += 1
cur += 2
if cur > len(s1) - 1:
break
ans = []
temp = 1
for i in range(len(s1) - 1, -1, -1):
if (int(s1[indices[i]]) + temp) % 2 == int(s2[i]) % 2:
ans += [i + 1]
else:
ans += [1, i + 1]
temp += 1
return ans
ANS = []
for _ in range(int(input())):
input()
s1 = input()
s2 = input()
ans = find(s1, s2)
ans = [str(x) for x in ans]
ans = [str(len(ans))] + ans
ANS += [" ".join(ans)]
print("\n".join(ANS))
|
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN LIST RETURN LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR LIST BIN_OP VAR NUMBER VAR LIST NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR LIST 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 FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR LIST FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
import sys
def input():
return sys.stdin.readline().rstrip()
def input_split():
return [int(i) for i in input().split()]
testCases = int(input())
answers = []
def update_zero(arr, diff, num_flips):
if num_flips % 2 == 0:
i = -diff
arr[i] = 1 - arr[i]
else:
i = diff
arr[i] = 1 - arr[i]
def fetch_val(arr, index, diff, num_flips):
if num_flips % 2 == 0:
i = -diff + index
ans = arr[i]
else:
i = diff - index
ans = 1 - arr[i]
return ans
def print_arr(arr, diff, num_flips, ans_till_now):
temp = [fetch_val(arr, i, diff, num_flips) for i in range(ans_till_now[-1])]
temp = temp + arr[ans_till_now[-1] :]
print("array is ")
print(*temp, sep=" ")
for _ in range(testCases):
n = int(input())
arr = [int(i) for i in input()]
barr = [int(i) for i in input()]
ans = []
num_flips = 0
diff = 0
for i in range(n):
index = n - 1 - i
desired = barr[index]
if fetch_val(arr, index, diff, num_flips) == desired:
continue
else:
target = fetch_val(arr, 0, diff, num_flips)
if target == 1 - desired:
ans.append(index + 1)
num_flips += 1
diff = index - diff
else:
ans.append(1)
update_zero(arr, diff, num_flips)
ans.append(index + 1)
num_flips += 1
diff = index - diff
answers.append(ans)
for ans in answers:
print(len(ans), end=" ")
print(*ans, sep=" ")
|
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR FUNC_DEF IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
import sys
r = sys.stdin.readline
def revAndnot(bit, idx):
tmp = ""
for i in range(idx):
tmp += "1" if bit[i] == "0" else "0"
return tmp[::-1] + bit[idx:]
for _ in range(int(r())):
n = int(r())
a = r().strip()
b = r().strip()
ans = []
flip = False
idx = 0
for i in range(n - 1, -1, -1):
if flip ^ (a[idx] == b[i]):
ans.append(str(1))
ans.append(str(i + 1))
if flip:
idx -= i
else:
idx += i
flip ^= True
print(len(ans), end=" ")
print(" ".join(ans))
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR STRING STRING STRING RETURN BIN_OP VAR NUMBER 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input()))
b = list(map(int, input()))
l, r = 0, n - 1
f = 0
o = []
while l != r:
k = abs(r - l)
if a[r] ^ f != b[k]:
if a[l] ^ f == b[k]:
o += (1,)
o += (k + 1,)
f ^= 1
l += 1 if r > l else -1
l, r = r, l
else:
r += -1 if r > l else 1
if a[l] ^ f != b[0]:
o += (1,)
print(len(o), *o)
|
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 VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
def task():
n = int(input())
a = input()
b = input()
answer = []
j = len(b) - 1
for i in range(len(a)):
if i % 2 == 0:
i1 = i // 2
else:
i1 = n - 1 - i // 2
c = a[i1]
if i % 2 != 0:
c = "0" if c == "1" else "1"
if c == b[j]:
answer.append(1)
answer.append(j + 1)
j -= 1
print(len(answer), end=" ")
print(*answer)
n = int(input())
for _ in range(n):
task()
|
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 BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR STRING STRING STRING IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
for i in range(int(input())):
n = int(input())
a = input() + "0"
b = input() + "0"
aToZero = []
bToZero = []
for i in range(0, n):
if a[i] != a[i + 1]:
aToZero.append(i + 1)
if b[i] != b[i + 1]:
bToZero.append(i + 1)
aToZero += bToZero[::-1]
print(len(aToZero), end=" ")
print(*aToZero)
|
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 STRING ASSIGN VAR BIN_OP FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
t = int(input())
while t:
t -= 1
n = int(input())
s1 = list(map(int, input()))
s2 = list(map(int, input()))
ans = []
start = 0
end = n - 1
flipped = 0
for i in range(n - 1, -1, -1):
if end > start:
if s1[end] == s2[i]:
end -= 1
continue
if s1[start] == s2[i]:
ans.append(1)
s1[start] = 1 - s1[start]
ans.append(i + 1)
end, start = start, end
end += 1
flipped += 1
elif end < start:
if 1 - s1[end] == s2[i]:
end += 1
continue
if 1 - s1[start] == s2[i]:
ans.append(1)
s1[start] = 1 - s1[start]
ans.append(i + 1)
end, start = start, end
end -= 1
flipped += 1
elif flipped % 2 ^ s1[end] != s2[i]:
ans.append(i + 1)
print(len(ans), *ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR IF BIN_OP NUMBER VAR VAR VAR VAR VAR NUMBER IF BIN_OP NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
for tc in range(int(input())):
n = int(input())
A = input()
B = input()
steps = []
for i in range(n):
if i % 2 == 0:
if A[int(i / 2)] != B[n - 1 - i]:
steps.append(n - i)
else:
steps.append(1)
steps.append(n - i)
elif A[n - 1 - int(i / 2)] == B[n - 1 - i]:
steps.append(n - i)
else:
steps.append(1)
steps.append(n - i)
print(len(steps), end=" ")
for i in steps:
print(i, 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
n = int(input())
a = input()
b = input()
scheck = []
pos = 0
pos1 = n - 1
reverse = 0
for j in range(n - 1, -1, -1):
if not reverse:
if a[pos] != b[j]:
scheck.append(j + 1)
else:
scheck.append(1)
scheck.append(j + 1)
pos += 1
reverse = 1
else:
if a[pos1] != b[j]:
scheck.append(1)
scheck.append(j + 1)
else:
scheck.append(j + 1)
pos1 -= 1
reverse = 0
print(len(scheck), *scheck)
|
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 VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
import sys
from sys import stdin
tt = int(input())
for loop in range(tt):
n = int(input())
a = input()
b = input()
if n == None:
if a[0] == b[0]:
print(0)
else:
print(1, 1)
sys.exit()
ind = [None] * n
tmp = 0
for i in range(n - 1, -1, -2):
ind[i] = tmp
tmp += 1
if n % 2 == 0:
for i in range(0, n, 2):
ind[i] = tmp
tmp += 1
else:
for i in range(1, n, 2):
ind[i] = tmp
tmp += 1
ans = []
x = 0
for i in range(n - 1, -1, -1):
if x == 0 and b[i] == a[ind[i]]:
ans.append(1)
elif x == 1 and b[i] != a[ind[i]]:
ans.append(1)
ans.append(i + 1)
x ^= 1
print(len(ans), *ans)
|
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 ASSIGN VAR FUNC_CALL VAR IF VAR NONE IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
for _ in range(int(input())):
n = int(input())
arr = list(input())
brr = list(input())
a = 0
b = n - 1
k = 0
ans = []
for i in range(n - 1, -1, -1):
if k == 0:
if arr[a] == brr[i]:
ans.append(1)
ans.append(i + 1)
k = 1
a += 1
else:
if arr[b] != brr[i]:
ans.append(1)
ans.append(i + 1)
k = 0
b -= 1
print(len(ans), *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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.
For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.
Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible.
-----Input-----
The first line contains a single integer $t$ ($1\le t\le 1000$) Β β the number of test cases. Next $3t$ lines contain descriptions of test cases.
The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) Β β the length of the binary strings.
The next two lines contain two binary strings $a$ and $b$ of length $n$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.
-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
-----Note-----
In the first test case, we have $01\to 11\to 00\to 10$.
In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
|
for _ in range(int(input())):
n = int(input())
A = input()
B = input()
a = []
b = []
for i in A:
a.append(i)
for i in B:
b.append(i)
c = []
cnt = 0
x = 0
y = n - 1
for i in range(n - 1, -1, -1):
if cnt % 2 == 0:
if b[i] == "1":
if a[x] == "1":
c.append(1)
c.append(i + 1)
else:
c.append(i + 1)
elif a[x] == "0":
c.append(1)
c.append(i + 1)
else:
c.append(i + 1)
x += 1
else:
if b[i] == "1":
if a[y] == "0":
c.append(1)
c.append(i + 1)
else:
c.append(i + 1)
elif a[y] == "1":
c.append(1)
c.append(i + 1)
else:
c.append(i + 1)
y -= 1
cnt += 1
print(len(c), end=" ")
for i in c:
print(i, 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR STRING IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.