description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
n = int(input()) for i in range(n): n, k = map(int, input().split()) a = set(list(map(int, input().split()))) n = n + k + 1 escp = [] for l in range(n): if l not in a: escp.append(l) print(escp[k])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
for _ in range(int(input())): n, k = map(int, input().split()) l = list(map(int, input().split())) mx = max(l) l = set(l) con = False for i in range(mx): if k != 0: if i not in l: k -= 1 elif i not in l: print(i) con = True break if k != 0: print(mx + k + 1) if k == 0 and con == False: print(mx + 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
def mex(set_size, add_num, d): max_num = max(d.keys()) for i in range(0, max_num + add_num + 1): if i not in d: if add_num == 0: return i else: d[i] = 1 add_num -= 1 return max(d.keys()) + 1 n = int(input()) for i in range(n): set_size, add_num = [int(i) for i in input().split()] set_ = [int(i) for i in input().split()] d = {} for i in set_: d[i] = 1 print(mex(set_size, add_num, d))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
t = int(input()) while t: n, k = input().split() k = int(k) n = int(n) l = set(list(map(int, input().split()))) for i in range(n + k + 1): if i not in l: if k > 0: k -= 1 else: print(i) break t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
for _ in range(int(input())): n, k = list(map(int, input().split())) s = set(map(int, input().split())) for i in range(0, 2 * 10**5 + 1): if i not in s: if k != 0: k -= 1 elif k == 0: print(i) break
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER IF VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
for _ in range(int(input())): n, k = map(int, input().split(" ")) l = list(map(int, input().split(" "))) m = max(l) a = [0] * (m + 1) for i in range(n): a[l[i]] += 1 x = a.count(0) if x > k: i = 0 k += 1 while k > 0: if a[i] == 0: k -= 1 i += 1 print(i - 1) else: k -= x print(m + k + 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
for _ in range(int(input())): N, K = map(int, input().split()) S = list(map(int, input().split())) Z = [] for i in range(0, max(S) + 1): Z.append(i) S.sort() Z.sort() S = set(S) Z = set(Z) Y = list(Z - Z.intersection(S)) if K <= len(Y) - 1: print(Y[K]) else: print(max(S) + K + 1 - len(Y))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
def ayush(N, K, arr): no = k h = 0 for j in range(N + K + 1): if j not in arr: if K > 0: K -= 1 else: print(j) break t = int(input()) for i in range(t): arr1 = input().split() n = int(arr1[0]) k = int(arr1[1]) arr2 = set(list(map(int, input().split()))) ayush(n, k, arr2)
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
t = int(input()) while t != 0: n, k = map(int, input().split()) s = set(list(map(int, input().split()))) m = 0 while True: if m in s: m = m + 1 elif k > 0: k = k - 1 m = m + 1 else: break print(m) t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
for t in range(int(input())): n, k = map(int, input().split()) s = sorted(map(int, input().split())) if len(list(set(range(0, s[-1])) - set(s))) > k: print(list(set(range(0, s[-1])) - set(s))[k]) else: print(s[-1] + 1 + (k - len(list(set(range(0, s[-1])) - set(s)))))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
def sol(n, k, l): s = set(l) i = 0 while 1: if i in s: i += 1 elif k > 0: k -= 1 i += 1 else: return i for tc in range(int(input())): n, k = map(int, input().split()) l = list(map(int, input().split())) print(sol(n, k, l))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
def mex(nList): nList = set(nList) mex = 0 while mex in nList: mex += 1 return mex for i in range(int(input())): n, k = map(int, input().split()) arr = set(map(int, input().split())) mex = 0 while True: if mex in arr: mex += 1 elif k > 0: mex += 1 k -= 1 else: break print(mex)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
for i in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) m = max(a) c = [0] * (m + 2 + k) for i in a: c[i] = 1 for i in range(len(c)): if k > 0 and c[i] == 0: c[i] = 1 k -= 1 if k == 0: break print(c.index(0))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER VAR FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
T = int(input()) for x in range(T): n, k = map(int, input().split()) s = list(map(int, input().split())) t = [-1] * 200001 for i in s: t[i] = i for i in range(200001): if t[i] == -1: k -= 1 if k < 0: break print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
t = int(input()) ans = [] for i in range(t): gg = {} n, k = map(int, input().split()) s = list(map(int, input().split())) for i in s: gg[i] = gg.get(i, 0) + 1 j = 0 while k != 0: if j not in gg: k -= 1 gg[j] = gg.get(j, 0) + 1 j += 1 f = 0 while True: if f not in gg: print(f) break f += 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
t = int(input()) while t: n, k = map(int, input().split(" ")) a = set(map(int, input().split(" "))) cnt = 0 flag = True while flag: if cnt in a: cnt += 1 elif k > 0: cnt += 1 k -= 1 else: print(cnt) flag = False t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
t = int(input()) for i in range(t): n, k = [int(x) for x in input().split()] s = [int(x) for x in input().split()] a = [0] * (max(s) + 1) for i in s: a[i] += 1 k1 = 0 for j in range(len(a)): if a[j] == 0: k1 += 1 if k1 > k: print(j) break if k1 <= k: print(len(a) + k - k1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
for _ in range(int(input())): n, k = map(int, input().split()) s = set(map(int, input().split())) i = 0 while k >= 0: if i not in s: if k == 0: print(i) break else: s.add(i) k -= 1 i += 1
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
for i in range(int(input())): N, K = map(int, input().split()) multiset = set(list(map(int, input().split()))) for j in range(N + K + 1): if j not in multiset: if K > 0: K -= 1 else: print(j) break
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
tests = int(input()) for i in range(tests): n, k = map(int, input().split()) num_set = set(map(int, input().split())) num = 0 while k >= 0: if num not in num_set: k -= 1 num += 1 print(num - 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
for _ in range(int(input())): n, k = map(int, input().split()) s = list(map(int, input().split())) s.sort() l = -1 for i in s: diff = i - l if diff > 1: if diff - 1 <= k: k -= diff - 1 else: print(l + k + 1) break l = i else: print(s[n - 1] + k + 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
for _ in range(int(input())): n, k = list(map(int, input().split())) l = list(map(int, input().split())) l = set(l) sample = [i for i in range(max(l) + k + 2)] for i in sample: if i not in l and k != 0: l.add(i) k -= 1 elif i not in l and k == 0: print(i) break
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
for i in range(int(input())): n, k = map(int, input().split()) s = list(map(int, input().split())) s.sort() r = 0 a = [] x = 0 flag = 0 if s[0] != 0: if k >= s[0]: k -= s[0] else: flag = 1 r = k if flag == 0: for j in range(0, len(s) - 1): if s[j + 1] - s[j] > 1: if k >= s[j + 1] - s[j] - 1: k -= s[j + 1] - s[j] - 1 x = j else: x = j break x = j if j == len(s) - 2: x += 1 r = s[x] + k + 1 print(r)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
for _ in range(int(input())): n, k = map(int, input().split()) arr = [int(i) for i in input().split()] l = list(set(arr)) l.sort() if len(l) == 1: if k < l[0]: print(k) else: k = k - l[0] print(k + l[0] + 1) elif k < l[0]: print(k) else: flag = True k = k - l[0] for i in range(1, len(l)): diff = l[i] - l[i - 1] - 1 if k < diff: print(l[i - 1] + k + 1) flag = False break elif k >= diff: k = k - diff if flag: print(l[-1] + k + 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
t = int(input()) while t > 0: n, k = map(int, input().split()) flag = 0 s = [int(i) for i in input().split()] ref = [0] * (n + k) for i in range(len(s)): if s[i] < n + k: ref[s[i]] = 1 i = 0 count = k while count: if ref[i] == 0: ref[i] = 1 count -= 1 i += 1 for i in range(len(ref)): if ref[i] == 0: print(i) flag = 1 break if flag == 0: print(n + k) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
t = int(input()) while t: n, k = map(int, input().split()) unique = set() s = list(map(int, input().split())) for i in range(n): unique.add(s[i]) i = 0 while k >= 0: if k == 0: if i not in unique: print(i) break i = i + 1 else: if i not in unique: k = k - 1 i += 1 t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
for _ in range(int(input())): n, k = map(int, input().split()) arr = list(map(int, input().split())) h = max(arr) temp = [True] * (h + 1) for i in range(n): temp[arr[i]] = False temp1 = [] for i in range(len(temp)): if temp[i]: temp1.append(i) if len(temp1) <= k: print(h + k - len(temp1) + 1) else: print(temp1[k])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
def getRslt(st: set, lst: int, lAd: int): i = 0 hld = lst + lAd while lAd != 0: if i not in st: st.add(i) lAd -= 1 i += 1 for i in range(0, hld): if i not in st: return i return i + 1 T = int(input()) while T != 0: setCnt, addCnt = [int(x) for x in input().split()] toAdd = [int(x) for x in input().split()] toAdd = sorted(toAdd) elem = set() for i in range(setCnt): elem.add(toAdd[i]) print(getRslt(elem, setCnt, addCnt)) T -= 1
FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR RETURN VAR RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
for _ in range(int(input())): n, k = map(int, input().split()) l = set(list(map(int, input().split()))) z = 0 while k != 0: if z not in l: l.add(z) z += 1 k -= 1 else: z += 1 l = list(l) l.sort() if len(l) - 1 == l[-1]: print(l[-1] + 1) else: for i in range(0, l[-1]): if i != l[i]: print(i) break
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
def main(): t = int(input()) for t in range(t): n, k = map(int, input().split(" ")) numbers = sorted(set(map(int, input().split(" ")))) mex = 0 for number in numbers: while k > 0 and number > mex: mex += 1 k -= 1 if number > mex: print(mex) break mex += 1 else: print(mex + k) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
for _ in range(int(input())): n, k = map(int, input().split()) l = set(map(int, input().split())) m = max(l) p = set(range(m + 1)) p = list(set(p) - l) if k < len(p): print(p[k]) elif k == len(p): print(m + 1) else: print(m + k + 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain. Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4. ------ Input ------ The first line of the input contains an integer T denoting the number of testcases. The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively. The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}. ------ Output ------ For each testcase, output the answer in a single line. ------ Constraints ------ 1 ≤ T ≤ 10 1 ≤ N ≤ 10^{5} 0 ≤ K ≤ 10^{5} 0 ≤ S_{i} ≤ 2 * 10^{5} ------ Subtasks ------ Subtask #1 (15 points): K=0. Subtask #2 (85 points): Original Constraints. ----- Sample Input 1 ------ 4 3 0 1 0 2 3 1 1 0 2 4 3 2 5 4 9 2 0 3 4 ----- Sample Output 1 ------ 3 4 6 0 ----- explanation 1 ------ Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3. Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) d = {} for i in a: d[i] = True m, c = 0, 0 while c <= k: if m not in d: d[m] = True c += 1 if c > k: break m += 1 print(m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The numbers $1, \, 2, \, \dots, \, n \cdot k$ are colored with $n$ colors. These colors are indexed by $1, \, 2, \, \dots, \, n$. For each $1 \le i \le n$, there are exactly $k$ numbers colored with color $i$. Let $[a, \, b]$ denote the interval of integers between $a$ and $b$ inclusive, that is, the set $\{a, \, a + 1, \, \dots, \, b\}$. You must choose $n$ intervals $[a_1, \, b_1], \, [a_2, \, b_2], \, \dots, [a_n, \, b_n]$ such that: for each $1 \le i \le n$, it holds $1 \le a_i < b_i \le n \cdot k$; for each $1 \le i \le n$, the numbers $a_i$ and $b_i$ are colored with color $i$; each number $1 \le x \le n \cdot k$ belongs to at most $\left\lceil \frac{n}{k - 1} \right\rceil$ intervals. One can show that such a family of intervals always exists under the given constraints. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 100$, $2 \le k \le 100$) — the number of colors and the number of occurrences of each color. The second line contains $n \cdot k$ integers $c_1, \, c_2, \, \dots, \, c_{nk}$ ($1 \le c_j \le n$), where $c_j$ is the color of number $j$. It is guaranteed that, for each $1 \le i \le n$, it holds $c_j = i$ for exactly $k$ distinct indices $j$. -----Output----- Output $n$ lines. The $i$-th line should contain the two integers $a_i$ and $b_i$. If there are multiple valid choices of the intervals, output any. -----Examples----- Input 4 3 2 4 3 1 1 4 2 3 2 1 3 4 Output 4 5 1 7 8 11 6 12 Input 1 2 1 1 Output 1 2 Input 3 3 3 1 2 3 2 1 2 1 3 Output 6 8 3 7 1 4 Input 2 3 2 1 1 1 2 2 Output 2 3 5 6 -----Note----- In the first sample, each number can be contained in at most $\left\lceil \frac{4}{3 - 1} \right\rceil = 2$ intervals. The output is described by the following picture: In the second sample, the only interval to be chosen is forced to be $[1, \, 2]$, and each number is indeed contained in at most $\left\lceil \frac{1}{2 - 1} \right\rceil = 1$ interval. In the third sample, each number can be contained in at most $\left\lceil \frac{3}{3 - 1} \right\rceil = 2$ intervals. The output is described by the following picture:
import sys input = lambda: sys.stdin.readline().rstrip() sys.setrecursionlimit(2 * 10**5 + 10) write = lambda x: sys.stdout.write(x + "\n") debug = lambda x: sys.stderr.write(x + "\n") writef = lambda x: print("{:.12f}".format(x)) n, k = list(map(int, input().split())) c = list(map(lambda i: int(i) - 1, input().split())) s = {} done = [0] * n ng = [0] * (n * k) ans = [-1] * n while 1: if all(done): break s = {} for i in range(n * k): if ng[i] or done[c[i]]: continue if c[i] in s: ans[c[i]] = s[c[i]], i done[c[i]] = 1 s = {} else: s[c[i]] = i count = [0] * (n * k) ok = [0] * n for u, v in ans: assert c[u] == c[v] ok[c[u]] = 1 for i in range(u, v + 1): count[i] += 1 assert all(v <= (n + k - 2) // (k - 1) for v in count) write("\n".join([" ".join(map(lambda i: str(i + 1), item)) for item in ans]))
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR
The numbers $1, \, 2, \, \dots, \, n \cdot k$ are colored with $n$ colors. These colors are indexed by $1, \, 2, \, \dots, \, n$. For each $1 \le i \le n$, there are exactly $k$ numbers colored with color $i$. Let $[a, \, b]$ denote the interval of integers between $a$ and $b$ inclusive, that is, the set $\{a, \, a + 1, \, \dots, \, b\}$. You must choose $n$ intervals $[a_1, \, b_1], \, [a_2, \, b_2], \, \dots, [a_n, \, b_n]$ such that: for each $1 \le i \le n$, it holds $1 \le a_i < b_i \le n \cdot k$; for each $1 \le i \le n$, the numbers $a_i$ and $b_i$ are colored with color $i$; each number $1 \le x \le n \cdot k$ belongs to at most $\left\lceil \frac{n}{k - 1} \right\rceil$ intervals. One can show that such a family of intervals always exists under the given constraints. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 100$, $2 \le k \le 100$) — the number of colors and the number of occurrences of each color. The second line contains $n \cdot k$ integers $c_1, \, c_2, \, \dots, \, c_{nk}$ ($1 \le c_j \le n$), where $c_j$ is the color of number $j$. It is guaranteed that, for each $1 \le i \le n$, it holds $c_j = i$ for exactly $k$ distinct indices $j$. -----Output----- Output $n$ lines. The $i$-th line should contain the two integers $a_i$ and $b_i$. If there are multiple valid choices of the intervals, output any. -----Examples----- Input 4 3 2 4 3 1 1 4 2 3 2 1 3 4 Output 4 5 1 7 8 11 6 12 Input 1 2 1 1 Output 1 2 Input 3 3 3 1 2 3 2 1 2 1 3 Output 6 8 3 7 1 4 Input 2 3 2 1 1 1 2 2 Output 2 3 5 6 -----Note----- In the first sample, each number can be contained in at most $\left\lceil \frac{4}{3 - 1} \right\rceil = 2$ intervals. The output is described by the following picture: In the second sample, the only interval to be chosen is forced to be $[1, \, 2]$, and each number is indeed contained in at most $\left\lceil \frac{1}{2 - 1} \right\rceil = 1$ interval. In the third sample, each number can be contained in at most $\left\lceil \frac{3}{3 - 1} \right\rceil = 2$ intervals. The output is described by the following picture:
import sys from sys import stdin tt = 1 for loop in range(tt): n, k = map(int, stdin.readline().split()) c = list(map(int, stdin.readline().split())) for i in range(n * k): c[i] -= 1 last = [None] * n RLC = [] for i in range(n * k): if last[c[i]] == None: last[c[i]] = i else: RLC.append((i, last[c[i]], c[i])) last[c[i]] = i RLC.sort() end = [False] * n rem = [(n + k - 2) // (k - 1)] * (n * k) ans = [None] * n for R, L, C in RLC: if end[C]: continue elif min(rem[L : R + 1]) == 0: continue else: end[C] = True ans[C] = L, R for i in range(L, R + 1): rem[i] -= 1 for i in ans: print(i[0] + 1, i[1] + 1)
IMPORT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR NONE ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR VAR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER
The numbers $1, \, 2, \, \dots, \, n \cdot k$ are colored with $n$ colors. These colors are indexed by $1, \, 2, \, \dots, \, n$. For each $1 \le i \le n$, there are exactly $k$ numbers colored with color $i$. Let $[a, \, b]$ denote the interval of integers between $a$ and $b$ inclusive, that is, the set $\{a, \, a + 1, \, \dots, \, b\}$. You must choose $n$ intervals $[a_1, \, b_1], \, [a_2, \, b_2], \, \dots, [a_n, \, b_n]$ such that: for each $1 \le i \le n$, it holds $1 \le a_i < b_i \le n \cdot k$; for each $1 \le i \le n$, the numbers $a_i$ and $b_i$ are colored with color $i$; each number $1 \le x \le n \cdot k$ belongs to at most $\left\lceil \frac{n}{k - 1} \right\rceil$ intervals. One can show that such a family of intervals always exists under the given constraints. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 100$, $2 \le k \le 100$) — the number of colors and the number of occurrences of each color. The second line contains $n \cdot k$ integers $c_1, \, c_2, \, \dots, \, c_{nk}$ ($1 \le c_j \le n$), where $c_j$ is the color of number $j$. It is guaranteed that, for each $1 \le i \le n$, it holds $c_j = i$ for exactly $k$ distinct indices $j$. -----Output----- Output $n$ lines. The $i$-th line should contain the two integers $a_i$ and $b_i$. If there are multiple valid choices of the intervals, output any. -----Examples----- Input 4 3 2 4 3 1 1 4 2 3 2 1 3 4 Output 4 5 1 7 8 11 6 12 Input 1 2 1 1 Output 1 2 Input 3 3 3 1 2 3 2 1 2 1 3 Output 6 8 3 7 1 4 Input 2 3 2 1 1 1 2 2 Output 2 3 5 6 -----Note----- In the first sample, each number can be contained in at most $\left\lceil \frac{4}{3 - 1} \right\rceil = 2$ intervals. The output is described by the following picture: In the second sample, the only interval to be chosen is forced to be $[1, \, 2]$, and each number is indeed contained in at most $\left\lceil \frac{1}{2 - 1} \right\rceil = 1$ interval. In the third sample, each number can be contained in at most $\left\lceil \frac{3}{3 - 1} \right\rceil = 2$ intervals. The output is described by the following picture:
n, k = list(map(int, input().split())) c = list(map(int, input().split())) p = [(i, c[i] - 1) for i in range(n * k)] def reduce(p): pairs = [] prev = [-1] * n counts = [0] * n pair_inds = set() pair_colors = set() q = 1 for index, color in p: if counts[color] == q and color not in pair_colors: pairs.append((prev[color], index)) pair_colors.add(color) q += 1 prev[color] = index counts[color] += 1 p2 = [] for index, color in p: if color not in pair_colors: p2.append((index, color)) return pairs, p2 p_orig = p[:] pairs = [] while len(pairs) < n: new_pairs, p2 = reduce(p) pairs += new_pairs p = p2 for pair in sorted(pairs, key=lambda pair: p_orig[pair[0]][1]): print(f"{pair[0] + 1} {pair[1] + 1}")
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER STRING BIN_OP VAR NUMBER NUMBER
The numbers $1, \, 2, \, \dots, \, n \cdot k$ are colored with $n$ colors. These colors are indexed by $1, \, 2, \, \dots, \, n$. For each $1 \le i \le n$, there are exactly $k$ numbers colored with color $i$. Let $[a, \, b]$ denote the interval of integers between $a$ and $b$ inclusive, that is, the set $\{a, \, a + 1, \, \dots, \, b\}$. You must choose $n$ intervals $[a_1, \, b_1], \, [a_2, \, b_2], \, \dots, [a_n, \, b_n]$ such that: for each $1 \le i \le n$, it holds $1 \le a_i < b_i \le n \cdot k$; for each $1 \le i \le n$, the numbers $a_i$ and $b_i$ are colored with color $i$; each number $1 \le x \le n \cdot k$ belongs to at most $\left\lceil \frac{n}{k - 1} \right\rceil$ intervals. One can show that such a family of intervals always exists under the given constraints. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 100$, $2 \le k \le 100$) — the number of colors and the number of occurrences of each color. The second line contains $n \cdot k$ integers $c_1, \, c_2, \, \dots, \, c_{nk}$ ($1 \le c_j \le n$), where $c_j$ is the color of number $j$. It is guaranteed that, for each $1 \le i \le n$, it holds $c_j = i$ for exactly $k$ distinct indices $j$. -----Output----- Output $n$ lines. The $i$-th line should contain the two integers $a_i$ and $b_i$. If there are multiple valid choices of the intervals, output any. -----Examples----- Input 4 3 2 4 3 1 1 4 2 3 2 1 3 4 Output 4 5 1 7 8 11 6 12 Input 1 2 1 1 Output 1 2 Input 3 3 3 1 2 3 2 1 2 1 3 Output 6 8 3 7 1 4 Input 2 3 2 1 1 1 2 2 Output 2 3 5 6 -----Note----- In the first sample, each number can be contained in at most $\left\lceil \frac{4}{3 - 1} \right\rceil = 2$ intervals. The output is described by the following picture: In the second sample, the only interval to be chosen is forced to be $[1, \, 2]$, and each number is indeed contained in at most $\left\lceil \frac{1}{2 - 1} \right\rceil = 1$ interval. In the third sample, each number can be contained in at most $\left\lceil \frac{3}{3 - 1} \right\rceil = 2$ intervals. The output is described by the following picture:
import sys def solve(): n, k = map(int, input().split()) arr = list(map(lambda x: int(x) - 1, input().split())) t = [[] for i in range(n)] for i in range(n * k): t[arr[i]].append(i) used = [0] * n c1 = [0] ans = [0] * n q = n // (k - 1) r = n % (k - 1) for i in range(k - 1): c1.append(c1[-1] + q + (i <= r)) for i in range(1, k): a = [] c = q + (i <= r) for j in range(n): if used[j] == 0: a.append((t[j][i], j)) a = sorted(a)[:c] for x, ind in a: used[ind] = 1 ans[ind] = t[ind][i - 1] + 1, t[ind][i] + 1 for a in ans: print(*a) input = lambda: sys.stdin.readline().rstrip() solve()
IMPORT FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR
The numbers $1, \, 2, \, \dots, \, n \cdot k$ are colored with $n$ colors. These colors are indexed by $1, \, 2, \, \dots, \, n$. For each $1 \le i \le n$, there are exactly $k$ numbers colored with color $i$. Let $[a, \, b]$ denote the interval of integers between $a$ and $b$ inclusive, that is, the set $\{a, \, a + 1, \, \dots, \, b\}$. You must choose $n$ intervals $[a_1, \, b_1], \, [a_2, \, b_2], \, \dots, [a_n, \, b_n]$ such that: for each $1 \le i \le n$, it holds $1 \le a_i < b_i \le n \cdot k$; for each $1 \le i \le n$, the numbers $a_i$ and $b_i$ are colored with color $i$; each number $1 \le x \le n \cdot k$ belongs to at most $\left\lceil \frac{n}{k - 1} \right\rceil$ intervals. One can show that such a family of intervals always exists under the given constraints. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 100$, $2 \le k \le 100$) — the number of colors and the number of occurrences of each color. The second line contains $n \cdot k$ integers $c_1, \, c_2, \, \dots, \, c_{nk}$ ($1 \le c_j \le n$), where $c_j$ is the color of number $j$. It is guaranteed that, for each $1 \le i \le n$, it holds $c_j = i$ for exactly $k$ distinct indices $j$. -----Output----- Output $n$ lines. The $i$-th line should contain the two integers $a_i$ and $b_i$. If there are multiple valid choices of the intervals, output any. -----Examples----- Input 4 3 2 4 3 1 1 4 2 3 2 1 3 4 Output 4 5 1 7 8 11 6 12 Input 1 2 1 1 Output 1 2 Input 3 3 3 1 2 3 2 1 2 1 3 Output 6 8 3 7 1 4 Input 2 3 2 1 1 1 2 2 Output 2 3 5 6 -----Note----- In the first sample, each number can be contained in at most $\left\lceil \frac{4}{3 - 1} \right\rceil = 2$ intervals. The output is described by the following picture: In the second sample, the only interval to be chosen is forced to be $[1, \, 2]$, and each number is indeed contained in at most $\left\lceil \frac{1}{2 - 1} \right\rceil = 1$ interval. In the third sample, each number can be contained in at most $\left\lceil \frac{3}{3 - 1} \right\rceil = 2$ intervals. The output is described by the following picture:
def no_intersections(numbers, colors): met_colors = {} pairs = [] for idx, color in enumerate(numbers): if color in met_colors: pairs.append([color, met_colors[color], idx]) colors.remove(color) met_colors = {} if color in colors: met_colors[color] = idx return pairs def solve(): n, k = (int(x) for x in input().split()) numbers = [int(x) for x in input().split()] repeats = (n + k - 2) // (k - 1) pairs = [] block_size = (n + repeats - 1) // repeats colors = list(range(1, n + 1)) for x in range(repeats): pairs += no_intersections( numbers, set(colors[x * block_size : x * block_size + block_size]) ) pairs = sorted(pairs, key=lambda x: x[0]) assert len(pairs) == n for pair in pairs: print(pair[1] + 1, pair[2] + 1) solve()
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR
The numbers $1, \, 2, \, \dots, \, n \cdot k$ are colored with $n$ colors. These colors are indexed by $1, \, 2, \, \dots, \, n$. For each $1 \le i \le n$, there are exactly $k$ numbers colored with color $i$. Let $[a, \, b]$ denote the interval of integers between $a$ and $b$ inclusive, that is, the set $\{a, \, a + 1, \, \dots, \, b\}$. You must choose $n$ intervals $[a_1, \, b_1], \, [a_2, \, b_2], \, \dots, [a_n, \, b_n]$ such that: for each $1 \le i \le n$, it holds $1 \le a_i < b_i \le n \cdot k$; for each $1 \le i \le n$, the numbers $a_i$ and $b_i$ are colored with color $i$; each number $1 \le x \le n \cdot k$ belongs to at most $\left\lceil \frac{n}{k - 1} \right\rceil$ intervals. One can show that such a family of intervals always exists under the given constraints. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 100$, $2 \le k \le 100$) — the number of colors and the number of occurrences of each color. The second line contains $n \cdot k$ integers $c_1, \, c_2, \, \dots, \, c_{nk}$ ($1 \le c_j \le n$), where $c_j$ is the color of number $j$. It is guaranteed that, for each $1 \le i \le n$, it holds $c_j = i$ for exactly $k$ distinct indices $j$. -----Output----- Output $n$ lines. The $i$-th line should contain the two integers $a_i$ and $b_i$. If there are multiple valid choices of the intervals, output any. -----Examples----- Input 4 3 2 4 3 1 1 4 2 3 2 1 3 4 Output 4 5 1 7 8 11 6 12 Input 1 2 1 1 Output 1 2 Input 3 3 3 1 2 3 2 1 2 1 3 Output 6 8 3 7 1 4 Input 2 3 2 1 1 1 2 2 Output 2 3 5 6 -----Note----- In the first sample, each number can be contained in at most $\left\lceil \frac{4}{3 - 1} \right\rceil = 2$ intervals. The output is described by the following picture: In the second sample, the only interval to be chosen is forced to be $[1, \, 2]$, and each number is indeed contained in at most $\left\lceil \frac{1}{2 - 1} \right\rceil = 1$ interval. In the third sample, each number can be contained in at most $\left\lceil \frac{3}{3 - 1} \right\rceil = 2$ intervals. The output is described by the following picture:
import sys input = sys.stdin.readline def solve(): n, k = map(int, input().split()) p = [None] * (n + 1) w = [False] * (n + 1) a = [] r = [None] * n i = 0 b = (n + k - 2) // (k - 1) for c in map(int, input().split()): if p[c] is not None and not w[c]: X, Y = p[c], i z = 0 for x, y in a: L = max(x, X) R = min(y, Y) if L <= R: z += 1 if z < b: a.append((X, Y)) r[c - 1] = str(X + 1) + " " + str(Y + 1) w[c] = True p[c] = i i += 1 print("\n".join(r)) solve()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NONE VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
The numbers $1, \, 2, \, \dots, \, n \cdot k$ are colored with $n$ colors. These colors are indexed by $1, \, 2, \, \dots, \, n$. For each $1 \le i \le n$, there are exactly $k$ numbers colored with color $i$. Let $[a, \, b]$ denote the interval of integers between $a$ and $b$ inclusive, that is, the set $\{a, \, a + 1, \, \dots, \, b\}$. You must choose $n$ intervals $[a_1, \, b_1], \, [a_2, \, b_2], \, \dots, [a_n, \, b_n]$ such that: for each $1 \le i \le n$, it holds $1 \le a_i < b_i \le n \cdot k$; for each $1 \le i \le n$, the numbers $a_i$ and $b_i$ are colored with color $i$; each number $1 \le x \le n \cdot k$ belongs to at most $\left\lceil \frac{n}{k - 1} \right\rceil$ intervals. One can show that such a family of intervals always exists under the given constraints. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 100$, $2 \le k \le 100$) — the number of colors and the number of occurrences of each color. The second line contains $n \cdot k$ integers $c_1, \, c_2, \, \dots, \, c_{nk}$ ($1 \le c_j \le n$), where $c_j$ is the color of number $j$. It is guaranteed that, for each $1 \le i \le n$, it holds $c_j = i$ for exactly $k$ distinct indices $j$. -----Output----- Output $n$ lines. The $i$-th line should contain the two integers $a_i$ and $b_i$. If there are multiple valid choices of the intervals, output any. -----Examples----- Input 4 3 2 4 3 1 1 4 2 3 2 1 3 4 Output 4 5 1 7 8 11 6 12 Input 1 2 1 1 Output 1 2 Input 3 3 3 1 2 3 2 1 2 1 3 Output 6 8 3 7 1 4 Input 2 3 2 1 1 1 2 2 Output 2 3 5 6 -----Note----- In the first sample, each number can be contained in at most $\left\lceil \frac{4}{3 - 1} \right\rceil = 2$ intervals. The output is described by the following picture: In the second sample, the only interval to be chosen is forced to be $[1, \, 2]$, and each number is indeed contained in at most $\left\lceil \frac{1}{2 - 1} \right\rceil = 1$ interval. In the third sample, each number can be contained in at most $\left\lceil \frac{3}{3 - 1} \right\rceil = 2$ intervals. The output is described by the following picture:
N, K = map(int, input().split()) M = 0 - -N // (K - 1) A = [(int(a) - 1) for a in input().split()] X = [[] for _ in range(N)] for i, a in enumerate(A): X[a].append(i) L = [i for i in range(N)] ANS = [(-1, -1) for _ in range(N)] c = 1 while L: Y = [] for i in L: Y.append((X[i][c], i)) Y.sort(key=lambda x: x[0]) for _, i in Y[:M]: ANS[i] = X[i][c - 1], X[i][c] L = [i for _, i in Y[M:]] c += 1 for l, r in ANS: print(l + 1, r + 1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
The numbers $1, \, 2, \, \dots, \, n \cdot k$ are colored with $n$ colors. These colors are indexed by $1, \, 2, \, \dots, \, n$. For each $1 \le i \le n$, there are exactly $k$ numbers colored with color $i$. Let $[a, \, b]$ denote the interval of integers between $a$ and $b$ inclusive, that is, the set $\{a, \, a + 1, \, \dots, \, b\}$. You must choose $n$ intervals $[a_1, \, b_1], \, [a_2, \, b_2], \, \dots, [a_n, \, b_n]$ such that: for each $1 \le i \le n$, it holds $1 \le a_i < b_i \le n \cdot k$; for each $1 \le i \le n$, the numbers $a_i$ and $b_i$ are colored with color $i$; each number $1 \le x \le n \cdot k$ belongs to at most $\left\lceil \frac{n}{k - 1} \right\rceil$ intervals. One can show that such a family of intervals always exists under the given constraints. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 100$, $2 \le k \le 100$) — the number of colors and the number of occurrences of each color. The second line contains $n \cdot k$ integers $c_1, \, c_2, \, \dots, \, c_{nk}$ ($1 \le c_j \le n$), where $c_j$ is the color of number $j$. It is guaranteed that, for each $1 \le i \le n$, it holds $c_j = i$ for exactly $k$ distinct indices $j$. -----Output----- Output $n$ lines. The $i$-th line should contain the two integers $a_i$ and $b_i$. If there are multiple valid choices of the intervals, output any. -----Examples----- Input 4 3 2 4 3 1 1 4 2 3 2 1 3 4 Output 4 5 1 7 8 11 6 12 Input 1 2 1 1 Output 1 2 Input 3 3 3 1 2 3 2 1 2 1 3 Output 6 8 3 7 1 4 Input 2 3 2 1 1 1 2 2 Output 2 3 5 6 -----Note----- In the first sample, each number can be contained in at most $\left\lceil \frac{4}{3 - 1} \right\rceil = 2$ intervals. The output is described by the following picture: In the second sample, the only interval to be chosen is forced to be $[1, \, 2]$, and each number is indeed contained in at most $\left\lceil \frac{1}{2 - 1} \right\rceil = 1$ interval. In the third sample, each number can be contained in at most $\left\lceil \frac{3}{3 - 1} \right\rceil = 2$ intervals. The output is described by the following picture:
import sys from sys import stdin, stdout try: n, k = map(int, stdin.readline().split()) c_a = list(map(int, stdin.readline().split())) ceil = (n + k - 2) // (k - 1) co_a = [[] for _ in range(n)] for i in range(len(c_a)): co_a[c_a[i] - 1].append(i) kc_a = [[] for _ in range(k)] for i in range(1, k): for j in range(n): kc_a[i].append([j, co_a[j][i - 1], co_a[j][i]]) res = {} for i in range(1, k): kc_a[i].sort(key=lambda x: x[2]) cnt = 0 for j in range(n): c, l, r = kc_a[i][j] if c in res: continue res[c] = [l + 1, r + 1] cnt += 1 if cnt >= ceil: break for i in range(n): stdout.write(str(res[i][0]) + " " + str(res[i][1]) + "\n") except: print(sys.exc_info()[0])
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER STRING FUNC_CALL VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER
The numbers $1, \, 2, \, \dots, \, n \cdot k$ are colored with $n$ colors. These colors are indexed by $1, \, 2, \, \dots, \, n$. For each $1 \le i \le n$, there are exactly $k$ numbers colored with color $i$. Let $[a, \, b]$ denote the interval of integers between $a$ and $b$ inclusive, that is, the set $\{a, \, a + 1, \, \dots, \, b\}$. You must choose $n$ intervals $[a_1, \, b_1], \, [a_2, \, b_2], \, \dots, [a_n, \, b_n]$ such that: for each $1 \le i \le n$, it holds $1 \le a_i < b_i \le n \cdot k$; for each $1 \le i \le n$, the numbers $a_i$ and $b_i$ are colored with color $i$; each number $1 \le x \le n \cdot k$ belongs to at most $\left\lceil \frac{n}{k - 1} \right\rceil$ intervals. One can show that such a family of intervals always exists under the given constraints. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 100$, $2 \le k \le 100$) — the number of colors and the number of occurrences of each color. The second line contains $n \cdot k$ integers $c_1, \, c_2, \, \dots, \, c_{nk}$ ($1 \le c_j \le n$), where $c_j$ is the color of number $j$. It is guaranteed that, for each $1 \le i \le n$, it holds $c_j = i$ for exactly $k$ distinct indices $j$. -----Output----- Output $n$ lines. The $i$-th line should contain the two integers $a_i$ and $b_i$. If there are multiple valid choices of the intervals, output any. -----Examples----- Input 4 3 2 4 3 1 1 4 2 3 2 1 3 4 Output 4 5 1 7 8 11 6 12 Input 1 2 1 1 Output 1 2 Input 3 3 3 1 2 3 2 1 2 1 3 Output 6 8 3 7 1 4 Input 2 3 2 1 1 1 2 2 Output 2 3 5 6 -----Note----- In the first sample, each number can be contained in at most $\left\lceil \frac{4}{3 - 1} \right\rceil = 2$ intervals. The output is described by the following picture: In the second sample, the only interval to be chosen is forced to be $[1, \, 2]$, and each number is indeed contained in at most $\left\lceil \frac{1}{2 - 1} \right\rceil = 1$ interval. In the third sample, each number can be contained in at most $\left\lceil \frac{3}{3 - 1} \right\rceil = 2$ intervals. The output is described by the following picture:
n, k = map(int, input().split(" ")) arr = [(int(x) - 1) for x in input().split()] m = 0 - -n // (k - 1) gp = [[] for x in range(n)] ct = 0 for x in arr: gp[x].append(ct) ct += 1 c = 1 L = [x for x in range(n)] ans = [[] for x in range(n)] while L: y = [] for x in L: y.append([gp[x][c], x]) y.sort(key=lambda x: x[0]) for x in y[:m]: ans[x[1]].append(gp[x[1]][c - 1]) ans[x[1]].append(x[0]) L = [] for x in y[m:]: L.append(x[1]) c += 1 for x in ans: print(x[0] + 1, x[1] + 1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) s = input() t = input() if s.count("1") != t.count("1"): print(-1) else: a = [] for i in range(n): if s[i] == t[i]: a.append(0) elif s[i] == "1": a.append(1) else: a.append(-1) mx = -1 mn = 10**9 sm = 0 tmax = -1 tmin = 10**9 for i in range(n): sm += a[i] mx = max(mx, sm - tmin) mn = min(mn, sm - tmax) tmin = min(tmin, sm) tmax = max(tmax, sm) print(max(mx, abs(mn)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
import sys input = sys.stdin.readline inputr = lambda: input().rstrip("\n") n = int(input()) S = inputr() T = inputr() if S.count("1") != T.count("1"): print(-1) sys.exit(0) needz = [] needo = [] for i in range(n): if S[i] == T[i]: continue if S[i] == "1": if needo: needz.append(needo.pop()) else: needz.append(None) elif needz: needo.append(needz.pop()) else: needo.append(None) print(len(needz) + len(needo))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NONE IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NONE EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
import sys input = sys.stdin.readline n = int(input()) s = input() t = input() v = 0 s1 = 0 t1 = 0 stype1 = 0 stype2 = 0 for i in range(n): if s[i] == t[i]: v += 1 if s[i] == "1" and t[i] == "0": stype1 += 1 stype2 = max(0, stype2 - 1) elif s[i] == "0" and t[i] == "1": stype2 += 1 stype1 = max(0, stype1 - 1) if s[i] == "1": s1 += 1 if t[i] == "1": t1 += 1 if v == n: print(0) elif s1 != t1: print(-1) else: print(stype1 + stype2)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
input() s0 = input() s1 = input() if s0.count("1") != s1.count("1"): print(-1) else: m0 = 0 m1 = 0 v0 = 0 v1 = 0 for i, j in zip(s0, s1): if i != j: v0 += 1 if i == "1" else -1 v1 += 1 if j == "1" else -1 m0 = max(v0, m0) m1 = max(v1, m1) print(m0 + m1)
EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR STRING NUMBER NUMBER VAR VAR STRING NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) s = [(ord(x) - ord("0")) for x in input()] t = [(ord(x) - ord("0")) for x in input()] if sum(s) != sum(t): print(-1) else: cur, mx, mn = 0, 0, 0 for i in range(n): cur += s[i] - t[i] mx = max(mx, cur) mn = min(mn, cur) print(mx - mn)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) s = input() t = input() if s.count("1") != t.count("1") or s.count("0") != t.count("0"): print(-1) exit(0) ones, zeroes = [], [] ss = 0 for i in range(0, n): if s[i] == "0" and t[i] == "1": if len(ones) == 0: ss += 1 zeroes.append(i) else: k = ones.pop() zeroes.append(k) if s[i] == "1" and t[i] == "0": if len(zeroes) == 0: ss += 1 ones.append(i) else: k = zeroes.pop() ones.append(k) print(ss)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) s = input() t = input() if s.count("1") != t.count("1"): print(-1) else: fpat = 0 spat = 0 for i in range(n): if s[i] == "0" and t[i] == "1": if fpat > 0: fpat = fpat - 1 spat = spat + 1 elif t[i] == "0" and s[i] == "1": if spat > 0: spat = spat - 1 fpat = fpat + 1 print(spat + fpat)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
rr = lambda: input().strip() n, s, t = int(rr()), rr(), rr() curr = 0 currs = [] for i in range(n): if s[i] == "1": curr += 1 if t[i] == "1": curr -= 1 currs.append(curr) if curr != 0: print(-1) else: print(max(currs) - min(currs))
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) a = str(input()) b = str(input()) v = [0] * n cur, curr = 0, 0 x, mx = 0, 0 for i in range(n): if a[i] != b[i]: if a[i] == "1": v[i] -= 1 else: v[i] = 1 if sum(v) != 0: print(-1) else: for i in range(n): cur += 1 * v[i] x = max(x, cur) cur = max(0, cur) for i in range(n): curr += -1 * v[i] mx = max(mx, curr) curr = max(0, curr) print(max(x, mx))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
from sys import gettrace, stdin if gettrace(): inputi = input else: def input(): return next(stdin)[:-1] def inputi(): return stdin.buffer.readline() def main(): n = int(inputi()) ss = [int(a) for a in input()] tt = [int(a) for a in input()] if sum(ss) != sum(tt): print(-1) return c = 0 mxc = 0 mnc = 0 for i in range(n): if ss[i % n] == tt[i % n]: continue c += ss[i % n] - tt[i % n] mxc = max(c, mxc) mnc = min(c, mnc) print(mxc - mnc) main()
IF FUNC_CALL VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) s = input() t = input() k, f, mx, mn = 0, 0, 0, 0 for i in range(n): if s[i] == "1": f += 1 if t[i] == "1": f -= 1 if s[i] == "0" and t[i] == "1": k += 1 elif s[i] == "1" and t[i] == "0": k -= 1 mx = max(mx, k) mn = min(mn, k) if f != 0: print(-1) else: print(mx - mn)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
from sys import stdin n = int(stdin.readline().strip()) s = stdin.readline().strip() t = stdin.readline().strip() lvl, M0, M1, prev = 0, 0, 0, None for i in range(n): if s[i] == t[i]: continue if not prev: prev = s[i] lvl = 1 elif s[i] == prev: lvl += 1 else: lvl -= 1 if lvl == 0: prev = None if prev == "0": M0 = max(M0, lvl) else: M1 = max(M1, lvl) if lvl > 0: print(-1) else: print(M0 + M1)
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 VAR VAR VAR NUMBER NUMBER NUMBER NONE FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NONE IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
import sys input = sys.stdin.readline n = int(input()) S = input().strip() * 2 T = input().strip() * 2 if S.count("1") != T.count("1"): print(-1) sys.exit() count = 0 MAX = 0 for i in range(2 * n): if S[i] == T[i]: continue else: if S[i] == "1": count += 1 else: count = max(0, count - 1) MAX = max(MAX, count) print(MAX)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) s = input() t = input() arr = [] ze, on = 0, 0 for i in range(n): if s[i] != t[i]: if int(s[i]): on += 1 else: ze += 1 arr.append(int(s[i])) if ze != on: print(-1) else: on = [] ze = [] res = 0 for i in arr: if i == 0: if on: on.pop() ze.append(0) else: ze.append(0) res += 1 elif ze: ze.pop() on.append(1) else: on.append(1) res += 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) s = input() t = input() count1 = 0 count2 = 0 for i in s: if i == "1": count1 += 1 for i in t: if i == "1": count2 += 1 if count1 != count2: print(-1) else: l = [] for i in range(n): if s[i] != t[i]: if s[i] == "1": l.append(1) else: l.append(-1) if l == []: print(0) exit() n = len(l) ans = 0 dp = [(0) for i in range(n)] dp[0] = l[0] for i in range(1, n): dp[i] = max(dp[i - 1] + l[i], l[i]) ans = max(dp) for i in range(n): if l[i] == 1: l[i] = -1 else: l[i] = 1 dp = [(0) for i in range(n)] dp[0] = l[0] for i in range(1, n): dp[i] = max(dp[i - 1] + l[i], l[i]) ans = max(ans, max(dp)) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
import sys input = sys.stdin.readline I = lambda: list(map(int, input().split())) (n,) = I() s = input().strip() t = input().strip() if s.count("1") != t.count("1"): print(-1) else: an = 0 if s == t: print(an) else: a = b = 0 for i in range(n): if s[i] != t[i]: if s[i] == "1": if b: b -= 1 else: an += 1 a += 1 else: if a: a -= 1 else: an += 1 b += 1 print(an)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
def answer(n, s, t): if s == t: return 0 c1s = 0 for i in s: if i == "1": c1s += 1 c1t = 0 for i in t: if i == "1": c1t += 1 if c1s != c1t: return -1 ones = 0 zeros = 0 for i in range(n): if s[i] == "1" and t[i] == "0": if zeros: zeros -= 1 ones += 1 elif s[i] == "0" and t[i] == "1": if ones: ones -= 1 zeros += 1 return zeros + ones n = int(input()) s = input() t = input() print(answer(n, s, t))
FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING IF VAR VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") INF = 10**19 MOD = 10**9 + 7 N = INT() S = input() T = input() if S.count("0") != T.count("0"): print(-1) exit() S2 = [] for i in range(N): if S[i] != T[i]: S2.append(S[i]) ans = 0 cnt0 = cnt1 = 0 for s in S2: if s == "0": cnt0 += 1 cnt1 -= 1 else: cnt0 -= 1 cnt1 += 1 cnt0 = max(cnt0, 0) cnt1 = max(cnt1, 0) ans = max(ans, cnt0, cnt1) print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) a = input() a = [int(i) for i in a] b = input() b = [int(i) for i in b] li = [] for i in range(n): if a[i] != b[i]: li.append(a[i]) c = 0 c0 = 0 c1 = 0 a.sort() b.sort() if a != b: print(-1) else: for i in range(len(li)): if li[i] == 1: c1 += 1 if c0 != 0: c0 -= 1 else: c += 1 else: c0 += 1 if c1 != 0: c1 -= 1 else: c += 1 print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
import sys input = sys.stdin.readline n = int(input()) s = list(input().rstrip()) t = list(input().rstrip()) if s.count("1") != t.count("1"): print(-1) exit() ns = [] for si, ti in zip(s, t): if si != ti: ns.append(si) ns = ns + ns d = 0 ans = 0 for item in ns: if item == "1": d += 1 else: d -= 1 if d < 0: d = 0 ans = max(ans, d) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
import sys def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s + "\n") def wi(n): sys.stdout.write(str(n) + "\n") def wia(a): sys.stdout.write(" ".join([str(x) for x in a]) + "\n") def solve(n, s, t): if s.count("1") != t.count("1"): return -1 m1 = 0 m2 = 0 balance = 0 for i in range(n): if s[i] == "1": balance += 1 if t[i] == "1": balance -= 1 m1 = max(m1, balance) m2 = min(m2, balance) return m1 - m2 def main(): n = ri() s = rs() t = rs() wi(solve(n, s, t)) main()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR STRING FUNC_DEF IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) a = list(map(int, input())) b = list(map(int, input())) c = [] if sum(a) != sum(b): print(-1) exit() for i in range(n): if a[i] != b[i]: c.append(a[i]) open1 = 0 open0 = 0 flag1 = False flag0 = False ans = 0 oooopen1 = 0 oooopen0 = 0 for i in c: if i == 0: if open1 > 0: open1 -= 1 else: open0 += 1 flag0 = True elif open0 > 0: open0 -= 1 else: open1 += 1 flag1 = True oooopen1 = max(oooopen1, open1) oooopen0 = max(oooopen0, open0) ans = max(ans, oooopen1 + oooopen0) print(ans)
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 IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) s = input() t = input() if s.count("1") != t.count("1"): print(-1) exit(0) p = [] ans = [0, 0] for i in range(n): if s[i] != t[i]: if p and s[i] != p[-1]: p.pop(-1) else: p.append(s[i]) w = int(s[i]) ans[w] = max(ans[w], len(p)) print(sum(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) s1 = input() s2 = input() mn = 0 mx = 0 c = 0 for i in range(n): if s1[i] == "1": c += 1 if s2[i] == "1": c -= 1 mn = min(mn, c) mx = max(mx, c) if c != 0: print(-1) else: print(mx - mn)
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 NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) a, b = input(), input() if sorted(a) != sorted(b): print(-1) exit(0) _00 = _01 = _10 = _11 = 0 for i in range(n): if a[i] == b[i]: continue if a[i] == "0": if _11 > 0: _11 -= 1 _10 += 1 elif _01 > 0: _01 -= 1 _00 += 1 else: _00 += 1 elif _00 > 0: _00 -= 1 _01 += 1 elif _10 > 0: _10 -= 1 _11 += 1 else: _11 += 1 if _11 != 0 or _00 != 0: print(-1) else: print(_01 + _10)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) s, t = input(), input() mx, mn, bal = -1, 10**9, 0 for i in range(n): bal += ord(s[i]) - ord("0") bal -= ord(t[i]) - ord("0") mx = max(mx, bal) mn = min(mn, bal) print(-1 if bal != 0 else mx - mn)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
inputlen = int(input()) input1 = input() input2 = input() diff = [] for i in range(inputlen): if input1[i] != input2[i]: diff.append(input1[i]) if len(diff) == 0: print(0) elif diff.count("0") != diff.count("1"): print(-1) else: maxlen0 = 0 maxlen1 = 0 curlen0 = 0 curlen1 = 0 for i in range(len(diff)): if diff[i] == "0": curlen0 += 1 maxlen0 = max(maxlen0, curlen0) curlen1 = max(curlen1 - 1, 0) if diff[i] == "1": curlen1 += 1 maxlen1 = max(maxlen1, curlen1) curlen0 = max(curlen0 - 1, 0) i = -1 while diff[i] == diff[i + 1]: if diff[i] == "0": curlen0 += 1 maxlen0 = max(maxlen0, curlen0) curlen1 = max(curlen1 - 1, 0) if diff[i] == "1": curlen1 += 1 maxlen1 = max(maxlen1, curlen1) curlen0 = max(curlen0 - 1, 0) i += 1 print(max(maxlen1, maxlen0))
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 VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) ar1 = list(map(int, input())) ar2 = list(map(int, input())) if ar1.count(1) != ar2.count(1): print(-1) else: cnt = 0 ans1 = 0 ans2 = 0 for i in range(n): if ar1[i] == 1 and ar2[i] == 0: cnt += 1 elif ar1[i] == 0 and ar2[i] == 1: cnt -= 1 ans1 = max(cnt, ans1) ans2 = min(cnt, ans2) print(ans1 - ans2)
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 IF FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
import sys n = int(input()) a = list(map(str, input().strip())) b = list(map(str, input().strip())) x = [] input = sys.stdin.readline for i in range(n): if a[i] != b[i]: x += [a[i]] zero = 0 one = 0 co = 0 for i in x: if i == "0": co += 1 if one != 0: one -= 1 zero += 1 else: zero += 1 else: co -= 1 if zero != 0: zero -= 1 one += 1 else: one += 1 if co == 0: print(zero + one) else: print(-1)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
m1, m2, bal = 0, 0, 0 n = int(input()) a = input() b = input() for i in range(n): bal += int(a[i]) bal -= int(b[i]) m1 = max(m1, bal) m2 = min(m2, bal) ans = -1 if bal != 0 else m1 - m2 print(ans)
ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) s1 = input() s2 = input() dp = [(False) for i in range(n)] end = False if s1.count("1") != s2.count("1") or s1.count("0") != s2.count("0"): print(-1) else: zero, one = 0, 0 for i in range(n): if s1[i] == "1" and s2[i] == "0": if zero: zero -= 1 one += 1 elif s1[i] == "0" and s2[i] == "1": if one: one -= 1 zero += 1 print(one + zero)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING IF VAR VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) first_string = list(input()) second_string = list(input()) a = 0 b = 0 final_answer = 0 for i in range(n): if first_string[i] == "1": a = a + 1 else: b = b + 1 if second_string[i] == "1": a = a - 1 else: b = b - 1 one_zero = [] zero_one = [] if a == 0 and b == 0: for i in range(n): if first_string[i] == "1" and second_string[i] == "0": if len(one_zero) == 0: final_answer += 1 zero_one.append(final_answer) else: poped = one_zero.pop() zero_one.append(poped) elif first_string[i] == "0" and second_string[i] == "1": if len(zero_one) == 0: final_answer += 1 one_zero.append(final_answer) else: poped = zero_one.pop() one_zero.append(poped) else: final_answer = -1 print(final_answer)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) s = input() t = input() if s.count("1") != t.count("1"): print(-1) exit() x = [s[i] for i in range(n) if s[i] != t[i]] v1, v2, w1, w2 = 0, 0, 0, 0 for i in x: if i == "0": if w2 == 0: if v1 == v2: v1 += 1 v2 += 1 else: w2 -= 1 elif v2 == 0: if w1 == w2: w1 += 1 w2 += 1 else: v2 -= 1 print(v1 + w1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR VAR IF VAR STRING IF VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) s = input() t = input() if sorted(s) != sorted(t): print(-1) exit(0) s1 = 0 s2 = 0 ans1 = 0 ans2 = 0 for i in range(len(s)): if s[i] != t[i]: if s[i] == "0": if s2 > 0: s2 -= 1 else: s1 += 1 ans1 = max(ans1, s1) elif s1 > 0: s1 -= 1 else: s2 += 1 ans2 = max(ans2, s2) print(ans1 + ans2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) todo = [(0) for i in range(n)] a = input() b = input() for i in range(n): if a[i] == b[i]: todo[i] = 0 elif a[i] == "0" and b[i] == "1": todo[i] = 1 else: todo[i] = -1 maxx1, maxx2 = 0, 0 res1, res2 = 0, 0 for i in range(n): if ( res2 == 0 and todo[i] == 1 and res1 + todo[i] <= 0 or todo[i] == -1 and res2 == 0 ): res1 += todo[i] maxx1 = max(abs(res1), maxx1) else: res2 += todo[i] maxx2 = max(abs(res2), maxx2) if res1 != 0 or res2 != 0: print(-1) else: print(maxx1 + maxx2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
import sys input = sys.stdin.readline n = int(input()) s = list(input().rstrip()) t = list(input().rstrip()) if s.count("1") != t.count("1"): print(-1) exit() s11 = 0 s10 = 0 s01 = 0 s00 = 0 for i in range(n): if s[i] != t[i]: if s[i] == "1": if s00: s00 -= 1 s01 += 1 elif s10: s10 -= 1 s11 += 1 else: s11 += 1 elif s11: s11 -= 1 s10 += 1 elif s01: s01 -= 1 s00 += 1 else: s00 += 1 print(s01 + s10)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) a = list(map(int, list(input()))) b = list(map(int, list(input()))) if sum(a) != sum(b): print(-1) exit() if a == b: print(0) exit() u = [] for i in range(n): if a[i] == b[i]: continue elif a[i] == 1: u.append(1) else: u.append(-1) k = 0 d = [] for i in range(len(u)): k += u[i] d.append(k) print(max(d) + abs(min(d)))
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 IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) s = input() t = input() o, z = 0, 0 a = 0 if s == t: print(0) elif s.count("1") != t.count("1"): print(-1) else: for i in range(n): a += int(s[i]) - int(t[i]) o = max(a, o) z = min(a, z) print(o - z)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
def help(): n = int(input()) s1 = list(input()) s2 = list(input()) if s1 == s2: print(0) return if s1.count("1") != s2.count("1"): print("-1") return operate = [] for i in range(n): if s1[i] != s2[i]: operate.append(1 if s1[i] == "1" else -1) ll = len(operate) op1 = [0] * ll op1[0] = operate[0] for i in range(1, ll): op1[i] = operate[i] + op1[i - 1] ans = abs(max(op1) - min(op1)) print(ans) help()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
def get(x): cur, mx = 0, 0 for i in range(n): cur += x * L[i] mx = max(mx, cur) if cur < 0: cur = 0 return mx n = int(input()) s = str(input()) t = str(input()) L = [] for x, y in zip(s, t): if x == y: L.append(0) elif x == "1" and y == "0": L.append(1) else: L.append(-1) if sum(L) != 0: print(-1) elif s == t: print(0) else: print(max(get(1), get(-1)))
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR STRING VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
(N,) = map(int, input().split()) s = input().strip() t = input().strip() x = y = mx = mn = c = 0 for i in range(N): if s[i] == t[i]: continue if s[i] == "1": c += 1 x += 1 else: c -= 1 y += 1 mx = max(c, mx) mn = min(c, mn) if x != y: print(-1) else: print(mx - mn)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
import sys int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def main(): inf = 10**9 n = II() s = SI() t = SI() if s.count("1") != t.count("1"): print(-1) exit() cnt = 0 mx = -inf mn = inf for c1, c2 in zip(s, t): if c1 == "1": cnt += 1 if c2 == "1": cnt -= 1 if cnt > mx: mx = cnt if cnt < mn: mn = cnt print(mx - mn) main()
IMPORT ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
import sys input = sys.stdin.readline def main(): _ = int(input()) a = list(input().rstrip()) a = list(map(int, a)) b = list(input().rstrip()) b = list(map(int, b)) if sorted(a) != sorted(b): print("-1") return c = [0, 0] ans = 0 for x, y in zip(a, b): if x == y: continue if c[x ^ 1] > 0: c[x ^ 1] -= 1 else: ans += 1 c[x] += 1 print(ans) main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) s = input() t = input() if s.count("1") != t.count("1") or s.count("0") != t.count("0"): print("-1") else: sum = 0 maxsum = 0 minsum = 0 for i in range(n): if s[i] == t[i]: continue if s[i] == "1": sum += 1 elif s[i] == "0": sum -= 1 if sum > maxsum: maxsum = sum if sum < minsum: minsum = sum print(maxsum - minsum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) s, t = input().strip(), input().strip() now, ma, mi = 0, 0, 0 for i in range(n): if s[i] == t[i]: continue if s[i] == "1": now += 1 ma = max(ma, now) else: now -= 1 mi = min(mi, now) print(ma - mi if now == 0 else -1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) a = input() b = input() ans = 0 cnt = 0 df = 0 temp = [] if a != b: for i in range(n): if a[i] != b[i]: if a[i] == "1": if df == ans: ans = ans + 1 df = df + 1 else: df = df + 1 cnt = cnt + 1 else: cnt = cnt - 1 if df > 0: df = df - 1 else: ans = ans + 1 print(ans if cnt == 0 else -1)
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 NUMBER ASSIGN VAR LIST IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) s1 = list(input()) s2 = list(input()) o1 = s1.count("1") o2 = s2.count("1") ans1 = ans2 = 0 if o1 != o2: print(-1) else: zer = one = 0 for i in range(n): if s1[i] != s2[i]: if s1[i] == "0": if one > 0: one -= 1 else: zer += 1 ans1 = max(ans1, zer) elif zer > 0: zer -= 1 else: one += 1 ans2 = max(ans2, one) print(ans1 + ans2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) s = input() t = input() ss = [] tt = [] s0 = 0 s1 = 0 t0 = 0 t1 = 0 for i in range(n): if s[i] == "0": s0 += 1 elif s[i] == "1": s1 += 1 if t[i] == "0": t0 += 1 elif t[i] == "1": t1 += 1 if s[i] == t[i]: continue else: ss.append(s[i]) tt.append(t[i]) if s0 != t0 or s1 != t1: print(-1) else: dip = 0 rise = 0 pit = 0 for i in ss: if i == "0": pit -= 1 dip = min(dip, pit) else: pit += 1 rise = max(rise, pit) print(rise - dip)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n = int(input()) s = input() t = input() s1 = 0 t1 = 0 if s == t: print(0) else: for i in range(n): if s[i] == "1": s1 += 1 if t[i] == "1": t1 += 1 if s1 != t1: print(-1) else: stype1 = 0 stype2 = 0 for i in range(n): if s[i] == "1" and t[i] == "0": stype1 += 1 stype2 = max(0, stype2 - 1) elif s[i] == "0" and t[i] == "1": stype2 += 1 stype1 = max(0, stype1 - 1) print(stype1 + stype2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
from sys import stdin, stdout rr = lambda: input().strip() rri = lambda: int(stdin.readline()) rrm = lambda: [int(x) for x in rr().split()] def sol(): n = rri() s = rr() t = rr() cnt = 0 mn = 0 mx = 0 for i in range(n): if s[i] == "1": cnt += 1 if t[i] == "1": cnt -= 1 mn = min(mn, cnt) mx = max(mx, cnt) if cnt != 0: print(-1) else: print(mx - mn) return sol()
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN EXPR FUNC_CALL VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
def solve(arr): mx = 0 curr = 0 for i in range(len(arr)): if curr < 0: curr = 0 if arr[i] == 1: curr = curr + 1 else: curr = curr - 1 mx = max(curr, mx) return mx def solve_1(arr): mx = 0 curr = 0 for i in range(len(arr)): if curr < 0: curr = 0 if arr[i] == 0: curr = curr + 1 else: curr = curr - 1 mx = max(curr, mx) return mx n = int(input()) str_1 = str(input()) str_2 = str(input()) count_1 = 0 count_2 = 0 ind_10 = [] for i in range(n): if str_1[i] == "1": count_1 = count_1 + 1 if str_2[i] == "1": count_2 = count_2 + 1 if count_1 != count_2: print("-1") else: for i in range(n): if str_1[i] == "1" and str_2[i] == "0": ind_10.append(1) if str_2[i] == "1" and str_1[i] == "0": ind_10.append(0) count = solve(ind_10) count_f = solve_1(ind_10) print(max(count, count_f))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
import sys input = sys.stdin.buffer.readline N = int(input()) S = list(input().rstrip()) T = list(input().rstrip()) def clean(P): if not P: return P pre = P[0] tmp = 0 ret = [] for p in P: if p * pre < 0: ret.append(tmp) tmp = p else: tmp += p pre = p if P[-1] * P[0] > 0: if ret: ret[0] += tmp else: ret.append(tmp) else: ret.append(tmp) return ret P = [] for i, (s, t) in enumerate(zip(S, T)): if s > t: P.append(+1) elif s < t: P.append(-1) if not P: ans = 0 elif sum(P) != 0: ans = -1 else: P = clean(P) ans = 0 while P: ans += 1 nP = [] for p in P: if p > 1: nP.append(p - 1) elif p < -1: nP.append(p + 1) P = clean(nP) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR RETURN VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR LIST FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
import sys n = int(input()) s = input() t = input() ssum = 0 tsum = 0 for i in range(n): ssum += int(s[i]) tsum += int(t[i]) if ssum != tsum: print(-1) sys.exit() nex = [0, 0] for i in range(n): if s[i] == "0" and t[i] == "1": if nex[1] > 0: nex[1] -= 1 nex[0] += 1 else: nex[0] += 1 if s[i] == "1" and t[i] == "0": if nex[0] > 0: nex[0] -= 1 nex[1] += 1 else: nex[1] += 1 print(sum(nex))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
n, s, t = int(input()), input(), input() ar = [(0 if s[i] == t[i] else 2 * (s[i] == "1") - 1) for i in range(n)] def f(x): c, s = 0, 0 for e in ar: c = max(c + e * x, e * x) s = max(s, c) return s print(max(f(1), f(-1)) if s.count("1") == t.count("1") else -1)
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR STRING NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER NUMBER
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
def solve(n, s, t): maxi = 0 mini = 0 summ = 0 for i in range(n): summ += int(s[i]) - int(t[i]) if summ > maxi: maxi = summ if summ < mini: mini = summ if summ != 0: print(-1) else: print(maxi - mini) n = int(input()) s = input() t = input() solve(n, s, t)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
for _ in range(1): n = input() s = input() t = input() a = "" c_s = 0 c_t = 0 if 1: accept_0, accept_1 = 0, 0 for x in range(len(s)): if s[x] == "1": c_s += 1 if t[x] == "1": c_t += 1 if s[x] == t[x]: continue elif s[x] == "1": if accept_0 > 0: accept_0 -= 1 accept_1 += 1 else: if accept_1 > 0: accept_1 -= 1 accept_0 += 1 if c_s != c_t: print(-1) else: print(accept_1 + accept_0)
FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
import sys input = sys.stdin.readline n = int(input()) s = input() t = input() fault1 = 0 fault2 = 0 li = [0] * n for i in range(n): if s[i] != t[i]: if s[i] == "1": fault1 += 1 li[i] += 1 else: fault2 += 1 li[i] -= 1 if fault1 != fault2: print(-1) else: for i in range(1, n): li[i] += li[i - 1] print(max(li) - min(li))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
def max_subarray_sum(x): cur = 0 mx = 0 for i in p: cur += x * i mx = max(mx, cur) if cur < 0: cur = 0 return mx n = int(input()) a = input().strip() b = input().strip() p = [] s = 0 for i in range(n): if a[i] != b[i]: if a[i] == "1": p.append(1) s += 1 else: p.append(-1) s -= 1 if s != 0: print(-1) else: print(max(max_subarray_sum(1), max_subarray_sum(-1)))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
from sys import stdin, stdout n = int(stdin.readline()) s = stdin.readline().strip() t = stdin.readline().strip() def binary_subsequence_rotation(n, s, t): scnt0 = 0 scnt1 = 0 tcnt0 = 0 tcnt1 = 0 for i in range(n): if s[i] == "0": scnt0 += 1 else: scnt1 += 1 if t[i] == "0": tcnt0 += 1 else: tcnt1 += 1 if scnt0 != tcnt0 or scnt1 != tcnt1: return -1 tl0 = 0 tl1 = 0 for i in range(n): if s[i] == t[i]: continue if s[i] == "1": if tl0 == 0: tl1 += 1 else: tl0 -= 1 tl1 += 1 elif tl1 == 0: tl0 += 1 else: tl1 -= 1 tl0 += 1 return tl0 + tl1 print(str(binary_subsequence_rotation(n, s, t)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR