description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
t = int(input()) for i in range(t): n, k = map(int, input().split()) st = input() minsum = 11 * st.count("1") if minsum == 0: print(0) continue ones = st.count("1") first = st.index("1") last = st[::-1].index("1") if last <= k: k -= last minsum -= 10 if first <= k and minsum > 1: k -= first minsum -= 1 print(minsum)
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 ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER STRING IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
for _ in [0] * int(input()): n, k = map(int, input().split()) s = input() one = s.count("1") if one == 0: print(0) continue if one == 1: for i in range(n): if s[i] == "1": index = i break if n - 1 - index <= k: print(1) continue print(11 - (index <= k)) continue for i in range(n): if s[i] == "1": index_1 = i break for i in range(n - 1, -1, -1): if s[i] == "1": index_2 = i break if n - 1 - index_2 <= k: print(11 * one - 10 - (index_1 <= k - n + index_2 + 1)) continue print(11 * one - (index_1 <= k))
FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
num = int(input()) def counter(array): res = 0 for i in range(len(array) - 1): res += array[i] * 10 + array[i + 1] return res for _ in range(num): n, k = map(int, input().split()) array = list(map(int, list(input()))) answer = counter(array) if k > 0: if array[n - 1] == 0: count = 0 for i in range(n - 1, 0, -1): if array[i] == 1: break count += 1 if count <= k and answer >= 10: k -= count answer -= 10 if answer == 0: answer += 1 if array[0] == 0: count = 0 for i in range(n - 1): if array[i] == 1: break count += 1 if count <= k and answer > 1: k -= count answer -= 1 print(answer)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
import sys def f(s): c = 0 for i in range(len(s) - 1): c += int(str(s[i]) + str(s[i + 1])) return c def aux(n, k, s): i = n - 1 while i >= 0 and s[i] != 1: i -= 1 if i < 0: return 0 elif i == 0: if k >= n - 1: s[i], s[-1] = s[-1], s[i] return f(s) return f(s) else: if k >= n - 1 - i: s[i], s[-1] = s[-1], s[i] k -= n - 1 - i j = 0 while j < n and s[j] != 1: j += 1 if j == n - 1: return f(s) else: if k >= j: s[j], s[0] = s[0], s[j] return f(s) return f(s) def main(): input = sys.stdin.readline sys.setrecursionlimit(10**6) t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = list(map(int, list(input().replace("\n", "")))) print(aux(n, k, s)) main()
IMPORT FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER 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 VAR FUNC_CALL FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
import sys input = sys.stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) s = input() count1 = 0 for i in range(n): if s[i] == "1": count1 += 1 r = i if count1 == 1: l = i if not count1: print(0) continue ans = count1 * 11 r = n - r - 1 if k >= r: ans -= 10 count1 -= 1 k -= r if k >= l and count1: ans -= 1 print(ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
for _ in range(int(input())): n, k = map(int, input().split()) a = input() p1, p2, ans = -1, -1, 0 for i in range(n): if a[i] == "1": ans += 11 p2 = i if p1 == -1: p1 = i if p2 == -1: print(ans) continue if k >= n - 1 - p2: ans -= 10 if p1 != p2 or k < n - p2 - 1: if k - (n - p2 - 1) >= p1: ans -= 1 print(ans) continue else: print(ans) continue if p1 != p2 or k < n - p2 - 1: if k >= p1: ans -= 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) mas = list(input()) d = dict() d["1"] = [] d["0"] = [] for i in range(n): d[mas[i]].append(i) if len(d["1"]) == 0: print(0) elif len(d["1"]) == 1: ans = 11 index = d["1"][0] if n - index - 1 <= k: ans -= 10 elif index <= k: ans -= 1 print(ans) else: ans = len(d["1"]) * 11 r = max(d["1"]) l = min(d["1"]) if n - r - 1 <= k: ans -= 10 k -= n - r - 1 if l <= k: ans -= 1 k -= l print(ans)
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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING LIST ASSIGN VAR STRING LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR STRING NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
for _ in range(int(input())): n, k = map(int, input().split()) s = input() count = 0 for i in s[::-1]: if i == "0": count += 1 else: break count_end = count count = 0 for i in s: if i == "0": count += 1 else: break count_start = count if s.count("1") == 0: print(0) elif s.count("1") == 1: if count_end <= k: print(1) elif count_start <= k: print(10) else: print(11) elif count_end <= k and count_end + count_start <= k: print((s.count("1") - 1) * 11) elif count_end <= k: print((s.count("1") - 1) * 11 + 1) elif count_start <= k: print((s.count("1") - 1) * 11 + 10) else: print(s.count("1") * 11)
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 ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR STRING NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR STRING NUMBER NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR STRING NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING NUMBER
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
def evaluateStr(): n, swaps = map(int, input().split()) s = str(input()) closest_front = closest_back = dist_front = dist_back = -1 ones_count = 0 flag = False for i in range(n): if s[i] == "1": ones_count += 1 closest_back = i if not flag: closest_front = i flag = True dist_front = closest_front dist_back = n - 1 - closest_back if closest_front == -1: return 0 elif ones_count == 1: if dist_back <= swaps: return 1 elif dist_front <= swaps: return 10 else: return 11 elif dist_front + dist_back <= swaps: return (ones_count - 2) * 11 + 10 + 1 elif dist_back <= swaps: return (ones_count - 1) * 11 + 1 elif dist_front <= swaps: return (ones_count - 1) * 11 + 10 else: return ones_count * 11 n = int(input()) for i in range(n): print(evaluateStr())
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
for _ in range(int(input())): n, k = map(int, input().split()) s = input() b = s.find("1") e = n - s.rfind("1") - 1 c = s.count("1") ans = c * 11 if e <= k: ans -= 10 k -= e if c != 1 and b <= k: ans -= 1 elif b <= k: ans -= 1 if c: print(ans) else: print(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 ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
for _ in range(int(input())): n, k = map(int, input().split()) a = input() if "1" in a: can = 1 ans = a.count("1") * 11 if a[::-1].index("1") <= k: k -= a[::-1].index("1") ans -= 10 if a.count("1") == 1: can = 0 if a.index("1") <= k and can: ans -= 1 print(ans) else: print(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 IF STRING VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR STRING NUMBER IF FUNC_CALL VAR NUMBER STRING VAR VAR FUNC_CALL VAR NUMBER STRING VAR NUMBER IF FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR STRING VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
T = int(input()) while T: T -= 1 n, k = [int(x) for x in input().split(" ")] a = list(input()) l = -1 c = -1 cnt = 0 for i in range(len(a)): if a[i] == "0": continue c = i cnt += 1 if l == -1: l = i r = c if l == -1 and r == -1: print(0) elif l == r: if n - r - 1 <= k: print(1) elif l <= k: print(10) else: print(11) else: ans = 0 if n - r - 1 <= k: ans += 1 if l <= k - n + r + 1: ans += 10 else: ans += 11 else: ans += 11 if l <= k: ans += 10 else: ans += 11 ans += (cnt - 2) * 11 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
def solve(): n, k = map(int, input().split()) inp = input() ones = inp.count("1") if ones == 0: return 0 front = inp.find("1") end = inp[::-1].find("1") if ones == 1: if k >= end: return 1 if k >= front: return 10 return 11 inp = list(inp) if k >= end != 0: k -= end inp[len(inp) - end - 1] = "0" inp[-1] = "1" if k >= front != 0: inp[0] = "1" inp[front] = "0" res = 0 for i in range(len(inp) - 1): res += int(inp[i] + inp[i + 1]) return res def main(): for _ in range(int(input())): print(solve()) return main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER STRING IF VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER STRING ASSIGN VAR NUMBER STRING IF VAR VAR NUMBER ASSIGN VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
t = int(input()) for c in range(t): n, k = input().split() n, k = int(n), int(k) s = input() first, last = -1, -1 sum = 0 for i in range(n): if s[i] == "1": sum += 11 if first < 0: first = i last = i if sum: lswp = n - last - 1 if first != last and first + lswp <= k: sum -= 11 elif lswp <= k: sum -= 10 elif first <= k: sum -= 1 print(sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
def spliter(): d = input() a = d.split() r = [] for i in a: k = int(i) r.append(k) return r _t = int(input()) for _ in range(_t): n = spliter() a = input() count1 = -1 count2 = -1 count3 = 0 for i in range(n[0]): if a[i] == "1": if count3 == 0: count1 = i count2 = n[0] - i - 1 count3 += 1 if count2 <= n[1]: n[1] -= count2 count2 = 0 if count1 <= n[1]: count1 = 0 if count3 == 0: print(0) continue elif count3 == 1: if count2 == 0: print(1) elif count1 == 0: print(10) else: print(11) else: resul = 11 * count3 if count1 == 0: resul -= 1 if count2 == 0: resul -= 10 print(resul)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 NUMBER IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
t = int(input()) for _ in range(t): n, x = map(int, input().split()) s = list(input()) ones, leftmost, rightmost = 0, n, -1 for i in range(n): if s[i] == "1": ones += 1 if leftmost == n: leftmost = i rightmost = i ans = 0 if ones > 0 and n - rightmost - 1 <= x: x -= n - rightmost - 1 ans += 1 ones -= 1 if ones > 0 and leftmost <= x: ans += 10 ones -= 1 ans += 11 * ones print(ans)
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 ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
def func(s): ans = 0 for i in range(len(s) - 1): ans += int(s[i : i + 2]) return ans t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input() lone = 0 rone = 0 for i in range(n): if s[i] == "1": lone = i break for i in range(n - 1, -1, -1): if s[i] == "1": rone = i break right = False left = False if rone != n - 1: if n - 1 - rone <= k: right = True k -= n - 1 - rone if right: if rone != lone: if lone != 0: if lone <= k: left = True elif lone != 0: if lone <= k: left = True ans = func(s) if ans == 0: print(0) continue if right: ans -= 10 if rone == 0: ans += 1 if left: ans -= 1 if lone == n - 1: ans += 1 print(ans)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR IF VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
from sys import stdin def input(): return stdin.readline().strip() def read_int(): return int(input()) def read_ints(): return map(int, input().split()) t = read_int() for case_num in range(t): n, k = read_ints() s = input() m = s.count("1") if m == 0: print(0) elif m == n: print(11 * (n - 1)) else: ans = 11 * m l = s.find("1") r = s.rfind("1") lneed = l rneed = n - 1 - r if k >= lneed + rneed and m > 1: ans = 11 * (m - 1) elif k >= rneed: ans = 11 * m - 10 elif k >= lneed: ans = 11 * m - 1 print(ans)
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
i = input() for a in range(0, int(i)): [n, k] = [int(x) for x in input().split()] s = input() num = s.count("1") score = num * 11 if num == 0: print(0) elif num == 1: p = s.index("1") if n - 1 - p <= k: print(1) elif p <= k: print(10) else: print(11) else: pl = s.index("1") pr = s[::-1].index("1") if pr <= k: score -= 10 k -= pr if pl <= k: score -= 1 print(score)
ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER STRING IF VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
import sys input = sys.stdin.readline def solve(): n, k = map(int, input().split()) s = input().strip() cnt = 0 f = n + 1 b = -1 for i in range(n): if s[i] == "1": cnt += 1 f = min(f, i) b = max(b, i) if cnt == 0: return 0 op1, op2 = f, n - 1 - b if f == b: if op2 <= k: return 1 elif op1 <= k: return 10 else: return 11 ans = 11 * cnt if op2 <= k: ans -= 10 k -= op2 if op1 <= k: ans -= 1 k -= op1 return ans for _ in range(int(input())): print(solve())
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
T = int(input()) q = [] for i in range(T): a = input().split() n, k = int(a[0]), int(a[1]) ins = input() data = [] for i in ins: data.append(i) q.append((n, k, data)) def solve(n, k, data): first_one = -1 last_one = -1 for idx, c in enumerate(data): if c == "1": if first_one == -1: first_one = idx if last_one < idx: last_one = idx if first_one != -1 and k >= n - last_one - 1: temp = data[n - 1] data[n - 1] = data[last_one] data[last_one] = temp k -= n - last_one - 1 if first_one == last_one: last_one = -1 if last_one != -1 and k >= first_one: temp = data[0] data[0] = data[first_one] data[first_one] = temp sol = 0 for i in range(n - 1): sol += int(data[i] + data[i + 1]) print(sol) for n, k, data in q: solve(n, k, data)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
for _ in range(int(input())): n, k = list(map(int, input().split())) s = list(input()) answ = 0 i = n - 1 while i >= 0 and s[i] != "1": i -= 1 if i == -1: print(0) continue if n - i - 1 <= k: k -= n - i - 1 s[i], s[-1] = s[-1], s[i] i = 0 while i < n - 1 and s[i] != "1": i += 1 if i != n - 1 and k >= i: s[0], s[i] = s[i], s[0] print(int(s[0]) * 10 + 11 * s[1:-1].count("1") + int(s[-1]))
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 ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP NUMBER FUNC_CALL VAR NUMBER NUMBER STRING FUNC_CALL VAR VAR NUMBER
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
for _ in range(int(input())): n, k = map(int, input().split()) l = list(map(int, input())) i = 0 j = n - 1 while j >= 0: if l[j] == 0: pass else: if k >= n - 1 - j: k -= n - 1 - j l[j] = 0 l[-1] = 1 break j -= 1 while i <= n - 2: if l[i] == 0: pass elif k >= i: k -= i l[i] = 0 l[0] = 1 break i += 1 ans = 0 for i in range(n - 1): ans += l[i] * 10 + l[i + 1] print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
for _ in range(int(input())): n, k = map(int, input().split()) s = list(input()) one = 0 b = n e = 0 for i in range(n): if s[i] == "1": one += 1 if b == n: b = i e = i offset = 0 if one > 0 and n - 1 - e <= k: one -= 1 offset += 1 k -= n - 1 - e if one > 0 and b <= k: one -= 1 offset += 10 print(11 * one + offset)
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 ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
t = int(input()) for i in range(t): n, k = map(int, input().split()) a = input() sh = 0 pe = 0 p = 0 for j in range(n): if a[j] == "1": sh += 1 if sh == 1: pe = j p = j else: p = j minus = 0 if sh > 0: if p + k >= n - 1: k = p + k - (n - 1) minus += 10 if sh > 1 or minus == 0: if k >= pe: minus += 1 print(sh * 11 - minus) else: print(0)
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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
def solve(): n, k = map(int, input().split()) s = input() mone = s.count("1") ls = s.find("1") rs = n - s.rfind("1") - 1 ans = mone * 11 if rs <= k: ans -= 10 k -= rs if mone != 1 and ls <= k: ans -= 1 elif ls <= k: ans -= 1 if mone: print(ans) else: print(0) for t in range(int(input())): solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
T = int(input()) for case in range(T): n, k = map(int, input().split()) s = input() if "1" not in s: print(0) continue ans = 0 for i in range(n - 1): ans += int(s[i : i + 2]) l, r = s.find("1"), s.rfind("1") if 0 < n - 1 - r <= k: if r == 0: ans -= 9 else: ans -= 10 k -= n - 1 - r if l != r and 0 < l <= k: ans -= 1 elif 0 < l <= k: if l != n - 1: ans -= 1 print(ans)
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 IF STRING VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING IF NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER IF NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ of length $n$. Let's define $d_i$ as the number whose decimal representation is $s_i s_{i+1}$ (possibly, with a leading zero). We define $f(s)$ to be the sum of all the valid $d_i$. In other words, $f(s) = \sum\limits_{i=1}^{n-1} d_i$. For example, for the string $s = 1011$: $d_1 = 10$ (ten); $d_2 = 01$ (one) $d_3 = 11$ (eleven); $f(s) = 10 + 01 + 11 = 22$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $f(s)$ that can be achieved if at most $k$ operations are allowed. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. First line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $0 \le k \le 10^9$) β€” the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $s$ of length $n$, consisting of only zeros and ones. It is also given that sum of $n$ over all the test cases doesn't exceed $10^5$. -----Output----- For each test case, print the minimum value of $f(s)$ you can obtain with at most $k$ operations. -----Examples----- Input 3 4 0 1010 7 1 0010100 5 2 00110 Output 21 22 12 -----Note----- For the first example, you can't do any operation so the optimal string is $s$ itself. $f(s) = f(1010) = 10 + 01 + 10 = 21$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $f$ value of $22$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $f$ value of $12$.
t = int(input()) for _ in range(t): n, k = [int(x) for x in input().split()] s = input() p = s.count("1") if p == 0: print(0) continue l = s.find("1") f = n - s.rfind("1") - 1 if p == 1: if f < k + 1: print(1) elif l < k + 1: print(10) else: print(11) elif l + f < k + 1: print(11 * p - 11) elif f < k + 1: print(11 * p - 10) elif l < k + 1: print(11 * p - 1) else: print(11 * p)
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 ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR STRING NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR
Currently, XXOC's rap is a string consisting of zeroes, ones, and question marks. Unfortunately, haters gonna hate. They will write $x$ angry comments for every occurrence of subsequence 01 and $y$ angry comments for every occurrence of subsequence 10. You should replace all the question marks with 0 or 1 in such a way that the number of angry comments would be as small as possible. String $b$ is a subsequence of string $a$, if it can be obtained by removing some characters from $a$. Two occurrences of a subsequence are considered distinct if sets of positions of remaining characters are distinct. -----Input----- The first line contains string $s$ β€” XXOC's rap ($1 \le |s| \leq 10^5$). The second line contains two integers $x$ and $y$ β€” the number of angry comments XXOC will recieve for every occurrence of 01 and 10 accordingly ($0 \leq x, y \leq 10^6$). -----Output----- Output a single integer β€” the minimum number of angry comments. -----Examples----- Input 0?1 2 3 Output 4 Input ????? 13 37 Output 0 Input ?10? 239 7 Output 28 Input 01101001 5 7 Output 96 -----Note----- In the first example one of the optimum ways to replace is 001. Then there will be $2$ subsequences 01 and $0$ subsequences 10. Total number of angry comments will be equal to $2 \cdot 2 + 0 \cdot 3 = 4$. In the second example one of the optimum ways to replace is 11111. Then there will be $0$ subsequences 01 and $0$ subsequences 10. Total number of angry comments will be equal to $0 \cdot 13 + 0 \cdot 37 = 0$. In the third example one of the optimum ways to replace is 1100. Then there will be $0$ subsequences 01 and $4$ subsequences 10. Total number of angry comments will be equal to $0 \cdot 239 + 4 \cdot 7 = 28$. In the fourth example one of the optimum ways to replace is 01101001. Then there will be $8$ subsequences 01 and $8$ subsequences 10. Total number of angry comments will be equal to $8 \cdot 5 + 8 \cdot 7 = 96$.
import sys input = sys.stdin.readline def solve(s, x, y): c0 = 0 c1 = 0 r = 0 for i in s: if i == "0": c0 += 1 else: r += c0 * x c1 += 1 z0 = 0 for i in range(len(s) - 1, -1, -1): if s[i] == "0": z0 += 1 else: r += z0 * y ans = r l0 = 0 l1 = 0 for i in s: if i == "?": r -= l0 * x + c0 * y c1 -= 1 r += l1 * y + c1 * x l0 += 1 elif i == "0": l0 += 1 c0 -= 1 else: l1 += 1 c1 -= 1 ans = min(ans, r) return ans s = input().strip() x, y = map(int, input().split()) if x <= y: print(solve(s, x, y)) else: s1 = s.replace("0", "z") s1 = s1.replace("1", "0") s1 = s1.replace("z", "1") print(solve(s1, y, x))
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Currently, XXOC's rap is a string consisting of zeroes, ones, and question marks. Unfortunately, haters gonna hate. They will write $x$ angry comments for every occurrence of subsequence 01 and $y$ angry comments for every occurrence of subsequence 10. You should replace all the question marks with 0 or 1 in such a way that the number of angry comments would be as small as possible. String $b$ is a subsequence of string $a$, if it can be obtained by removing some characters from $a$. Two occurrences of a subsequence are considered distinct if sets of positions of remaining characters are distinct. -----Input----- The first line contains string $s$ β€” XXOC's rap ($1 \le |s| \leq 10^5$). The second line contains two integers $x$ and $y$ β€” the number of angry comments XXOC will recieve for every occurrence of 01 and 10 accordingly ($0 \leq x, y \leq 10^6$). -----Output----- Output a single integer β€” the minimum number of angry comments. -----Examples----- Input 0?1 2 3 Output 4 Input ????? 13 37 Output 0 Input ?10? 239 7 Output 28 Input 01101001 5 7 Output 96 -----Note----- In the first example one of the optimum ways to replace is 001. Then there will be $2$ subsequences 01 and $0$ subsequences 10. Total number of angry comments will be equal to $2 \cdot 2 + 0 \cdot 3 = 4$. In the second example one of the optimum ways to replace is 11111. Then there will be $0$ subsequences 01 and $0$ subsequences 10. Total number of angry comments will be equal to $0 \cdot 13 + 0 \cdot 37 = 0$. In the third example one of the optimum ways to replace is 1100. Then there will be $0$ subsequences 01 and $4$ subsequences 10. Total number of angry comments will be equal to $0 \cdot 239 + 4 \cdot 7 = 28$. In the fourth example one of the optimum ways to replace is 01101001. Then there will be $8$ subsequences 01 and $8$ subsequences 10. Total number of angry comments will be equal to $8 \cdot 5 + 8 \cdot 7 = 96$.
def Kurisu_delta(v, y_01, x_10): c_x = [0] * len(v) one = 0 two = 0 q = 0 for i in range(len(v)): i = len(v) - i - 1 c_x[i] = [one, two] if v[i] == "1": one += 1 q += c_x[i][1] * x_10 elif v[i] == "0": two += 1 q += c_x[i][0] * y_01 elif y_01 <= x_10: one += 1 q += c_x[i][1] * x_10 else: two += 1 q += c_x[i][0] * y_01 one = 0 two = 0 d_one = 0 d_two = 0 qm = q for i in range(len(v)): if v[i] == "1": one += 1 elif v[i] == "0": two += 1 else: if qm > q: qm = q if y_01 > x_10: q -= ( (d_one + one) * x_10 + c_x[i][0] * y_01 - (d_two + two) * y_01 - c_x[i][1] * x_10 ) one += 1 else: q -= ( -(d_one + one) * x_10 - c_x[i][0] * y_01 + (d_two + two) * y_01 + c_x[i][1] * x_10 ) two += 1 if qm > q: qm = q print(qm) s = list(input()) y, k = map(lambda x: int(x), input().split()) Kurisu_delta(s, y, k)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR LIST VAR VAR IF VAR VAR STRING VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR STRING VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
Currently, XXOC's rap is a string consisting of zeroes, ones, and question marks. Unfortunately, haters gonna hate. They will write $x$ angry comments for every occurrence of subsequence 01 and $y$ angry comments for every occurrence of subsequence 10. You should replace all the question marks with 0 or 1 in such a way that the number of angry comments would be as small as possible. String $b$ is a subsequence of string $a$, if it can be obtained by removing some characters from $a$. Two occurrences of a subsequence are considered distinct if sets of positions of remaining characters are distinct. -----Input----- The first line contains string $s$ β€” XXOC's rap ($1 \le |s| \leq 10^5$). The second line contains two integers $x$ and $y$ β€” the number of angry comments XXOC will recieve for every occurrence of 01 and 10 accordingly ($0 \leq x, y \leq 10^6$). -----Output----- Output a single integer β€” the minimum number of angry comments. -----Examples----- Input 0?1 2 3 Output 4 Input ????? 13 37 Output 0 Input ?10? 239 7 Output 28 Input 01101001 5 7 Output 96 -----Note----- In the first example one of the optimum ways to replace is 001. Then there will be $2$ subsequences 01 and $0$ subsequences 10. Total number of angry comments will be equal to $2 \cdot 2 + 0 \cdot 3 = 4$. In the second example one of the optimum ways to replace is 11111. Then there will be $0$ subsequences 01 and $0$ subsequences 10. Total number of angry comments will be equal to $0 \cdot 13 + 0 \cdot 37 = 0$. In the third example one of the optimum ways to replace is 1100. Then there will be $0$ subsequences 01 and $4$ subsequences 10. Total number of angry comments will be equal to $0 \cdot 239 + 4 \cdot 7 = 28$. In the fourth example one of the optimum ways to replace is 01101001. Then there will be $8$ subsequences 01 and $8$ subsequences 10. Total number of angry comments will be equal to $8 \cdot 5 + 8 \cdot 7 = 96$.
s = input() x, y = map(int, input().split()) if x > y: x, y = y, x s = s.replace("1", "#").replace("0", "1").replace("#", "0") assert x <= y z = y - x s_if_all_ones = s.replace("?", "1") ones_before = 0 zeros_before = 0 result = 0 for char in s_if_all_ones: if char == "0": result += ones_before * y zeros_before += 1 else: assert char == "1" result += zeros_before * x ones_before += 1 best_result = result zeros_n = zeros_before ones_n = ones_before zeros_before = 0 ones_before = 0 for char in s: if char == "0": zeros_before += 1 elif char == "1": ones_before += 1 else: assert char == "?" ones_n -= 1 result += (ones_before - (zeros_n - zeros_before)) * z + (ones_n - zeros_n) * x if best_result > result: best_result = result zeros_n += 1 zeros_before += 1 print(best_result)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING STRING STRING STRING STRING STRING VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR BIN_OP VAR VAR VAR NUMBER VAR STRING VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR STRING VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Currently, XXOC's rap is a string consisting of zeroes, ones, and question marks. Unfortunately, haters gonna hate. They will write $x$ angry comments for every occurrence of subsequence 01 and $y$ angry comments for every occurrence of subsequence 10. You should replace all the question marks with 0 or 1 in such a way that the number of angry comments would be as small as possible. String $b$ is a subsequence of string $a$, if it can be obtained by removing some characters from $a$. Two occurrences of a subsequence are considered distinct if sets of positions of remaining characters are distinct. -----Input----- The first line contains string $s$ β€” XXOC's rap ($1 \le |s| \leq 10^5$). The second line contains two integers $x$ and $y$ β€” the number of angry comments XXOC will recieve for every occurrence of 01 and 10 accordingly ($0 \leq x, y \leq 10^6$). -----Output----- Output a single integer β€” the minimum number of angry comments. -----Examples----- Input 0?1 2 3 Output 4 Input ????? 13 37 Output 0 Input ?10? 239 7 Output 28 Input 01101001 5 7 Output 96 -----Note----- In the first example one of the optimum ways to replace is 001. Then there will be $2$ subsequences 01 and $0$ subsequences 10. Total number of angry comments will be equal to $2 \cdot 2 + 0 \cdot 3 = 4$. In the second example one of the optimum ways to replace is 11111. Then there will be $0$ subsequences 01 and $0$ subsequences 10. Total number of angry comments will be equal to $0 \cdot 13 + 0 \cdot 37 = 0$. In the third example one of the optimum ways to replace is 1100. Then there will be $0$ subsequences 01 and $4$ subsequences 10. Total number of angry comments will be equal to $0 \cdot 239 + 4 \cdot 7 = 28$. In the fourth example one of the optimum ways to replace is 01101001. Then there will be $8$ subsequences 01 and $8$ subsequences 10. Total number of angry comments will be equal to $8 \cdot 5 + 8 \cdot 7 = 96$.
s = list(input()) y, k = map(lambda x: int(x), input().split()) def Kurisu(v, y_01, x_10): c_x = [0] * len(v) one, two = 0, 0 q = 0 for i in range(len(v)): i = len(v) - i - 1 c_x[i] = [one, two] if v[i] == "1" or y_01 <= x_10 and v[i] == "?": one += 1 q += c_x[i][1] * x_10 else: two += 1 q += c_x[i][0] * y_01 qm = q one, two = 0, 0 for i in range(len(v)): if v[i] == "1": one += 1 elif v[i] == "0": two += 1 else: if y_01 > x_10: q -= one * x_10 + c_x[i][0] * y_01 - two * y_01 - c_x[i][1] * x_10 one += 1 else: q -= -+one * x_10 - c_x[i][0] * y_01 + two * y_01 + c_x[i][1] * x_10 two += 1 if qm > q: qm = q print(qm) Kurisu(s, y, k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR LIST VAR VAR IF VAR VAR STRING VAR VAR VAR VAR STRING VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR 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 BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after t_{s} minutes have passed after midnight and closes after t_{f} minutes have passed after midnight (so that (t_{f} - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. [Image] "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! -----Input----- The first line contains three integers: the point of time when the receptionist begins to work t_{s}, the point of time when the receptionist stops working t_{f} and the time the receptionist spends on each visitor t. The second line contains one integer nΒ β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing orderΒ β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 10^12; it is guaranteed that t_{s} < t_{f}. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. -----Output----- Print single non-negative integerΒ β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. -----Examples----- Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 -----Note----- In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
ts, tf, t = map(int, input().split()) n = int(input()) INF = int(10**18) if n == 0: print(ts) quit() a = list(map(int, input().split())) i = 0 ans = -1 best = INF end = ts dif = 0 def checkans(curans, time): global ans, best if curans < best and time + max(curans, 0) + t <= tf: ans = time best = curans while i < n: dif = t pri = i - 1 while i + 1 < n and a[i] == a[i + 1]: dif += t i += 1 if pri < 0 or a[pri] != a[i] - 1: checkans(end - (a[i] - 1), a[i] - 1) end += dif curans = end - a[i] checkans(end - a[i], a[i]) i += 1 checkans(0, end) print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after t_{s} minutes have passed after midnight and closes after t_{f} minutes have passed after midnight (so that (t_{f} - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. [Image] "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! -----Input----- The first line contains three integers: the point of time when the receptionist begins to work t_{s}, the point of time when the receptionist stops working t_{f} and the time the receptionist spends on each visitor t. The second line contains one integer nΒ β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing orderΒ β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 10^12; it is guaranteed that t_{s} < t_{f}. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. -----Output----- Print single non-negative integerΒ β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. -----Examples----- Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 -----Note----- In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
[ts, tf, t] = list(map(int, input().split())) n = int(input()) if n == 0: print(ts) exit(0) a = list(map(int, input().split())) if a[0] > ts: print(ts) exit(0) min_wait = 1e18 tbest = a[0] - 1 tnow = ts twait = tnow - tbest min_wait = min(min_wait, twait) i = 0 while i < n: tnow = max(tnow, a[i]) j = i while j < n and a[j] == a[i]: j += 1 tnow += (j - i) * t if j == n: break tcome = a[j] - 1 twait = tnow - tcome if twait < min_wait and tcome + t <= tf: min_wait = twait tbest = tcome i = j if tnow + t <= tf: print(tnow) else: print(tbest)
ASSIGN LIST VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after t_{s} minutes have passed after midnight and closes after t_{f} minutes have passed after midnight (so that (t_{f} - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. [Image] "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! -----Input----- The first line contains three integers: the point of time when the receptionist begins to work t_{s}, the point of time when the receptionist stops working t_{f} and the time the receptionist spends on each visitor t. The second line contains one integer nΒ β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing orderΒ β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 10^12; it is guaranteed that t_{s} < t_{f}. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. -----Output----- Print single non-negative integerΒ β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. -----Examples----- Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 -----Note----- In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
begin, end, time = list(map(int, input().split())) n = int(input()) if n == 1: print(begin) elif n > 0: ar = list(map(int, input().split())) ar.sort() i = 0 ar.append(end - time + 1) n += 1 num = begin ans = begin - ar[0] + 1 fin_ans = ar[0] - 1 while i < n: num = max(num + time, ar[i] + time) while i + 1 < n and ar[i + 1] == ar[i]: i += 1 num += time i += 1 if i < n and num - ar[i] + 1 < ans and max(ar[i] - 1, num) + time <= end: ans = num - ar[i] + 1 fin_ans = ar[i] - 1 print(fin_ans) else: print(begin)
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after t_{s} minutes have passed after midnight and closes after t_{f} minutes have passed after midnight (so that (t_{f} - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. [Image] "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! -----Input----- The first line contains three integers: the point of time when the receptionist begins to work t_{s}, the point of time when the receptionist stops working t_{f} and the time the receptionist spends on each visitor t. The second line contains one integer nΒ β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing orderΒ β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 10^12; it is guaranteed that t_{s} < t_{f}. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. -----Output----- Print single non-negative integerΒ β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. -----Examples----- Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 -----Note----- In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
s, e, t_per_v = list(map(int, input().split())) e -= t_per_v n = int(input()) if n > 0: visitors = list(map(int, input().split())) else: visitors = [] min_wait_time = float("inf") min_arrive_time = -1 visitor_index = 0 t = s while t <= e: if visitor_index >= len(visitors) or visitors[visitor_index] > t: print(t) exit(0) wait_time = t - visitors[visitor_index] if wait_time < min_wait_time: min_wait_time = wait_time min_arrive_time = visitors[visitor_index] t += t_per_v visitor_index += 1 print(min_arrive_time - 1)
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after t_{s} minutes have passed after midnight and closes after t_{f} minutes have passed after midnight (so that (t_{f} - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. [Image] "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! -----Input----- The first line contains three integers: the point of time when the receptionist begins to work t_{s}, the point of time when the receptionist stops working t_{f} and the time the receptionist spends on each visitor t. The second line contains one integer nΒ β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing orderΒ β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 10^12; it is guaranteed that t_{s} < t_{f}. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. -----Output----- Print single non-negative integerΒ β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. -----Examples----- Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 -----Note----- In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
from sys import stdin lines = stdin.readlines() current_free, f, t = map(int, lines[0].split()) n = int(lines[1]) arrival_times = [] if n > 0: arrival_times = list(map(int, lines[2].split())) min_wait = f min_arrival = -1 current_free def check_arrival_at(arrival): global min_arrival global min_wait global t global f if current_free + t <= f and arrival + t <= f: wait = current_free - max(0, arrival) if wait < min_wait: min_wait = wait min_arrival = arrival for a in arrival_times: check_arrival_at(a - 1) if min_wait == 0: break current_free = current_free + t if current_free + t > f: break check_arrival_at(current_free) print(min_arrival)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR VAR FUNC_DEF IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after t_{s} minutes have passed after midnight and closes after t_{f} minutes have passed after midnight (so that (t_{f} - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. [Image] "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! -----Input----- The first line contains three integers: the point of time when the receptionist begins to work t_{s}, the point of time when the receptionist stops working t_{f} and the time the receptionist spends on each visitor t. The second line contains one integer nΒ β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing orderΒ β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 10^12; it is guaranteed that t_{s} < t_{f}. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. -----Output----- Print single non-negative integerΒ β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. -----Examples----- Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 -----Note----- In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
def ri(): return map(int, input().split()) s, f, t = ri() n = int(input()) if n == 0: print(s) exit() i = list(ri()) p = 0 a = s m = 10**100 for ii in i: if ii <= f - t: if a - ii + 1 < m: m = a - ii + 1 p = min(ii - 1, a) a = max(a, ii) + t if a <= f - t: p = a print(p)
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR IF VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after t_{s} minutes have passed after midnight and closes after t_{f} minutes have passed after midnight (so that (t_{f} - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. [Image] "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! -----Input----- The first line contains three integers: the point of time when the receptionist begins to work t_{s}, the point of time when the receptionist stops working t_{f} and the time the receptionist spends on each visitor t. The second line contains one integer nΒ β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing orderΒ β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 10^12; it is guaranteed that t_{s} < t_{f}. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. -----Output----- Print single non-negative integerΒ β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. -----Examples----- Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 -----Note----- In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
ts, tf, t = map(int, input().split()) n = int(input()) if n == 0: print(ts) exit() vis = list(map(int, input().split())) ans = min(vis[0] - 1, ts) wt = ts - ans cur = max(vis[0], ts) for i in range(1, n): if vis[i] == vis[i - 1]: cur += t elif cur + t + t <= tf: if wt > cur + t - (vis[i] - 1): ans = min(cur + t, vis[i] - 1) wt = cur + t - ans cur = max(cur + t, vis[i]) else: break if cur + t + t <= tf: print(tf - t) else: print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after t_{s} minutes have passed after midnight and closes after t_{f} minutes have passed after midnight (so that (t_{f} - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. [Image] "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! -----Input----- The first line contains three integers: the point of time when the receptionist begins to work t_{s}, the point of time when the receptionist stops working t_{f} and the time the receptionist spends on each visitor t. The second line contains one integer nΒ β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing orderΒ β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 10^12; it is guaranteed that t_{s} < t_{f}. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. -----Output----- Print single non-negative integerΒ β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. -----Examples----- Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 -----Note----- In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
import sys t1, t2, t = map(int, input().split()) u = 1 n = int(input()) if n == 0: print(t1) sys.exit() a = [int(i) for i in input().split()] mini = 10**20 ans = 0 time = t1 for x in a: if x <= t2 - t: if time - x + 1 < mini and x > 0: mini = time - x + 1 if x - 1 > time: ans = time else: ans = x - 1 time = t + max(time, x) if time > t2 - t: print(ans) else: print(time)
IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after t_{s} minutes have passed after midnight and closes after t_{f} minutes have passed after midnight (so that (t_{f} - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. [Image] "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! -----Input----- The first line contains three integers: the point of time when the receptionist begins to work t_{s}, the point of time when the receptionist stops working t_{f} and the time the receptionist spends on each visitor t. The second line contains one integer nΒ β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing orderΒ β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 10^12; it is guaranteed that t_{s} < t_{f}. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. -----Output----- Print single non-negative integerΒ β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. -----Examples----- Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 -----Note----- In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
import sys def debug(x, table): for name, val in table.items(): if x is val: print("DEBUG:{} -> {}".format(name, val), file=sys.stderr) return None def solve(): ts, tf, t = map(int, input().split()) n = int(input()) if n == 0: print(ts) return None AT = [int(i) for i in input().split()] BT = [0] * n for i, at in enumerate(AT): if i == 0: if at > ts: print(ts) return None else: BT[i] = ts elif at > BT[i - 1] + t and BT[i - 1] + t <= tf: print(BT[i - 1] + t) return None else: BT[i] = BT[i - 1] + t if BT[n - 1] + 2 * t <= tf: print(BT[n - 1] + t) return None min_wait = float("inf") ans = None for i in range(n): if i > 0 and AT[i] == AT[i - 1]: continue elif BT[i] - (AT[i] - 1) < min_wait and BT[i] + t <= tf: min_wait = BT[i] - (AT[i] - 1) ans = AT[i] - 1 print(ans) def __starting_point(): solve() __starting_point()
IMPORT FUNC_DEF FOR VAR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR RETURN NONE FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN NONE ASSIGN VAR VAR VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN NONE ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after t_{s} minutes have passed after midnight and closes after t_{f} minutes have passed after midnight (so that (t_{f} - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. [Image] "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! -----Input----- The first line contains three integers: the point of time when the receptionist begins to work t_{s}, the point of time when the receptionist stops working t_{f} and the time the receptionist spends on each visitor t. The second line contains one integer nΒ β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing orderΒ β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 10^12; it is guaranteed that t_{s} < t_{f}. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. -----Output----- Print single non-negative integerΒ β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. -----Examples----- Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 -----Note----- In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
a, b, c = map(int, input().split()) n = int(input()) if n == 0: print(a) exit() arr = [int(i) for i in input().split()] p, m = 0, int(1e18) for i in arr: if i <= b - c: if i > 0 and a - i + 1 < m: m = a - i + 1 p = min(i - 1, a) a = max(a, i) + c print(a if a <= b - c else p)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR
Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after t_{s} minutes have passed after midnight and closes after t_{f} minutes have passed after midnight (so that (t_{f} - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. [Image] "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! -----Input----- The first line contains three integers: the point of time when the receptionist begins to work t_{s}, the point of time when the receptionist stops working t_{f} and the time the receptionist spends on each visitor t. The second line contains one integer nΒ β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing orderΒ β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 10^12; it is guaranteed that t_{s} < t_{f}. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. -----Output----- Print single non-negative integerΒ β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. -----Examples----- Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 -----Note----- In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
ts, tf, t = map(int, input().split()) n = int(input()) visitors = list(map(int, input().split())) if n else [] finish = [ts] for visitor in visitors: finish.append(max(finish[-1], visitor) + t) res = 0 min_time = 10**13 for i in range(n): if finish[i] + t >= tf: continue if visitors[i] == 0: continue if max(0, finish[i] - (visitors[i] - 1)) < min_time: res = min(visitors[i] - 1, finish[i]) min_time = max(0, finish[i] - (visitors[i] - 1)) if finish[-1] + t <= tf: res = finish[-1] min_time = 0 print(res)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST ASSIGN VAR LIST VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR IF VAR VAR NUMBER IF FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after t_{s} minutes have passed after midnight and closes after t_{f} minutes have passed after midnight (so that (t_{f} - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. [Image] "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! -----Input----- The first line contains three integers: the point of time when the receptionist begins to work t_{s}, the point of time when the receptionist stops working t_{f} and the time the receptionist spends on each visitor t. The second line contains one integer nΒ β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing orderΒ β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 10^12; it is guaranteed that t_{s} < t_{f}. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. -----Output----- Print single non-negative integerΒ β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. -----Examples----- Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 -----Note----- In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
f = lambda: map(int, input().split()) a, b, t = f() (k,) = f() s = d = 1000000000000.0 for q in f() if k else []: if q > a: s = a if q > a or a + t > b: break elif a - q < d: s, d = q - 1, a - q a += t print(s if a + t > b else a)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR LIST IF VAR VAR ASSIGN VAR VAR IF VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR
Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after t_{s} minutes have passed after midnight and closes after t_{f} minutes have passed after midnight (so that (t_{f} - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. [Image] "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! -----Input----- The first line contains three integers: the point of time when the receptionist begins to work t_{s}, the point of time when the receptionist stops working t_{f} and the time the receptionist spends on each visitor t. The second line contains one integer nΒ β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing orderΒ β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 10^12; it is guaranteed that t_{s} < t_{f}. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. -----Output----- Print single non-negative integerΒ β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. -----Examples----- Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 -----Note----- In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
INF = 10**19 ts, tf, t = map(int, input().split()) n = int(input()) u = [] if n > 0: u = list(map(int, input().split())) a = [] b = [] i = 0 while i < n: a.append(u[i]) if i == 0: start = max(ts, u[i]) else: start = max(b[-1], a[-1]) ng = 1 while i + 1 < n and u[i] == u[i + 1]: i += 1 ng += 1 end = start + ng * t b.append(end) i += 1 res = None if n == 0: res = ts else: res = a[0] - 1 min_w = max(0, ts - a[0] + 1) m = len(a) for j in range(1, m): start = a[j] - 1 stop = max(b[j - 1], start) w = stop - start if w < min_w: min_w = w if stop < tf: res = start for j in range(1, m): start = a[j] stop = b[j] w = stop - start if w < min_w: min_w = w if stop < tf: res = start if b[m - 1] <= tf - t: res = tf - t if ts < a[0]: res = ts print(res)
ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NONE IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after t_{s} minutes have passed after midnight and closes after t_{f} minutes have passed after midnight (so that (t_{f} - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. [Image] "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! -----Input----- The first line contains three integers: the point of time when the receptionist begins to work t_{s}, the point of time when the receptionist stops working t_{f} and the time the receptionist spends on each visitor t. The second line contains one integer nΒ β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing orderΒ β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 10^12; it is guaranteed that t_{s} < t_{f}. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. -----Output----- Print single non-negative integerΒ β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. -----Examples----- Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 -----Note----- In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
def main(): start, end, t = map(int, input().split()) n = int(input()) people = [] if n != 0: people = list(map(int, input().split())) time = start ans = 1e16 answer = 0 for p in people: if p + t > end: break if p - 1 >= 0: if ans > max(0, time - (p - 1)): ans = max(0, time - (p - 1)) answer = p - 1 time = max(time, p) + t if time + t > end: break if time + t <= end: answer = time print(answer) main()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after t_{s} minutes have passed after midnight and closes after t_{f} minutes have passed after midnight (so that (t_{f} - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. [Image] "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! -----Input----- The first line contains three integers: the point of time when the receptionist begins to work t_{s}, the point of time when the receptionist stops working t_{f} and the time the receptionist spends on each visitor t. The second line contains one integer nΒ β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing orderΒ β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 10^12; it is guaranteed that t_{s} < t_{f}. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. -----Output----- Print single non-negative integerΒ β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. -----Examples----- Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 -----Note----- In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
inp = input().split(" ") ts = int(inp[0]) tf = int(inp[1]) T = int(inp[2]) inp = input() n = int(inp) if n == 0: print(ts) else: inp = input().split(" ") min_del_t = 10000000000000 ans = int(inp[0]) - 1 t_cur = ts for tS in inp: t = int(tS) time_waiting = t_cur - t + 1 if t_cur < t and t_cur + T <= tf: ans = t_cur break elif min_del_t > time_waiting and t_cur + T <= tf: min_del_t = time_waiting ans = t - 1 t_cur += T if t_cur + T <= tf: print(t_cur) else: print(ans)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after t_{s} minutes have passed after midnight and closes after t_{f} minutes have passed after midnight (so that (t_{f} - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. [Image] "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! -----Input----- The first line contains three integers: the point of time when the receptionist begins to work t_{s}, the point of time when the receptionist stops working t_{f} and the time the receptionist spends on each visitor t. The second line contains one integer nΒ β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing orderΒ β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 10^12; it is guaranteed that t_{s} < t_{f}. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. -----Output----- Print single non-negative integerΒ β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. -----Examples----- Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 -----Note----- In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
ts, tf, te = map(int, input().split(" ")) n = int(input()) if n == 0: print(ts) exit(0) arr = list(map(int, input().split(" "))) idx = 0 curr_time = ts wait_time = max(0, ts - arr[0] + 1) ans = min(arr[0] - 1, ts) while wait_time > 0: curr_time += te idx += 1 if idx == n or curr_time >= tf: break next_wait = max(0, curr_time - arr[idx] + 1) if next_wait < wait_time: wait_time = next_wait ans = arr[idx] - 1 if wait_time > 0 and curr_time + te <= tf: ans = curr_time print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR WHILE VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after t_{s} minutes have passed after midnight and closes after t_{f} minutes have passed after midnight (so that (t_{f} - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. [Image] "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! -----Input----- The first line contains three integers: the point of time when the receptionist begins to work t_{s}, the point of time when the receptionist stops working t_{f} and the time the receptionist spends on each visitor t. The second line contains one integer nΒ β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing orderΒ β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 10^12; it is guaranteed that t_{s} < t_{f}. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. -----Output----- Print single non-negative integerΒ β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. -----Examples----- Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 -----Note----- In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
def update(t, l, ans): if l < ans[1]: ans[0] = t ans[1] = l def solve(): S, E, L = list(map(int, input().split())) n = int(input()) if n == 0: return S a = list(map(int, input().split()))[::-1] if S < a[-1]: return S ans = [0, S] cur_t = S while cur_t + L <= E and len(a) > 0: if cur_t < a[-1]: return cur_t t = a.pop() update(t - 1, cur_t - t + 1, ans) cur_t += L if cur_t + L <= E: return cur_t return ans[0] print(solve())
FUNC_DEF IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER IF VAR VAR NUMBER RETURN VAR ASSIGN VAR LIST NUMBER VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR VAR RETURN VAR RETURN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
for _ in range(int(input())): ans1, ans2 = 100000, 100000 n = int(input()) str1 = input() str2 = input() x, y, v01 = str1.count("1"), str2.count("1"), 0 for i in range(n): if str1[i] != str2[i]: v01 += 1 if x == y: ans1 = v01 if y == n - x + 1: ans2 = n - v01 if ans1 == ans2 == 100000: print(-1) else: print(min(ans1, ans2))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
import sys t = int(sys.stdin.readline()) ans = [] for _ in range(t): n = int(sys.stdin.readline()) s1 = sys.stdin.readline().strip() s2 = sys.stdin.readline().strip() if s1 == s2: ans.append("0") continue elif len(s1) != len(s2) or s1.count("1") == 0: ans.append("-1") else: cc = [0] * 4 for i in range(len(s1)): cc[int(s1[i]) * 2 + int(s2[i])] += 1 ret = n * 2 if cc[1] == cc[2]: ret = cc[1] + cc[2] if cc[3] == cc[0] + 1: ret = min(ret, cc[0] + cc[3]) if ret < n * 2: ans.append(str(ret)) else: ans.append("-1") print("\n".join(ans))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
for _ in range(int(input())): n = int(input()) a = list(map(int, input())) ans = float("+inf") b = list(map(int, input())) cr, cd = 0, 0 for i in range(n): if a[i] and not b[i]: cr += 1 elif b[i] and not a[i]: cd += 1 if cr == cd: ans = cr + cd cr, cd = 0, 0 for i in range(n): if not a[i] and not b[i]: cr += 1 elif b[i] and a[i]: cd += 1 if cd == cr + 1: ans = min(ans, cd + cr) print(ans if ans <= n else -1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
def operation(a, b): c01, c10 = 0, 0 for i in range(len(a)): if a[i] == "1" and b[i] == "0": c10 += 1 elif a[i] == "0" and b[i] == "1": c01 += 1 if c10 != c01: return float("inf") return c10 + c01 def change(a, b, f): idx = -1 for i in range(len(a)): if a[i] == "1" and b[i] == f: idx = i break if idx == -1: return float("inf") for i in range(len(a)): if i == idx: continue if a[i] == "1": a[i] = "0" else: a[i] = "1" return operation(a, b) for _ in range(int(input())): n = int(input()) a = input() b = input() a1, b1 = list(a), list(b) ans = operation(a1, b1) ans = min(ans, 1 + change(a1, b1, "1"), 1 + change(a1, b1, "0")) if a == b: print(0) elif ans == float("inf"): print(-1) else: print(ans)
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR STRING RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING RETURN FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
import sys input = sys.stdin.readline for i in " " * int(input()): n = int(input()) s1 = input().strip() s2 = input().strip() k1 = s1.count("1") k2 = s2.count("1") if k1 != k2 and k1 + k2 != n + 1: print(-1) continue res = 10**9 if k1 == k2: ct = 0 for i in range(n): if s1[i] != s2[i]: ct += 1 res = min(res, ct) if k1 + k2 == n + 1: ct = 0 for i in range(n): if s1[i] == s2[i]: ct += 1 res = min(res, ct) print(res)
IMPORT ASSIGN VAR VAR FOR VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
def solve(): n = int(input()) a = input() b = input() x, y, z = 0, 0, 0 for i in range(n): if a[i] == "1": x += 1 if b[i] == "1": y += 1 if a[i] != b[i]: z += 1 if x == y: if n - x + 1 == y: return min(z, n - z) return z elif n - x + 1 == y: return n - z return -1 for _ in range(int(input())): print(solve())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN BIN_OP VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
import sys input = sys.stdin.readline t = int(input()) inf = pow(10, 6) + 1 for _ in range(t): n = int(input()) a = list(input().rstrip()) b = list(input().rstrip()) c = [0] * 4 for i in range(n): if a[i] == b[i] == "0": c[0] += 1 elif a[i] == b[i] == "1": c[3] += 1 elif a[i] == "0" and b[i] == "1": c[1] += 1 else: c[2] += 1 ans = inf if c[3] - 1 == c[0]: ans = min(ans, c[0] + c[3]) if c[2] == c[1]: ans = min(ans, c[1] + c[2]) ans = (ans + 1) % (inf + 1) - 1 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL 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 ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR STRING VAR NUMBER NUMBER IF VAR VAR VAR VAR STRING VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
t = int(input()) for i in range(t): opp = True n = int(input()) a = [int(j) for j in list(input())] b = [int(j) for j in list(input())] ans = 0 if sum(a) == sum(b) and sum(a) == n - sum(b) + 1: for j in range(n): if b[j] == 1 and a[j] == 0: ans += 1 if ans < sum(a) / 2: print(ans * 2) else: print((sum(a) - ans - 1) * 2 + 1) elif sum(a) == sum(b): for j in range(n): if b[j] == 1 and a[j] == 0: ans += 2 print(ans) elif sum(a) == n - sum(b) + 1: for j in range(n): if b[j] == 1 and a[j] == 1: ans += 1 a = [(1 if k == 0 else 0) for k in a] a[j] = 1 for k in range(n): if b[k] == 1 and a[k] == 0: ans += 2 print(ans) break else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
def f(a, b): zo, oz = 0, 0 for x, y in zip(a, b): if a == "0" and b == "1": zo += 1 elif a == "1" and b == "0": oz += 1 return oz, zo for _ in range(int(input())): n = int(input()) s = input() desire = input() ones_d = desire.count("1") zeros_d = n - ones_d zo, oz = 0, 0 oz_index, oo_index = None, None num_ones = 0 i = 0 for x, y in zip(s, desire): if x == "0" and y == "1": zo += 1 elif x == "1": num_ones += 1 if y == "0": oz += 1 oz_index = i else: oo_index = i i += 1 num_zeros = n - num_ones if not num_ones: if ones_d: print(-1) else: print(0) continue if ones_d not in (num_ones, num_zeros + 1): print(-1) continue diff1 = oz + zo diff2 = n - diff1 - 1 if oo_index is not None else n - diff1 + 1 if diff2 < diff1: if num_zeros + 1 == ones_d: if oo_index is not None: oz, zo = num_zeros - zo, num_ones - oz - 1 else: oz, zo = num_zeros - zo + 1, num_ones - oz print(1 + 2 * oz) else: print(2 * oz) elif num_ones == ones_d: print(2 * oz) else: if oo_index is not None: oz, zo = num_zeros - zo, num_ones - oz - 1 else: oz, zo = num_zeros - zo + 1, num_ones - oz print(1 + 2 * oz)
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR STRING VAR STRING VAR NUMBER IF VAR STRING VAR STRING VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NONE NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR STRING VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NONE BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER VAR IF VAR NONE ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR NONE ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
t = int(input()) for _ in range(t): n = int(input()) a = list(input()) b = list(input()) if a != b and (a.count("1") == 0 or a.count("1") != 0 and b.count("1") == 0): print(-1) elif a == b: print(0) else: count = 0 count11 = 0 count21 = 0 ans1 = 10**9 ans2 = 10**9 for i in range(len(a)): if a[i] != b[i]: count += 1 if a[i] == "1": count11 += 1 elif a[i] == "1": count21 += 1 eq = len(a) - count if eq % 2 == 1: if count21 - (eq - count21) == 1: ans1 = eq if count % 2 == 0: if count - count11 == count11: ans2 = count ans = min(ans1, ans2) if ans != 10**9: print(ans) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
t = int(input()) for i in range(t): n = int(input()) a = input() b = input() uu = 0 ll = 0 ul = 0 lu = 0 for j in range(n): if a[j] == "1" and b[j] == "1": ll += 1 elif a[j] == "1" and b[j] == "0": lu += 1 elif a[j] == "0" and b[j] == "1": ul += 1 elif a[j] == "0" and b[j] == "0": uu += 1 ans1 = n ans2 = n c = 0 if ul == lu: ans1 = 2 * ul c += 1 if uu == ll - 1: ans2 = 2 * uu + 1 c += 1 if c == 0: print(-1) else: print(min(ans1, ans2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
t = int(input()) for i in range(t): kifkif = 0 moch_kif = 0 nbr1_kif = 0 nbr1_moch = 0 n = int(input()) a = input() b = input() for j in range(len(a)): if a[j] == b[j]: kifkif += 1 if a[j] == "1": nbr1_kif += 1 elif a[j] == "1": nbr1_moch += 1 moch_kif = n - kifkif if kifkif % 2 == 0 or nbr1_kif != int(kifkif / 2) + 1: kifkif = -1 if moch_kif % 2 != 0 or nbr1_moch != int(moch_kif / 2): moch_kif = -1 if kifkif != -1 and moch_kif != -1: print(min(moch_kif, kifkif)) elif kifkif != -1: print(kifkif) elif moch_kif != -1: print(moch_kif) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
for t in range(int(input())): n = int(input()) a = input() b = input() _1_1 = _0_0 = _1_0 = _0_1 = 0 if a == b: print(0) elif int(b) == 0 or int(a) == 0: print(-1) else: for i, j in zip(a, b): if i == j: if i == "1": _1_1 += 1 else: _0_0 += 1 elif i == "1": _1_0 += 1 else: _0_1 += 1 s = _1_1 + _0_0 s1 = _1_0 + _0_1 if _1_1 - _0_0 == 1 and _1_0 == _0_1: print(min(s, s1)) elif _1_1 - _0_0 == 1: print(s) elif _1_0 == _0_1: print(s1) else: print("-1")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
for _ in range(int(input())): n = int(input()) a = input() b = input() counts = [0, 0, 0, 0] ones = [0, 0] for i in range(n): counts[int(a[i]) * 2 + int(b[i])] += 1 ones[0] += a[i] == "1" ones[1] += b[i] == "1" ops = 1000000000.0 if ones[0] == ones[1]: ops = min(ops, counts[1] + counts[2]) if n - ones[0] + 1 == ones[1]: if counts[3]: counts = [counts[2], counts[3] - 1, counts[0], counts[1] + 1] ops = min(ops, counts[1] + counts[2] + 1) else: counts = [counts[2] - 1, counts[3], counts[0] + 1, counts[1]] ops = min(ops, counts[1] + counts[2] + 1) if ops > 100000000.0: print(-1) else: print(ops)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR VAR STRING VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
import sys def menorah(n, a, b): _01 = 0 _00 = 0 _10 = 0 _11 = 0 for i in range(n): if a[i] == "0": if b[i] == "0": _00 += 1 else: _01 += 1 elif b[i] == "1": _11 += 1 else: _10 += 1 best = float("inf") if _01 == _10: best = min(best, _01 + _10) if _11 > 0: __01 = _11 - 1 __00 = _10 __10 = _00 __11 = _01 + 1 if __01 == __10: best = min(best, 1 + __01 + __10) if best == float("inf"): best = -1 return best for _ in range(int(input())): n = int(input()) a = input() b = input() sys.stdout.write(f"{menorah(n, a, b)}\n")
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
def f(a, b): if a == b: return 0 one = [] z = [] bigo = set() for id, i in enumerate(a): if i == "1": one.append(id) else: z.append(id) if b[id] == "1": bigo.add(id) cst11 = 0 cst12 = 0 if one == []: return -1 bado = [] goodo = [] badz = [] goodz = [] for i in one: if i not in bigo: bado.append(i) else: goodo.append(i) for i in z: if i not in bigo: badz.append(i) else: goodz.append(i) cst1 = float("inf") if len(goodo) - len(badz) == 1: cst1 = len(goodo) + len(badz) if cst1 % 2 == 0 and cst1 != 0: cst1 = float("inf") cst2 = float("inf") if len(bado) - len(goodz) == 1 or len(bado) - len(goodz) == 0: cst2 = len(bado) + len(goodz) if cst2 % 2 != 0 and cst2 != 0: cst2 = float("inf") if min(cst2, cst1) == float("inf"): return -1 return min(cst2, cst1) for _ in range(int(input())): n = input() a = input() b = input() print(f(a, b))
FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR LIST RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
for _ in range(int(input())): n = int(input()) a = list(map(int, input())) b = list(map(int, input())) a1 = a.count(1) a0 = a.count(0) b1 = b.count(1) b0 = b.count(0) ans = 0 if a1 != b1 or a0 != b0: if a1 == 0: print(-1) continue j = a.index(1) for i in range(n): if a[i] and b[i]: j = i for i in range(n): if i != j: a[i] = 1 - a[i] ans += 1 a1 = a.count(1) a0 = a.count(0) if a1 != b1 or a0 != b0: print(-1) continue for i in range(n): ans += a[i] != b[i] ans2 = float("inf") if 1 in a: j = a.index(1) ans2 = 1 for i in range(n): if a[i] and b[i]: j = i for i in range(n): if i != j: a[i] = 1 - a[i] for i in range(n): ans2 += a[i] != b[i] a1 = a.count(1) a0 = a.count(0) if a1 != b1 or a0 != b0: ans2 = float("inf") print(min(ans, ans2))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
def checkeven(k, res): if k == res // 2: return True else: return False def checkodd(g, res): if g == (n - res) // 2 + 1: return True else: return False def find(a, b, n): if int(a) == 0 or int(b) == 0: if int(a) != 0 or int(b) != 0: return -1 if int(a) == int(b): return 0 res = 0 g = k = 0 for i in range(n): if a[i] == "1": if a[i] != b[i]: res += 1 k += 1 else: g += 1 elif a[i] != b[i]: res += 1 ans1 = ans2 = -1 extra = 0 if res % 2 == 0 and checkeven(k, res): ans1 = res if (n - res) % 2 != 0 and checkodd(g, res): ans2 = n - res if ans2 == ans1 and ans2 == -1: return -1 if ans1 == -1: return ans2 elif ans2 == -1: return ans1 else: return min(ans2, ans1) r = int(input()) res = [] for i in range(r): n = int(input()) a = input() b = input() res.append(find(a, b, n)) for i in res: print(i)
FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
for _ in range(int(input())): n = int(input()) a = input() b = input() x = [0] * 4 for i in range(n): if a[i] == "1" and b[i] == "1": x[0] += 1 elif a[i] == "0" and b[i] == "0": x[1] += 1 elif a[i] == "1" and b[i] == "0": x[2] += 1 elif a[i] == "0" and b[i] == "1": x[3] += 1 if x[2] == x[3] == 0: print(0) elif x[0] == x[2] == 0: print(-1) else: if x[2] == x[3]: ans = x[2] + x[3] else: ans = n + 1 if x[2] > 0 and x[1] + 1 == x[0]: temp_ans = x[0] * 2 + 1 if temp_ans < ans: ans = temp_ans if x[0] > 0 and x[1] + 1 == x[0]: temp_ans = x[1] * 2 + 1 if temp_ans < ans: ans = temp_ans if ans > n: ans = -1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
problem_num = int(input()) for i in range(problem_num): n = int(input()) arr1 = input() arr2 = input() if arr2.count("1") not in [arr1.count("1"), n - arr1.count("1") + 1]: print(-1) continue same = 0 diff = 0 for j in range(len(arr1)): if arr1[j] == arr2[j]: same += 1 else: diff += 1 if arr1.count("1") == n - arr1.count("1") + 1: print(min([same, diff])) elif arr2.count("1") == arr1.count("1"): print(diff) else: print(same)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING LIST FUNC_CALL VAR STRING BIN_OP BIN_OP VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR STRING BIN_OP BIN_OP VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
for _ in range(int(input())): n = int(input()) a = input() b = input() r = a.count("1") l = a.count("0") rr = 0 ll = 0 for i in range(n): if a[i] != b[i]: if a[i] == "1": rr += 1 else: ll += 1 m = 0 if ll == rr: m = ll * 2 else: m = float("inf") rr = r - rr ll = l - ll if rr and ll + 1 == rr: m = min(m, ll * 2 + 1) if m == float("inf"): m = -1 print(m)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
for _ in range(int(input())): n = int(input()) a = input() b = input() x, x1, y, y1 = 0, 0, 0, 0 for i in range(n): if a[i] == b[i]: x += 1 if a[i] == "1": x1 += 1 else: y += 1 if a[i] == "1": y1 += 1 flag = 1 ans = 10**9 if x % 2 != 0 and (x + 1) // 2 == x1: ans = x flag = 0 if y % 2 == 0 and y // 2 == y1: ans = min(ans, y) flag = 0 if flag: print(-1) else: print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
for _ in range(int(input())): n = int(input()) a = input() b = input() n1 = 0 for i in a: if i == "1": n1 += 1 n2 = 0 for i in b: if i == "1": n2 += 1 noc = 0 for i in range(n): if a[i] != b[i]: noc += 1 ans = 999999 if noc == 0: ans = 0 elif n1 == 0: ans = -1 else: if n1 == n2: ans = noc if n1 == n - n2 + 1: ans = min(ans, n - noc) if ans == 999999: ans = -1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
maxx = 100000000 T = int(input()) for t in range(T): n = int(input()) a = input() b = input() a1 = a.count("1") b1 = b.count("1") diff = 0 for i in range(n): diff += a[i] != b[i] op1, op2 = maxx, maxx if a1 == b1: op1 = diff if a1 == n - b1 + 1: op2 = n - diff if op1 == op2 == maxx: print(-1) else: print(min(op1, op2))
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
from sys import stdin, stdout rd = lambda: list(map(int, stdin.readline().split())) rds = lambda: stdin.readline().rstrip() ii = lambda: int(stdin.readline()) INF = 1 << 62 mod = 10**9 + 7 for _ in range(ii()): n = ii() a = rds() b = rds() if a == b: print(0) continue d = 0 e1 = 0 e0 = 0 ac = 0 bc = 0 for x, y in zip(a, b): if x == "1": ac += 1 if y == "1": bc += 1 if x == y == "1": e1 += 1 elif x == y == "0": e0 += 1 elif x != y: d += 1 if e1 == 1 and d == len(a) - 1: print(1) continue res1 = INF if ac == bc: res1 = d res2 = INF if e1 - e0 == 1: res2 = 1 + 2 * e0 res = min(res1, res2) if res == INF: print(-1) else: print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
def func(): n = int(input()) a = input() b = input() x = 0 y = 0 x1 = 0 y1 = 0 for i in range(n): if a[i] == b[i]: x += 1 if a[i] == "1": x1 += 1 else: y += 1 if a[i] == "1": y1 += 1 flg = True ans = 10**9 if x % 2 != 0 and (x + 1) // 2 == x1: ans = x flg = False if y % 2 == 0 and y // 2 == y1: ans = min(ans, y) flg = False if flg: print(-1) else: print(ans) t = int(input()) for i in range(t): func()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR 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 NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
def sumString(x): ans = 0 for i in x: ans += int(i) return ans def solve(a, b): if a == b: return 0 suma = sumString(a) sumb = sumString(b) if suma == 0 or sumb == 0: return -1 eveonOrOdd = [] evens = 0 odds = 0 zeroEvens = 0 oneEvens = 0 zeroOdds = 0 oneOdds = 0 for i in range(len(a)): if a[i] == b[i]: eveonOrOdd.append("1") evens += 1 if a[i] == "1": oneEvens += 1 else: zeroEvens += 1 else: eveonOrOdd.append("0") odds += 1 if a[i] == "1": oneOdds += 1 else: zeroOdds += 1 ans = [] if evens % 2 == 1: if oneEvens - zeroEvens == 1: ans.append(evens) if odds % 2 == 0: if zeroOdds == oneOdds: ans.append(odds) if ans: return min(ans) return -1 for _ in range(int(input())): n = int(input()) a = input() b = input() print(solve(a, b))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR RETURN FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
import sys input = sys.stdin.readline def solve(): n = int(input()) a = input().strip() b = input().strip() diff = cnt1 = cnt2 = 0 for i in range(n): cnt1 += int(a[i]) cnt2 += int(b[i]) diff += 1 if a[i] != b[i] else 0 ans = float("inf") if cnt1 == cnt2: ans = min(ans, diff) if cnt1 + cnt2 == n + 1: ans = min(ans, n - diff) return ans if ans < float("inf") else -1 for _ in range(int(input())): print(solve())
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) s = input()[:-1] s2 = input()[:-1] pos1 = [0, 0] pos2 = [0, 0] comp = int(s2, 2) for i in range(len(s)): if s[i] != s2[i]: pos1[int(s[i])] += 1 else: pos2[int(s[i])] += 1 ans = [] if pos1[0] == pos1[1]: ans.append(sum(pos1)) if pos2[1] == pos2[0] + 1: ans.append(sum(pos2)) if ans: print(min(ans)) else: print(-1)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
def operation(a, b): c1, c2 = 0, 0 for i, j in zip(a, b): if i != j: if i == "1": c1 += 1 else: c2 += 1 if c1 != c2: return float("inf") else: return 2 * c1 def change(a, b, f): idx = -1 for i in range(len(a)): if a[i] == "1" and b[i] == f: idx = i if idx == -1: return float("inf") else: l = "" for i in range(len(a)): if i != idx: if a[i] == "1": l += "0" else: l += "1" else: l += a[i] return operation(l, b) for _ in range(int(input())): n = int(input()) s = list(input()) t = list(input()) if s == t: print(0) continue else: ans = operation(s, t) ans = min(ans, 1 + change(s, t, "1")) ans = min(ans, 1 + change(s, t, "0")) if ans == float("inf"): print(-1) else: print(ans)
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR STRING RETURN BIN_OP NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR STRING VAR STRING VAR STRING VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR STRING IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
for i in range(int(input())): n = int(input()) a = input() b = input() x, y, z = 0, 0, 0 for i in range(n): if a[i] != b[i]: x += 1 if a[i] == "1": y += 1 if b[i] == "1": z += 1 if y == z: if n - y + 1 == z: print(min(x, n - x)) else: print(x) elif n - y + 1 == z: print(n - x) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
inf = 1000000000.0 t = int(input()) for u in range(t): n = int(input()) a = input() b = input() ok = False for i in range(n): if a[i] == "1": ok = True if ok == False and a != b: print(-1) continue ans = inf for i in range(2): cnt = [0, 0, 0, 0] for j in range(n): if a[j] == "0": if b[j] == "0": cnt[0] += 1 else: cnt[1] += 1 elif b[j] == "0": cnt[2] += 1 else: cnt[3] += 1 if i == 1: if cnt[3] - cnt[0] == 1 or cnt[3] - cnt[0] == 0: if (cnt[3] + cnt[0]) % 2 == i: ans = min(ans, cnt[3] + cnt[0]) elif cnt[2] - cnt[1] == 1 or cnt[2] - cnt[1] == 0: if (cnt[2] + cnt[1]) % 2 == i: ans = min(ans, cnt[2] + cnt[1]) if ans == inf: print(-1) else: print(ans)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
import sys input = sys.stdin.readline def read_strs(): s = input().strip() return list(map(int, list(s))) def solve(a, b): x, y, diff = 0, 0, 0 n = len(a) result = n + 1 for i in range(n): x += a[i] y += b[i] diff += 1 if a[i] != b[i] else 0 if x == y: result = min(diff, result) if x == n - y + 1: result = min(n - diff, result) return result if result < n + 1 else -1 n = int(input()) for _ in range(n): m = int(input()) a = read_strs() b = read_strs() print(solve(a, b))
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
from sys import stdin input = stdin.readline def solve(a): ans, bal, flip = 0, 0, 0 for i in range(n): if a[i] == "1" and b[i] == "0": bal += 1 ans += 1 if a[i] == "0" and b[i] == "1": bal -= 1 ans += 1 if bal == 0: return ans return float("inf") def answer(): ans = solve(a) ind = -1 for i in range(n): if a[i] == "1" and b[i] == "1": ind = i break if ind == -1: if ans == float("inf"): return -1 return ans na = "" for i in range(n): if ind == i: na += a[i] elif a[i] == "1": na += "0" else: na += "1" ans = min(ans, solve(na) + 1) if ans == float("inf"): return -1 return ans for T in range(int(input())): n = int(input()) a = input().strip() b = input().strip() print(answer())
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR IF VAR VAR STRING VAR STRING VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
import sys def readnum(): input = sys.stdin.readline a = list(map(int, input().split())) return a def readstr(): input = sys.stdin.readline a = str(input().split()[0]) return a def printf(val): sys.stdout.write(str(val) + "\n") def power(a, b, mod): if b == 1: return a if b == 0: return 1 ans = power(a, b // 2, mod) ans *= ans if b % 2: ans *= a ans = ans % mod return ans def factorial(n, start=1, skip=None): res = start for i in range(1, n + 1): if i == skip: continue res *= i return res def make_deg(l): deg = {} for i in l: if not i in deg: deg[i] = 0 deg[i] += 1 return deg def get_num_of_bits(n): res = [] for i in range(1, 31): res.append(2 ** (i - 1) * (n // 2**i) + max(0, n % 2**i - 2 ** (i - 1))) return res def minus(l, r): res = [] for i in range(len(l)): res.append(r[i] - l[i]) return res def revert(s, idx): res = "" for i in range(len(s)): if i == idx: res += "1" continue if s[i] == "0": res += "1" elif s[i] == "1": res += "0" return res for _ in range(readnum()[0]): n = readnum()[0] a = readstr() b = readstr() ones_a = sum([(i == "1") for i in a]) ones_b = sum([(i == "1") for i in b]) res = [] if ones_a == ones_b: res.append(sum(a[i] != b[i] for i in range(n))) if ones_a == 1 + n - ones_b: reverted = None for i in range(n): if a[i] == b[i] and a[i] == "1": reverted = i break if reverted is None: for i in range(n): if a[i] == "1": reverted = i break a = revert(a, reverted) res.append(1 + sum(a[i] != b[i] for i in range(n))) if len(res) == 0: print(-1) else: print(min(res))
IMPORT FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF NUMBER NONE ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING IF VAR VAR STRING VAR STRING IF VAR VAR STRING VAR STRING RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING VAR VAR ASSIGN VAR LIST IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR IF VAR NONE FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
def find(n, a, b): dif = 0 sum_a = 0 sum_b = 0 for i in range(n): if a[i] == "1": sum_a += 1 if b[i] == "1": sum_b += 1 if a[i] != b[i]: dif += 1 if sum_a != sum_b and sum_a + sum_b != n + 1: return -1 if sum_a == sum_b: if sum_a + sum_b == n + 1: return min(dif, n - dif) return dif return n - dif cases = int(input()) for _ in range(cases): n = int(input()) a = input() b = input() print(find(n, a, b))
FUNC_DEF 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 VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
def solve(): n = int(input()) a = list(input()) b = list(input()) bcount = b.count("1") acount = a.count("1") aicount = n - acount + 1 if acount != bcount and aicount != bcount: print(-1) return diffcount = 0 for i in range(len(a)): if a[i] != b[i]: diffcount += 1 if acount != bcount: print(n - diffcount) return if aicount != bcount: print(diffcount) return print(min(diffcount, n - diffcount)) for _ in range(int(input())): solve()
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 ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
import sys I = lambda: [*map(int, sys.stdin.readline().split())] prin = sys.stdout.write (t,) = I() for _ in range(t): (n,) = I() s1 = input() s2 = input() o1 = s1.count("1") o2 = s2.count("1") if o1 != o2 and o1 + o2 != n + 1: print(-1) elif o1 == o2 and o1 + o2 != n + 1: diff = 0 for i in range(n): if s1[i] != s2[i]: diff += 1 print(diff) elif o1 + o2 == n + 1 and o1 != o2: diff = 0 for i in range(n): if s1[i] == s2[i]: diff += 1 print(diff) else: diff = 0 for i in range(n): if s1[i] != s2[i]: diff += 1 print(min(diff, n - diff))
IMPORT ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
t = int(input()) for test in range(t): n = int(input()) a = input() b = input() zC = a.count("0") oC = n - zC desiredOC = b.count("1") if oC == 0: if desiredOC == 0: print(0) continue else: print(-1) continue flag = False if oC == desiredOC: diff = 0 for idx in range(n): if a[idx] == "1" and b[idx] == "0": diff += 1 m = 2 * diff flag = True if zC + 1 == desiredOC: diff = 0 for idx in range(n): if a[idx] == "0" and b[idx] == "0": diff += 1 if not flag: flag = True m = diff * 2 + 1 else: m = min(diff * 2 + 1, m) if flag: print(m) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$. Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. -----Input----- The first line contains an integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) β€” the number of candles. The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 β€” the initial pattern of lights. The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 β€” the desired pattern of lights. It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible. -----Examples----- Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Output 0 1 -1 3 4 -----Note----- In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $a$ into $b$: Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$. Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$. Select the $1$-st candle: ${1}10010011\to {1}01101100$. In the fifth test case, we can perform the following operations to transform $a$ into $b$: Select the $6$-th candle: $00101{1}011\to 11010{1}100$ Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$ Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$ Select the $7$-th candle: $100101{1}10\to 011010{1}01$
t = int(input()) while t: n = int(input()) a = input() b = input() f00, f01, f10, f11 = 0, 0, 0, 0 res = 0 for i in range(n): if a[i] == "0" and b[i] == "0": f00 += 1 elif a[i] == "0" and b[i] == "1": f01 += 1 elif a[i] == "1" and b[i] == "0": f10 += 1 else: f11 += 1 res = 100000000.0 + 1 if f01 == f10: res = min(res, 2 * f01) if f00 == f11 - 1: res = min(res, 1 + 2 * f00) if res > 100000000.0: res = -1 print(res) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Innocentius has a problem β€” his computer monitor has broken. Now some of the pixels are "dead", that is, they are always black. As consequence, Innocentius can't play the usual computer games. He is recently playing the following game with his younger brother Polycarpus. Innocentius is touch-typing a program that paints a white square one-pixel wide frame on the black screen. As the monitor is broken, some pixels that should be white remain black. Polycarpus should look at what the program displayed on the screen and guess the position and size of the frame Innocentius has painted. Polycarpus doesn't like the game but Innocentius persuaded brother to play as "the game is good for the imagination and attention". Help Polycarpus, automatize his part in the gaming process. Write the code that finds such possible square frame that: the frame's width is 1 pixel, the frame doesn't go beyond the borders of the screen, all white pixels of the monitor are located on the frame, of all frames that satisfy the previous three conditions, the required frame must have the smallest size. Formally, a square frame is represented by such pixels of the solid square, that are on the square's border, that is, are not fully surrounded by the other pixels of the square. For example, if the frame's size is d = 3, then it consists of 8 pixels, if its size is d = 2, then it contains 4 pixels and if d = 1, then the frame is reduced to a single pixel. -----Input----- The first line contains the resolution of the monitor as a pair of integers n, m (1 ≀ n, m ≀ 2000). The next n lines contain exactly m characters each β€” the state of the monitor pixels at the moment of the game. Character "." (period, ASCII code 46) corresponds to the black pixel, and character "w" (lowercase English letter w) corresponds to the white pixel. It is guaranteed that at least one pixel of the monitor is white. -----Output----- Print the monitor screen. Represent the sought frame by characters "+" (the "plus" character). The pixels that has become white during the game mustn't be changed. Print them as "w". If there are multiple possible ways to position the frame of the minimum size, print any of them. If the required frame doesn't exist, then print a single line containing number -1. -----Examples----- Input 4 8 ..w..w.. ........ ........ ..w..w.. Output ..w++w.. ..+..+.. ..+..+.. ..w++w.. Input 5 6 ...... .w.... ...... ..w... ...... Output ...... +w+... +.+... ++w... ...... Input 2 4 .... .w.. Output .... .w.. Input 2 6 w..w.w ...w.. Output -1 -----Note----- In the first sample the required size of the optimal frame equals 4. In the second sample the size of the optimal frame equals 3. In the third sample, the size of the optimal frame is 1. In the fourth sample, the required frame doesn't exist.
import sys def readln(): return list(map(int, input().split())) def exit(): print(-1) sys.exit() n, m = readln() mon = [list(input()) for _ in range(n)] hor = [i for i in range(n) if mon[i] != ["."] * m] rmon = list(zip(*mon)) ver = [j for j in range(m) if rmon[j] != (".",) * n] mini = hor[0] maxi = hor[-1] minj = ver[0] maxj = ver[-1] cnt_in = len( [ (1) for i in range(mini + 1, maxi) for j in range(minj + 1, maxj) if mon[i][j] == "w" ] ) cnt_l = len([(1) for i in range(mini + 1, maxi) if mon[i][minj] == "w"]) cnt_r = len([(1) for i in range(mini + 1, maxi) if mon[i][maxj] == "w"]) cnt_d = len([(1) for j in range(minj + 1, maxj) if mon[mini][j] == "w"]) cnt_u = len([(1) for j in range(minj + 1, maxj) if mon[maxi][j] == "w"]) if cnt_in: exit() if maxi - mini < maxj - minj: k = maxj - minj + 1 if maxi == mini and cnt_d: if mini >= k - 1: mini -= k - 1 elif maxi + k - 1 < n: maxi += k - 1 else: exit() else: if not cnt_d: mini = max(0, maxi - k + 1) if maxi - maxi + 1 != k and not cnt_u: maxi = min(mini + k - 1, n - 1) if maxi - mini + 1 != k: exit() else: k = maxi - mini + 1 if maxj == minj and cnt_l: if minj >= k - 1: minj -= k - 1 elif maxj + k - 1 < m: maxj += k - 1 else: exit() else: if not cnt_l: minj = max(0, maxj - k + 1) if maxj - minj + 1 != k and not cnt_r: maxj = min(minj + k - 1, m - 1) if maxj - minj + 1 != k: exit() for i in range(mini, maxi + 1): if mon[i][minj] == ".": mon[i][minj] = "+" for i in range(mini, maxi + 1): if mon[i][maxj] == ".": mon[i][maxj] = "+" for j in range(minj, maxj + 1): if mon[mini][j] == ".": mon[mini][j] = "+" for j in range(minj, maxj + 1): if mon[maxi][j] == ".": mon[maxi][j] = "+" print("\n".join(["".join(row) for row in mon]))
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP LIST STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP STRING VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR STRING IF VAR EXPR FUNC_CALL VAR IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING VAR VAR VAR
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it? The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides. Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it. A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters. Input The input consists of a single string s (2 ≀ |s| ≀ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal. Output Output a palindrome t that is a subsequence of s and |t| β‰₯ ⌊ (|s|)/(2) βŒ‹. If there are multiple solutions, you may print any of them. You don't have to maximise the length of t. If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity). Examples Input cacbac Output aba Input abc Output a Input cbacacacbcbababacbcb Output cbaaacbcaaabc Note In the first example, other valid answers include "cacac", "caac", "aca" and "ccc".
s = input() n = len(s) cnt = {"a": 0, "b": 0, "c": 0} for i in range(n): cnt[s[i]] += 1 base = "a" if cnt[s[0]] > cnt[base]: base = s[0] if cnt[s[1]] > cnt[base]: base = s[1] on = [] le = cnt[base] for i in range(n): on.append(s[i] == base) bl, br = 0, 0 il, ir = 0, n - 1 while le < n // 2 and il <= ir: if s[il] == base: bl += 1 il += 1 elif s[ir] == base: br += 1 ir -= 1 elif bl < br: il += 1 elif bl > br: ir -= 1 elif s[il] == s[ir]: on[il] = True on[ir] = True il += 1 ir -= 1 if il != ir: le += 2 else: le += 1 else: ir -= 1 if le >= n // 2: ans = "".join([s[i] for i in range(n) if on[i]]) print(ans) else: print("IMPOSSIBLE")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR STRING IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it? The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides. Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it. A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters. Input The input consists of a single string s (2 ≀ |s| ≀ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal. Output Output a palindrome t that is a subsequence of s and |t| β‰₯ ⌊ (|s|)/(2) βŒ‹. If there are multiple solutions, you may print any of them. You don't have to maximise the length of t. If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity). Examples Input cacbac Output aba Input abc Output a Input cbacacacbcbababacbcb Output cbaaacbcaaabc Note In the first example, other valid answers include "cacac", "caac", "aca" and "ccc".
import sys def main(): s = input() n = len(s) l = 0 r = n - 1 temp = [] temp2 = [] while l <= r: if s[l] == s[r]: temp.append(s[l]) if l != r: temp2.append(s[r]) l += 1 r -= 1 elif s[l] == s[r - 1]: temp.append(s[l]) if l != r - 1: temp2.append(s[r - 1]) l += 1 r -= 2 else: l += 1 r -= 1 rev = temp2[::-1] cout << "".join(temp) << "".join(rev) class ostream: def __lshift__(self, a): sys.stdout.write(str(a)) return self cout = ostream() endl = "\n" main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER EXPR BIN_OP BIN_OP VAR FUNC_CALL STRING VAR FUNC_CALL STRING VAR CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it? The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides. Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it. A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters. Input The input consists of a single string s (2 ≀ |s| ≀ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal. Output Output a palindrome t that is a subsequence of s and |t| β‰₯ ⌊ (|s|)/(2) βŒ‹. If there are multiple solutions, you may print any of them. You don't have to maximise the length of t. If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity). Examples Input cacbac Output aba Input abc Output a Input cbacacacbcbababacbcb Output cbaaacbcaaabc Note In the first example, other valid answers include "cacac", "caac", "aca" and "ccc".
from sys import exit, stdin, stdout s = stdin.readline().strip() n = len(s) ans_head = [] ans_tail = [] idx_h = 0 idx_t = n - 1 while idx_h + 1 < idx_t - 1: heads = set([s[idx_h], s[idx_h + 1]]) if s[idx_t] in heads: ans_head.append(s[idx_t]) ans_tail.append(s[idx_t]) else: ans_head.append(s[idx_t - 1]) ans_tail.append(s[idx_t - 1]) idx_h += 2 idx_t -= 2 if n % 4 != 0: ans_head.append(s[(n + 1) // 2]) ans = "".join(ans_head) + "".join(reversed(ans_tail)) stdout.write(ans + "\n")
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL STRING VAR FUNC_CALL STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it? The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides. Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it. A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters. Input The input consists of a single string s (2 ≀ |s| ≀ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal. Output Output a palindrome t that is a subsequence of s and |t| β‰₯ ⌊ (|s|)/(2) βŒ‹. If there are multiple solutions, you may print any of them. You don't have to maximise the length of t. If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity). Examples Input cacbac Output aba Input abc Output a Input cbacacacbcbababacbcb Output cbaaacbcaaabc Note In the first example, other valid answers include "cacac", "caac", "aca" and "ccc".
import sys input = sys.stdin.readline s = [0] + list(input().strip()) + [0] A = 0 B = len(s) - 1 ANS = [] while A < B: str0 = [5] * 3 str1 = [5] * 3 str0[ord(s[A + 2]) - 97] = 2 str0[ord(s[A + 1]) - 97] = 1 str1[ord(s[B - 2]) - 97] = 2 str1[ord(s[B - 1]) - 97] = 1 M = [(str0[i] + str1[i]) for i in range(3)] nextstr = M.index(min(M)) A += str0[nextstr] B -= str1[nextstr] if A < B: ANS.append(s[A]) elif A == B: ANS = ANS + [s[A]] + ANS[::-1] break else: ANS = ANS + ANS[::-1] break print("".join(ANS))
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR LIST VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it? The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides. Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it. A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters. Input The input consists of a single string s (2 ≀ |s| ≀ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal. Output Output a palindrome t that is a subsequence of s and |t| β‰₯ ⌊ (|s|)/(2) βŒ‹. If there are multiple solutions, you may print any of them. You don't have to maximise the length of t. If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity). Examples Input cacbac Output aba Input abc Output a Input cbacacacbcbababacbcb Output cbaaacbcaaabc Note In the first example, other valid answers include "cacac", "caac", "aca" and "ccc".
import sys input = sys.stdin.readline s = input() n = len(s) ans = [] ans1 = [] mxm = (n - 1) // 2 pt = 0 pt1 = n - 2 while pt < pt1 and mxm > 0: if mxm == 1: ans.append(s[pt]) break q1 = pt q2 = pt1 while q1 < q2 and q2 > pt1 - 2: if s[q1] == s[q2]: break q2 -= 1 if s[q1] == s[q2]: ans.append(s[q1]) ans1.append(s[q2]) pt1 = q2 - 1 mxm -= 2 pt += 1 ans1.reverse() for i in ans: print(i, end="") for i in ans1: print(i, end="")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it? The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides. Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it. A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters. Input The input consists of a single string s (2 ≀ |s| ≀ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal. Output Output a palindrome t that is a subsequence of s and |t| β‰₯ ⌊ (|s|)/(2) βŒ‹. If there are multiple solutions, you may print any of them. You don't have to maximise the length of t. If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity). Examples Input cacbac Output aba Input abc Output a Input cbacacacbcbababacbcb Output cbaaacbcaaabc Note In the first example, other valid answers include "cacac", "caac", "aca" and "ccc".
from sys import stdin, stdout s = stdin.readline().strip() n = len(s) if n <= 3: print(s[0]) else: ans = "" start, end = 0, n - 1 while start + 1 < end - 1: if s[start] == s[end] or s[start] == s[end - 1]: ans += s[start] else: ans += s[start + 1] start += 2 end -= 2 t = len(ans) if 2 * t < n // 2: ans = ans + s[start] + ans[::-1] print(ans) else: print(ans + ans[::-1])
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it? The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides. Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it. A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters. Input The input consists of a single string s (2 ≀ |s| ≀ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal. Output Output a palindrome t that is a subsequence of s and |t| β‰₯ ⌊ (|s|)/(2) βŒ‹. If there are multiple solutions, you may print any of them. You don't have to maximise the length of t. If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity). Examples Input cacbac Output aba Input abc Output a Input cbacacacbcbababacbcb Output cbaaacbcaaabc Note In the first example, other valid answers include "cacac", "caac", "aca" and "ccc".
s = input() l = 0 r = len(s) - 1 ans = "" while r - l > 1: if s[l] == s[r]: ans += s[l] l += 1 r -= 1 elif r - l > 2 and s[l + 1] == s[r]: ans += s[l + 1] l += 2 r -= 1 elif r - l > 2 and s[l] == s[r - 1]: ans += s[l] l += 1 r -= 2 elif r - l > 3 and s[l + 1] == s[r - 1]: ans += s[l + 1] l += 2 r -= 2 else: break if r - l > 0: msg = ans + s[l + 1] + ans[::-1] elif r - l == 0: msg = ans + s[l] + ans[::-1] else: msg = ans + ans[::-1] if len(msg) >= len(s) // 2: print(msg) else: print("IMPOSSIBLE")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING WHILE BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it? The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides. Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it. A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters. Input The input consists of a single string s (2 ≀ |s| ≀ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal. Output Output a palindrome t that is a subsequence of s and |t| β‰₯ ⌊ (|s|)/(2) βŒ‹. If there are multiple solutions, you may print any of them. You don't have to maximise the length of t. If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity). Examples Input cacbac Output aba Input abc Output a Input cbacacacbcbababacbcb Output cbaaacbcaaabc Note In the first example, other valid answers include "cacac", "caac", "aca" and "ccc".
S = input() i = 0 j = len(S) - 1 ans = [] f = 0 while i < j: if S[i] == S[j]: pass elif S[i] == S[j - 1]: j -= 1 elif S[i + 1] == S[j]: i += 1 else: i += 1 j -= 1 if i == j: f = 1 ans.append(S[i]) i += 1 j -= 1 print("".join(ans + ans[::-1][f:]))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR NUMBER VAR
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it? The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides. Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it. A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters. Input The input consists of a single string s (2 ≀ |s| ≀ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal. Output Output a palindrome t that is a subsequence of s and |t| β‰₯ ⌊ (|s|)/(2) βŒ‹. If there are multiple solutions, you may print any of them. You don't have to maximise the length of t. If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity). Examples Input cacbac Output aba Input abc Output a Input cbacacacbcbababacbcb Output cbaaacbcaaabc Note In the first example, other valid answers include "cacac", "caac", "aca" and "ccc".
sre = input() pre = 0 kre = len(sre) - 1 pointeree = 0 plotttt = [] while pre + 1 < kre - 1: if sre[pre] == sre[kre]: plotttt.append(sre[pre]) pre += 1 kre -= 1 pointeree += 2 elif sre[pre] == sre[kre - 1]: plotttt.append(sre[pre]) pre += 1 kre -= 2 elif sre[pre + 1] == sre[kre]: plotttt.append(sre[pre + 1]) pre += 2 kre -= 1 else: plotttt.append(sre[pre + 1]) pre += 2 kre -= 2 dfdkngkfgjfb = 0 if kre - pre == 2: xrere = [sre[pre], sre[pre + 1], sre[pre + 2]] xrere.sort() if xrere[0] == xrere[1]: plotttt.append(xrere[0]) elif xrere[1] == xrere[2]: plotttt.append(xrere[1]) else: dfdkngkfgjfb = xrere[1] if kre - pre == 1: if sre[pre] == sre[pre + 1]: plotttt.append(sre[pre]) else: dfdkngkfgjfb = sre[pre] if kre == pre: dfdkngkfgjfb = sre[pre] if dfdkngkfgjfb == 0: if len(plotttt) * 2 >= len(sre) // 2: for i in plotttt: print(i, end="") for i in range(len(plotttt)): j = len(plotttt) - 1 - i print(plotttt[j], end="") else: print("IMPOSSIBLE") elif 2 * len(plotttt) + 1 >= len(sre) // 2: for i in plotttt: print(i, end="") print(dfdkngkfgjfb, end="") for i in range(len(plotttt)): j = len(plotttt) - 1 - i print(plotttt[j], end="") else: print("IMPOSSIBLE")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it? The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides. Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it. A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters. Input The input consists of a single string s (2 ≀ |s| ≀ 10^6). The string s consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal. Output Output a palindrome t that is a subsequence of s and |t| β‰₯ ⌊ (|s|)/(2) βŒ‹. If there are multiple solutions, you may print any of them. You don't have to maximise the length of t. If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity). Examples Input cacbac Output aba Input abc Output a Input cbacacacbcbababacbcb Output cbaaacbcaaabc Note In the first example, other valid answers include "cacac", "caac", "aca" and "ccc".
s = input() length = len(s) left = 0 right = len(s) - 1 endStr = "" while left < right - 2: if s[left] == s[right]: endStr = endStr + s[left] left += 1 right -= 1 elif s[left] == s[right - 1]: endStr = endStr + s[left] left += 1 right -= 2 elif s[left + 1] == s[right]: endStr = endStr + s[right] left += 2 right -= 1 else: endStr = endStr + s[left + 1] left += 2 right -= 2 endStr = endStr + s[left] + endStr[::-1] if len(s) // 2 > len(endStr): print("IMPOSSIBLE") else: print(endStr)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR