description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well. Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$: P := [1, 2, ..., N] for i in 1..N do j := rand(1, N) swap(P[i], P[j]) Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$. Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible. ------ Input ------ The first and only line of the input contains a single integer $N$. ------ Output ------ Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$. If there are multiple answers, you may print any one. ------ Constraints ------ $1 ≤ N ≤ 17$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≤ N ≤ 7$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 ----- Sample Output 1 ------ 1 2 2 1 ----- explanation 1 ------ There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer.
n = int(input()) if n == 1: print("1") print("1") elif n == 2: print("1 2") print("2 1") elif n == 3: print("2 1 3") print("3 1 2") else: med = n // 2 print( " ".join([str(x) for x in range(2, med + 1)]), 1, " ".join([str(x) for x in range(med + 2, n + 1)]), med + 1, ) print(n, end=" ") for i in range(1, n): print(i, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well. Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$: P := [1, 2, ..., N] for i in 1..N do j := rand(1, N) swap(P[i], P[j]) Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$. Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible. ------ Input ------ The first and only line of the input contains a single integer $N$. ------ Output ------ Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$. If there are multiple answers, you may print any one. ------ Constraints ------ $1 ≤ N ≤ 17$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≤ N ≤ 7$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 ----- Sample Output 1 ------ 1 2 2 1 ----- explanation 1 ------ There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer.
n = int(input()) arr = [i for i in range(0, n + 1)] for i in range(1, n): if i != n // 2: arr[i], arr[i + 1] = arr[i + 1], arr[i] for i in range(1, n + 1): print(arr[i], end=" ") print() print(n, end=" ") for i in range(1, n): print(i, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well. Consider the following algorithm, which generates a (not necessarily uniformly) random permutation of numbers $1$ through $N$: P := [1, 2, ..., N] for i in 1..N do j := rand(1, N) swap(P[i], P[j]) Here, rand(1, N) returns a uniformly random integer between $1$ and $N$ inclusive. Let's denote the probability that the permutation generated by this algorithm is $P$ by $p(P)$. Find a permutation $P_{1}$ such that $p(P_{1})$ is maximum possible and a permutation $P_{2}$ such that $p(P_{2})$ is minimum possible. ------ Input ------ The first and only line of the input contains a single integer $N$. ------ Output ------ Print two lines. The first line should contain $N$ space-separated integers denoting your permutation $P_{1}$. The second line should contain $N$ space-separated integers denoting your permutation $P_{2}$. If there are multiple answers, you may print any one. ------ Constraints ------ $1 ≤ N ≤ 17$ ------ Subtasks ------ Subtask #1 (20 points): $1 ≤ N ≤ 7$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 ----- Sample Output 1 ------ 1 2 2 1 ----- explanation 1 ------ There are two possible permutations, $[1, 2]$ and $[2, 1]$. Both are equally likely to appear as the result of the given algorithm, so any pair of permutations is a valid answer.
n = int(input()) middle = n / 2 if middle * 10 % 10 != 0: middle = int(middle) + 1 middle = int(middle) for i in range(1, middle): print(i + 1, end=" ") print(1, end=" ") for i in range(middle + 2, n + 1): print(i, end=" ") print(middle + 1, end="\n") print(n, end=" ") for i in range(n - 1): print(i + 1, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING
You have a string of decimal digits s. Let's define b_{ij} = s_{i}·s_{j}. Find in matrix b the number of such rectangles that the sum b_{ij} for all cells (i, j) that are the elements of the rectangle equals a in each rectangle. A rectangle in a matrix is a group of four integers (x, y, z, t) (x ≤ y, z ≤ t). The elements of the rectangle are all cells (i, j) such that x ≤ i ≤ y, z ≤ j ≤ t. -----Input----- The first line contains integer a (0 ≤ a ≤ 10^9), the second line contains a string of decimal integers s (1 ≤ |s| ≤ 4000). -----Output----- Print a single integer — the answer to a problem. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 10 12345 Output 6 Input 16 439873893693495623498263984765 Output 40
a = int(input()) b = [int(i) for i in input()] n = len(b) c = [0] * (sum(b) + 1) d = [0] e = 0 for i in range(n): e += b[i] d.append(e) for i in range(n): for j in range(i, n): c[d[j + 1] - d[i]] += 1 if a == 0: print(2 * c[0] * sum(c) - c[0] ** 2) else: f = 0 c1 = len(c) for i in range(1, int(a**0.5) + 1): if a % i == 0 and i < c1 and a // i < c1: f += c[i] * c[a // i] if i != a // i: f += c[a // i] * c[i] print(f)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You have a string of decimal digits s. Let's define b_{ij} = s_{i}·s_{j}. Find in matrix b the number of such rectangles that the sum b_{ij} for all cells (i, j) that are the elements of the rectangle equals a in each rectangle. A rectangle in a matrix is a group of four integers (x, y, z, t) (x ≤ y, z ≤ t). The elements of the rectangle are all cells (i, j) such that x ≤ i ≤ y, z ≤ j ≤ t. -----Input----- The first line contains integer a (0 ≤ a ≤ 10^9), the second line contains a string of decimal integers s (1 ≤ |s| ≤ 4000). -----Output----- Print a single integer — the answer to a problem. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 10 12345 Output 6 Input 16 439873893693495623498263984765 Output 40
sm = [0] * 40000 n = int(input()) s = input() l = len(s) for i in range(l): ss = 0 for j in range(i, l): ss += int(s[j]) sm[ss] += 1 if n == 0: ans = 0 for i in range(1, 40000): ans += sm[0] * sm[i] * 2 ans += sm[0] * sm[0] print(ans) else: ans = 0 u = int(n**0.5) for i in range(1, u + 1): if n % i == 0: if n // i < 40000: ans += sm[i] * sm[n // i] ans *= 2 if u**2 == n: ans -= sm[u] ** 2 print(ans)
ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a string of decimal digits s. Let's define b_{ij} = s_{i}·s_{j}. Find in matrix b the number of such rectangles that the sum b_{ij} for all cells (i, j) that are the elements of the rectangle equals a in each rectangle. A rectangle in a matrix is a group of four integers (x, y, z, t) (x ≤ y, z ≤ t). The elements of the rectangle are all cells (i, j) such that x ≤ i ≤ y, z ≤ j ≤ t. -----Input----- The first line contains integer a (0 ≤ a ≤ 10^9), the second line contains a string of decimal integers s (1 ≤ |s| ≤ 4000). -----Output----- Print a single integer — the answer to a problem. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 10 12345 Output 6 Input 16 439873893693495623498263984765 Output 40
def divisors(x): def f(y, q): t = -len(r) while not y % q: y //= q for i in range(t, 0): r.append(r[t] * q) return y r, p = [1], 7 x = f(f(f(x, 2), 3), 5) while x >= p * p: for s in (4, 2, 4, 2, 4, 6, 2, 6): if not x % p: x = f(x, p) p += s if x > 1: f(x, x) return r def main(): a, s = int(input()), input() if not a: z = ( sum( x * (x + 1) for x in map( len, s.translate(str.maketrans("123456789", " ")).split() ) ) // 2 ) x = len(s) print((x * (x + 1) - z) * z) return sums, x, cnt = {}, 0, 1 for u in map(int, s): if u: sums[x] = cnt x += u cnt = 1 else: cnt += 1 if x * x < a: print(0) return sums[x], u = cnt, a // x l = [v for v in divisors(a) if v <= x] z = a // max(l) d = {x: (0) for x in l if z <= x} for x in d: for k, v in list(sums.items()): u = sums.get(k + x, 0) if u: d[x] += v * u print(sum(u * d[a // x] for x, u in list(d.items()))) def __starting_point(): main() __starting_point()
FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER WHILE VAR BIN_OP VAR VAR FOR VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN ASSIGN VAR VAR VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR VAR FOR VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You have a string of decimal digits s. Let's define b_{ij} = s_{i}·s_{j}. Find in matrix b the number of such rectangles that the sum b_{ij} for all cells (i, j) that are the elements of the rectangle equals a in each rectangle. A rectangle in a matrix is a group of four integers (x, y, z, t) (x ≤ y, z ≤ t). The elements of the rectangle are all cells (i, j) such that x ≤ i ≤ y, z ≤ j ≤ t. -----Input----- The first line contains integer a (0 ≤ a ≤ 10^9), the second line contains a string of decimal integers s (1 ≤ |s| ≤ 4000). -----Output----- Print a single integer — the answer to a problem. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 10 12345 Output 6 Input 16 439873893693495623498263984765 Output 40
def f(t, k): i, j = 0, 1 s, d = 0, t[0] n = len(t) while j <= n: if d > k: d -= t[i] i += 1 elif d == k: if t[i] and (j == n or t[j]): s += 1 else: a, b = i - 1, j - 1 while j < n and t[j] == 0: j += 1 while t[i] == 0: i += 1 s += (i - a) * (j - b) if j < n: d += t[j] d -= t[i] i += 1 j += 1 else: if j < n: d += t[j] j += 1 return s s, n = 0, int(input()) t = list(map(int, input())) if n: k = sum(t) if k == 0: print(0) else: p = [(i, n // i) for i in range(max(1, n // k), int(n**0.5) + 1) if n % i == 0] for a, b in p: if a != b: s += 2 * f(t, a) * f(t, b) else: k = f(t, a) s += k * k print(s) else: n = len(t) m = n * (n + 1) s = j = 0 while j < n: if t[j] == 0: i = j j += 1 while j < n and t[j] == 0: j += 1 k = (j - i) * (j - i + 1) // 2 s += k j += 1 print((m - s) * s)
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
You have a string of decimal digits s. Let's define b_{ij} = s_{i}·s_{j}. Find in matrix b the number of such rectangles that the sum b_{ij} for all cells (i, j) that are the elements of the rectangle equals a in each rectangle. A rectangle in a matrix is a group of four integers (x, y, z, t) (x ≤ y, z ≤ t). The elements of the rectangle are all cells (i, j) such that x ≤ i ≤ y, z ≤ j ≤ t. -----Input----- The first line contains integer a (0 ≤ a ≤ 10^9), the second line contains a string of decimal integers s (1 ≤ |s| ≤ 4000). -----Output----- Print a single integer — the answer to a problem. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 10 12345 Output 6 Input 16 439873893693495623498263984765 Output 40
a = int(input()) s = input() d = {} for i in range(len(s)): for j in range(i, len(s)): if j == i: t = int(s[j]) else: t += int(s[j]) d[t] = d.get(t, 0) + 1 if a == 0: if 0 in d: cnt_pairs = len(s) * (len(s) + 1) // 2 print(d[0] * cnt_pairs + d[0] * (cnt_pairs - d[0])) else: print(0) else: c = 0 for f in d: if f != 0 and a % f == 0 and a // f in d: c += d[f] * d[a // f] print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER IF NUMBER VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You have a string of decimal digits s. Let's define b_{ij} = s_{i}·s_{j}. Find in matrix b the number of such rectangles that the sum b_{ij} for all cells (i, j) that are the elements of the rectangle equals a in each rectangle. A rectangle in a matrix is a group of four integers (x, y, z, t) (x ≤ y, z ≤ t). The elements of the rectangle are all cells (i, j) such that x ≤ i ≤ y, z ≤ j ≤ t. -----Input----- The first line contains integer a (0 ≤ a ≤ 10^9), the second line contains a string of decimal integers s (1 ≤ |s| ≤ 4000). -----Output----- Print a single integer — the answer to a problem. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 10 12345 Output 6 Input 16 439873893693495623498263984765 Output 40
a = int(input()) s = input() di = {} for i in range(len(s)): total = 0 for j in range(i, len(s)): total += int(s[j]) di[total] = 1 if total not in di else di[total] + 1 ans = 0 if a == 0: ans = 0 if 0 in di: ans += di[0] * di[0] for each in di: if not each: continue ans += di[each] * di[0] * 2 print(ans) quit() for p in di: if p and a % p == 0 and a // p in di: ans += di[a // p] * di[p] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
def consc_count(stri): res = [] i = 0 while i < len(stri): if stri[i] == "0": c = 1 j = i + 1 while j < len(stri): if stri[i] == stri[j]: c += 1 else: break j += 1 res.append(c) i += c continue i += 1 res.sort(reverse=True) return res def mainfunc(stri, l, h): res = consc_count(stri) if res: if h <= res[0]: return "YES" temp = 2 * (h - res[0]) ind = 1 while ind < len(res) and temp: if temp <= res[ind]: return "YES" temp = 2 * (temp - res[ind]) ind += 1 return "NO" for _ in range(int(input())): l, h = map(int, input().split()) stri = input() print(mainfunc(stri, l, h))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR IF VAR VAR NUMBER RETURN STRING ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR RETURN STRING ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR VAR VAR NUMBER RETURN STRING 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
for t in range(int(input())): l, h = map(int, input().split()) total = input() ans = "NO" l1 = total.split("1") l2 = [] for i in l1: l2.append(len(i)) for i in l2: if i < h: if i > h // 2: h = 2 * (h - i) else: continue else: ans = "YES" 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 STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
for _ in range(int(input())): l, h = map(int, input().split()) s = input().split("1")[:l] a = False for i in s: if i != "": if len(i) >= h: a = True break elif len(i) > h // 2: h = 2 * (h - len(i)) if a == True: print("YES") else: print("NO")
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 FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
for _ in range(int(input())): l, h = map(int, input().split()) s = input() ai = [] c = flag = 0 for i in range(l): if s[i] == "1": ai.append(c) c = 0 else: c = c + 1 ai.append(c) ai.sort(reverse=True) for j in range(len(ai)): if ai[j] >= h: flag = 1 break else: h = 2 * (h - ai[j]) if flag == 1: print("YES") else: print("NO")
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 LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
T = int(input()) for t in range(T): l, h = map(int, input().split()) a = list(input()) for i in range(len(a)): a[i] = int(a[i]) s = [] c = 0 for i in range(len(a)): if a[i] == 0: c += 1 elif i == 0: pass elif a[i - 1] == 0: s.append(c) c = 0 if c != 0: s.append(c) s.sort() i = len(s) - 1 while i >= 0: h -= s[i] if h <= 0: break h *= 2 i -= 1 if h <= 0: print("YES") else: print("NO")
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 FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
t = int(input()) for i in range(t): a, b = map(int, input().split()) x = input() lt = [] count = 0 for i in x: if i == "1": if count != 0: lt.append(count) count = 0 else: count += 1 if count != 0: lt.append(count) lt.sort(reverse=True) flag = 0 for i in lt: if i >= b: flag = 1 print("YES") break b = 2 * (b - i) if flag == 0: print("NO")
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 LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
from itertools import groupby T = int(input()) for i in range(T): n, h = tuple(map(int, input().split())) s = input() cluster = groupby(s) st = [] for key, value in cluster: st.append((key, len(list(value)))) ok = False for i in st: key, value = i if key == "1": continue if int(value) >= h: ok = True break if int(value) > h // 2: h = 2 * (h - int(value)) if ok: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR STRING IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
for _ in range(int(input())): l, h = map(int, input().split()) s = input() j = 0 x = 0 s1 = s.split("1") if s.count("0") >= h: while j < len(s1): x = len(s1[j]) if x != 0: if x >= h // 2: h = 2 * (h - x) if h <= 0: break j += 1 if h <= 0: print("YES") else: print("NO") else: print("NO")
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 ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR STRING VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
try: t = int(input()) for i in range(t): outcomes = ["NO", "YES"] n, h = map(int, input().split()) flag = False dayString = input().split("1") for i in dayString: if len(i) < h: h = min(h, 2 * (h - len(i))) else: flag = True break print(outcomes[flag]) except: pass
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST STRING STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
def cons_count(stri): n_s = [] i = 0 while i < len(stri): if stri[i] == "0": c = 1 j = i + 1 while j < len(stri): if stri[i] == stri[j]: c += 1 else: break j += 1 n_s.append(c) i += c continue i += 1 return n_s def mainfunc(stri, l, h): n_s = cons_count(stri) k = 0 while k < len(n_s) and h: if h <= n_s[k]: return "YES" else: temp = 2 * (h - n_s[k]) if temp < h: h = temp k += 1 return "NO" for _ in range(int(input())): l, h = map(int, input().split()) stri = input() print(mainfunc(stri, l, h))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR RETURN STRING ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN STRING 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
def binarySearch(arr, l, r, x): ans = -1 while l <= r: mid = l + (r - l) // 2 if arr[mid] >= x: ans = mid r = mid - 1 else: l = mid + 1 return ans def continous_zero(s): lst = [] c = 0 for i in range(len(s) - 1): if s[i] == "0": if s[i] == s[i + 1]: c += 1 if i == len(s) - 2: lst.append(c + 1) else: lst.append(c + 1) c = 0 lst.sort() return lst def check_slept(lst, l, h): while h > lst[-1]: h = 2 * (h - lst[-1]) lst.remove(lst[-1]) if len(lst) == 0: return "NO" else: continue else: return "YES" for i in range(int(input())): l, h = map(int, input().split()) s = input() lst = continous_zero(s) if len(lst) != 0: print(check_slept(lst, l, h)) elif h == 1: c = 0 for j in s: if j == "0": c += 1 if c != 0: print("YES") else: print("NO") else: print("NO")
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING IF VAR VAR VAR BIN_OP 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 VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF WHILE VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN STRING RETURN STRING 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 VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
t = int(input()) for i in range(t): l, h = map(int, input().split()) s = input() flag = False zeroes = s.split("1") count = [] for e in zeroes: count.append(len(e)) for val in count: if 2 * (h - val) <= h: h = 2 * (h - val) if h <= 0: flag = True if flag == True: print("YES") else: print("NO")
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 FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
T = int(input()) while T != 0: l, h = map(int, input().split()) s = input() arr = [] count = 0 for i in s: if i == "0": count += 1 else: arr.append(count) if count == 0 and len(arr) != 0: arr.pop() count = 0 if count != 0: arr.append(count) flag = False for free in arr: if free > h // 2: h = 2 * (h - free) if h <= 0: flag = True break if flag: print("YES") else: print("NO") T -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
for tc in range(int(input())): l, h = map(int, input().split()) s = input().split("1") s = [(len(s[i]), -i) for i in range(len(s))] s.sort() i0 = -1 while len(s) > 0 and h > 0: x, i = s.pop() while len(s) > 0 and -i < i0: x, i = s.pop() h = 2 * (h - x) i0 = -i print("NO" if h > 0 else "YES")
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 FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
from sys import stdin for __ in range(int(stdin.readline())): l, h = map(int, stdin.readline().split()) s = stdin.readline().strip() cnt = 0 for i in s: if i == "0": cnt += 1 else: if 2 * (h - cnt) < h: h = 2 * (h - cnt) cnt = 0 if 2 * (h - cnt) < h: h = 2 * (h - cnt) print("YES") if h <= 0 else print("NO")
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 FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF BIN_OP NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER IF BIN_OP NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR EXPR VAR NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
t = int(input()) while t > 0: l, h = map(int, input().split()) s = input() units = [] c = 0 for i in range(l): if s[i] == "0": c += 1 else: units.append(c) c = 0 units.append(c) units = [i for i in units if i != 0] for i in range(len(units)): if units[i] > h // 2: h = 2 * (h - units[i]) if h <= 0: break if h <= 0: print("YES") else: print("NO") t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
for _ in range(int(input())): L, H = map(int, input().split()) S = input() oc = 0 z = [] for ele in S: if ele == "0": oc += 1 elif oc != 0: z.append(oc) oc = 0 if oc != 0: z.append(oc) for ele in z: if ele > H / 2: H = 2 * (H - ele) if H <= 0: break if H <= 0: print("YES") else: print("NO")
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 ASSIGN VAR LIST FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
t = int(input()) for i in range(0, t): l, h = map(int, input().split()) s = list(input()) aa = [] cau = False a = 0 for j in range(0, len(s)): if s[j] == "0" and not cau: cau = True a = 1 elif s[j] == "0" and cau: a += 1 if s[j] == "1": if cau and a > 0: aa.append(a) cau = False a = 0 if j == len(s) - 1: if a > 0: aa.append(a) left = h for j in range(0, len(aa)): if left >= 2 * (left - aa[j]): left = 2 * (left - aa[j]) if left <= 0: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER IF VAR VAR STRING IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
for _ in range(int(input())): l, h = map(int, input().split()) a = list(input()) i = 1 b = [] s = 0 if "0" not in a: print("NO") continue if a[0] == "0": s = 1 while i < l: if a[i] == "0" and a[i - 1] == "0": s += 1 elif a[i] == "0": s = 1 elif a[i] == "1" and a[i - 1] != "1": b.append(s) i += 1 if a[-1] == "0": b.append(s) for i in b: if i <= h and i != 0: h = min(2 * (h - i), h) else: print("YES") h = 0 break if h == 0: print("YES") break if h > 0: print("NO")
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 LIST ASSIGN VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER STRING ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
for q in range(int(input())): l, h = map(int, input().split()) s = input() c = 0 for i in s: if int(i) == 0: c += 1 else: if 2 * c > h: h = 2 * (h - c) c = 0 if h - c <= 0: break if h - c <= 0: print("YES") else: print("NO")
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 IF FUNC_CALL VAR VAR NUMBER VAR NUMBER IF BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
t = int(input()) for _ in range(t): n, h = map(int, input().split()) s = input() arr = [] count = 0 for i in range(n): if s[i] == "0": count = count + 1 else: if count != 0: arr.append(count) count = 0 if count != 0: arr.append(count) ans = 0 for num in arr: if num >= h: ans = 1 break if 2 * num > h: h = 2 * (h - num) if ans == 1: print("YES") else: print("NO")
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 LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
t = int(input()) for _ in range(t): L, h = map(int, input().split()) s = input() l = [] a = [] c = 0 for i in range(L): if s[i] == "0" and i != L - 1: c = c + 1 elif s[i] == "0" and i == L - 1: c = c + 1 a.append(c) elif i == L - 1 or s[i] == "1": a.append(c) c = 0 d = 0 for j in range(len(a)): p = 2 * (h - a[j]) if p >= h: j = j + 1 else: h = p if h <= 0: d = 1 print("YES") break if d == 0: print("NO")
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 LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
for _ in range(int(input())): l, h = map(int, input().split()) s = input() s = s.split("1") l = [len(i) for i in s if len(i) != 0] ans = "NO" for i in l: if i >= h: ans = "YES" break elif i > h // 2: h = 2 * (h - i) else: continue 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 FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR ASSIGN VAR STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
for _ in range(int(input())): l, h = map(int, input().split()) s = input()[:l] lst = s.split("1") flag = True zeros = [] for i in lst: zeros.append(len(i)) for i in zeros: if i >= h: print("YES") flag = False break elif i > h // 2: h = 2 * (h - i) else: continue if flag: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
for _ in range(int(input())): _, h = map(int, input().split()) c = input() cnt = 0 for i in c: if i == "0": cnt += 1 if i == "1" and cnt: rh = 2 * (h - cnt) h = rh if rh < h else h cnt = 0 if cnt >= h or h <= 0: h = 0 break print("YES" if not h else "NO")
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 IF VAR STRING VAR NUMBER IF VAR STRING VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
for _ in range(int(input())): l, h = map(int, input().split()) s = input() a = s.split("1") a = " ".join(a).split() flag = 0 if l == s.count("0"): print("YES") elif len(a) == 0: print("NO") else: for i in a: b = 2 * (h - len(i)) if b <= h: h = b if h <= 0: flag = 1 break if flag == 0: print("NO") else: print("YES")
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 FUNC_CALL FUNC_CALL STRING VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
for i in range(int(input())): flag = False l, h = [int(i) for i in input().split()] s = input() s = s.split("1") f = [] s = sorted(s, reverse=True) for i in s: f.append(len(i)) if 0 in f: f.remove(0) if f != None: for j in f: if j >= h: flag = True break else: h = 2 * (h - j) if flag: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN 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 LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR NONE FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
for h in range(int(input())): n, k = [int(x) for x in input().split()] string = [int(x) for x in input()] zeroes = [] index = 0 for i in range(len(string)): j = i if j >= index: while string[j] == 0: j += 1 if j == len(string): break index = j if j - i > 0: zeroes.append(j - i) required = k printed = False for i in range(len(zeroes)): if zeroes[i] >= required: print("YES") printed = True break elif zeroes[i] >= required // 2: required = (required - zeroes[i]) * 2 if not printed: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR WHILE VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
for _ in range(int(input())): l, h = list(map(int, input().split())) s = input().split("1") a = [] for i in s: a += [len(i)] a.sort(reverse=True) for i in a: if i == 0 or h <= 0: break h = (h - i) * 2 if h > 0: print("NO") else: print("YES")
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 FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR VAR LIST FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
try: for _ in range(int(input())): A = [int(x) for x in input().split()] s = input() arr = [] i, p = 0, 0 while i < len(s): if s[i] == "0": p += 1 if s[i] == "1": if p != 0: arr.append(p) p = 0 i += 1 if p != 0: arr.append(p) for j in arr: x = 2 * (A[1] - j) if x < A[1]: A[1] = x if A[1] <= 0: print("YES") else: print("NO") except: pass
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
t = int(input()) for _ in range(0, t): inputs = [int(num) for num in input().split()] l = inputs[0] h = inputs[1] s = input() streak = 0 flag = 0 for i in range(0, len(s)): if s[i] == "0": streak += 1 elif s[i] != "0": if streak > h // 2: h = (h - streak) * 2 if h <= 0: flag = 1 break streak = 0 if i == len(s) - 1: if streak > h // 2: h = (h - streak) * 2 if h <= 0: flag = 1 break if flag == 1: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
T = int(input()) l2 = [] for i in range(1, T + 1): L, H = map(int, input().split()) S = input()[:L] count = 0 l = [] for i in range(L): if S[i] == "0": count += 1 elif S[i] == "1": if count != 0: l.append(count) count = 0 if count != 0: l.append(count) for i in range(len(l)): if l[i] > H: H = 0 break elif 2 * (H - l[i]) > H: continue else: H = 2 * (H - l[i]) if H == 0: l2.append("YES") else: l2.append("NO") for items in l2: print(items)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
t = int(input()) for i in range(t): l, h = map(int, input().split()) s = input() l1 = s.split("1") l2 = [] for j in l1: if j != "": l2.append(len(j)) if l2 == []: print("NO") else: for k in l2: if k >= h: print("YES") break elif k > h // 2: h = 2 * (h - k) else: print("NO")
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 FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR LIST EXPR FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
t = int(input()) for i in range(0, t): p = input().split(" ") l = int(p[0]) h = int(p[1]) s = input() s = s.split("1") ml = [] for j in s: if j != "": ml.append(j) if len(ml) == 0: print("NO") else: d = dict() for var in ml: d[var] = len(var) d = sorted(d.items(), key=lambda x: x[1], reverse=True) flag = 0 for pp in d: current = pp[1] if current >= h: flag = 1 print("YES") break else: h = 2 * (h - current) if flag == 0: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR 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 ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
T = int(input()) for _ in range(T): l, h = map(int, input().split()) s = input() cont_free = [] for i in s: if len(cont_free) == 0: cont_free.append([i, 1]) elif cont_free[-1][0] == i: cont_free[-1][1] += 1 else: cont_free.append([i, 1]) for char, count in cont_free: if char == "0" and count > h // 2: h = 2 * (h - count) if h <= 0: print("YES") else: print("NO")
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 LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER FOR VAR VAR VAR IF VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
for _ in range(int(input())): a, b = map(int, input().split()) s = input() l = [] i = 0 while i < a: if s[i] == "0": n = 0 while i < a and s[i] == "0": n += 1 i += 1 l.append(n) i += 1 for i in l: if 2 * (b - i) < b: b = 2 * (b - i) if b <= 0: break if b <= 0: print("YES") else: print("NO")
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 LIST ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR IF BIN_OP NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
t = int(input()) for _ in range(t): l, h = map(int, input().split()) L = list(input()) M = [0] a = 0 for i in range(l): if L[i] == "0": a += 1 elif a == 0: pass else: M.append(a) a = 0 if L[-1] == "0": M.append(a) M.sort(reverse=True) i = h j = 0 found = False while j < len(M): y = M[j] if y >= i: found = True break else: i = 2 * (i - y) j += 1 if found: print("YES") else: print("NO")
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 LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
def solve(l, h, s): t = [] z = 0 le = 0 for i in range(l): if s[i] == "0": z += 1 else: if z > 0: t.append(z) le += 1 z = 0 if z > 0: t.append(z) le += 1 t.sort() while le > 0: temp = t.pop() h = 2 * (h - temp) le -= 1 if h < 1: return "YES" return "NO" for _ in range(int(input())): l, h = map(int, input().split()) s = input() print(solve(l, h, s))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER RETURN STRING RETURN STRING 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
def compute(S, L, H): temp = S.split("1") zeroes = [] for each in temp: if len(each) > 0: zeroes.append(len(each)) for zero in zeroes: if 2 * (H - zero) < H: H = 2 * (H - zero) if H <= 0: break return H <= 0 t = int(input()) for i in range(t): l, h = [int(x) for x in input().split()] s = input() ans = compute(s, l, h) if ans: print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
t = int(input()) while t: l, h = [int(x) for x in input().split()] s = input() s += "1" sum = 0 a = [] for i in s: if i == "0": sum += 1 elif sum > 0: a.append(sum) sum = 0 for i in a: if i >= h: print("YES") sum = 1 break elif 2 * (h - i) <= h: h = 2 * (h - i) if sum == 0: print("NO") t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF BIN_OP NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
for _ in range(int(input())): l, h = map(int, input().split()) string = input() dp = [0] * l dp[0] = int(not int(string[0])) for i in range(1, l): a = string[i] b = string[i - 1] if a == "0" and b == "0": dp[i] = dp[i - 1] + 1 elif a == "1" and b == "1": dp[i] = dp[i - 1] elif a == "1" and b == "0": dp[i] = max(0, h - 2 * (h - dp[i - 1])) elif a == "0" and b == "1": dp[i] = dp[i - 1] + 1 if max(dp) >= h: print("YES") else: print("NO")
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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR STRING VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR STRING VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING VAR STRING ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR STRING VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
try: for _ in range(int(input())): total, s = [int(x) for x in input().split()] if total < s: print("NO") continue if s == 0: print("YES") continue val = input() if total == s: if "1" in val: print("NO") else: print("YES") continue c = 0 l = [] for v in val: if v == "1" and c != 0: l.append(c) c = 0 elif v == "0": c += 1 if c > 0: l.append(c) l.sort(reverse=True) start = s length = len(l) i = 0 while i != length and start != 0: if start <= l[i]: start = 0 break if start > l[i]: rem = start - l[i] l[i] = 0 if rem == 0: start = rem break elif rem != 0: s = 2 * rem start = s i += 1 if start == 0: print("YES") else: print("NO") except: pass
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR IF VAR VAR IF STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has been studying the human sleep cycle for years. He divides a day into $L$ units of time (numbered $1$ through $L$). Chef's research shows that his body requires spending only $H$ continuous units of time per day on sleep — then, he can stay active during work. However, in case of travel or any other responsibilities, he might not be able to achieve this ideal state. Next, Chef noticed that if he sleeps for $x$ ($x < H$) continuous units of time and then performs some other activity, he can still meet his daily sleep quota, but the remaining time he needs to spend on continuously sleeping becomes $2\cdot(H-x)$. He can further split this sleeping time in the same way, i.e. if he now sleeps for $y$ more ($y < 2\cdot(H-x)$) continuous units of time, his required sleeping time becomes $2\cdot(2\cdot(H-x)-y)$, and so on. You are given a string $S$ with length $L$; for each valid $i$, the $i$-th character of this string is '0' if the $i$-th unit of time is free (so Chef can use it to sleep) or '1' if it is occupied. Can Chef ever get enough sleep to feel fresh during his work, given that he can choose when to sleep in his free time? ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $L$ and $H$. The second line contains a single string $S$ with length $L$. ------ Output ------ Print a single line containing the string "YES" if Chef can have the required amount of sleep or "NO" if he cannot (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ H ≤ L ≤ 10^{5}$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 4 3 1 111 3 1 101 7 5 0000100 6 5 000010 ----- Sample Output 1 ------ NO YES YES NO ----- explanation 1 ------ Example case 1: Chef needs only $1$ unit of sleep, but since he is busy all the time, he cannot sleep at all. Example case 2: Chef needs only $1$ unit of sleep and he has exactly one unit of time when he is free, so he can sleep during that unit of time. Example case 3: Chef needs $5$ continuous units of sleep. First, he spends the first $4$ free units of time, and then he needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, which he has available as the last $2$ free units of time. Example case 4: Chef needs $5$ units of sleep. If he spends the first $4$ units of time, he still needs $2 \cdot (5 - 4) = 2$ continuous units of sleep, but only a single unit of time is left now, so he is unable to get enough sleep.
def get_next_required_sleep(current_required_sleep, start_idle, end_idle): sleep_duration = end_idle - start_idle + 1 next_required_sleep = ( 0 if sleep_duration >= current_required_sleep else min(current_required_sleep, (current_required_sleep - sleep_duration) * 2) ) return next_required_sleep n_testcase = int(input()) for testcase in range(n_testcase): day_length, required_sleep = (int(x) for x in input().split()) works = input() start_idle = day_length for i, work in enumerate(works): if work == "0": start_idle = min(start_idle, i) if work == "1": if start_idle < day_length: end_idle = i - 1 required_sleep = get_next_required_sleep( required_sleep, start_idle, end_idle ) start_idle = day_length if start_idle < day_length: end_idle = day_length - 1 required_sleep = get_next_required_sleep(required_sleep, start_idle, end_idle) print("YES" if required_sleep == 0 else "NO")
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING STRING
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
t = int(input()) l1 = [0] * 100000 l2 = [0] * 100000 c = 0 for i in range(0, t): n = int(input()) c = 0 l = list(map(int, input().split())) l1 = l[:] l2 = l[:] for j in range(0, n): if l[j] == l1[j]: for k in range(0, n): if l[k] != l1[j]: if l1[k] != l1[j]: temp = l1[k] l1[k] = l1[j] l1[j] = temp break for j in range(0, n): if l1[j] == l2[j]: c = c + 1 print(n - c) for j in range(0, n): if j != n - 1: print(str(l1[j]) + " ", end="") else: print(str(l1[j]), end="") if not i == t - 1: print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR STRING STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
from itertools import permutations from sys import stdin for _ in range(int(stdin.readline())): n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) h = 0 b = a.copy() if n <= 4: b = a.copy() for p in list(permutations(a)): temph = 0 for i in range(n): if a[i] != p[i]: temph += 1 if temph > h: h = temph b = p else: h = n while True: for i in range(n): if a[i] == b[i]: b.append(b.pop(0)) break else: break print(h) print(*b)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
for _ in range(int(input())): ans = [] n = int(input()) l = [int(x) for x in input().rstrip().split()] if n == 1: print(0) print(l[0]) continue elif n == 2: if l[0] == l[1]: print(0) ans = l else: print(2) ans = [l[1], l[0]] for i in range(n): print(ans[i], end=" ") print() continue elif n == 3: if l[0] != l[1] and l[1] != l[2] and l[0] != l[2]: ans = [l[1], l[2], l[0]] print(3) else: print(2) if l[2] == l[0]: ans = [l[1], l[0], l[2]] else: ans = [l[2], l[0], l[1]] for i in range(n): print(ans[i], end=" ") print() continue else: print(n) ans = [-999] * n rem = n % 4 nn = n - rem for i in range(0, nn, 4): a = l[i] b = l[i + 1] c = l[i + 2] d = l[i + 3] if a == c or b == d: ans[i] = b ans[i + 1] = a ans[i + 2] = d ans[i + 3] = c continue else: ans[i] = c ans[i + 1] = d ans[i + 2] = a ans[i + 3] = b continue if rem == 1: for jij in range(0, 4): if l[jij] != l[-1] and ans[jij] != l[-1]: ans[-1] = ans[jij] ans[jij] = l[-1] break elif rem == 2: lasta = l[-2] lastb = l[-1] if lasta == lastb: ans[-2] = ans[0] ans[-1] = ans[1] ans[0] = lasta ans[1] = lastb else: ans[-1] = l[-2] ans[-2] = l[-1] elif rem == 3: a = l[-3] b = l[-2] c = l[-1] if a == b: ans[-3] = l[-1] ans[-1] = l[-3] loc = -2 for jij in range(0, 4): if l[jij] != l[loc] and ans[jij] != l[loc]: ans[loc] = ans[jij] ans[jij] = l[loc] break elif b == c: ans[-3] = l[-2] ans[-2] = l[-3] loc = -1 for jij in range(0, 5): if l[jij] != l[loc] and ans[jij] != l[loc]: ans[loc] = ans[jij] ans[jij] = l[loc] break elif a == c: ans[-3] = l[-2] ans[-2] = l[-3] loc = -1 for jij in range(0, 5): if l[jij] != l[loc] and ans[jij] != l[loc]: ans[loc] = ans[jij] ans[jij] = l[loc] break else: ans[-1] = a ans[-2] = c ans[-3] = b for i in range(2): if l[i] == ans[i]: ans[i + 2], ans[i] = ans[i], ans[i + 2] for i in range(2, n): if l[i] == ans[i]: ans[i - 2], ans[i] = ans[i], ans[i - 2] for i in range(n): print(ans[i], end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) d = {} b = [0] * n for i in range(n): d[a[i]] = d.get(a[i], []) + [i] if n == 2: b[0] = a[1] b[1] = a[0] elif n == 3: p = a[0] b[0] = a[1] b[1] = a[2] b[2] = p elif n == 1: b[0] = a[0] k = 1 else: d1 = {} d2 = {} for i in d: if len(d[i]) == 1: d1[i] = d[i] else: d2[i] = d[i] ld1 = list(d1.keys()) ld2 = list(d2.keys()) if len(d1) > 1 and len(d2) > 1: p = d1[ld1[0]][0] for i in range(1, len(ld1)): d1[ld1[i - 1]][0] = d1[ld1[i]][0] d1[ld1[i]][0] = p p = d2[ld2[0]] for i in range(1, len(ld2)): d2[ld2[i - 1]] = d2[ld2[i]] d2[ld2[i]] = p elif len(d1) == 0: p = d2[ld2[0]] for i in range(1, len(ld2)): d2[ld2[i - 1]] = d2[ld2[i]] d2[ld2[i]] = p elif len(d2) == 0: p = d1[ld1[0]][0] for i in range(1, len(ld1)): d1[ld1[i - 1]][0] = d1[ld1[i]][0] d1[ld1[i]][0] = p elif len(d1) == 1: p = d2[ld2[0]] for i in range(1, len(ld2)): d2[ld2[i - 1]] = d2[ld2[i]] d2[ld2[i]] = p p = d1[ld1[0]][0] d1[ld1[0]][0] = d2[ld2[0]][0] d2[ld2[0]][0] = p elif len(d2) == 1: p = d1[ld1[0]][0] for i in range(1, len(ld1)): d1[ld1[i - 1]][0] = d1[ld1[i]][0] d1[ld1[i]][0] = p x, y = d2[ld2[0]][0], d2[ld2[0]][1] d2[ld2[0]][0] = d1[ld1[0]][0] d2[ld2[0]][1] = d1[ld1[1]][0] d1[ld1[1]][0] = x d1[ld1[0]][0] = y for i in d1: b[d1[i][0]] = i for i in d2: b[d2[i][0]] = i b[d2[i][1]] = i c = 0 for i in range(n): if b[i] != a[i]: c += 1 print(c) print(*b)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR LIST LIST VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
for TestCase in range(int(input())): Size = int(input()) L = list(map(int, input().split())) LCopy = list(L) e = [] if Size == 1: print(0) print(*L) else: for i in range(Size): L[i] = str(LCopy[i - 1]) if str(L[i]) == str(LCopy[i]): e.append(i) f = list(L) if len(e) <= 0: print(Size) print(*L) elif len(e) == 1: if Size == 3: print(2) print(*L) else: L[e[0]], L[e[0] - 2] = L[e[0] - 2], L[e[0]] print(Size) print(*L) elif Size == 2: print(0) print(*LCopy) else: for i in range(len(e)): L[e[i]] = int(f[e[i - 1]]) print(Size) print(*L)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
for _ in range(int(input())): no = input() no = int(no) arr = map(int, input().split()) arr = list(arr) st = 0 if no == 1 and st == 0: print(no - 1) ansarr = [] ansarr.append(arr[0]) print(*ansarr) st = 1 elif no == 2 and st == 0: if arr[1] == arr[0]: print(no - 2) ansarr = [] ansarr.append(arr[0]) ansarr.append(arr[1]) print(*ansarr) st = 1 else: print(no) ansarr = [] ansarr.append(arr[1]) ansarr.append(arr[0]) print(*ansarr) st = 1 elif no == 3 and st == 0: if arr[0] == arr[2] or arr[0] == arr[1] or arr[2] == arr[1]: print("2") ansarr = [] ansarr.append(arr[2]) ansarr.append(arr[0]) ansarr.append(arr[1]) print(*ansarr) st = 1 else: print("3") ansarr = [] ansarr.append(arr[1]) ansarr.append(arr[2]) ansarr.append(arr[0]) print(*ansarr) st = 1 elif no == 4 and st == 0: print("4") ansarr = [] for i in range(no): ansarr.append(-5) if len(set(arr)) == no: ansarr = arr[::-1] print(*ansarr) st = 1 else: ansarr = [] for i in range(no): ansarr.append(-5) if arr[2] == arr[3]: ansarr[0], ansarr[1], ansarr[2], ansarr[3] = ( arr[3], arr[2], arr[1], arr[0], ) print(*ansarr) st = 1 else: temp2 = [] for i in range(4): temp2.append(arr[i]) poin = 0 element = temp2[poin] while poin != no: element = temp2[poin] for j in range(no): if arr[j] != element and ansarr[j] == -5: ansarr[j] = element poin += 1 break print(*ansarr) st = 1 else: print(no) ansarr = [] for i in range(no): ansarr.append(-5) rep = [] temp = arr[no - 1] for j in range(no - 1): ansarr[j + 1] = arr[j] if arr[j] == arr[j + 1]: rep.append(j + 1) if arr[0] == arr[no - 1]: rep.append(0) ansarr[0] = temp repc = len(rep) if repc > 0: if repc == 1: for x in range(no): if rep[0] != x: temp = x break if rep[0] == no - 1: temp += 1 ansarr[temp], ansarr[rep[0]] = ansarr[rep[0]], ansarr[temp] elif len(rep) == 2: ansarr[rep[1]], ansarr[rep[0]] = ansarr[rep[0]], ansarr[rep[1]] else: for i in range(len(rep) - 1): h2 = rep[i + 1] h1 = rep[i] ansarr[h1], ansarr[h2] = ansarr[h2], ansarr[h1] print(*ansarr)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
t = int(input()) while t: t -= 1 n = int(input()) a = list(map(int, input().split())) if n == 1: print(0) print(*a) elif n == 2: if a[0] == a[1]: print(0) print(*a) else: print(n) print(*a[::-1]) elif n == 3: if a[0] == a[1] or a[0] == a[2] or a[1] == a[2]: print(n - 1) temp = a[n - 1] for i in range(n - 1, 0, -1): a[i] = a[i - 1] a[0] = temp print(*a) else: print(n) temp = a[0] a[0] = a[1] a[1] = a[2] a[2] = temp print(*a) elif n == 4: print(n) b = [-1] * n c = 0 if len(set(a)) == 4: print(*a[::-1]) else: b = [-1] * n if a[3] == a[2]: b[0] = a[3] b[1] = a[2] b[2] = a[1] b[3] = a[0] print(*b) else: arr2 = [-1] * 4 arr2[0] = a[0] arr2[1] = a[1] arr2[2] = a[2] arr2[3] = a[3] c = 0 ele = arr2[c] while c != n: ele = arr2[c] for i in range(n): if a[i] != ele and b[i] == -1: b[i] = ele c += 1 break print(*b) else: print(n) b = [-1] * n c = 0 che = [] temp = a[n - 1] for i in range(n - 1): b[i + 1] = a[i] if a[i + 1] == a[i]: che.append(i + 1) c += 1 if a[n - 1] == a[0]: che.append(0) c += 1 b[0] = temp if c > 0: if c == 1: for u in range(n): if u != che[0]: temp = u break if che[0] == n - 1: temp += 1 b[che[0]], b[temp] = b[temp], b[che[0]] elif c == 2: b[che[0]], b[che[1]] = b[che[1]], b[che[0]] else: for i in range(c - 1): no = che[i] no1 = che[i + 1] b[no], b[no1] = b[no1], b[no] print(*b)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
t = int(input()) for test in range(t): n = int(input()) arr = list(map(int, input().split())) indices = [] sortArr = [] hamDist = 0 for i in sorted(enumerate(arr), key=lambda x: x[1]): indices.append(i[0]) sortArr.append(i[1]) pointer = 0 i = 0 while pointer < n: if arr[indices[i]] == sortArr[pointer]: pointer += 1 else: arr[indices[i]] = sortArr[pointer] hamDist += 1 pointer += 1 i += 1 pointer = 0 for j in range(i, n): if arr[indices[j]] != sortArr[pointer]: hamDist += 1 arr[indices[j]] = sortArr[pointer] pointer += 1 print(hamDist) print(*arr)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
def count(a, b, n): c = 0 for i in range(n): if a[i] != b[i]: c += 1 return c t = int(input()) for e in range(t): n = int(input()) a = list(map(int, input().split())) if n == 1: print(0) print(a[0]) continue dic = dict() s, d = [], [] for i in range(n): if a[i] in dic: d.append([dic[a[i]], i]) del dic[a[i]] else: dic[a[i]] = i for i in dic: s.append(dic[i]) if len(d) == 0: print(n) for i in range(1, n): print(a[i], end=" ") print(a[0]) else: b = list(a) x, y = d[0][0], d[0][1] for i in range(1, len(d)): e, f, p, q = d[i - 1][0], d[i - 1][1], d[i][0], d[i][1] b[e], b[f] = a[p], a[q] l = len(d) - 1 i, j = d[l][0], d[l][1] b[i], b[j] = a[x], a[y] l = len(s) if l == 1 and len(d) > 1: i, j = s[0], d[0][0] b[i], b[j] = b[j], b[i] if l > 1: for i in range(1, l): b[s[i - 1]] = a[s[i]] b[s[l - 1]] = a[s[0]] if len(d) == 1 and l > 0: i, j = d[0][0], s[0] b[i], b[j] = b[j], b[i] if l > 1: i, j = d[0][1], s[1] b[i], b[j] = b[j], b[i] print(count(a, b, n)) for i in range(n): print(b[i], end=" ") print()
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
test = int(input()) while test > 0: n = int(input()) new = [(0) for i in range(n)] array = [int(a) for a in input().split()] i = 0 if n == 1: print(0) print(array[0]) test = test - 1 continue while i < n: if array[i] == array[(i + 1) % n]: break else: i = i + 1 i = i % n y = i status = 0 dist = 0 while True: if i >= n: i = i % n status = 1 if status and i == y: break if array[i] != array[(i + 1) % n]: if new[(i + 1) % n]: if array[(i + 2) % n] == array[i]: new[(i + 2) % n] = array[(i + 1) % n] new[(i + 3) % n] = array[i] if (i + 3) % n == i: dist = dist + 1 i = i + 1 else: new[(i + 2) % n] = array[i] if array[i] == array[(i + 2) % n]: dist = dist + 1 else: new[(i + 1) % n] = array[i] else: new[(i + 2) % n] = array[i] if array[i] == array[(i + 2) % n]: dist = dist + 1 i = i + 1 print(n - dist) for j in new: print(j, end=" ") print("") test = test - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
t = int(input()) for i in range(t): n = int(input()) l = list(map(int, input().split())) a = [] b = [] for j in range(n): a.append([l[j], j]) b.append(l[j]) a.sort() b.sort(reverse=True) f = False for k in range(n): if b[k] == a[k][0]: if f == False: b[k], b[0] = b[0], b[k] f = True else: b[k], b[n - 1] = b[n - 1], b[k] c = 0 s = [0] * n for z in range(n): if b[z] != a[z][0]: c += 1 s[a[z][1]] = b[z] print(c) print(*s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
def swap(a, ind): a[ind], a[ind - 2] = a[ind - 2], a[ind] a[ind - 1], a[ind - 3] = a[ind - 3], a[ind - 1] t = int(input()) for _ in range(t): hd = 0 n = int(input()) ar = list(map(int, input().split())) i = n - 1 if n == 1: print(0) print(*ar) elif n == 2: if ar[0] == ar[1]: print(0) print(*ar) else: print(2) print(ar[1], ar[0]) elif len(ar) == len(set(ar)): print(n) print(*ar[1:n], ar[0]) elif n == 3: if ar[2] == ar[1] or ar[0] == ar[2]: ar[0], ar[1] = ar[1], ar[0] elif ar[1] == ar[0]: ar[1], ar[2] = ar[2], ar[1] print(2) print(*ar) else: tmp = ar[0] while i > 0: if ar[i] == ar[i - 1]: if i > 2: swap(ar, i) i = i - 4 hd = hd + 4 elif i == 2: ar[2], ar[n - 1] = ar[n - 1], ar[2] ar[1], ar[n - 2] = ar[n - 2], ar[1] ar[0], ar[n - 3] = ar[n - 3], ar[0] hd = hd + 3 i = 0 elif i == 1: ar[1], ar[n - 1] = ar[n - 1], ar[1] ar[0], ar[n - 2] = ar[n - 2], ar[0] hd = hd + 2 i = 0 elif ar[i] != ar[i - 1]: ar[i], ar[i - 1] = ar[i - 1], ar[i] hd = hd + 2 i = i - 2 if ar[0] == tmp: ar[0], ar[n - 1] = ar[n - 1], ar[0] hd += 1 print(n) print(*ar)
FUNC_DEF ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
t = int(input().strip()) pos = {} d = {} arr = [] swap = [] def swap1(x, y): arr[pos[x][0]], arr[pos[y][0]] = arr[pos[y][0]], arr[pos[x][0]] arr[pos[x][1]], arr[pos[y][1]] = arr[pos[y][1]], arr[pos[x][1]] swap[pos[x][0]] = 1 swap[pos[y][0]] = 1 swap[pos[x][1]] = 1 swap[pos[y][1]] = 1 def swap2(x, y, z): arr[pos[x][0]], arr[pos[y][0]] = arr[pos[y][0]], arr[pos[x][0]] arr[pos[x][1]], arr[pos[z][0]] = arr[pos[z][0]], arr[pos[x][1]] swap[pos[x][0]] = 1 swap[pos[y][0]] = 1 swap[pos[x][1]] = 1 swap[pos[z][0]] = 1 def swap3(x, y): arr[x], arr[y] = arr[y], arr[x] swap[x] = 1 swap[y] = 1 for _ in range(t): length = int(input().strip()) arr = list(map(int, input().strip().split())) c = 0 flag = False pos = {} d = {} swap = [0] * length single = [] pair = [] reserve = -1 for i in arr: try: d[i] += 1 pos[i].append(c) except: d[i] = 1 pos[i] = [c] c += 1 for i in list(set(arr)): if d[i] > 1: pair.append(i) else: single.append(i) l1 = len(pair) i = 0 while i < l1 // 2: swap1(pair[2 * i], pair[2 * i + 1]) if i == 0: reserve = pair[0] i += 1 j = 0 l2 = len(single) if l1 % 2 != 0 and l2 >= 2: swap2(pair[2 * i], single[2 * j], single[2 * j + 1]) single = single[2:] l2 -= 2 elif l1 % 2 != 0 and l2 == 1: swap3(pos[pair[2 * i]][0], pos[single[2 * j]][0]) single = single[1:] l2 -= 1 if reserve != -1: swap3(pos[pair[2 * i]][1], pos[reserve][0]) elif l1 % 2 != 0 and l2 == 0 and reserve != -1: swap1(pair[2 * i], reserve) while j < l2 // 2: if reserve == -1 and j == 0: reserve = single[0] flag = True swap3(pos[single[2 * j]][0], pos[single[2 * j + 1]][0]) j += 1 if l2 % 2 != 0 and reserve != -1: if flag: swap3(pos[single[2 * j]][0], pos[reserve][0]) else: swap3(pos[single[2 * j]][0], pos[reserve][1]) hamd = swap.count(0) print(length - hamd) for i in arr: print(i, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR LIST VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR WHILE VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
t = int(input()) while t: n = int(input()) ct = 0 has = [] has1 = [] has2 = [] has3 = [] b = [] a = list(map(int, input().split())) for i in range(0, n): b.append(a[i]) for i in range(0, 100010): has.append(0) has3.append(0) for i in range(0, n): has[a[i]] += 1 s = set(a) sz = len(s) s = list(s) for i in range(0, sz): if has[s[i]] == 2: has2.append(s[i]) else: has1.append(s[i]) s1 = len(has1) s2 = len(has2) for i in range(0, s2 - 1, 2): has3[has2[i]] = has2[i + 1] has3[has2[i + 1]] = has2[i] for i in range(0, s1 - 1, 2): has3[has1[i]] = has1[i + 1] has3[has1[i + 1]] = has1[i] for i in range(0, n): if has3[b[i]] != 0: b[i] = has3[b[i]] for i in range(0, n, 1): if a[i] == b[i]: if i + 1 < n and b[i] != a[i + 1]: b[i], b[i + 1] = b[i + 1], b[i] elif i - 1 >= 0 and b[i] != a[i - 1]: b[i], b[i - 1] = b[i - 1], b[i] elif i == 0 or i == n - 1: b[0], b[n - 1] = b[n - 1], b[0] for i in range(0, n): if a[i] != b[i]: ct += 1 print(ct) print(*b) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
t = int(input()) for _ in range(t): n = int(input()) st = input() s = st.split() a = [0] * n if n == 1: print(0) print(st) elif n == 2: if s[0] == s[1]: print(0) for i in s: print(i, end=" ") print() else: print(2) for i in s[::-1]: print(i, end=" ") print() elif n == 3: if s[0] == s[1] or s[1] == s[2] or s[0] == s[2]: print(2) else: print(3) r = n - 1 for _ in range(n): print(s[r], end=" ") if r == n - 1: r = 0 else: r += 1 print() elif n > 3: print(n) if st[: len(st) // 2] == st[: len(st) // 2 : -1]: p = [] r = n // 2 + 1 for _ in range(n): p.append(s[r]) if r == n - 1: r = 0 else: r += 1 m = p[n - 1] p[n - 1] = p[n - 2] p[n - 2] = m for j in p: print(j, end=" ") print() elif s[0] == s[n - 1]: r = n - 2 for _ in range(n): print(s[r], end=" ") if r == n - 1: r = 0 else: r += 1 print() else: for i in range(n): r = i + 1 for _ in range(n - 1): if r > n - 1: r = 0 if s[r] != s[i]: if a[r] == 0: a[r] = s[i] break r += 1 for i in a: print(i, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
while True: try: t = int(input()) except EOFError: break break for i in range(t): n = int(input()) a = list(map(int, input().split())) c = set(a) hd = 0 b = [] if n == len(c): if len(a) % 2 == 0: b = a[::-1] hd = len(b) elif len(a) > 1 and len(a) % 2 != 0: b = a[::-1] b[int(n / 2)], b[int(n / 2) + 1] = b[int(n / 2) + 1], b[int(n / 2)] hd = len(b) elif len(a) == 1: b = a hd = 0 print(hd) print(" ".join(str(x) for x in b)) else: hd = len(a) b = [x for x in a] for i in range(n - 2): if a[i] != a[i + 2]: a[i], a[i + 2] = a[i + 2], a[i] elif a[i] != a[i + 1]: a[i], a[i + 1] = a[i + 1], a[i] for i in range(n - 1): if a[i] == b[i]: if a[i + 1] != b[i]: a[i], a[i + 1] = a[i + 1], a[i] elif a[i - 1] != b[i] and i != 0: a[i], a[i - 1] = a[i - 1], a[i] if a[-1] == b[-1]: if a[-2] == b[-1] and len(a) > 3: a[-3], a[-1] = a[-1], a[-3] if a[-2] != b[-1]: a[-2], a[-1] = a[-1], a[-2] if a[0] == b[0] and a[1] == b[0] and len(a) > 3: a[0], a[2] = a[2], a[0] for i in range(n): if a[i] == b[i]: hd -= 1 print(hd) print(" ".join(str(x) for x in a))
WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
def ham(A, B, n, c): dist = 0 for i in range(n): if A[i] != B[i]: dist = dist + 1 print(dist) def shuffle(A, n): B = [None] * n T = list() T = sorted(A) flag = 0 for i in range(n): for j in range(i, n): if A[i] != T[j]: if i == j: break elif A[j] != T[i]: temp = T[j] T[j] = T[i] T[i] = temp flag = 1 break conflict = list() for i in range(n): if A[i] == T[i]: conflict.append(i) count = 0 for con in range(len(conflict)): c = conflict[con] for i in range(n): if T[i] != T[c] and A[i] != T[c] and A[c] != T[i]: temp = T[c] T[c] = T[i] T[i] = temp count = count + 1 break ham(A, T, n, count) for i in range(n): print(T[i], end=" ") print() Case = int(input()) for i in range(Case): n = int(input()) numbers = list(map(int, input().split())) if n != 1: shuffle(numbers, n) else: print(0) print(numbers[0])
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
for t in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(a) for i in range(n): if a[i] != b[i]: continue for j in range(n): if i == j: continue if a[i] != b[j] and a[j] != b[i]: b[i], b[j] = b[j], b[i] h = 0 for i in range(n): if a[i] != b[i]: h += 1 print(h) for each in b: print(each, end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
for t in range(int(input())): m = int(100000.0) n = int(input()) ar = list(map(int, input().split())) freq = [(0) for i in range(m)] idxs = [[] for i in range(m)] idx = 0 for a in ar: freq[a - 1] += 1 idxs[a - 1].append(idx) idx += 1 counts = [] for i in range(m): if freq[i] > 0: counts.append([freq[i], i]) counts.sort(reverse=True) j = 0 ans = 0 while j < len(counts) and len(counts) > 1: if j == len(counts) - 1: if counts[j][0] == 2: temp = idxs[counts[j][1]][:] idxs[counts[j][1]] = idxs[counts[0][1]] idxs[counts[0][1]] = temp ans += 2 j += 1 continue else: temp = idxs[counts[j][1]][0] idxs[counts[j][1]][0] = idxs[counts[0][1]][0] idxs[counts[0][1]][0] = temp j += 1 ans += 1 continue if counts[j][0] == 2 and counts[j + 1][0] == 2: temp = idxs[counts[j][1]][:] idxs[counts[j][1]] = idxs[counts[j + 1][1]] idxs[counts[j + 1][1]] = temp j += 2 ans += 4 continue if counts[j][0] == 2 and counts[j + 1][0] == 1: if j + 2 < len(counts): temp = idxs[counts[j][1]][:] idxs[counts[j][1]][0] = idxs[counts[j + 1][1]][0] idxs[counts[j][1]][1] = idxs[counts[j + 2][1]][0] idxs[counts[j + 1][1]][0] = temp[0] idxs[counts[j + 2][1]][0] = temp[1] j += 3 ans += 4 continue else: temp = idxs[counts[j][1]][:] idxs[counts[j][1]][0] = idxs[counts[j + 1][1]][0] idxs[counts[j][1]][1] = idxs[counts[0][1]][0] idxs[counts[j + 1][1]][0] = temp[0] idxs[counts[0][1]][0] = temp[1] j += 2 ans += 3 continue if counts[j][0] == 1: temp = idxs[counts[j][1]][:] idxs[counts[j][1]] = idxs[counts[j + 1][1]] idxs[counts[j + 1][1]] = temp j += 2 ans += 2 continue br = [(0) for i in range(n)] for i in range(m): idx = idxs[i] if len(idx) == 1: br[idx[0]] = i + 1 if len(idx) == 2: br[idx[0]] = i + 1 br[idx[1]] = i + 1 ans = 0 for a, b in zip(ar, br): if a != b: ans += 1 print(ans) print(" ".join(str(x) for x in br))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
t = int(input()) for i in range(t): n = int(input()) if n == 1: a = str(input()) print(0) print(a) else: a = [s for s in input().split()] p = set(a) if len(p) != len(a): if n == 3: b = [a[-1]] + a[:-1] print(2) print(" ".join(b)) elif n != 2: b = [a[-1]] + a[:-1] d = len(b) for k in range(d): if b[k] == a[k]: for m in range(d): if b[m] != b[k] and b[k] != a[m]: temp = b[k] b[k] = b[m] b[m] = temp break print(n) print(" ".join(b)) else: print(0) print(" ".join(a)) else: b = [a[-1]] + a[:-1] print(n) print(" ".join(b))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
def shift(seq, n=0): a = n % len(seq) return seq[-a:] + seq[:-a] n = int(input()) while n: n = n - 1 length = int(input()) l = [int(x) for x in input().split()] maxi = 0 ival = 0 for i in range(1, length): if maxi == length: break dp = shift(l, i) count = 0 for j in range(0, length): if dp[j] != l[j]: count = count + 1 if count > maxi: maxi = count ival = i print(maxi) ans = str(shift(l, ival)) print(ans.replace(",", "").replace("[", "").replace("]", ""))
FUNC_DEF NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING STRING STRING STRING STRING STRING
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
t = int(input()) arr_list = [] hd_list = [] for i in range(t): n = int(input()) inp_list = input().split() arr = "" hd = 0 swap = 0 if n == 1: hd_list.append(0) arr = inp_list[0] elif n == 2 and inp_list[0] == inp_list[1]: hd_list.append(0) arr = inp_list[0] + " " + inp_list[0] elif n == 2: hd_list.append(2) arr = inp_list[1] + " " + inp_list[0] elif n == 3 and inp_list[0] == inp_list[1]: hd_list.append(2) arr = inp_list[0] + " " + inp_list[-1] + " " + inp_list[0] elif n == 3 and inp_list[1] == inp_list[2]: hd_list.append(2) arr = inp_list[1] + " " + inp_list[0] + " " + inp_list[2] elif n == 3 and inp_list[0] == inp_list[2]: hd_list.append(2) arr = inp_list[0] + " " + inp_list[2] + " " + inp_list[1] elif n == 3: hd_list.append(3) arr = inp_list[2] + " " + inp_list[0] + " " + inp_list[1] else: hd_list.append(n) temp_list = inp_list.copy() j = 0 flag = 0 ctr = n while flag == 0: ctr -= 1 if ctr == -1: temp_list = inp_list.copy() ctr = n temp_list[1], temp_list[0] = temp_list[0], temp_list[1] temp_arr = temp_list[1:n] temp_arr.append(temp_list[0]) flag = 1 for j in range(n): if temp_arr[j] == inp_list[j]: temp_list = temp_arr flag = 0 break for j in range(n - 1): arr = arr + temp_arr[j] + " " arr += temp_arr[n - 1] arr_list.append(arr) for i in range(t): print(hd_list[i]) print(arr_list[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST 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 STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR STRING VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) f = [None] * 100001 s = [None, None] l = [None, None] cnt = 0 b = 0 for i, x in enumerate(a): if f[x] is not None: f[x] ^= 1 else: cnt += 1 if cnt == 3 and n > 3: b = 1 f[x] = b j = f[x] if s[j] is None: s[j] = x else: a[l[j]] = x l[j] = i for i in (0, 1): if l[i] is not None: a[l[i]] = s[i] print(0 if cnt == 1 else cnt if n <= 3 else n) print(*a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR LIST NONE NONE ASSIGN VAR LIST NONE NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NONE VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR NUMBER NUMBER IF VAR VAR NONE ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
t = int(input()) for i in range(t): n = int(input()) s = input().split() s = [int(x) for x in s] if n == 1: print("0") print(s[0]) continue elif n == 2 and s[0] == s[1]: print("0") print(s[0], s[1]) continue elif n == 2: print("2") print(s[1], s[0]) continue elif n == 3 and s[0] == s[1]: print("2") print(s[0], s[2], s[1]) continue elif n == 3 and s[0] == s[2]: print("2") print(s[1], s[0], s[2]) continue elif n == 3 and s[1] == s[2]: print("2") print(s[1], s[0], s[2]) continue elif n == 3: print("3") print(s[1], s[2], s[0]) continue j = 0 count = 0 temp = 0 while j < n: if j + 1 < n: if s[j] != s[j + 1]: temp = s[j] s[j] = s[j + 1] s[j + 1] = temp count += 2 j += 2 elif j + 3 < n: temp = s[j] s[j] = s[j + 2] s[j + 2] = temp temp = s[j + 1] s[j + 1] = s[j + 3] s[j + 3] = temp count += 4 j += 4 elif j + 2 < n: temp = s[j] s[j] = s[j + 2] s[j + 2] = temp if s[j + 1] != s[0]: temp = s[j + 1] s[j + 1] = s[0] s[0] = temp else: temp = s[j + 1] s[j + 1] = s[1] s[1] = temp count += 3 j += 3 else: temp = s[j] s[j] = s[0] s[0] = temp temp = s[j + 1] s[j + 1] = s[1] s[1] = temp count += 2 j += 2 else: if s[j] != s[0]: temp = s[j] s[j] = s[0] s[0] = temp else: temp = s[j] s[j] = s[1] s[1] = temp count += 1 j += 1 print(count) for i in range(n): print(s[i], end="") print(" ", end="") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
T = int(input()) for P in range(T): n = int(input()) x = input().split() y = list(reversed(x)) z = y.copy() for j in range(n): if x[j] == y[j]: for i in range(n): if y[i] != y[j]: if x[i] != y[j]: b = y[i] y[i] = y[j] y[j] = b break m = 0 for i in range(n): if x[i] != y[i]: m += 1 print(m) N = "" for i in y: N += str(i) + " " N = N[0:-1] print(N)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR 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 STRING FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
t = int(input()) for i in range(t): N = int(input()) A = list(map(int, input().split())) if len(A) == 1: print(0) print(*A) elif len(A) == 2: if A.count(A[0]) == 2: print(0) print(*A) else: A[0], A[1] = A[1], A[0] print(2) print(*A) elif len(A) == 3: if A.count(A[0]) == 2 or A.count(A[1]) == 2: print(2) if A[0] == A[1]: A[0], A[2] = A[2], A[0] elif A[0] == A[2]: A[0], A[1] = A[1], A[0] elif A[1] == A[2]: A[0], A[2] = A[2], A[0] print(*A) else: print(3) A[0], A[1], A[2] = A[1], A[2], A[0] print(*A) else: print(len(A)) ACopy = list(A) for k in range(len(A)): if ACopy[k] == A[k]: for j in range(len(A)): if j != k and ACopy[j] != A[k] and ACopy[k] != A[j]: ACopy[k], ACopy[j] = ACopy[j], ACopy[k] print(*ACopy)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
def ip(): return int(input()) def ipp(): return map(int, input().split()) def sar(): return list(ipp()) def pars(a): print(" ".join(list(map(str, a)))) def parl(a): print("\r".join(list(map(str, a)))) def ham(a, b): s = 0 for i in range(len(a)): if a[i] != b[i]: s += 1 return s def ans(a, n): b = [0] * len(a) for i in range(len(a)): for j in range(len(a)): b[j] = a[(i + j) % len(a)] if ham(a, b) == n: break print(n) pars(b) T = 1 T = int(input().strip()) for _ in range(T): n = ip() a = [] a = sar() if n == 2 and a[0] == a[1] or n == 1: ans(a, 0) elif n == 3 and (a[0] == a[1] or a[1] == a[2] or a[0] == a[2]): ans(a, 2) else: ans(a, n)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
def dist(a, ans): c = 0 for i in range(0, n): if a[i] == ans[i]: c += 1 return c t = int(input()) for iteration in range(0, t): n = int(input()) a = list(map(int, input().split(" "))) ans = [i for i in a] c = dist(a, ans) for i in range(0, n): if a[i] == ans[i]: for j in range(0, n): if i != j and a[i] != ans[j] and a[j] != ans[i]: ans[i], ans[j] = ans[j], ans[i] c = dist(a, ans) print(n - c) print(" ".join(list(map(lambda x: str(x), ans))))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
def h(l1, l2): x = 0 l = len(l1) for i in range(l): if l1[i] == l2[i]: x += 1 return l - x for i in range(int(input())): x = int(input()) arr = input().split() arr2 = list(arr) for i in range(len(arr)): if arr[i] == arr2[i]: for j in range(0, len(arr)): if arr[i] != arr[j] and arr[i] != arr2[j]: arr[i], arr[j] = arr[j], arr[i] break print(h(arr, arr2)) print(" ".join(arr))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
tests = int(input()) results = [] for i in range(0, tests): n = int(input()) numso = input().split(" ") nums = list(numso) n = len(nums) l = 0 r = n - 1 while l < r: if nums[l] != nums[r]: nums[l], nums[r] = nums[r], nums[l] l += 1 r -= 1 elif l + 1 < r - 1: nums[l], nums[l + 1] = nums[l + 1], nums[l] nums[r], nums[r - 1] = nums[r - 1], nums[r] l += 2 r -= 2 elif l - 1 >= 0 and r + 1 <= n - 1: nums[l], nums[l - 1] = nums[l - 1], nums[l] nums[r], nums[r + 1] = nums[r + 1], nums[r] l += 2 r -= 2 else: break if n % 2 == 1 and n >= 3: m = int(n / 2) mv = nums[m] for p in range(0, n): if mv != nums[p] and mv != numso[p]: nums[m], nums[p] = nums[p], nums[m] break dist = 0 for p in range(0, n): if nums[p] != numso[p]: dist += 1 results.append(dist) results.append(" ".join(nums)) for i in range(0, len(results)): if i % 2 == 1: print(results[i]) else: ll = results[i] print(ll)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
test = int(input()) for _ in range(test): S = int(input()) arr = {} two = {} hd = 0 c = 1 for i in input().split(): i = int(i) if i in arr: temp = arr[i][1] arr.pop(i) two[i] = {(2): [temp, c]} else: arr[i] = {(1): c} c += 1 res = [0] * S hd = 0 lastc = lasta = 0 lastx = lasty = lastx1 = -1 lt = list(two.keys()) lent = len(lt) lent_c = 0 lentwo = len(two) while two != {} and lentwo > 1: if lent_c < lent - 1: x = two.pop(lt[lent_c]) k1 = lt[lent_c] if lastc == 0: lastx, lasty = x[2] lastc = 1 indy1, indy2 = two[lt[lent_c + 1]][2] res[indy1 - 1] = k1 res[indy2 - 1] = k1 hd += 2 else: x = two.popitem() res[lastx - 1] = x[0] res[lasty - 1] = x[0] hd += 2 lent_c += 1 arrl = [] lenarr = len(arr) l = list(arr.keys()) lenl = len(l) lenl_c = 0 while arr != {} and lenarr > 1: hd += 1 if lenl_c < lenl - 1: x = arr.pop(l[lenl_c]) k1 = l[lenl_c] if lasta == 0: lastx1 = x[1] lasta = 1 indy1 = arr[l[lenl_c + 1]][1] res[indy1 - 1] = k1 arrl.append(indy1 - 1) else: x = arr.popitem() if lastx1 != -1: res[lastx1 - 1] = x[0] arrl.append(lastx1 - 1) lenl_c += 1 if len(two) == 1 and lenarr > 1: t = two.popitem() indt1, indt2 = t[1][2] valt = t[0] x, y = res[arrl[0]], res[arrl[1]] res[arrl[0]], res[arrl[1]], res[indt1 - 1], res[indt2 - 1] = valt, valt, x, y hd += 2 elif len(two) == 1 and len(arr) == 1: t = two.popitem() indt1, indt2 = t[1][2] valt = t[0] a = arr.popitem() inda = a[1][1] vala = a[0] res[indt1 - 1], res[indt2 - 1], res[inda - 1] = valt, vala, valt hd = 2 elif len(two) == 1 and lenarr == 0: t = two.popitem() indt1, indt2 = t[1][2] valt = t[0] res[0] = res[1] = valt hd = 0 elif len(arr) == 1 and two == {}: if S == 1: res[0] = arr.popitem()[0] hd = 0 elif S > 1: a = arr.popitem() inda = a[1][1] vala = a[0] if inda < S - 1: res[inda], res[inda - 1] = a[0], res[inda] elif inda == S: res[0], res[inda - 1] = a[0], res[0] hd += 1 print(hd) print(*res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR DICT NUMBER LIST VAR VAR ASSIGN VAR VAR DICT NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR DICT VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR DICT VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR DICT IF VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
def callme(): n = int(input()) arr = list(map(int, input().split())) if n == 1: print("0") print(arr[0]) return newarr = [0] * n if len(arr) == len(set(arr)): for i in range(n): newarr[i - 1] = arr[i] print(n) print(*newarr) return freq = {} for i in arr: if i not in freq.keys(): freq[i] = 0 freq[i] += 1 newarr = [-1] * n x = 0 for i in freq.keys(): while freq[i] > 0 and x < n: if newarr[x] == -1 and i != arr[x]: newarr[x] = i freq[i] -= 1 x += 1 y = [] for i in freq.keys(): if freq[i] > 0: y.append(i) x = 0 for i in y: while freq[i] > 0 and x < n: if newarr[x] == -1 and i != arr[x]: newarr[x] = i freq[i] -= 1 x += 1 y = [] for i in freq.keys(): if freq[i] > 0: y.append(i) x = 0 if len(y) > 0: x = 0 for i in y: while freq[i] > 0 and x < n: if newarr[x] == -1: newarr[x] = i freq[i] -= 1 x += 1 cost = 0 for i in range(n): if arr[i] != newarr[i]: cost += 1 print(cost) print(*newarr) for _ in range(int(input())): callme()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR BIN_OP LIST NUMBER VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR WHILE VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
for _ in range(int(input())): n = int(input()) a = [int(s) for s in input().split()] b = [] if n == 1: print(0) print(a[0]) elif n == 2: if a[1] == a[0]: print(0) else: print(2) print(a[1], a[0]) elif n == 3: if a[1] == a[0]: print(2) print(a[0], a[2], a[1]) elif a[0] == a[2]: print(2) print(a[0], a[2], a[1]) elif a[1] == a[2]: print(2) print(a[1], a[0], a[2]) else: print(3) print(a[1], a[2], a[0]) else: if n % 2 == 0: i = 0 ad = 0 while i < n: if a[i] == a[i + 1]: if b != []: tmp1 = b[-2] tmp2 = b[-1] b[-1], b[-2] = a[i], a[i] b.append(tmp1) b.append(tmp2) elif ad == 1: b.append(a[i]) b.append(a[i]) b.append(a1) b.append(a1) ad = 0 else: ad = 1 a1 = a[i] else: b.append(a[i + 1]) b.append(a[i]) if ad == 1: ad = 0 b.append(a1) b.append(a1) i += 2 else: i = 0 ad = 0 while i < n - 1: if a[i] == a[i + 1]: if b != []: tmp1 = b[-2] tmp2 = b[-1] b[-1], b[-2] = a[i], a[i] b.append(tmp1) b.append(tmp2) elif ad == 1: b.append(a[i]) b.append(a[i]) b.append(a1) b.append(a1) ad = 0 else: ad = 1 a1 = a[i] else: b.append(a[i + 1]) b.append(a[i]) if ad == 1: ad = 0 b.append(a1) b.append(a1) i += 2 if a[-2] == a[-1]: b.append(a[-1]) temp = b[0] b[0] = b[-1] b[-1] = temp else: b.append(a[-1]) temp = b[-2] b[-2] = b[-1] b[-1] = temp print(n) print(*b)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) l1 = [] l1 += l[n // 2 :] l1 += l[: n // 2] c = 0 for i in range(len(l)): if l[i] != l1[i]: c += 1 print(c) for i in l1: print(i, end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
for i in range(int(input())): n = int(input()) l1 = [] flag = 0 f = 0 l = [int(x) for x in input().split()] if n == 1: print(0) print(*l) flag = 1 if n == 2 and n != len(set(l)) and flag == 0: print(0) print(*l) flag = 1 if n == len(set(l)) and flag == 0: print(n) for i in range(n - 1): l1.append(l[i + 1]) l1.append(l[0]) print(*l1) elif flag == 0: c = 1 f = 0 for i in range(n - 1): if l[i] == l[i + 1] or l[0] == l[-1]: f = 1 break else: continue if f != 1 or f == 1 and n == 3 and len(set(l)) == 2: if n != 3: print(n) else: print(2) for i in range(n - 1): l1.append(l[i + 1]) l1.append(l[0]) print(*l1) else: print(n) for i in range(n - 2): l1.append(l[i + 2]) l1.append(l[0]) l1.append(l[1]) for i in range(n - 1): if l1[i] == l[i]: a = l1[i] l1[i] = l1[i + 1] l1[i + 1] = a print(*l1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
T = int(input()) for _testcase_ in range(T): N = int(input()) A = [int(x) for x in input().split()] if len(A) <= 3: if len(A) == 1: B = [A[0]] print(0) if len(A) == 2: if A[0] == A[1]: B = [A[1], A[0]] print(0) else: B = [A[1], A[0]] print(2) if len(A) == 3: if len(set(A)) == 3: B = [A[1], A[2], A[0]] print(3) elif len(set(A)) == 2: print(2) single = min(A, key=lambda x: A.count(x)) double = max(A, key=lambda x: A.count(x)) singleI = A.index(single) if singleI == 0: B = [double, single, double] else: B = [single, double, double] print(" ".join(str(x) for x in B)) continue B = [A[-1]] + A[:-1] badValues = [] badIndices = [] for i in range(N): if A[i] == B[i]: badValues.append(B[i]) badIndices.append(i) if len(badValues) > 1: badIndices = [badIndices[-1]] + badIndices[:-1] for i in range(len(badIndices)): badIndex = badIndices[i] badValue = badValues[i] B[badIndex] = badValue elif len(badValues) == 1: for newI in range(N): if A[newI] != badValues[0] and A[badIndices[0]] != B[newI]: B[newI], B[badIndices[0]] = badValues[0], B[newI] break elif len(badValues) < 1: pass print(N) print(" ".join(str(x) for x in B)) continue
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR LIST VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
def swap(b, i, x, n): if a[i] != b[i + x]: temp = b[i] b[i] = b[i + x] b[i + x] = temp elif x < n - 2: x += 1 swap(b, i, x, n) return b for t in range(int(input())): n = int(input()) a = [int(i) for i in input().split()] b = a[::-1] ct = 0 if n > 2: for i in range(n - 1): x = 0 if a[i] == b[i + x]: if x < n - 2: x += 1 swap(b, i, x, n) else: break if a[n - 1] == b[n - 1]: if a[n - 1] != b[0]: temp = b[n - 1] b[n - 1] = b[0] b[0] = temp elif a[n - 1] != b[n // 2]: temp = b[n // 2] b[n // 2] = b[n - 1] b[n - 1] = temp if a[0] == b[0]: if a[n - 1] != b[1]: temp = b[0] b[0] = b[1] b[0] = temp elif a[0] != b[n // 2]: temp = b[n // 2] b[n // 2] = b[0] b[0] = temp if n >= 4: ct = n else: for i in range(n): if a[i] != b[i]: ct += 1 print(ct) print(" ".join(str(x) for x in b))
FUNC_DEF IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
t = int(input()) while t > 0: n = int(input()) c = [0] * n b = list(map(int, input().split())) if n == 1: print(0) print(b[0]) if n == 2: if b[0] == b[1]: print(0) print(b[0], b[1]) else: print(2) print(b[1], b[0]) if n == 3: if b[0] == b[1] or b[1] == b[2] or b[2] == b[0]: print(2) if b[0] == b[1]: print(b[2], b[0], b[1]) elif b[1] == b[2]: print(b[1], b[0], b[2]) elif b[0] == b[2]: print(b[1], b[0], b[2]) else: print(3) print(b[2], b[0], b[1]) if n >= 4: x = [] print(n) for i in range(n): for j in range(i - 2, n): if c[j] == 0 and b[j] != b[i] and j != i: if j >= 0: c[j] = b[i] break if c[n - 1] == 0: if c[n - 2] == 0: c[n - 1] = c[0] c[n - 2] = c[1] c[0] = b[n - 1] c[1] = b[n - 2] else: for i in range(n): if b[n - 1] != b[i] and c[i] != b[n - 1]: c[n - 1] = c[i] c[i] = b[n - 1] break if c[n - 1] != 0 and c[n - 2] == 0: for i in range(n): if b[n - 2] != b[i] and c[i] != b[n - 2]: c[n - 2] = c[i] c[i] = b[n - 2] break print(" ".join(map(str, c))) t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
t = int(input()) while t: t -= 1 n = int(input()) a = list(map(int, input().split())) b = a[:] i = 0 while i < n: while i < n and a[i] != b[i]: i += 1 j = i + 1 while j < n and b[j] == b[i]: j += 1 if j < n: b[i], b[j] = b[j], b[i] i += 1 for i in range(n - 1, -1, -1): if b[i] == a[i]: j = i - 1 while j >= 0 and (b[j] == b[i] or b[j] == a[i] or a[j] == b[i]): j -= 1 if j >= 0: b[j], b[i] = b[i], b[j] i += 1 ans = 0 for i in range(n): if a[i] != b[i]: ans += 1 print(ans) print(*b)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
t = int(input()) while t > 0: n = int(input()) l = list(map(int, input().split())) if n > 3: print(n) j = 0 v = l[0:n] z = [(0) for i in range(n)] while j < n: if z[j] == 1: j += 1 continue temp = l[j] k = j + 1 while k < n and (z[k] != 0 or v[j] == v[k]): k += 1 if k != n: v[j], v[k] = v[k], v[j] z[j] = 1 z[k] = 1 j += 1 j = 0 while j < n: if z[j] == 0: for i in range(n): if v[i] != v[j] and l[i] != l[j]: v[i], v[j] = v[j], v[i] z[j] = 1 break j += 1 for i in v: print(i, end=" ") elif n == 1: print(0) print(l[0]) elif n == 2: if l.count(l[0]) > 1: print(0) print(l[0], l[1]) else: print(2) print(l[1], l[0]) else: x = l.count(l[0]) y = l.count(l[1]) z = l.count(l[2]) if x > 1: print(2) print(l[0], l[2], l[1]) elif y > 1: print(2) print(l[2], l[1], l[0]) else: print(3) print(l[2], l[0], l[1]) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
for t in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = a[::-1] count = 0 for i, val in enumerate(b): if val == a[i]: j = (i + 1) % n while j != i: if val != b[j] and val != a[j]: b[j], b[i] = b[i], b[j] count += 1 break j = (j + 1) % n else: count += 1 print(count) for i in b: print(i, end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
for t in range(int(input())): n = int(input()) a = list(map(int, input().split())) f = "ncorr" if len(a) == 1: print(0) print(a[0]) continue if n == len(set(a)): print(n) print(" ".join([str(a[n - 1])] + [str(a[i]) for i in range(n - 1)])) continue if n == 2: print(0) print(a[0], a[0]) continue if n == 3: print(2) print(a[1], a[2], a[0]) continue ans = [a[n - 2], a[n - 1]] + [a[i] for i in range(n - 2)] while f == "ncorr": f = "corr" if ans[n - 1] == a[n - 1]: ans[0], ans[n - 1] = ans[n - 1], ans[0] f = "ncorr" for i in range(n - 1): if a[i] == ans[i]: ans[i], ans[i + 1] = ans[i + 1], ans[i] f = "ncorr" ans = list(map(str, ans)) print(n) print(" ".join(ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP LIST FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR STRING ASSIGN VAR STRING IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
for i in range(int(input())): n = int(input()) l = list(map(int, input().split())) count = 0 l_copy = l[:] k = 0 while k < n: if l[k] != l[(k + 1) % n]: l[k], l[(k + 1) % n] = l[(k + 1) % n], l[k] k = k + 2 else: l[k], l[(k + 2) % n] = l[(k + 2) % n], l[k] l[(k + 1) % n], l[(k + 3) % n] = l[(k + 3) % n], l[(k + 1) % n] k = k + 4 s = "" count = 0 for _ in range(n): if l[_] != l_copy[_]: count = count + 1 s = s + str(l[_]) + " " print(count) print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
for i in range(int(input())): n = int(input()) l = [int(s) for s in input().split()] if len(l) == 3: ans1 = 0 ans = l[1:] + [l[0]] for w in range(3): if ans[w] != l[w]: ans1 += 1 print(ans1) print(ans[0], ans[1], ans[2]) elif len(l) == 2: ans = [l[1], l[0]] if ans == l: print(0) print(ans[0], ans[1]) else: print(2) print(ans[0], ans[1]) elif len(l) == 1: print(0) print(l[0]) else: ans = l.copy() r = [] s = 0 if n % 2 * (n - 1) > 0: s = n % 2 * (n - 1) for j in range(0, n - 1, 2): if ans[j] != ans[j + 1]: a = ans[j + 1] ans[j + 1] = ans[j] ans[j] = a else: r += [j, j + 1] r1 = [] for k in range(len(r)): if k % 2 == 0: if r[k] not in r1 and r[k] - 1 not in r1: b = ans[r[k] - 1] ans[r[k] - 1] = ans[r[k]] ans[r[k]] = b r1 += [r[k], r[k] - 1] elif r[k] not in r1 and (r[k] + 1) % n not in r1: d = ans[(r[k] + 1) % n] ans[(r[k] + 1) % n] = ans[r[k]] ans[r[k]] = d r1 += [r[k], (r[k] + 1) % n] if s != 0: c = ans[s] ans[s] = ans[2] ans[2] = c print(n) for v in ans: print(v, end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR LIST VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR LIST VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
for sadadsads in range(int(input())): n = int(input()) a = list(map(lambda x: int(x), input().split())) b = [i for i in a] c = [] d = {} for i in a: d[i] = i if len(d) == n: a[n // 2], a[-1] = a[-1], a[n // 2] a = a[::-1] else: i = 0 while i < len(a) - 3: if a[i + 1] == a[i]: a[i + 2], a[i] = a[i], a[i + 2] a[i + 3], a[i + 2] = a[i + 2], a[i + 3] else: a[i], a[i + 1] = a[i + 1], a[i] i += 2 if len(a) > 1: if len(a) == 2: a = a[::-1] else: if a[-1] == a[-2]: a[-2], a[-3] = a[-3], a[-2] elif a[-2] == a[-3]: a[-1], a[-3] = a[-3], a[-1] for i in range(len(a)): if a[i] != a[-3] and a[-3] != b[i]: a[i], a[-3] = a[-3], a[i] dist = 0 if n > 3 and n < 500: for i in range(n): if b[i] != a[i]: dist += 1 if n < 500 and dist != n: a = [i for i in b] b = sorted(a) c = [] for i in range(len(a)): if b[0] == a[i]: c.append(b[-1]) del b[-1] else: c.append(b[0]) del b[0] if len(a) == 1: print(0) elif len(a) == 2: if a[0] == a[1]: print(0) else: print(2) elif len(a) == 3: if a[0] == a[1] or a[1] == a[2] or a[0] == a[2]: print(2) else: print(3) else: print(len(a)) for i in c: print(i, end=" ") print() else: if len(a) == 1: print(0) elif len(a) == 2: if a[0] == a[1]: print(0) else: print(2) elif len(a) == 3: if a[0] == a[1] or a[1] == a[2] or a[0] == a[2]: print(2) else: print(3) else: print(len(a)) for i in a: print(i, end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
def print_arr(arr): for i in arr: print(i, end=" ") print() def repl(order_2, order_1, arr): b = [] for i in order_2: b.append(i) b.append(i) for i in order_1: b.append(i) l = b[:] l.extend(b[0:2]) l = l[2:] res = [] d = {} for i in range(len(b)): if b[i] in d: d[b[i]].append(l[i]) else: d[b[i]] = [l[i]] for i in arr: res.append(d[i][0]) d[i] = d[i][1:] return res t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) d = {} for i in a: if i in d: d[i] += 1 else: d[i] = 1 order_2 = [] order_1 = [] replace = {} for i in d: if d[i] == 2: order_2.append(i) else: order_1.append(i) if len(a) == 1: print(0) print_arr(a) continue elif len(a) == 2: if len(order_2) == 1: print(0) print_arr(a) continue else: print(2) a.reverse() print_arr(a) continue b = repl(order_2, order_1, a) dist = 0 for i in range(len(a)): if a[i] != b[i]: dist += 1 print(dist) print_arr(b)
FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
for q in range(int(input())): n = int(input()) a = [int(i) for i in input().split()] b = list(set(a)) c = [a[i] for i in range(n)] lb = len(b) flag = [(0) for i in range(n)] count = 0 count1 = 0 if n == 1: print(0) for k in a: print(k, end=" ") print() elif n == lb: print(n) for k in range(1, n): print(a[k], end=" ") print(a[0]) elif n == 2 and lb == 1: print(0) for k in a: print(k, end=" ") print() elif n == 3 and lb == 2: if a[0] == a[1]: print(2) print(a[0], end=" ") print(a[2], end=" ") print(a[1]) if a[0] == a[2] or a[1] == a[2]: print(2) print(a[1], end=" ") print(a[0], end=" ") print(a[2]) elif n == 4 and lb == 3 and a[3] == a[2]: print(4) print(a[2], end=" ") print(a[3], end=" ") print(a[0], end=" ") print(a[1]) else: print(n) i = 1 while i < n: if i == n - 1 and flag[i - 1] == 1: break if i == n - 1 and flag[i - 1] == 0 and c[i] != c[i - 1]: c[i], c[i - 1] = c[i - 1], c[i] flag[i - 1] = 1 flag[i] = 1 break if c[i] == c[i - 1]: i += 1 else: c[i], c[i - 1] = c[i - 1], c[i] flag[i - 1] = 1 flag[i] = 1 i += 2 for i in range(n): if c[i] != a[i]: count1 += 1 if count1 == n: for d in c: print(d, end=" ") print() else: ind = [] for i in range(n): if flag[i] == 0: count += 1 if count % 2 == 0 and count > 0: for i in range(n): if flag[i] == 0: ind.append(i) l = len(ind) i = 1 while i < l: c[ind[i]], c[ind[i - 1]] = c[ind[i - 1]], c[ind[i]] flag[ind[i - 1]] = 1 flag[ind[i]] = 1 i += 2 else: for i in range(n): if flag[i] == 0: ind.append(i) l = len(ind) if l == 1: for i in range(n): if c[ind[0]] != a[i] and c[ind[0]] != c[i]: c[ind[0]], c[i] = c[i], c[ind[0]] flag[ind[0]] = 1 break else: i = 1 while i < l - 1: c[ind[i]], c[ind[i - 1]] = c[ind[i - 1]], c[ind[i]] flag[ind[i - 1]] = 1 flag[ind[i]] = 1 i += 2 for i in range(n): if c[ind[l - 1]] != a[i] and c[ind[l - 1]] != c[i]: c[ind[l - 1]], c[i] = c[i], c[ind[l - 1]] flag[ind[l - 1]] = 1 break for d in c: print(d, end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Read problems statements in Vietnamese . Chef likes to work with arrays a lot. Today he has an array A of length N consisting of positive integers. Chef's little brother likes to follow his elder brother, so he thought of creating an array B of length N. The little brother is too small to think of new numbers himself, so he decided to use all the elements of array A to create the array B. In other words, array B is obtained by shuffling the elements of array A. The little brother doesn't want Chef to know that he has copied the elements of his array A. Therefore, he wants to create the array B in such a way that the Hamming distance between the two arrays A and B is maximized. The Hamming distance between A and B is the number of indices i (1 ≤ i ≤ N) such that A_{i} ≠ B_{i}. The brother needs your help in finding any such array B. Can you please find one such array for him? Note that it's guaranteed that no element in A appears more than twice, i.e. frequency of each element is at most 2. ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains an integer N denoting the length of the array A. The second line contains N space-separated integers A_{1}, A_{2} ... A_{N}. ------ Output ------ For each test case, print two lines. The first line should contain the maximum possible Hamming distance that array B can have from array A. The second line should contain N space-separated integers denoting the array B; the i-th integer should denote the value of B_{i}. Note that B should be an array obtained after shuffling the elements of A. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{5}$ $The frequency of each integer in the array A will be at most 2.$ ------ Subtasks ------ Subtask #1 (30 points): all elements in the array A are unique Subtask #2 (30 points): 5 ≤ N ≤ 10^{5} Subtask #3 (40 points): original constraints ----- Sample Input 1 ------ 3 2 1 2 3 1 2 1 4 2 6 5 2 ----- Sample Output 1 ------ 2 2 1 2 2 1 1 4 6 2 2 5
def check(x, y): count = 0 for i in range(len(x)): if x[i] != y[i]: count += 1 return count t = int(input()) for k in range(t): n = int(input()) b = [int(x) for x in input().split()] a = [b[i] for i in range(1, n)] a.append(b[0]) if n > 2: for i in range(n): if a[i] == b[i]: for j in range(n): if j != i and a[i] != b[j] and b[i] != a[j]: a[i], a[j] = a[j], a[i] break print(check(a, b)) for j in a: print(j, end=" ") print()
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR