text
stringlengths
198
433k
conversation_id
int64
0
109k
Provide a correct Python 3 solution for this coding contest problem. Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists. * Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s'). Constraints * 1 \leq |s| \leq 10^5 * 1 \leq |t| \leq 10^5 * s and t consists of lowercase English letters. Input Input is given from Standard Input in the following format: s t Output If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print `-1`. Examples Input contest son Output 10 Input contest programming Output -1 Input contest sentence Output 33 "Correct Solution: ``` from bisect import bisect_right s = input() t = input() n = len(s) mp = [[] for _ in range(26)] s += s for i in range(len(s)): mp[ord(s[i]) - ord('a')].append(i) ans = 0 now = -1 for i in range(len(t)): a = ord(t[i]) - ord('a') nxt = bisect_right(mp[a], now) if len(mp[a]) == nxt: print(-1) exit() ans += mp[a][nxt] - now now = mp[a][nxt] if now >= n: now -= n print(ans) ```
14,800
Provide a correct Python 3 solution for this coding contest problem. Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists. * Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s'). Constraints * 1 \leq |s| \leq 10^5 * 1 \leq |t| \leq 10^5 * s and t consists of lowercase English letters. Input Input is given from Standard Input in the following format: s t Output If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print `-1`. Examples Input contest son Output 10 Input contest programming Output -1 Input contest sentence Output 33 "Correct Solution: ``` import string import bisect S = input() T = input() n = len(S) sa = {_: [] for _ in string.ascii_lowercase} for i, s in enumerate(S): sa[s] += [i] ans = 0 i = -1 for t in T: if sa[t]: j = bisect.bisect_left(sa[t], i + 1) if j == len(sa[t]): ni = sa[t][0] + n else: ni = sa[t][j] ans += ni - i i = ni % n else: print(-1) exit() print(ans) ```
14,801
Provide a correct Python 3 solution for this coding contest problem. Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists. * Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s'). Constraints * 1 \leq |s| \leq 10^5 * 1 \leq |t| \leq 10^5 * s and t consists of lowercase English letters. Input Input is given from Standard Input in the following format: s t Output If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print `-1`. Examples Input contest son Output 10 Input contest programming Output -1 Input contest sentence Output 33 "Correct Solution: ``` S = input() T = input() length = len(S) ans = 0 pre = -1 for t in T: x = S.find(t, pre+1, length) if x != -1: ans += x - pre pre = x else: x = S.find(t, 0, pre+1) if x != -1: ans += length + x - pre pre = x else: ans = -1 break print(ans) ```
14,802
Provide a correct Python 3 solution for this coding contest problem. Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists. * Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s'). Constraints * 1 \leq |s| \leq 10^5 * 1 \leq |t| \leq 10^5 * s and t consists of lowercase English letters. Input Input is given from Standard Input in the following format: s t Output If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print `-1`. Examples Input contest son Output 10 Input contest programming Output -1 Input contest sentence Output 33 "Correct Solution: ``` s = input() n = len(s) l = [[]for i in range(26)] for i, c in enumerate(s * 2): l[ord(c) - ord('a')].append(i) from bisect import * shukai = 0 now = 0 t = input() for c in t: x = ord(c) - ord('a') if len(l[x]) == 0: print(-1) exit() i = bisect_left(l[x], now) now = l[x][i] + 1 if now >= n: now -= n shukai += 1 print(shukai * n + now) ```
14,803
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists. * Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s'). Constraints * 1 \leq |s| \leq 10^5 * 1 \leq |t| \leq 10^5 * s and t consists of lowercase English letters. Input Input is given from Standard Input in the following format: s t Output If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print `-1`. Examples Input contest son Output 10 Input contest programming Output -1 Input contest sentence Output 33 Submitted Solution: ``` from collections import Counter, defaultdict from bisect import bisect_right s = input() t = input() c = Counter(s) for e in t: if c[e] == 0: print(-1) exit() ss = s + s d = defaultdict(list) for i, e in enumerate(ss): d[e].append(i) now = -1 ans = 0 for e in t: i = bisect_right(d[e], now) nxt = d[e][i] ans += nxt - now now = nxt % len(s) print(ans) ``` Yes
14,804
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists. * Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s'). Constraints * 1 \leq |s| \leq 10^5 * 1 \leq |t| \leq 10^5 * s and t consists of lowercase English letters. Input Input is given from Standard Input in the following format: s t Output If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print `-1`. Examples Input contest son Output 10 Input contest programming Output -1 Input contest sentence Output 33 Submitted Solution: ``` s=input() t=input() leng=len(s) lent=len(t) count=0 tmp=0 i=0 while True: ans=s[tmp:].find(t[i]) if ans!=-1: i += 1 tmp += ans+1 if i ==lent: count += tmp break continue if tmp==0: count=-1 break tmp=0 count+=leng if count>leng*10**100: count=-1 print(count) ``` Yes
14,805
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists. * Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s'). Constraints * 1 \leq |s| \leq 10^5 * 1 \leq |t| \leq 10^5 * s and t consists of lowercase English letters. Input Input is given from Standard Input in the following format: s t Output If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print `-1`. Examples Input contest son Output 10 Input contest programming Output -1 Input contest sentence Output 33 Submitted Solution: ``` import bisect s=input() t=input() n=len(s)*2 dic={} for idx,i in enumerate(s*2): if i in dic.keys(): dic[i].append(idx) else: dic[i]=[idx] #print(dic) ans=k=0 before='-1' for i in t: if i not in dic.keys(): print(-1) exit() t=bisect.bisect_left(dic[i], ans%n) if before==i: t+=1 if len(dic[i])==t: t=0 k+=n ans=dic[i][t]+k before=i print(ans+1) ``` Yes
14,806
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists. * Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s'). Constraints * 1 \leq |s| \leq 10^5 * 1 \leq |t| \leq 10^5 * s and t consists of lowercase English letters. Input Input is given from Standard Input in the following format: s t Output If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print `-1`. Examples Input contest son Output 10 Input contest programming Output -1 Input contest sentence Output 33 Submitted Solution: ``` s = input() t = input() t_length = len(t) temp = s[:] count = 0 ans = 0 for i in range(len(t)): a = s.find(t[i]) if a == -1: print(-1) exit() while count < t_length: value = temp.find(t[count]) if value == -1: ans += len(temp) temp = s[:] else: ans += value + 1 count += 1 temp = temp[value + 1:] print(ans) ``` Yes
14,807
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists. * Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s'). Constraints * 1 \leq |s| \leq 10^5 * 1 \leq |t| \leq 10^5 * s and t consists of lowercase English letters. Input Input is given from Standard Input in the following format: s t Output If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print `-1`. Examples Input contest son Output 10 Input contest programming Output -1 Input contest sentence Output 33 Submitted Solution: ``` import string import bisect s=input() t=input() tng=len(t) sng=len(s) komoji=string.ascii_lowercase kjlist=[] kjlist2=[] mada=[] for kj in komoji: kjlist.append([kj, []]) kjlist2.append(kj) mada.append(kj) for tt in range(sng): ban=kjlist2.index(s[tt]) kjlist[ban][1].append(tt) #print(kjlist) kosu=0 now=0 nasi=0 for ss in range(tng): ban=kjlist2.index(t[ss]) if len(kjlist[ban][1])>0: basho=bisect.bisect_left(kjlist[ban][1], now) #print(basho, kjlist[ban][1], now) if basho==len(kjlist[ban][1]): now=kjlist[ban][1][0] kosu=(int(kosu/sng)+1)*sng+now else: now=kjlist[ban][1][basho] kosu=int(kosu/sng)*sng+now else: nasi=1 #print(kosu) if nasi==0: print(kosu+1) else: print(-1) ``` No
14,808
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists. * Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s'). Constraints * 1 \leq |s| \leq 10^5 * 1 \leq |t| \leq 10^5 * s and t consists of lowercase English letters. Input Input is given from Standard Input in the following format: s t Output If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print `-1`. Examples Input contest son Output 10 Input contest programming Output -1 Input contest sentence Output 33 Submitted Solution: ``` s = input() t = input() lent = len(t) tcount = 0 snow = 0 count = 0 flag = True if t[0] not in s: print(-1) else: snow = s.index(t[0]) while True: tcount += 1 if tcount == lent: break if t[tcount] in s[snow+1:]: snow = s[snow+1:].index(t[tcount])+snow elif t[tcount] in s: snow = s.index(t[tcount]) count += 1 else: flag = False break if flag: print(count*len(s) + s.index(t[-1])+1) else: print(-1) ``` No
14,809
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists. * Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s'). Constraints * 1 \leq |s| \leq 10^5 * 1 \leq |t| \leq 10^5 * s and t consists of lowercase English letters. Input Input is given from Standard Input in the following format: s t Output If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print `-1`. Examples Input contest son Output 10 Input contest programming Output -1 Input contest sentence Output 33 Submitted Solution: ``` from heapq import heappush, heappop from collections import deque,defaultdict,Counter import itertools from itertools import permutations import sys import bisect import string sys.setrecursionlimit(10**6) def SI(): return input().split() def MI(): return map(int,input().split()) def I(): return int(input()) def LI(): return [int(i) for i in input().split()] YN=['Yes','No'] mo=10**9+7 alp=string.ascii_lowercase d_al=dict([(i,j) for j,i in enumerate(alp)]) s=[d_al[i] for i in input()] t=[d_al[i] for i in input()] Ss=set(s) St=set(t) ns=len(s) nt=len(t) ss=s*2 #print(ss) g=[{} for _ in range(ns)] pos=[-1]*26 for i in range(ns*2)[::-1]: for j in Ss: if i<ns: g[i][j]=pos[j]-i pos[ss[i]]=i #for i in range(len(g)): # print(g[i]) if len(St-Ss)!=0: ans=-1 else: ans=0 cur=0 for i in range(nt): ans+=g[cur][t[i]] cur+=g[cur][t[i]] cur%=ns print(ans)#,loop,has) ``` No
14,810
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists. * Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s'). Constraints * 1 \leq |s| \leq 10^5 * 1 \leq |t| \leq 10^5 * s and t consists of lowercase English letters. Input Input is given from Standard Input in the following format: s t Output If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print `-1`. Examples Input contest son Output 10 Input contest programming Output -1 Input contest sentence Output 33 Submitted Solution: ``` import sys sys.setrecursionlimit(2147483647) INF=float("inf") MOD=10**9+7 input=lambda:sys.stdin.readline().rstrip() from collections import defaultdict from bisect import bisect_left def resolve(): S,T=input(),input() if(not set(S)>set(T)): print(-1) return E=defaultdict(list) for i,s in enumerate(2*S): E[s].append(i) n=len(S) ans=0 now=0 # mod nで考える for t in T: i=bisect_left(E[t],now) ans+=E[t][i]-now+1 now=(E[t][i]+1)%n print(ans) resolve() ``` No
14,811
Provide a correct Python 3 solution for this coding contest problem. N people are arranged in a row from left to right. You are given a string S of length N consisting of `0` and `1`, and a positive integer K. The i-th person from the left is standing on feet if the i-th character of S is `0`, and standing on hands if that character is `1`. You will give the following direction at most K times (possibly zero): Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands. Find the maximum possible number of consecutive people standing on hands after at most K directions. Constraints * N is an integer satisfying 1 \leq N \leq 10^5. * K is an integer satisfying 1 \leq K \leq 10^5. * The length of the string S is N. * Each character of the string S is `0` or `1`. Input Input is given from Standard Input in the following format: N K S Output Print the maximum possible number of consecutive people standing on hands after at most K directions. Examples Input 5 1 00010 Output 4 Input 14 2 11101010110011 Output 8 Input 1 1 1 Output 1 "Correct Solution: ``` n, k = map(int, input().split()) s = list(input()) L = [0] R = [] for i, (a, b) in enumerate(zip(s, s[1:])): if (a, b) == ('0', '1'): L.append(i+1) if (a, b) == ('1', '0'): R.append(i) else: R.append(n-1) ans = R[0]-L[0]+1 if s[0] == '0': k -= 1 k = min(k, len(R)-1) for l, r in zip(L, R[k:]): x = r-l+1 if ans < x: ans = x print(ans) ```
14,812
Provide a correct Python 3 solution for this coding contest problem. N people are arranged in a row from left to right. You are given a string S of length N consisting of `0` and `1`, and a positive integer K. The i-th person from the left is standing on feet if the i-th character of S is `0`, and standing on hands if that character is `1`. You will give the following direction at most K times (possibly zero): Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands. Find the maximum possible number of consecutive people standing on hands after at most K directions. Constraints * N is an integer satisfying 1 \leq N \leq 10^5. * K is an integer satisfying 1 \leq K \leq 10^5. * The length of the string S is N. * Each character of the string S is `0` or `1`. Input Input is given from Standard Input in the following format: N K S Output Print the maximum possible number of consecutive people standing on hands after at most K directions. Examples Input 5 1 00010 Output 4 Input 14 2 11101010110011 Output 8 Input 1 1 1 Output 1 "Correct Solution: ``` n,k=map(int,input().split()) s='1'+input() i=0 for j in range(n):k-='10'==s[j:j+2];i+=k<0;k+=k<0and'01'==s[i:i+2] print(n-i) ```
14,813
Provide a correct Python 3 solution for this coding contest problem. N people are arranged in a row from left to right. You are given a string S of length N consisting of `0` and `1`, and a positive integer K. The i-th person from the left is standing on feet if the i-th character of S is `0`, and standing on hands if that character is `1`. You will give the following direction at most K times (possibly zero): Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands. Find the maximum possible number of consecutive people standing on hands after at most K directions. Constraints * N is an integer satisfying 1 \leq N \leq 10^5. * K is an integer satisfying 1 \leq K \leq 10^5. * The length of the string S is N. * Each character of the string S is `0` or `1`. Input Input is given from Standard Input in the following format: N K S Output Print the maximum possible number of consecutive people standing on hands after at most K directions. Examples Input 5 1 00010 Output 4 Input 14 2 11101010110011 Output 8 Input 1 1 1 Output 1 "Correct Solution: ``` import sys def input(): return sys.stdin.readline()[:-1] N,K=map(int,input().split()) S=input() l=[0] t="1" for i in S: if t==i: l[-1]+=1 else: l.append(1) if t=="1": t="0" else: t="1" if t=="0": l.append(0) a=sum(l[:2*K+1]) A=a for i in range(2*K+2,len(l),2): a+=l[i]+l[i-1] a-=l[i-2*K-2]+l[i-2*K-1] A=max(A,a) print(A) ```
14,814
Provide a correct Python 3 solution for this coding contest problem. N people are arranged in a row from left to right. You are given a string S of length N consisting of `0` and `1`, and a positive integer K. The i-th person from the left is standing on feet if the i-th character of S is `0`, and standing on hands if that character is `1`. You will give the following direction at most K times (possibly zero): Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands. Find the maximum possible number of consecutive people standing on hands after at most K directions. Constraints * N is an integer satisfying 1 \leq N \leq 10^5. * K is an integer satisfying 1 \leq K \leq 10^5. * The length of the string S is N. * Each character of the string S is `0` or `1`. Input Input is given from Standard Input in the following format: N K S Output Print the maximum possible number of consecutive people standing on hands after at most K directions. Examples Input 5 1 00010 Output 4 Input 14 2 11101010110011 Output 8 Input 1 1 1 Output 1 "Correct Solution: ``` n,k=map(int,input().split()) s='1'+input() i=j=0 while j<n:k-='10'==s[j:j+2];i+=k<0;k+=k<0and'01'==s[i:i+2];j+=1 print(n-i) ```
14,815
Provide a correct Python 3 solution for this coding contest problem. N people are arranged in a row from left to right. You are given a string S of length N consisting of `0` and `1`, and a positive integer K. The i-th person from the left is standing on feet if the i-th character of S is `0`, and standing on hands if that character is `1`. You will give the following direction at most K times (possibly zero): Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands. Find the maximum possible number of consecutive people standing on hands after at most K directions. Constraints * N is an integer satisfying 1 \leq N \leq 10^5. * K is an integer satisfying 1 \leq K \leq 10^5. * The length of the string S is N. * Each character of the string S is `0` or `1`. Input Input is given from Standard Input in the following format: N K S Output Print the maximum possible number of consecutive people standing on hands after at most K directions. Examples Input 5 1 00010 Output 4 Input 14 2 11101010110011 Output 8 Input 1 1 1 Output 1 "Correct Solution: ``` n,k=map(int,input().split()) s=input() list10 = {'0':[],'1':[]} pre_c='1' c_c = 0 cur_sum = 0 cur_max = 0 for c in s: if c==pre_c: c_c += 1 else: list10[pre_c].append(c_c) cur_sum += c_c c_c = 1 pre_c = c if len(list10['1'])== k+1: cur_max = max(cur_max,cur_sum) cur_sum -= (list10['0'].pop(0) + list10['1'].pop(0)) print(max(cur_max,cur_sum+c_c)) ```
14,816
Provide a correct Python 3 solution for this coding contest problem. N people are arranged in a row from left to right. You are given a string S of length N consisting of `0` and `1`, and a positive integer K. The i-th person from the left is standing on feet if the i-th character of S is `0`, and standing on hands if that character is `1`. You will give the following direction at most K times (possibly zero): Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands. Find the maximum possible number of consecutive people standing on hands after at most K directions. Constraints * N is an integer satisfying 1 \leq N \leq 10^5. * K is an integer satisfying 1 \leq K \leq 10^5. * The length of the string S is N. * Each character of the string S is `0` or `1`. Input Input is given from Standard Input in the following format: N K S Output Print the maximum possible number of consecutive people standing on hands after at most K directions. Examples Input 5 1 00010 Output 4 Input 14 2 11101010110011 Output 8 Input 1 1 1 Output 1 "Correct Solution: ``` N,K=map(int,input().split()) S=list(input()) l = [0,0] if (S[0] == '0') else [0] l += [i for i in range(1,N) if S[i-1] != S[i]] l += [N] if (S[-1] == '1') else [N,N] ll=len(l) p=2*K+1 if ll<p: print(N) else: print(max(l[p+i]-l[i] for i in range(0,ll-p+1,2))) ```
14,817
Provide a correct Python 3 solution for this coding contest problem. N people are arranged in a row from left to right. You are given a string S of length N consisting of `0` and `1`, and a positive integer K. The i-th person from the left is standing on feet if the i-th character of S is `0`, and standing on hands if that character is `1`. You will give the following direction at most K times (possibly zero): Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands. Find the maximum possible number of consecutive people standing on hands after at most K directions. Constraints * N is an integer satisfying 1 \leq N \leq 10^5. * K is an integer satisfying 1 \leq K \leq 10^5. * The length of the string S is N. * Each character of the string S is `0` or `1`. Input Input is given from Standard Input in the following format: N K S Output Print the maximum possible number of consecutive people standing on hands after at most K directions. Examples Input 5 1 00010 Output 4 Input 14 2 11101010110011 Output 8 Input 1 1 1 Output 1 "Correct Solution: ``` N,K=map(int,input().split()) S=input() po=[0] X=[] i=0 for i in range(N-1): if not S[i] == S[i+1]: po.append(i+1) po.append(len(S)) for i in range(len(po)): if 2*K+1>=len(po): X.append(po[-1]) break elif 2*K+i+1>=len(po): X.append(po[2*K+i]-po[i]) break elif S[po[i]]=='0': X.append(po[2*K+i]-po[i]) else: X.append(po[2*K+1+i]-po[i]) print(max(X)) ```
14,818
Provide a correct Python 3 solution for this coding contest problem. N people are arranged in a row from left to right. You are given a string S of length N consisting of `0` and `1`, and a positive integer K. The i-th person from the left is standing on feet if the i-th character of S is `0`, and standing on hands if that character is `1`. You will give the following direction at most K times (possibly zero): Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands. Find the maximum possible number of consecutive people standing on hands after at most K directions. Constraints * N is an integer satisfying 1 \leq N \leq 10^5. * K is an integer satisfying 1 \leq K \leq 10^5. * The length of the string S is N. * Each character of the string S is `0` or `1`. Input Input is given from Standard Input in the following format: N K S Output Print the maximum possible number of consecutive people standing on hands after at most K directions. Examples Input 5 1 00010 Output 4 Input 14 2 11101010110011 Output 8 Input 1 1 1 Output 1 "Correct Solution: ``` n, k = map(int, input().split()) s = input() c = 1 l = [] l += [0,0] if (s[0] == '0') else [0] for i in range(1, n): if s[i-1] != s[i]: l += [c+l[-1]]; c = 1; else: c +=1 l += [c+l[-1]] if s[-1] == '0': l += [l[-1]] ll = len(l) pl = 2 * k + 1 if ll <= pl: print(n) else: m = 0 for i in range(0, ll-pl+1, 2): t = l[pl+i]-l[i]; m = t if (t > m) else m print(m) ```
14,819
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. N people are arranged in a row from left to right. You are given a string S of length N consisting of `0` and `1`, and a positive integer K. The i-th person from the left is standing on feet if the i-th character of S is `0`, and standing on hands if that character is `1`. You will give the following direction at most K times (possibly zero): Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands. Find the maximum possible number of consecutive people standing on hands after at most K directions. Constraints * N is an integer satisfying 1 \leq N \leq 10^5. * K is an integer satisfying 1 \leq K \leq 10^5. * The length of the string S is N. * Each character of the string S is `0` or `1`. Input Input is given from Standard Input in the following format: N K S Output Print the maximum possible number of consecutive people standing on hands after at most K directions. Examples Input 5 1 00010 Output 4 Input 14 2 11101010110011 Output 8 Input 1 1 1 Output 1 Submitted Solution: ``` n, k = map(int, input().split()) s = input().rstrip() #s = "0001111011110" #n, k = len(s), 1 s="1"+s k_pt = 0 cur_tail = 0 for cur in range(1, len(s)): if s[cur-1] == '1' and s[cur] == '0': k_pt+=1 if k_pt > k: cur_tail+=1 if s[cur_tail] == '0' and s[cur_tail+1] == '1': k_pt-=1 #print((cur_tail, cur,k_pt)) print(cur-cur_tail) ``` Yes
14,820
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. N people are arranged in a row from left to right. You are given a string S of length N consisting of `0` and `1`, and a positive integer K. The i-th person from the left is standing on feet if the i-th character of S is `0`, and standing on hands if that character is `1`. You will give the following direction at most K times (possibly zero): Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands. Find the maximum possible number of consecutive people standing on hands after at most K directions. Constraints * N is an integer satisfying 1 \leq N \leq 10^5. * K is an integer satisfying 1 \leq K \leq 10^5. * The length of the string S is N. * Each character of the string S is `0` or `1`. Input Input is given from Standard Input in the following format: N K S Output Print the maximum possible number of consecutive people standing on hands after at most K directions. Examples Input 5 1 00010 Output 4 Input 14 2 11101010110011 Output 8 Input 1 1 1 Output 1 Submitted Solution: ``` N, K = map(int, input().split()) S = input() S = '1' + S + '1' rev = [] for i in range(N+1): if S[i] != S[i+1]: rev.append(i) l = len(rev)//2 a = [(rev[2*i], rev[2*i+1]) for i in range(l)] a = [(0, 0)] + a + [(N, N)] K = min(K, l) m = -1 for i in range(l-K+1): x = a[i+K+1][0]-a[i][1] m = max(m, x) print(m) ``` Yes
14,821
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. N people are arranged in a row from left to right. You are given a string S of length N consisting of `0` and `1`, and a positive integer K. The i-th person from the left is standing on feet if the i-th character of S is `0`, and standing on hands if that character is `1`. You will give the following direction at most K times (possibly zero): Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands. Find the maximum possible number of consecutive people standing on hands after at most K directions. Constraints * N is an integer satisfying 1 \leq N \leq 10^5. * K is an integer satisfying 1 \leq K \leq 10^5. * The length of the string S is N. * Each character of the string S is `0` or `1`. Input Input is given from Standard Input in the following format: N K S Output Print the maximum possible number of consecutive people standing on hands after at most K directions. Examples Input 5 1 00010 Output 4 Input 14 2 11101010110011 Output 8 Input 1 1 1 Output 1 Submitted Solution: ``` n,k=map(int,input().split()) s=list(input()) judge=[0] max_=0 for i in range(n-1): if s[i]!=s[i+1]: judge.append(i+1) judge.append(n) if len(judge)//2<=k: print(len(s)) else: for i in range(len(judge)-1): if s[judge[i]]=='0': ans=judge[min(i+2*k,len(judge)-1)]-judge[i] else: ans=judge[min(i+2*k+1,len(judge)-1)]-judge[i] if ans>max_: max_=ans print(max_) ``` Yes
14,822
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. N people are arranged in a row from left to right. You are given a string S of length N consisting of `0` and `1`, and a positive integer K. The i-th person from the left is standing on feet if the i-th character of S is `0`, and standing on hands if that character is `1`. You will give the following direction at most K times (possibly zero): Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands. Find the maximum possible number of consecutive people standing on hands after at most K directions. Constraints * N is an integer satisfying 1 \leq N \leq 10^5. * K is an integer satisfying 1 \leq K \leq 10^5. * The length of the string S is N. * Each character of the string S is `0` or `1`. Input Input is given from Standard Input in the following format: N K S Output Print the maximum possible number of consecutive people standing on hands after at most K directions. Examples Input 5 1 00010 Output 4 Input 14 2 11101010110011 Output 8 Input 1 1 1 Output 1 Submitted Solution: ``` n,k = map(int,input().split()) s=input() s = list(map(int,s)) memo = [0] for i in range(1,n): if s[i-1] != s[i]: memo.append(i) ans = 0 for i in range(len(memo)): start = memo[i] if (s[start] == 1): end = i + 2*k +1 else: end = i + 2*k if end < len(memo): end = memo[end] else: end = len(s) ans = max(ans, end-start) print(ans) ``` Yes
14,823
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. N people are arranged in a row from left to right. You are given a string S of length N consisting of `0` and `1`, and a positive integer K. The i-th person from the left is standing on feet if the i-th character of S is `0`, and standing on hands if that character is `1`. You will give the following direction at most K times (possibly zero): Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands. Find the maximum possible number of consecutive people standing on hands after at most K directions. Constraints * N is an integer satisfying 1 \leq N \leq 10^5. * K is an integer satisfying 1 \leq K \leq 10^5. * The length of the string S is N. * Each character of the string S is `0` or `1`. Input Input is given from Standard Input in the following format: N K S Output Print the maximum possible number of consecutive people standing on hands after at most K directions. Examples Input 5 1 00010 Output 4 Input 14 2 11101010110011 Output 8 Input 1 1 1 Output 1 Submitted Solution: ``` from itertools import groupby, accumulate N, K = map(int, input().split()) S = groupby(list(input())) state = [] number = [] for i, j in S: state.append(i) number.append(len(list(j))) answer = 0 if len(state) == 1: print(number[0]) exit() number = list(accumulate([0] + number)) l = len(number) - 1 if state[0] == '0': answer = number[min(l, 2 * K)] - number[0] start = state.index('1') + 1 for i in range(start, l, 2): answer = max(answer, number[min(2 * K + i + 1, l)] - number[i - 1]) print(answer) ``` No
14,824
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. N people are arranged in a row from left to right. You are given a string S of length N consisting of `0` and `1`, and a positive integer K. The i-th person from the left is standing on feet if the i-th character of S is `0`, and standing on hands if that character is `1`. You will give the following direction at most K times (possibly zero): Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands. Find the maximum possible number of consecutive people standing on hands after at most K directions. Constraints * N is an integer satisfying 1 \leq N \leq 10^5. * K is an integer satisfying 1 \leq K \leq 10^5. * The length of the string S is N. * Each character of the string S is `0` or `1`. Input Input is given from Standard Input in the following format: N K S Output Print the maximum possible number of consecutive people standing on hands after at most K directions. Examples Input 5 1 00010 Output 4 Input 14 2 11101010110011 Output 8 Input 1 1 1 Output 1 Submitted Solution: ``` n, k = map(int, input().split()) s = input() + "#" count = 1 group = 0 L = [] for i in range(n): if s[i] == s[i+1]: count += 1 else: L.append(count) group += 1 count = 1 ans = 0 if k >= group//2: ans = n else: temp = sum(L[:2*k+1]) ans = temp for i in range(2*k+1, group): temp -= L[i-(2*k+1)] temp += L[i] ans = max(temp, ans) print(ans) ``` No
14,825
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. N people are arranged in a row from left to right. You are given a string S of length N consisting of `0` and `1`, and a positive integer K. The i-th person from the left is standing on feet if the i-th character of S is `0`, and standing on hands if that character is `1`. You will give the following direction at most K times (possibly zero): Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands. Find the maximum possible number of consecutive people standing on hands after at most K directions. Constraints * N is an integer satisfying 1 \leq N \leq 10^5. * K is an integer satisfying 1 \leq K \leq 10^5. * The length of the string S is N. * Each character of the string S is `0` or `1`. Input Input is given from Standard Input in the following format: N K S Output Print the maximum possible number of consecutive people standing on hands after at most K directions. Examples Input 5 1 00010 Output 4 Input 14 2 11101010110011 Output 8 Input 1 1 1 Output 1 Submitted Solution: ``` from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify import math import bisect import random from itertools import permutations, accumulate, combinations, product import sys import string from bisect import bisect_left, bisect_right from math import factorial, ceil, floor from operator import mul from functools import reduce sys.setrecursionlimit(2147483647) INF = 10 ** 13 def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def I(): return int(sys.stdin.buffer.readline()) def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') 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)] def SRL(n): return [list(S()) for i in range(n)] def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] mod = 1000000007 n, k = LI() s = S() X = [] if s[0] == '0': X += [0] cnt = 1 for i in range(n - 1): if s[i] != s[i + 1]: X += [cnt] cnt = 1 else: cnt += 1 X += [cnt] if len(X) <= 2 * k + 1: print(n) else: ans = 0 acc = list(accumulate([0] + X)) for j in range(2 * k + 1, len(acc), 2): ans = max(acc[j] - acc[j - (2 * k + 1)], ans) print(ans) ``` No
14,826
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. N people are arranged in a row from left to right. You are given a string S of length N consisting of `0` and `1`, and a positive integer K. The i-th person from the left is standing on feet if the i-th character of S is `0`, and standing on hands if that character is `1`. You will give the following direction at most K times (possibly zero): Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands. Find the maximum possible number of consecutive people standing on hands after at most K directions. Constraints * N is an integer satisfying 1 \leq N \leq 10^5. * K is an integer satisfying 1 \leq K \leq 10^5. * The length of the string S is N. * Each character of the string S is `0` or `1`. Input Input is given from Standard Input in the following format: N K S Output Print the maximum possible number of consecutive people standing on hands after at most K directions. Examples Input 5 1 00010 Output 4 Input 14 2 11101010110011 Output 8 Input 1 1 1 Output 1 Submitted Solution: ``` n, k = map(int, input().split()) s = input() + "2" c = [] ci = 1 for i in range(n): if s[i] == s[i+1]: ci += 1 else: c.append(ci) ci = 1 c = c + [0] ans = 1 if s[0] == "1": for i in range(0, len(c), 2): if i+2*k+1 > len(c): ans = max(ans, sum(c[i:])) else: ans = max(ans, sum(c[i:i+2*k+1])) else: c = [0] + c for i in range(0, len(c), 2): if i+2*k+1 > len(c): ans = max(ans, sum(c[i:])) else: ans = max(ans, sum(c[i:i+2*k+1])) print(ans) ``` No
14,827
Provide a correct Python 3 solution for this coding contest problem. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140 "Correct Solution: ``` import sys #input = sys.stdin.buffer.readline def main(): N = int(input()) S = input() Q = input() x = tuple(map(int,input().split())) for k in x: d,m,dm,dmc = 0,0,0,0 for i in range(N): st = S[i] if st == "D": d += 1 elif st == "M": m += 1 dm += d elif st == "C": dmc += dm if i >= k-1: if S[i-k+1] == "D": dm -= m d -= 1 elif S[i-k+1] == "M": m -= 1 print(dmc) if __name__ == "__main__": main() ```
14,828
Provide a correct Python 3 solution for this coding contest problem. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140 "Correct Solution: ``` import sys input = sys.stdin.readline N = int(input()) S = list(input())[: -1] Q = int(input()) qs = list(map(int, input().split())) cs = [0] * (N + 1) c = [] d = [] for i in range(N): cs[i + 1] = cs[i] + (S[i] == "M") if S[i] == "C": c.append(i) if S[i] == "D": d.append(i) ccs = [0] * (N + 1) ccounts = [0] * (N + 1) for i in range(N): ccs[i + 1] = ccs[i] + cs[i] * (S[i] == "C") ccounts[i + 1] = ccounts[i] + (S[i] == "C") #print(cs, c) #print(ccs) for k in qs: res = 0 for i in d: l = i r = min(N, i + k) res += max(0, ccs[r] - ccs[l] - cs[i] * (ccounts[r] - ccounts[l])) print(res) ```
14,829
Provide a correct Python 3 solution for this coding contest problem. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140 "Correct Solution: ``` n = int(input()) s = input() q = int(input()) K = list(map(int,input().split())) for ii in range(q): # D = [[0] * 3 for i in range(n)] d = 0 m = 0 dm = 0 ans = 0 k = K[ii] for i in range(n): # for j in range(3): # D[i][j] = D[i-1][j] if i-k >= 0: if s[i-k] == "D": d -= 1 dm -= m if s[i-k] == "M": m -= 1 if s[i] == "D": d += 1 if s[i] == "M": m += 1 dm += d if s[i] == "C": ans += dm print(ans) # print(D) ```
14,830
Provide a correct Python 3 solution for this coding contest problem. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140 "Correct Solution: ``` def main(): n = int(input()) s = input() q = int(input()) k = list(map(int, input().split())) for ki in k: ans = 0 DMC = [0]*3 for i in range(n): if s[i] == "D": DMC[0] += 1 elif s[i] == "M": DMC[1] += 1 DMC[2] += DMC[0] if i >= ki: j = i-ki if s[j] == "D": DMC[0] -= 1 DMC[2] -= DMC[1] elif s[j] == "M": DMC[1] -= 1 if s[i] == "C": ans += DMC[2] print(ans) if __name__ == "__main__": main() ```
14,831
Provide a correct Python 3 solution for this coding contest problem. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140 "Correct Solution: ``` import sys read = sys.stdin.buffer.read input = sys.stdin.readline input = sys.stdin.buffer.readline #sys.setrecursionlimit(10**9) #from functools import lru_cache def RD(): return sys.stdin.read() def II(): return int(input()) def MI(): return map(int,input().split()) def MF(): return map(float,input().split()) def LI(): return list(map(int,input().split())) def LF(): return list(map(float,input().split())) def TI(): return tuple(map(int,input().split())) # rstrip().decode('utf-8') def main(): n=II() s=input().rstrip().decode() li=[0]*n q=II() K=LI() for k in K: D=0 M=0 DM=0 ans=0 for i in range(n): if s[i]=="D": D+=1 elif s[i]=="M": M+=1 DM+=D elif s[i]=="C": ans+=DM if i-k+1>=0: if s[i-k+1]=="D": D-=1 DM-=M elif s[i-k+1]=="M": M-=1 print(ans) if __name__ == "__main__": main() ```
14,832
Provide a correct Python 3 solution for this coding contest problem. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140 "Correct Solution: ``` n=int(input()) s=input() q=int(input()) k=list(map(int,input().split())) for x in k: d,m,dm,ans=0,0,0,0 for i in range(n): ss=s[i] if ss=='D': d+=1 elif ss=='M': m+=1 dm+=d elif ss=='C': ans+=dm if i>=x-1: ss=s[i-x+1] if ss=='D': dm-=m d-=1 elif ss=='M': m-=1 print(ans) ```
14,833
Provide a correct Python 3 solution for this coding contest problem. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140 "Correct Solution: ``` N = int(input()) S = input() Q = int(input()) K = list(map(int, input().split())) for k in K: res = 0 d = 0 m = 0 dm = 0 for i in range(N): if S[i] == 'D': d += 1 elif S[i] == 'M': dm += d m += 1 elif S[i] == 'C': res += dm if i + 1 - k >= 0: if S[i + 1 - k] == 'D': d -= 1 dm -= m elif S[i + 1 - k] == 'M': m -= 1 print(res) ```
14,834
Provide a correct Python 3 solution for this coding contest problem. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140 "Correct Solution: ``` import sys input = sys.stdin.readline def main(): N = int(input()) S = input() Q = input() x = tuple(map(int,input().split())) for k in x: d,m,dm,dmc = 0,0,0,0 for i in range(N): st = S[i] if st == "D": d += 1 elif st == "M": m += 1 dm += d elif st == "C": dmc += dm if i >= k-1: if S[i-k+1] == "D": dm -= m d -= 1 elif S[i-k+1] == "M": m -= 1 print(dmc) if __name__ == "__main__": main() ```
14,835
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140 Submitted Solution: ``` # コピペ https://atcoder.jp/contests/dwacon5th-prelims/submissions/7670569 # 自分の https://atcoder.jp/contests/dwacon5th-prelims/submissions/7822126 # とほぼ変わらないのに自分のものはTLEするので検証 n=int(input()) s=input() q=int(input()) for i in list(map(int,input().split())): a=0 v=[0,0,0] for j in range(n): if s[j]=="D": v[0]+=1 elif s[j]=="M": v[1]+=1 v[2]+=v[0] if j>=i: if s[j-i]=="D": v[0]-=1 v[2]-=v[1] elif s[j-i]=="M": v[1]-=1 if s[j]=="C": a+=v[2] print(a) ``` Yes
14,836
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140 Submitted Solution: ``` import sys n = int(input()) s = input() q = int(input()) k = list(map(int, sys.stdin.readline().split())) x = [] for i in s: if i == "D": x.append(1) elif i == "M": x.append(2) elif i == "C": x.append(3) else: x.append(0) mrui = [0]* (n+1) for i in range(1,n+1): mrui[i] = mrui[i-1] if x[i-1] == 2: mrui[i] += 1 dp = [0,0,0,0] dp1 = [0,0,0,0] for i in k: dp = [1,0,0,0] for j in range(n): dp1 = dp kon = x[j] if kon: dp1[kon] += dp[kon-1] dp = dp1 if j >= i-1: if x[j-i+1] == 1: dp[1] -= 1 dp[2] -= (mrui[j+1]-mrui[j-i+2]) print(dp1[3]) ``` Yes
14,837
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140 Submitted Solution: ``` import sys readline = sys.stdin.readline MOD = 10 ** 9 + 7 INF = float('INF') sys.setrecursionlimit(10 ** 5) def main(): N = int(readline()) S = input() Q = int(readline()) K = list(map(int, readline().split())) for q in range(Q): k = K[q] res = 0 cnt = [0] * 3 for i in range(k): cur = S[i] if cur == "D": cnt[0] += 1 elif cur == "M": cnt[1] += 1 cnt[2] += cnt[0] elif cur == "C": res += cnt[2] for i in range(k, N): prev = S[i - k] cur = S[i] if prev == "D": cnt[0] -= 1 cnt[2] -= cnt[1] elif prev == "M": cnt[1] -= 1 if cur == "D": cnt[0] += 1 elif cur == "M": cnt[1] += 1 cnt[2] += cnt[0] elif cur == "C": res += cnt[2] print(res) if __name__ == '__main__': main() ``` Yes
14,838
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140 Submitted Solution: ``` import sys def main(): input = sys.stdin.readline N=int(input()) S=input() Q=int(input()) *K,=map(int, input().split()) for k in K: ans = 0 d,m,dm=0,0,0 j=0 for i in range(N): if i-j == k: if S[j]=='D': d -= 1 dm -= m elif S[j]=='M': m -= 1 j += 1 if S[i] == 'D': d += 1 elif S[i] == 'M': m += 1 dm += d elif S[i] == 'C': ans += dm print(ans) if __name__ == '__main__': main() ``` Yes
14,839
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140 Submitted Solution: ``` import numpy as np N = int(input()) S = [i for i in list(input())] Q = int(input()) k = [int(i) for i in input().split()] S = np.array(S, dtype=None) index_D = np.where(S == 'D') index_M = np.where(S == 'M') index_C = np.where(S == 'C') count = 0 for k_i in k: for a in index_D[0]: for b in index_M[0]: for c in index_C[0]: if a < b < c: if c-a < k_i: count += 1 print(count) ``` No
14,840
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140 Submitted Solution: ``` N=int(input()) D,M,C,A=map(ord,'DMCA') S=[ord(c)-A for c in input()] Q=int(input()) K=list(map(int,input().split())) DMC=[D-A,M-A,C-A] for k in K: dmc=[0,0,0] dm=0 a=0 for i,s in enumerate(S): if i>=k: if S[i-k] in DMC: m=DMC.index(S[i-k]) dmc[m]-=1 if m==0: dm-=dmc[1] if s in DMC: p=DMC.index(s) dmc[p]+=1 if p==1: dm+=dmc[0] if p==2: a+=dm print(a) ``` No
14,841
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140 Submitted Solution: ``` N=int(input()) D,M,C=map(ord,'DMC') S=[ord(c) for c in input()] input() K=map(int,input().split()) for k in K: d,m,c,dm=0,0,0,0 a=0 for i,s in enumerate(S): if i>=k: if S[i-k]==D: d-=1 dm-=m elif S[i-k]==M: m-=1 elif S[i-k]==C: c-=1 if s==D: d+=1 elif s==M: m+=1 dm+=d elif s==C: c+=1 a+=dm print(a) ``` No
14,842
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140 Submitted Solution: ``` from itertools import accumulate n = int(input()) s = input() acc_cnt_a = [0] * (n + 1) acc_cnt_b = [0] * (n + 1) acc_cnt_ab = [0] * (n + 1) c_loc = [] cnt_a = 0 for i, c in enumerate(s): if c == 'D': acc_cnt_a[i + 1] += 1 cnt_a += 1 elif c == 'M': acc_cnt_b[i + 1] += 1 acc_cnt_ab[i + 1] += cnt_a elif c == 'C': c_loc.append(i + 1) acc_cnt_a = list(accumulate(acc_cnt_a)) acc_cnt_b = list(accumulate(acc_cnt_b)) acc_cnt_ab = list(accumulate(acc_cnt_ab)) q = int(input()) buf = [] for k in map(int, input().split()): ans = 0 for r in c_loc: l = max(0, r - k) rejected_a = acc_cnt_a[l] cnt_internal_b = acc_cnt_b[r] - acc_cnt_b[l] cnt_internal_ab = acc_cnt_ab[r] - acc_cnt_ab[l] ans += cnt_internal_ab - rejected_a * cnt_internal_b buf.append(ans) print('\n'.join(map(str, buf))) ``` No
14,843
Provide a correct Python 3 solution for this coding contest problem. There are N squares lining up in a row, numbered 1 through N from left to right. Initially, all squares are white. We also have N-1 painting machines, numbered 1 through N-1. When operated, Machine i paints Square i and i+1 black. Snuke will operate these machines one by one. The order in which he operates them is represented by a permutation of (1, 2, ..., N-1), P, which means that the i-th operated machine is Machine P_i. Here, the score of a permutation P is defined as the number of machines that are operated before all the squares are painted black for the first time, when the machines are operated in the order specified by P. Snuke has not decided what permutation P to use, but he is interested in the scores of possible permutations. Find the sum of the scores over all possible permutations for him. Since this can be extremely large, compute the sum modulo 10^9+7. Constraints * 2 \leq N \leq 10^6 Input Input is given from Standard Input in the following format: N Output Print the sum of the scores over all possible permutations, modulo 10^9+7. Examples Input 4 Output 16 Input 2 Output 1 Input 5 Output 84 Input 100000 Output 341429644 "Correct Solution: ``` import sys def I(): return int(sys.stdin.readline().rstrip()) N = I() mod = 10**9+7 fac = [1]*(N+1) for i in range(1,N+1): fac[i] = (fac[i-1]*i) % mod fac_inverse = [1]*(N+1) for i in range(1,N+1): fac_inverse[i] = pow(fac[i],mod-2,mod) def nCr(n,r): if n < r: return 0 return (fac[n]*fac_inverse[r]*fac_inverse[n-r]) % mod A = [0]*(N+1) # Ai = i回以下マシンを稼働することで、全て黒く塗られるような順列の個数 for i in range(N+1): A[i] = (fac[i]*fac[N-1-i]*nCr(i-1,N-1-i)) % mod ans = 0 for i in range(1,N): ans += i*(A[i]-A[i-1]) ans %= mod print(ans) ```
14,844
Provide a correct Python 3 solution for this coding contest problem. There are N squares lining up in a row, numbered 1 through N from left to right. Initially, all squares are white. We also have N-1 painting machines, numbered 1 through N-1. When operated, Machine i paints Square i and i+1 black. Snuke will operate these machines one by one. The order in which he operates them is represented by a permutation of (1, 2, ..., N-1), P, which means that the i-th operated machine is Machine P_i. Here, the score of a permutation P is defined as the number of machines that are operated before all the squares are painted black for the first time, when the machines are operated in the order specified by P. Snuke has not decided what permutation P to use, but he is interested in the scores of possible permutations. Find the sum of the scores over all possible permutations for him. Since this can be extremely large, compute the sum modulo 10^9+7. Constraints * 2 \leq N \leq 10^6 Input Input is given from Standard Input in the following format: N Output Print the sum of the scores over all possible permutations, modulo 10^9+7. Examples Input 4 Output 16 Input 2 Output 1 Input 5 Output 84 Input 100000 Output 341429644 "Correct Solution: ``` import sys input = lambda : sys.stdin.readline().rstrip() sys.setrecursionlimit(max(1000, 10**9)) write = lambda x: sys.stdout.write(x+"\n") n = int(input()) M = 10**9+7 # 出力の制限 N = n+3 # 必要なテーブルサイズ g1 = [None] * (N+1) # 元テーブル g2 = [None] * (N+1) #逆元テーブル inverse = [None] * (N+1) #逆元テーブル計算用テーブル g1[0] = g1[1] = g2[0] = g2[1] = 1 inverse[0], inverse[1] = [0, 1] for i in range( 2, N + 1 ): g1[i] = ( g1[i-1] * i ) % M inverse[i] = ( -inverse[M % i] * (M//i) ) % M # ai+b==0 mod M <=> i==-b*a^(-1) <=> i^(-1)==-b^(-1)*aより g2[i] = (g2[i-1] * inverse[i]) % M def cmb(n, r, M): if ( r<0 or r>n ): return 0 r = min(r, n-r) return (g1[n] * g2[r] * g2[n-r]) % M ans = 0 prev = 0 # s = 0 for i in range((n+1)//2, n): tmp = (cmb(i-1, n-i-1, M) * g1[i] * g1[n-1-i]) ans += tmp # ans += i*(tmp-prev) prev = tmp # print(i, tmp, g1[i], g1[n-1-i]) ans %= M i = n-1 ans = (i+1) * (cmb(i-1, n-i-1, M) * g1[i] * g1[n-1-i]) - ans ans %= M print(ans) ```
14,845
Provide a correct Python 3 solution for this coding contest problem. There are N squares lining up in a row, numbered 1 through N from left to right. Initially, all squares are white. We also have N-1 painting machines, numbered 1 through N-1. When operated, Machine i paints Square i and i+1 black. Snuke will operate these machines one by one. The order in which he operates them is represented by a permutation of (1, 2, ..., N-1), P, which means that the i-th operated machine is Machine P_i. Here, the score of a permutation P is defined as the number of machines that are operated before all the squares are painted black for the first time, when the machines are operated in the order specified by P. Snuke has not decided what permutation P to use, but he is interested in the scores of possible permutations. Find the sum of the scores over all possible permutations for him. Since this can be extremely large, compute the sum modulo 10^9+7. Constraints * 2 \leq N \leq 10^6 Input Input is given from Standard Input in the following format: N Output Print the sum of the scores over all possible permutations, modulo 10^9+7. Examples Input 4 Output 16 Input 2 Output 1 Input 5 Output 84 Input 100000 Output 341429644 "Correct Solution: ``` import sys input = lambda : sys.stdin.readline().rstrip() sys.setrecursionlimit(max(1000, 10**9)) write = lambda x: sys.stdout.write(x+"\n") n = int(input()) M = 10**9+7 # 出力の制限 N = n+3 # 必要なテーブルサイズ g1 = [None] * (N+1) # 元テーブル g2 = [None] * (N+1) #逆元テーブル inverse = [None] * (N+1) #逆元テーブル計算用テーブル g1[0] = g1[1] = g2[0] = g2[1] = 1 inverse[0], inverse[1] = [0, 1] for i in range( 2, N + 1 ): g1[i] = ( g1[i-1] * i ) % M inverse[i] = ( -inverse[M % i] * (M//i) ) % M # ai+b==0 mod M <=> i==-b*a^(-1) <=> i^(-1)==-b^(-1)*aより g2[i] = (g2[i-1] * inverse[i]) % M def cmb(n, r, M): if ( r<0 or r>n ): return 0 r = min(r, n-r) return (g1[n] * g2[r] * g2[n-r]) % M ans = 0 prev = 0 # s = 0 for i in range((n+1)//2, n): tmp = (cmb(i-1, n-i-1, M) * g1[i] * g1[n-1-i]) ans += i*(tmp-prev) prev = tmp # print(i, tmp, g1[i], g1[n-1-i]) ans %= M print(ans) ```
14,846
Provide a correct Python 3 solution for this coding contest problem. There are N squares lining up in a row, numbered 1 through N from left to right. Initially, all squares are white. We also have N-1 painting machines, numbered 1 through N-1. When operated, Machine i paints Square i and i+1 black. Snuke will operate these machines one by one. The order in which he operates them is represented by a permutation of (1, 2, ..., N-1), P, which means that the i-th operated machine is Machine P_i. Here, the score of a permutation P is defined as the number of machines that are operated before all the squares are painted black for the first time, when the machines are operated in the order specified by P. Snuke has not decided what permutation P to use, but he is interested in the scores of possible permutations. Find the sum of the scores over all possible permutations for him. Since this can be extremely large, compute the sum modulo 10^9+7. Constraints * 2 \leq N \leq 10^6 Input Input is given from Standard Input in the following format: N Output Print the sum of the scores over all possible permutations, modulo 10^9+7. Examples Input 4 Output 16 Input 2 Output 1 Input 5 Output 84 Input 100000 Output 341429644 "Correct Solution: ``` n = int(input()) p = 10**9 + 7 def fact(n): n_ = 1 yield n_ for i in range(1, n+1): n_ = (n_*i) % p yield n_ def invfact(n, f, p): m = pow(f[n], p-2, p) yield m for i in range(n, 0, -1): m = m * i % p yield m ans = 0 m = n - 1 f = list(fact(m)) rf = list(invfact(m, f, p)) rf.reverse() perm = 0 for k in range((n+1)//2, n): b = m - k a = (m - 1) - (2 * b) perm_ = f[a+b] * rf[a] %p * f[k] % p ans += (perm_ - perm) %p * k % p ans %= p perm = perm_ print(ans) ```
14,847
Provide a correct Python 3 solution for this coding contest problem. There are N squares lining up in a row, numbered 1 through N from left to right. Initially, all squares are white. We also have N-1 painting machines, numbered 1 through N-1. When operated, Machine i paints Square i and i+1 black. Snuke will operate these machines one by one. The order in which he operates them is represented by a permutation of (1, 2, ..., N-1), P, which means that the i-th operated machine is Machine P_i. Here, the score of a permutation P is defined as the number of machines that are operated before all the squares are painted black for the first time, when the machines are operated in the order specified by P. Snuke has not decided what permutation P to use, but he is interested in the scores of possible permutations. Find the sum of the scores over all possible permutations for him. Since this can be extremely large, compute the sum modulo 10^9+7. Constraints * 2 \leq N \leq 10^6 Input Input is given from Standard Input in the following format: N Output Print the sum of the scores over all possible permutations, modulo 10^9+7. Examples Input 4 Output 16 Input 2 Output 1 Input 5 Output 84 Input 100000 Output 341429644 "Correct Solution: ``` def inv(x, mod = 10 ** 9 + 7): return pow(x, mod - 2, mod) mod = 10 ** 9 + 7 N = int(input()) fact = [1] for i in range(1, N): fact.append(fact[-1] * i % mod) inv_fact = [inv(fact[-1])] for i in reversed(range(1, N)): inv_fact.append(inv_fact[-1] * i % mod) inv_fact = inv_fact[::-1] ans = prev = 0 for k in range((N + 1) // 2, N): cnt = fact[k - 1] * inv_fact[k * 2 - N] % mod * fact[k] % mod ans += (cnt - prev + mod) * k ans %= mod prev = cnt print(ans) ```
14,848
Provide a correct Python 3 solution for this coding contest problem. There are N squares lining up in a row, numbered 1 through N from left to right. Initially, all squares are white. We also have N-1 painting machines, numbered 1 through N-1. When operated, Machine i paints Square i and i+1 black. Snuke will operate these machines one by one. The order in which he operates them is represented by a permutation of (1, 2, ..., N-1), P, which means that the i-th operated machine is Machine P_i. Here, the score of a permutation P is defined as the number of machines that are operated before all the squares are painted black for the first time, when the machines are operated in the order specified by P. Snuke has not decided what permutation P to use, but he is interested in the scores of possible permutations. Find the sum of the scores over all possible permutations for him. Since this can be extremely large, compute the sum modulo 10^9+7. Constraints * 2 \leq N \leq 10^6 Input Input is given from Standard Input in the following format: N Output Print the sum of the scores over all possible permutations, modulo 10^9+7. Examples Input 4 Output 16 Input 2 Output 1 Input 5 Output 84 Input 100000 Output 341429644 "Correct Solution: ``` n=int(input()) mod=10**9+7 fra=[1]*(n+2) inv=[1]*(n+2) t=1 for i in range(1,n+2): t*=i t%=mod fra[i]=t t=pow(fra[n+1],mod-2,mod) for i in range(n+1,0,-1): inv[i]=t t*=i t%=mod ans=fra[n] for i in range((n+1)//2,n): ans-=fra[i-1]*inv[2*i-n]*fra[i]%mod ans%=mod print(ans) ```
14,849
Provide a correct Python 3 solution for this coding contest problem. There are N squares lining up in a row, numbered 1 through N from left to right. Initially, all squares are white. We also have N-1 painting machines, numbered 1 through N-1. When operated, Machine i paints Square i and i+1 black. Snuke will operate these machines one by one. The order in which he operates them is represented by a permutation of (1, 2, ..., N-1), P, which means that the i-th operated machine is Machine P_i. Here, the score of a permutation P is defined as the number of machines that are operated before all the squares are painted black for the first time, when the machines are operated in the order specified by P. Snuke has not decided what permutation P to use, but he is interested in the scores of possible permutations. Find the sum of the scores over all possible permutations for him. Since this can be extremely large, compute the sum modulo 10^9+7. Constraints * 2 \leq N \leq 10^6 Input Input is given from Standard Input in the following format: N Output Print the sum of the scores over all possible permutations, modulo 10^9+7. Examples Input 4 Output 16 Input 2 Output 1 Input 5 Output 84 Input 100000 Output 341429644 "Correct Solution: ``` n = int(input()) fn,fk,mod = [1]*n,[1]*n,10**9+7 for i in range(n-1): fn[i+1] = (fn[i]*(i+2))%mod def power(n,k): if k==1: return n elif k%2==0: return power((n**2)%mod,k//2) else: return (n*power(n,k-1))%mod def comb(n,k): if n<k or k<0: return 0 elif k==0 or n==k: return 1 else: return (((fn[n-1]*fk[n-k-1])%mod)*fk[k-1])%mod fk[-1] = power(fn[-1],mod-2) for i in range(2,n+1): fk[-i] = (fk[-i+1]*(n+2-i))%mod fn.append(1) ans = fn[n-2]*(n-1) for i in range(n-2,(n-1)//2,-1): ans = (ans-comb(i-1,n-i-1)*fn[i-1]*fn[n-i-2])%mod print(ans) ```
14,850
Provide a correct Python 3 solution for this coding contest problem. There are N squares lining up in a row, numbered 1 through N from left to right. Initially, all squares are white. We also have N-1 painting machines, numbered 1 through N-1. When operated, Machine i paints Square i and i+1 black. Snuke will operate these machines one by one. The order in which he operates them is represented by a permutation of (1, 2, ..., N-1), P, which means that the i-th operated machine is Machine P_i. Here, the score of a permutation P is defined as the number of machines that are operated before all the squares are painted black for the first time, when the machines are operated in the order specified by P. Snuke has not decided what permutation P to use, but he is interested in the scores of possible permutations. Find the sum of the scores over all possible permutations for him. Since this can be extremely large, compute the sum modulo 10^9+7. Constraints * 2 \leq N \leq 10^6 Input Input is given from Standard Input in the following format: N Output Print the sum of the scores over all possible permutations, modulo 10^9+7. Examples Input 4 Output 16 Input 2 Output 1 Input 5 Output 84 Input 100000 Output 341429644 "Correct Solution: ``` n = int(input()) p = 10**9 + 7 def fact(n): n_ = 1 yield n_ for i in range(1, n+1): n_ = (n_*i) % p yield n_ def invfact(n, f, p): m = pow(f[n], p-2, p) yield m for i in range(n, 0, -1): m = m * i % p yield m ans = 0 m = n - 1 f = list(fact(m)) rf = list(invfact(m, f, p)) rf.reverse() perm = 0 for k in range((n+1)//2, n): perm_ = f[k-1] * rf[2*k-n] %p * f[k] % p ans += (perm_ - perm) %p * k % p ans %= p perm = perm_ print(ans) ```
14,851
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N squares lining up in a row, numbered 1 through N from left to right. Initially, all squares are white. We also have N-1 painting machines, numbered 1 through N-1. When operated, Machine i paints Square i and i+1 black. Snuke will operate these machines one by one. The order in which he operates them is represented by a permutation of (1, 2, ..., N-1), P, which means that the i-th operated machine is Machine P_i. Here, the score of a permutation P is defined as the number of machines that are operated before all the squares are painted black for the first time, when the machines are operated in the order specified by P. Snuke has not decided what permutation P to use, but he is interested in the scores of possible permutations. Find the sum of the scores over all possible permutations for him. Since this can be extremely large, compute the sum modulo 10^9+7. Constraints * 2 \leq N \leq 10^6 Input Input is given from Standard Input in the following format: N Output Print the sum of the scores over all possible permutations, modulo 10^9+7. Examples Input 4 Output 16 Input 2 Output 1 Input 5 Output 84 Input 100000 Output 341429644 Submitted Solution: ``` # seishin.py N = int(input()) MOD = 10**9 + 7 fact = [1]*(N+1) rfact = [1]*(N+1) for i in range(1, N+1): fact[i] = r = i*fact[i-1] % MOD rfact[i] = pow(r, MOD-2, MOD) ans = cnt = 0 for K in range((N+1)//2, N): res = fact[K]*fact[K-1]*rfact[2*K-N] % MOD ans += (res - cnt) * K % MOD cnt = res ans %= MOD print(ans) ``` Yes
14,852
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N squares lining up in a row, numbered 1 through N from left to right. Initially, all squares are white. We also have N-1 painting machines, numbered 1 through N-1. When operated, Machine i paints Square i and i+1 black. Snuke will operate these machines one by one. The order in which he operates them is represented by a permutation of (1, 2, ..., N-1), P, which means that the i-th operated machine is Machine P_i. Here, the score of a permutation P is defined as the number of machines that are operated before all the squares are painted black for the first time, when the machines are operated in the order specified by P. Snuke has not decided what permutation P to use, but he is interested in the scores of possible permutations. Find the sum of the scores over all possible permutations for him. Since this can be extremely large, compute the sum modulo 10^9+7. Constraints * 2 \leq N \leq 10^6 Input Input is given from Standard Input in the following format: N Output Print the sum of the scores over all possible permutations, modulo 10^9+7. Examples Input 4 Output 16 Input 2 Output 1 Input 5 Output 84 Input 100000 Output 341429644 Submitted Solution: ``` characteristic=10**9+7 def plus(input1,input2): return (input1+input2)%characteristic def minus(input1,input2): return (input1-input2)%characteristic def times(input1,input2): return (input1*input2)%characteristic def exponen(input1,input2): return pow(input1,input2,characteristic) def divide(input1,input2): return times(input1,exponen(input2,characteristic-2)) N=int(input()) Fact=[1 for i in range(N+1)] Finv=[1 for i in range(N+1)] for i in range(1,N+1): Fact[i]=times(Fact[i-1],i) Finv[i]=divide(1,Fact[i]) ans=0 SGL=[0 for i in range(N)] for K in range(N): if 2*K-N<0: continue SGL[K]=times(times(Fact[K],Finv[2*K-N]),Fact[K-1]) for K in range(1,N): ans=plus(ans,times(SGL[K]-SGL[K-1],K)) print(ans) ``` Yes
14,853
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N squares lining up in a row, numbered 1 through N from left to right. Initially, all squares are white. We also have N-1 painting machines, numbered 1 through N-1. When operated, Machine i paints Square i and i+1 black. Snuke will operate these machines one by one. The order in which he operates them is represented by a permutation of (1, 2, ..., N-1), P, which means that the i-th operated machine is Machine P_i. Here, the score of a permutation P is defined as the number of machines that are operated before all the squares are painted black for the first time, when the machines are operated in the order specified by P. Snuke has not decided what permutation P to use, but he is interested in the scores of possible permutations. Find the sum of the scores over all possible permutations for him. Since this can be extremely large, compute the sum modulo 10^9+7. Constraints * 2 \leq N \leq 10^6 Input Input is given from Standard Input in the following format: N Output Print the sum of the scores over all possible permutations, modulo 10^9+7. Examples Input 4 Output 16 Input 2 Output 1 Input 5 Output 84 Input 100000 Output 341429644 Submitted Solution: ``` P=10**9+7 def egcd(a, b): (x, lastx) = (0, 1) (y, lasty) = (1, 0) while b != 0: q = a // b (a, b) = (b, a % b) (x, lastx) = (lastx - q * x, x) (y, lasty) = (lasty - q * y, y) return (lastx, lasty, a) def inv(x): return egcd(x,P)[0] N=int(input()) Fact=[0 for i in range(N+1)] Finv=[0 for i in range(N+1)] Fact[0]=1 Finv[0]=1 for i in range(N): Fact[i+1]=((i+1)*Fact[i])%P Finv[i+1]=(Finv[i]*inv(i+1))%P SGN=[0 for i in range(N)] ans=0 for k in range(N): if 2*k-N>=0: SGN[k]=(((Fact[k-1]*Fact[k])%P)*Finv[2*k-N])%P ans=(ans+k*(SGN[k]-SGN[k-1]))%P print(ans) ``` Yes
14,854
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N squares lining up in a row, numbered 1 through N from left to right. Initially, all squares are white. We also have N-1 painting machines, numbered 1 through N-1. When operated, Machine i paints Square i and i+1 black. Snuke will operate these machines one by one. The order in which he operates them is represented by a permutation of (1, 2, ..., N-1), P, which means that the i-th operated machine is Machine P_i. Here, the score of a permutation P is defined as the number of machines that are operated before all the squares are painted black for the first time, when the machines are operated in the order specified by P. Snuke has not decided what permutation P to use, but he is interested in the scores of possible permutations. Find the sum of the scores over all possible permutations for him. Since this can be extremely large, compute the sum modulo 10^9+7. Constraints * 2 \leq N \leq 10^6 Input Input is given from Standard Input in the following format: N Output Print the sum of the scores over all possible permutations, modulo 10^9+7. Examples Input 4 Output 16 Input 2 Output 1 Input 5 Output 84 Input 100000 Output 341429644 Submitted Solution: ``` N = int(input()) - 1 LARGE = 10**9+7 def ex_euclid(x, y): c0, c1 = x, y a0, a1 = 1, 0 b0, b1 = 0, 1 while c1 != 0: m = c0 % c1 q = c0 // c1 c0, c1 = c1, m a0, a1 = a1, (a0 - q * a1) b0, b1 = b1, (b0 - q * b1) return c0, a0, b0 # precompute fac_list = [1]*(N+1) fac = 1 for i in range(1, N+1): fac = (fac * i) % LARGE fac_list[i] = fac fac_inv_list = [1]*(N+1) for i in range(N+1): fac_inv_list[i] = pow(fac_list[i], LARGE-2, LARGE) def nCr(n, r): return (((fac_list[n] * fac_inv_list[r]) % LARGE) * fac_inv_list[n-r]) % LARGE def pat(n, r): return (((fac_list[n] * fac_inv_list[r]) % LARGE) * fac_inv_list[n-r]) % LARGE pat = 0 score = 0 for k in range(N+1): if k-1 >= N-k: res = (((fac_list[k-1]*fac_list[k]) % LARGE) * fac_inv_list[k-1-N+k]) % LARGE score = (score + (res - pat) * k) % LARGE # print(k, pat, res) pat = res print(score) ``` Yes
14,855
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N squares lining up in a row, numbered 1 through N from left to right. Initially, all squares are white. We also have N-1 painting machines, numbered 1 through N-1. When operated, Machine i paints Square i and i+1 black. Snuke will operate these machines one by one. The order in which he operates them is represented by a permutation of (1, 2, ..., N-1), P, which means that the i-th operated machine is Machine P_i. Here, the score of a permutation P is defined as the number of machines that are operated before all the squares are painted black for the first time, when the machines are operated in the order specified by P. Snuke has not decided what permutation P to use, but he is interested in the scores of possible permutations. Find the sum of the scores over all possible permutations for him. Since this can be extremely large, compute the sum modulo 10^9+7. Constraints * 2 \leq N \leq 10^6 Input Input is given from Standard Input in the following format: N Output Print the sum of the scores over all possible permutations, modulo 10^9+7. Examples Input 4 Output 16 Input 2 Output 1 Input 5 Output 84 Input 100000 Output 341429644 Submitted Solution: ``` def permutations(L): if L == []: return [[]] else: return [[h]+t for i,h in enumerate(L) for t in permutations(L[:i]+L[i+1:])] n = int(input()) perm_list = [] for i in range(n - 1): perm_list.append(str(i+1)) perm_result = permutations(perm_list) #print(perm_result) score = 0 for p in perm_result: m = [0] * n for j in range(len(p)): m[int(p[j])-1] = 1 m[int(p[j])] = 1 mini_score = 0 for i in range(n-1): mini_score += m[i] if mini_score == n-1: score += j+1 break print(score) ``` No
14,856
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N squares lining up in a row, numbered 1 through N from left to right. Initially, all squares are white. We also have N-1 painting machines, numbered 1 through N-1. When operated, Machine i paints Square i and i+1 black. Snuke will operate these machines one by one. The order in which he operates them is represented by a permutation of (1, 2, ..., N-1), P, which means that the i-th operated machine is Machine P_i. Here, the score of a permutation P is defined as the number of machines that are operated before all the squares are painted black for the first time, when the machines are operated in the order specified by P. Snuke has not decided what permutation P to use, but he is interested in the scores of possible permutations. Find the sum of the scores over all possible permutations for him. Since this can be extremely large, compute the sum modulo 10^9+7. Constraints * 2 \leq N \leq 10^6 Input Input is given from Standard Input in the following format: N Output Print the sum of the scores over all possible permutations, modulo 10^9+7. Examples Input 4 Output 16 Input 2 Output 1 Input 5 Output 84 Input 100000 Output 341429644 Submitted Solution: ``` n = int(input()) p = 10**9 + 7 def fact(n, p): n_ = [1] for i in range(1, n+1): n_.append((n_[-1]*i) % p) return n def invfact(n, f, p): m = [pow(f[n], p-2, p)] for i in range(n, 0, -1): m.append(m[-1] * i % p) return m ans = 0 m = n - 1 f = fact(m, p) rf = invfact(m, f, p) rf.reverse() perm = 0 for k in range((n+1)//2, n): perm_ = f[k-1] * rf[2*k-n] %p * f[k] % p ans += (perm_ - perm) %p * k % p ans %= p perm = perm_ print(ans) ``` No
14,857
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N squares lining up in a row, numbered 1 through N from left to right. Initially, all squares are white. We also have N-1 painting machines, numbered 1 through N-1. When operated, Machine i paints Square i and i+1 black. Snuke will operate these machines one by one. The order in which he operates them is represented by a permutation of (1, 2, ..., N-1), P, which means that the i-th operated machine is Machine P_i. Here, the score of a permutation P is defined as the number of machines that are operated before all the squares are painted black for the first time, when the machines are operated in the order specified by P. Snuke has not decided what permutation P to use, but he is interested in the scores of possible permutations. Find the sum of the scores over all possible permutations for him. Since this can be extremely large, compute the sum modulo 10^9+7. Constraints * 2 \leq N \leq 10^6 Input Input is given from Standard Input in the following format: N Output Print the sum of the scores over all possible permutations, modulo 10^9+7. Examples Input 4 Output 16 Input 2 Output 1 Input 5 Output 84 Input 100000 Output 341429644 Submitted Solution: ``` n = int(input()) p = 10**9 + 7 def fact(n): n_ = 1 yield n_ for i in range(1, n+1): n_ = (n_*i) % p yield n_ ans = 0 m = n - 1 f = list(fact(m)) perm = 0 for k in range((n+1)//2, n): b = m - k a = (m - 1) - (2 * b) perm_ = f[a+b] * pow(f[a], p-2, p) %p * f[k] % p ans += (perm_ - perm) %p * k % p ans %= p perm = perm_ print(ans) ``` No
14,858
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N squares lining up in a row, numbered 1 through N from left to right. Initially, all squares are white. We also have N-1 painting machines, numbered 1 through N-1. When operated, Machine i paints Square i and i+1 black. Snuke will operate these machines one by one. The order in which he operates them is represented by a permutation of (1, 2, ..., N-1), P, which means that the i-th operated machine is Machine P_i. Here, the score of a permutation P is defined as the number of machines that are operated before all the squares are painted black for the first time, when the machines are operated in the order specified by P. Snuke has not decided what permutation P to use, but he is interested in the scores of possible permutations. Find the sum of the scores over all possible permutations for him. Since this can be extremely large, compute the sum modulo 10^9+7. Constraints * 2 \leq N \leq 10^6 Input Input is given from Standard Input in the following format: N Output Print the sum of the scores over all possible permutations, modulo 10^9+7. Examples Input 4 Output 16 Input 2 Output 1 Input 5 Output 84 Input 100000 Output 341429644 Submitted Solution: ``` n = int(input()) p = 10**9 + 7 # def fact(n, p): # n_ = 1 # yield n_ # for i in range(1, n+1): # n_ = (n_*i) % p # yield n_ # def invfact(n, f, p): # m = pow(f[n], p-2, p) # yield m # for i in range(n, 0, -1): # m = m * i % p # yield m def fact(n, p): n_ = [1] for i in range(1, n+1): n_.append((n_[-1]*i) % p) return n def invfact(n, f, p): m = [pow(f[n], p-2, p)] for i in range(n, 0, -1): m.append(m[-1] * i % p) return m ans = 0 m = n - 1 f = fact(m, p) rf = invfact(m, f, p) rf.reverse() perm = 0 for k in range((n+1)//2, n): perm_ = f[k-1] * rf[2*k-n] %p * f[k] % p ans += (perm_ - perm) %p * k % p ans %= p perm = perm_ print(ans) ``` No
14,859
Provide a correct Python 3 solution for this coding contest problem. You are given a string S. Takahashi can insert the character `A` at any position in this string any number of times. Can he change S into `AKIHABARA`? Constraints * 1 \leq |S| \leq 50 * S consists of uppercase English letters. Input Input is given from Standard Input in the following format: S Output If it is possible to change S into `AKIHABARA`, print `YES`; otherwise, print `NO`. Examples Input KIHBR Output YES Input AKIBAHARA Output NO Input AAKIAHBAARA Output NO "Correct Solution: ``` import re s = input() if re.match('A?KIHA?BA?RA?$', s): print('YES') else: print('NO') ```
14,860
Provide a correct Python 3 solution for this coding contest problem. You are given a string S. Takahashi can insert the character `A` at any position in this string any number of times. Can he change S into `AKIHABARA`? Constraints * 1 \leq |S| \leq 50 * S consists of uppercase English letters. Input Input is given from Standard Input in the following format: S Output If it is possible to change S into `AKIHABARA`, print `YES`; otherwise, print `NO`. Examples Input KIHBR Output YES Input AKIBAHARA Output NO Input AAKIAHBAARA Output NO "Correct Solution: ``` import re S = input() if re.match('A?KIHA?BA?RA?$', S): print('YES') else: print('NO') ```
14,861
Provide a correct Python 3 solution for this coding contest problem. You are given a string S. Takahashi can insert the character `A` at any position in this string any number of times. Can he change S into `AKIHABARA`? Constraints * 1 \leq |S| \leq 50 * S consists of uppercase English letters. Input Input is given from Standard Input in the following format: S Output If it is possible to change S into `AKIHABARA`, print `YES`; otherwise, print `NO`. Examples Input KIHBR Output YES Input AKIBAHARA Output NO Input AAKIAHBAARA Output NO "Correct Solution: ``` S=input() b=S.replace("A","")=="KIHBR" if b: L=list(map(len, "".join("*" if s!="A" else s for s in S).split("*"))) X=[1,0,0,1,1,1,1] b&=all(L[i]<=X[i] for i in range(len(L))) print("YNEOS"[not b::2]) ```
14,862
Provide a correct Python 3 solution for this coding contest problem. You are given a string S. Takahashi can insert the character `A` at any position in this string any number of times. Can he change S into `AKIHABARA`? Constraints * 1 \leq |S| \leq 50 * S consists of uppercase English letters. Input Input is given from Standard Input in the following format: S Output If it is possible to change S into `AKIHABARA`, print `YES`; otherwise, print `NO`. Examples Input KIHBR Output YES Input AKIBAHARA Output NO Input AAKIAHBAARA Output NO "Correct Solution: ``` s = input() a = 'AKIHABARA' a = list(a) idxs = [0, 4, 6, 8] for i in range(2**4): for j in range(4): a[idxs[j]] = 'A' if i>>j&1 else '' if s==''.join(a): print('YES') exit(0) print('NO') ```
14,863
Provide a correct Python 3 solution for this coding contest problem. You are given a string S. Takahashi can insert the character `A` at any position in this string any number of times. Can he change S into `AKIHABARA`? Constraints * 1 \leq |S| \leq 50 * S consists of uppercase English letters. Input Input is given from Standard Input in the following format: S Output If it is possible to change S into `AKIHABARA`, print `YES`; otherwise, print `NO`. Examples Input KIHBR Output YES Input AKIBAHARA Output NO Input AAKIAHBAARA Output NO "Correct Solution: ``` import re s = input() if re.fullmatch(r'A?KIHA?BA?RA?', s) == None: print('NO') else: print('YES') ```
14,864
Provide a correct Python 3 solution for this coding contest problem. You are given a string S. Takahashi can insert the character `A` at any position in this string any number of times. Can he change S into `AKIHABARA`? Constraints * 1 \leq |S| \leq 50 * S consists of uppercase English letters. Input Input is given from Standard Input in the following format: S Output If it is possible to change S into `AKIHABARA`, print `YES`; otherwise, print `NO`. Examples Input KIHBR Output YES Input AKIBAHARA Output NO Input AAKIAHBAARA Output NO "Correct Solution: ``` import re print("YES" if re.match(r"^A?KIHA?BA?RA?$", input()) else "NO") ```
14,865
Provide a correct Python 3 solution for this coding contest problem. You are given a string S. Takahashi can insert the character `A` at any position in this string any number of times. Can he change S into `AKIHABARA`? Constraints * 1 \leq |S| \leq 50 * S consists of uppercase English letters. Input Input is given from Standard Input in the following format: S Output If it is possible to change S into `AKIHABARA`, print `YES`; otherwise, print `NO`. Examples Input KIHBR Output YES Input AKIBAHARA Output NO Input AAKIAHBAARA Output NO "Correct Solution: ``` S=input() akh=list("AKIHABARA") lis=[] a=[0,4,6,8] for i in range(16): akh=list("AKIHABARA") for j in range(4): if (i>>j) & 1: akh[a[j]]="" lis.append("".join(akh)) if S in lis:print("YES") else:print("NO") ```
14,866
Provide a correct Python 3 solution for this coding contest problem. You are given a string S. Takahashi can insert the character `A` at any position in this string any number of times. Can he change S into `AKIHABARA`? Constraints * 1 \leq |S| \leq 50 * S consists of uppercase English letters. Input Input is given from Standard Input in the following format: S Output If it is possible to change S into `AKIHABARA`, print `YES`; otherwise, print `NO`. Examples Input KIHBR Output YES Input AKIBAHARA Output NO Input AAKIAHBAARA Output NO "Correct Solution: ``` S = input().strip() if S.find('AA') != -1: print('NO') exit() if S.find('KAI') != -1: print('NO') exit() if S.find('IAH') != -1: print('NO') exit() if S.replace('A', '') == 'KIHBR': print('YES') else: print('NO') ```
14,867
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S. Takahashi can insert the character `A` at any position in this string any number of times. Can he change S into `AKIHABARA`? Constraints * 1 \leq |S| \leq 50 * S consists of uppercase English letters. Input Input is given from Standard Input in the following format: S Output If it is possible to change S into `AKIHABARA`, print `YES`; otherwise, print `NO`. Examples Input KIHBR Output YES Input AKIBAHARA Output NO Input AAKIAHBAARA Output NO Submitted Solution: ``` import re pattern = '^A?KIHA?BA?RA?$' print("YES" if re.match(pattern,input()) else "NO") ``` Yes
14,868
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S. Takahashi can insert the character `A` at any position in this string any number of times. Can he change S into `AKIHABARA`? Constraints * 1 \leq |S| \leq 50 * S consists of uppercase English letters. Input Input is given from Standard Input in the following format: S Output If it is possible to change S into `AKIHABARA`, print `YES`; otherwise, print `NO`. Examples Input KIHBR Output YES Input AKIBAHARA Output NO Input AAKIAHBAARA Output NO Submitted Solution: ``` s = input() flag = 1 if s.find("AA") != -1: flag = 0 if s.find("KA") != -1: flag = 0 if s.find("IA") != -1: flag = 0 if s.replace("A","") != "KIHBR": flag = 0 if flag == 1: print("YES") else: print("NO") ``` Yes
14,869
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S. Takahashi can insert the character `A` at any position in this string any number of times. Can he change S into `AKIHABARA`? Constraints * 1 \leq |S| \leq 50 * S consists of uppercase English letters. Input Input is given from Standard Input in the following format: S Output If it is possible to change S into `AKIHABARA`, print `YES`; otherwise, print `NO`. Examples Input KIHBR Output YES Input AKIBAHARA Output NO Input AAKIAHBAARA Output NO Submitted Solution: ``` S = input() T = ["AKIHABARA","KIHABARA","AKIHBARA","AKIHABRA","AKIHABAR"\ ,"KIHBARA","KIHABRA","KIHABAR","AKIHBRA","AKIHBAR","AKIHABR"\ ,"KIHBRA","KIHBAR","KIHABR","AKIHBR","KIHBR"] print("YES" if S in T else "NO") ``` Yes
14,870
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S. Takahashi can insert the character `A` at any position in this string any number of times. Can he change S into `AKIHABARA`? Constraints * 1 \leq |S| \leq 50 * S consists of uppercase English letters. Input Input is given from Standard Input in the following format: S Output If it is possible to change S into `AKIHABARA`, print `YES`; otherwise, print `NO`. Examples Input KIHBR Output YES Input AKIBAHARA Output NO Input AAKIAHBAARA Output NO Submitted Solution: ``` import re;print('YNEOS'[re.match('A?KIHA?BA?RA?$',input())==None::2]) ``` Yes
14,871
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S. Takahashi can insert the character `A` at any position in this string any number of times. Can he change S into `AKIHABARA`? Constraints * 1 \leq |S| \leq 50 * S consists of uppercase English letters. Input Input is given from Standard Input in the following format: S Output If it is possible to change S into `AKIHABARA`, print `YES`; otherwise, print `NO`. Examples Input KIHBR Output YES Input AKIBAHARA Output NO Input AAKIAHBAARA Output NO Submitted Solution: ``` s = input() if len(s) > 9: print("NO") exit() for i in range(len(s)-1): if s[i] + s[i+1] == "AA": print("NO") exit() s = "".join([i for i in s if i != "A"]) c = "KIHBR" print("YES") if s == c else print("NO") ``` No
14,872
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S. Takahashi can insert the character `A` at any position in this string any number of times. Can he change S into `AKIHABARA`? Constraints * 1 \leq |S| \leq 50 * S consists of uppercase English letters. Input Input is given from Standard Input in the following format: S Output If it is possible to change S into `AKIHABARA`, print `YES`; otherwise, print `NO`. Examples Input KIHBR Output YES Input AKIBAHARA Output NO Input AAKIAHBAARA Output NO Submitted Solution: ``` S = input().strip() if S.find('AA') >= 0: print('NO') exit() if S.replace('A', '') == 'KIHBR': print('YES') else: print('NO') ``` No
14,873
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S. Takahashi can insert the character `A` at any position in this string any number of times. Can he change S into `AKIHABARA`? Constraints * 1 \leq |S| \leq 50 * S consists of uppercase English letters. Input Input is given from Standard Input in the following format: S Output If it is possible to change S into `AKIHABARA`, print `YES`; otherwise, print `NO`. Examples Input KIHBR Output YES Input AKIBAHARA Output NO Input AAKIAHBAARA Output NO Submitted Solution: ``` def main(): s = str(input()) t = 'AKIHABARA' i = 0 j = 0 while i < len(t) and j < len(s): if t[i] == s[j]: i += 1 j += 1 else: i += 1 print('YES' if j == len(s) - 1 else 'NO') if __name__ == '__main__': main() ``` No
14,874
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string S. Takahashi can insert the character `A` at any position in this string any number of times. Can he change S into `AKIHABARA`? Constraints * 1 \leq |S| \leq 50 * S consists of uppercase English letters. Input Input is given from Standard Input in the following format: S Output If it is possible to change S into `AKIHABARA`, print `YES`; otherwise, print `NO`. Examples Input KIHBR Output YES Input AKIBAHARA Output NO Input AAKIAHBAARA Output NO Submitted Solution: ``` s = input() ans = "AKIHABARA" now = 0 for i in s: while now < len(ans) and ans[now] != i: now += 1 if now <= 8: print("YES") else: print("NO") ``` No
14,875
Provide a correct Python 3 solution for this coding contest problem. There are N cats. We number them from 1 through N. Each of the cats wears a hat. Cat i says: "there are exactly a_i different colors among the N - 1 hats worn by the cats except me." Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats. Constraints * 2 ≤ N ≤ 10^5 * 1 ≤ a_i ≤ N-1 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print `Yes` if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print `No` otherwise. Examples Input 3 1 2 2 Output Yes Input 3 1 1 2 Output No Input 5 4 3 4 3 4 Output No Input 3 2 2 2 Output Yes Input 4 2 2 2 2 Output Yes Input 5 3 3 3 3 3 Output No "Correct Solution: ``` N = int(input()) a = list(map(int, input().split())) seta = set(a) if len(seta) == 1: if a[0] in (0, N-1) or a[0] <= N // 2: print("Yes") else: print("No") elif len(seta) == 2: a1 = min(seta) a2 = max(seta) counta1 = a.count(a1) if a2 != a1 + 1: print("No") elif counta1 < a2 and counta1 + 2 * (a2 - counta1) <= N: print("Yes") else: print("No") else: print("No") ```
14,876
Provide a correct Python 3 solution for this coding contest problem. There are N cats. We number them from 1 through N. Each of the cats wears a hat. Cat i says: "there are exactly a_i different colors among the N - 1 hats worn by the cats except me." Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats. Constraints * 2 ≤ N ≤ 10^5 * 1 ≤ a_i ≤ N-1 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print `Yes` if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print `No` otherwise. Examples Input 3 1 2 2 Output Yes Input 3 1 1 2 Output No Input 5 4 3 4 3 4 Output No Input 3 2 2 2 Output Yes Input 4 2 2 2 2 Output Yes Input 5 3 3 3 3 3 Output No "Correct Solution: ``` n = int(input()) a = list(map(int, input().split())) Min = min(a) Max = max(a) if Max - Min > 1: print("No") elif Max == Min: if a[0] == n - 1 or a[0] <= n // 2: print("Yes") else: print("No") else: c_M = a.count(Max) c_m = n - c_M if c_m < Max <= c_m + c_M // 2: print("Yes") else: print("No") ```
14,877
Provide a correct Python 3 solution for this coding contest problem. There are N cats. We number them from 1 through N. Each of the cats wears a hat. Cat i says: "there are exactly a_i different colors among the N - 1 hats worn by the cats except me." Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats. Constraints * 2 ≤ N ≤ 10^5 * 1 ≤ a_i ≤ N-1 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print `Yes` if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print `No` otherwise. Examples Input 3 1 2 2 Output Yes Input 3 1 1 2 Output No Input 5 4 3 4 3 4 Output No Input 3 2 2 2 Output Yes Input 4 2 2 2 2 Output Yes Input 5 3 3 3 3 3 Output No "Correct Solution: ``` N = int(input()) A = sorted(list(map(int,input().split())),reverse=True) if A[0]==A[-1] and (A[0]==1 or A[0]==N-1): print("Yes") else: k = A[0] cnt = A.count(k-1) n = N-cnt k -= cnt if k<=0: print("No") else: n -= 2*k if n>=0: print("Yes") else: print("No") ```
14,878
Provide a correct Python 3 solution for this coding contest problem. There are N cats. We number them from 1 through N. Each of the cats wears a hat. Cat i says: "there are exactly a_i different colors among the N - 1 hats worn by the cats except me." Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats. Constraints * 2 ≤ N ≤ 10^5 * 1 ≤ a_i ≤ N-1 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print `Yes` if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print `No` otherwise. Examples Input 3 1 2 2 Output Yes Input 3 1 1 2 Output No Input 5 4 3 4 3 4 Output No Input 3 2 2 2 Output Yes Input 4 2 2 2 2 Output Yes Input 5 3 3 3 3 3 Output No "Correct Solution: ``` n = int(input()) a = list(map(int,input().split())) L = len(set(a)) if L ==1: if a[0] < n//2+1 or a[0]==n-1: print('Yes') else: print('No') elif L == 2: M,m =max(a),min(a) m_c =a.count(m) if M == m+1 and m_c <= m and n-m_c >= 2*(M-m_c): print('Yes') else: print('No') else: print('No') ```
14,879
Provide a correct Python 3 solution for this coding contest problem. There are N cats. We number them from 1 through N. Each of the cats wears a hat. Cat i says: "there are exactly a_i different colors among the N - 1 hats worn by the cats except me." Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats. Constraints * 2 ≤ N ≤ 10^5 * 1 ≤ a_i ≤ N-1 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print `Yes` if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print `No` otherwise. Examples Input 3 1 2 2 Output Yes Input 3 1 1 2 Output No Input 5 4 3 4 3 4 Output No Input 3 2 2 2 Output Yes Input 4 2 2 2 2 Output Yes Input 5 3 3 3 3 3 Output No "Correct Solution: ``` n = int(input()) a = list(map(int, input().split())) a.sort() cnt = 0 for i in range(n): if a[i] == a[0]: cnt = i+1 else: break if a[-1] - a[0] > 1: print('No') exit() elif a[-1] - a[0] == 1: actual = a[-1] if actual >= n: print('No') elif cnt <= actual - 1 and actual <= cnt + (n - cnt) // 2: print('Yes') else: print('No') exit() elif a[-1] - a[0] == 0: solve = False actual = a[0] if n >= actual * 2 or n-1 == actual: solve = True if solve: print('Yes') else: print('No') exit() else: print('No') ```
14,880
Provide a correct Python 3 solution for this coding contest problem. There are N cats. We number them from 1 through N. Each of the cats wears a hat. Cat i says: "there are exactly a_i different colors among the N - 1 hats worn by the cats except me." Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats. Constraints * 2 ≤ N ≤ 10^5 * 1 ≤ a_i ≤ N-1 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print `Yes` if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print `No` otherwise. Examples Input 3 1 2 2 Output Yes Input 3 1 1 2 Output No Input 5 4 3 4 3 4 Output No Input 3 2 2 2 Output Yes Input 4 2 2 2 2 Output Yes Input 5 3 3 3 3 3 Output No "Correct Solution: ``` N = int(input()) a = list(map(int, input().split())) M = max(a) m = min(a) if M > m + 1: print("No") else: if M == m: if M == N-1: print("Yes") elif M*2 <= N: print("Yes") else: print("No") else: # ai == m ならば、ai以外にその色はない c = a.count(m) if (M - c) * 2 <= N-c and c < M: # (M-c) -> 一人だけではない色の種類一人だけではないはずなので*2 したものよりも残りの人数が多いはず print("Yes") else: print("No") ```
14,881
Provide a correct Python 3 solution for this coding contest problem. There are N cats. We number them from 1 through N. Each of the cats wears a hat. Cat i says: "there are exactly a_i different colors among the N - 1 hats worn by the cats except me." Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats. Constraints * 2 ≤ N ≤ 10^5 * 1 ≤ a_i ≤ N-1 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print `Yes` if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print `No` otherwise. Examples Input 3 1 2 2 Output Yes Input 3 1 1 2 Output No Input 5 4 3 4 3 4 Output No Input 3 2 2 2 Output Yes Input 4 2 2 2 2 Output Yes Input 5 3 3 3 3 3 Output No "Correct Solution: ``` N = int(input()) a = list(map(int, input().split())) ma = max(a) mi = min(a) if ma - mi >= 2: print('No') elif ma == mi: if a[0] == N - 1: print('Yes') elif 2 * a[0] <= N: print('Yes') else: print('No') else: al = 0 na = 0 for i in range(N): if a[i] == ma - 1: al += 1 else: na += 1 if al < ma and 2 * (ma - al) <= na: print('Yes') else: print('No') ```
14,882
Provide a correct Python 3 solution for this coding contest problem. There are N cats. We number them from 1 through N. Each of the cats wears a hat. Cat i says: "there are exactly a_i different colors among the N - 1 hats worn by the cats except me." Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats. Constraints * 2 ≤ N ≤ 10^5 * 1 ≤ a_i ≤ N-1 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print `Yes` if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print `No` otherwise. Examples Input 3 1 2 2 Output Yes Input 3 1 1 2 Output No Input 5 4 3 4 3 4 Output No Input 3 2 2 2 Output Yes Input 4 2 2 2 2 Output Yes Input 5 3 3 3 3 3 Output No "Correct Solution: ``` n = int(input()) a = sorted(list(map(int, input().split()))) data = [[-1, 0]] for x in a: if data[-1][0] == x: data[-1][1] += 1 else: data.append([x, 1]) data = data[1:] if len(data) == 1: print("Yes" if data[0][0] * 2 <= n or data[0][0] + 1 == n else "No") elif len(data) == 2: if data[0][0] + 1 == data[1][0] and data[0][0] >= data[0][1] and data[1][1] >= 2 * (data[1][0] - data[0][1]): print("Yes") else: print("No") else: print("No") ```
14,883
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cats. We number them from 1 through N. Each of the cats wears a hat. Cat i says: "there are exactly a_i different colors among the N - 1 hats worn by the cats except me." Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats. Constraints * 2 ≤ N ≤ 10^5 * 1 ≤ a_i ≤ N-1 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print `Yes` if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print `No` otherwise. Examples Input 3 1 2 2 Output Yes Input 3 1 1 2 Output No Input 5 4 3 4 3 4 Output No Input 3 2 2 2 Output Yes Input 4 2 2 2 2 Output Yes Input 5 3 3 3 3 3 Output No Submitted Solution: ``` N=int(input()) A=[int(x) for x in input().split()] ma=max(A) mi=min(A) ans=True if ma-mi > 1: ans=False elif ma==mi: if ma!=N-1 and ma*2 > N: ans=False else: uniq=ma*N - sum(A) no_uni=N-uniq if no_uni==1: ans=False else: if uniq >= ma or 2*(ma-uniq) > no_uni: ans=False if ans: print("Yes") else: print("No") ``` Yes
14,884
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cats. We number them from 1 through N. Each of the cats wears a hat. Cat i says: "there are exactly a_i different colors among the N - 1 hats worn by the cats except me." Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats. Constraints * 2 ≤ N ≤ 10^5 * 1 ≤ a_i ≤ N-1 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print `Yes` if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print `No` otherwise. Examples Input 3 1 2 2 Output Yes Input 3 1 1 2 Output No Input 5 4 3 4 3 4 Output No Input 3 2 2 2 Output Yes Input 4 2 2 2 2 Output Yes Input 5 3 3 3 3 3 Output No Submitted Solution: ``` import sys N=int(input()) a=[int(i) for i in input().split()] a.sort() if a[N-1]-a[0]>1: print("No") sys.exit() if a[0]==N-1: print("Yes") sys.exit() if a[N-1]==1: print("Yes") sys.exit() if a[0]==a[N-1]: if 2*a[0]<=N: print("Yes") else: print("No") sys.exit() count=a.index(a[N-1]) if count+1<=a[N-1] and a[N-1]<=count+int((N-count)/2): print("Yes") else: print("No") ``` Yes
14,885
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cats. We number them from 1 through N. Each of the cats wears a hat. Cat i says: "there are exactly a_i different colors among the N - 1 hats worn by the cats except me." Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats. Constraints * 2 ≤ N ≤ 10^5 * 1 ≤ a_i ≤ N-1 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print `Yes` if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print `No` otherwise. Examples Input 3 1 2 2 Output Yes Input 3 1 1 2 Output No Input 5 4 3 4 3 4 Output No Input 3 2 2 2 Output Yes Input 4 2 2 2 2 Output Yes Input 5 3 3 3 3 3 Output No Submitted Solution: ``` n = int(input()) a = list(map(int,input().split())) s = set(a) if len(s)>=3: print('No') elif len(s)==2: #頑張る mx = max(a) mn = min(a) y = a.count(mx) x = a.count(mn) if mx-mn>1: print('No') elif x < mx and 2*(mx-x)<=y: print('Yes') else: print('No') else: if a[0]==n-1: print('Yes') elif a[0]*2<=n: print('Yes') else: print('No') ``` Yes
14,886
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cats. We number them from 1 through N. Each of the cats wears a hat. Cat i says: "there are exactly a_i different colors among the N - 1 hats worn by the cats except me." Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats. Constraints * 2 ≤ N ≤ 10^5 * 1 ≤ a_i ≤ N-1 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print `Yes` if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print `No` otherwise. Examples Input 3 1 2 2 Output Yes Input 3 1 1 2 Output No Input 5 4 3 4 3 4 Output No Input 3 2 2 2 Output Yes Input 4 2 2 2 2 Output Yes Input 5 3 3 3 3 3 Output No Submitted Solution: ``` n = int(input()) a = list(map(int, input().split())) x = min(a) y = max(a) if y - x > 1: print('No') elif y == x: if n == x + 1 or 2 * x <= n: print('Yes') else: print('No') else: x_cnt = a.count(x) y_cnt = a.count(y) if y > x_cnt and 2 * (y - x_cnt) <= y_cnt: print('Yes') else: print('No') ``` Yes
14,887
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cats. We number them from 1 through N. Each of the cats wears a hat. Cat i says: "there are exactly a_i different colors among the N - 1 hats worn by the cats except me." Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats. Constraints * 2 ≤ N ≤ 10^5 * 1 ≤ a_i ≤ N-1 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print `Yes` if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print `No` otherwise. Examples Input 3 1 2 2 Output Yes Input 3 1 1 2 Output No Input 5 4 3 4 3 4 Output No Input 3 2 2 2 Output Yes Input 4 2 2 2 2 Output Yes Input 5 3 3 3 3 3 Output No Submitted Solution: ``` import numpy as np H,W,h,w = map(int,input().split()) # H,W,h,w = (3,4,2,3) hrep = H//h +1 wrep = W//w +1 elem = np.ones((h,w),dtype=int) elem[-1,-1] = -h*w mat = np.tile(elem, (hrep,wrep)) mat = mat[:H,:W] if mat.sum() > 0: print("Yes") for i in mat: print(" ".join(map(str,i))) else: print("No") ``` No
14,888
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cats. We number them from 1 through N. Each of the cats wears a hat. Cat i says: "there are exactly a_i different colors among the N - 1 hats worn by the cats except me." Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats. Constraints * 2 ≤ N ≤ 10^5 * 1 ≤ a_i ≤ N-1 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print `Yes` if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print `No` otherwise. Examples Input 3 1 2 2 Output Yes Input 3 1 1 2 Output No Input 5 4 3 4 3 4 Output No Input 3 2 2 2 Output Yes Input 4 2 2 2 2 Output Yes Input 5 3 3 3 3 3 Output No Submitted Solution: ``` import sys, re from collections import deque, defaultdict, Counter from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby from operator import itemgetter, mul from copy import deepcopy from string import ascii_lowercase, ascii_uppercase, digits from bisect import bisect, bisect_left, insort, insort_left from heapq import heappush, heappop from functools import reduce, lru_cache def input(): return sys.stdin.readline().strip() def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(): return list(map(int, input().split())) def TUPLE(): return tuple(map(int, input().split())) def ZIP(n): return zip(*(MAP() for _ in range(n))) sys.setrecursionlimit(10 ** 9) INF = float('inf') mod = 10 ** 9 + 7 #mod = 998244353 #from decimal import * #import numpy as np #decimal.getcontext().prec = 10 N = INT() a = LIST() if max(a) - min(a) == 0 and (a[0] == N-1 or a[0] <= N//2): print("Yes") elif max(a) - min(a) == 1 and a.count(min(a)) < len(set(a)) and (len(set(a))-a.count(min(a))) <= a.count(max(a))//2: print("Yes") else: print("No") ``` No
14,889
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cats. We number them from 1 through N. Each of the cats wears a hat. Cat i says: "there are exactly a_i different colors among the N - 1 hats worn by the cats except me." Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats. Constraints * 2 ≤ N ≤ 10^5 * 1 ≤ a_i ≤ N-1 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print `Yes` if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print `No` otherwise. Examples Input 3 1 2 2 Output Yes Input 3 1 1 2 Output No Input 5 4 3 4 3 4 Output No Input 3 2 2 2 Output Yes Input 4 2 2 2 2 Output Yes Input 5 3 3 3 3 3 Output No Submitted Solution: ``` N=int(input()) L=list(map(int,input().split())) D={} for i in range(N): if L[i] not in D: D[L[i]]=1 else: D[L[i]]+=1 if len(D)>2: print("No") exit() elif len(D)==1: if L[0]==N-1: print("Yes") if L[0]*2>N: print("No") exit() if 1<=L[0]<=N-1: print("Yes") exit() D=list(D.items()) D.sort() #print(D) if D[0][0]+1!=D[1][0]: print("No") exit() Odd=D[0][1] if D[1][0]!=N-Odd: print("No") exit() if D[1][1]==1: print("No") exit() print("Yes") ``` No
14,890
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cats. We number them from 1 through N. Each of the cats wears a hat. Cat i says: "there are exactly a_i different colors among the N - 1 hats worn by the cats except me." Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats. Constraints * 2 ≤ N ≤ 10^5 * 1 ≤ a_i ≤ N-1 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print `Yes` if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print `No` otherwise. Examples Input 3 1 2 2 Output Yes Input 3 1 1 2 Output No Input 5 4 3 4 3 4 Output No Input 3 2 2 2 Output Yes Input 4 2 2 2 2 Output Yes Input 5 3 3 3 3 3 Output No Submitted Solution: ``` import sys sys.setrecursionlimit(10**9) input = sys.stdin.readline N = int(input()) L = list(map(int, input().split())) ans = "Yes" L_max = max(L) L_min = min(L) if L_max - L_min >= 2: ans = "No" else: num_alone = L.count(L_min) not_alone_L = [v-num_alone for v in L if v == L_max] if not_alone_L[0] < 0 or len(not_alone_L) < not_alone_L[0]*2: ans = "No" print(ans) ``` No
14,891
Provide a correct Python 3 solution for this coding contest problem. You are given an integer sequence x of length N. Determine if there exists an integer sequence a that satisfies all of the following conditions, and if it exists, construct an instance of a. * a is N^2 in length, containing N copies of each of the integers 1, 2, ..., N. * For each 1 ≤ i ≤ N, the i-th occurrence of the integer i from the left in a is the x_i-th element of a from the left. Constraints * 1 ≤ N ≤ 500 * 1 ≤ x_i ≤ N^2 * All x_i are distinct. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N Output If there does not exist an integer sequence a that satisfies all the conditions, print `No`. If there does exist such an sequence a, print `Yes` in the first line, then print an instance of a in the second line, with spaces inbetween. Examples Input 3 1 5 9 Output Yes 1 1 1 2 2 2 3 3 3 Input 2 4 1 Output No "Correct Solution: ``` # AOJ DSL_2_A "Range Minimum Query" # SegmantTreeの実装 # 初期化最大値 INF = (1 << 31) - 1 class SegmentTree: def __init__(self, N): self.N = 2**(N-1).bit_length() self.data = [[INF, -1] for _ in range(2*self.N-1)] # k番目の値(0-indexed)をaに変更 def update(self, k, a): self.data[k+self.N-1] = [a, k] k += self.N - 1 while k > 0: k = (k-1)//2 if self.data[2*k+1][0] < self.data[2*k+2][0]: self.data[k] = self.data[2*k+1][:] else: self.data[k] = self.data[2*k+2][:] # [l, r)の最小値取得 # kがNodeの番号、対応する区間が[a, b) def query_min(self, l, r): L = l + self.N R = r + self.N s = [INF, -1] while L < R: if R & 1: R -= 1 if s[0] > self.data[R-1][0]: s = self.data[R-1] if L & 1: if s[0] > self.data[L-1][0]: s = self.data[L-1] L += 1 L >>= 1; R >>= 1 return s import sys input = sys.stdin.readline from bisect import bisect_left N = int(input()) X = list(map(int, input().split())) sortedX = sorted(X) decided = [-1]*(N**2+1) for i, x in enumerate(X): decided[x] = i+1 marge = SegmentTree(N+1) remains = [0]*(N+1) alreadyUsed = [None]*(N+1) for i, x in enumerate(X): i += 1 alreadyused = bisect_left(sortedX, x) if i != 1: marge.update(i, x-alreadyused-i) alreadyUsed[i] = alreadyused remains[i] = i-1 def solve(): offset = 0 for n in range(1,N**2+1): if decided[n] != -1: decided_num = decided[n] if remains[decided_num] > 0: return False, None remain = N-decided_num if remain < 0: return False, None if remain == 0: marge.update(decided_num, INF) remains[decided_num] = 0 else: marge_now = (N**2-n)-(N-alreadyUsed[decided_num]-1)-remain+offset marge.update(decided_num, marge_now) remains[decided_num] = remain else: marge_now, num = marge.query_min(0, N+1) if marge_now-offset < 0 or marge_now == INF: return False, None remains[num] -= 1 if remains[num] == 0: marge.update(num, INF) else: marge.update(num, marge_now+1) decided[n] = num offset += 1 return True, decided[1:] if __name__ == "__main__": ok, ans = solve() if ok: print("Yes") print(*ans) else: print("No") ```
14,892
Provide a correct Python 3 solution for this coding contest problem. You are given an integer sequence x of length N. Determine if there exists an integer sequence a that satisfies all of the following conditions, and if it exists, construct an instance of a. * a is N^2 in length, containing N copies of each of the integers 1, 2, ..., N. * For each 1 ≤ i ≤ N, the i-th occurrence of the integer i from the left in a is the x_i-th element of a from the left. Constraints * 1 ≤ N ≤ 500 * 1 ≤ x_i ≤ N^2 * All x_i are distinct. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N Output If there does not exist an integer sequence a that satisfies all the conditions, print `No`. If there does exist such an sequence a, print `Yes` in the first line, then print an instance of a in the second line, with spaces inbetween. Examples Input 3 1 5 9 Output Yes 1 1 1 2 2 2 3 3 3 Input 2 4 1 Output No "Correct Solution: ``` import sys n = int(input()) x = list(map(int, input().split())) s = sorted([[y-1, i+1] for i, y in enumerate(x)]) cur = 0 cnt = [0 for _ in range(n+1)] fill_cur = 0 ans = [] residual = [] for i in range(n*n): if i == s[cur][0] and cnt[s[cur][1]] != s[cur][1]-1: print("No") sys.exit() elif i == s[cur][0]: ans.append(s[cur][1]) cnt[s[cur][1]] += 1 residual += [s[cur][1]] * (n - s[cur][1]) cur += 1 fill_cur = max(fill_cur, cur) if cur == n: break elif fill_cur < n: while cnt[s[fill_cur][1]] == s[fill_cur][1]-1: fill_cur += 1 if fill_cur == n: if not residual: print("No") sys.exit() ans.append(residual.pop()) break else: fill_cur = max(fill_cur, cur) ans.append(s[fill_cur][1]) cnt[s[fill_cur][1]] += 1 elif not residual: print("No") sys.exit() else: ans.append(residual.pop()) ans += residual print("Yes") print(*ans) ```
14,893
Provide a correct Python 3 solution for this coding contest problem. You are given an integer sequence x of length N. Determine if there exists an integer sequence a that satisfies all of the following conditions, and if it exists, construct an instance of a. * a is N^2 in length, containing N copies of each of the integers 1, 2, ..., N. * For each 1 ≤ i ≤ N, the i-th occurrence of the integer i from the left in a is the x_i-th element of a from the left. Constraints * 1 ≤ N ≤ 500 * 1 ≤ x_i ≤ N^2 * All x_i are distinct. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N Output If there does not exist an integer sequence a that satisfies all the conditions, print `No`. If there does exist such an sequence a, print `Yes` in the first line, then print an instance of a in the second line, with spaces inbetween. Examples Input 3 1 5 9 Output Yes 1 1 1 2 2 2 3 3 3 Input 2 4 1 Output No "Correct Solution: ``` n = int(input()) a = list(map(int, input().split())) ans = [-1] * (n ** 2) for i in range(n): ans[a[i] - 1] = i + 1 b = sorted([(a[i], i + 1) for i in range(n)], reverse=True) stack = [] for _, val in b: for _ in range(val - 1): stack.append(val) for i in range(n ** 2): if ans[i] == -1: if not stack: continue ans[i] = stack.pop() b = sorted([(a[i], i + 1) for i in range(n)]) stack = [] for _, val in b: for _ in range(n - val): stack.append(val) for i in range(n ** 2)[::-1]: if ans[i] == -1: if not stack: continue ans[i] = stack.pop() cnt = [0] * (n + 1) for i in range(n ** 2): cnt[ans[i]] += 1 if cnt[ans[i]] == ans[i]: if a[ans[i] - 1] == i + 1: continue else: print("No") exit() print("Yes") print(*ans) ```
14,894
Provide a correct Python 3 solution for this coding contest problem. You are given an integer sequence x of length N. Determine if there exists an integer sequence a that satisfies all of the following conditions, and if it exists, construct an instance of a. * a is N^2 in length, containing N copies of each of the integers 1, 2, ..., N. * For each 1 ≤ i ≤ N, the i-th occurrence of the integer i from the left in a is the x_i-th element of a from the left. Constraints * 1 ≤ N ≤ 500 * 1 ≤ x_i ≤ N^2 * All x_i are distinct. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N Output If there does not exist an integer sequence a that satisfies all the conditions, print `No`. If there does exist such an sequence a, print `Yes` in the first line, then print an instance of a in the second line, with spaces inbetween. Examples Input 3 1 5 9 Output Yes 1 1 1 2 2 2 3 3 3 Input 2 4 1 Output No "Correct Solution: ``` n = int(input()) x = [int(x) for i, x in enumerate(input().split())] x = sorted(zip(x, range(1, n+1))) stack = [] for v, i in x[::-1]: for _ in range(i-1): stack.append(i) cur = 1 ans = [] res = [] cnt = [0]*(n+1) for i in range(n): for _ in range(x[i][0]-cur): if stack: nxt = stack.pop() elif res: nxt = res.pop() else: print('No') exit() ans.append(nxt) cnt[nxt] += 1 if cnt[x[i][1]] != x[i][1]-1: print('No') exit() ans.append(x[i][1]) for _ in range(n-x[i][1]): res.append(x[i][1]) cur = x[i][0]+1 ans += res print('Yes') print(*ans) ```
14,895
Provide a correct Python 3 solution for this coding contest problem. You are given an integer sequence x of length N. Determine if there exists an integer sequence a that satisfies all of the following conditions, and if it exists, construct an instance of a. * a is N^2 in length, containing N copies of each of the integers 1, 2, ..., N. * For each 1 ≤ i ≤ N, the i-th occurrence of the integer i from the left in a is the x_i-th element of a from the left. Constraints * 1 ≤ N ≤ 500 * 1 ≤ x_i ≤ N^2 * All x_i are distinct. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N Output If there does not exist an integer sequence a that satisfies all the conditions, print `No`. If there does exist such an sequence a, print `Yes` in the first line, then print an instance of a in the second line, with spaces inbetween. Examples Input 3 1 5 9 Output Yes 1 1 1 2 2 2 3 3 3 Input 2 4 1 Output No "Correct Solution: ``` from heapq import heappop,heappush n = int(input()) a = list(map(int,input().split())) dic = dict() for i,ai in enumerate(a,1): if(ai-1 in dic): print('No') eixt() dic[ai-1] = i hq = [] for i,ai in enumerate(a[1:],2): heappush(hq,(ai-i,i-1,i)) others = [] ans = [] for ind in range(n**2): if ind in dic: i = dic[ind] ans.append(i) others += [i] * (n-i) continue if(hq): num,rem,i = heappop(hq) if(num < ind): print('No') exit() elif(rem==1): ans.append(i) else: ans.append(i) heappush(hq,(num+1,rem-1,i)) else: if(others): ans.append(others.pop()) else: print('No') exit() print('Yes') print(' '.join(map(str,ans))) ```
14,896
Provide a correct Python 3 solution for this coding contest problem. You are given an integer sequence x of length N. Determine if there exists an integer sequence a that satisfies all of the following conditions, and if it exists, construct an instance of a. * a is N^2 in length, containing N copies of each of the integers 1, 2, ..., N. * For each 1 ≤ i ≤ N, the i-th occurrence of the integer i from the left in a is the x_i-th element of a from the left. Constraints * 1 ≤ N ≤ 500 * 1 ≤ x_i ≤ N^2 * All x_i are distinct. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N Output If there does not exist an integer sequence a that satisfies all the conditions, print `No`. If there does exist such an sequence a, print `Yes` in the first line, then print an instance of a in the second line, with spaces inbetween. Examples Input 3 1 5 9 Output Yes 1 1 1 2 2 2 3 3 3 Input 2 4 1 Output No "Correct Solution: ``` from collections import deque from collections import defaultdict n = int(input()) x = list(map(int,input().split())) for i in range(n): if x[i]-1 < i or x[i]-1 > n*n-n+i: print("No") exit() ans = [-1]*n*n for i in range(n): ans[x[i]-1] = i+1 x2 = [] for i in range(n): x2.append((x[i],i+1)) x2.sort() yusen = deque([]) ato = deque([]) for i in range(n): yusen.extend([x2[i][1]]*(x2[i][1]-1)) ato.extend([x2[i][1]]*(n-x2[i][1])) for i in range(n*n): if ans[i] == -1: if yusen: ko = yusen.popleft() ans[i] = ko else: ko = ato.popleft() ans[i] = ko d = defaultdict(int) for i in range(n*n): d[ans[i]] += 1 if d[ans[i]] == ans[i]: if i+1 != x[ans[i]-1]: print("No") exit() print("Yes") print(*ans) ```
14,897
Provide a correct Python 3 solution for this coding contest problem. You are given an integer sequence x of length N. Determine if there exists an integer sequence a that satisfies all of the following conditions, and if it exists, construct an instance of a. * a is N^2 in length, containing N copies of each of the integers 1, 2, ..., N. * For each 1 ≤ i ≤ N, the i-th occurrence of the integer i from the left in a is the x_i-th element of a from the left. Constraints * 1 ≤ N ≤ 500 * 1 ≤ x_i ≤ N^2 * All x_i are distinct. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N Output If there does not exist an integer sequence a that satisfies all the conditions, print `No`. If there does exist such an sequence a, print `Yes` in the first line, then print an instance of a in the second line, with spaces inbetween. Examples Input 3 1 5 9 Output Yes 1 1 1 2 2 2 3 3 3 Input 2 4 1 Output No "Correct Solution: ``` N = int(input()) ans = [-1] * (N ** 2) X = [int(x)-1 for x in input().split()] for i, x in enumerate(X): ans[x] = i+1 left, right = [], [] for i in range(N): l_cnt, r_cnt = i, N - (i+1) left.extend([(X[i], i+1)] * l_cnt) right.extend([(X[i], i+1)] * r_cnt) l, r = 0, N ** 2 - 1 left.sort() right.sort(reverse=True) for _, x in left: while ans[l] != -1: l += 1 ans[l] = x for _, x in right: while ans[r] != -1: r -= 1 ans[r] = x counter = [[] for _ in range(N)] for i, a in enumerate(ans): counter[a-1].append(i) if all(counter[i].index(x) == i for i, x in zip(range(N), X)): print("Yes") print(*ans) else: print("No") ```
14,898
Provide a correct Python 3 solution for this coding contest problem. You are given an integer sequence x of length N. Determine if there exists an integer sequence a that satisfies all of the following conditions, and if it exists, construct an instance of a. * a is N^2 in length, containing N copies of each of the integers 1, 2, ..., N. * For each 1 ≤ i ≤ N, the i-th occurrence of the integer i from the left in a is the x_i-th element of a from the left. Constraints * 1 ≤ N ≤ 500 * 1 ≤ x_i ≤ N^2 * All x_i are distinct. Input The input is given from Standard Input in the following format: N x_1 x_2 ... x_N Output If there does not exist an integer sequence a that satisfies all the conditions, print `No`. If there does exist such an sequence a, print `Yes` in the first line, then print an instance of a in the second line, with spaces inbetween. Examples Input 3 1 5 9 Output Yes 1 1 1 2 2 2 3 3 3 Input 2 4 1 Output No "Correct Solution: ``` def solve(): n=int(input()) x=list(map(int,input().split())) list1=[] def ap1(num): list1.append(num) for i in range(n): ap1([x[i],i+1]) str1= lambda val: val[0] list1.sort(key=str1) numa=[] numb=[] for i in range(n): num3=list1[i][1] numa+=[num3]*(num3-1) numb+=[num3]*(n-num3) count1=0 count2=0 count3=0 ans=[] ansnum=0 def countnum(num): return ans.count(num) def apans(num): ans.append(num) for i in range(n*n): yn=0 if count1!=n: if i==list1[count1][0]-1: if countnum(list1[count1][1])!=list1[count1][1]-1: ansnum=1 break apans(list1[count1][1]) count1+=1 yn=1 if yn==0: if count2!=len(numa): apans(numa[count2]) count2+=1 elif count3!=len(numb): apans(numb[count3]) count3+=1 else: if i!=n*n-1: ansnum=1 break if ansnum==1: print("No") else: print("Yes") print(*ans) solve() ```
14,899