message
stringlengths
2
28.7k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
21
109k
cluster
float64
7
7
__index_level_0__
int64
42
217k
Provide a correct Python 3 solution for this coding contest problem. We have 3N colored balls with IDs from 1 to 3N. A string S of length 3N represents the colors of the balls. The color of Ball i is red if S_i is `R`, green if S_i is `G`, and blue if S_i is `B`. There are N red balls, N green balls, and N blue balls. Takahashi will distribute these 3N balls to N people so that each person gets one red ball, one blue ball, and one green ball. The people want balls with IDs close to each other, so he will additionally satisfy the following condition: * Let a_j < b_j < c_j be the IDs of the balls received by the j-th person in ascending order. * Then, \sum_j (c_j-a_j) should be as small as possible. Find the number of ways in which Takahashi can distribute the balls. Since the answer can be enormous, compute it modulo 998244353. We consider two ways to distribute the balls different if and only if there is a person who receives different sets of balls. Constraints * 1 \leq N \leq 10^5 * |S|=3N * S consists of `R`, `G`, and `B`, and each of these characters occurs N times in S. Input Input is given from Standard Input in the following format: N S Output Print the number of ways in which Takahashi can distribute the balls, modulo 998244353. Examples Input 3 RRRGGGBBB Output 216 Input 5 BBRGRRGRGGRBBGB Output 960
instruction
0
87,340
7
174,680
"Correct Solution: ``` from collections import defaultdict mod = 998244353 N = int(input()) S = input() colors = ['R', 'G', 'B'] invs = {'R':'GB', 'G':'BR', 'B':'RG'} cnt = defaultdict(int) ans = 1 for i in range(N*3): s = S[i] inv = invs[s] if cnt[inv] != 0: ans *= cnt[inv] ans%=mod cnt[inv]-=1 continue flag = False for c in colors: if c == s: continue if cnt[c]: ans*=cnt[c] ans%=mod cnt[c]-=1 cnt[c+s]+=1 cnt[s+c]+=1 flag = True if not flag: cnt[s]+=1 ans%=mod for i in range(1, N+1): ans*=i ans%=mod print(ans) ```
output
1
87,340
7
174,681
Provide a correct Python 3 solution for this coding contest problem. We have 3N colored balls with IDs from 1 to 3N. A string S of length 3N represents the colors of the balls. The color of Ball i is red if S_i is `R`, green if S_i is `G`, and blue if S_i is `B`. There are N red balls, N green balls, and N blue balls. Takahashi will distribute these 3N balls to N people so that each person gets one red ball, one blue ball, and one green ball. The people want balls with IDs close to each other, so he will additionally satisfy the following condition: * Let a_j < b_j < c_j be the IDs of the balls received by the j-th person in ascending order. * Then, \sum_j (c_j-a_j) should be as small as possible. Find the number of ways in which Takahashi can distribute the balls. Since the answer can be enormous, compute it modulo 998244353. We consider two ways to distribute the balls different if and only if there is a person who receives different sets of balls. Constraints * 1 \leq N \leq 10^5 * |S|=3N * S consists of `R`, `G`, and `B`, and each of these characters occurs N times in S. Input Input is given from Standard Input in the following format: N S Output Print the number of ways in which Takahashi can distribute the balls, modulo 998244353. Examples Input 3 RRRGGGBBB Output 216 Input 5 BBRGRRGRGGRBBGB Output 960
instruction
0
87,341
7
174,682
"Correct Solution: ``` import sys from bisect import * def main(): input = sys.stdin.readline N = int(input()) S = input().strip() M = 998244353 ri = [] gi = [] bi = [] for i, c in enumerate(S): if c=='R': ri.append(i) elif c=='G': gi.append(i) else: bi.append(i) si = [] mi = [] li = [] for i, tt in enumerate(zip(ri,gi,bi)): s, m, l = sorted(tt) si.append(s) mi.append(m) li.append(l) ans = 1 for i, mm in enumerate(mi): st = bisect(si,mm) lt = bisect(li,mm) ans = ans*(st-i)*(i+1-lt)*(i+1) ans = ans%M print(ans) if __name__ == "__main__": main() ```
output
1
87,341
7
174,683
Provide a correct Python 3 solution for this coding contest problem. We have 3N colored balls with IDs from 1 to 3N. A string S of length 3N represents the colors of the balls. The color of Ball i is red if S_i is `R`, green if S_i is `G`, and blue if S_i is `B`. There are N red balls, N green balls, and N blue balls. Takahashi will distribute these 3N balls to N people so that each person gets one red ball, one blue ball, and one green ball. The people want balls with IDs close to each other, so he will additionally satisfy the following condition: * Let a_j < b_j < c_j be the IDs of the balls received by the j-th person in ascending order. * Then, \sum_j (c_j-a_j) should be as small as possible. Find the number of ways in which Takahashi can distribute the balls. Since the answer can be enormous, compute it modulo 998244353. We consider two ways to distribute the balls different if and only if there is a person who receives different sets of balls. Constraints * 1 \leq N \leq 10^5 * |S|=3N * S consists of `R`, `G`, and `B`, and each of these characters occurs N times in S. Input Input is given from Standard Input in the following format: N S Output Print the number of ways in which Takahashi can distribute the balls, modulo 998244353. Examples Input 3 RRRGGGBBB Output 216 Input 5 BBRGRRGRGGRBBGB Output 960
instruction
0
87,342
7
174,684
"Correct Solution: ``` import math n=int(input()) a=1;c={"R":0,"G":0,"B":0} m=998244353 for i in input(): s=sorted(c.values()) c[i]+=1 if c[i]>s[2]:pass elif c[i]>s[1]:a*=s[2]-s[1] else:a*=s[1]-s[0] a%=m print(math.factorial(n)*a%m) ```
output
1
87,342
7
174,685
Provide a correct Python 3 solution for this coding contest problem. We have 3N colored balls with IDs from 1 to 3N. A string S of length 3N represents the colors of the balls. The color of Ball i is red if S_i is `R`, green if S_i is `G`, and blue if S_i is `B`. There are N red balls, N green balls, and N blue balls. Takahashi will distribute these 3N balls to N people so that each person gets one red ball, one blue ball, and one green ball. The people want balls with IDs close to each other, so he will additionally satisfy the following condition: * Let a_j < b_j < c_j be the IDs of the balls received by the j-th person in ascending order. * Then, \sum_j (c_j-a_j) should be as small as possible. Find the number of ways in which Takahashi can distribute the balls. Since the answer can be enormous, compute it modulo 998244353. We consider two ways to distribute the balls different if and only if there is a person who receives different sets of balls. Constraints * 1 \leq N \leq 10^5 * |S|=3N * S consists of `R`, `G`, and `B`, and each of these characters occurs N times in S. Input Input is given from Standard Input in the following format: N S Output Print the number of ways in which Takahashi can distribute the balls, modulo 998244353. Examples Input 3 RRRGGGBBB Output 216 Input 5 BBRGRRGRGGRBBGB Output 960
instruction
0
87,343
7
174,686
"Correct Solution: ``` import math n=int(input()) a=1;c={"R":0,"G":0,"B":0} m=998244353 for i in input(): f,s,t=sorted(c.values()) c[i]+=1 a*=(s-f,t-s,1)[(c[i]>t)+(c[i]>s)] a%=m print(math.factorial(n)*a%m) ```
output
1
87,343
7
174,687
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have 3N colored balls with IDs from 1 to 3N. A string S of length 3N represents the colors of the balls. The color of Ball i is red if S_i is `R`, green if S_i is `G`, and blue if S_i is `B`. There are N red balls, N green balls, and N blue balls. Takahashi will distribute these 3N balls to N people so that each person gets one red ball, one blue ball, and one green ball. The people want balls with IDs close to each other, so he will additionally satisfy the following condition: * Let a_j < b_j < c_j be the IDs of the balls received by the j-th person in ascending order. * Then, \sum_j (c_j-a_j) should be as small as possible. Find the number of ways in which Takahashi can distribute the balls. Since the answer can be enormous, compute it modulo 998244353. We consider two ways to distribute the balls different if and only if there is a person who receives different sets of balls. Constraints * 1 \leq N \leq 10^5 * |S|=3N * S consists of `R`, `G`, and `B`, and each of these characters occurs N times in S. Input Input is given from Standard Input in the following format: N S Output Print the number of ways in which Takahashi can distribute the balls, modulo 998244353. Examples Input 3 RRRGGGBBB Output 216 Input 5 BBRGRRGRGGRBBGB Output 960 Submitted Solution: ``` #B問題 import bisect N = int(input()) S = list(str(input())) mod = 998244353 N_factorial = 1 for i in range(N): N_factorial*=(i+1) N_factorial%=mod r,g,b = [],[],[] for i,s in enumerate(S): if s == "R": r.append(i) if s == "G": g.append(i) if s == "B": b.append(i) A,B,C = [],[],[] for i in range(N): rgb = [r[i],g[i],b[i]] rgb.sort() A.append(rgb[0]) B.append(rgb[1]) C.append(rgb[2]) ans = N_factorial for i,B_ind in enumerate(B): x = bisect.bisect_left(A,B_ind)-i y = i+1-bisect.bisect_left(C,B_ind) ans*=(x*y) ans%=mod print(ans) ```
instruction
0
87,344
7
174,688
Yes
output
1
87,344
7
174,689
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have 3N colored balls with IDs from 1 to 3N. A string S of length 3N represents the colors of the balls. The color of Ball i is red if S_i is `R`, green if S_i is `G`, and blue if S_i is `B`. There are N red balls, N green balls, and N blue balls. Takahashi will distribute these 3N balls to N people so that each person gets one red ball, one blue ball, and one green ball. The people want balls with IDs close to each other, so he will additionally satisfy the following condition: * Let a_j < b_j < c_j be the IDs of the balls received by the j-th person in ascending order. * Then, \sum_j (c_j-a_j) should be as small as possible. Find the number of ways in which Takahashi can distribute the balls. Since the answer can be enormous, compute it modulo 998244353. We consider two ways to distribute the balls different if and only if there is a person who receives different sets of balls. Constraints * 1 \leq N \leq 10^5 * |S|=3N * S consists of `R`, `G`, and `B`, and each of these characters occurs N times in S. Input Input is given from Standard Input in the following format: N S Output Print the number of ways in which Takahashi can distribute the balls, modulo 998244353. Examples Input 3 RRRGGGBBB Output 216 Input 5 BBRGRRGRGGRBBGB Output 960 Submitted Solution: ``` import sys sys.setrecursionlimit(10**6) def main(): input = sys.stdin.readline N = int(input()) S = str(input().strip()) MOD = 998244353 #state = (_, r, g, b, rg, gb, br) def f(i, ans, state): if i > 3*N-1: return ans if min(state) < 0: return 0 no_ball, r, g, b, rg, gb, br = state if S[i] == 'R': if gb > 0: ans = (ans * gb) % MOD return f(i+1, ans, [no_ball, r, g, b, rg, gb-1, br]) elif g > 0 or b > 0: ans = ans * (g + b) % MOD s1 = [no_ball, r, g-1, b, rg+1, gb, br] s2 = [no_ball, r, g, b-1, rg, gb, br+1] return (f(i+1, ans, s1) + f(i+1, ans, s2)) % MOD else: ans = (ans * no_ball) % MOD return f(i+1, ans, [no_ball-1, r+1, g, b, rg, gb, br]) elif S[i] == 'G': if br > 0: ans = (ans * br) % MOD return f(i+1, ans, [no_ball, r, g, b, rg, gb, br-1]) elif r > 0 or b > 0: ans = ans * (r + b) % MOD s1 = [no_ball, r-1, g, b, rg+1, gb, br] s2 = [no_ball, r, g, b-1, rg, gb+1, br] return (f(i+1, ans, s1) + f(i+1, ans, s2)) % MOD else: ans = (ans * no_ball) % MOD return f(i+1, ans, [no_ball-1, r, g+1, b, rg, gb, br]) else: #S[i] == 'B': if rg > 0: ans = (ans * rg) % MOD return f(i+1, ans, [no_ball, r, g, b, rg-1, gb, br]) elif r > 0 or g > 0: ans = ans * (r + g) % MOD s1 = [no_ball, r-1, g, b, rg, gb, br+1] s2 = [no_ball, r, g-1, b, rg, gb+1, br] return (f(i+1, ans, s1) + f(i+1, ans, s2)) % MOD else: ans = (ans * no_ball) % MOD return f(i+1, ans, [no_ball-1, r, g, b+1, rg, gb, br]) return f(0, 1, [N, 0, 0, 0, 0, 0, 0]) % MOD if __name__ == '__main__': print(main()) ```
instruction
0
87,345
7
174,690
Yes
output
1
87,345
7
174,691
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have 3N colored balls with IDs from 1 to 3N. A string S of length 3N represents the colors of the balls. The color of Ball i is red if S_i is `R`, green if S_i is `G`, and blue if S_i is `B`. There are N red balls, N green balls, and N blue balls. Takahashi will distribute these 3N balls to N people so that each person gets one red ball, one blue ball, and one green ball. The people want balls with IDs close to each other, so he will additionally satisfy the following condition: * Let a_j < b_j < c_j be the IDs of the balls received by the j-th person in ascending order. * Then, \sum_j (c_j-a_j) should be as small as possible. Find the number of ways in which Takahashi can distribute the balls. Since the answer can be enormous, compute it modulo 998244353. We consider two ways to distribute the balls different if and only if there is a person who receives different sets of balls. Constraints * 1 \leq N \leq 10^5 * |S|=3N * S consists of `R`, `G`, and `B`, and each of these characters occurs N times in S. Input Input is given from Standard Input in the following format: N S Output Print the number of ways in which Takahashi can distribute the balls, modulo 998244353. Examples Input 3 RRRGGGBBB Output 216 Input 5 BBRGRRGRGGRBBGB Output 960 Submitted Solution: ``` mod = 998244353 n = int(input()) s = input() ans = 1 R,G,B,RG,GB,BR = 0,0,0,0,0,0 if s[0] == 'R': R = 1 elif s[0] == 'G': G = 1 else: B = 1 for i in range(1,3*n): if s[i] == 'R': if GB > 0: ans = (ans * GB) % mod GB -= 1 elif G > 0: ans = (ans * G) % mod G -= 1 RG += 1 elif B > 0: ans = (ans * B) % mod B -= 1 BR += 1 else: R += 1 elif s[i] == 'G': if BR > 0: ans = (ans * BR) % mod BR -= 1 elif R > 0: ans = (ans * R) % mod R -= 1 RG += 1 elif B > 0: ans = (ans * B) % mod B -= 1 GB += 1 else: G += 1 else: if RG > 0: ans = (ans * RG) % mod RG -= 1 elif R > 0: ans = (ans * R) % mod R -= 1 BR += 1 elif G > 0: ans = (ans * G) % mod G -= 1 GB += 1 else: B += 1 for i in range(n): ans = (ans * (i+1)) % mod print(ans) ```
instruction
0
87,346
7
174,692
Yes
output
1
87,346
7
174,693
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have 3N colored balls with IDs from 1 to 3N. A string S of length 3N represents the colors of the balls. The color of Ball i is red if S_i is `R`, green if S_i is `G`, and blue if S_i is `B`. There are N red balls, N green balls, and N blue balls. Takahashi will distribute these 3N balls to N people so that each person gets one red ball, one blue ball, and one green ball. The people want balls with IDs close to each other, so he will additionally satisfy the following condition: * Let a_j < b_j < c_j be the IDs of the balls received by the j-th person in ascending order. * Then, \sum_j (c_j-a_j) should be as small as possible. Find the number of ways in which Takahashi can distribute the balls. Since the answer can be enormous, compute it modulo 998244353. We consider two ways to distribute the balls different if and only if there is a person who receives different sets of balls. Constraints * 1 \leq N \leq 10^5 * |S|=3N * S consists of `R`, `G`, and `B`, and each of these characters occurs N times in S. Input Input is given from Standard Input in the following format: N S Output Print the number of ways in which Takahashi can distribute the balls, modulo 998244353. Examples Input 3 RRRGGGBBB Output 216 Input 5 BBRGRRGRGGRBBGB Output 960 Submitted Solution: ``` def factorial(n): ans = 1 for i in range(n): ans *= i+1 ans %= mod return ans mod = 998244353 n = int(input()) s = input() ans = 1 r,g,b = 0,0,0 rg,rb,gb = 0,0,0 for i in s: if i == "R": if gb > 0: ans *= gb gb -= 1 elif g > 0: ans *= g g -= 1 rg += 1 elif b > 0: ans *= b b -= 1 rb += 1 else: r += 1 elif i == "G": if rb > 0: ans *= rb rb -= 1 elif r > 0: ans *= r r -= 1 rg += 1 elif b > 0: ans *= b b -= 1 gb += 1 else: g += 1 elif i == "B": if rg > 0: ans *= rg rg -= 1 elif r > 0: ans *= r r -= 1 rb += 1 elif g > 0: ans *= g g -= 1 gb += 1 else: b += 1 ans %= mod print((factorial(n) * ans) % mod) ```
instruction
0
87,347
7
174,694
Yes
output
1
87,347
7
174,695
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have 3N colored balls with IDs from 1 to 3N. A string S of length 3N represents the colors of the balls. The color of Ball i is red if S_i is `R`, green if S_i is `G`, and blue if S_i is `B`. There are N red balls, N green balls, and N blue balls. Takahashi will distribute these 3N balls to N people so that each person gets one red ball, one blue ball, and one green ball. The people want balls with IDs close to each other, so he will additionally satisfy the following condition: * Let a_j < b_j < c_j be the IDs of the balls received by the j-th person in ascending order. * Then, \sum_j (c_j-a_j) should be as small as possible. Find the number of ways in which Takahashi can distribute the balls. Since the answer can be enormous, compute it modulo 998244353. We consider two ways to distribute the balls different if and only if there is a person who receives different sets of balls. Constraints * 1 \leq N \leq 10^5 * |S|=3N * S consists of `R`, `G`, and `B`, and each of these characters occurs N times in S. Input Input is given from Standard Input in the following format: N S Output Print the number of ways in which Takahashi can distribute the balls, modulo 998244353. Examples Input 3 RRRGGGBBB Output 216 Input 5 BBRGRRGRGGRBBGB Output 960 Submitted Solution: ``` import bisect import collections import math n = int(input()) s = input() r = [] g = [] b = [] for i in range(3 * n): if s[i] == "R": r.append(i) if s[i] == "G": g.append(i) if s[i] == "B": b.append(i) def comp(a, b): t = [] for i in range(n): t.append(bisect.bisect_left(a,b[i])) c = collections.Counter(t) ans = 1 for i in list(c.values()): ans = ans * math.factorial(i) return ans p = comp(r, g) * comp(g, b) * comp(b, r) q = comp(g, r) * comp(b, g) * comp(r, b) print(max(p, q)) ```
instruction
0
87,348
7
174,696
No
output
1
87,348
7
174,697
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have 3N colored balls with IDs from 1 to 3N. A string S of length 3N represents the colors of the balls. The color of Ball i is red if S_i is `R`, green if S_i is `G`, and blue if S_i is `B`. There are N red balls, N green balls, and N blue balls. Takahashi will distribute these 3N balls to N people so that each person gets one red ball, one blue ball, and one green ball. The people want balls with IDs close to each other, so he will additionally satisfy the following condition: * Let a_j < b_j < c_j be the IDs of the balls received by the j-th person in ascending order. * Then, \sum_j (c_j-a_j) should be as small as possible. Find the number of ways in which Takahashi can distribute the balls. Since the answer can be enormous, compute it modulo 998244353. We consider two ways to distribute the balls different if and only if there is a person who receives different sets of balls. Constraints * 1 \leq N \leq 10^5 * |S|=3N * S consists of `R`, `G`, and `B`, and each of these characters occurs N times in S. Input Input is given from Standard Input in the following format: N S Output Print the number of ways in which Takahashi can distribute the balls, modulo 998244353. Examples Input 3 RRRGGGBBB Output 216 Input 5 BBRGRRGRGGRBBGB Output 960 Submitted Solution: ``` n=int(input()) s=input() df={"R":0, "G":0, "B":0} db={"R":0, "G":0, "B":0} ans=1 for i in range(3*n): db[s[i]]+=1 for i in range(3*n): df[s[i]]+=1 db[s[i]]-=1 if df["R"]>0 and df["G"]>0: ans*=df["R"]*df["G"]*db["B"] df["R"] -=1 df["G"] -=1 df[s[i]] =0 db["B"] -=1 if df["R"]>0 and df["B"]>0: ans*=df["R"]*df["B"]*db["G"] df["R"] -=1 df["B"] -=1 df[s[i]] =0 db["G"] -=1 if df["B"]>0 and df["G"]>0: ans*=df["B"]*df["G"]*db["R"] df["B"] -=1 df["G"] -=1 df[s[i]] =0 db["R"] -=1 for i in range(1,n+1): ans*=i print(ans) ```
instruction
0
87,349
7
174,698
No
output
1
87,349
7
174,699
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have 3N colored balls with IDs from 1 to 3N. A string S of length 3N represents the colors of the balls. The color of Ball i is red if S_i is `R`, green if S_i is `G`, and blue if S_i is `B`. There are N red balls, N green balls, and N blue balls. Takahashi will distribute these 3N balls to N people so that each person gets one red ball, one blue ball, and one green ball. The people want balls with IDs close to each other, so he will additionally satisfy the following condition: * Let a_j < b_j < c_j be the IDs of the balls received by the j-th person in ascending order. * Then, \sum_j (c_j-a_j) should be as small as possible. Find the number of ways in which Takahashi can distribute the balls. Since the answer can be enormous, compute it modulo 998244353. We consider two ways to distribute the balls different if and only if there is a person who receives different sets of balls. Constraints * 1 \leq N \leq 10^5 * |S|=3N * S consists of `R`, `G`, and `B`, and each of these characters occurs N times in S. Input Input is given from Standard Input in the following format: N S Output Print the number of ways in which Takahashi can distribute the balls, modulo 998244353. Examples Input 3 RRRGGGBBB Output 216 Input 5 BBRGRRGRGGRBBGB Output 960 Submitted Solution: ``` from operator import mul from functools import reduce N = int(input()) S = input() MOD = 998244353 # make A, B, C oredesr r, g, b = 0, 0, 0 T = {'R': 0, 'G': 0, 'B': 0} ABC = '' for s in S: T[s] += 1 ge = [t >= T[s] for t in T.values()].count(True) lt = [t < T[s] for t in T.values()].count(True) if ge == 1: ABC += 'A' elif lt == 0: ABC += 'C' else: ABC += 'B' la, rc = 0, 0 # selectable num of left A and right C ans = 1 # # from left for A # for d in ABC: # if d == 'A': # la += 1 # elif d == 'B': # ans *= la # ans %= MOD # la -= 1 # # from right for C # for d in ABC[::-1]: # if d == 'C': # rc += 1 # elif d == 'B': # ans *= rc # ans %= MOD # rc -= 1 # # human permutation # ans *= reduce(mul, range(1, N+1)) # ans %= MOD print(ans) ```
instruction
0
87,350
7
174,700
No
output
1
87,350
7
174,701
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have 3N colored balls with IDs from 1 to 3N. A string S of length 3N represents the colors of the balls. The color of Ball i is red if S_i is `R`, green if S_i is `G`, and blue if S_i is `B`. There are N red balls, N green balls, and N blue balls. Takahashi will distribute these 3N balls to N people so that each person gets one red ball, one blue ball, and one green ball. The people want balls with IDs close to each other, so he will additionally satisfy the following condition: * Let a_j < b_j < c_j be the IDs of the balls received by the j-th person in ascending order. * Then, \sum_j (c_j-a_j) should be as small as possible. Find the number of ways in which Takahashi can distribute the balls. Since the answer can be enormous, compute it modulo 998244353. We consider two ways to distribute the balls different if and only if there is a person who receives different sets of balls. Constraints * 1 \leq N \leq 10^5 * |S|=3N * S consists of `R`, `G`, and `B`, and each of these characters occurs N times in S. Input Input is given from Standard Input in the following format: N S Output Print the number of ways in which Takahashi can distribute the balls, modulo 998244353. Examples Input 3 RRRGGGBBB Output 216 Input 5 BBRGRRGRGGRBBGB Output 960 Submitted Solution: ``` n = int(input()) s = input() mod = 998244353 ans = 1 z, o, t = n, 0, 0 O, T = "", "" for i in range(3*n): if T != "" and s[i] != O and s[i] != T: ans *= t ans %= mod t -= 1 if t == 0: T = "" elif s[i] == O or O == "": o += 1 ans *= z ans %= mod z -= 1 O = s[i] else: t += 1 ans *= o ans %= mod o -= 1 T = s[i] if o == 0: O = "" print(ans) ```
instruction
0
87,351
7
174,702
No
output
1
87,351
7
174,703
Provide a correct Python 3 solution for this coding contest problem. A: Find the difference problem Given rectangular boards A and B with N squares vertically and M squares horizontally. For each board, each square is painted white or black. The color of the square in the i-th row and j-th column of the board X is written as C (i, j, X). Count how many pairs of integers (i, j) meet the following conditions: * 1 \ leq i \ leq N * 1 \ leq j \ leq M * C (i, j, A) \ neq C (i, j, B) Input format N M A_1_1 ... A_N B_1_1 ... B_N When the jth character of A_i (1 \ leq i \ leq N) is `#`, C (i, j, A) is black, and when it is `.`, C (i, j, A) is white. Represents that. The same is true for B. Constraint * 1 \ leq N, M \ leq 2,000 * | A_i | = | B_i | = M * A_i and B_i are strings consisting only of `#` and `.` Output format Print the answer on one line. Input example 1 twenty three .. # .. .## .. Output example 1 2 Input example 2 7 28 ............................ ... # ..... ### ... #### .... ### .. .. #. # ... # ... # ... # ... # .... # ... #. . # ... # ... # ...... #### ... # ..... . ##### .. # ... # .. # ...... # ... #. . # ... # ... ### ... # ....... ### .. ............................ ............................ .. ### .... ### ..... ## .... ### .. . # ... # ... # ... # ... #. # ... # ... #. ...##...#...# ..... # .... ####. .. # ..... # ... # ..... # ....... #. . ##### ... ### .... #### ... ### .. ............................ Output example 2 40 Example Input 2 3 ..# ##. .## #.. Output 2
instruction
0
87,490
7
174,980
"Correct Solution: ``` N, M = map(int, input().split()) A = [input() for _ in range(N)] B = [input() for _ in range(N)] ans = 0 for a, b in zip(A, B): for aa, bb in zip(a, b): if aa != bb: ans += 1 print(ans) ```
output
1
87,490
7
174,981
Provide a correct Python 3 solution for this coding contest problem. A: Find the difference problem Given rectangular boards A and B with N squares vertically and M squares horizontally. For each board, each square is painted white or black. The color of the square in the i-th row and j-th column of the board X is written as C (i, j, X). Count how many pairs of integers (i, j) meet the following conditions: * 1 \ leq i \ leq N * 1 \ leq j \ leq M * C (i, j, A) \ neq C (i, j, B) Input format N M A_1_1 ... A_N B_1_1 ... B_N When the jth character of A_i (1 \ leq i \ leq N) is `#`, C (i, j, A) is black, and when it is `.`, C (i, j, A) is white. Represents that. The same is true for B. Constraint * 1 \ leq N, M \ leq 2,000 * | A_i | = | B_i | = M * A_i and B_i are strings consisting only of `#` and `.` Output format Print the answer on one line. Input example 1 twenty three .. # .. .## .. Output example 1 2 Input example 2 7 28 ............................ ... # ..... ### ... #### .... ### .. .. #. # ... # ... # ... # ... # .... # ... #. . # ... # ... # ...... #### ... # ..... . ##### .. # ... # .. # ...... # ... #. . # ... # ... ### ... # ....... ### .. ............................ ............................ .. ### .... ### ..... ## .... ### .. . # ... # ... # ... # ... #. # ... # ... #. ...##...#...# ..... # .... ####. .. # ..... # ... # ..... # ....... #. . ##### ... ### .... #### ... ### .. ............................ Output example 2 40 Example Input 2 3 ..# ##. .## #.. Output 2
instruction
0
87,491
7
174,982
"Correct Solution: ``` #!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def solve(): N, M = LI() A = SR(N) B = SR(N) ans = 0 for i in range(N): for j in range(M): if A[i][j] != B[i][j]: ans += 1 print(ans) return if __name__ == "__main__": solve() ```
output
1
87,491
7
174,983
Provide a correct Python 3 solution for this coding contest problem. A: Find the difference problem Given rectangular boards A and B with N squares vertically and M squares horizontally. For each board, each square is painted white or black. The color of the square in the i-th row and j-th column of the board X is written as C (i, j, X). Count how many pairs of integers (i, j) meet the following conditions: * 1 \ leq i \ leq N * 1 \ leq j \ leq M * C (i, j, A) \ neq C (i, j, B) Input format N M A_1_1 ... A_N B_1_1 ... B_N When the jth character of A_i (1 \ leq i \ leq N) is `#`, C (i, j, A) is black, and when it is `.`, C (i, j, A) is white. Represents that. The same is true for B. Constraint * 1 \ leq N, M \ leq 2,000 * | A_i | = | B_i | = M * A_i and B_i are strings consisting only of `#` and `.` Output format Print the answer on one line. Input example 1 twenty three .. # .. .## .. Output example 1 2 Input example 2 7 28 ............................ ... # ..... ### ... #### .... ### .. .. #. # ... # ... # ... # ... # .... # ... #. . # ... # ... # ...... #### ... # ..... . ##### .. # ... # .. # ...... # ... #. . # ... # ... ### ... # ....... ### .. ............................ ............................ .. ### .... ### ..... ## .... ### .. . # ... # ... # ... # ... #. # ... # ... #. ...##...#...# ..... # .... ####. .. # ..... # ... # ..... # ....... #. . ##### ... ### .... #### ... ### .. ............................ Output example 2 40 Example Input 2 3 ..# ##. .## #.. Output 2
instruction
0
87,492
7
174,984
"Correct Solution: ``` N, M = map(int, input().split()) table1 = '' table2 = '' for i in range(2 * N): if i < N: table1 += input() else: table2 += input() count = 0 for i, j in zip(table1, table2): if i != j: count+=1 print(count) ```
output
1
87,492
7
174,985
Provide a correct Python 3 solution for this coding contest problem. A: Find the difference problem Given rectangular boards A and B with N squares vertically and M squares horizontally. For each board, each square is painted white or black. The color of the square in the i-th row and j-th column of the board X is written as C (i, j, X). Count how many pairs of integers (i, j) meet the following conditions: * 1 \ leq i \ leq N * 1 \ leq j \ leq M * C (i, j, A) \ neq C (i, j, B) Input format N M A_1_1 ... A_N B_1_1 ... B_N When the jth character of A_i (1 \ leq i \ leq N) is `#`, C (i, j, A) is black, and when it is `.`, C (i, j, A) is white. Represents that. The same is true for B. Constraint * 1 \ leq N, M \ leq 2,000 * | A_i | = | B_i | = M * A_i and B_i are strings consisting only of `#` and `.` Output format Print the answer on one line. Input example 1 twenty three .. # .. .## .. Output example 1 2 Input example 2 7 28 ............................ ... # ..... ### ... #### .... ### .. .. #. # ... # ... # ... # ... # .... # ... #. . # ... # ... # ...... #### ... # ..... . ##### .. # ... # .. # ...... # ... #. . # ... # ... ### ... # ....... ### .. ............................ ............................ .. ### .... ### ..... ## .... ### .. . # ... # ... # ... # ... #. # ... # ... #. ...##...#...# ..... # .... ####. .. # ..... # ... # ..... # ....... #. . ##### ... ### .... #### ... ### .. ............................ Output example 2 40 Example Input 2 3 ..# ##. .## #.. Output 2
instruction
0
87,493
7
174,986
"Correct Solution: ``` n,m = map(int,input().split()) a = [list(list(input())) for i in range(n)] b = [list(list(input())) for i in range(n)] cout = 0 for i in range(n): for j in range(m): if a[i][j] != b[i][j]: cout += 1 print(cout) ```
output
1
87,493
7
174,987
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
instruction
0
87,685
7
175,370
Tags: dp, greedy, math Correct Solution: ``` input() a=[*map(int, input().split())] b=sum(e//2 for e in a[::2])+sum((e+1)//2 for e in a[1::2]) print(min(sum(a)-b,b)) ```
output
1
87,685
7
175,371
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
instruction
0
87,686
7
175,372
Tags: dp, greedy, math Correct Solution: ``` # ---------------------------iye ha aam zindegi--------------------------------------------- import math import random import heapq, bisect import sys from collections import deque, defaultdict from fractions import Fraction import sys # import threading from math import inf, log2 from collections import defaultdict # threading.stack_size(10**8) mod = 10 ** 9 + 7 mod1 = 998244353 # ------------------------------warmup---------------------------- import os import sys from io import BytesIO, IOBase # sys.setrecursionlimit(300000) BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #---------------------------------------------------------------------------------------------------------------- class LazySegmentTree: def __init__(self, array, func=max): self.n = len(array) self.size = 2**(int(log2(self.n-1))+1) if self.n != 1 else 1 self.func = func self.default = 0 if self.func != min else inf self.data = [self.default] * (2 * self.size) self.lazy = [0] * (2 * self.size) self.process(array) def process(self, array): self.data[self.size : self.size+self.n] = array for i in range(self.size-1, -1, -1): self.data[i] = self.func(self.data[2*i], self.data[2*i+1]) def push(self, index): """Push the information of the root to it's children!""" self.lazy[2*index] += self.lazy[index] self.lazy[2*index+1] += self.lazy[index] self.data[2 * index] += self.lazy[index] self.data[2 * index + 1] += self.lazy[index] self.lazy[index] = 0 def build(self, index): """Build data with the new changes!""" index >>= 1 while index: self.data[index] = self.func(self.data[2*index], self.data[2*index+1]) + self.lazy[index] index >>= 1 def query(self, alpha, omega): """Returns the result of function over the range (inclusive)!""" res = self.default alpha += self.size omega += self.size + 1 for i in range(len(bin(alpha)[2:])-1, 0, -1): self.push(alpha >> i) for i in range(len(bin(omega-1)[2:])-1, 0, -1): self.push((omega-1) >> i) while alpha < omega: if alpha & 1: res = self.func(res, self.data[alpha]) alpha += 1 if omega & 1: omega -= 1 res = self.func(res, self.data[omega]) alpha >>= 1 omega >>= 1 return res def update(self, alpha, omega, value): """Increases all elements in the range (inclusive) by given value!""" alpha += self.size omega += self.size + 1 l, r = alpha, omega while alpha < omega: if alpha & 1: self.data[alpha] += value self.lazy[alpha] += value alpha += 1 if omega & 1: omega -= 1 self.data[omega] += value self.lazy[omega] += value alpha >>= 1 omega >>= 1 self.build(l) self.build(r-1) #--------------------------------------------------------------------------------------------- n=int(input()) l=list(map(int,input().split())) b=0 w=0 for i in range(n): if i%2==0: b+=int(math.ceil(l[i]/2)) w+=l[i]//2 else: w += int(math.ceil(l[i]/2)) b += l[i] // 2 print(min(b,w)) ```
output
1
87,686
7
175,373
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
instruction
0
87,687
7
175,374
Tags: dp, greedy, math Correct Solution: ``` # 20200118 19:26 ~ 19:47 ~ 20:20 n = int(input()) arr = list(map(int, input().split())) ans_1 = 0 ans_2 = 0 for i in range(n): if i%2 == 1: ans_1+=int(arr[i]/2) + arr[i]%2 ans_2+=int(arr[i]/2) else: ans_1+=int(arr[i]/2) ans_2+=int(arr[i]/2) + arr[i]%2 print(min(ans_1, ans_2)) ```
output
1
87,687
7
175,375
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
instruction
0
87,688
7
175,376
Tags: dp, greedy, math Correct Solution: ``` n=int(input()) it=list(map(int,input().split())) a=0 b=0 for i in range(n): if i%2==0: a+=it[i]//2 b+=it[i]//2 a+=it[i]%2 else: b+=it[i]//2 a+=it[i]//2 b+=it[i]%2 print(min(a,b)) ```
output
1
87,688
7
175,377
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
instruction
0
87,689
7
175,378
Tags: dp, greedy, math Correct Solution: ``` n = int(input()) l = list(map(int, input().split())) ch = 0 b = 0 for i in range(n): if l[i] % 2 == 0: ch += l[i] // 2 b += l[i] // 2 else: b += l[i] // 2 ch += l[i] // 2 if i % 2 == 0: b += 1 else: ch += 1 print(min(ch, b)) ```
output
1
87,689
7
175,379
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
instruction
0
87,690
7
175,380
Tags: dp, greedy, math Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) b,c=0,0 for i in range(n): if i%2==1: b+=a[i]//2 c+=a[i]//2+a[i]%2 else: c+=a[i]//2 b+=a[i]//2+a[i]%2 print(min(b,c)) ```
output
1
87,690
7
175,381
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
instruction
0
87,691
7
175,382
Tags: dp, greedy, math Correct Solution: ``` import sys n = int(sys.stdin.readline().strip()) a = list(map(int,sys.stdin.readline().strip().split())) d = 0 s = 0 for i in range (0, n): s = s + a[i] if i % 2 == 0: d = d + a[i] % 2 else: d = d - a[i] % 2 print((s - abs(d)) // 2) ```
output
1
87,691
7
175,383
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
instruction
0
87,692
7
175,384
Tags: dp, greedy, math Correct Solution: ``` n = int(input()) a = list(map(int,input().split())) counts = [0]*2 for i in range(n): counts[i&1] += a[i]//2 #use i&1 so it alternates since coloring as checkerboard counts[i&1^1] += (a[i]+1)//2 print(min(counts)) ```
output
1
87,692
7
175,385
Provide tags and a correct Python 3 solution for this coding contest problem. There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1). You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅. In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation. Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on. You have to compute m_i for all 1 ≤ i ≤ n. Input The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5). The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1). The description of each one of the k subsets follows, in the following format: The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset. The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset. It is guaranteed that: * The intersection of any three subsets is empty; * It's possible to make all lamps be simultaneously on using some operations. Output You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on. Examples Input 7 3 0011100 3 1 4 6 3 3 4 7 2 2 3 Output 1 2 3 3 3 3 3 Input 8 6 00110011 3 1 3 8 5 1 2 5 6 7 2 6 8 2 3 5 2 4 7 1 2 Output 1 1 1 1 1 1 4 4 Input 5 3 00011 3 1 2 3 1 4 3 3 4 5 Output 1 1 1 1 1 Input 19 5 1001001001100000110 2 2 3 2 5 6 2 8 9 5 12 13 14 15 16 1 19 Output 0 1 1 1 2 2 2 3 3 3 3 4 4 4 4 4 4 4 5 Note In the first example: * For i = 1, we can just apply one operation on A_1, the final states will be 1010110; * For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110; * For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111. In the second example: * For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101; * For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
instruction
0
87,701
7
175,402
Tags: dfs and similar, dsu, graphs Correct Solution: ``` import sys readline = sys.stdin.readline class UF(): def __init__(self, num): self.par = [-1]*num self.weight = [0]*num def find(self, x): if self.par[x] < 0: return x else: stack = [] while self.par[x] >= 0: stack.append(x) x = self.par[x] for xi in stack: self.par[xi] = x return x def union(self, x, y): rx = self.find(x) ry = self.find(y) if rx != ry: if self.par[rx] > self.par[ry]: rx, ry = ry, rx self.par[rx] += self.par[ry] self.par[ry] = rx self.weight[rx] += self.weight[ry] return rx N, K = map(int, readline().split()) S = list(map(int, readline().strip())) A = [[] for _ in range(N)] for k in range(K): BL = int(readline()) B = list(map(int, readline().split())) for b in B: A[b-1].append(k) cnt = 0 T = UF(2*K) used = set() Ans = [None]*N inf = 10**9+7 for i in range(N): if not len(A[i]): Ans[i] = cnt continue kk = 0 if len(A[i]) == 2: x, y = A[i] if S[i]: rx = T.find(x) ry = T.find(y) if rx != ry: rx2 = T.find(x+K) ry2 = T.find(y+K) sp = min(T.weight[rx], T.weight[rx2]) + min(T.weight[ry], T.weight[ry2]) if x not in used: used.add(x) T.weight[rx] += 1 if y not in used: used.add(y) T.weight[ry] += 1 rz = T.union(rx, ry) rz2 = T.union(rx2, ry2) sf = min(T.weight[rz], T.weight[rz2]) kk = sf - sp else: rx = T.find(x) ry2 = T.find(y+K) sp = 0 if rx != ry2: ry = T.find(y) rx2 = T.find(x+K) sp = min(T.weight[rx], T.weight[rx2]) + min(T.weight[ry], T.weight[ry2]) if x not in used: used.add(x) T.weight[rx] += 1 if y not in used: used.add(y) T.weight[ry] += 1 rz = T.union(rx, ry2) rz2 = T.union(rx2, ry) sf = min(T.weight[rz], T.weight[rz2]) kk = sf - sp else: if S[i]: x = A[i][0] rx = T.find(x) rx2 = T.find(x+K) sp = min(T.weight[rx], T.weight[rx2]) T.weight[rx] += inf sf = min(T.weight[rx], T.weight[rx2]) kk = sf - sp else: x = A[i][0] rx = T.find(x) rx2 = T.find(x+K) sp = min(T.weight[rx], T.weight[rx2]) T.weight[rx2] += inf if x not in used: used.add(x) T.weight[rx] += 1 sf = min(T.weight[rx], T.weight[rx2]) kk = sf-sp Ans[i] = cnt + kk cnt = Ans[i] print('\n'.join(map(str, Ans))) ```
output
1
87,701
7
175,403
Provide tags and a correct Python 3 solution for this coding contest problem. There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1). You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅. In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation. Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on. You have to compute m_i for all 1 ≤ i ≤ n. Input The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5). The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1). The description of each one of the k subsets follows, in the following format: The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset. The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset. It is guaranteed that: * The intersection of any three subsets is empty; * It's possible to make all lamps be simultaneously on using some operations. Output You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on. Examples Input 7 3 0011100 3 1 4 6 3 3 4 7 2 2 3 Output 1 2 3 3 3 3 3 Input 8 6 00110011 3 1 3 8 5 1 2 5 6 7 2 6 8 2 3 5 2 4 7 1 2 Output 1 1 1 1 1 1 4 4 Input 5 3 00011 3 1 2 3 1 4 3 3 4 5 Output 1 1 1 1 1 Input 19 5 1001001001100000110 2 2 3 2 5 6 2 8 9 5 12 13 14 15 16 1 19 Output 0 1 1 1 2 2 2 3 3 3 3 4 4 4 4 4 4 4 5 Note In the first example: * For i = 1, we can just apply one operation on A_1, the final states will be 1010110; * For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110; * For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111. In the second example: * For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101; * For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
instruction
0
87,702
7
175,404
Tags: dfs and similar, dsu, graphs Correct Solution: ``` import sys def find(x): if x == pre[x]: return x else: pre[x]=find(pre[x]) return pre[x] def merge(x,y): x = find(x) y = find(y) if x != y: pre[x] = y siz[y] +=siz[x] def cal(x): return min(siz[find(x)],siz[find(x+k)]) def Solve(i): global ans cant = col[i][0] if cant == 2: x = col[i][1] y = col[i][2] if S[i] == 1: if find(x) == find(y): return ans -=cal(x) + cal(y) merge(x,y) merge(x+k,y+k) ans +=cal(x) else: if find(x) == find(y+k): return ans -=cal(x)+cal(y) merge(x,y+k) merge(x+k,y) ans +=cal(x) elif cant == 1: x = col[i][1] if S[i] == 1: if find(x) == find(0): return ans -=cal(x) merge(x,0) ans +=cal(x) else: if find(x+k) == find(0): return ans -=cal(x) merge(x+k,0) ans +=cal(x) n,k = map(int,input().split()) S = [1]+list(map(int,list(sys.stdin.readline().strip()))) col = [[0 for _ in range(3)] for _ in range(n+2)] pre = [i for i in range(k*2+1)] siz = [0 for _ in range(k*2+1)] ans = 0 for i in range(1,k+1): c = sys.stdin.readline() c = int(c) conjunto = [1]+list(map(int,list(sys.stdin.readline().split()))) for j in range(1,len(conjunto)): x = conjunto[j] col[x][0] = col[x][0]+1 col[x][col[x][0]] = i for i in range(1,k+1): siz[i]=1 siz[0]=3*10e5 for i in range(1,n+1): Solve(i) print(ans) ```
output
1
87,702
7
175,405
Provide tags and a correct Python 3 solution for this coding contest problem. There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1). You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅. In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation. Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on. You have to compute m_i for all 1 ≤ i ≤ n. Input The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5). The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1). The description of each one of the k subsets follows, in the following format: The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset. The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset. It is guaranteed that: * The intersection of any three subsets is empty; * It's possible to make all lamps be simultaneously on using some operations. Output You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on. Examples Input 7 3 0011100 3 1 4 6 3 3 4 7 2 2 3 Output 1 2 3 3 3 3 3 Input 8 6 00110011 3 1 3 8 5 1 2 5 6 7 2 6 8 2 3 5 2 4 7 1 2 Output 1 1 1 1 1 1 4 4 Input 5 3 00011 3 1 2 3 1 4 3 3 4 5 Output 1 1 1 1 1 Input 19 5 1001001001100000110 2 2 3 2 5 6 2 8 9 5 12 13 14 15 16 1 19 Output 0 1 1 1 2 2 2 3 3 3 3 4 4 4 4 4 4 4 5 Note In the first example: * For i = 1, we can just apply one operation on A_1, the final states will be 1010110; * For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110; * For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111. In the second example: * For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101; * For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
instruction
0
87,703
7
175,406
Tags: dfs and similar, dsu, graphs Correct Solution: ``` from sys import stdin class disjoinSet(object): def __init__(self,n): self.father = [x for x in range(0,n+1)] self.rank = [0 for x in range(0,n+1)] def setOf(self, x): if(self.father[x] != x): self.father[x] = self.setOf(self.father[x]) return self.father[x] def Merge(self,x,y): xR = self.setOf(x) yR = self.setOf(y) if(xR == yR): return if self.rank[xR] < self.rank[yR]: self.father[xR] = yR size[yR] += size[xR] elif self.rank[xR] > self.rank[yR]: self.father[yR] = xR size[xR] += size[yR] else: self.father[yR] = xR size[xR] += size[yR] self.rank[xR] +=1 def cal(x): return min(size[dsu.setOf(x)],size[dsu.setOf(x+k)]) def Solve(i): global ans cant = col[i][0] if cant == 2: x = col[i][1] y = col[i][2] if S[i] == 1: if dsu.setOf(x) == dsu.setOf(y): return ans -=cal(x) + cal(y) dsu.Merge(x,y) dsu.Merge(x+k,y+k) ans +=cal(y) else: if dsu.setOf(x) == dsu.setOf(y+k): return ans -=cal(x)+cal(y) dsu.Merge(x,y+k) dsu.Merge(x+k,y) ans +=cal(y) elif cant == 1: x = col[i][1] if S[i] == 1: if dsu.setOf(x) == dsu.setOf(0): return ans -=cal(x) dsu.Merge(x,0) ans +=cal(x) else: if dsu.setOf(x+k) == dsu.setOf(0): return ans -=cal(x) dsu.Merge(x+k,0) ans +=cal(x) n,k = map(int,input().split()) S = [1]+list(map(int,list(stdin.readline().strip()))) dsu = disjoinSet(k*2+1) col = [[0 for _ in range(3)] for _ in range(n+2)] size = [0 for _ in range(k*2+1)] ans = 0 for i in range(1,k+1): c = stdin.readline() c = int(c) conjunto = [1]+list(map(int,list(stdin.readline().split()))) for j in range(1,len(conjunto)): x = conjunto[j] col[x][0] = col[x][0]+1 col[x][col[x][0]] = i for i in range(1,k+1): size[i]=1 size[0]=3*10e5 for i in range(1,n+1): Solve(i) print(ans) ```
output
1
87,703
7
175,407
Provide tags and a correct Python 3 solution for this coding contest problem. There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1). You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅. In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation. Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on. You have to compute m_i for all 1 ≤ i ≤ n. Input The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5). The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1). The description of each one of the k subsets follows, in the following format: The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset. The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset. It is guaranteed that: * The intersection of any three subsets is empty; * It's possible to make all lamps be simultaneously on using some operations. Output You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on. Examples Input 7 3 0011100 3 1 4 6 3 3 4 7 2 2 3 Output 1 2 3 3 3 3 3 Input 8 6 00110011 3 1 3 8 5 1 2 5 6 7 2 6 8 2 3 5 2 4 7 1 2 Output 1 1 1 1 1 1 4 4 Input 5 3 00011 3 1 2 3 1 4 3 3 4 5 Output 1 1 1 1 1 Input 19 5 1001001001100000110 2 2 3 2 5 6 2 8 9 5 12 13 14 15 16 1 19 Output 0 1 1 1 2 2 2 3 3 3 3 4 4 4 4 4 4 4 5 Note In the first example: * For i = 1, we can just apply one operation on A_1, the final states will be 1010110; * For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110; * For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111. In the second example: * For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101; * For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
instruction
0
87,704
7
175,408
Tags: dfs and similar, dsu, graphs Correct Solution: ``` from sys import stdin input = stdin.readline n , k = [int(i) for i in input().split()] pairs = [i + k for i in range(k)] + [i for i in range(k)] initial_condition = list(map(lambda x: x == '1',input().strip())) data = [i for i in range(2*k)] constrain = [-1] * (2*k) h = [0] * (2*k) L = [1] * k + [0] * k dp1 = [-1 for i in range(n)] dp2 = [-1 for i in range(n)] for i in range(k): input() inp = [int(j) for j in input().split()] for s in inp: if dp1[s-1] == -1:dp1[s-1] = i else:dp2[s-1] = i pfsums = 0 ans = [] def remove_pfsum(s1): global pfsums if constrain[s1] == 1: pfsums -= L[s1] elif constrain[pairs[s1]] == 1: pfsums -= L[pairs[s1]] else: pfsums -= min(L[s1],L[pairs[s1]]) def sh(i): while i != data[i]: i = data[i] return i def upd_pfsum(s1): global pfsums if constrain[s1] == 1: pfsums += L[s1] elif constrain[pairs[s1]] == 1: pfsums += L[pairs[s1]] else: pfsums += min(L[s1],L[pairs[s1]]) def ms(i,j): i = sh(i) ; j = sh(j) cons = max(constrain[i],constrain[j]) if h[i] < h[j]: data[i] = j L[j] += L[i] constrain[j] = cons return j else: data[j] = i if h[i] == h[j]: h[i] += 1 L[i] += L[j] constrain[i] = cons return i for i in range(n): if dp1[i] == -1 and dp2[i] == -1: pass elif dp2[i] == -1: s1 = sh(dp1[i]) remove_pfsum(s1) constrain[s1] = 0 if initial_condition[i] else 1 constrain[pairs[s1]] = 1 if initial_condition[i] else 0 upd_pfsum(s1) else: s1 = sh(dp1[i]) ; s2 = sh(dp2[i]) if s1 == s2 or pairs[s1] == s2: pass else: remove_pfsum(s1) remove_pfsum(s2) if initial_condition[i]: new_s1 = ms(s1,s2) new_s2 = ms(pairs[s1],pairs[s2]) else: new_s1 = ms(s1,pairs[s2]) new_s2 = ms(pairs[s1],s2) pairs[new_s1] = new_s2 pairs[new_s2] = new_s1 upd_pfsum(new_s1) ans.append(pfsums) for i in ans: print(i) ```
output
1
87,704
7
175,409
Provide tags and a correct Python 3 solution for this coding contest problem. There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1). You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅. In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation. Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on. You have to compute m_i for all 1 ≤ i ≤ n. Input The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5). The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1). The description of each one of the k subsets follows, in the following format: The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset. The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset. It is guaranteed that: * The intersection of any three subsets is empty; * It's possible to make all lamps be simultaneously on using some operations. Output You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on. Examples Input 7 3 0011100 3 1 4 6 3 3 4 7 2 2 3 Output 1 2 3 3 3 3 3 Input 8 6 00110011 3 1 3 8 5 1 2 5 6 7 2 6 8 2 3 5 2 4 7 1 2 Output 1 1 1 1 1 1 4 4 Input 5 3 00011 3 1 2 3 1 4 3 3 4 5 Output 1 1 1 1 1 Input 19 5 1001001001100000110 2 2 3 2 5 6 2 8 9 5 12 13 14 15 16 1 19 Output 0 1 1 1 2 2 2 3 3 3 3 4 4 4 4 4 4 4 5 Note In the first example: * For i = 1, we can just apply one operation on A_1, the final states will be 1010110; * For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110; * For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111. In the second example: * For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101; * For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
instruction
0
87,705
7
175,410
Tags: dfs and similar, dsu, graphs Correct Solution: ``` import sys readline = sys.stdin.readline class UF(): def __init__(self, num): self.par = [-1]*num self.weight = [0]*num def find(self, x): stack = [] while self.par[x] >= 0: stack.append(x) x = self.par[x] for xi in stack: self.par[xi] = x return x def union(self, x, y): rx = self.find(x) ry = self.find(y) if rx != ry: if self.par[rx] > self.par[ry]: rx, ry = ry, rx self.par[rx] += self.par[ry] self.par[ry] = rx self.weight[rx] += self.weight[ry] return rx N, K = map(int, readline().split()) S = list(map(int, readline().strip())) A = [[] for _ in range(N)] for k in range(K): BL = int(readline()) B = list(map(int, readline().split())) for b in B: A[b-1].append(k) cnt = 0 T = UF(2*K) used = set() Ans = [None]*N inf = 10**9+7 for i in range(N): if not len(A[i]): Ans[i] = cnt continue kk = 0 if len(A[i]) == 2: x, y = A[i] if S[i]: rx = T.find(x) ry = T.find(y) if rx != ry: rx2 = T.find(x+K) ry2 = T.find(y+K) sp = min(T.weight[rx], T.weight[rx2]) + min(T.weight[ry], T.weight[ry2]) if x not in used: used.add(x) T.weight[rx] += 1 if y not in used: used.add(y) T.weight[ry] += 1 rz = T.union(rx, ry) rz2 = T.union(rx2, ry2) sf = min(T.weight[rz], T.weight[rz2]) kk = sf - sp else: rx = T.find(x) ry2 = T.find(y+K) sp = 0 if rx != ry2: ry = T.find(y) rx2 = T.find(x+K) sp = min(T.weight[rx], T.weight[rx2]) + min(T.weight[ry], T.weight[ry2]) if x not in used: used.add(x) T.weight[rx] += 1 if y not in used: used.add(y) T.weight[ry] += 1 rz = T.union(rx, ry2) rz2 = T.union(rx2, ry) sf = min(T.weight[rz], T.weight[rz2]) kk = sf - sp else: if S[i]: x = A[i][0] rx = T.find(x) rx2 = T.find(x+K) sp = min(T.weight[rx], T.weight[rx2]) T.weight[rx] += inf sf = min(T.weight[rx], T.weight[rx2]) kk = sf - sp else: x = A[i][0] rx = T.find(x) rx2 = T.find(x+K) sp = min(T.weight[rx], T.weight[rx2]) T.weight[rx2] += inf if x not in used: used.add(x) T.weight[rx] += 1 sf = min(T.weight[rx], T.weight[rx2]) kk = sf-sp Ans[i] = cnt + kk cnt = Ans[i] print('\n'.join(map(str, Ans))) ```
output
1
87,705
7
175,411
Provide tags and a correct Python 3 solution for this coding contest problem. There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1). You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅. In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation. Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on. You have to compute m_i for all 1 ≤ i ≤ n. Input The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5). The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1). The description of each one of the k subsets follows, in the following format: The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset. The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset. It is guaranteed that: * The intersection of any three subsets is empty; * It's possible to make all lamps be simultaneously on using some operations. Output You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on. Examples Input 7 3 0011100 3 1 4 6 3 3 4 7 2 2 3 Output 1 2 3 3 3 3 3 Input 8 6 00110011 3 1 3 8 5 1 2 5 6 7 2 6 8 2 3 5 2 4 7 1 2 Output 1 1 1 1 1 1 4 4 Input 5 3 00011 3 1 2 3 1 4 3 3 4 5 Output 1 1 1 1 1 Input 19 5 1001001001100000110 2 2 3 2 5 6 2 8 9 5 12 13 14 15 16 1 19 Output 0 1 1 1 2 2 2 3 3 3 3 4 4 4 4 4 4 4 5 Note In the first example: * For i = 1, we can just apply one operation on A_1, the final states will be 1010110; * For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110; * For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111. In the second example: * For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101; * For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
instruction
0
87,706
7
175,412
Tags: dfs and similar, dsu, graphs Correct Solution: ``` import sys input = sys.stdin.readline n,k=map(int,input().split()) S=[1]+list(map(int,list(input().strip()))) C=[] for i in range(k): c=int(input()) C.append(list(map(int,input().split()))) NUM=[[] for i in range(n+1)] for i in range(k): for c in C[i]: NUM[c].append(i) COLORS=[-1]*k EDGE0=[[] for i in range(k)] EDGE1=[[] for i in range(k)] for i in range(1,n+1): if len(NUM[i])==1: if S[i]==0: COLORS[NUM[i][0]]=1 else: COLORS[NUM[i][0]]=0 elif len(NUM[i])==2: x,y=NUM[i] if S[i]==0: EDGE0[x].append(y) EDGE0[y].append(x) else: EDGE1[x].append(y) EDGE1[y].append(x) Q=[i for i in range(k) if COLORS[i]!=-1] while Q: x=Q.pop() for to in EDGE0[x]: if COLORS[to]==-1: COLORS[to]=1-COLORS[x] Q.append(to) for to in EDGE1[x]: if COLORS[to]==-1: COLORS[to]=COLORS[x] Q.append(to) for i in range(k): if COLORS[i]==-1: COLORS[i]=0 Q=[i] while Q: x=Q.pop() for to in EDGE0[x]: if COLORS[to]==-1: COLORS[to]=1-COLORS[x] Q.append(to) for to in EDGE1[x]: if COLORS[to]==-1: COLORS[to]=COLORS[x] Q.append(to) #print(COLORS) Group = [i for i in range(k)] W0 = [0]*(k) W1 = [0]*(k) for i in range(k): if COLORS[i]==0: W0[i]=1 else: W1[i]=1 def find(x): while Group[x] != x: x=Group[x] return x def Union(x,y): if find(x) != find(y): if W0[find(x)] + W1[find(x)] < W0[find(y)] + W1[find(y)]: W0[find(y)] += W0[find(x)] W0[find(x)] =0 W1[find(y)] += W1[find(x)] W1[find(x)] =0 Group[find(x)] =find(y) else: W0[find(x)] += W0[find(y)] W0[find(y)] =0 W1[find(x)] += W1[find(y)] W1[find(y)] =0 Group[find(y)] =find(x) ANS=0 SCORES=[0]*(k) for i in range(1,n+1): if len(NUM[i])==0: True elif len(NUM[i])==1: x=NUM[i][0] ANS-=SCORES[find(x)] SCORES[find(x)]=0 W0[find(x)]+=1<<31 ANS+=W1[find(x)] SCORES[find(x)]=W1[find(x)] else: x,y=NUM[i] ANS-=SCORES[find(x)] SCORES[find(x)]=0 ANS-=SCORES[find(y)] SCORES[find(y)]=0 if find(x)!=find(y): Union(x,y) SCORES[find(x)]+=min(W0[find(x)],W1[find(x)]) ANS+=min(W0[find(x)],W1[find(x)]) print(ANS) ```
output
1
87,706
7
175,413
Provide tags and a correct Python 3 solution for this coding contest problem. There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1). You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅. In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation. Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on. You have to compute m_i for all 1 ≤ i ≤ n. Input The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5). The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1). The description of each one of the k subsets follows, in the following format: The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset. The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset. It is guaranteed that: * The intersection of any three subsets is empty; * It's possible to make all lamps be simultaneously on using some operations. Output You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on. Examples Input 7 3 0011100 3 1 4 6 3 3 4 7 2 2 3 Output 1 2 3 3 3 3 3 Input 8 6 00110011 3 1 3 8 5 1 2 5 6 7 2 6 8 2 3 5 2 4 7 1 2 Output 1 1 1 1 1 1 4 4 Input 5 3 00011 3 1 2 3 1 4 3 3 4 5 Output 1 1 1 1 1 Input 19 5 1001001001100000110 2 2 3 2 5 6 2 8 9 5 12 13 14 15 16 1 19 Output 0 1 1 1 2 2 2 3 3 3 3 4 4 4 4 4 4 4 5 Note In the first example: * For i = 1, we can just apply one operation on A_1, the final states will be 1010110; * For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110; * For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111. In the second example: * For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101; * For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
instruction
0
87,707
7
175,414
Tags: dfs and similar, dsu, graphs Correct Solution: ``` from sys import stdin class disjoinSet(object): def __init__(self,n): self.father = [x for x in range(0,n+1)] self.rank = [0 for x in range(0,n+1)] def setOf(self, x): if(self.father[x] != x): self.father[x] = self.setOf(self.father[x]) return self.father[x] def Merge(self,x,y): xR = self.setOf(x) yR = self.setOf(y) if(xR == yR): return if self.rank[xR] < self.rank[yR]: self.father[xR] = yR size[yR] += size[xR] elif self.rank[xR] > self.rank[yR]: self.father[yR] = xR size[xR] += size[yR] else: self.father[yR] = xR size[xR] += size[yR] self.rank[xR] +=1 def LessSize(x): return min(size[dsu.setOf(x)],size[dsu.setOf(x+k)]) def Solve(i): global ans cant = col[i][0] if cant == 2: x = col[i][1] y = col[i][2] if S[i] == 1: if dsu.setOf(x) == dsu.setOf(y): return ans -=LessSize(x) + LessSize(y) dsu.Merge(x,y) dsu.Merge(x+k,y+k) ans +=LessSize(y) else: if dsu.setOf(x) == dsu.setOf(y+k): return ans -=LessSize(x)+LessSize(y) dsu.Merge(x,y+k) dsu.Merge(x+k,y) ans +=LessSize(y) elif cant == 1: x = col[i][1] if S[i] == 1: if dsu.setOf(x) == dsu.setOf(0): return ans -=LessSize(x) dsu.Merge(x,0) ans +=LessSize(x) else: if dsu.setOf(x+k) == dsu.setOf(0): return ans -=LessSize(x) dsu.Merge(x+k,0) ans +=LessSize(x) n,k = map(int,input().split()) S = [1]+list(map(int,list(stdin.readline().strip()))) dsu = disjoinSet(k*2+1) col = [[0 for _ in range(3)] for _ in range(n+2)] size = [0 for _ in range(k*2+1)] ans = 0 for i in range(1,k+1): c = stdin.readline() c = int(c) conjunto = [1]+list(map(int,list(stdin.readline().split()))) for j in range(1,len(conjunto)): x = conjunto[j] col[x][0] = col[x][0]+1 col[x][col[x][0]] = i for i in range(1,k+1): size[i]=1 size[0]=3*10e5 for i in range(1,n+1): Solve(i) print(ans) ```
output
1
87,707
7
175,415
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1). You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅. In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation. Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on. You have to compute m_i for all 1 ≤ i ≤ n. Input The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5). The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1). The description of each one of the k subsets follows, in the following format: The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset. The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset. It is guaranteed that: * The intersection of any three subsets is empty; * It's possible to make all lamps be simultaneously on using some operations. Output You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on. Examples Input 7 3 0011100 3 1 4 6 3 3 4 7 2 2 3 Output 1 2 3 3 3 3 3 Input 8 6 00110011 3 1 3 8 5 1 2 5 6 7 2 6 8 2 3 5 2 4 7 1 2 Output 1 1 1 1 1 1 4 4 Input 5 3 00011 3 1 2 3 1 4 3 3 4 5 Output 1 1 1 1 1 Input 19 5 1001001001100000110 2 2 3 2 5 6 2 8 9 5 12 13 14 15 16 1 19 Output 0 1 1 1 2 2 2 3 3 3 3 4 4 4 4 4 4 4 5 Note In the first example: * For i = 1, we can just apply one operation on A_1, the final states will be 1010110; * For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110; * For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111. In the second example: * For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101; * For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111. Submitted Solution: ``` import sys input = sys.stdin.readline n,k=map(int,input().split()) S=(1,)+tuple(map(int,list(input().strip()))) Q=[[] for i in range(n+1)] for i in range(k): m=int(input()) L=sorted(map(int,input().split())) Q[L[0]].append(L) seg_el=1<<((n+1).bit_length()) SEG=[1<<31]*(2*seg_el) def getvalue(n,seg_el): i=n+seg_el ANS=1<<40 ANS=min(SEG[i],ANS) i>>=1 while i!=0: ANS=min(SEG[i],ANS) i>>=1 return ANS def updates(l,r,x): L=l+seg_el R=r+seg_el while L<R: if L & 1: SEG[L]=min(x,SEG[L]) L+=1 if R & 1: R-=1 SEG[R]=min(x,SEG[R]) L>>=1 R>>=1 from functools import lru_cache @lru_cache(maxsize=None) def calc(s,l,ANS): #print(s,l,ANS) updates(0,l,ANS) if l==n+1: return for SET in Q[l]: t=list(s) for j in SET: t[j]=1-t[j] for k in range(l,n): if t[k]==0: nl=k break else: nl=n+1 calc(tuple(t),nl,ANS+1) l=S.index(0) calc(S,l,0) for i in range(1,n+1): print(getvalue(i,seg_el)) ```
instruction
0
87,708
7
175,416
No
output
1
87,708
7
175,417
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1). You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅. In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation. Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on. You have to compute m_i for all 1 ≤ i ≤ n. Input The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5). The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1). The description of each one of the k subsets follows, in the following format: The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset. The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset. It is guaranteed that: * The intersection of any three subsets is empty; * It's possible to make all lamps be simultaneously on using some operations. Output You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on. Examples Input 7 3 0011100 3 1 4 6 3 3 4 7 2 2 3 Output 1 2 3 3 3 3 3 Input 8 6 00110011 3 1 3 8 5 1 2 5 6 7 2 6 8 2 3 5 2 4 7 1 2 Output 1 1 1 1 1 1 4 4 Input 5 3 00011 3 1 2 3 1 4 3 3 4 5 Output 1 1 1 1 1 Input 19 5 1001001001100000110 2 2 3 2 5 6 2 8 9 5 12 13 14 15 16 1 19 Output 0 1 1 1 2 2 2 3 3 3 3 4 4 4 4 4 4 4 5 Note In the first example: * For i = 1, we can just apply one operation on A_1, the final states will be 1010110; * For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110; * For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111. In the second example: * For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101; * For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111. Submitted Solution: ``` from sys import stdin class disjoinSet(object): def __init__(self,n): self.father = [x for x in range(0,n+1)] self.rank = [0 for x in range(0,n+1)] def setOf(self, x): if(self.father[x] != x): self.father[x] = self.setOf(self.father[x]) return self.father[x] def Merge(self,x,y): xR = self.setOf(x) yR = self.setOf(y) if(xR == yR): return if self.rank[xR] < self.rank[yR]: self.father[xR] = yR size[yR] += size[xR] elif self.rank[xR] > self.rank[yR]: self.father[yR] = xR size[xR] += size[yR] else: self.father[yR] = xR size[xR] += size[yR] self.rank[xR] +=1 def LessSize(x): return min(size[dsu.setOf(x)],size[dsu.setOf(x+k)]) def Solve(i): global ans cant = col[i][0] if cant == 2: x = col[i][1] y = col[i][2] if S[i] == 1: if dsu.setOf(x) == dsu.setOf(y): return ans -=LessSize(x) + LessSize(y) dsu.Merge(x,y) dsu.Merge(x+k,y+k) ans +=LessSize(y) else: if dsu.setOf(x) == dsu.setOf(y+k): return ans -=LessSize(x)+LessSize(y) dsu.Merge(x,y+k) dsu.Merge(x+k,y) ans +=LessSize(y) elif cant == 1: x = col[i][1] if S[i] == 1: if dsu.setOf(x) == dsu.setOf(0): return # ans -=LessSize(x) dsu.Merge(x,0) #ans +=LessSize(x) else: if dsu.setOf(x+k) == dsu.setOf(0): return ans -=LessSize(x) dsu.Merge(x+k,0) ans +=LessSize(x) n,k = map(int,input().split()) S = [1]+list(map(int,list(stdin.readline().strip()))) dsu = disjoinSet(k*2+1) col = [[0 for _ in range(3)] for _ in range(n+2)] size = [0 for _ in range(k*2+1)] ans = 0 for i in range(1,k+1): c = stdin.readline() c = int(c) conjunto = [1]+list(map(int,list(stdin.readline().split()))) for j in range(1,len(conjunto)): x = conjunto[j] col[x][0] = col[x][0]+1 col[x][col[x][0]] = i for i in range(1,k+1): size[i]=1 size[0]=3*10e5 for i in range(1,n+1): Solve(i) print(ans) ```
instruction
0
87,709
7
175,418
No
output
1
87,709
7
175,419
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1). You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅. In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation. Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on. You have to compute m_i for all 1 ≤ i ≤ n. Input The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5). The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1). The description of each one of the k subsets follows, in the following format: The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset. The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset. It is guaranteed that: * The intersection of any three subsets is empty; * It's possible to make all lamps be simultaneously on using some operations. Output You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on. Examples Input 7 3 0011100 3 1 4 6 3 3 4 7 2 2 3 Output 1 2 3 3 3 3 3 Input 8 6 00110011 3 1 3 8 5 1 2 5 6 7 2 6 8 2 3 5 2 4 7 1 2 Output 1 1 1 1 1 1 4 4 Input 5 3 00011 3 1 2 3 1 4 3 3 4 5 Output 1 1 1 1 1 Input 19 5 1001001001100000110 2 2 3 2 5 6 2 8 9 5 12 13 14 15 16 1 19 Output 0 1 1 1 2 2 2 3 3 3 3 4 4 4 4 4 4 4 5 Note In the first example: * For i = 1, we can just apply one operation on A_1, the final states will be 1010110; * For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110; * For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111. In the second example: * For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101; * For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111. Submitted Solution: ``` class UnionFindNode: def __init__(self, group_id, tp): self.group_id_ = group_id self.parent_ = None self.rank_ = 1 self.cost = 0 if tp: self.t0num = 0 self.t1num = 1 else: self.t0num = 1 self.t1num = 0 def is_root(self): return not self.parent_ def root(self): parent = self while not parent.is_root(): parent = parent.parent_ self.parent_ = parent return parent def unite(self, unite_node, k): root = self.root() unite_root = unite_node.root() if root.group_id_ == unite_root.group_id_: return 0 tmp = root.cost + unite_root.cost k_root = k.root() if root.rank_ > unite_root.rank_: n_root, child = root, unite_root else: n_root, child = unite_root, root child.parent_ = n_root n_root.rank_ = max(n_root.rank_, child.rank_ + 1) n_root.t0num = n_root.t0num + child.t0num n_root.t1num = n_root.t1num + child.t1num if k_root.group_id_ == n_root.group_id_: r = n_root.t0num else: r = min(n_root.t0num, n_root.t1num) n_root.cost = r return r -tmp if __name__ == "__main__": N, K = map(int, input().split()) S = input().strip() vs = [set() for _ in range(N)] for i in range(K): input() for x in map(int, input().split()): vs[x-1].add(i) es = [] e2i = dict() G = [set() for _ in range(K+1)] for i in range(N): if len(vs[i]) == 1: e = (vs[i].pop(), K) elif len(vs[i]) == 2: e = (vs[i].pop(), vs[i].pop()) else: e = (K, K) # i番目のランプ es.append(e) e2i[e] = int(S[i]) e2i[(e[1], e[0])] = int(S[i]) G[e[0]].add(e[1]) G[e[1]].add(e[0]) v2t = [1] * (K+1) vvs = set() for i in range(K, -1, -1): if i in vvs: continue stack = [i] while stack: v = stack.pop() vvs.add(v) for u in G[v]: if u in vvs: continue if e2i[(v, u)]: v2t[u] = v2t[v] else: v2t[u] = 1 - v2t[v] stack.append(u) node_list = [UnionFindNode(i, v2t[i]) for i in range(K+1)] r = 0 for v, u in es: r += node_list[v].unite(node_list[u], node_list[K]) print(r) ```
instruction
0
87,710
7
175,420
No
output
1
87,710
7
175,421
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1). You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅. In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation. Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on. You have to compute m_i for all 1 ≤ i ≤ n. Input The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5). The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1). The description of each one of the k subsets follows, in the following format: The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset. The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset. It is guaranteed that: * The intersection of any three subsets is empty; * It's possible to make all lamps be simultaneously on using some operations. Output You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on. Examples Input 7 3 0011100 3 1 4 6 3 3 4 7 2 2 3 Output 1 2 3 3 3 3 3 Input 8 6 00110011 3 1 3 8 5 1 2 5 6 7 2 6 8 2 3 5 2 4 7 1 2 Output 1 1 1 1 1 1 4 4 Input 5 3 00011 3 1 2 3 1 4 3 3 4 5 Output 1 1 1 1 1 Input 19 5 1001001001100000110 2 2 3 2 5 6 2 8 9 5 12 13 14 15 16 1 19 Output 0 1 1 1 2 2 2 3 3 3 3 4 4 4 4 4 4 4 5 Note In the first example: * For i = 1, we can just apply one operation on A_1, the final states will be 1010110; * For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110; * For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111. In the second example: * For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101; * For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111. Submitted Solution: ``` import sys def find(x): if x == pre[x]: return x else: return find(pre[x]) def merge(x,y): x = find(x) y = find(y) if x != y: pre[x] = y siz[y] +=siz[x] def cal(x): return min(siz[find(x)],siz[find(x+k)]) def Solve(i): global ans cant = col[i][0] if cant == 2: x = col[i][1] y = col[i][2] if S[i] == 1: if find(x) == find(y): return ans -=cal(x) + cal(y) merge(x,y) merge(x+k,y+k) ans +=cal(x) else: if find(x) == find(y): return ans -=cal(x)+cal(y) merge(x,y+k) merge(x+k,y) ans +=cal(x) elif cant == 1: x = col[i][1] if S[i] == 1: if find(x) == find(0): return ans -=cal(x) merge(x,0) ans +=cal(x) else: if find(x+k) == find(0): return ans -=cal(x) merge(x+k,0) ans +=cal(x) n,k = map(int,input().split()) S = [1]+list(map(int,list(sys.stdin.readline().strip()))) col = [[0 for _ in range(3)] for _ in range(n+2)] pre = [i for i in range(k*2+1)] siz = [0 for _ in range(k*2+1)] ans = 0 for i in range(1,k+1): c = sys.stdin.readline() c = int(c) conjunto = [1]+list(map(int,list(sys.stdin.readline().split()))) for j in range(1,len(conjunto)): x = conjunto[j] col[x][0] = col[x][0]+1 col[x][col[x][0]] = i for i in range(1,k+1): siz[i]=1 siz[0]=3*10e5 for i in range(1,n+1): Solve(i) print(ans) ```
instruction
0
87,711
7
175,422
No
output
1
87,711
7
175,423
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a garland consisting of n lamps. States of the lamps are represented by the string s of length n. The i-th character of the string s_i equals '0' if the i-th lamp is turned off or '1' if the i-th lamp is turned on. You are also given a positive integer k. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called k-periodic if the distance between each pair of adjacent turned on lamps is exactly k. Consider the case k=3. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain k-periodic garland from the given one. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 25~ 000) — the number of test cases. Then t test cases follow. The first line of the test case contains two integers n and k (1 ≤ n ≤ 10^6; 1 ≤ k ≤ n) — the length of s and the required period. The second line of the test case contains the string s consisting of n characters '0' and '1'. It is guaranteed that the sum of n over all test cases does not exceed 10^6 (∑ n ≤ 10^6). Output For each test case, print the answer — the minimum number of moves you need to make to obtain k-periodic garland from the given one. Example Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
instruction
0
87,732
7
175,464
Tags: brute force, dp, greedy Correct Solution: ``` t = int(input()) def dp(l): dp = [[float("INF")]*3 for m in range(len(l)+1)] dp[0][0] = 0 dp[0][1] = 0 c = 0 for m in range(len(l)): x = l[m] c += x dp[m+1][0] = dp[m][0] + x dp[m+1][1] = min(dp[m][0],dp[m][1])+abs(1-x) dp[m+1][2] = min(dp[m][1],dp[m][2])+x return min(dp[-1])-c def main(): import sys input = sys.stdin.readline for i in range(t): n,k = map(int,input().split()) s = list(input()) l = [[] for j in range(k)] count = 0 for j in range(n): if s[j] =="0": l[j%k].append(0) else: count += 1 l[j%k].append(1) ans = count for j in range(k): ans = min(ans,dp(l[j])+count) print(ans) if __name__ =="__main__": main() ```
output
1
87,732
7
175,465
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a garland consisting of n lamps. States of the lamps are represented by the string s of length n. The i-th character of the string s_i equals '0' if the i-th lamp is turned off or '1' if the i-th lamp is turned on. You are also given a positive integer k. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called k-periodic if the distance between each pair of adjacent turned on lamps is exactly k. Consider the case k=3. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain k-periodic garland from the given one. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 25~ 000) — the number of test cases. Then t test cases follow. The first line of the test case contains two integers n and k (1 ≤ n ≤ 10^6; 1 ≤ k ≤ n) — the length of s and the required period. The second line of the test case contains the string s consisting of n characters '0' and '1'. It is guaranteed that the sum of n over all test cases does not exceed 10^6 (∑ n ≤ 10^6). Output For each test case, print the answer — the minimum number of moves you need to make to obtain k-periodic garland from the given one. Example Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
instruction
0
87,733
7
175,466
Tags: brute force, dp, greedy Correct Solution: ``` import os, sys from io import BytesIO, IOBase from types import GeneratorType from bisect import bisect_left, bisect_right from collections import defaultdict as dd, deque as dq, Counter as dc import math, string import heapq as h, time BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): import os self.os = os self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: self.os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #start_time = time.time() def getInt(): return int(input()) def getStrs(): return input().split() def getInts(): return list(map(int,input().split())) def getStr(): return input() def listStr(): return list(input()) def getMat(n): return [getInts() for _ in range(n)] def getBin(): return list(map(int,list(input()))) def isInt(s): return '0' <= s[0] <= '9' def ceil_(a,b): return a//b + (a%b > 0) MOD = 10**9 + 7 """ K-period can be achieved by starting at any index Turn them all off is an option Suppose index i is the first Then either i+K is on (and nothing in between), or nothing else Then either i+2*K is on (and nothing in between), or nothing else etc dp[i] = min number of moves such that i is switched on """ def solve(): N, K = getInts() S = getBin() pref = [0] curr = 0 for s in S: curr += s pref.append(curr) dp = [0 for i in range(N+1)] dp[0] = 10**18 S = [0]+S for i in range(1,N+1): dp[i] = pref[i-1]+(S[i]^1) if i > K: dp[i] = min(dp[i],dp[i-K]+pref[i-1]-pref[i-K]+(S[i]^1)) ans = pref[-1] for i in range(1,N+1): ans = min(ans,dp[i]+pref[-1]-pref[i]) return ans for _ in range(getInt()): print(solve()) #solve() #print(time.time()-start_time) ```
output
1
87,733
7
175,467
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a garland consisting of n lamps. States of the lamps are represented by the string s of length n. The i-th character of the string s_i equals '0' if the i-th lamp is turned off or '1' if the i-th lamp is turned on. You are also given a positive integer k. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called k-periodic if the distance between each pair of adjacent turned on lamps is exactly k. Consider the case k=3. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain k-periodic garland from the given one. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 25~ 000) — the number of test cases. Then t test cases follow. The first line of the test case contains two integers n and k (1 ≤ n ≤ 10^6; 1 ≤ k ≤ n) — the length of s and the required period. The second line of the test case contains the string s consisting of n characters '0' and '1'. It is guaranteed that the sum of n over all test cases does not exceed 10^6 (∑ n ≤ 10^6). Output For each test case, print the answer — the minimum number of moves you need to make to obtain k-periodic garland from the given one. Example Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
instruction
0
87,734
7
175,468
Tags: brute force, dp, greedy Correct Solution: ``` T = int(input()) for kase in range(T): n, k = map(int, input().split()) s = input() one, ans = s.count('1'), n for i in range(0, k): d = 0 for j in range(i, n, k): d = max(0, d-1) if s[j] == '0' else d+1 ans = min(ans, one-d) print(ans) ```
output
1
87,734
7
175,469
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a garland consisting of n lamps. States of the lamps are represented by the string s of length n. The i-th character of the string s_i equals '0' if the i-th lamp is turned off or '1' if the i-th lamp is turned on. You are also given a positive integer k. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called k-periodic if the distance between each pair of adjacent turned on lamps is exactly k. Consider the case k=3. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain k-periodic garland from the given one. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 25~ 000) — the number of test cases. Then t test cases follow. The first line of the test case contains two integers n and k (1 ≤ n ≤ 10^6; 1 ≤ k ≤ n) — the length of s and the required period. The second line of the test case contains the string s consisting of n characters '0' and '1'. It is guaranteed that the sum of n over all test cases does not exceed 10^6 (∑ n ≤ 10^6). Output For each test case, print the answer — the minimum number of moves you need to make to obtain k-periodic garland from the given one. Example Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
instruction
0
87,735
7
175,470
Tags: brute force, dp, greedy Correct Solution: ``` #gaalt sochliya #jald baazi #time out ho raha hai order n hote hue bhi import sys input = lambda: sys.stdin.readline().strip() #input method from shiv_99 t = int(input()) answers = [] for _ in range(t): n, k = [int(i) for i in input().split()] s = input() vals = [0 if i=='0' else 1 for i in s] total_on = sum(vals) ans = n for j in range(k): # breakpoint() counter = j sub_arr = [] # already_on = 0 # num_lamps = 0 # longest_continuous = 0 while(counter<n): sub_arr.append(vals[counter]) counter += k #now look at temp last = sub_arr[0] already_on = sum(sub_arr) compulsory = total_on - already_on if compulsory > ans: continue num_lamps = len(sub_arr) sub_sub_arr = [] s = 0 mult = 1 if last == 1 else -1 for i in range(len(sub_arr)): if last == sub_arr[i]: s += 1 else: sub_sub_arr.append(mult*s) mult = -1*mult s = 1 if i == len(sub_arr)-1: sub_sub_arr.append(mult*s) last = sub_arr[i] #to find the max_sum_of_a_sub_array_in_this_arr curr_best = 0 best = 0 for i in range(len(sub_sub_arr)): if sub_sub_arr[i] > 0: curr_best += sub_sub_arr[i] else: el = sub_sub_arr[i] if curr_best + el < 0: curr_best = 0 else: curr_best = curr_best + el best = max(curr_best, best) option = min(num_lamps - already_on, already_on, already_on - best) ans = min(compulsory + option, ans) answers.append(ans) print(*answers, sep = '\n') # for a in answers: # print(a) ```
output
1
87,735
7
175,471
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a garland consisting of n lamps. States of the lamps are represented by the string s of length n. The i-th character of the string s_i equals '0' if the i-th lamp is turned off or '1' if the i-th lamp is turned on. You are also given a positive integer k. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called k-periodic if the distance between each pair of adjacent turned on lamps is exactly k. Consider the case k=3. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain k-periodic garland from the given one. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 25~ 000) — the number of test cases. Then t test cases follow. The first line of the test case contains two integers n and k (1 ≤ n ≤ 10^6; 1 ≤ k ≤ n) — the length of s and the required period. The second line of the test case contains the string s consisting of n characters '0' and '1'. It is guaranteed that the sum of n over all test cases does not exceed 10^6 (∑ n ≤ 10^6). Output For each test case, print the answer — the minimum number of moves you need to make to obtain k-periodic garland from the given one. Example Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
instruction
0
87,736
7
175,472
Tags: brute force, dp, greedy Correct Solution: ``` T = int(input()) while T != 0: T -= 1 ans = 10 ** 9 n, k = map(int, input().split()) s = input() tot = s.count('1'); for i in range(k): sum = 0; maxn = 0 for j in range(i, n, k): sum = max(0, sum - 1) if s[j] == '0' else sum + 1 maxn = max(maxn, sum) ans = min(ans, tot - maxn) print(ans) ```
output
1
87,736
7
175,473
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a garland consisting of n lamps. States of the lamps are represented by the string s of length n. The i-th character of the string s_i equals '0' if the i-th lamp is turned off or '1' if the i-th lamp is turned on. You are also given a positive integer k. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called k-periodic if the distance between each pair of adjacent turned on lamps is exactly k. Consider the case k=3. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain k-periodic garland from the given one. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 25~ 000) — the number of test cases. Then t test cases follow. The first line of the test case contains two integers n and k (1 ≤ n ≤ 10^6; 1 ≤ k ≤ n) — the length of s and the required period. The second line of the test case contains the string s consisting of n characters '0' and '1'. It is guaranteed that the sum of n over all test cases does not exceed 10^6 (∑ n ≤ 10^6). Output For each test case, print the answer — the minimum number of moves you need to make to obtain k-periodic garland from the given one. Example Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
instruction
0
87,737
7
175,474
Tags: brute force, dp, greedy Correct Solution: ``` t=int(input()) for tt in range(t): global positions global total_ones global n global k global s n,k=[int(x) for x in input().split(' ')] s=input() total_ones=0 for x in s: total_ones+=int(x) positions=[] def changes_req(positions): summ=[] sum_upto=[0] for position in positions: if(s[int(position)]=='1'): summ.append(-1) elif(s[int(position)]=='0'): summ.append(1) else: print('TNP') for i in range(len(summ)): sum_upto.append(summ[i]+sum_upto[-1]) max_upto=[sum_upto[0]] for i in range(1,len(sum_upto)): max_upto.append(max(max_upto[-1],sum_upto[i])) sum_upto.reverse() min_after=[sum_upto[0]] for i in range(1,len(sum_upto)): min_after.append(min(min_after[-1],sum_upto[i])) sum_upto.reverse() min_after.reverse() ans=n for i in range(len(sum_upto)): ans=min(ans,round(min_after[i]-max_upto[i])) return(ans+total_ones) def update_1(positions): for i in range(len(positions)): positions[i]+=1 if(positions[-1]>=n): positions.pop() positions=[] position=0 while (position<n): positions.append(position) position+=k ans=n for i in range(k): ans=min(ans,changes_req(positions)) update_1(positions) print(ans) ```
output
1
87,737
7
175,475
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a garland consisting of n lamps. States of the lamps are represented by the string s of length n. The i-th character of the string s_i equals '0' if the i-th lamp is turned off or '1' if the i-th lamp is turned on. You are also given a positive integer k. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called k-periodic if the distance between each pair of adjacent turned on lamps is exactly k. Consider the case k=3. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain k-periodic garland from the given one. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 25~ 000) — the number of test cases. Then t test cases follow. The first line of the test case contains two integers n and k (1 ≤ n ≤ 10^6; 1 ≤ k ≤ n) — the length of s and the required period. The second line of the test case contains the string s consisting of n characters '0' and '1'. It is guaranteed that the sum of n over all test cases does not exceed 10^6 (∑ n ≤ 10^6). Output For each test case, print the answer — the minimum number of moves you need to make to obtain k-periodic garland from the given one. Example Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
instruction
0
87,738
7
175,476
Tags: brute force, dp, greedy Correct Solution: ``` import sys input = sys.stdin.readline t = int(input()) out = [] for i in range(t): n,k = map(int,input().split()) s = list(input()) count = [0] for j in s: if j == "1": count.append(count[-1]+1) else: count.append(count[-1]) ans = count[-1] dp = [] for j in range(n): cost = count[j] if j >= k: cost = min(cost,dp[j-k]+count[j]-count[j-k+1]) if s[j] =="0": cost += 1 dp.append(cost) ans = min(ans,cost+count[-1]-count[j+1]) out.append(ans) for i in out: print(i) ```
output
1
87,738
7
175,477
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a garland consisting of n lamps. States of the lamps are represented by the string s of length n. The i-th character of the string s_i equals '0' if the i-th lamp is turned off or '1' if the i-th lamp is turned on. You are also given a positive integer k. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called k-periodic if the distance between each pair of adjacent turned on lamps is exactly k. Consider the case k=3. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain k-periodic garland from the given one. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 25~ 000) — the number of test cases. Then t test cases follow. The first line of the test case contains two integers n and k (1 ≤ n ≤ 10^6; 1 ≤ k ≤ n) — the length of s and the required period. The second line of the test case contains the string s consisting of n characters '0' and '1'. It is guaranteed that the sum of n over all test cases does not exceed 10^6 (∑ n ≤ 10^6). Output For each test case, print the answer — the minimum number of moves you need to make to obtain k-periodic garland from the given one. Example Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
instruction
0
87,739
7
175,478
Tags: brute force, dp, greedy Correct Solution: ``` for _ in range(int(input())): n, k = map(int, input().split()) s = input() ones = s.count('1') best = 0 for i in range(k): count = 0 for j in range(i, n, k): if s[j] == '1': count += 1 else: count = max(0, count - 1) best = max(count, best) print(ones - best) ```
output
1
87,739
7
175,479
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a garland consisting of n lamps. States of the lamps are represented by the string s of length n. The i-th character of the string s_i equals '0' if the i-th lamp is turned off or '1' if the i-th lamp is turned on. You are also given a positive integer k. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called k-periodic if the distance between each pair of adjacent turned on lamps is exactly k. Consider the case k=3. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain k-periodic garland from the given one. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 25~ 000) — the number of test cases. Then t test cases follow. The first line of the test case contains two integers n and k (1 ≤ n ≤ 10^6; 1 ≤ k ≤ n) — the length of s and the required period. The second line of the test case contains the string s consisting of n characters '0' and '1'. It is guaranteed that the sum of n over all test cases does not exceed 10^6 (∑ n ≤ 10^6). Output For each test case, print the answer — the minimum number of moves you need to make to obtain k-periodic garland from the given one. Example Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0 Submitted Solution: ``` from sys import stdin, stdout read = stdin.readline T = int(read()) for i in range(T): n, k = map(int, read().split()) s = read()[:-1] ones = sum([c == '1' for c in s]) cnt = [0 for i in range(k)] for i in range(n): if s[i] == '1': cnt[i%k] += 1 ans = ones dp = [[0 for j in range(3)] for i in range(n)] for i in range(n-1, n-k-1, -1): if s[i] == '1': dp[i][0] = 1 dp[i][2] = 1 else: dp[i][1] = 1 for i in range(n-k-1, -1, -1): if s[i] == '1': dp[i][0] = dp[i+k][0] + 1 dp[i][1] = min(dp[i+k][0], dp[i+k][1]) dp[i][2] = min(dp[i+k][1], dp[i+k][2]) + 1 else: dp[i][0] = dp[i+k][0] dp[i][1] = min(dp[i+k][0], dp[i+k][1]) + 1 dp[i][2] = min(dp[i+k][1], dp[i+k][2]) for i in range(k): ans = min(ans, ones - cnt[i] + min(dp[i])) stdout.write(str(ans)+"\n") ```
instruction
0
87,740
7
175,480
Yes
output
1
87,740
7
175,481
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a garland consisting of n lamps. States of the lamps are represented by the string s of length n. The i-th character of the string s_i equals '0' if the i-th lamp is turned off or '1' if the i-th lamp is turned on. You are also given a positive integer k. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called k-periodic if the distance between each pair of adjacent turned on lamps is exactly k. Consider the case k=3. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain k-periodic garland from the given one. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 25~ 000) — the number of test cases. Then t test cases follow. The first line of the test case contains two integers n and k (1 ≤ n ≤ 10^6; 1 ≤ k ≤ n) — the length of s and the required period. The second line of the test case contains the string s consisting of n characters '0' and '1'. It is guaranteed that the sum of n over all test cases does not exceed 10^6 (∑ n ≤ 10^6). Output For each test case, print the answer — the minimum number of moves you need to make to obtain k-periodic garland from the given one. Example Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0 Submitted Solution: ``` from __future__ import division, print_function import sys if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip import os, sys, bisect, copy from collections import defaultdict, Counter, deque #from functools import lru_cache #use @lru_cache(None) if os.path.exists('in.txt'): sys.stdin=open('in.txt','r') if os.path.exists('out.txt'): sys.stdout=open('out.txt', 'w') # def input(): return sys.stdin.readline() def mapi(arg=0): return map(int if arg==0 else str,input().split()) #------------------------------------------------------------------ for _ in range(int(input())): n,k = mapi() a = input().strip() res = float("inf") if n==1: print(0); continue pre = [0]*(n+1) for i in range(n): pre[i+1] = pre[i] if a[i]=="1": pre[i+1]+=1 dp1 =[0]*(n+1) dp2 = [0]*(n+1) ones = lambda x,y: pre[y]-pre[x] for i in range(1,n+1): dp1[i] = pre[i-1] if i-k>=1: dp1[i] = min(dp1[i], dp1[i-k]+ones(i-k,i-1))+ (a[i-1]=="0") for i in range(n,0,-1): dp2[i] =pre[n]-pre[i] if i+k<=n: dp2[i] = min(dp2[i], dp2[i+k]+ones(i,i+k))+ (a[i-1]=="0") for i in range(1,n+1): res = min(res,dp1[i]+dp2[i]-(a[i-1]=="0")) print(max(res,0)) ```
instruction
0
87,741
7
175,482
Yes
output
1
87,741
7
175,483
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a garland consisting of n lamps. States of the lamps are represented by the string s of length n. The i-th character of the string s_i equals '0' if the i-th lamp is turned off or '1' if the i-th lamp is turned on. You are also given a positive integer k. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called k-periodic if the distance between each pair of adjacent turned on lamps is exactly k. Consider the case k=3. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain k-periodic garland from the given one. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 25~ 000) — the number of test cases. Then t test cases follow. The first line of the test case contains two integers n and k (1 ≤ n ≤ 10^6; 1 ≤ k ≤ n) — the length of s and the required period. The second line of the test case contains the string s consisting of n characters '0' and '1'. It is guaranteed that the sum of n over all test cases does not exceed 10^6 (∑ n ≤ 10^6). Output For each test case, print the answer — the minimum number of moves you need to make to obtain k-periodic garland from the given one. Example Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0 Submitted Solution: ``` import sys import heapq import math import bisect def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def rinput(): return map(int, input().split()) def rlinput(): return list(map(int, input().split())) def srlinput(fl=False): return sorted(list(map(int, input().split())) , reverse=fl) def main(): #n = iinput() n, k = rinput() #n, m, k = rinput() lam = [int(i) for i in list(input())] lamrev = lam[::-1] q, qrev = [0], [0] w, wrev = [], [] for i in range(n): q.append(q[-1] + lam[i]) for i in range(k): t = q[i] + 1 - lam[i] w.append(t) for i in range(n - k): i += k t, f = q[i] + 1 - lam[i], i - k w.append(t + min(0, w[f] - q[f + 1])) for i in range(n): qrev.append(qrev[-1] + lamrev[i]) for i in range(k): t = qrev[i] + 1 - lamrev[i] wrev.append(t) for i in range(n - k): i += k t, f = qrev[i] + 1 - lamrev[i], i - k wrev.append(t + min(0, wrev[f] - qrev[f])) wrev.reverse() print(min(min((w[i] + wrev[i] - (lam[i] + 1) % 2) for i in range(n)), q[-1])) for sdfghjkl in range(iinput()): main() ```
instruction
0
87,742
7
175,484
Yes
output
1
87,742
7
175,485
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a garland consisting of n lamps. States of the lamps are represented by the string s of length n. The i-th character of the string s_i equals '0' if the i-th lamp is turned off or '1' if the i-th lamp is turned on. You are also given a positive integer k. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called k-periodic if the distance between each pair of adjacent turned on lamps is exactly k. Consider the case k=3. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain k-periodic garland from the given one. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 25~ 000) — the number of test cases. Then t test cases follow. The first line of the test case contains two integers n and k (1 ≤ n ≤ 10^6; 1 ≤ k ≤ n) — the length of s and the required period. The second line of the test case contains the string s consisting of n characters '0' and '1'. It is guaranteed that the sum of n over all test cases does not exceed 10^6 (∑ n ≤ 10^6). Output For each test case, print the answer — the minimum number of moves you need to make to obtain k-periodic garland from the given one. Example Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0 Submitted Solution: ``` def car(): for i in range(int(input())): n,k=map(int,input().split()) s=input().strip() c1=s.count('1') ans=n for i in range(k): su=0 for j in range(i,n,k): if(s[j]=='1'): su+=1 else: su-=1 su=max(0,su) ans=min(ans,c1-su) print(ans) car() ```
instruction
0
87,743
7
175,486
Yes
output
1
87,743
7
175,487
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a garland consisting of n lamps. States of the lamps are represented by the string s of length n. The i-th character of the string s_i equals '0' if the i-th lamp is turned off or '1' if the i-th lamp is turned on. You are also given a positive integer k. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called k-periodic if the distance between each pair of adjacent turned on lamps is exactly k. Consider the case k=3. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain k-periodic garland from the given one. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 25~ 000) — the number of test cases. Then t test cases follow. The first line of the test case contains two integers n and k (1 ≤ n ≤ 10^6; 1 ≤ k ≤ n) — the length of s and the required period. The second line of the test case contains the string s consisting of n characters '0' and '1'. It is guaranteed that the sum of n over all test cases does not exceed 10^6 (∑ n ≤ 10^6). Output For each test case, print the answer — the minimum number of moves you need to make to obtain k-periodic garland from the given one. Example Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0 Submitted Solution: ``` t=int(input()) for i in range(t): n,k=map(int,input().split()) s=input() cnt=s.count('1') ans=cnt for j in range(k): score=0 for r in range(j,n,k): if(s[r]=='1'): score+=1 else: score-=1 score=max(0,score) ans=min(ans,cnt-score) print(ans) ```
instruction
0
87,744
7
175,488
No
output
1
87,744
7
175,489
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a garland consisting of n lamps. States of the lamps are represented by the string s of length n. The i-th character of the string s_i equals '0' if the i-th lamp is turned off or '1' if the i-th lamp is turned on. You are also given a positive integer k. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called k-periodic if the distance between each pair of adjacent turned on lamps is exactly k. Consider the case k=3. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain k-periodic garland from the given one. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 25~ 000) — the number of test cases. Then t test cases follow. The first line of the test case contains two integers n and k (1 ≤ n ≤ 10^6; 1 ≤ k ≤ n) — the length of s and the required period. The second line of the test case contains the string s consisting of n characters '0' and '1'. It is guaranteed that the sum of n over all test cases does not exceed 10^6 (∑ n ≤ 10^6). Output For each test case, print the answer — the minimum number of moves you need to make to obtain k-periodic garland from the given one. Example Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0 Submitted Solution: ``` import os import sys from io import BytesIO, IOBase from collections import Counter import math as mt BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) def gcd(a, b): if a == 0: return b return gcd(b % a, a) def lcm(a, b): return (a * b) / gcd(a, b) mod = int(1e9) + 7 def power(k, n): if n == 0: return 1 if n % 2: return (power(k, n - 1) * k) % mod t = power(k, n // 2) return (t * t) % mod def totalPrimeFactors(n): count = 0 if (n % 2) == 0: count += 1 while (n % 2) == 0: n //= 2 i = 3 while i * i <= n: if (n % i) == 0: count += 1 while (n % i) == 0: n //= i i += 2 if n > 2: count += 1 return count # #MAXN = int(1e7 + 1) # # spf = [0 for i in range(MAXN)] # # # def sieve(): # spf[1] = 1 # for i in range(2, MAXN): # spf[i] = i # for i in range(4, MAXN, 2): # spf[i] = 2 # # for i in range(3, mt.ceil(mt.sqrt(MAXN))): # if (spf[i] == i): # for j in range(i * i, MAXN, i): # if (spf[j] == j): # spf[j] = i # # # def getFactorization(x): # ret = 0 # while (x != 1): # k = spf[x] # ret += 1 # # ret.add(spf[x]) # while x % k == 0: # x //= k # # return ret # Driver code # precalculating Smallest Prime Factor # sieve() # 7 2 3 def main(): for _ in range(int(input())): n, k = map(int, input().split()) S = input() s = [] pre = [] for i in S: if i == '0' or i == '1': s.append(int(i)) pre.append(int(i)) for i in range(1, len(pre)): pre[i] += pre[i - 1] ans = n curr = 0 if n==1: ans=0 for i in range(k): t=[] for j in range(i, n, k): t.append([s[j], j]) end=-1 maxx=0 curr=0 for j in range(len(t)): if t[j][0]==0 and j>0 and t[j-1][0]==1: if curr>maxx: maxx=curr end=t[j-1][1] curr=0 else: if t[j][0]==1: curr+=1 if curr: if curr > maxx: maxx = curr end = t[-1][1] if end!=-1: ans=min(ans, pre[-1]-maxx) print(ans) return if __name__ == "__main__": main() ```
instruction
0
87,745
7
175,490
No
output
1
87,745
7
175,491
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a garland consisting of n lamps. States of the lamps are represented by the string s of length n. The i-th character of the string s_i equals '0' if the i-th lamp is turned off or '1' if the i-th lamp is turned on. You are also given a positive integer k. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called k-periodic if the distance between each pair of adjacent turned on lamps is exactly k. Consider the case k=3. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain k-periodic garland from the given one. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 25~ 000) — the number of test cases. Then t test cases follow. The first line of the test case contains two integers n and k (1 ≤ n ≤ 10^6; 1 ≤ k ≤ n) — the length of s and the required period. The second line of the test case contains the string s consisting of n characters '0' and '1'. It is guaranteed that the sum of n over all test cases does not exceed 10^6 (∑ n ≤ 10^6). Output For each test case, print the answer — the minimum number of moves you need to make to obtain k-periodic garland from the given one. Example Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0 Submitted Solution: ``` t=int(input()) p=0 while p<t: n,k=list(map(int,(input().split()))) s=input() f=s.find('1') l=s.rfind('1') if f==-1: print(0) p+=1 continue s1=s[f:l+1] mn=len(s1) s2=('1'+'0'*(k-1))*n s3=s2[:mn] if p==70 or p==71: print(n,k,s) #print(s1," ",s2," ",s3) r=int(s3,2)^int(s1,2) d=bin(r).count('1') print(d) p+=1 ```
instruction
0
87,746
7
175,492
No
output
1
87,746
7
175,493