message stringlengths 2 67k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 463 109k | cluster float64 19 19 | __index_level_0__ int64 926 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Nauuo is a girl who loves playing cards.
One day she was playing cards but found that the cards were mixed with some empty ones.
There are n cards numbered from 1 to n, and they were mixed with another n empty cards. She piled up the 2n cards and drew n of them. The n cards in Nauuo's hands are given. The remaining n cards in the pile are also given in the order from top to bottom.
In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile.
Nauuo wants to make the n numbered cards piled up in increasing order (the i-th card in the pile from top to bottom is the card i) as quickly as possible. Can you tell her the minimum number of operations?
Input
The first line contains a single integer n (1≤ n≤ 2⋅ 10^5) — the number of numbered cards.
The second line contains n integers a_1,a_2,…,a_n (0≤ a_i≤ n) — the initial cards in Nauuo's hands. 0 represents an empty card.
The third line contains n integers b_1,b_2,…,b_n (0≤ b_i≤ n) — the initial cards in the pile, given in order from top to bottom. 0 represents an empty card.
It is guaranteed that each number from 1 to n appears exactly once, either in a_{1..n} or b_{1..n}.
Output
The output contains a single integer — the minimum number of operations to make the n numbered cards piled up in increasing order.
Examples
Input
3
0 2 0
3 0 1
Output
2
Input
3
0 2 0
1 0 3
Output
4
Input
11
0 0 0 5 0 0 0 4 0 0 11
9 2 6 0 8 1 7 0 3 0 10
Output
18
Note
Example 1
We can play the card 2 and draw the card 3 in the first operation. After that, we have [0,3,0] in hands and the cards in the pile are [0,1,2] from top to bottom.
Then, we play the card 3 in the second operation. The cards in the pile are [1,2,3], in which the cards are piled up in increasing order.
Example 2
Play an empty card and draw the card 1, then play 1, 2, 3 in order. | instruction | 0 | 74,228 | 19 | 148,456 |
Tags: greedy, implementation
Correct Solution:
```
import io, os
#input = io.StringIO(os.read(0, os.fstat(0).st_size).decode()).readline
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
f = True
if 1 in b:
j = b.index(1)
for i in range(n - j):
d = i + 1 - b[j + i]
if d != 0:
break
else:
s = -2
for k in range(j):
if b[k] != 0 and b[k] - k <= n - (j - 1):
break
else:
print(j)
f = False
if f:
s = -2
for k in range(n):
if b[k] != 0:
s = max(s, k - b[k])
print(s + n + 2)
``` | output | 1 | 74,228 | 19 | 148,457 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Nauuo is a girl who loves playing cards.
One day she was playing cards but found that the cards were mixed with some empty ones.
There are n cards numbered from 1 to n, and they were mixed with another n empty cards. She piled up the 2n cards and drew n of them. The n cards in Nauuo's hands are given. The remaining n cards in the pile are also given in the order from top to bottom.
In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile.
Nauuo wants to make the n numbered cards piled up in increasing order (the i-th card in the pile from top to bottom is the card i) as quickly as possible. Can you tell her the minimum number of operations?
Input
The first line contains a single integer n (1≤ n≤ 2⋅ 10^5) — the number of numbered cards.
The second line contains n integers a_1,a_2,…,a_n (0≤ a_i≤ n) — the initial cards in Nauuo's hands. 0 represents an empty card.
The third line contains n integers b_1,b_2,…,b_n (0≤ b_i≤ n) — the initial cards in the pile, given in order from top to bottom. 0 represents an empty card.
It is guaranteed that each number from 1 to n appears exactly once, either in a_{1..n} or b_{1..n}.
Output
The output contains a single integer — the minimum number of operations to make the n numbered cards piled up in increasing order.
Examples
Input
3
0 2 0
3 0 1
Output
2
Input
3
0 2 0
1 0 3
Output
4
Input
11
0 0 0 5 0 0 0 4 0 0 11
9 2 6 0 8 1 7 0 3 0 10
Output
18
Note
Example 1
We can play the card 2 and draw the card 3 in the first operation. After that, we have [0,3,0] in hands and the cards in the pile are [0,1,2] from top to bottom.
Then, we play the card 3 in the second operation. The cards in the pile are [1,2,3], in which the cards are piled up in increasing order.
Example 2
Play an empty card and draw the card 1, then play 1, 2, 3 in order. | instruction | 0 | 74,229 | 19 | 148,458 |
Tags: greedy, implementation
Correct Solution:
```
#!/usr/bin/env python
import sys
n = int(input())
a = [0]; a.extend(list(map(int, input().split())))
b = [0]; b.extend(list(map(int, input().split())))
p = [0 for _ in range(n + 10)]
for i in range(1, n + 1): p[a[i]] = 0
for i in range(1, n + 1): p[b[i]] = i
if p[1]:
i = 2
while p[i] == p[1] + i - 1:
i += 1
if p[i - 1] == n:
j = i
while j <= n and p[j] <= j - i:
j += 1
if j > n:
print(n - i + 1)
sys.exit(0)
ans = 0
for i in range(1, n + 1):
ans = max(ans, p[i] - i + 1 + n)
print(ans)
``` | output | 1 | 74,229 | 19 | 148,459 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Nauuo is a girl who loves playing cards.
One day she was playing cards but found that the cards were mixed with some empty ones.
There are n cards numbered from 1 to n, and they were mixed with another n empty cards. She piled up the 2n cards and drew n of them. The n cards in Nauuo's hands are given. The remaining n cards in the pile are also given in the order from top to bottom.
In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile.
Nauuo wants to make the n numbered cards piled up in increasing order (the i-th card in the pile from top to bottom is the card i) as quickly as possible. Can you tell her the minimum number of operations?
Input
The first line contains a single integer n (1≤ n≤ 2⋅ 10^5) — the number of numbered cards.
The second line contains n integers a_1,a_2,…,a_n (0≤ a_i≤ n) — the initial cards in Nauuo's hands. 0 represents an empty card.
The third line contains n integers b_1,b_2,…,b_n (0≤ b_i≤ n) — the initial cards in the pile, given in order from top to bottom. 0 represents an empty card.
It is guaranteed that each number from 1 to n appears exactly once, either in a_{1..n} or b_{1..n}.
Output
The output contains a single integer — the minimum number of operations to make the n numbered cards piled up in increasing order.
Examples
Input
3
0 2 0
3 0 1
Output
2
Input
3
0 2 0
1 0 3
Output
4
Input
11
0 0 0 5 0 0 0 4 0 0 11
9 2 6 0 8 1 7 0 3 0 10
Output
18
Note
Example 1
We can play the card 2 and draw the card 3 in the first operation. After that, we have [0,3,0] in hands and the cards in the pile are [0,1,2] from top to bottom.
Then, we play the card 3 in the second operation. The cards in the pile are [1,2,3], in which the cards are piled up in increasing order.
Example 2
Play an empty card and draw the card 1, then play 1, 2, 3 in order. | instruction | 0 | 74,230 | 19 | 148,460 |
Tags: greedy, implementation
Correct Solution:
```
n=int(input())
a1=list(map(int,input().split()))
a=list(map(int,input().split()))
a=[0]+a
d=[0]*(n+1);
mx=0
for i in range(1,n+1):
d[a[i]]=i
mx=max(d[i]-i+1 for i in range(1,n+1))
br=n-d[1]+1
b=(d[1]!=0)
for i in range(1,n+1):
if(i<=br):
if(d[i]-d[1]==i-1):
continue
else:
b=False
break
else:
if(d[i]<i-br):
continue
else:
b=False
break
if(b):
print(n-br)
else:
print(n+mx)
``` | output | 1 | 74,230 | 19 | 148,461 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nauuo is a girl who loves playing cards.
One day she was playing cards but found that the cards were mixed with some empty ones.
There are n cards numbered from 1 to n, and they were mixed with another n empty cards. She piled up the 2n cards and drew n of them. The n cards in Nauuo's hands are given. The remaining n cards in the pile are also given in the order from top to bottom.
In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile.
Nauuo wants to make the n numbered cards piled up in increasing order (the i-th card in the pile from top to bottom is the card i) as quickly as possible. Can you tell her the minimum number of operations?
Input
The first line contains a single integer n (1≤ n≤ 2⋅ 10^5) — the number of numbered cards.
The second line contains n integers a_1,a_2,…,a_n (0≤ a_i≤ n) — the initial cards in Nauuo's hands. 0 represents an empty card.
The third line contains n integers b_1,b_2,…,b_n (0≤ b_i≤ n) — the initial cards in the pile, given in order from top to bottom. 0 represents an empty card.
It is guaranteed that each number from 1 to n appears exactly once, either in a_{1..n} or b_{1..n}.
Output
The output contains a single integer — the minimum number of operations to make the n numbered cards piled up in increasing order.
Examples
Input
3
0 2 0
3 0 1
Output
2
Input
3
0 2 0
1 0 3
Output
4
Input
11
0 0 0 5 0 0 0 4 0 0 11
9 2 6 0 8 1 7 0 3 0 10
Output
18
Note
Example 1
We can play the card 2 and draw the card 3 in the first operation. After that, we have [0,3,0] in hands and the cards in the pile are [0,1,2] from top to bottom.
Then, we play the card 3 in the second operation. The cards in the pile are [1,2,3], in which the cards are piled up in increasing order.
Example 2
Play an empty card and draw the card 1, then play 1, 2, 3 in order.
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 9 01:06:50 2019
@author: Hamadeh
"""
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 8 23:43:22 2019
@author: Hamadeh
"""
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 8 23:34:53 2019
@author: Hamadeh
"""
from math import ceil
class cinn:
def __init__(self):
self.x=[]
def cin(self,t=int):
if(len(self.x)==0):
a=input()
self.x=a.split()
self.x.reverse()
return self.get(t)
def get(self,t):
return t(self.x.pop())
def clist(self,n,t=int): #n is number of inputs, t is type to be casted
l=[0]*n
for i in range(n):
l[i]=self.cin(t)
return l
def clist2(self,n,t1=int,t2=int,t3=int,tn=2):
l=[0]*n
for i in range(n):
if(tn==2):
a1=self.cin(t1)
a2=self.cin(t2)
l[i]=(a1,a2)
elif (tn==3):
a1=self.cin(t1)
a2=self.cin(t2)
a3=self.cin(t3)
l[i]=(a1,a2,a3)
return l
def clist3(self,n,t1=int,t2=int,t3=int):
return self.clist2(self,n,t1,t2,t3,3)
def cout(self,i,ans=''):
if(ans==''):
print("Case #"+str(i+1)+":", end=' ')
else:
print("Case #"+str(i+1)+":",ans)
def printf(self,thing):
print(thing,end='')
def countlist(self,l,s=0,e=None):
if(e==None):
e=len(l)
dic={}
for el in range(s,e):
if l[el] not in dic:
dic[l[el]]=1
else:
dic[l[el]]+=1
return dic
def talk (self,x):
print(x,flush=True)
def dp1(self,k):
L=[-1]*(k)
return L
def dp2(self,k,kk):
L=[-1]*(k)
for i in range(k):
L[i]=[-1]*kk
return L
c=cinn()
n=c.cin()
hand=c.clist(n)
stack=c.clist(n)
lpos=[0]*(n+1)
for i in range(n):
el=hand[i]
el2=stack[i]
lpos[el]=0
lpos[el2]=i+1
ideal=n
for j in range(n):
if(j+lpos[1]-1==n):
ideal=n-lpos[1]+1
break
if(not stack[j+lpos[1]-1]==j+1):
ideal=0
break
if(ideal!=0):
#print("Fra")
#print(ideal)
good=True
for i in range(ideal+1,n+1):
if(lpos[i]>i-ideal-1):
good=False
break
if(good==True):
print(lpos[1]-1)
else:
print(lpos[1]+n)
else:
#print(lpos)
#print("frigibigy")
maxdist=0
for i in range(1,n+1):
#print("hewbew")
if(lpos[i]>i-1):
# print("bidumdum",i)
if(maxdist<lpos[i]-i+1):
maxdist=lpos[i]-i+1
print(maxdist+n)
``` | instruction | 0 | 74,231 | 19 | 148,462 |
Yes | output | 1 | 74,231 | 19 | 148,463 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nauuo is a girl who loves playing cards.
One day she was playing cards but found that the cards were mixed with some empty ones.
There are n cards numbered from 1 to n, and they were mixed with another n empty cards. She piled up the 2n cards and drew n of them. The n cards in Nauuo's hands are given. The remaining n cards in the pile are also given in the order from top to bottom.
In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile.
Nauuo wants to make the n numbered cards piled up in increasing order (the i-th card in the pile from top to bottom is the card i) as quickly as possible. Can you tell her the minimum number of operations?
Input
The first line contains a single integer n (1≤ n≤ 2⋅ 10^5) — the number of numbered cards.
The second line contains n integers a_1,a_2,…,a_n (0≤ a_i≤ n) — the initial cards in Nauuo's hands. 0 represents an empty card.
The third line contains n integers b_1,b_2,…,b_n (0≤ b_i≤ n) — the initial cards in the pile, given in order from top to bottom. 0 represents an empty card.
It is guaranteed that each number from 1 to n appears exactly once, either in a_{1..n} or b_{1..n}.
Output
The output contains a single integer — the minimum number of operations to make the n numbered cards piled up in increasing order.
Examples
Input
3
0 2 0
3 0 1
Output
2
Input
3
0 2 0
1 0 3
Output
4
Input
11
0 0 0 5 0 0 0 4 0 0 11
9 2 6 0 8 1 7 0 3 0 10
Output
18
Note
Example 1
We can play the card 2 and draw the card 3 in the first operation. After that, we have [0,3,0] in hands and the cards in the pile are [0,1,2] from top to bottom.
Then, we play the card 3 in the second operation. The cards in the pile are [1,2,3], in which the cards are piled up in increasing order.
Example 2
Play an empty card and draw the card 1, then play 1, 2, 3 in order.
Submitted Solution:
```
for _ in range(1):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
k=b[n-1]
for i in range(k):
if b[n-i-1]!=k-i:
k=0
#ind=[0 for i in range(n+1)]
ma=0
for i in range(n-k):
if b[i]==0:
continue
if b[i]-k-1<=i:
ma=max(ma,i-b[i]+k+2)
if ma==0:
print(n-k)
else:
if k==0:
print(n+ma)
else:
k=0
ma=0
for i in range(n-k):
if b[i]==0:
continue
if b[i]-k-1<=i:
ma=max(ma,i-b[i]+k+2)
print(n+ma)
``` | instruction | 0 | 74,232 | 19 | 148,464 |
Yes | output | 1 | 74,232 | 19 | 148,465 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nauuo is a girl who loves playing cards.
One day she was playing cards but found that the cards were mixed with some empty ones.
There are n cards numbered from 1 to n, and they were mixed with another n empty cards. She piled up the 2n cards and drew n of them. The n cards in Nauuo's hands are given. The remaining n cards in the pile are also given in the order from top to bottom.
In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile.
Nauuo wants to make the n numbered cards piled up in increasing order (the i-th card in the pile from top to bottom is the card i) as quickly as possible. Can you tell her the minimum number of operations?
Input
The first line contains a single integer n (1≤ n≤ 2⋅ 10^5) — the number of numbered cards.
The second line contains n integers a_1,a_2,…,a_n (0≤ a_i≤ n) — the initial cards in Nauuo's hands. 0 represents an empty card.
The third line contains n integers b_1,b_2,…,b_n (0≤ b_i≤ n) — the initial cards in the pile, given in order from top to bottom. 0 represents an empty card.
It is guaranteed that each number from 1 to n appears exactly once, either in a_{1..n} or b_{1..n}.
Output
The output contains a single integer — the minimum number of operations to make the n numbered cards piled up in increasing order.
Examples
Input
3
0 2 0
3 0 1
Output
2
Input
3
0 2 0
1 0 3
Output
4
Input
11
0 0 0 5 0 0 0 4 0 0 11
9 2 6 0 8 1 7 0 3 0 10
Output
18
Note
Example 1
We can play the card 2 and draw the card 3 in the first operation. After that, we have [0,3,0] in hands and the cards in the pile are [0,1,2] from top to bottom.
Then, we play the card 3 in the second operation. The cards in the pile are [1,2,3], in which the cards are piled up in increasing order.
Example 2
Play an empty card and draw the card 1, then play 1, 2, 3 in order.
Submitted Solution:
```
def get_int_list():
return list(map(int, input().split()))
def get_max_repeats(l):
if len(l) == 0:
return 0
repeats = 1
max_repeats = 1
last_element = l[0]
for i in range(1, len(l)):
if l[i] == last_element:
repeats += 1
else:
if repeats > max_repeats:
max_repeats = repeats
repeats = 1
last_element = l[i]
if repeats > max_repeats:
max_repeats = repeats
return max_repeats
def order(pile):
if pile[-1] == 1:
return 1
if len(pile) == 1:
return 0
for i in range(len(pile) - 2, -1, -1):
if pile[i] == 1 and pile[i + 1] == 2:
return len(pile) - i
elif pile[i] == pile[i + 1] - 1:
continue
else:
return 0
n = int(input())
hand = get_int_list()
pile = get_int_list()
places = {}
for i in range(n):
if hand[i] > 0:
places[hand[i]] = 0
if pile[i] > 0:
places[pile[i]] = i + 1
offset = order(pile)
if offset > 0:
in_order = True
# in order, check distance
for i in range(offset + 1,n + 1):
if places[i] == 0:
continue
distance = places[i] - (i - 1) + offset
if distance > 0:
in_order = False
break
if in_order:
print(str(n - offset))
else:
# not in order calculate normally
one_to_hand = places[1]
max_distance = 0
for i in range(2,n + 1):
if places[i] == 0:
continue
distance = places[i] - (i - 1) - one_to_hand
if distance > max_distance:
max_distance = distance
moves = one_to_hand + max_distance + n
print(str(moves))
else:
# not in order calculate normally
one_to_hand = places[1]
max_distance = 0
for i in range(2,n + 1):
if places[i] == 0:
continue
distance = places[i] - (i - 1) - one_to_hand
if distance > max_distance:
max_distance = distance
moves = one_to_hand + max_distance + n
print(str(moves))
``` | instruction | 0 | 74,233 | 19 | 148,466 |
Yes | output | 1 | 74,233 | 19 | 148,467 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nauuo is a girl who loves playing cards.
One day she was playing cards but found that the cards were mixed with some empty ones.
There are n cards numbered from 1 to n, and they were mixed with another n empty cards. She piled up the 2n cards and drew n of them. The n cards in Nauuo's hands are given. The remaining n cards in the pile are also given in the order from top to bottom.
In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile.
Nauuo wants to make the n numbered cards piled up in increasing order (the i-th card in the pile from top to bottom is the card i) as quickly as possible. Can you tell her the minimum number of operations?
Input
The first line contains a single integer n (1≤ n≤ 2⋅ 10^5) — the number of numbered cards.
The second line contains n integers a_1,a_2,…,a_n (0≤ a_i≤ n) — the initial cards in Nauuo's hands. 0 represents an empty card.
The third line contains n integers b_1,b_2,…,b_n (0≤ b_i≤ n) — the initial cards in the pile, given in order from top to bottom. 0 represents an empty card.
It is guaranteed that each number from 1 to n appears exactly once, either in a_{1..n} or b_{1..n}.
Output
The output contains a single integer — the minimum number of operations to make the n numbered cards piled up in increasing order.
Examples
Input
3
0 2 0
3 0 1
Output
2
Input
3
0 2 0
1 0 3
Output
4
Input
11
0 0 0 5 0 0 0 4 0 0 11
9 2 6 0 8 1 7 0 3 0 10
Output
18
Note
Example 1
We can play the card 2 and draw the card 3 in the first operation. After that, we have [0,3,0] in hands and the cards in the pile are [0,1,2] from top to bottom.
Then, we play the card 3 in the second operation. The cards in the pile are [1,2,3], in which the cards are piled up in increasing order.
Example 2
Play an empty card and draw the card 1, then play 1, 2, 3 in order.
Submitted Solution:
```
n = int(input())
l1=list(map(int,input().split()))
l2= list(map(int,input().split()))
f=0
if 1 in l2:
c=l2.index(1)
d=1
for i in range(c,n):
if l2[i]!=d:
# print(l2[i],d,i,c)
break
d+=1
else:
# print("ll")
for k in range(c):
if l2[k]!=0 and l2[k]-k<=d:
break
else:
print(c)
# print(d)
f=1
# n-=d
if f==0:
s=-2
for i in range(n):
if l2[i]!=0:
s=max(s,i-l2[i])
print(n+s+2)
``` | instruction | 0 | 74,234 | 19 | 148,468 |
Yes | output | 1 | 74,234 | 19 | 148,469 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nauuo is a girl who loves playing cards.
One day she was playing cards but found that the cards were mixed with some empty ones.
There are n cards numbered from 1 to n, and they were mixed with another n empty cards. She piled up the 2n cards and drew n of them. The n cards in Nauuo's hands are given. The remaining n cards in the pile are also given in the order from top to bottom.
In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile.
Nauuo wants to make the n numbered cards piled up in increasing order (the i-th card in the pile from top to bottom is the card i) as quickly as possible. Can you tell her the minimum number of operations?
Input
The first line contains a single integer n (1≤ n≤ 2⋅ 10^5) — the number of numbered cards.
The second line contains n integers a_1,a_2,…,a_n (0≤ a_i≤ n) — the initial cards in Nauuo's hands. 0 represents an empty card.
The third line contains n integers b_1,b_2,…,b_n (0≤ b_i≤ n) — the initial cards in the pile, given in order from top to bottom. 0 represents an empty card.
It is guaranteed that each number from 1 to n appears exactly once, either in a_{1..n} or b_{1..n}.
Output
The output contains a single integer — the minimum number of operations to make the n numbered cards piled up in increasing order.
Examples
Input
3
0 2 0
3 0 1
Output
2
Input
3
0 2 0
1 0 3
Output
4
Input
11
0 0 0 5 0 0 0 4 0 0 11
9 2 6 0 8 1 7 0 3 0 10
Output
18
Note
Example 1
We can play the card 2 and draw the card 3 in the first operation. After that, we have [0,3,0] in hands and the cards in the pile are [0,1,2] from top to bottom.
Then, we play the card 3 in the second operation. The cards in the pile are [1,2,3], in which the cards are piled up in increasing order.
Example 2
Play an empty card and draw the card 1, then play 1, 2, 3 in order.
Submitted Solution:
```
n=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
arr3=[0]
mini=n+1
for i in range(0,len(arr2)):
if arr2[i]<mini and arr2[i]!=0:
mini=arr2[i]
if mini==n+1:
print(len(arr1))
exit()
q=n+1
a=0
b=0
m=0
for i in range(0,len(arr2)):
if mini==arr2[i]:
q=int(i)
if arr2[i]!=0:
# a=q+arr2[i]
# b=i+1
m=max(i+1-arr2[i]+1,m)
# if (i+1)>=:
# arr3.append(b-a)
# print(arr3)
c=1
count=0
print(m)
# x1=max(arr3)
# print(q)
if arr2[q]==1:
for i in range(q+1,len(arr2)):
c+=1
if arr2[i]==c:
count+=1
else:
break
a1=0
b1=0
if count==len(arr2)-q-1:
for i in range(0,q):
if arr2[i]!=0:
a1=arr2[i]-arr2[count+q]-1
b1=i+1
if (a1-b1)!=0:
print(m+n)
exit()
print(q)
exit()
print(m+n)
exit()
# if (q+1+x1+n)==2335:
# print(2334)
# exit()
# print("hi")
print(m+n)
``` | instruction | 0 | 74,235 | 19 | 148,470 |
No | output | 1 | 74,235 | 19 | 148,471 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nauuo is a girl who loves playing cards.
One day she was playing cards but found that the cards were mixed with some empty ones.
There are n cards numbered from 1 to n, and they were mixed with another n empty cards. She piled up the 2n cards and drew n of them. The n cards in Nauuo's hands are given. The remaining n cards in the pile are also given in the order from top to bottom.
In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile.
Nauuo wants to make the n numbered cards piled up in increasing order (the i-th card in the pile from top to bottom is the card i) as quickly as possible. Can you tell her the minimum number of operations?
Input
The first line contains a single integer n (1≤ n≤ 2⋅ 10^5) — the number of numbered cards.
The second line contains n integers a_1,a_2,…,a_n (0≤ a_i≤ n) — the initial cards in Nauuo's hands. 0 represents an empty card.
The third line contains n integers b_1,b_2,…,b_n (0≤ b_i≤ n) — the initial cards in the pile, given in order from top to bottom. 0 represents an empty card.
It is guaranteed that each number from 1 to n appears exactly once, either in a_{1..n} or b_{1..n}.
Output
The output contains a single integer — the minimum number of operations to make the n numbered cards piled up in increasing order.
Examples
Input
3
0 2 0
3 0 1
Output
2
Input
3
0 2 0
1 0 3
Output
4
Input
11
0 0 0 5 0 0 0 4 0 0 11
9 2 6 0 8 1 7 0 3 0 10
Output
18
Note
Example 1
We can play the card 2 and draw the card 3 in the first operation. After that, we have [0,3,0] in hands and the cards in the pile are [0,1,2] from top to bottom.
Then, we play the card 3 in the second operation. The cards in the pile are [1,2,3], in which the cards are piled up in increasing order.
Example 2
Play an empty card and draw the card 1, then play 1, 2, 3 in order.
Submitted Solution:
```
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ma=0
if 1 in b:
i=b.index(1)
else:
i=-1
if b[-1]==1:
for j in range(n-1):
if b[j]>j+1 or b[j]==0:
continue
else:
print(2*n)
exit()
else:
print(n-1)
exit()
for j in range(i+1,n):
if b[j]>j-i or b[j]==0:
#print("con")
continue
else:
ma=j-i-b[j]+1
#print(max)
print(n+i+1+ma)
#print(n,i,max)
``` | instruction | 0 | 74,236 | 19 | 148,472 |
No | output | 1 | 74,236 | 19 | 148,473 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nauuo is a girl who loves playing cards.
One day she was playing cards but found that the cards were mixed with some empty ones.
There are n cards numbered from 1 to n, and they were mixed with another n empty cards. She piled up the 2n cards and drew n of them. The n cards in Nauuo's hands are given. The remaining n cards in the pile are also given in the order from top to bottom.
In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile.
Nauuo wants to make the n numbered cards piled up in increasing order (the i-th card in the pile from top to bottom is the card i) as quickly as possible. Can you tell her the minimum number of operations?
Input
The first line contains a single integer n (1≤ n≤ 2⋅ 10^5) — the number of numbered cards.
The second line contains n integers a_1,a_2,…,a_n (0≤ a_i≤ n) — the initial cards in Nauuo's hands. 0 represents an empty card.
The third line contains n integers b_1,b_2,…,b_n (0≤ b_i≤ n) — the initial cards in the pile, given in order from top to bottom. 0 represents an empty card.
It is guaranteed that each number from 1 to n appears exactly once, either in a_{1..n} or b_{1..n}.
Output
The output contains a single integer — the minimum number of operations to make the n numbered cards piled up in increasing order.
Examples
Input
3
0 2 0
3 0 1
Output
2
Input
3
0 2 0
1 0 3
Output
4
Input
11
0 0 0 5 0 0 0 4 0 0 11
9 2 6 0 8 1 7 0 3 0 10
Output
18
Note
Example 1
We can play the card 2 and draw the card 3 in the first operation. After that, we have [0,3,0] in hands and the cards in the pile are [0,1,2] from top to bottom.
Then, we play the card 3 in the second operation. The cards in the pile are [1,2,3], in which the cards are piled up in increasing order.
Example 2
Play an empty card and draw the card 1, then play 1, 2, 3 in order.
Submitted Solution:
```
from sys import stdin,stdout
from itertools import combinations
from collections import defaultdict,OrderedDict
import math
import heapq
def listIn():
return list((map(int,stdin.readline().strip().split())))
def stringListIn():
return([x for x in stdin.readline().split()])
def intIn():
return (int(stdin.readline()))
def stringIn():
return (stdin.readline().strip())
if __name__=="__main__":
n=intIn()
a=set(listIn())
if len(a)==n and 0 not in a:
print(n)
exit(0)
pile=listIn()
op=0
x=pile[-1]
start=x
a1=a.copy()
p=pile[::]
while(x+1 in a1 and x+1<=n):
op+=1
ele=p.pop(0)
p.append(x+1)
a1.remove(x+1)
x=x+1
a1.add(ele)
#print(a1)
#print(p)
c=1
for i in range(n):
if p[i]==c:
c+=1
else:
break
if c==n:
print(op)
exit(0)
op=0
if 1 not in a:
idx=pile.index(1)
op+=idx+1
b=pile[:idx+1]
pile=pile[idx+1:]+[0]*(idx+1)
for ele in b:
a.add(ele)
#print(a)
#print(pile)
maxx=0
for i in range(n):
if (pile[i]!=0):
idx=i
num=pile[i]-1 #previous number
m=idx-(num-1)
#print(m,pile[i])
maxx=max(0,m,maxx)
#print(maxx)
op+=maxx+n
print(op)
``` | instruction | 0 | 74,237 | 19 | 148,474 |
No | output | 1 | 74,237 | 19 | 148,475 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nauuo is a girl who loves playing cards.
One day she was playing cards but found that the cards were mixed with some empty ones.
There are n cards numbered from 1 to n, and they were mixed with another n empty cards. She piled up the 2n cards and drew n of them. The n cards in Nauuo's hands are given. The remaining n cards in the pile are also given in the order from top to bottom.
In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile.
Nauuo wants to make the n numbered cards piled up in increasing order (the i-th card in the pile from top to bottom is the card i) as quickly as possible. Can you tell her the minimum number of operations?
Input
The first line contains a single integer n (1≤ n≤ 2⋅ 10^5) — the number of numbered cards.
The second line contains n integers a_1,a_2,…,a_n (0≤ a_i≤ n) — the initial cards in Nauuo's hands. 0 represents an empty card.
The third line contains n integers b_1,b_2,…,b_n (0≤ b_i≤ n) — the initial cards in the pile, given in order from top to bottom. 0 represents an empty card.
It is guaranteed that each number from 1 to n appears exactly once, either in a_{1..n} or b_{1..n}.
Output
The output contains a single integer — the minimum number of operations to make the n numbered cards piled up in increasing order.
Examples
Input
3
0 2 0
3 0 1
Output
2
Input
3
0 2 0
1 0 3
Output
4
Input
11
0 0 0 5 0 0 0 4 0 0 11
9 2 6 0 8 1 7 0 3 0 10
Output
18
Note
Example 1
We can play the card 2 and draw the card 3 in the first operation. After that, we have [0,3,0] in hands and the cards in the pile are [0,1,2] from top to bottom.
Then, we play the card 3 in the second operation. The cards in the pile are [1,2,3], in which the cards are piled up in increasing order.
Example 2
Play an empty card and draw the card 1, then play 1, 2, 3 in order.
Submitted Solution:
```
n = int(input())
poss = [-1]*(n + 1)
for card in input().split():
card = int(card)
if card==0:
continue
poss[card] = 0
good = 0
for i, card in enumerate(input().split()):
card = int(card)
if card==0:
good = 0
continue
if card==1:
good = 1
else:
if good:
if good+1==card:
good +=1
else:
good = 0
poss[card] = i+1
mx = 0
sm = 0
for i in range(1, n+1):
if i<=good:
poss[i] = 0
if poss[i]<mx:
poss[i] = 0
else:
poss[i]-=mx
mx = poss[i]+mx
poss[i]-=(i-1-good)*(i>good)
if poss[i]>0:
sm+=poss[i]
if good and sm:
good = 0
mx = 0
sm = 0
for i in range(1, n + 1):
if i <= good:
poss[i] = 0
if poss[i] < mx:
poss[i] = 0
else:
poss[i] -= mx
mx = poss[i] + mx
poss[i] -= (i - 1 - good) * (i > good)
if poss[i] > 0:
sm += poss[i]
print(sm+n-good)
``` | instruction | 0 | 74,238 | 19 | 148,476 |
No | output | 1 | 74,238 | 19 | 148,477 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya and Vasya are competing with each other in a new interesting game as they always do.
At the beginning of the game Petya has to come up with an array of N positive integers. Sum of all elements in his array should be equal to S. Then Petya has to select an integer K such that 0 ≤ K ≤ S.
In order to win, Vasya has to find a non-empty subarray in Petya's array such that the sum of all selected elements equals to either K or S - K. Otherwise Vasya loses.
You are given integers N and S. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.
Input
The first line contains two integers N and S (1 ≤ N ≤ S ≤ 10^{6}) — the required length of the array and the required sum of its elements.
Output
If Petya can win, print "YES" (without quotes) in the first line. Then print Petya's array in the second line. The array should contain N positive integers with sum equal to S. In the third line print K. If there are many correct answers, you can print any of them.
If Petya can't win, print "NO" (without quotes).
You can print each letter in any register (lowercase or uppercase).
Examples
Input
1 4
Output
YES
4
2
Input
3 4
Output
NO
Input
3 8
Output
YES
2 1 5
4 | instruction | 0 | 74,308 | 19 | 148,616 |
Tags: constructive algorithms, math
Correct Solution:
```
t = 1
while t > 0:
t -= 1
n, s = map(int, input().split())
if s < 2 * n :
print('NO')
else :
print('YES')
print(2 + s - 2 * n, end = ' ')
for i in range(n - 1) :
print(2, end = ' ')
print('')
print('1')
``` | output | 1 | 74,308 | 19 | 148,617 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya and Vasya are competing with each other in a new interesting game as they always do.
At the beginning of the game Petya has to come up with an array of N positive integers. Sum of all elements in his array should be equal to S. Then Petya has to select an integer K such that 0 ≤ K ≤ S.
In order to win, Vasya has to find a non-empty subarray in Petya's array such that the sum of all selected elements equals to either K or S - K. Otherwise Vasya loses.
You are given integers N and S. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.
Input
The first line contains two integers N and S (1 ≤ N ≤ S ≤ 10^{6}) — the required length of the array and the required sum of its elements.
Output
If Petya can win, print "YES" (without quotes) in the first line. Then print Petya's array in the second line. The array should contain N positive integers with sum equal to S. In the third line print K. If there are many correct answers, you can print any of them.
If Petya can't win, print "NO" (without quotes).
You can print each letter in any register (lowercase or uppercase).
Examples
Input
1 4
Output
YES
4
2
Input
3 4
Output
NO
Input
3 8
Output
YES
2 1 5
4 | instruction | 0 | 74,309 | 19 | 148,618 |
Tags: constructive algorithms, math
Correct Solution:
```
def calc(N, S):
if N == 1:
if S == 1:
return False
return [S]
if 2 * N <= S:
l = []
first = S - 2 * (N - 1)
l.append(first)
for i in range(N - 1):
l.append(2)
return l
else:
return False
def main():
N, S = map(int, input().split())
res = calc(N, S)
if res:
print("YES")
print(*res)
print(1)
else:
print("NO")
main()
``` | output | 1 | 74,309 | 19 | 148,619 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya and Vasya are competing with each other in a new interesting game as they always do.
At the beginning of the game Petya has to come up with an array of N positive integers. Sum of all elements in his array should be equal to S. Then Petya has to select an integer K such that 0 ≤ K ≤ S.
In order to win, Vasya has to find a non-empty subarray in Petya's array such that the sum of all selected elements equals to either K or S - K. Otherwise Vasya loses.
You are given integers N and S. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.
Input
The first line contains two integers N and S (1 ≤ N ≤ S ≤ 10^{6}) — the required length of the array and the required sum of its elements.
Output
If Petya can win, print "YES" (without quotes) in the first line. Then print Petya's array in the second line. The array should contain N positive integers with sum equal to S. In the third line print K. If there are many correct answers, you can print any of them.
If Petya can't win, print "NO" (without quotes).
You can print each letter in any register (lowercase or uppercase).
Examples
Input
1 4
Output
YES
4
2
Input
3 4
Output
NO
Input
3 8
Output
YES
2 1 5
4 | instruction | 0 | 74,310 | 19 | 148,620 |
Tags: constructive algorithms, math
Correct Solution:
```
import sys
import math as mt
input=sys.stdin.buffer.readline
#t=int(input())
#tot=0
t=1
for __ in range(t):
#n=int(input())
n,s=map(int,input().split())
#e=list(map(int,input().split()))
if s>=2*n:
print("YES")
suma=0
for i in range(n-1):
print(2,end=" ")
suma+=2
print(s-suma)
print(1)
else:
print("NO")
``` | output | 1 | 74,310 | 19 | 148,621 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya and Vasya are competing with each other in a new interesting game as they always do.
At the beginning of the game Petya has to come up with an array of N positive integers. Sum of all elements in his array should be equal to S. Then Petya has to select an integer K such that 0 ≤ K ≤ S.
In order to win, Vasya has to find a non-empty subarray in Petya's array such that the sum of all selected elements equals to either K or S - K. Otherwise Vasya loses.
You are given integers N and S. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.
Input
The first line contains two integers N and S (1 ≤ N ≤ S ≤ 10^{6}) — the required length of the array and the required sum of its elements.
Output
If Petya can win, print "YES" (without quotes) in the first line. Then print Petya's array in the second line. The array should contain N positive integers with sum equal to S. In the third line print K. If there are many correct answers, you can print any of them.
If Petya can't win, print "NO" (without quotes).
You can print each letter in any register (lowercase or uppercase).
Examples
Input
1 4
Output
YES
4
2
Input
3 4
Output
NO
Input
3 8
Output
YES
2 1 5
4 | instruction | 0 | 74,311 | 19 | 148,622 |
Tags: constructive algorithms, math
Correct Solution:
```
t = 1
for _ in range(t):
#n = int(input())
n,s = list(map(int,input().rstrip().split()))
if( s//2 -1<n-1 ):
print('NO')
else:
print('YES')
if(n==1):
print(s)
print(s//2)
continue
k = s//2
a = '1 '*(n-2) + str(k-1-n+2)+' ' + str(s-k+1)
print(a)
print(k)
``` | output | 1 | 74,311 | 19 | 148,623 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya and Vasya are competing with each other in a new interesting game as they always do.
At the beginning of the game Petya has to come up with an array of N positive integers. Sum of all elements in his array should be equal to S. Then Petya has to select an integer K such that 0 ≤ K ≤ S.
In order to win, Vasya has to find a non-empty subarray in Petya's array such that the sum of all selected elements equals to either K or S - K. Otherwise Vasya loses.
You are given integers N and S. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.
Input
The first line contains two integers N and S (1 ≤ N ≤ S ≤ 10^{6}) — the required length of the array and the required sum of its elements.
Output
If Petya can win, print "YES" (without quotes) in the first line. Then print Petya's array in the second line. The array should contain N positive integers with sum equal to S. In the third line print K. If there are many correct answers, you can print any of them.
If Petya can't win, print "NO" (without quotes).
You can print each letter in any register (lowercase or uppercase).
Examples
Input
1 4
Output
YES
4
2
Input
3 4
Output
NO
Input
3 8
Output
YES
2 1 5
4 | instruction | 0 | 74,313 | 19 | 148,626 |
Tags: constructive algorithms, math
Correct Solution:
```
n,s=map(int,input().split())
if s>=2*n:
print("YES")
a=[2]*(n-1)
a.append(s-sum(a))
print(*a)
print(1)
else:
print("NO")
``` | output | 1 | 74,313 | 19 | 148,627 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya and Vasya are competing with each other in a new interesting game as they always do.
At the beginning of the game Petya has to come up with an array of N positive integers. Sum of all elements in his array should be equal to S. Then Petya has to select an integer K such that 0 ≤ K ≤ S.
In order to win, Vasya has to find a non-empty subarray in Petya's array such that the sum of all selected elements equals to either K or S - K. Otherwise Vasya loses.
You are given integers N and S. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.
Input
The first line contains two integers N and S (1 ≤ N ≤ S ≤ 10^{6}) — the required length of the array and the required sum of its elements.
Output
If Petya can win, print "YES" (without quotes) in the first line. Then print Petya's array in the second line. The array should contain N positive integers with sum equal to S. In the third line print K. If there are many correct answers, you can print any of them.
If Petya can't win, print "NO" (without quotes).
You can print each letter in any register (lowercase or uppercase).
Examples
Input
1 4
Output
YES
4
2
Input
3 4
Output
NO
Input
3 8
Output
YES
2 1 5
4 | instruction | 0 | 74,314 | 19 | 148,628 |
Tags: constructive algorithms, math
Correct Solution:
```
n, s = map(int, input().split())
if s - n > n - 1:
print('YES')
for _ in range(n - 1):
print(1, end=' ')
print(s - n + 1)
print(n)
else:
print('NO')
``` | output | 1 | 74,314 | 19 | 148,629 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya and Vasya are competing with each other in a new interesting game as they always do.
At the beginning of the game Petya has to come up with an array of N positive integers. Sum of all elements in his array should be equal to S. Then Petya has to select an integer K such that 0 ≤ K ≤ S.
In order to win, Vasya has to find a non-empty subarray in Petya's array such that the sum of all selected elements equals to either K or S - K. Otherwise Vasya loses.
You are given integers N and S. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.
Input
The first line contains two integers N and S (1 ≤ N ≤ S ≤ 10^{6}) — the required length of the array and the required sum of its elements.
Output
If Petya can win, print "YES" (without quotes) in the first line. Then print Petya's array in the second line. The array should contain N positive integers with sum equal to S. In the third line print K. If there are many correct answers, you can print any of them.
If Petya can't win, print "NO" (without quotes).
You can print each letter in any register (lowercase or uppercase).
Examples
Input
1 4
Output
YES
4
2
Input
3 4
Output
NO
Input
3 8
Output
YES
2 1 5
4 | instruction | 0 | 74,315 | 19 | 148,630 |
Tags: constructive algorithms, math
Correct Solution:
```
import sys
import atexit
import io
input = sys.stdin.readline
def main():
N, S = [int(i) for i in input().split()]
if S <2*N:
print("NO")
else:
print("Yes")
rem = S-(2*N - 2)
li = [2]*(N-1)
li.append(rem)
print(*li)
print(S-1)
if __name__ == '__main__':
main()
``` | output | 1 | 74,315 | 19 | 148,631 |
Provide tags and a correct Python 2 solution for this coding contest problem.
Petya and Vasya are competing with each other in a new interesting game as they always do.
At the beginning of the game Petya has to come up with an array of N positive integers. Sum of all elements in his array should be equal to S. Then Petya has to select an integer K such that 0 ≤ K ≤ S.
In order to win, Vasya has to find a non-empty subarray in Petya's array such that the sum of all selected elements equals to either K or S - K. Otherwise Vasya loses.
You are given integers N and S. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.
Input
The first line contains two integers N and S (1 ≤ N ≤ S ≤ 10^{6}) — the required length of the array and the required sum of its elements.
Output
If Petya can win, print "YES" (without quotes) in the first line. Then print Petya's array in the second line. The array should contain N positive integers with sum equal to S. In the third line print K. If there are many correct answers, you can print any of them.
If Petya can't win, print "NO" (without quotes).
You can print each letter in any register (lowercase or uppercase).
Examples
Input
1 4
Output
YES
4
2
Input
3 4
Output
NO
Input
3 8
Output
YES
2 1 5
4 | instruction | 0 | 74,316 | 19 | 148,632 |
Tags: constructive algorithms, math
Correct Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
mod=10**9+7
def ni():
return int(raw_input())
def li():
return map(int,raw_input().split())
def pn(n):
stdout.write(str(n)+'\n')
def pa(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return map(int,stdin.read().split())
range = xrange # not for python 3.0+
n,s=li()
if s<2*n:
pr('NO')
exit()
pr('YES\n')
for i in range(n-1):
pr(str(2)+' ')
pr(str(s-(2*(n-1)))+'\n')
pr(str(1))
``` | output | 1 | 74,316 | 19 | 148,633 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya and Vasya are competing with each other in a new interesting game as they always do.
At the beginning of the game Petya has to come up with an array of N positive integers. Sum of all elements in his array should be equal to S. Then Petya has to select an integer K such that 0 ≤ K ≤ S.
In order to win, Vasya has to find a non-empty subarray in Petya's array such that the sum of all selected elements equals to either K or S - K. Otherwise Vasya loses.
You are given integers N and S. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.
Input
The first line contains two integers N and S (1 ≤ N ≤ S ≤ 10^{6}) — the required length of the array and the required sum of its elements.
Output
If Petya can win, print "YES" (without quotes) in the first line. Then print Petya's array in the second line. The array should contain N positive integers with sum equal to S. In the third line print K. If there are many correct answers, you can print any of them.
If Petya can't win, print "NO" (without quotes).
You can print each letter in any register (lowercase or uppercase).
Examples
Input
1 4
Output
YES
4
2
Input
3 4
Output
NO
Input
3 8
Output
YES
2 1 5
4
Submitted Solution:
```
n,s=map(int,input().split())
if s/n<2:
print("NO")
else:
print("YES")
ans=[]
for i in range(n-1):
ans.append(2)
suma=sum(ans)
ans.append(s-suma)
print(*ans)
print(1)
``` | instruction | 0 | 74,317 | 19 | 148,634 |
Yes | output | 1 | 74,317 | 19 | 148,635 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya and Vasya are competing with each other in a new interesting game as they always do.
At the beginning of the game Petya has to come up with an array of N positive integers. Sum of all elements in his array should be equal to S. Then Petya has to select an integer K such that 0 ≤ K ≤ S.
In order to win, Vasya has to find a non-empty subarray in Petya's array such that the sum of all selected elements equals to either K or S - K. Otherwise Vasya loses.
You are given integers N and S. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.
Input
The first line contains two integers N and S (1 ≤ N ≤ S ≤ 10^{6}) — the required length of the array and the required sum of its elements.
Output
If Petya can win, print "YES" (without quotes) in the first line. Then print Petya's array in the second line. The array should contain N positive integers with sum equal to S. In the third line print K. If there are many correct answers, you can print any of them.
If Petya can't win, print "NO" (without quotes).
You can print each letter in any register (lowercase or uppercase).
Examples
Input
1 4
Output
YES
4
2
Input
3 4
Output
NO
Input
3 8
Output
YES
2 1 5
4
Submitted Solution:
```
from sys import stdin,stdout
n,s = map(int, stdin.readline().split())
if n>s>>1:
stdout.write("NO\n")
else:
stdout.write("YES\n")
for i in range(n-1):
stdout.write("2 ")
stdout.write(str(s-2*n+2)+'\n1\n')
``` | instruction | 0 | 74,318 | 19 | 148,636 |
Yes | output | 1 | 74,318 | 19 | 148,637 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya and Vasya are competing with each other in a new interesting game as they always do.
At the beginning of the game Petya has to come up with an array of N positive integers. Sum of all elements in his array should be equal to S. Then Petya has to select an integer K such that 0 ≤ K ≤ S.
In order to win, Vasya has to find a non-empty subarray in Petya's array such that the sum of all selected elements equals to either K or S - K. Otherwise Vasya loses.
You are given integers N and S. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.
Input
The first line contains two integers N and S (1 ≤ N ≤ S ≤ 10^{6}) — the required length of the array and the required sum of its elements.
Output
If Petya can win, print "YES" (without quotes) in the first line. Then print Petya's array in the second line. The array should contain N positive integers with sum equal to S. In the third line print K. If there are many correct answers, you can print any of them.
If Petya can't win, print "NO" (without quotes).
You can print each letter in any register (lowercase or uppercase).
Examples
Input
1 4
Output
YES
4
2
Input
3 4
Output
NO
Input
3 8
Output
YES
2 1 5
4
Submitted Solution:
```
import sys,os,io
import math,bisect,operator
inf,mod = float('inf'),10**9+7
# sys.setrecursionlimit(10 ** 6)
from itertools import groupby,accumulate
from heapq import heapify,heappop,heappush
from collections import deque,Counter,defaultdict
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
Neo = lambda : list(map(int,input().split()))
test,m = Neo()
n = test
if m/n < 2:
print('NO')
else:
print('YES')
t = m//n
Ans = [t]*(n-1)
Ans.append(t+m%n)
print(*Ans)
print(1)
``` | instruction | 0 | 74,320 | 19 | 148,640 |
Yes | output | 1 | 74,320 | 19 | 148,641 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya and Vasya are competing with each other in a new interesting game as they always do.
At the beginning of the game Petya has to come up with an array of N positive integers. Sum of all elements in his array should be equal to S. Then Petya has to select an integer K such that 0 ≤ K ≤ S.
In order to win, Vasya has to find a non-empty subarray in Petya's array such that the sum of all selected elements equals to either K or S - K. Otherwise Vasya loses.
You are given integers N and S. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.
Input
The first line contains two integers N and S (1 ≤ N ≤ S ≤ 10^{6}) — the required length of the array and the required sum of its elements.
Output
If Petya can win, print "YES" (without quotes) in the first line. Then print Petya's array in the second line. The array should contain N positive integers with sum equal to S. In the third line print K. If there are many correct answers, you can print any of them.
If Petya can't win, print "NO" (without quotes).
You can print each letter in any register (lowercase or uppercase).
Examples
Input
1 4
Output
YES
4
2
Input
3 4
Output
NO
Input
3 8
Output
YES
2 1 5
4
Submitted Solution:
```
n,s=[int(i) for i in input().split()]
chk=[]
for i in range(1,n+1):
chk.append(min(2,s-2*i))
if len(set(chk))==1:
print('YES')
print(*(chk[:n-1]+[2+s-sum(chk)]))
print(1)
else:
print('NO')
``` | instruction | 0 | 74,321 | 19 | 148,642 |
No | output | 1 | 74,321 | 19 | 148,643 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya and Vasya are competing with each other in a new interesting game as they always do.
At the beginning of the game Petya has to come up with an array of N positive integers. Sum of all elements in his array should be equal to S. Then Petya has to select an integer K such that 0 ≤ K ≤ S.
In order to win, Vasya has to find a non-empty subarray in Petya's array such that the sum of all selected elements equals to either K or S - K. Otherwise Vasya loses.
You are given integers N and S. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.
Input
The first line contains two integers N and S (1 ≤ N ≤ S ≤ 10^{6}) — the required length of the array and the required sum of its elements.
Output
If Petya can win, print "YES" (without quotes) in the first line. Then print Petya's array in the second line. The array should contain N positive integers with sum equal to S. In the third line print K. If there are many correct answers, you can print any of them.
If Petya can't win, print "NO" (without quotes).
You can print each letter in any register (lowercase or uppercase).
Examples
Input
1 4
Output
YES
4
2
Input
3 4
Output
NO
Input
3 8
Output
YES
2 1 5
4
Submitted Solution:
```
n, s = map(int, input().split())
flag = False
B = [2] * (n - 1) + [s - 2 * (n - 1)]
if s > 2 * (n - 1) and s >= 2:
print("YES")
print(*B)
print(1)
else:
print("NO")
``` | instruction | 0 | 74,322 | 19 | 148,644 |
No | output | 1 | 74,322 | 19 | 148,645 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya and Vasya are competing with each other in a new interesting game as they always do.
At the beginning of the game Petya has to come up with an array of N positive integers. Sum of all elements in his array should be equal to S. Then Petya has to select an integer K such that 0 ≤ K ≤ S.
In order to win, Vasya has to find a non-empty subarray in Petya's array such that the sum of all selected elements equals to either K or S - K. Otherwise Vasya loses.
You are given integers N and S. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.
Input
The first line contains two integers N and S (1 ≤ N ≤ S ≤ 10^{6}) — the required length of the array and the required sum of its elements.
Output
If Petya can win, print "YES" (without quotes) in the first line. Then print Petya's array in the second line. The array should contain N positive integers with sum equal to S. In the third line print K. If there are many correct answers, you can print any of them.
If Petya can't win, print "NO" (without quotes).
You can print each letter in any register (lowercase or uppercase).
Examples
Input
1 4
Output
YES
4
2
Input
3 4
Output
NO
Input
3 8
Output
YES
2 1 5
4
Submitted Solution:
```
n,s=map(int,input().split())
if s-n<=1:
print('NO')
else:
print("YES")
if n==1:
print(s)
print(n+1)
else:
print('2 '+'1 '*(n-2)+str(s-n))
print(n+1)
``` | instruction | 0 | 74,323 | 19 | 148,646 |
No | output | 1 | 74,323 | 19 | 148,647 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya and Vasya are competing with each other in a new interesting game as they always do.
At the beginning of the game Petya has to come up with an array of N positive integers. Sum of all elements in his array should be equal to S. Then Petya has to select an integer K such that 0 ≤ K ≤ S.
In order to win, Vasya has to find a non-empty subarray in Petya's array such that the sum of all selected elements equals to either K or S - K. Otherwise Vasya loses.
You are given integers N and S. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.
Input
The first line contains two integers N and S (1 ≤ N ≤ S ≤ 10^{6}) — the required length of the array and the required sum of its elements.
Output
If Petya can win, print "YES" (without quotes) in the first line. Then print Petya's array in the second line. The array should contain N positive integers with sum equal to S. In the third line print K. If there are many correct answers, you can print any of them.
If Petya can't win, print "NO" (without quotes).
You can print each letter in any register (lowercase or uppercase).
Examples
Input
1 4
Output
YES
4
2
Input
3 4
Output
NO
Input
3 8
Output
YES
2 1 5
4
Submitted Solution:
```
n,m=map(int,input().split())
if m//2<n:print("NO")
else:print("YES","1 "*(n-1)+str(m-n+1),m//2+1 if n>1 else m-1,sep="\n")
``` | instruction | 0 | 74,324 | 19 | 148,648 |
No | output | 1 | 74,324 | 19 | 148,649 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play a game. The game consists of several sets, and each set consists of several rounds. Each round is won either by Alice or by Bob, and the set ends when one of the players has won x rounds in a row. For example, if Bob won five rounds in a row and x = 2, then two sets ends.
You know that Alice and Bob have already played n rounds, and you know the results of some rounds. For each x from 1 to n, calculate the maximum possible number of sets that could have already finished if each set lasts until one of the players wins x rounds in a row. It is possible that the last set is still not finished — in that case, you should not count it in the answer.
Input
The first line contains one integer n (1 ≤ n ≤ 10^6) — the number of rounds.
The second line contains one string s of length n — the descriptions of rounds. If the i-th element of the string is 0, then Alice won the i-th round; if it is 1, then Bob won the i-th round, and if it is ?, then you don't know who won the i-th round.
Output
In the only line print n integers. The i-th integer should be equal to the maximum possible number of sets that could have already finished if each set lasts until one of the players wins i rounds in a row.
Examples
Input
6
11?000
Output
6 3 2 1 0 0
Input
5
01?01
Output
5 1 0 0 0
Input
12
???1??????1?
Output
12 6 4 3 2 2 1 1 1 1 1 1
Note
Let's consider the first test case:
* if x = 1 and s = 110000 or s = 111000 then there are six finished sets;
* if x = 2 and s = 110000 then there are three finished sets;
* if x = 3 and s = 111000 then there are two finished sets;
* if x = 4 and s = 110000 then there is one finished set;
* if x = 5 then there are no finished sets;
* if x = 6 then there are no finished sets. | instruction | 0 | 74,341 | 19 | 148,682 |
Tags: binary search, data structures, dp, greedy, two pointers
Correct Solution:
```
import sys
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]
def RLE(s):
cc=[]
ww=[]
pc=s[0]
w=0
for c in s:
if c==pc:w+=1
else:
cc.append(pc)
ww.append(w)
w=1
pc=c
cc.append(pc)
ww.append(w)
return cc,ww
n=II()
s=SI()
ans=[]
#print(len(s))
cc,ww=RLE(s)
#print(cc)
#print(ww)
if len(cc)==1 and cc[0]=="?":
for x in range(1,n+1):
ans.append(n//x)
print(*ans)
exit()
if cc[0]=="?":
ww[1]+=ww[0]
ww[0]=0
if cc[-1]=="?":
ww[-2]+=ww[-1]
ww[-1]=0
for i in range(len(cc)-2):
if cc[i+1]=="?" and cc[i]==cc[i+2]:
ww[i+2]+=ww[i+1]+ww[i]
ww[i + 1]=ww[i]=0
#print(cc)
#print(ww)
aa=[]
for c,w in zip(cc,ww):
if w==0:continue
if len(aa)&1 and c!="?":aa.append(0)
aa.append(w)
aa.append(0)
#print(aa)
ans.append(n)
for x in range(2,n+1):
bb=[]
r=0
pq=0
move=0
cur=0
for i in range(0,len(aa),2):
a=aa[i]
q=aa[i+1]
cur+=(r+a+q)//x
r=min(q,(r+a+q)%x)
if min(x,pq)+a+move+q<=x:
if bb:
b=bb.pop()
bb[-1]+=b
bb.append(0)
pq=0
move=q
else:
bb+=[move+a,q]
move=0
pq=q
aa=bb
ans.append(cur)
#print(x,aa)
if cur==0:break
ans+=[0]*(n-len(ans))
print(*ans)
``` | output | 1 | 74,341 | 19 | 148,683 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play a game. The game consists of several sets, and each set consists of several rounds. Each round is won either by Alice or by Bob, and the set ends when one of the players has won x rounds in a row. For example, if Bob won five rounds in a row and x = 2, then two sets ends.
You know that Alice and Bob have already played n rounds, and you know the results of some rounds. For each x from 1 to n, calculate the maximum possible number of sets that could have already finished if each set lasts until one of the players wins x rounds in a row. It is possible that the last set is still not finished — in that case, you should not count it in the answer.
Input
The first line contains one integer n (1 ≤ n ≤ 10^6) — the number of rounds.
The second line contains one string s of length n — the descriptions of rounds. If the i-th element of the string is 0, then Alice won the i-th round; if it is 1, then Bob won the i-th round, and if it is ?, then you don't know who won the i-th round.
Output
In the only line print n integers. The i-th integer should be equal to the maximum possible number of sets that could have already finished if each set lasts until one of the players wins i rounds in a row.
Examples
Input
6
11?000
Output
6 3 2 1 0 0
Input
5
01?01
Output
5 1 0 0 0
Input
12
???1??????1?
Output
12 6 4 3 2 2 1 1 1 1 1 1
Note
Let's consider the first test case:
* if x = 1 and s = 110000 or s = 111000 then there are six finished sets;
* if x = 2 and s = 110000 then there are three finished sets;
* if x = 3 and s = 111000 then there are two finished sets;
* if x = 4 and s = 110000 then there is one finished set;
* if x = 5 then there are no finished sets;
* if x = 6 then there are no finished sets. | instruction | 0 | 74,342 | 19 | 148,684 |
Tags: binary search, data structures, dp, greedy, two pointers
Correct Solution:
```
import sys
input = sys.stdin.readline
n = int(input())
s = input()
cnt0 = 0
cnt1 = 0
ans = [0] * n
last = [-1] * n
check = [[i+1] for i in range(n)]
check[0] = []
for i in range(n):
if s[i] == '1':
cnt0 = 0
cnt1 += 1
elif s[i] == '0':
cnt0 += 1
cnt1 = 0
else:
cnt0 += 1
cnt1 += 1
for j in check[i]:
if j<=max(cnt0, cnt1) and j<=i-last[j-1]:
ans[j-1] += 1
last[j-1] = i
if i+j<n:
check[i+j].append(j)
else:
pos = i+j-max(cnt0, cnt1)
if pos < n:
check[pos].append(j)
ans[0] = n
print(" ".join(map(str, ans)))
``` | output | 1 | 74,342 | 19 | 148,685 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play a game. The game consists of several sets, and each set consists of several rounds. Each round is won either by Alice or by Bob, and the set ends when one of the players has won x rounds in a row. For example, if Bob won five rounds in a row and x = 2, then two sets ends.
You know that Alice and Bob have already played n rounds, and you know the results of some rounds. For each x from 1 to n, calculate the maximum possible number of sets that could have already finished if each set lasts until one of the players wins x rounds in a row. It is possible that the last set is still not finished — in that case, you should not count it in the answer.
Input
The first line contains one integer n (1 ≤ n ≤ 10^6) — the number of rounds.
The second line contains one string s of length n — the descriptions of rounds. If the i-th element of the string is 0, then Alice won the i-th round; if it is 1, then Bob won the i-th round, and if it is ?, then you don't know who won the i-th round.
Output
In the only line print n integers. The i-th integer should be equal to the maximum possible number of sets that could have already finished if each set lasts until one of the players wins i rounds in a row.
Examples
Input
6
11?000
Output
6 3 2 1 0 0
Input
5
01?01
Output
5 1 0 0 0
Input
12
???1??????1?
Output
12 6 4 3 2 2 1 1 1 1 1 1
Note
Let's consider the first test case:
* if x = 1 and s = 110000 or s = 111000 then there are six finished sets;
* if x = 2 and s = 110000 then there are three finished sets;
* if x = 3 and s = 111000 then there are two finished sets;
* if x = 4 and s = 110000 then there is one finished set;
* if x = 5 then there are no finished sets;
* if x = 6 then there are no finished sets. | instruction | 0 | 74,343 | 19 | 148,686 |
Tags: binary search, data structures, dp, greedy, two pointers
Correct Solution:
```
class bitset():
def __init__(self,n):
self.size=1<<((n+2).bit_length())
self.bit=[0]*(self.size+1)
def append(self,val):
val+=1
id=val
while id<=self.size:
self.bit[id]+=1
id+=(id)&(-id)
def erase(self,val):
val+=1
id=val
while id<=self.size:
self.bit[id]-=1
id+=(id)&(-id)
def cnt(self,val):
res_sum = 0
val+=1
while val > 0:
res_sum += self.bit[val]
val -= val&(-val)
return res_sum
def next(self,val):
val+=1
base=self.cnt(val-1)
start=0
end=self.size
while end-start>1:
test=(end+start)//2
if self.bit[test]>base:
end=test
else:
start=test
base-=self.bit[test]
return end-1
n=int(input())
s=input()
alice=[int(s[i]=="0") for i in range(n)]
bob=[int(s[i]=="1") for i in range(n)]
for i in range(1,n):
alice[i]+=alice[i-1]
bob[i]+=bob[i-1]
alice.append(0)
bob.append(0)
update_que=[[] for i in range(n)]
alice_win=[]
id=0
while id<n:
if s[id]!="0":
pos=id
while pos<n and s[pos]!="0":
pos+=1
update_que[pos-id-1].append(id)
id=pos
else:
id+=1
bob_win=[]
id=0
while id<n:
if s[id]!="1":
pos=id
while pos<n and s[pos]!="1":
pos+=1
update_que[pos-id-1].append(id)
id=pos
else:
id+=1
bst=bitset(n)
bst.append(n)
ans=[0]*n
for i in range(n-1,-1,-1):
for id in update_que[i]:
bst.append(id)
pos=0
res=0
while pos<n-i:
check1=alice[pos+i]-alice[pos-1]
check2=bob[pos+i]-bob[pos-1]
if not check1 or not check2:
res+=1
pos+=i+1
else:
npos=bst.next(pos)
if npos==n:
break
else:
pos=npos+i+1
res+=1
ans[i]=res
print(*ans)
``` | output | 1 | 74,343 | 19 | 148,687 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play a game. The game consists of several sets, and each set consists of several rounds. Each round is won either by Alice or by Bob, and the set ends when one of the players has won x rounds in a row. For example, if Bob won five rounds in a row and x = 2, then two sets ends.
You know that Alice and Bob have already played n rounds, and you know the results of some rounds. For each x from 1 to n, calculate the maximum possible number of sets that could have already finished if each set lasts until one of the players wins x rounds in a row. It is possible that the last set is still not finished — in that case, you should not count it in the answer.
Input
The first line contains one integer n (1 ≤ n ≤ 10^6) — the number of rounds.
The second line contains one string s of length n — the descriptions of rounds. If the i-th element of the string is 0, then Alice won the i-th round; if it is 1, then Bob won the i-th round, and if it is ?, then you don't know who won the i-th round.
Output
In the only line print n integers. The i-th integer should be equal to the maximum possible number of sets that could have already finished if each set lasts until one of the players wins i rounds in a row.
Examples
Input
6
11?000
Output
6 3 2 1 0 0
Input
5
01?01
Output
5 1 0 0 0
Input
12
???1??????1?
Output
12 6 4 3 2 2 1 1 1 1 1 1
Note
Let's consider the first test case:
* if x = 1 and s = 110000 or s = 111000 then there are six finished sets;
* if x = 2 and s = 110000 then there are three finished sets;
* if x = 3 and s = 111000 then there are two finished sets;
* if x = 4 and s = 110000 then there is one finished set;
* if x = 5 then there are no finished sets;
* if x = 6 then there are no finished sets. | instruction | 0 | 74,344 | 19 | 148,688 |
Tags: binary search, data structures, dp, greedy, two pointers
Correct Solution:
```
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop
import math
from collections import *
from functools import reduce,cmp_to_key,lru_cache
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
# import sys
# input = sys.stdin.readline
M = mod = 10**9 + 7
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)
def li():return [int(i) for i in input().rstrip().split()]
def st():return str(input().rstrip())[2:-1]
def val():return int(input().rstrip())
def li2():return [str(i)[2:-1] for i in input().rstrip().split()]
def li3():return [int(i) for i in st()]
n = val()
s = st()
pref0 = [0]
pref1 = [0]
for i in s:
if i == '1':
pref1.append(pref1[-1] + 1)
pref0.append(pref0[-1])
elif i == '0':
pref0.append(pref0[-1] + 1)
pref1.append(pref1[-1])
else:
pref1.append(pref1[-1] + 1)
pref0.append(pref0[-1] + 1)
next0 = [-1]
next1 = [-1]
m1 = m2 = -1
for i in range(n):
if s[i] == '1':m2 = i
if s[i] == '0':m1 = i
next0.append(m1)
next1.append(m2)
flag = 0
last = float('inf')
for x in range(1, n + 1):
if flag:
ans = 0
else:
ans = start = 0
while start + x <= n and ans < last:
# print(start)
if pref0[start + x] - pref0[start] == x:
start += x
ans += 1
elif pref1[start + x] - pref1[start] == x:
start += x
ans += 1
else:
start = next0[start + x] if next1[start + x] > next0[start + x] else next1[start + x]
start += 1
if not ans:flag = 1
last = ans
print(ans,end = ' ')
``` | output | 1 | 74,344 | 19 | 148,689 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play a game. The game consists of several sets, and each set consists of several rounds. Each round is won either by Alice or by Bob, and the set ends when one of the players has won x rounds in a row. For example, if Bob won five rounds in a row and x = 2, then two sets ends.
You know that Alice and Bob have already played n rounds, and you know the results of some rounds. For each x from 1 to n, calculate the maximum possible number of sets that could have already finished if each set lasts until one of the players wins x rounds in a row. It is possible that the last set is still not finished — in that case, you should not count it in the answer.
Input
The first line contains one integer n (1 ≤ n ≤ 10^6) — the number of rounds.
The second line contains one string s of length n — the descriptions of rounds. If the i-th element of the string is 0, then Alice won the i-th round; if it is 1, then Bob won the i-th round, and if it is ?, then you don't know who won the i-th round.
Output
In the only line print n integers. The i-th integer should be equal to the maximum possible number of sets that could have already finished if each set lasts until one of the players wins i rounds in a row.
Examples
Input
6
11?000
Output
6 3 2 1 0 0
Input
5
01?01
Output
5 1 0 0 0
Input
12
???1??????1?
Output
12 6 4 3 2 2 1 1 1 1 1 1
Note
Let's consider the first test case:
* if x = 1 and s = 110000 or s = 111000 then there are six finished sets;
* if x = 2 and s = 110000 then there are three finished sets;
* if x = 3 and s = 111000 then there are two finished sets;
* if x = 4 and s = 110000 then there is one finished set;
* if x = 5 then there are no finished sets;
* if x = 6 then there are no finished sets. | instruction | 0 | 74,345 | 19 | 148,690 |
Tags: binary search, data structures, dp, greedy, two pointers
Correct Solution:
```
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop,heapify
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline
from itertools import accumulate
M = mod = 998244353
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)
def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n')]
def li3():return [int(i) for i in input().rstrip('\n')]
n = val()
s = st()
pref0 = [0]
pref1 = [0]
for i in s:
if i == '1':
pref1.append(pref1[-1] + 1)
pref0.append(pref0[-1])
elif i == '0':
pref0.append(pref0[-1] + 1)
pref1.append(pref1[-1])
else:
pref1.append(pref1[-1] + 1)
pref0.append(pref0[-1] + 1)
next0 = [-1]
next1 = [-1]
m1 = m2 = -1
for i in range(n):
if s[i] == '1':m2 = i
if s[i] == '0':m1 = i
next0.append(m1)
next1.append(m2)
flag = 0
last = float('inf')
for x in range(1, n + 1):
if flag:
ans = 0
else:
ans = start = 0
while start + x <= n:
# print(start)
if pref0[start + x] - pref0[start] == x:
start += x
ans += 1
elif pref1[start + x] - pref1[start] == x:
start += x
ans += 1
else:
start = next0[start + x] if next1[start + x] > next0[start + x] else next1[start + x]
start += 1
if not ans:flag = 1
print(ans,end = ' ')
``` | output | 1 | 74,345 | 19 | 148,691 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play a game. The game consists of several sets, and each set consists of several rounds. Each round is won either by Alice or by Bob, and the set ends when one of the players has won x rounds in a row. For example, if Bob won five rounds in a row and x = 2, then two sets ends.
You know that Alice and Bob have already played n rounds, and you know the results of some rounds. For each x from 1 to n, calculate the maximum possible number of sets that could have already finished if each set lasts until one of the players wins x rounds in a row. It is possible that the last set is still not finished — in that case, you should not count it in the answer.
Input
The first line contains one integer n (1 ≤ n ≤ 10^6) — the number of rounds.
The second line contains one string s of length n — the descriptions of rounds. If the i-th element of the string is 0, then Alice won the i-th round; if it is 1, then Bob won the i-th round, and if it is ?, then you don't know who won the i-th round.
Output
In the only line print n integers. The i-th integer should be equal to the maximum possible number of sets that could have already finished if each set lasts until one of the players wins i rounds in a row.
Examples
Input
6
11?000
Output
6 3 2 1 0 0
Input
5
01?01
Output
5 1 0 0 0
Input
12
???1??????1?
Output
12 6 4 3 2 2 1 1 1 1 1 1
Note
Let's consider the first test case:
* if x = 1 and s = 110000 or s = 111000 then there are six finished sets;
* if x = 2 and s = 110000 then there are three finished sets;
* if x = 3 and s = 111000 then there are two finished sets;
* if x = 4 and s = 110000 then there is one finished set;
* if x = 5 then there are no finished sets;
* if x = 6 then there are no finished sets. | instruction | 0 | 74,346 | 19 | 148,692 |
Tags: binary search, data structures, dp, greedy, two pointers
Correct Solution:
```
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop
import math
from collections import *
from functools import reduce,cmp_to_key,lru_cache
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
# import sys
# input = sys.stdin.readline
M = mod = 10**9 + 7
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)
def li():return [int(i) for i in input().rstrip().split()]
def st():return str(input().rstrip())[2:-1]
def val():return int(input().rstrip())
def li2():return [str(i)[2:-1] for i in input().rstrip().split()]
def li3():return [int(i) for i in st()]
n = val()
s = st()
pref0 = [0]
pref1 = [0]
for i in s:
if i == '1':
pref1.append(pref1[-1] + 1)
pref0.append(pref0[-1])
elif i == '0':
pref0.append(pref0[-1] + 1)
pref1.append(pref1[-1])
else:
pref1.append(pref1[-1] + 1)
pref0.append(pref0[-1] + 1)
next0 = [-1]
next1 = [-1]
m1 = m2 = -1
for i in range(n):
if s[i] == '1':m2 = i
if s[i] == '0':m1 = i
next0.append(m1)
next1.append(m2)
flag = 0
last = float('inf')
for x in range(1, n + 1):
if flag:
ans = 0
else:
ans = start = 0
while start + x <= n:
# print(start)
if pref0[start + x] - pref0[start] == x:
start += x
ans += 1
elif pref1[start + x] - pref1[start] == x:
start += x
ans += 1
else:
start = next0[start + x] if next1[start + x] > next0[start + x] else next1[start + x]
start += 1
if not ans:flag = 1
last = ans
print(ans,end = ' ')
``` | output | 1 | 74,346 | 19 | 148,693 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play a game. The game consists of several sets, and each set consists of several rounds. Each round is won either by Alice or by Bob, and the set ends when one of the players has won x rounds in a row. For example, if Bob won five rounds in a row and x = 2, then two sets ends.
You know that Alice and Bob have already played n rounds, and you know the results of some rounds. For each x from 1 to n, calculate the maximum possible number of sets that could have already finished if each set lasts until one of the players wins x rounds in a row. It is possible that the last set is still not finished — in that case, you should not count it in the answer.
Input
The first line contains one integer n (1 ≤ n ≤ 10^6) — the number of rounds.
The second line contains one string s of length n — the descriptions of rounds. If the i-th element of the string is 0, then Alice won the i-th round; if it is 1, then Bob won the i-th round, and if it is ?, then you don't know who won the i-th round.
Output
In the only line print n integers. The i-th integer should be equal to the maximum possible number of sets that could have already finished if each set lasts until one of the players wins i rounds in a row.
Examples
Input
6
11?000
Output
6 3 2 1 0 0
Input
5
01?01
Output
5 1 0 0 0
Input
12
???1??????1?
Output
12 6 4 3 2 2 1 1 1 1 1 1
Note
Let's consider the first test case:
* if x = 1 and s = 110000 or s = 111000 then there are six finished sets;
* if x = 2 and s = 110000 then there are three finished sets;
* if x = 3 and s = 111000 then there are two finished sets;
* if x = 4 and s = 110000 then there is one finished set;
* if x = 5 then there are no finished sets;
* if x = 6 then there are no finished sets. | instruction | 0 | 74,347 | 19 | 148,694 |
Tags: binary search, data structures, dp, greedy, two pointers
Correct Solution:
```
class BalancingTree:
def __init__(self, n):
self.N = n
self.root = self.node(1<<n, 1<<n)
def append(self, v):
v += 1
nd = self.root
while True:
if v == nd.value:
return 0
else:
mi, ma = min(v, nd.value), max(v, nd.value)
if mi < nd.pivot:
nd.value = ma
if nd.left:
nd = nd.left
v = mi
else:
p = nd.pivot
nd.left = self.node(mi, p - (p&-p)//2)
break
else:
nd.value = mi
if nd.right:
nd = nd.right
v = ma
else:
p = nd.pivot
nd.right = self.node(ma, p + (p&-p)//2)
break
def leftmost(self, nd):
if nd.left: return self.leftmost(nd.left)
return nd
def rightmost(self, nd):
if nd.right: return self.rightmost(nd.right)
return nd
def find_l(self, v):
v += 1
nd = self.root
prev = 0
if nd.value < v: prev = nd.value
while True:
if v <= nd.value:
if nd.left:
nd = nd.left
else:
return prev - 1
else:
prev = nd.value
if nd.right:
nd = nd.right
else:
return prev - 1
def find_r(self, v):
v += 1
nd = self.root
prev = 0
if nd.value > v: prev = nd.value
while True:
if v < nd.value:
prev = nd.value
if nd.left:
nd = nd.left
else:
return prev - 1
else:
if nd.right:
nd = nd.right
else:
return prev - 1
@property
def max(self):
return self.find_l((1<<self.N)-1)
@property
def min(self):
return self.find_r(-1)
def delete(self, v, nd = None, prev = None):
v += 1
if not nd: nd = self.root
if not prev: prev = nd
while v != nd.value:
prev = nd
if v <= nd.value:
if nd.left:
nd = nd.left
else:
return
else:
if nd.right:
nd = nd.right
else:
return
if (not nd.left) and (not nd.right):
if nd.value < prev.value:
prev.left = None
else:
prev.right = None
elif not nd.left:
if nd.value < prev.value:
prev.left = nd.right
else:
prev.right = nd.right
elif not nd.right:
if nd.value < prev.value:
prev.left = nd.left
else:
prev.right = nd.left
else:
nd.value = self.leftmost(nd.right).value
self.delete(nd.value - 1, nd.right, nd)
def __contains__(self, v: int) -> bool:
return self.find_r(v - 1) == v
class node:
def __init__(self, v, p):
self.value = v
self.pivot = p
self.left = None
self.right = None
n=int(input())
s=input()
alice=[int(s[i]=="0") for i in range(n)]
bob=[int(s[i]=="1") for i in range(n)]
for i in range(1,n):
alice[i]+=alice[i-1]
bob[i]+=bob[i-1]
alice.append(0)
bob.append(0)
update_que=[[] for i in range(n)]
alice_win=[]
id=0
while id<n:
if s[id]!="0":
pos=id
while pos<n and s[pos]!="0":
pos+=1
update_que[pos-id-1].append(id)
id=pos
else:
id+=1
bob_win=[]
id=0
while id<n:
if s[id]!="1":
pos=id
while pos<n and s[pos]!="1":
pos+=1
update_que[pos-id-1].append(id)
id=pos
else:
id+=1
bst=BalancingTree(20)
bst.append(n)
ans=[0]*n
for i in range(n-1,-1,-1):
for id in update_que[i]:
bst.append(id)
pos=0
res=0
while pos<n-i:
check1=alice[pos+i]-alice[pos-1]
check2=bob[pos+i]-bob[pos-1]
if not check1 or not check2:
res+=1
pos+=i+1
else:
npos=bst.find_r(pos-1)
if npos==n:
break
else:
pos=npos+i+1
res+=1
ans[i]=res
print(*ans)
``` | output | 1 | 74,347 | 19 | 148,695 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play a game. The game consists of several sets, and each set consists of several rounds. Each round is won either by Alice or by Bob, and the set ends when one of the players has won x rounds in a row. For example, if Bob won five rounds in a row and x = 2, then two sets ends.
You know that Alice and Bob have already played n rounds, and you know the results of some rounds. For each x from 1 to n, calculate the maximum possible number of sets that could have already finished if each set lasts until one of the players wins x rounds in a row. It is possible that the last set is still not finished — in that case, you should not count it in the answer.
Input
The first line contains one integer n (1 ≤ n ≤ 10^6) — the number of rounds.
The second line contains one string s of length n — the descriptions of rounds. If the i-th element of the string is 0, then Alice won the i-th round; if it is 1, then Bob won the i-th round, and if it is ?, then you don't know who won the i-th round.
Output
In the only line print n integers. The i-th integer should be equal to the maximum possible number of sets that could have already finished if each set lasts until one of the players wins i rounds in a row.
Examples
Input
6
11?000
Output
6 3 2 1 0 0
Input
5
01?01
Output
5 1 0 0 0
Input
12
???1??????1?
Output
12 6 4 3 2 2 1 1 1 1 1 1
Note
Let's consider the first test case:
* if x = 1 and s = 110000 or s = 111000 then there are six finished sets;
* if x = 2 and s = 110000 then there are three finished sets;
* if x = 3 and s = 111000 then there are two finished sets;
* if x = 4 and s = 110000 then there is one finished set;
* if x = 5 then there are no finished sets;
* if x = 6 then there are no finished sets. | instruction | 0 | 74,348 | 19 | 148,696 |
Tags: binary search, data structures, dp, greedy, two pointers
Correct Solution:
```
n=int(input())
s=input()
N=n
N0 = 2**(N-1).bit_length()
data = [n]*(2*N0)
INF = n
# 区間[l, r+1)の値をvに書き換える
# vは(t, value)という値にする (新しい値ほどtは大きくなる)
def update(l, r, v):
L = l + N0; R = r + N0
while L < R:
if R & 1:
R -= 1
data[R-1] = min(v,data[R-1])
if L & 1:
data[L-1] = min(v,data[L-1])
L += 1
L >>= 1; R >>= 1
# a_iの現在の値を取得
def _query(k):
k += N0-1
s = INF
while k >= 0:
if data[k]:
s = min(s, data[k])
k = (k - 1) // 2
return s
# これを呼び出す
def query(k):
return _query(k)
alice=[int(s[i]=="0") for i in range(n)]
bob=[int(s[i]=="1") for i in range(n)]
for i in range(1,n):
alice[i]+=alice[i-1]
bob[i]+=bob[i-1]
alice.append(0)
bob.append(0)
update_que=[[] for i in range(n)]
alice_win=[]
id=0
while id<n:
if s[id]!="0":
pos=id
while pos<n and s[pos]!="0":
pos+=1
update_que[pos-id-1].append(id)
id=pos
else:
id+=1
bob_win=[]
id=0
while id<n:
if s[id]!="1":
pos=id
while pos<n and s[pos]!="1":
pos+=1
update_que[pos-id-1].append(id)
id=pos
else:
id+=1
ans=[0]*n
for i in range(n-1,-1,-1):
for id in update_que[i]:
update(0,id+1,id)
pos=0
res=0
while pos<n-i:
check1=alice[pos+i]-alice[pos-1]
check2=bob[pos+i]-bob[pos-1]
if not check1 or not check2:
res+=1
pos+=i+1
else:
npos=query(pos)
if query(pos)==n:
break
else:
pos=npos+i+1
res+=1
ans[i]=res
print(*ans)
``` | output | 1 | 74,348 | 19 | 148,697 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob play a game. The game consists of several sets, and each set consists of several rounds. Each round is won either by Alice or by Bob, and the set ends when one of the players has won x rounds in a row. For example, if Bob won five rounds in a row and x = 2, then two sets ends.
You know that Alice and Bob have already played n rounds, and you know the results of some rounds. For each x from 1 to n, calculate the maximum possible number of sets that could have already finished if each set lasts until one of the players wins x rounds in a row. It is possible that the last set is still not finished — in that case, you should not count it in the answer.
Input
The first line contains one integer n (1 ≤ n ≤ 10^6) — the number of rounds.
The second line contains one string s of length n — the descriptions of rounds. If the i-th element of the string is 0, then Alice won the i-th round; if it is 1, then Bob won the i-th round, and if it is ?, then you don't know who won the i-th round.
Output
In the only line print n integers. The i-th integer should be equal to the maximum possible number of sets that could have already finished if each set lasts until one of the players wins i rounds in a row.
Examples
Input
6
11?000
Output
6 3 2 1 0 0
Input
5
01?01
Output
5 1 0 0 0
Input
12
???1??????1?
Output
12 6 4 3 2 2 1 1 1 1 1 1
Note
Let's consider the first test case:
* if x = 1 and s = 110000 or s = 111000 then there are six finished sets;
* if x = 2 and s = 110000 then there are three finished sets;
* if x = 3 and s = 111000 then there are two finished sets;
* if x = 4 and s = 110000 then there is one finished set;
* if x = 5 then there are no finished sets;
* if x = 6 then there are no finished sets.
Submitted Solution:
```
import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]
def RLE(s):
cc=[]
ww=[]
pc=s[0]
w=0
for c in s:
if c==pc:w+=1
else:
cc.append(pc)
ww.append(w)
w=1
pc=c
cc.append(pc)
ww.append(w)
return cc,ww
n=II()
s=SI()
ans=[]
#print(len(s))
cc,ww=RLE(s)
#print(cc)
#print(ww)
if len(cc)==1 and cc[0]=="?":
for x in range(1,n+1):
ans.append(n//x)
print(*ans)
exit()
if cc[0]=="?":
ww[1]+=ww[0]
ww[0]=0
if cc[-1]=="?":
ww[-2]+=ww[-1]
ww[-1]=0
for i in range(len(cc)-2):
if cc[i+1]=="?" and cc[i]==cc[i+2]:
ww[i+2]+=ww[i+1]+ww[i]
ww[i + 1]=ww[i]=0
#print(cc)
#print(ww)
aa=[]
for c,w in zip(cc,ww):
if w==0:continue
if len(aa)&1 and c!="?":aa.append(0)
aa.append(w)
aa.append(0)
#print(aa)
ans.append(n)
for x in range(2,n+1):
bb=[]
r=0
pq=0
move=0
cur=0
for i in range(0,len(aa),2):
a=aa[i]
q=aa[i+1]
cur+=(r+a+q)//x
r=min(q,(r+a+q)%x)
if min(x,pq)+a+move+q<=x:
if bb:
b=bb.pop()
bb[-1]+=b
bb.append(0)
pq=0
move=q
else:
bb+=[move+a,q]
move=0
pq=q
aa=bb
ans.append(cur)
#print(x,aa)
if cur==0:break
ans+=[0]*(n-len(ans))
print(*ans)
``` | instruction | 0 | 74,349 | 19 | 148,698 |
Yes | output | 1 | 74,349 | 19 | 148,699 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob play a game. The game consists of several sets, and each set consists of several rounds. Each round is won either by Alice or by Bob, and the set ends when one of the players has won x rounds in a row. For example, if Bob won five rounds in a row and x = 2, then two sets ends.
You know that Alice and Bob have already played n rounds, and you know the results of some rounds. For each x from 1 to n, calculate the maximum possible number of sets that could have already finished if each set lasts until one of the players wins x rounds in a row. It is possible that the last set is still not finished — in that case, you should not count it in the answer.
Input
The first line contains one integer n (1 ≤ n ≤ 10^6) — the number of rounds.
The second line contains one string s of length n — the descriptions of rounds. If the i-th element of the string is 0, then Alice won the i-th round; if it is 1, then Bob won the i-th round, and if it is ?, then you don't know who won the i-th round.
Output
In the only line print n integers. The i-th integer should be equal to the maximum possible number of sets that could have already finished if each set lasts until one of the players wins i rounds in a row.
Examples
Input
6
11?000
Output
6 3 2 1 0 0
Input
5
01?01
Output
5 1 0 0 0
Input
12
???1??????1?
Output
12 6 4 3 2 2 1 1 1 1 1 1
Note
Let's consider the first test case:
* if x = 1 and s = 110000 or s = 111000 then there are six finished sets;
* if x = 2 and s = 110000 then there are three finished sets;
* if x = 3 and s = 111000 then there are two finished sets;
* if x = 4 and s = 110000 then there is one finished set;
* if x = 5 then there are no finished sets;
* if x = 6 then there are no finished sets.
Submitted Solution:
```
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop
import math
from collections import *
from functools import reduce,cmp_to_key,lru_cache
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
# import sys
# input = sys.stdin.readline
M = mod = 10**9 + 7
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)
def li():return [int(i) for i in input().rstrip().split()]
def st():return str(input().rstrip())[2:-1]
def val():return int(input().rstrip())
def li2():return [str(i)[2:-1] for i in input().rstrip().split()]
def li3():return [int(i) for i in st()]
n = val()
s = st()
pref0 = [0]
pref1 = [0]
for i in s:
if i == '1':
pref1.append(pref1[-1] + 1)
pref0.append(pref0[-1])
elif i == '0':
pref0.append(pref0[-1] + 1)
pref1.append(pref1[-1])
else:
pref1.append(pref1[-1] + 1)
pref0.append(pref0[-1] + 1)
next0 = []
next1 = []
next2 = []
m1, m2 = n, n
m3 = m2
for i in range(n-1, -1, -1):
if s[i] != '1':m1 = i
if s[i] != '0':m2 = i
if s[i] != '?':m3 = i
next0.append(m1)
next1.append(m2)
next2.append(m3)
next0.reverse()
next1.reverse()
next2.reverse()
flag = 0
last = float('inf')
for x in range(1, n + 1):
if flag:
ans = 0
else:
ans = start = 0
while start + x <= n and ans < last:
if pref0[start + x] - pref0[start] == x:
start += x
ans += 1
elif pref1[start + x] - pref1[start] == x:
start += x
ans += 1
else:
start = next1[start + 1] if next1[start + 1] > next0[start + 1] else next0[start + 1]
if not ans:flag = 1
last = ans
print(ans,end = ' ')
``` | instruction | 0 | 74,350 | 19 | 148,700 |
No | output | 1 | 74,350 | 19 | 148,701 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob play a game. The game consists of several sets, and each set consists of several rounds. Each round is won either by Alice or by Bob, and the set ends when one of the players has won x rounds in a row. For example, if Bob won five rounds in a row and x = 2, then two sets ends.
You know that Alice and Bob have already played n rounds, and you know the results of some rounds. For each x from 1 to n, calculate the maximum possible number of sets that could have already finished if each set lasts until one of the players wins x rounds in a row. It is possible that the last set is still not finished — in that case, you should not count it in the answer.
Input
The first line contains one integer n (1 ≤ n ≤ 10^6) — the number of rounds.
The second line contains one string s of length n — the descriptions of rounds. If the i-th element of the string is 0, then Alice won the i-th round; if it is 1, then Bob won the i-th round, and if it is ?, then you don't know who won the i-th round.
Output
In the only line print n integers. The i-th integer should be equal to the maximum possible number of sets that could have already finished if each set lasts until one of the players wins i rounds in a row.
Examples
Input
6
11?000
Output
6 3 2 1 0 0
Input
5
01?01
Output
5 1 0 0 0
Input
12
???1??????1?
Output
12 6 4 3 2 2 1 1 1 1 1 1
Note
Let's consider the first test case:
* if x = 1 and s = 110000 or s = 111000 then there are six finished sets;
* if x = 2 and s = 110000 then there are three finished sets;
* if x = 3 and s = 111000 then there are two finished sets;
* if x = 4 and s = 110000 then there is one finished set;
* if x = 5 then there are no finished sets;
* if x = 6 then there are no finished sets.
Submitted Solution:
```
import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]
def RLE(s):
cc=[]
ww=[]
pc=s[0]
w=0
for c in s:
if c==pc:w+=1
else:
cc.append(pc)
ww.append(w)
w=1
pc=c
cc.append(pc)
ww.append(w)
return cc,ww
n=II()
s=SI()
ans=[]
if s.count("?")==n:
for x in range(1,n+1):
ans.append(n//x)
print(*ans)
exit()
cc,ww=RLE(s)
#print(cc)
#print(ww)
if cc[0]=="?":
ww[1]+=ww[0]
ww[0]=0
if cc[-1]=="?":
ww[-2]+=ww[-1]
ww[-1]=0
for i in range(len(cc)-2):
if cc[i+1]=="?" and cc[i]==cc[i+2]:
ww[i]+=ww[i+1]+ww[i+2]
ww[i + 1]=ww[i + 2]=0
#print(cc)
#print(ww)
aa=[]
for c,w in zip(cc,ww):
if w==0:continue
if len(aa)&1 and c!="?":aa.append(0)
aa.append(w)
aa.append(0)
#print(aa)
ans.append(n)
for x in range(2,n+1):
bb=[]
r=0
pq=0
move=0
cur=0
for i in range(0,len(aa),2):
a=aa[i]
q=aa[i+1]
cur+=(r+a+q)//x
r=min(q,(r+a+q)%x)
if bb and min(x,bb[-1])+a+q<=x:
b=bb.pop()
bb[-1]+=b
bb.append(0)
move=q
else:
bb+=[move+a,q]
move=0
aa=bb
ans.append(cur)
if cur==0:break
ans+=[0]*(n-len(ans))
print(*ans)
``` | instruction | 0 | 74,351 | 19 | 148,702 |
No | output | 1 | 74,351 | 19 | 148,703 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob play a game. The game consists of several sets, and each set consists of several rounds. Each round is won either by Alice or by Bob, and the set ends when one of the players has won x rounds in a row. For example, if Bob won five rounds in a row and x = 2, then two sets ends.
You know that Alice and Bob have already played n rounds, and you know the results of some rounds. For each x from 1 to n, calculate the maximum possible number of sets that could have already finished if each set lasts until one of the players wins x rounds in a row. It is possible that the last set is still not finished — in that case, you should not count it in the answer.
Input
The first line contains one integer n (1 ≤ n ≤ 10^6) — the number of rounds.
The second line contains one string s of length n — the descriptions of rounds. If the i-th element of the string is 0, then Alice won the i-th round; if it is 1, then Bob won the i-th round, and if it is ?, then you don't know who won the i-th round.
Output
In the only line print n integers. The i-th integer should be equal to the maximum possible number of sets that could have already finished if each set lasts until one of the players wins i rounds in a row.
Examples
Input
6
11?000
Output
6 3 2 1 0 0
Input
5
01?01
Output
5 1 0 0 0
Input
12
???1??????1?
Output
12 6 4 3 2 2 1 1 1 1 1 1
Note
Let's consider the first test case:
* if x = 1 and s = 110000 or s = 111000 then there are six finished sets;
* if x = 2 and s = 110000 then there are three finished sets;
* if x = 3 and s = 111000 then there are two finished sets;
* if x = 4 and s = 110000 then there is one finished set;
* if x = 5 then there are no finished sets;
* if x = 6 then there are no finished sets.
Submitted Solution:
```
import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]
def RLE(s):
cc=[]
ww=[]
pc=s[0]
w=0
for c in s:
if c==pc:w+=1
else:
cc.append(pc)
ww.append(w)
w=1
pc=c
cc.append(pc)
ww.append(w)
return cc,ww
n=II()
s=SI()
ans=[]
cc,ww=RLE(s)
#print(cc)
#print(ww)
if len(cc)==1 and cc[0]=="?":
for x in range(1,n+1):
ans.append(n//x)
print(*ans)
exit()
if cc[0]=="?":
ww[1]+=ww[0]
ww[0]=0
if cc[-1]=="?":
ww[-2]+=ww[-1]
ww[-1]=0
for i in range(len(cc)-2):
if cc[i+1]=="?" and cc[i]==cc[i+2]:
ww[i+2]+=ww[i+1]+ww[i]
ww[i + 1]=ww[i]=0
#print(cc)
#print(ww)
aa=[]
for c,w in zip(cc,ww):
if w==0:continue
if len(aa)&1 and c!="?":aa.append(0)
aa.append(w)
aa.append(0)
#print(aa)
ans.append(n)
for x in range(2,n+1):
bb=[]
r=0
pq=0
move=0
cur=0
for i in range(0,len(aa),2):
a=aa[i]
q=aa[i+1]
cur+=(r+a+q)//x
r=min(q,(r+a+q)%x)
if bb and min(x,bb[-1])+a+q<=x:
b=bb.pop()
bb[-1]+=b
bb.append(0)
move=q
else:
bb+=[move+a,q]
move=0
aa=bb
ans.append(cur)
if cur==0:break
ans+=[0]*(n-len(ans))
print(*ans)
``` | instruction | 0 | 74,352 | 19 | 148,704 |
No | output | 1 | 74,352 | 19 | 148,705 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not.
First Petya puts a marble under the glass in position s. Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position p1, the glass from the second position to position p2 and so on. That is, a glass goes from position i to position pi. Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn't travel from one glass to another: it moves together with the glass it was initially been put in.
After all shuffling operations Petya shows Vasya that the ball has moved to position t. Vasya's task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position s to position t.
Input
The first line contains three integers: n, s, t (1 ≤ n ≤ 105; 1 ≤ s, t ≤ n) — the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p1, p2, ..., pn (1 ≤ pi ≤ n) — the shuffling operation parameters. It is guaranteed that all pi's are distinct.
Note that s can equal t.
Output
If the marble can move from position s to position t, then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.
Examples
Input
4 2 1
2 3 4 1
Output
3
Input
4 3 3
4 1 3 2
Output
0
Input
4 3 4
1 2 3 4
Output
-1
Input
3 1 3
2 1 3
Output
-1 | instruction | 0 | 74,476 | 19 | 148,952 |
Tags: implementation
Correct Solution:
```
c = []
a = []
c = input().split()
b = []
b = input().split()
n = int(c[0])
s = int(c[1])
t = int(c[2])
for i in range(n):
ele = int(b[i])
a.append(ele)
if s == t:
print(0)
exit()
j = 0
v = []
count = 0
v.append(int(a[s-1]))
for j in range(n-1):
v.append(int(a[int(v[j]-1)]))
for i in range(n):
if t == v[i]:
print(i+1)
exit()
print(-1)
``` | output | 1 | 74,476 | 19 | 148,953 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not.
First Petya puts a marble under the glass in position s. Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position p1, the glass from the second position to position p2 and so on. That is, a glass goes from position i to position pi. Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn't travel from one glass to another: it moves together with the glass it was initially been put in.
After all shuffling operations Petya shows Vasya that the ball has moved to position t. Vasya's task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position s to position t.
Input
The first line contains three integers: n, s, t (1 ≤ n ≤ 105; 1 ≤ s, t ≤ n) — the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p1, p2, ..., pn (1 ≤ pi ≤ n) — the shuffling operation parameters. It is guaranteed that all pi's are distinct.
Note that s can equal t.
Output
If the marble can move from position s to position t, then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.
Examples
Input
4 2 1
2 3 4 1
Output
3
Input
4 3 3
4 1 3 2
Output
0
Input
4 3 4
1 2 3 4
Output
-1
Input
3 1 3
2 1 3
Output
-1 | instruction | 0 | 74,477 | 19 | 148,954 |
Tags: implementation
Correct Solution:
```
import sys
import math
n, s, t = [int(x) for x in (sys.stdin.readline()).split()]
p = [int(x) for x in (sys.stdin.readline()).split()]
res = 0
k = s
while(s != t):
s = p[s - 1]
res += 1
if(k == s):
print(-1)
exit()
print(res)
``` | output | 1 | 74,477 | 19 | 148,955 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not.
First Petya puts a marble under the glass in position s. Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position p1, the glass from the second position to position p2 and so on. That is, a glass goes from position i to position pi. Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn't travel from one glass to another: it moves together with the glass it was initially been put in.
After all shuffling operations Petya shows Vasya that the ball has moved to position t. Vasya's task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position s to position t.
Input
The first line contains three integers: n, s, t (1 ≤ n ≤ 105; 1 ≤ s, t ≤ n) — the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p1, p2, ..., pn (1 ≤ pi ≤ n) — the shuffling operation parameters. It is guaranteed that all pi's are distinct.
Note that s can equal t.
Output
If the marble can move from position s to position t, then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.
Examples
Input
4 2 1
2 3 4 1
Output
3
Input
4 3 3
4 1 3 2
Output
0
Input
4 3 4
1 2 3 4
Output
-1
Input
3 1 3
2 1 3
Output
-1 | instruction | 0 | 74,478 | 19 | 148,956 |
Tags: implementation
Correct Solution:
```
n,s,t=map(int,input().split())
l=list(map(int,input().split()))
step=0
while(s!=t and l[s-1]!=0):
tmp=l[s-1]
l[s-1]=0
s=tmp
step+=1
if(s==t):
print(step)
else:
print(-1)
``` | output | 1 | 74,478 | 19 | 148,957 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not.
First Petya puts a marble under the glass in position s. Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position p1, the glass from the second position to position p2 and so on. That is, a glass goes from position i to position pi. Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn't travel from one glass to another: it moves together with the glass it was initially been put in.
After all shuffling operations Petya shows Vasya that the ball has moved to position t. Vasya's task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position s to position t.
Input
The first line contains three integers: n, s, t (1 ≤ n ≤ 105; 1 ≤ s, t ≤ n) — the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p1, p2, ..., pn (1 ≤ pi ≤ n) — the shuffling operation parameters. It is guaranteed that all pi's are distinct.
Note that s can equal t.
Output
If the marble can move from position s to position t, then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.
Examples
Input
4 2 1
2 3 4 1
Output
3
Input
4 3 3
4 1 3 2
Output
0
Input
4 3 4
1 2 3 4
Output
-1
Input
3 1 3
2 1 3
Output
-1 | instruction | 0 | 74,479 | 19 | 148,958 |
Tags: implementation
Correct Solution:
```
N,s,t=map(int,input().split())
A=list(map(int,input().split()))
s-=1
t-=1
ans=0
Taken=[False]*(N+1)
while(Taken[s]==False and s!=t):
Taken[s]=True
s=A[s]-1
ans+=1
if(s==t):
print(ans)
else:
print(-1)
``` | output | 1 | 74,479 | 19 | 148,959 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not.
First Petya puts a marble under the glass in position s. Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position p1, the glass from the second position to position p2 and so on. That is, a glass goes from position i to position pi. Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn't travel from one glass to another: it moves together with the glass it was initially been put in.
After all shuffling operations Petya shows Vasya that the ball has moved to position t. Vasya's task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position s to position t.
Input
The first line contains three integers: n, s, t (1 ≤ n ≤ 105; 1 ≤ s, t ≤ n) — the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p1, p2, ..., pn (1 ≤ pi ≤ n) — the shuffling operation parameters. It is guaranteed that all pi's are distinct.
Note that s can equal t.
Output
If the marble can move from position s to position t, then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.
Examples
Input
4 2 1
2 3 4 1
Output
3
Input
4 3 3
4 1 3 2
Output
0
Input
4 3 4
1 2 3 4
Output
-1
Input
3 1 3
2 1 3
Output
-1 | instruction | 0 | 74,480 | 19 | 148,960 |
Tags: implementation
Correct Solution:
```
def arr_inp():
return [int(x) for x in stdin.readline().split()]
from sys import stdin
from collections import defaultdict
n, s, t = arr_inp()
p, mem = arr_inp(), defaultdict(int)
p.insert(0, 0)
ans, root = 0, s
while (root != t):
if mem[root]:
exit(print(-1))
else:
mem[root] = 1
root = p[root]
ans += 1
print(ans)
``` | output | 1 | 74,480 | 19 | 148,961 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not.
First Petya puts a marble under the glass in position s. Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position p1, the glass from the second position to position p2 and so on. That is, a glass goes from position i to position pi. Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn't travel from one glass to another: it moves together with the glass it was initially been put in.
After all shuffling operations Petya shows Vasya that the ball has moved to position t. Vasya's task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position s to position t.
Input
The first line contains three integers: n, s, t (1 ≤ n ≤ 105; 1 ≤ s, t ≤ n) — the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p1, p2, ..., pn (1 ≤ pi ≤ n) — the shuffling operation parameters. It is guaranteed that all pi's are distinct.
Note that s can equal t.
Output
If the marble can move from position s to position t, then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.
Examples
Input
4 2 1
2 3 4 1
Output
3
Input
4 3 3
4 1 3 2
Output
0
Input
4 3 4
1 2 3 4
Output
-1
Input
3 1 3
2 1 3
Output
-1 | instruction | 0 | 74,481 | 19 | 148,962 |
Tags: implementation
Correct Solution:
```
n,s,t = [int(x) for x in input().split()]
p = [int(x) for x in input().split()]
if s != t :
i = 0
m = p[s-1]
flag = 0
while i<n:
if t == m :
flag = 1
break
else :
m = p[m-1]
i = i+1
if flag == 1:
print(i+1)
else:
print("-1")
else:
print("0")
``` | output | 1 | 74,481 | 19 | 148,963 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not.
First Petya puts a marble under the glass in position s. Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position p1, the glass from the second position to position p2 and so on. That is, a glass goes from position i to position pi. Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn't travel from one glass to another: it moves together with the glass it was initially been put in.
After all shuffling operations Petya shows Vasya that the ball has moved to position t. Vasya's task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position s to position t.
Input
The first line contains three integers: n, s, t (1 ≤ n ≤ 105; 1 ≤ s, t ≤ n) — the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p1, p2, ..., pn (1 ≤ pi ≤ n) — the shuffling operation parameters. It is guaranteed that all pi's are distinct.
Note that s can equal t.
Output
If the marble can move from position s to position t, then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.
Examples
Input
4 2 1
2 3 4 1
Output
3
Input
4 3 3
4 1 3 2
Output
0
Input
4 3 4
1 2 3 4
Output
-1
Input
3 1 3
2 1 3
Output
-1 | instruction | 0 | 74,482 | 19 | 148,964 |
Tags: implementation
Correct Solution:
```
def cs(f,l,v):
s=set()
t,c=f,0
while 1:
if t==l:
return c
t=v[t]
if t in s:
return -1
s.add(t)
c+=1
n,f,l=[int (c)-1 for c in input().split()]
v=[int (c)-1 for c in input().split()]
print(cs(f,l,v))
``` | output | 1 | 74,482 | 19 | 148,965 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not.
First Petya puts a marble under the glass in position s. Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position p1, the glass from the second position to position p2 and so on. That is, a glass goes from position i to position pi. Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn't travel from one glass to another: it moves together with the glass it was initially been put in.
After all shuffling operations Petya shows Vasya that the ball has moved to position t. Vasya's task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position s to position t.
Input
The first line contains three integers: n, s, t (1 ≤ n ≤ 105; 1 ≤ s, t ≤ n) — the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p1, p2, ..., pn (1 ≤ pi ≤ n) — the shuffling operation parameters. It is guaranteed that all pi's are distinct.
Note that s can equal t.
Output
If the marble can move from position s to position t, then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.
Examples
Input
4 2 1
2 3 4 1
Output
3
Input
4 3 3
4 1 3 2
Output
0
Input
4 3 4
1 2 3 4
Output
-1
Input
3 1 3
2 1 3
Output
-1 | instruction | 0 | 74,483 | 19 | 148,966 |
Tags: implementation
Correct Solution:
```
def mi():
return map(int, input().split(' '))
n, s, t = mi()
li = list(mi())
if s==t:
print(0)
exit()
temp = li[s-1]
count = 1
while(temp!= t):
temp = li[temp-1]
#print(temp)
count+=1
if temp == s:
print(-1)
exit()
print(count)
``` | output | 1 | 74,483 | 19 | 148,967 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not.
First Petya puts a marble under the glass in position s. Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position p1, the glass from the second position to position p2 and so on. That is, a glass goes from position i to position pi. Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn't travel from one glass to another: it moves together with the glass it was initially been put in.
After all shuffling operations Petya shows Vasya that the ball has moved to position t. Vasya's task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position s to position t.
Input
The first line contains three integers: n, s, t (1 ≤ n ≤ 105; 1 ≤ s, t ≤ n) — the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p1, p2, ..., pn (1 ≤ pi ≤ n) — the shuffling operation parameters. It is guaranteed that all pi's are distinct.
Note that s can equal t.
Output
If the marble can move from position s to position t, then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.
Examples
Input
4 2 1
2 3 4 1
Output
3
Input
4 3 3
4 1 3 2
Output
0
Input
4 3 4
1 2 3 4
Output
-1
Input
3 1 3
2 1 3
Output
-1
Submitted Solution:
```
n, s, t = (int(x) for x in input().split())
p = [0] + [int(x) for x in input().split()]
val = 0
while s > 0 and s != t:
nxt = p[s]
p[s] = 0
s = nxt
val += 1
print(val if s else -1)
``` | instruction | 0 | 74,484 | 19 | 148,968 |
Yes | output | 1 | 74,484 | 19 | 148,969 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not.
First Petya puts a marble under the glass in position s. Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position p1, the glass from the second position to position p2 and so on. That is, a glass goes from position i to position pi. Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn't travel from one glass to another: it moves together with the glass it was initially been put in.
After all shuffling operations Petya shows Vasya that the ball has moved to position t. Vasya's task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position s to position t.
Input
The first line contains three integers: n, s, t (1 ≤ n ≤ 105; 1 ≤ s, t ≤ n) — the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p1, p2, ..., pn (1 ≤ pi ≤ n) — the shuffling operation parameters. It is guaranteed that all pi's are distinct.
Note that s can equal t.
Output
If the marble can move from position s to position t, then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.
Examples
Input
4 2 1
2 3 4 1
Output
3
Input
4 3 3
4 1 3 2
Output
0
Input
4 3 4
1 2 3 4
Output
-1
Input
3 1 3
2 1 3
Output
-1
Submitted Solution:
```
from collections import defaultdict
n,s,t=map(int,input().split())
a=[int(x) for x in input().split()]
p1=s
d={}
d=defaultdict(lambda:-1,d)
for i in range(len(a)):
d[i+1]=a[i]
count=0
while(p1!=t):
p1=d[p1]
if p1==s:
count=-1
break
count+=1
if count==-1:
print(-1)
else:
print(count)
``` | instruction | 0 | 74,485 | 19 | 148,970 |
Yes | output | 1 | 74,485 | 19 | 148,971 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not.
First Petya puts a marble under the glass in position s. Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position p1, the glass from the second position to position p2 and so on. That is, a glass goes from position i to position pi. Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn't travel from one glass to another: it moves together with the glass it was initially been put in.
After all shuffling operations Petya shows Vasya that the ball has moved to position t. Vasya's task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position s to position t.
Input
The first line contains three integers: n, s, t (1 ≤ n ≤ 105; 1 ≤ s, t ≤ n) — the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p1, p2, ..., pn (1 ≤ pi ≤ n) — the shuffling operation parameters. It is guaranteed that all pi's are distinct.
Note that s can equal t.
Output
If the marble can move from position s to position t, then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.
Examples
Input
4 2 1
2 3 4 1
Output
3
Input
4 3 3
4 1 3 2
Output
0
Input
4 3 4
1 2 3 4
Output
-1
Input
3 1 3
2 1 3
Output
-1
Submitted Solution:
```
n,s,t=map(int,input().split())
x=list(map(int,input().split()))
if(s==t):
print('0')
else:
pos=s
k=0
f=0
for i in range(0,n):
if(pos==t):
f=1
break
pos=x[pos-1]
k=k+1
if(pos>n):
pos=pos%n
if(f==1):
print(k)
else:
print('-1')
``` | instruction | 0 | 74,486 | 19 | 148,972 |
Yes | output | 1 | 74,486 | 19 | 148,973 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not.
First Petya puts a marble under the glass in position s. Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position p1, the glass from the second position to position p2 and so on. That is, a glass goes from position i to position pi. Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn't travel from one glass to another: it moves together with the glass it was initially been put in.
After all shuffling operations Petya shows Vasya that the ball has moved to position t. Vasya's task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position s to position t.
Input
The first line contains three integers: n, s, t (1 ≤ n ≤ 105; 1 ≤ s, t ≤ n) — the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p1, p2, ..., pn (1 ≤ pi ≤ n) — the shuffling operation parameters. It is guaranteed that all pi's are distinct.
Note that s can equal t.
Output
If the marble can move from position s to position t, then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.
Examples
Input
4 2 1
2 3 4 1
Output
3
Input
4 3 3
4 1 3 2
Output
0
Input
4 3 4
1 2 3 4
Output
-1
Input
3 1 3
2 1 3
Output
-1
Submitted Solution:
```
'''
def main():
from sys import stdin,stdout
if __name__=='__main__':
main()
'''
#118B
'''
def main():
from sys import stdin,stdout
n=int(stdin.readline())
for i in range(n+1):
stdout.write(' '*(2*(n-i)))
for k in range(i+1):
if k:
if k==1:
stdout.write(' 1 ')
else:
stdout.write(str(k)+' ')
else:
stdout.write('0')
for k in range(i-1,-1,-1):
if k:
stdout.write(str(k)+' ')
else:
stdout.write('0')
stdout.write('')
for j in range(i-1,-1,-1):
stdout.write(' '*2*(n-j))
for k in range(j+1):
if k:
if k==1:
stdout.write(' 1 ')
else:
stdout.write(str(k)+' ')
else:
stdout.write('0')
for k in range(j-1,-1,-1):
if k:
stdout.write(str(k)+' ')
else:
stdout.write('0')
stdout.write('')
if __name__=='__main__':
main()
'''
#499B
'''
def main():
from sys import stdin,stdout
n,m=map(int,stdin.readline().split())
mydic={}
for _ in range(m):
a,b=map(str,stdin.readline().strip().split())
mydic[a]=b
for k in stdin.readline().strip().split():
if len(mydic[k])<len(k):
stdout.write(mydic[k]+' ')
else:
stdout.write(k+' ')
if __name__=='__main__':
main()
'''
#450A
'''
def main():
from sys import stdin,stdout
n,m=map(int,stdin.readline().split())
k=list(range(1,n+1))
a=list(map(int,stdin.readline().split()))
while k!=[]:
top=k[0]
if a[top-1]<=m:
k=k[1:]
a[top-1]=0
else:
a[top-1]-=m
k=k[1:]+[top]
stdout.write(str(top))
if __name__=='__main__':
main()
'''
#550C
'''
def main():
from sys import stdin,stdout
n=stdin.readline().strip()
i=0
k=2
t=''
for j in n[::-1]:
if j=='0':
t='0'
break
elif j=='8':
tz='8'
break
i+=1
if k==16:
break
if int(j+t) & (k-1):
continue
else:
t=j+t
k<<=1
#print(t,i)
if t!='':
if int(t)%8==0:
if i!=len(n) and t!='0' and t!='8':
stdout.write('YES\n'+n[:len(n)-i+1]+t)
else:
stdout.write('YES\n'+t)
else:
stdout.write('NO')
else:
stdout.write('NO')
if __name__=='__main__':
main()
'''
#476B
'''
def main():
from sys import stdin,stdout
a=stdin.readline().strip()
b=stdin.readline().strip()
apc=0
anc=0
bpc=0
bnc=0
bqc=0
for i in a:
if i =='+':
apc+=1
elif i=='-':
anc+=1
for i in b:
if i=='+':
bpc+=1
elif i=='-':
bnc+=1
else:
bqc+=1
#print(apc,anc,bpc,bnc,bqc)
if apc==bpc and anc==bnc and bqc==0:
p=1
elif bpc>apc or bnc>anc or len(a)!=len(b):
p=0
else:
denom=(1<<bqc)
numer=1
maxim=max(apc-bpc,anc-bnc)
minim=min(aspc-bpc,anc-bnc)
while(bqc>maxim):
#print(numer)
numer*=bqc
bqc-=1
if minim>1:
k=minim
while(minim>=2):
#print('k',k)
k*=(minim-1)
minim-=1
numer/=k
p=numer/denom
stdout.write('{:.12f}'.format(p))
if __name__=='__main__':
main()
'''
'''
def main():
from sys import stdin,stdout
for _ in range(int(stdin.readline())):
l1,l2,l3,n=stdin.readline().strip().split()
L=l1+l2*int(n)+l3
n=int(L,2)
c=0
while n>=0:
n=(n&(n+1)) -1
#print(n)
c+=1
stdout.write(str(c)+'\n')
if __name__=='__main__':
main()
'''
#230B
'''
def main():
from sys import stdin,stdout
s=[]
for i in range(1000000):
s.append(True)
for i in range(2,1000000):
if s[i-1]:
j=2*i
while j<=1000000:
s[j-1]=False
j+=i
#print(s[:10])
t=int(stdin.readline())
for i in stdin.readline().strip().split():
n=int(i)
k=n**0.5
if int(k)==k and s[int(k)-1] and n!=1:
stdout.write('YES\n')
else:
stdout.write('NO\n')
if __name__=='__main__':
main()
'''
#489C
'''
def main():
import sys
a,b=map(int,sys.stdin.readline().split())
if not a:
maxim='-1'
minim='-1'
elif not b:
if a==1:
maxim='0'
minim='0'
else:
maxim='-1'
minim='-1'
else:
#finding the maximum number
maxim=0
n=b
while n:
if 9>n:
maxim=maxim*10+n
n=0
else:
n-=9
maxim=maxim*10+9
s=str(maxim)
diff=a-len(s)
if diff<0:
maxim='-1'
elif diff>0:
maxim=s+'0'*diff
else:
maxim=s
#finding the minimum number
if diff:
if diff<0:
minim='-1'
else:
s=maxim[::-1]
for i in range(len(s)):
if int(s[i]):
break
minim='1'+'0'*(i-1)+str(int(s[i])-1)+s[i+1:]
else:
minim=maxim[::-1]
sys.stdout.write(minim+' '+maxim+'\n')
if __name__=='__main__':
main()
'''
#230B
'''
def main():
import sys
import bisect
C=int(1e6)
l=[]
for i in range(C):
l.append(True)
for i in range(3,C,2):
if l[i-1]:
j=3*i
while j<=C:
l[j-1]=False
j+=2*i
for i in range(3,C,2):
l[i]=False
l[0]=False
s=[]
for i in range(C):
if l[i]:
s.append((i+1)*(i+1))
#print(s[:10])
n=int(sys.stdin.readline())
tup=tuple(map(int,sys.stdin.readline().split()))
for i in tup:
pos=bisect.bisect_left(s,i)
if pos!=len(s) and s[pos]==i:
sys.stdout.write('YES\n')
else:
sys.stdout.write('NO\n')
#print(s[:10])
if __name__=='__main__':
main()
'''
#508B TLE
'''
def main():
import sys
flag=False
minim=10
minindex=-1
maxindex=-1
n=sys.stdin.readline().strip()
for i in range(len(n)):
if (n[i]=='2' or n[i]=='4' or n[i]=='6' or n[i]=='8'or n[i]=='0') and int(n[i])<=minim:
if minindex<0:
minindex=i
minim=int(n[i])
flag=True
else:
maxindex=i
if flag:
s=''
for i in range(len(n)):
if i==minindex:
s+=n[-1]
elif i==len(n)-1:
s+=str(minim)
else:
s+=n[i]
if maxindex>0:
t=''
for i in range(len(n)):
if i==maxindex:
t+=n[-1]
elif i==len(n)-1:
t+=str(minim)
else:
t+=n[i]
s=str(max(int(s),int(t)))
else:
s='-1'
sys.stdout.write(s)
if __name__=='__main__':
main()
'''
#466C
'''
def main():
import sys
n=int(sys.stdin.readline())
t=tuple(map(int,sys.stdin.readline().split()))
acc=sum(t)
if acc % 3:
sys.stdout.write('0')
else:
cnt=[]
const=acc//3
ss=0
for i in t:
if acc-ss==const:
cnt.append(1)
else:
cnt.append(0)
ss+=i
for i in range(n-2,-1,-1):
cnt[i]+=cnt[i+1];
#print(cnt)
ans=0
ss=0
for i in range(n-2):
ss+=t[i]
if ss==const:
ans+=cnt[i+2]
sys.stdout.write(str(ans)+'\n')
if __name__=='__main__':
main()
'''
#285B
def main():
from sys import stdin,stdout
import collections
stack=collections.deque()
n,s,t=map(int,stdin.readline().split())
M=tuple(map(int,stdin.readline().split()))
visited=list(0 for x in range(n))
count=0
stack.appendleft(s-1)
while(len(stack)):
top=stack.popleft()
if top==t-1:
break
else:
if not visited[M[top]-1]:
stack.appendleft(M[top]-1)
visited[M[top]-1]=1
count+=1
if top==t-1:
stdout.write(str(count)+'\n')
else:
stdout.write('-1\n')
if __name__=='__main__':
main()
``` | instruction | 0 | 74,487 | 19 | 148,974 |
Yes | output | 1 | 74,487 | 19 | 148,975 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.