message stringlengths 2 28.7k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 21 109k | cluster float64 7 7 | __index_level_0__ int64 42 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ β¦,\ p_n (0 β€ p_i β€ n) β the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number β the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2. | instruction | 0 | 58,150 | 7 | 116,300 |
Tags: dp, greedy, sortings
Correct Solution:
```
n=int(input())
l=list(map(int,input().split()))
d=set([i for i in range(1,n+1)])
for i in l:
if i!=0:
d.remove(i)
t=[0,0]
for i in d:
t[i%2]+=1
l=[1]+l+[1]
z=-1
oe=[]
e=[]
o=[]
edo=[]
ede=[]
for i in range(1,n+1):
if l[i]==0:
if z==-1:
z=i-1
if i==n:
if l[z]%2==0:
ede.append([z+1,i])
else:
edo.append([z+1,i])
z=-1
elif l[i+1]!=0:
if z==0:
if l[i+1]%2==0:
ede.append([z+1,i])
else:
edo.append([z+1,i])
elif l[z]%2==l[i+1]%2:
if l[z]%2==0:
e.append([z+1,i])
else:
o.append([z+1,i])
else:
oe.append([z+1,i])
z=-1
else:
if l[i+1]==0:
continue
else:
if z==0 or i==n:
if z==0:
if i==n:
ede.append([z+1,i])
elif l[i+1]%2==0:
ede.append([z+1,i])
else:
edo.append([z+1,i])
else:
if l[z]%2==0:
ede.append([z+1,i])
else:
edo.append([z+1,i])
else:
if l[z]%2==l[i+1]%2:
if l[i+1]%2==0:
e.append([z+1,i])
else:
o.append([z+1,i])
else:
oe.append([z+1,i])
z=-1
o.sort(reverse=True,key=lambda a:a[1]-a[0])
e.sort(reverse=True,key=lambda a:a[1]-a[0])
oe.sort(reverse=True,key=lambda a:a[1]-a[0])
ede.sort(reverse=True,key=lambda a:a[1]-a[0])
edo.sort(reverse=True,key=lambda a:a[1]-a[0])
# print(edge)
# print(o)
# print(t)
for i in range(len(o)):
x,y=o.pop()
if t[1]>=abs(y-x+1):
for j in range(x,y+1):
l[j]=1
t[1]-=1
else:
o.append([x,y])
break
for i in range(len(e)):
x,y=e.pop()
if t[0]>=abs(y-x+1):
for j in range(x,y+1):
l[j]=2
t[0]-=1
else:
e.append([x,y])
break
# print(edge)
for i in range(len(ede)):
x,y=ede.pop()
if t[0]>=y-x+1:
for j in range(x,y+1):
l[j]=2
t[0]-=1
else:
ede.append([x,y])
for i in range(len(edo)):
x,y=edo.pop()
if t[1]>=abs(y-x+1):
for j in range(x,y+1):
l[j]=1
t[1]-=1
else:
edo.append([x,y])
break
edge=ede+edo
for i in range(len(edge)):
x,y=edge.pop()
if x==1 and y==n:
f=x
while t[0]:
l[f]=2
t[0]-=1
f+=1
while t[1]:
l[f]=1
t[1]-=1
f+=1
elif x==1:
if l[y+1]%2==0:
while y>=x:
if t[0]:
l[y]=2
t[0]-=1
y-=1
else:
l[y]=1
t[1]-=1
y-=1
else:
while y>=x:
if t[1]:
l[y]=1
t[1]-=1
y-=1
else:
l[y]=2
t[0]-=1
y-=1
else:
# print(00)
if l[x-1]%2==0:
while y>=x:
if t[0]:
l[x]=2
t[0]-=1
x+=1
else:
l[x]=1
t[1]-=1
x+=1
else:
while y>=x:
if t[1]:
l[x]=1
t[1]-=1
x+=1
else:
l[x]=2
t[0]-=1
x+=1
o=o+e+oe
for i in range(len(o)):
x,y=o.pop()
if l[x-1]%2==0:
while y>=x:
if t[0]:
l[x]=2
t[0]-=1
x+=1
else:
l[x]=1
t[1]-=1
x+=1
else:
while y>=x:
if t[1]:
l[x]=1
t[1]-=1
x+=1
else:
l[x]=2
t[0]-=1
x+=1
count=0
for i in range(2,n+1):
if l[i]%2!=l[i-1]%2:
count+=1
print(count)
# print(l)
``` | output | 1 | 58,150 | 7 | 116,301 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ β¦,\ p_n (0 β€ p_i β€ n) β the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number β the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2. | instruction | 0 | 58,151 | 7 | 116,302 |
Tags: dp, greedy, sortings
Correct Solution:
```
# complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity(remainder of the division by 2).
n = int(input())
p = input().split()
allnos = range(1,n+1)
thenos = []
givnos = []
extra = []
comp = 0
no = None
gap = 0
for i in range(len(p)):
if int(p[i]) != 0:
givnos.append(int(p[i]))
if no == None and gap > 0:
extra.append([int(p[i]),int(p[i]),gap])
elif gap > 0:
thenos.append([no,int(p[i]),gap])
else:
if no is not None and int(p[i]) % 2 != no % 2:
comp += 1
no = int(p[i])
gap = 0
else:
gap += 1
if gap > 0:
if no != None:
extra.append([no,no,gap])
else:
if gap == 1:
print("0")
else:
print("1")
exit()
remnos = [item for item in allnos if item not in givnos]
pos = 0
neg = 0
for i in range(len(remnos)):
if remnos[i] % 2 == 0:
pos += 1
else:
neg += 1
thenos.sort(key=lambda x: x[2])
for ano in thenos:
if ano[0] % 2 == ano[1] % 2:
if ano[0] % 2 == 0:
if ano[2] <= pos:
pos -= ano[2]
else:
comp += 2
else:
if ano[2] <= neg:
neg -= ano[2]
else:
comp += 2
else:
comp += 1
for e in extra:
if e[0] % 2 == 0:
if e[2] > pos:
comp += 1
else:
pos -= e[2]
else:
if e[2] > neg:
comp += 1
else:
neg -= e[2]
print(comp)
``` | output | 1 | 58,151 | 7 | 116,303 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ β¦,\ p_n (0 β€ p_i β€ n) β the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number β the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2. | instruction | 0 | 58,152 | 7 | 116,304 |
Tags: dp, greedy, sortings
Correct Solution:
```
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)
def main():
n = int(input())
l = [int(s) for s in input().split()]
e,o = n//2, n-n//2
for i in range(n):
if l[i]!=0:
if l[i]%2==0:
e-=1
else:
o-=1
dp=[[[[-1,-1] for i in range(o+1)] for j in range(e+1)] for k in range(n+1)]
def solve(i,e,o,p):
if dp[i][e][o][p]!=-1:
return dp[i][e][o][p]
if i==0:
if l[i]!=0:
if p==l[i]%2:
dp[i][e][o][p] = 0
return 0
else:
dp[i][e][o][p] = 1000
return 1000
else:
if e==0 and o==0:
dp[i][e][o][p] = 1000
return 1000
elif e==0:
if p==0:
dp[i][e][o][p] = 1000
return 1000
else:
dp[i][e][o][p] = 0
return 0
elif o==0:
if p==1:
dp[i][e][o][p] = 1000
return 1000
else:
dp[i][e][o][p] = 0
return 0
else:
dp[i][e][o][p] = 0
return 0
if l[i]==0:
if p==0:
if e==0:
dp[i][e][o][p] = 1000
return 1000
w1 = solve(i-1,e-1,o,1)
w2 = solve(i-1, e-1, o,0)
if w1+1<w2:
dp[i][e][o][p] = w1+1
return w1+1
else:
dp[i][e][o][p] = w2
return w2
else:
if o==0:
dp[i][e][o][p] = 1000
return 1000
w1 = solve(i-1,e,o-1,0)
w2 = solve(i-1,e,o-1,1)
if w1+1<w2:
dp[i][e][o][p] = w1+1
return w1+1
else:
dp[i][e][o][p] = w2
return w2
else:
if p!=l[i]%2:
dp[i][e][o][p] = 1000
return 1000
else:
if p==0:
w1 = solve(i-1,e,o,1)
w2 = solve(i-1,e, o,0)
if w1+1<w2:
dp[i][e][o][p] = w1+1
return w1+1
else:
dp[i][e][o][p] = w2
return w2
else:
w1 = solve(i-1,e,o,0)
w2 = solve(i-1,e,o,1)
if w1+1<w2:
dp[i][e][o][p] = w1+1
return w1+1
else:
dp[i][e][o][p] = w2
return w2
ans = min(solve(n-1, e,o,0), solve(n-1, e,o,1))
print(ans)
if __name__=="__main__":
main()
``` | output | 1 | 58,152 | 7 | 116,305 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ β¦,\ p_n (0 β€ p_i β€ n) β the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number β the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2. | instruction | 0 | 58,153 | 7 | 116,306 |
Tags: dp, greedy, sortings
Correct Solution:
```
n = int(input())
pr = 0
t = 0
ch = n // 2
nech = n // 2 + n % 2
mass1 = []
mass3 = []
answer = 0
for i in [int(x) for x in input().split()]:
if i == 0:
pr += 1
elif pr > 0:
if i % 2 == 0:
ch -= 1
else:
nech -= 1
if t == 0:
t = i % 2 + 1
mass1.append([pr, t])
else:
if t == i % 2 + 1:
mass3.append([pr, t])
else:
answer += 1
t = i % 2 + 1
pr = 0
else:
if i % 2 == 0:
ch -= 1
else:
nech -= 1
if t != i % 2 + 1 and t != 0:
answer += 1
t = i % 2 + 1
if pr != 0:
mass1.append([pr, t])
mass3.sort()
mass1.sort()
for l in mass3:
if l[1] == 1:
if ch >= l[0]:
ch -= l[0]
else:
nech -= l[0] - ch
answer += 2
else:
if nech >= l[0]:
nech -= l[0]
else:
ch -= l[0] - nech
answer += 2
for l in mass1:
if l[1] == 1:
if ch >= l[0]:
ch -= l[0]
else:
nech -= l[0] - ch
ch = 0
answer += 1
else:
if nech >= l[0]:
nech -= l[0]
else:
ch -= l[0] - nech
nech = 0
answer += 1
print(answer)
``` | output | 1 | 58,153 | 7 | 116,307 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ β¦,\ p_n (0 β€ p_i β€ n) β the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number β the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
Submitted Solution:
```
from collections import deque
from random import shuffle, random
n = int(input())
lamps = list(map(int, input().split()))
def calc(n, lamps):
counts = [0, 0]
counts[0] = n // 2
counts[1] = n // 2
counts[1] += n % 2
lines = []
start_idx = None
diff = 0
for i in range(1, len(lamps)):
if lamps[i] != 0 and lamps[i - 1] != 0 and lamps[i] % 2 != lamps[i - 1] % 2:
diff += 1
for el in lamps:
if el != 0:
counts[el % 2] -= 1
for i, el in enumerate(lamps):
if el == 0:
if start_idx is None:
start_idx = i
else:
if start_idx is not None:
lines.append((i - start_idx, lamps[start_idx - 1] % 2 if start_idx > 0 else -1, lamps[i] % 2))
start_idx = None
if start_idx is not None:
lines.append((len(lamps) - start_idx, lamps[start_idx - 1] % 2 if start_idx > 0 else -1, -1))
lines.sort()
penalty = [[[], []], [[], []]]
for i in range(len(lines)):
length, a, b = lines[i]
if a != -1 and b != -1:
if a == b:
penalty[a][1].append(length)
diff += 2
else:
diff += 1
elif a == -1 and b == -1:
diff += int(n > 1)
else:
res = a if a != -1 else b
diff += 1
penalty[res][0].append(length)
for pos in range(2):
pen = penalty[pos]
x = 0
y = 0
while y < len(pen[1]) and pen[1][y] <= counts[pos]:
if len(pen[0]) - x >= 2 and (pen[0][x] + pen[0][x + 1]) <= pen[1][y]:
diff -= 2
counts[pos] -= (pen[0][x] + pen[0][x + 1])
x += 2
else:
diff -= 2
counts[pos] -= pen[1][y]
y += 1
while x < len(pen[0]) and pen[0][x] <= counts[pos]:
diff -= 1
counts[pos] -= pen[0][x]
x += 1
return diff
print(calc(n, lamps))
``` | instruction | 0 | 58,154 | 7 | 116,308 |
Yes | output | 1 | 58,154 | 7 | 116,309 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ β¦,\ p_n (0 β€ p_i β€ n) β the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number β the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
Submitted Solution:
```
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO, IOBase
from math import inf
def main():
n = int(input())
p = list(map(int,input().split()))
eve = n//2
for i in p:
if i:
eve -= (i%2)^1
dp = [[[inf,inf] for _ in range(eve+1)] for _ in range(n+1)]
dp[0][-1] = [0,0]
for i in range(n):
if p[i]:
r = p[i]%2
for x in range(eve+1):
dp[i+1][x][r] = min(dp[i][x][r^1]+1,dp[i][x][r])
else:
for x in range(eve):
dp[i+1][x][0] = min(dp[i][x+1][1]+1,dp[i][x+1][0])
for x in range(eve+1):
dp[i+1][x][1] = min(dp[i][x][1],dp[i][x][0]+1)
if p[-1]:
print(dp[-1][0][p[-1]%2])
else:
print(min(dp[-1][0]))
#Fast IO Region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == '__main__':
main()
``` | instruction | 0 | 58,155 | 7 | 116,310 |
Yes | output | 1 | 58,155 | 7 | 116,311 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ β¦,\ p_n (0 β€ p_i β€ n) β the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number β the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
Submitted Solution:
```
n = int(input())
ps = list(map(int, input().split()))
used = [False] * (n+1)
all_zeros = True
for p in ps:
used[p] = True
if p > 0:
all_zeros = False
# check all 0 case
if all_zeros:
if n == 1:
print(0)
else:
print(1)
else:
start_i = 0
while ps[start_i] == 0:
start_i += 1
starter = ps[start_i]
end_i = 0
while ps[n-1-end_i] == 0:
end_i += 1
last = ps[n-1-end_i]
end_i = n-1-end_i
holes = [[], [], [], []] # even, odd, edge even, edge odd, other
hole_l = 0
hole_s = start_i
for i in range(start_i, end_i+1):
if ps[i] == 0:
hole_l += 1
else:
if hole_l > 0:
if (ps[i] % 2) == (ps[hole_s] % 2):
holes[ps[hole_s]%2].append([hole_l, hole_s, i])
hole_s = i
hole_l = 0
if start_i > 0:
holes[2 + (ps[start_i] % 2)].append([start_i, -1, start_i])
if end_i < n-1:
holes[2 + (ps[end_i] % 2)].append([n-end_i-1, end_i, n])
unused = [0, 0]
for i in range(1, n+1):
if not used[i]:
unused[i%2] += 1
for i in range(4):
holes[i].sort()
for hole_group in range(4):
if holes[hole_group]:
while unused[hole_group%2] >= holes[hole_group][0][0]:
unused[hole_group%2] -= holes[hole_group][0][0]
for i in range(holes[hole_group][0][1]+1, holes[hole_group][0][2]):
ps[i] = 1000 + hole_group
holes[hole_group].pop(0)
if not holes[hole_group]:
break
for even_part_hole in holes[2]:
for i in range(even_part_hole[1]+1, even_part_hole[2]):
ps[i] = 2000 + 1
for even_part_hole in holes[0]:
for i in range(even_part_hole[1]+1, even_part_hole[2]):
ps[i] = 2000 + 1
oe = ps[0]%2
sol = 0
for p in ps:
if p%2 != oe:
sol += 1
oe = p%2
print(sol)
``` | instruction | 0 | 58,156 | 7 | 116,312 |
Yes | output | 1 | 58,156 | 7 | 116,313 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ β¦,\ p_n (0 β€ p_i β€ n) β the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number β the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
Submitted Solution:
```
n = int(input().strip())
even,odd = n//2,n//2+n%2
ints = list(map(int, input().split()))
prev,previ,count = None,-1,0
complexity = 0
groups = []
for i,x in enumerate(ints):
if x==0:
count+=1
else:
if not prev:
if count:
groups.append((1,-count,x%2))
elif prev%2 == x%2:
if count:
groups.append((2,-count,x%2))
else:
complexity+=1
if x%2:
odd-=1
else:
even-=1
count = 0
prev=x
else:
if count:
if prev:
groups.append((1,-count,prev%2))
else:
if n%2==0:
complexity = 1
if groups:
groups.sort(reverse=True)
for g in groups:
if g[2]:
if odd + g[1] < 0:
complexity+=g[0]
else:
odd+=g[1]
else:
if even + g[1] < 0:
complexity+=g[0]
else:
even+=g[1]
print(complexity)
``` | instruction | 0 | 58,157 | 7 | 116,314 |
Yes | output | 1 | 58,157 | 7 | 116,315 |
Evaluate the correctness of the submitted Python 2 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ β¦,\ p_n (0 β€ p_i β€ n) β the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number β the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
Submitted Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
from fractions import gcd
import heapq
raw_input = stdin.readline
pr = stdout.write
mod=998244353
def ni():
return int(raw_input())
def li():
return list(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 tuple(map(int,stdin.read().split()))
range = xrange # not for python 3.0+
# main code
n=ni()
l=[0]+li()
mx=1005
dp=[[[mx,mx] for i in range(n+1)] for i in range(n+1)]
dp[0][0][0]=0
dp[0][0][1]=0
for i in range(1,n+1):
for j in range(i+1):
if l[i]%2 or not l[i]:
dp[i][j][1]=min(dp[i-1][j][1],dp[i-1][j][0]+1)
if not l[i]%2:
dp[i][j][0]=min(dp[i-1][j-1][0],dp[i-1][j-1][1]+1)
print min(dp[n][n/2])
``` | instruction | 0 | 58,158 | 7 | 116,316 |
Yes | output | 1 | 58,158 | 7 | 116,317 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ β¦,\ p_n (0 β€ p_i β€ n) β the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number β the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 7 19:52:50 2020
@author: Rodro
"""
def garland(a):
n = len(a)
a = [0] + a
oo = n + 1
dp = [[[oo, oo] for __ in range(n + 1)] for _ in range(n + 1)]
dp[0][0][0] = dp[0][0][1] = 0
for i in range(1, n + 1):
for j in range(i + 1):
if a[i]%2 == 1 or a[i] == 0:
dp[i][j][1] = min(dp[i - 1][j][0] + 1, dp[i - 1][j][1])
if a[i]%2 == 0 and j > 0:
dp[i][j][0] = min(dp[i - 1][j - 1][0], dp[i - 1][j - 1][1] + 1)
m = int(n/2)
print(dp)
return min(dp[n][m])
c1 = int(input())
c2 = str(input()).split()
a = []
for i in range(c1):
a.append(int(c2[i]))
print(garland(a))
#print(garland([7, 0, 9, 0, 5, 0, 15, 0, 0, 1, 17, 0, 11, 19, 0, 0, 3, 0, 13, 0]))
#print(garland([1, 0, 3]))
#print(garland([0, 5, 0, 2, 3]))
#print(garland([1, 0, 0, 5, 0, 0, 2]))
#print(garland([0, 0, 0, 0, 0]))
#print(garland([0, 2, 0, 4]))
#print(garland([2, 0, 0, 3]))
``` | instruction | 0 | 58,159 | 7 | 116,318 |
No | output | 1 | 58,159 | 7 | 116,319 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ β¦,\ p_n (0 β€ p_i β€ n) β the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number β the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
Submitted Solution:
```
n = int(input())
bulb = list(map(int,input().split()))
#function return complex
def complex(n,bulb):
temp = 0
for x in range(n-1):
if (bulb[x]%2 == 0) and (bulb[x+1]%2 != 0) and (bulb[x]!=0) and (bulb[x+1]!=0):
temp+=1
elif (bulb[x]%2 != 0) and (bulb[x+1]%2 == 0) and (bulb[x]!=0) and (bulb[x+1]!=0):
temp+=1
return temp;
#compare even or odd
def compareEvenOrOdd(num,even,odd):
if num%2==0:
even-=1
else:
odd-=1
return even,odd;
#calculated complext
def cal_complex(temp1,temp2,dic,complexs,k):
if temp1 - dic >=0:
temp1 = temp1 -dic
else:
temp2=temp2-(dic-temp1)
temp1=0
complexs=complexs+k
return temp1,temp2,complexs
#sorting value of all zero
def sorting(n,bulb):
even= n//2
odd=n-even
dictsort={} # key: position, Value: sum of all zero
i=0
complexs=complex(n,bulb)
print(complexs)
while i<n:
if bulb[i]!=0 and i<n-1:
if bulb[i+1]==0:
dictsort[i]=0
even,odd = compareEvenOrOdd(bulb[i],even,odd);
j=i
i+=1
while i<n and bulb[i]==0:
dictsort[j]+=1
i+=1
else:
even,odd=compareEvenOrOdd(bulb[i],even,odd)
i+=1
elif bulb[i]!=0:
even,odd = compareEvenOrOdd(bulb[i],even,odd);
i+=1
elif bulb[0]==0:
dictsort[0]=1
j=i
i+=1
while i<n and bulb[i]==0:
dictsort[j]+=1
i+=1
#put on tree
dictsort = sorted(dictsort.items(),key=lambda x: x[1])
specialdict=[]
print("odd",odd)
print("even",even)
print(dictsort)
if len(dictsort)>0 and dictsort[0][1]==n and n>1:
print(1)
elif n<=1:
print(0)
else:
for i in range(len(dictsort)):
first=dictsort[i][0]
last=dictsort[i][0]+dictsort[i][1]+1
if bulb[first]!=0 and last<n:
if bulb[first]%2==0 and bulb[last]%2==0:
even,odd,complexs=cal_complex(even,odd,dictsort[i][1],complexs,2)
elif bulb[first]%2!=0 and bulb[last]%2!=0:
odd,even,complexs=cal_complex(odd,even,dictsort[i][1],complexs,2)
else:
complexs=complexs+1
else:
specialdict.append(dictsort[i])
for x in specialdict:
if bulb[x[0]]==0:
if bulb[bulb[x[0]]+x[1]]%2==0:
even,odd,complexs=cal_complex(even,odd,dictsort[i][1],complexs,1)
else:
odd,even,complexs=cal_complex(odd,even,dictsort[i][1],complexs,1)
else:
if bulb[x[0]]%2==0:
even,odd,complexs=cal_complex(even,odd,dictsort[i][1],complexs,1)
else:
odd,even,complexs=cal_complex(odd,even,dictsort[i][1],complexs,1)
print(complexs)
sorting(n,bulb)
``` | instruction | 0 | 58,160 | 7 | 116,320 |
No | output | 1 | 58,160 | 7 | 116,321 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ β¦,\ p_n (0 β€ p_i β€ n) β the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number β the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
Submitted Solution:
```
from math import *
n = int(input())
a = list(map(int,input().split()))
o = e = 0
for i in a:
if(i == 0): continue
if(i&1): o += 1
else: e += 1
o = ceil(n/2) - o
e = n//2 - e
ans = 0
i = 0
while(i < n):
st = i-1
while(i < n and a[i] == 0):
i += 1
end = i
l = end - st - 1
if(l > 0):
if(st == -1 or end == n or a[st]%2 != a[end]%2):
ans += 1
else:
if(a[st]%2 == 0):
if(e >= l):
e -= l
else:
e = 0
ans += 2
else:
if(o >= l):
o -= l
else:
o = 0
ans += 2
#print(st,end,l,ans)
i += 1
print(ans)
``` | instruction | 0 | 58,161 | 7 | 116,322 |
No | output | 1 | 58,161 | 7 | 116,323 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.
<image>
Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 2). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of 1 3 5 7 6 4 2 is 1.
No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of light bulbs on the garland.
The second line contains n integers p_1,\ p_2,\ β¦,\ p_n (0 β€ p_i β€ n) β the number on the i-th bulb, or 0 if it was removed.
Output
Output a single number β the minimum complexity of the garland.
Examples
Input
5
0 5 0 2 3
Output
2
Input
7
1 0 0 5 0 0 2
Output
1
Note
In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5, 4) and (2, 3) are the pairs of adjacent bulbs that have different parity.
In the second case, one of the correct answers is 1 7 3 5 6 4 2.
Submitted Solution:
```
length=int(input())
inputlist=list(map(int,input().split( )))
replicatelist=inputlist.copy()
sortedlist=list(range(1,length+1))
complexity=0
missingvalues=[]
oddodd=0
eveneven=0
oddeven=0
chaineveneven=[]
chainoddodd=[]
chainoddeven=[]
odd=[]
even=[]
# i need a function to find initial complexity
for i in range(len(inputlist)-1):
if inputlist[i]!=0:
if inputlist[i]%2==inputlist[i+1]%2 and inputlist[i+1]!=0:
complexity+=0
elif inputlist[i]%2!=inputlist[i+1]%2 and inputlist[i+1]!=0:
complexity+=1
#
#
for i in range(length):
value=inputlist[i]
if value!=0:
sortedlist[value-1]='k'
for i in range(length):
if sortedlist[i]!='k':
missingvalues.append(i+1)
oddmissing=0
evenmissing=0
for b in missingvalues:
if b%2==0:
evenmissing+=1
else:
oddmissing+=1
#
def chainedzeros(countervariable,inputlist):
length=len(inputlist)
chainedzeros=0
zerobefore=True
while zerobefore and countervariable<=length-1:
if inputlist[countervariable]!=0:
zerobefore=False
else:
chainedzeros+=1
countervariable+=1
if chainedzeros>1:
return [True,countervariable]
else:
return [False,countervariable]
#
def parity(leftvarcheck,rightvarcheck,chainedzeroscheck,leftvalue,rightvalue):
global oddodd
global eveneven
global oddeven
global odd
global even
global chaineveneven
global chainoddodd
global chaineeven
global initialcounter
global countervariable
if (leftvarcheck==True and rightvarcheck==True) and chainedzeroscheck==True:
if leftvalue%2==0 and rightvalue%2==0:
chaineveneven.append(countervariable-initialcounter)
elif leftvalue%2==1 and rightvalue%2==1:
chainoddodd.append(countervariable-initialcounter)
else:
chainoddeven.append(countervariable-initialcounter)
elif (leftvarcheck==False and rightvarcheck==True) and chainedzeroscheck==True:
if rightvalue%2==0:
even.append(countervariable-initialcounter)
else:
odd.append(countervariable-initialcounter)
elif (leftvarcheck==True and rightvarcheck==False) and chainedzeroscheck==True:
if leftvalue%2==0:
even.append(countervariable-initialcounter)
else:
odd.append(countervariable-initialcounter)
elif (leftvarcheck==True and rightvarcheck==True) and chainedzeroscheck==False:
if leftvalue%2==0 and rightvalue%2==0:
eveneven+=1
elif leftvalue%2==1 and rightvalue%2==1:
oddodd+=1
else:
oddeven+=1
elif (leftvarcheck==True and rightvarcheck==False) and chainedzeroscheck==False:
if leftvalue%2==0:
even.append(1)
else:
odd.append(1)
elif (leftvarcheck==False and rightvarcheck==True) and chainedzeroscheck==False:
if rightvalue%2==0:
even.append(1)
else:
odd.append(1)
#
countervariable=0
leftvarcheck=False
leftvalue=None
rightvarcheck=True
while countervariable<=length-1:
alpha=inputlist[countervariable]
if alpha==0:
initialcounter=countervariable
if countervariable>=1:
leftvarcheck=True
leftvalue=inputlist[countervariable-1]
#this block checks if the zeros are chained
chainedinfo=chainedzeros(countervariable,inputlist)
if chainedinfo[0]==True:
countervariable=chainedinfo[1]
if countervariable<=length-1:
rightvalue=inputlist[countervariable]
parity(leftvarcheck,rightvarcheck,chainedinfo[0],leftvalue,rightvalue)
else:
rightvarcheck=False
rightvalue=None
parity(leftvarcheck,rightvarcheck,chainedinfo[0],leftvalue,rightvalue)
elif chainedinfo[0]==False:
countervariable=chainedinfo[1]
if countervariable<=length-1:
rightvalue=inputlist[countervariable]
parity(leftvarcheck,rightvarcheck,chainedinfo[0],leftvalue,rightvalue)
else:
rightvarcheck=False
rightvalue=None
parity(leftvarcheck,rightvarcheck,chainedinfo[0],leftvalue,rightvalue)
else:
countervariable+=1
def insertion(inputlist):
global oddodd
global eveneven
global oddeven
global odd
global even
global chaineveneven
global chainoddodd
global chainoddeven
global evenmissing
global oddmissing
global complexity
chaineveneven.sort()
chaineveneven.reverse()
chainoddodd.sort()
chainoddodd.reverse()
chainoddeven.sort()
chainoddeven.reverse()
even.sort()
even.reverse()
odd.sort()
odd.reverse()
while evenmissing!=0 and eveneven!=0:
evenmissing+=-1
eveneven+=-1
while evenmissing!=0 and (len(chaineveneven)!=0 or len(even)!=0):
if len(even)!=0 and len(chaineveneven)!=0 and chaineveneven[-1]>2*even[-1]:
evenmissing+=-1
even[-1]+=-1
if even[-1]==0:
even.pop()
elif len(chaineveneven)!=0:
evenmissing+=-1
chaineveneven[-1]+=-1
if chaineveneven[-1]==0:
chaineveneven.pop()
elif len(even)!=0:
evenmissing+=-1
even[-1]+=-1
if even[-1]==0:
even.pop()
while oddmissing!=0 and oddodd!=0:
oddmissing+=-1
oddodd+=-1
while oddmissing!=0 and (len(chainoddodd)!=0 or len(odd)!=0):
if len(odd)!=0 and len(chainoddodd)!=0 and chainoddodd[-1]>2*odd[-1]:
oddmissing+=-1
odd[-1]+=-1
if odd[-1]==0:
odd.pop()
elif len(chainoddodd)!=0:
oddmissing+=-1
chainoddodd[-1]+=-1
if chainoddodd[-1]==0:
chainoddodd.pop()
elif len(odd)!=0:
oddmissing+=-1
odd[-1]+=-1
if odd[-1]==0:
odd.pop()
while oddmissing!=0 and len(chainoddeven)!=0:
oddmissing+=-1
chainoddeven[-1]+=-1
if chainoddeven[-1]==0:
chainoddeven.pop()
complexity+=1
while evenmissing!=0 and len(chainoddeven)!=0:
evenmissing+=-1
chainoddeven[-1]+=-1
if chainoddeven[-1]==0:
chainoddeven.pop()
complexity+=1
#same parity,chained parity,single parity,chainedoddeven
complexity+=oddeven+len(chainoddeven)+2*len(chainoddodd)+2*len(chaineveneven)+2*oddodd+2*eveneven+len(even)+len(odd)
return complexity
print(insertion(inputlist))
``` | instruction | 0 | 58,162 | 7 | 116,324 |
No | output | 1 | 58,162 | 7 | 116,325 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a1, a2, ..., an of n integer numbers β saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:
* Each pencil belongs to exactly one box;
* Each non-empty box has at least k pencils in it;
* If pencils i and j belong to the same box, then |ai - aj| β€ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |ai - aj| β€ d and they belong to different boxes.
Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO".
Input
The first line contains three integer numbers n, k and d (1 β€ k β€ n β€ 5Β·105, 0 β€ d β€ 109) β the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 109) β saturation of color of each pencil.
Output
Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO".
Examples
Input
6 3 10
7 2 7 7 4 2
Output
YES
Input
6 2 3
4 5 3 13 4 10
Output
YES
Input
3 2 5
10 16 22
Output
NO
Note
In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.
In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box. | instruction | 0 | 58,634 | 7 | 117,268 |
Tags: binary search, data structures, dp, greedy, two pointers
Correct Solution:
```
import bisect;
def getIntList():
return list(map(int, input().split()));
def getTransIntList(n):
first=getIntList();
m=len(first);
result=[[0]*n for _ in range(m)];
for i in range(m):
result[i][0]=first[i];
for j in range(1, n):
curr=getIntList();
for i in range(m):
result[i][j]=curr[i];
return result;
n, k, d = getIntList()
a= getIntList();
a.sort();
seen=[False]*len(a);
def search(a, lowLim):
sameLim=bisect.bisect_right(a, a[lowLim]+d);
lowLim=max(lowLim, sameLim-2*k);
if seen[lowLim]:
return False;
if len(a)-lowLim<k:
return False;
if a[len(a)-1]-a[lowLim]<=d:
return True;
for i in range(lowLim+k-1, len(a)-1):
if a[i]-a[lowLim]>d:
break;
if search(a, i+1):
return True;
seen[lowLim]=True;
return False;
def searchFull(a):
if a[n-1]-a[n-k]>d:
return False;
return search(a, 0);
if k==1:
print('YES')
elif searchFull(a):
print('YES')
else:
print('NO');
``` | output | 1 | 58,634 | 7 | 117,269 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a1, a2, ..., an of n integer numbers β saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:
* Each pencil belongs to exactly one box;
* Each non-empty box has at least k pencils in it;
* If pencils i and j belong to the same box, then |ai - aj| β€ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |ai - aj| β€ d and they belong to different boxes.
Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO".
Input
The first line contains three integer numbers n, k and d (1 β€ k β€ n β€ 5Β·105, 0 β€ d β€ 109) β the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 109) β saturation of color of each pencil.
Output
Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO".
Examples
Input
6 3 10
7 2 7 7 4 2
Output
YES
Input
6 2 3
4 5 3 13 4 10
Output
YES
Input
3 2 5
10 16 22
Output
NO
Note
In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.
In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box. | instruction | 0 | 58,635 | 7 | 117,270 |
Tags: binary search, data structures, dp, greedy, two pointers
Correct Solution:
```
n, k, d = list(map(int, input().split()))
a = sorted(list(map(int, input().split())))
b = [0] * n
i = j = 0
for i in range(n):
while a[i] - a[j] > d:
j += 1
b[i] = j
c = [0] * n
# d = [0] * n
for i in range(k - 1, n):
# print(f'i={i}, b[i]={b[i]}, i-b[i]+1={i - b[i] + 1}, i-k={i-k}, c[i-k]={c[i-k]}, c[b[i]]={c[b[i]]}')
# print(i - k, b[i] - 2)
if i - b[i] + 1 >= k and (b[i] == 0 or c[i - k] > c[b[i] - 2] or (b[i] == 1 and c[i-k]> c[0])):
c[i] = c[i - 1] + 1
# d[i] = 1
else:
c[i] = c[i - 1]
# print(a)
# print(b)
# print(d)
# print(c)
print('YES' if n < 2 or c[n - 1] > c[n - 2] else 'NO')
``` | output | 1 | 58,635 | 7 | 117,271 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a1, a2, ..., an of n integer numbers β saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:
* Each pencil belongs to exactly one box;
* Each non-empty box has at least k pencils in it;
* If pencils i and j belong to the same box, then |ai - aj| β€ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |ai - aj| β€ d and they belong to different boxes.
Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO".
Input
The first line contains three integer numbers n, k and d (1 β€ k β€ n β€ 5Β·105, 0 β€ d β€ 109) β the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 109) β saturation of color of each pencil.
Output
Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO".
Examples
Input
6 3 10
7 2 7 7 4 2
Output
YES
Input
6 2 3
4 5 3 13 4 10
Output
YES
Input
3 2 5
10 16 22
Output
NO
Note
In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.
In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box. | instruction | 0 | 58,636 | 7 | 117,272 |
Tags: binary search, data structures, dp, greedy, two pointers
Correct Solution:
```
from bisect import bisect_right, bisect_left
def main():
n, k, d = map(int, input().split())
sat = [int(x) for x in input().split()]
sat = sorted(sat)
arrange = [False for _ in range(n + 1)]
arrange[0] = True
s = 0
# i means the first i items could be arranged well
i = min(bisect_right(sat, sat[s] + d), k)
n_arrange = 1 if i - k >= 0 else 0 # arrange[0] == True | the first 0 items
while i <= n:
if i - s >= k and n_arrange > 0:
arrange[i] = True
if i < n:
news = bisect_left(sat, sat[i] - d, s, i)
while s < news:
if s <= i - k and arrange[s]:
n_arrange -= 1
s += 1
i += 1
n_arrange += 1 if (i - k >= s and arrange[i - k]) else 0
if arrange[n]:
print('YES')
else:
print('NO')
if __name__ == '__main__':
main()
``` | output | 1 | 58,636 | 7 | 117,273 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a1, a2, ..., an of n integer numbers β saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:
* Each pencil belongs to exactly one box;
* Each non-empty box has at least k pencils in it;
* If pencils i and j belong to the same box, then |ai - aj| β€ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |ai - aj| β€ d and they belong to different boxes.
Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO".
Input
The first line contains three integer numbers n, k and d (1 β€ k β€ n β€ 5Β·105, 0 β€ d β€ 109) β the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 109) β saturation of color of each pencil.
Output
Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO".
Examples
Input
6 3 10
7 2 7 7 4 2
Output
YES
Input
6 2 3
4 5 3 13 4 10
Output
YES
Input
3 2 5
10 16 22
Output
NO
Note
In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.
In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box. | instruction | 0 | 58,637 | 7 | 117,274 |
Tags: binary search, data structures, dp, greedy, two pointers
Correct Solution:
```
import heapq
def run():
n, k, d = map(int, input().split())
a = sorted(map(int, input().split()))
max_size = [None] * n # how many pencils can be in box starting with this one
start = 0
end = 0
while start < n:
while end < n-1 and a[end+1] - a[start] <= d:
end += 1
max_size[start] = end - start + 1
start += 1
possilbe_starts = []
# heap with starts and stops of intervals where new box can start
# - all pencils before that are successfully boxed
heapq.heappush(possilbe_starts, (0, "start"))
heapq.heappush(possilbe_starts, (1, "stop"))
number_of_opened = 0 # number of opened intervals
for pencil in range(n):
while possilbe_starts and possilbe_starts[0][0] <= pencil:
top = heapq.heappop(possilbe_starts)
number_of_opened += (1 if top[1] == "start" else -1)
if number_of_opened <= 0:
continue
if max_size[pencil] < k:
continue
if pencil + max_size[pencil] + 1 > n:
print("YES")
break
heapq.heappush(possilbe_starts, (pencil + k, "start"))
heapq.heappush(possilbe_starts, (pencil + max_size[pencil] + 1, "stop"))
else:
print("NO")
run()
``` | output | 1 | 58,637 | 7 | 117,275 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a1, a2, ..., an of n integer numbers β saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:
* Each pencil belongs to exactly one box;
* Each non-empty box has at least k pencils in it;
* If pencils i and j belong to the same box, then |ai - aj| β€ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |ai - aj| β€ d and they belong to different boxes.
Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO".
Input
The first line contains three integer numbers n, k and d (1 β€ k β€ n β€ 5Β·105, 0 β€ d β€ 109) β the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 109) β saturation of color of each pencil.
Output
Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO".
Examples
Input
6 3 10
7 2 7 7 4 2
Output
YES
Input
6 2 3
4 5 3 13 4 10
Output
YES
Input
3 2 5
10 16 22
Output
NO
Note
In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.
In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box. | instruction | 0 | 58,638 | 7 | 117,276 |
Tags: binary search, data structures, dp, greedy, two pointers
Correct Solution:
```
#!/usr/bin/env python3
from bisect import bisect
from sys import exit
[n, k, d] = map(int, input().strip().split())
ais = list(map(int, input().strip().split()))
if k == 1:
print ('YES')
exit()
ais.sort()
# can do ais[i:]
cando = [False for _ in range(n)]
j = n - 1 # j is such that a[j] > a[i] + d >= a[j - 1] (upper_bound) a[:j] <= a[i] + d < a[j:]
count = 0 # sum(cando[i + k:j + 1])
for i in reversed(range(n)):
if i + k < n and cando[i + k]:
count += 1
if n - i < k:
continue
if ais[-1] - ais[i] <= d:
cando[i] = True
continue
while ais[j - 1] > ais[i] + d:
if cando[j]:
count -= 1
j -= 1
cando[i] = (count > 0)
if cando[0]:
print ('YES')
else:
print ('NO')
``` | output | 1 | 58,638 | 7 | 117,277 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a1, a2, ..., an of n integer numbers β saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:
* Each pencil belongs to exactly one box;
* Each non-empty box has at least k pencils in it;
* If pencils i and j belong to the same box, then |ai - aj| β€ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |ai - aj| β€ d and they belong to different boxes.
Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO".
Input
The first line contains three integer numbers n, k and d (1 β€ k β€ n β€ 5Β·105, 0 β€ d β€ 109) β the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 109) β saturation of color of each pencil.
Output
Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO".
Examples
Input
6 3 10
7 2 7 7 4 2
Output
YES
Input
6 2 3
4 5 3 13 4 10
Output
YES
Input
3 2 5
10 16 22
Output
NO
Note
In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.
In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box. | instruction | 0 | 58,639 | 7 | 117,278 |
Tags: binary search, data structures, dp, greedy, two pointers
Correct Solution:
```
def getIntList():
return list(map(int, input().split()));
def getTransIntList(n):
first=getIntList();
m=len(first);
result=[[0]*n for _ in range(m)];
for i in range(m):
result[i][0]=first[i];
for j in range(1, n):
curr=getIntList();
for i in range(m):
result[i][j]=curr[i];
return result;
n, k, d = getIntList()
a= getIntList();
a.sort();
#ΠΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΡΠ°Π·ΡΠ΅Π·Π°ΡΡ ΠΎΡΡΠ΅Π·ΠΎΠΊ Π΄ΠΎ j Π½Π΅ Π²ΠΊΠ»ΡΡΠΈΡΠ΅Π»ΡΠ½ΠΎ - cuttable[j];
cuttable=[False]*len(a);
cuttable[0]=True;
def search(a):
curr=0;
upLim=0;
upLimPrev=0;
while True:
#print(curr);
if cuttable[curr]:
#ΠΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ ΠΎΠ΄ΡΠ΅Π·ΠΎΠΊ Π΄Π»ΠΈΠ½Ρ ΠΊΠ°ΠΊ ΠΌΠΈΠ½ΠΈΠΌΡΠΌ k: [curr, curr+k)
lowLim=curr+k;
upLimPrev = upLim;
#ΠΠ°Ρ
ΠΎΠ΄ΠΈΠΌ ΠΏΠ΅ΡΠ²ΡΠΉ ΡΠ°ΠΊΠΎΠΉ ΠΈΠ½Π΄Π΅ΠΊΡ upLim, ΡΡΠΎ a[upLim]-a[curr]>d;
while upLim<len(a) and a[upLim]-a[curr]<=d:
upLim+=1;
#ΠΡΠ»ΠΈ ΡΠ°ΠΊΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° Π½Π΅Ρ, ΡΠ΅ΡΠ΅Π½ΠΈΠ΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ
if upLim==len(a):
return True;
#Π‘ΡΠ°Π²ΠΈΡΡ Π΅Π΄ΠΈΠ½ΠΈΡΠΊΠΈ Π½Π° ΡΠΆΠ΅ ΠΏΡΠΎΠΉΠ΄Π΅Π½ΡΠΉ ΠΎΡΡΠ΅Π·ΠΎΠΊ Π±Π΅ΡΡΠΌΡΡΠ»Π΅Π½Π½ΠΎ
lowLim=max(lowLim, upLimPrev+1);
#print('cuttable', lowLim, upLim);
#a[upLim-1]-a[curr]<=d, Π·Π½Π°ΡΠΈΡ cuttable[upLim]=True;
for i in range(lowLim, upLim+1):
cuttable[i]=True;
curr+=1;
if curr>len(a)-k:
break;
return False;
if k==1:
print('YES')
elif search(a):
print('YES')
else:
print('NO');
``` | output | 1 | 58,639 | 7 | 117,279 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a1, a2, ..., an of n integer numbers β saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:
* Each pencil belongs to exactly one box;
* Each non-empty box has at least k pencils in it;
* If pencils i and j belong to the same box, then |ai - aj| β€ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |ai - aj| β€ d and they belong to different boxes.
Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO".
Input
The first line contains three integer numbers n, k and d (1 β€ k β€ n β€ 5Β·105, 0 β€ d β€ 109) β the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 109) β saturation of color of each pencil.
Output
Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO".
Examples
Input
6 3 10
7 2 7 7 4 2
Output
YES
Input
6 2 3
4 5 3 13 4 10
Output
YES
Input
3 2 5
10 16 22
Output
NO
Note
In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.
In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box. | instruction | 0 | 58,640 | 7 | 117,280 |
Tags: binary search, data structures, dp, greedy, two pointers
Correct Solution:
```
from bisect import bisect
def main():
n, k, d = map(int, input().split())
if k == 1:
print('YES')
return
l = []
ll = [l]
a = 10 ** 10
for b in sorted(map(int, input().split())):
if a < b:
if len(l) < k:
print('NO')
return
l = [b]
ll.append(l)
else:
l.append(b)
a = b + d
def f(a):
def dfs(i):
avail[i] = False
if i + k <= le:
if a[-1] <= a[i] + d:
raise TabError
j = bisect(a, a[i] + d, i, -1) - 1
for j in range(bisect(a, a[i] + d, i, -1), i + k - 1, -1):
if avail[j]:
dfs(j)
le = len(a)
avail = [True] * le
try:
dfs(0)
except TabError:
return True
return False
print(('NO', 'YES')[all(map(f, ll))])
if __name__ == '__main__':
main()
``` | output | 1 | 58,640 | 7 | 117,281 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a1, a2, ..., an of n integer numbers β saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:
* Each pencil belongs to exactly one box;
* Each non-empty box has at least k pencils in it;
* If pencils i and j belong to the same box, then |ai - aj| β€ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |ai - aj| β€ d and they belong to different boxes.
Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO".
Input
The first line contains three integer numbers n, k and d (1 β€ k β€ n β€ 5Β·105, 0 β€ d β€ 109) β the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 109) β saturation of color of each pencil.
Output
Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO".
Examples
Input
6 3 10
7 2 7 7 4 2
Output
YES
Input
6 2 3
4 5 3 13 4 10
Output
YES
Input
3 2 5
10 16 22
Output
NO
Note
In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.
In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box. | instruction | 0 | 58,641 | 7 | 117,282 |
Tags: binary search, data structures, dp, greedy, two pointers
Correct Solution:
```
import bisect
import sys
input = sys.stdin.readline
class SegmentTree():
def __init__(self, n, op, e):
self.n = n
self.op = op
self.e = e
self.size = 2 ** ((n - 1).bit_length())
self.node = [self.e] * (2 * self.size)
def built(self, array):
for i in range(self.n):
self.node[self.size + i] = array[i]
for i in range(self.size - 1, 0, -1):
self.node[i] = self.op(self.node[i << 1], self.node[(i << 1) + 1])
def update(self, i, val):
i += self.size
self.node[i] = val
while i > 1:
i >>= 1
self.node[i] = self.op(self.node[i << 1], self.node[(i << 1) + 1])
def get_val(self, l, r):
l, r = l + self.size, r + self.size
res_l, res_r = self.e, self.e
while l < r:
if l & 1:
res_l = self.op(res_l, self.node[l])
l += 1
if r & 1:
r -= 1
res_r = self.op(self.node[r], res_r)
l, r = l >> 1, r >> 1
return self.op(res_l, res_r)
n, k, d = map(int, input().split())
a = list(map(int, input().split()))
a = sorted(a)
st = SegmentTree(n + 1, lambda a, b : a | b, 0)
st.update(0, 1)
for i in range(n):
tmp = bisect.bisect_left(a, a[i] - d)
if tmp >= max(i + 2 - k, 0):
continue
st.update(i + 1, st.get_val(tmp, max(i + 2 - k, 0)))
if st.get_val(n, n + 1) == 1:
print("YES")
else:
print("NO")
``` | output | 1 | 58,641 | 7 | 117,283 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a1, a2, ..., an of n integer numbers β saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:
* Each pencil belongs to exactly one box;
* Each non-empty box has at least k pencils in it;
* If pencils i and j belong to the same box, then |ai - aj| β€ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |ai - aj| β€ d and they belong to different boxes.
Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO".
Input
The first line contains three integer numbers n, k and d (1 β€ k β€ n β€ 5Β·105, 0 β€ d β€ 109) β the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 109) β saturation of color of each pencil.
Output
Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO".
Examples
Input
6 3 10
7 2 7 7 4 2
Output
YES
Input
6 2 3
4 5 3 13 4 10
Output
YES
Input
3 2 5
10 16 22
Output
NO
Note
In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.
In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box.
Submitted Solution:
```
import sys
n, k, d = map(int, input().split())
a = [0] + sorted(map(int, input().split()))
dp = [1] + [0]*n
j = k
for i in range(n):
if dp[i] == 0:
continue
j = max(j, i+k)
while j <= n and a[j] - a[i+1] <= d:
dp[j] |= dp[i]
j += 1
print('YES' if dp[-1] else 'NO')
``` | instruction | 0 | 58,642 | 7 | 117,284 |
Yes | output | 1 | 58,642 | 7 | 117,285 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a1, a2, ..., an of n integer numbers β saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:
* Each pencil belongs to exactly one box;
* Each non-empty box has at least k pencils in it;
* If pencils i and j belong to the same box, then |ai - aj| β€ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |ai - aj| β€ d and they belong to different boxes.
Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO".
Input
The first line contains three integer numbers n, k and d (1 β€ k β€ n β€ 5Β·105, 0 β€ d β€ 109) β the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 109) β saturation of color of each pencil.
Output
Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO".
Examples
Input
6 3 10
7 2 7 7 4 2
Output
YES
Input
6 2 3
4 5 3 13 4 10
Output
YES
Input
3 2 5
10 16 22
Output
NO
Note
In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.
In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box.
Submitted Solution:
```
from bisect import bisect_right, bisect_left
def main():
n, k, d = map(int, input().split())
sat = [int(x) for x in input().split()]
sat = sorted(sat)
# arrange[i] means the first i items could be arranged well
# arranged well means they could be put into boxes without conflicts
arrange = [True] + [False for _ in range(n)]
s = 0
# we can put [0, i) items into the first box
i = min(bisect_right(sat, sat[s] + d), k)
# keep the number of True-value in arrange[s, i-k]
# there must be at least one point in [s, i-k(included)] which satisfies arrange[this_point] == True
# that means we can cut from this_point and put [this_point, i) in a new box
# then the result is arrange[i] become True, because now all items before i-index could be arranged well
n_arrange = 1 if s <= i - k else 0
while i <= n:
if i - s >= k and n_arrange > 0:
arrange[i] = True
if i < n:
# find new s that we could put | item at new s - item at i | <= d
news = bisect_left(sat, sat[i] - d, s, i)
while s < news:
if s <= i - k and arrange[s]:
n_arrange -= 1
s += 1
i += 1
# every time when you count arrange[x] you should keep x between [s, i-k]
n_arrange += 1 if (s <= i - k and arrange[i - k]) else 0
if arrange[n]:
print('YES')
else:
print('NO')
if __name__ == '__main__':
main()
``` | instruction | 0 | 58,643 | 7 | 117,286 |
Yes | output | 1 | 58,643 | 7 | 117,287 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a1, a2, ..., an of n integer numbers β saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:
* Each pencil belongs to exactly one box;
* Each non-empty box has at least k pencils in it;
* If pencils i and j belong to the same box, then |ai - aj| β€ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |ai - aj| β€ d and they belong to different boxes.
Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO".
Input
The first line contains three integer numbers n, k and d (1 β€ k β€ n β€ 5Β·105, 0 β€ d β€ 109) β the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 109) β saturation of color of each pencil.
Output
Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO".
Examples
Input
6 3 10
7 2 7 7 4 2
Output
YES
Input
6 2 3
4 5 3 13 4 10
Output
YES
Input
3 2 5
10 16 22
Output
NO
Note
In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.
In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box.
Submitted Solution:
```
def main():
import sys
from bisect import bisect_left
input = sys.stdin.readline
N, K, D = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
part = [0] * (N+2)
part[0] = 1
part[1] = -1
for i, a in enumerate(A):
part[i] += part[i-1]
if part[i]:
j = bisect_left(A, a+D+1)
if j >= i+K:
part[i+K] += 1
part[j+1] -= 1
part[N] += part[N-1]
if part[N]:
print('YES')
else:
print('NO')
#print(A)
#print(part)
if __name__ == '__main__':
main()
``` | instruction | 0 | 58,644 | 7 | 117,288 |
Yes | output | 1 | 58,644 | 7 | 117,289 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a1, a2, ..., an of n integer numbers β saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:
* Each pencil belongs to exactly one box;
* Each non-empty box has at least k pencils in it;
* If pencils i and j belong to the same box, then |ai - aj| β€ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |ai - aj| β€ d and they belong to different boxes.
Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO".
Input
The first line contains three integer numbers n, k and d (1 β€ k β€ n β€ 5Β·105, 0 β€ d β€ 109) β the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 109) β saturation of color of each pencil.
Output
Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO".
Examples
Input
6 3 10
7 2 7 7 4 2
Output
YES
Input
6 2 3
4 5 3 13 4 10
Output
YES
Input
3 2 5
10 16 22
Output
NO
Note
In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.
In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box.
Submitted Solution:
```
import bisect;
def getIntList():
return list(map(int, input().split()));
def getTransIntList(n):
first=getIntList();
m=len(first);
result=[[0]*n for _ in range(m)];
for i in range(m):
result[i][0]=first[i];
for j in range(1, n):
curr=getIntList();
for i in range(m):
result[i][j]=curr[i];
return result;
n, k, d = getIntList()
a= getIntList();
a.sort();
#ΠΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΡΠ°Π·ΡΠ΅Π·Π°ΡΡ ΠΎΡΡΠ΅Π·ΠΎΠΊ Π΄ΠΎ j Π½Π΅ Π²ΠΊΠ»ΡΡΠΈΡΠ΅Π»ΡΠ½ΠΎ - cuttable[j];
cuttable=[False]*len(a);
cuttable[0]=True;
def search(a):
curr=0;
maxOne=0;
while True:
#print(curr);
if cuttable[curr]:
#ΠΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ ΠΎΠ΄ΡΠ΅Π·ΠΎΠΊ Π΄Π»ΠΈΠ½Ρ ΠΊΠ°ΠΊ ΠΌΠΈΠ½ΠΈΠΌΡΠΌ k: [curr, curr+k)
lowLim=curr+k;
#ΠΠ°Ρ
ΠΎΠ΄ΠΈΠΌ ΠΏΠ΅ΡΠ²ΡΠΉ ΡΠ°ΠΊΠΎΠΉ ΠΈΠ½Π΄Π΅ΠΊΡ upLim, ΡΡΠΎ a[upLim]-a[curr]>d;
upLim=bisect.bisect_right(a, a[curr]+d);
#ΠΡΠ»ΠΈ ΡΠ°ΠΊΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° Π½Π΅Ρ, ΡΠ΅ΡΠ΅Π½ΠΈΠ΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ
if upLim==len(a):
return True;
#Π‘ΡΠ°Π²ΠΈΡΡ Π΅Π΄ΠΈΠ½ΠΈΡΠΊΠΈ Π½Π° ΡΠΆΠ΅ ΠΏΡΠΎΠΉΠ΄Π΅Π½ΡΠΉ ΠΎΡΡΠ΅Π·ΠΎΠΊ Π±Π΅ΡΡΠΌΡΡΠ»Π΅Π½Π½ΠΎ
lowLim=max(lowLim, maxOne+1);
#print('cuttable', lowLim, upLim);
#a[upLim-1]-a[curr]<=d, Π·Π½Π°ΡΠΈΡ cuttable[upLim]=True;
for i in range(lowLim, upLim+1):
cuttable[i]=True;
maxOne=upLim;
curr+=1;
if curr>len(a)-k:
break;
return False;
if k==1:
print('YES')
elif search(a):
print('YES')
else:
print('NO');
``` | instruction | 0 | 58,645 | 7 | 117,290 |
Yes | output | 1 | 58,645 | 7 | 117,291 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a1, a2, ..., an of n integer numbers β saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:
* Each pencil belongs to exactly one box;
* Each non-empty box has at least k pencils in it;
* If pencils i and j belong to the same box, then |ai - aj| β€ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |ai - aj| β€ d and they belong to different boxes.
Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO".
Input
The first line contains three integer numbers n, k and d (1 β€ k β€ n β€ 5Β·105, 0 β€ d β€ 109) β the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 109) β saturation of color of each pencil.
Output
Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO".
Examples
Input
6 3 10
7 2 7 7 4 2
Output
YES
Input
6 2 3
4 5 3 13 4 10
Output
YES
Input
3 2 5
10 16 22
Output
NO
Note
In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.
In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box.
Submitted Solution:
```
n, k, d = list(map(int, input().split()))
a = sorted(list(map(int, input().split())))
dp = [0] * n
last = 1000000000
for i in range(k - 1, n):
# print(f'i={i}')
if i >= 1 and a[i] == a[i-1] and dp[i-1]:
dp[i] = 1
continue
for j in reversed(range(min(i - k + 2, last + 3))):
if a[i] - a[j] > d:
# print(f'j={j}')
break
if j == 0 or dp[j - 1]:
# print(f'j={j}, ok')
dp[i] = 1
last = i
break
# print(a)
# print(dp)
print('YES' if dp[n - 1] else 'NO')
``` | instruction | 0 | 58,646 | 7 | 117,292 |
No | output | 1 | 58,646 | 7 | 117,293 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a1, a2, ..., an of n integer numbers β saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:
* Each pencil belongs to exactly one box;
* Each non-empty box has at least k pencils in it;
* If pencils i and j belong to the same box, then |ai - aj| β€ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |ai - aj| β€ d and they belong to different boxes.
Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO".
Input
The first line contains three integer numbers n, k and d (1 β€ k β€ n β€ 5Β·105, 0 β€ d β€ 109) β the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 109) β saturation of color of each pencil.
Output
Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO".
Examples
Input
6 3 10
7 2 7 7 4 2
Output
YES
Input
6 2 3
4 5 3 13 4 10
Output
YES
Input
3 2 5
10 16 22
Output
NO
Note
In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.
In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box.
Submitted Solution:
```
import bisect;
def getIntList():
return list(map(int, input().split()));
def getTransIntList(n):
first=getIntList();
m=len(first);
result=[[0]*n for _ in range(m)];
for i in range(m):
result[i][0]=first[i];
for j in range(1, n):
curr=getIntList();
for i in range(m):
result[i][j]=curr[i];
return result;
n, k, d = getIntList()
a= getIntList();
a.sort();
#ΠΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΡΠ°Π·ΡΠ΅Π·Π°ΡΡ ΠΎΡΡΠ΅Π·ΠΎΠΊ Π΄ΠΎ j Π½Π΅ Π²ΠΊΠ»ΡΡΠΈΡΠ΅Π»ΡΠ½ΠΎ - cuttable[j];
cuttable=[False]*len(a);
cuttable[0]=True;
def search(a):
curr=0;
while True:
#print(curr);
if cuttable[curr]:
#ΠΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ ΠΎΠ΄ΡΠ΅Π·ΠΎΠΊ Π΄Π»ΠΈΠ½Ρ ΠΊΠ°ΠΊ ΠΌΠΈΠ½ΠΈΠΌΡΠΌ k: [curr, curr+k)
lowLim=curr+k;
#ΠΠ°Ρ
ΠΎΠ΄ΠΈΠΌ ΠΏΠ΅ΡΠ²ΡΠΉ ΡΠ°ΠΊΠΎΠΉ ΠΈΠ½Π΄Π΅ΠΊΡ upLim, ΡΡΠΎ a[upLim]-a[curr]>d;
upLim=bisect.bisect_right(a, a[curr]+d);
#ΠΡΠ»ΠΈ ΡΠ°ΠΊΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° Π½Π΅Ρ, ΡΠ΅ΡΠ΅Π½ΠΈΠ΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ
if upLim==len(a):
return True;
#ΠΠ°ΠΌ Π½Π΅ ΠΎΠ±ΡΠ·Π°ΡΠ΅Π»ΡΠ½ΠΎ ΡΠ°ΡΡΠΌΠ°ΡΡΠΈΠ²Π°ΡΡ ΠΈΠ½Π΄Π΅ΠΊΡΡ, ΠΌΠ΅Π½ΡΡΠΈΠ΅ Π»ΠΈΠ±ΠΎ ΡΠ°Π²Π½ΡΠ΅ upLim-k - Π½ΠΈΡΠ΅Π³ΠΎ ΠΏΠΎΠ»Π΅Π·Π½ΠΎΠ³ΠΎ ΠΎΠ½ΠΈ Π½Π°ΠΌ Π½Π΅ Π΄Π°Π΄ΡΡ
#ΠΠ΅ Π·Π°Π±ΡΠ²Π°Π΅ΠΌ, ΡΡΠΎ curr Π²ΡΡΠ°ΡΡΠ΅Ρ Π½Π° Π΅Π΄ΠΈΠ½ΠΈΡΡ
curr= max(upLim-k, curr);
#Π‘ΡΠ°Π²ΠΈΡΡ Π΅Π΄ΠΈΠ½ΠΈΡΠΊΠΈ Π½Π° ΡΠΆΠ΅ ΠΏΡΠΎΠΉΠ΄Π΅Π½ΡΠΉ ΠΎΡΡΠ΅Π·ΠΎΠΊ Π±Π΅ΡΡΠΌΡΡΠ»Π΅Π½Π½ΠΎ
lowLim=max(lowLim, curr+1);
#print('cuttable', lowLim, upLim);
#a[upLim-1]-a[curr]<=d, Π·Π½Π°ΡΠΈΡ cuttable[upLim]=True;
for i in range(lowLim, upLim+1):
cuttable[i]=True;
curr+=1;
if curr>len(a)-k:
break;
return False;
if k==1:
print('YES')
elif search(a):
print('YES')
else:
print('NO');
``` | instruction | 0 | 58,647 | 7 | 117,294 |
No | output | 1 | 58,647 | 7 | 117,295 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a1, a2, ..., an of n integer numbers β saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:
* Each pencil belongs to exactly one box;
* Each non-empty box has at least k pencils in it;
* If pencils i and j belong to the same box, then |ai - aj| β€ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |ai - aj| β€ d and they belong to different boxes.
Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO".
Input
The first line contains three integer numbers n, k and d (1 β€ k β€ n β€ 5Β·105, 0 β€ d β€ 109) β the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 109) β saturation of color of each pencil.
Output
Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO".
Examples
Input
6 3 10
7 2 7 7 4 2
Output
YES
Input
6 2 3
4 5 3 13 4 10
Output
YES
Input
3 2 5
10 16 22
Output
NO
Note
In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.
In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box.
Submitted Solution:
```
from bisect import bisect_right, bisect_left
def main():
n, k, d = map(int, input().split())
sat = [int(x) for x in input().split()]
sat = sorted(sat)
arrange = [False for _ in range(n + 1)]
arrange[0] = True
s = 0
i = bisect_right(sat, sat[s] + d)
n_arrange = arrange[s: i - k].count(True)
while i <= n:
if i - s >= k and n_arrange > 0:
arrange[i] = True
if i < n:
s = bisect_left(sat, sat[i] - d, s, i)
i += 1
n_arrange = arrange[s: i - k].count(True)
if arrange[n]:
print('YES')
else:
print('NO')
if __name__ == '__main__':
main()
``` | instruction | 0 | 58,648 | 7 | 117,296 |
No | output | 1 | 58,648 | 7 | 117,297 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a1, a2, ..., an of n integer numbers β saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:
* Each pencil belongs to exactly one box;
* Each non-empty box has at least k pencils in it;
* If pencils i and j belong to the same box, then |ai - aj| β€ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |ai - aj| β€ d and they belong to different boxes.
Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO".
Input
The first line contains three integer numbers n, k and d (1 β€ k β€ n β€ 5Β·105, 0 β€ d β€ 109) β the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 109) β saturation of color of each pencil.
Output
Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO".
Examples
Input
6 3 10
7 2 7 7 4 2
Output
YES
Input
6 2 3
4 5 3 13 4 10
Output
YES
Input
3 2 5
10 16 22
Output
NO
Note
In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.
In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box.
Submitted Solution:
```
from itertools import takewhile
n, k, d = list(map(int, input().split()))
a = sorted(list(map(int, input().split())))
dp = [0] * n
for i in range(k, n):
for j in reversed(range(i - k + 2)):
if a[i] - a[j] > d:
break
if j == 0 or dp[j - 1]:
dp[i] = 1
break
print('YES' if dp[n - 1] else 'NO')
``` | instruction | 0 | 58,649 | 7 | 117,298 |
No | output | 1 | 58,649 | 7 | 117,299 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | instruction | 0 | 59,127 | 7 | 118,254 |
Tags: brute force, dp, greedy
Correct Solution:
```
import bisect
import decimal
from decimal import Decimal
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
# sys.setrecursionlimit(200000)
# mod = 10**9+7
# mod = 998244353
decimal.getcontext().prec = 46
def primeFactors(n):
prime = set()
while n % 2 == 0:
prime.add(2)
n = n//2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
prime.add(i)
n = n//i
if n > 2:
prime.add(n)
return list(prime)
def getFactors(n) :
factors = []
i = 1
while i <= math.sqrt(n):
if (n % i == 0) :
if (n // i == i) :
factors.append(i)
else :
factors.append(i)
factors.append(n//i)
i = i + 1
return factors
def SieveOfEratosthenes(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
num = []
for p in range(2, n+1):
if prime[p]:
num.append(p)
return num
def lcm(a,b):
return (a*b)//math.gcd(a,b)
def sort_dict(key_value):
return sorted(key_value.items(), key = lambda kv:(kv[1], kv[0]))
def list_input():
return list(map(int,input().split()))
def num_input():
return map(int,input().split())
def string_list():
return list(input())
def decimalToBinary(n):
return bin(n).replace("0b", "")
def binaryToDecimal(n):
return int(n,2)
def new_chr(a,b,alpha,k):
for i in range(k):
if alpha[i] != a and alpha[i] != b:
return alpha[i]
def solve():
n,k = num_input()
s = input()
if n == 1:
print(0)
print(s)
return
s = list(s)
if k == 2:
s1 = 'AB'*(n//2)
s2 = 'BA'*(n//2)
if n%2 != 0:
s1 += 'A'
s2 += 'B'
c1 = c2 = 0
for i in range(n):
if s[i] != s1[i]:
c1 += 1
if s[i] != s2[i]:
c2 += 1
if c1 < c2:
print(c1)
print(s1)
else:
print(c2)
print(s2)
else:
alpha = ['A']
for i in range(1,26):
alpha.append(chr(ord('A')+i))
i = 0
ans = 0
while i < n-1:
if s[i] == s[i+1]:
count = 1
j = i+1
while j < n and s[i] == s[j]:
count += 1
if count%2 == 0:
if j+1 < n:
s[j] = new_chr(s[j],s[j+1],alpha,k)
else:
s[j] = new_chr(s[j],s[j],alpha,k)
j += 1
ans += count//2
else:
i += 1
print(ans)
print(''.join(s))
#t = int(input())
t = 1
for _ in range(t):
solve()
``` | output | 1 | 59,127 | 7 | 118,255 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | instruction | 0 | 59,128 | 7 | 118,256 |
Tags: brute force, dp, greedy
Correct Solution:
```
def fastio():
import sys
from io import StringIO
from atexit import register
global input
sys.stdin = StringIO(sys.stdin.read())
input = lambda : sys.stdin.readline().rstrip('\r\n')
sys.stdout = StringIO()
register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
a = 'abcdefghijklmnopqrstuvwxyz'
a = a.upper()
n, k = I()
s = list(input() + 'aaa')
ans = 0
if k > 2:
for i in range(1, n):
if s[i] == s[i-1]:
for j in range(k):
if a[j] != s[i-1] and a[j] != s[i+1]:
ans += 1
s[i] = a[j]
break
else:
for j in range(k):
if a[j] != s[i-1]:
ans += 1
s[i] = a[j]
break
else:
x = ('AB'*(n//2)) + ('A'*(n%2))
y = ('BA'*(n//2)) + ('B'*(n%2))
a1 = 0
a2 = 0
for i in range(n):
if s[i] != x[i]:
a1 += 1
if s[i] != y[i]:
a2 += 1
ans = min(a1, a2)
s = [y, x][a1 < a2]
print(ans)
print(''.join(s[:n]))
``` | output | 1 | 59,128 | 7 | 118,257 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | instruction | 0 | 59,129 | 7 | 118,258 |
Tags: brute force, dp, greedy
Correct Solution:
```
# Author : nitish420 --------------------------------------------------------------------
import os
import sys
from io import BytesIO, IOBase
def main():
n,k=map(int,input().split())
s=list(input())
if n==1:
print(0)
print(s[0])
exit()
if k==2:
a,b='A','B'
# either it will be ababab or bababa so just check which one needs least number of modification .
x,y=0,0
# x : ababab, y:bababa
for i,item in enumerate(s):
if i%2:
if item=='A':
x+=1
else:
y+=1
else:
if item=='B':
x+=1
else:
y+=1
if x>y:
print(y)
print('BA'*(n//2)+'B'*(n%2))
else:
print(x)
print('AB'*(n//2) + 'A'*(n%2))
else:
a,b,c='A','B','C'
ans=0
for i in range(1,n-1):
if s[i]==s[i-1]:
st=set([s[i-1],s[i+1]])
if a not in st:
s[i]=a
ans+=1
elif b not in st:
s[i]=b
ans+=1
else:
s[i]=c
ans+=1
if s[-1]==s[-2]:
ans+=1
if s[-2]==a:
s[-1]=b
else:
s[-1]=a
print(ans)
print(*s,sep="")
'''
8 3
AABBABBB
'''
#----------------------------------------------------------------------------------------
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = 'x' in file.mode or 'r' not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b'\n') + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode('ascii'))
self.read = lambda: self.buffer.read().decode('ascii')
self.readline = lambda: self.buffer.readline().decode('ascii')
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
# endregion
if __name__ == '__main__':
main()
``` | output | 1 | 59,129 | 7 | 118,259 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | instruction | 0 | 59,130 | 7 | 118,260 |
Tags: brute force, dp, greedy
Correct Solution:
```
n,k=map(int,input().split())
s=input()
if(k==2):
#A
sol1=[s[i] for i in range(n)]
ans1=0
for i in range(n):
if(i%2==0 and sol1[i]=="B"):
ans1+=1
sol1[i]="A"
elif(i%2!=0 and sol1[i]=="A"):
ans1+=1
sol1[i]="B"
#B
sol2=[s[i] for i in range(n)]
ans2=0
for i in range(n):
if(i%2==0 and sol2[i]=="A"):
ans2+=1
sol2[i]="B"
elif(i%2!=0 and sol2[i]=="B"):
ans2+=1
sol2[i]="A"
if(ans1<=ans2):
print(ans1)
print("".join(str(x) for x in sol1))
else:
print(ans2)
print("".join(str(x) for x in sol2))
else:
s=[s[i] for i in range(n)]
ans=0
for i in range(1,n):
if(s[i]==s[i-1]):
ans+=1
x=chr((ord(s[i-1])-65+1)%k+65)
if(i==n-1 or s[i+1]!=x):s[i]=x
else:
y=chr((ord(s[i-1])-65+1+1)%k+65)
s[i]=y
print(ans)
print("".join(str(x) for x in s))
``` | output | 1 | 59,130 | 7 | 118,261 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | instruction | 0 | 59,131 | 7 | 118,262 |
Tags: brute force, dp, greedy
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#######################################
n,k=map(int,input().split())
s=input()
if k==2:
s1='AB'*(len(s)//2)
if len(s)%2:
s1+='A'
s2='BA'*(len(s)//2)
if len(s)%2:
s2+='B'
ans1=0
ans2=0
for i in range(len(s)):
if s1[i]!=s[i]:
ans1+=1
if s2[i]!=s[i]:
ans2+=1
if ans1<ans2:
print(ans1)
print(s1)
else:
print(ans2)
print(s2)
else:
s1=[]
ans=0
l=[]
i=0
while i<len(s):
if i==len(s)-1:
l.append([1,len(s)-1,len(s)-1])
break
if s[i]==s[i+1]:
c=1
j=i
while s[i]==s[i+1]:
c+=1
i+=1
if i==len(s)-1:
break
l.append([c,j,i])
i+=1
else:
l.append([1,i,i])
i+=1
for j in l:
if j[0]==1:
s1.append(s[j[1]])
else:
a=0
for p in range(j[1],j[2]+1):
if a==0:
s1.append(s[p])
a^=1
else:
for q in range(k):
if chr(65+q)!=s[p]:
s1.append(chr(65+q))
break
ans+=1
a^=1
if j[0]%2==0:
if j[2]!=len(s)-1:
if s1[j[2]]==s[j[2]+1]:
for p in range(k):
if chr(65+p)!=s[j[2]+1] and chr(65+p)!=s[j[2]]:
s1[j[2]]=chr(65+p)
break
print(ans)
print(''.join(s1))
``` | output | 1 | 59,131 | 7 | 118,263 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | instruction | 0 | 59,132 | 7 | 118,264 |
Tags: brute force, dp, greedy
Correct Solution:
```
import sys
from math import sqrt, gcd, ceil, log
# from bisect import bisect, bisect_left
from collections import defaultdict, Counter, deque
# from heapq import heapify, heappush, heappop
input = sys.stdin.readline
read = lambda: list(map(int, input().strip().split()))
def main():
n, k = read()
s = input().strip()+chr(k+64)
if k == 2:
t1 = ("AB"*(n//2 + 1))[:n]
t2 = ("BA"*(n//2 + 1))[:n]
dif1 = dif2 = 0
for i in range(n):
dif1 += t1[i] != s[i]
dif2 += t2[i] != s[i]
if dif1 > dif2:
print(dif2)
print(t2)
else:
print(dif1)
print(t1)
exit()
ans = s[0]
i = 1
alphs = {i-65: chr(i) for i in range(65, 65+k)}
# print(alphs)
l = 0
while i < len(s)-1:
if ans[-1] == s[i]:
ind = 0
while ind < k and ind == ord(ans[-1])-65 or ind == ord(s[i+1])-65:
ind += 1
ind %= k
ans += alphs[ind%k]
l += 1
else:
ans += s[i]
i += 1
print(l)
print(ans)
if __name__ == "__main__":
main()
``` | output | 1 | 59,132 | 7 | 118,265 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | instruction | 0 | 59,133 | 7 | 118,266 |
Tags: brute force, dp, greedy
Correct Solution:
```
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
s=list(input().rstrip())
al=list("abcdefghijklmnopqrstuvwxyz".upper())[:k]
al=set(al)
if k>=3:
cnt=0
for i in range(1,n-1):
if s[i-1]==s[i]==s[i+1]:
s[i]=list(al-{s[i-1]})[0]
cnt+=1
for i in range(1,n):
if s[i]==s[i-1]:
if i<n-1:
s[i]=list(al-{s[i-1]}-{s[i+1]})[0]
else:
s[i]=list(al-{s[i-1]})[0]
cnt+=1
print(cnt)
print("".join(s))
else:
s1="AB"*(n//2)+"A"*(n%2)
s2="BA"*(n//2)+"B"*(n%2)
cnt1,cnt2=0,0
for i in range(n):
if s[i]!=s1[i]:
cnt1+=1
if s[i]!=s2[i]:
cnt2+=1
if cnt1<cnt2:
print(cnt1)
print(s1)
else:
print(cnt2)
print(s2)
``` | output | 1 | 59,133 | 7 | 118,267 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB | instruction | 0 | 59,134 | 7 | 118,268 |
Tags: brute force, dp, greedy
Correct Solution:
```
from sys import maxsize, stdout, stdin,stderr
mod = int(1e9+7)
import sys
def I(): return int(stdin.readline())
def lint(): return [int(x) for x in stdin.readline().split()]
def S(): return input().strip()
def grid(r, c): return [lint() for i in range(r)]
from collections import defaultdict, Counter, deque
import math
import heapq
from heapq import heappop , heappush
import bisect
from itertools import groupby
def gcd(a,b):
while b:
a %= b
tmp = a
a = b
b = tmp
return a
def lcm(a,b):
return a // gcd(a, b) * b
def check_prime(n):
for i in range(2, int(n ** (1 / 2)) + 1):
if not n % i:
return False
return True
def Bs(a, x):
i=0
j=0
left = 1
right = x
flag=False
while left<right:
mi = (left+right)//2
#print(smi,a[mi],x)
if a[mi]<=x:
left = mi+1
i+=1
else:
right = mi
j+=1
#print(left,right,"----")
#print(i-1,j)
if left>0 and a[left-1]==x:
return i-1, j
else:
return -1, -1
def nCr(n, r):
return (fact(n) // (fact(r)
* fact(n - r)))
# Returns factorial of n
def fact(n):
res = 1
for i in range(2, n+1):
res = res * i
return res
def primefactors(n):
num=0
while n % 2 == 0:
num+=1
n = n / 2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
num+=1
n = n // i
if n > 2:
num+=1
return num
'''
def iter_ds(src):
store=[src]
while len(store):
tmp=store.pop()
if not vis[tmp]:
vis[tmp]=True
for j in ar[tmp]:
store.append(j)
'''
def ask(a):
print('? {}'.format(a),flush=True)
n=I()
return n
def dfs(i,p):
a,tmp=0,0
for j in d[i]:
if j!=p:
a+=1
tmp+=dfs(j,i)
if a==0:
return 0
return tmp/a + 1
def primeFactors(n):
l=[]
while n % 2 == 0:
l.append(2)
n = n // 2
if n > 2:
l.append(n)
return l
n,k=lint()
p = list(map(str,input().strip()))
#print(p)
s=p.copy()
ans=0
if k==2:
tmp=['A','B']
cnt=0
for i in range(n):
if s[i]!=tmp[i%2]:
cnt+=1
if cnt<n-cnt:
c=(['A','B']*(n//2+1))[:n]
print(cnt)
print("".join(c))
else:
c=(['A','B']*(n//2+1))[1:n+1]
print(n-cnt)
print("".join(c))
else:
tmp=s[0]
for i in range(1,n):
if s[i]==tmp:
for j in range(26):
r=chr(65+j)
if r not in [s[i-1], s[min(i+1,n-1)]]:
s[i]=r
tmp=r
break
else:
tmp=s[i]
for i in range(n):
if s[i]!=p[i]:
ans+=1
print(ans)
print("".join(s))
``` | output | 1 | 59,134 | 7 | 118,269 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB
Submitted Solution:
```
n,k=list(map(int,input().split()))
a=[i for i in input()]
b=0
if k==2:
for i in range(n):
if a[i]!='AB'[i%2]:
b+=1
if b<n-b:
print(b)
print('AB'*(n//2)+'A'*(n%2))
else:
print(n-b)
print('BA'*(n//2)+'B'*(n%2))
else:
for i in range(1,n):
if a[i]==a[i-1]:
for j in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[:k]:
if j!=a[i-1] and (i==n-1 or j!=a[i+1]):
a[i]=j
b+=1
break
print(b)
print(''.join(a))
``` | instruction | 0 | 59,135 | 7 | 118,270 |
Yes | output | 1 | 59,135 | 7 | 118,271 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB
Submitted Solution:
```
# import itertools
import bisect
# import math
from collections import defaultdict
import os
import sys
from io import BytesIO, IOBase
# sys.setrecursionlimit(10 ** 5)
ii = lambda: int(input())
lmii = lambda: list(map(int, input().split()))
slmii = lambda: sorted(map(int, input().split()))
li = lambda: list(input())
mii = lambda: map(int, input().split())
msi = lambda: map(str, input().split())
def gcd(a, b):
if b == 0: return a
return gcd(b, a % b)
def lcm(a, b): return (a * b) // gcd(a, b)
def main():
# for _ in " " * int(input()):
n, k = mii()
s = li()
a = ["A", "B", "C"]
cnt = 0
if n == 1:
print(0)
print("".join(s))
elif n == 2:
if s[0] != s[1]:
print(0)
print("".join(s))
else:
print(1)
if s[0] == "A":
print("AB")
else:
print(s[0]+"A")
else:
if k > 2:
for i in range(1, n - 1):
if s[i] == s[i - 1] and s[i] == s[i + 1]:
cnt += 1
s[i] = (set(a) - {s[i - 1], s[i + 1]}).pop()
for i in range(1, n - 1):
if s[i] == s[i-1] or s[i] == s[i+1]:
cnt += 1
s[i] = (set(a) - {s[i - 1], s[i + 1]}).pop()
print(cnt)
print("".join(s))
else:
f = []
ss = []
for i in range(n):
if i % 2 == 0:
ss.append("B")
f.append("A")
else:
f.append("B")
ss.append("A")
fc = 0
sc = 0
for i in range(n):
if s[i] != f[i]:
fc += 1
else:
sc += 1
if fc < sc:
print(cnt + fc)
print("".join(f))
else:
print(cnt + sc)
print("".join(ss))
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
main()
``` | instruction | 0 | 59,136 | 7 | 118,272 |
Yes | output | 1 | 59,136 | 7 | 118,273 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB
Submitted Solution:
```
n, k = map(int, input().split())
s = input()
l = []
ans = 0
if(k == 2):
s1 = []
s2 = []
for i in range(n):
if(i&1):
s1.append('A')
s2.append('B')
else:
s2.append('A')
s1.append('B')
a1 = 0
a2 = 0
for i in range(n):
if(s[i]!= s1[i]):
a1+=1
if(s[i]!= s2[i]):
a2+=1
if(a1>a2):
print(a2)
print(("").join(s2))
else:
print(a1)
print(("").join(s1))
else:
for i in range(n):
if(len(l) == 0):
l.append(s[i])
else:
if(l[-1] == s[i]):
if(i!=n-1):
a = l[-1]
b = s[i+1]
if(a == b):
if(a== 'A'):
l.append('B')
else:
l.append('A')
else:
if(a == 'A' and b != 'B'):
l.append('B')
elif(a == 'A' and b == 'B'):
l.append('C')
elif(a != 'B' and b == 'A'):
l.append('B')
elif(a == 'B' and b == 'A'):
l.append('C')
else:
l.append('A')
else:
if(l[-1]== 'A'):
l.append('B')
else:
l.append('A')
ans+=1
else:
l.append(s[i])
print(ans)
print(("").join(l))
``` | instruction | 0 | 59,137 | 7 | 118,274 |
Yes | output | 1 | 59,137 | 7 | 118,275 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB
Submitted Solution:
```
#import sys, os.path
import os,sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
from collections import*
from copy import*
import math
import string
mod=10**9+7
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt","r")
sys.stdout = open("output.txt","w")
def charc(a,b):
for i in range(3):
if(alpha[i]!=a and alpha[i]!=b):
return alpha[i]
def charc2(a):
if(a=='A'):
return 'B'
else:
return 'A'
n,k=map(int,input().split())
s=input()
res=[]
for i in range(n):
res.append(s[i])
alpha=string.ascii_uppercase[:k]
c=0
if(n==1):
print(0)
print(s)
elif(k==2 or n==2):
if(n%2==0):
res1='AB'*(n//2)
res2='BA'*(n//2)
else:
res1='AB'*(n//2)+'A'
res2='BA'*(n//2)+'B'
c1=0
c2=0
for i in range(n):
if(s[i]!=res1[i]):
c1+=1
if(s[i]!=res2[i]):
c2+=1
final=min(c1,c2)
print(final)
if(final==c1):
print(res1)
else:
print(res2)
else:
for i in range(1,n-1):
temp=res[i]
if(res[i]!=res[i-1]):
continue
else:
res[i]=charc(res[i+1],res[i-1])
if(res[i]==None):
res[i]=temp
else:
c+=1
if(n>2):
if(res[-2]==res[-1]):
res[-1]=charc2(res[-2])
c+=1
print(c)
print(''.join(res))
``` | instruction | 0 | 59,138 | 7 | 118,276 |
Yes | output | 1 | 59,138 | 7 | 118,277 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB
Submitted Solution:
```
n, k = input().split()
n = int(n)
k = int(k)
a = input()
al = set(a)
if len(al) < k:
for i in range(65, 91):
al.add(chr(i))
if len(al) == k:
break
i = 0
if len(a) == 1:
print(0)
print(a)
elif len(a) == 2:
if a[0] == a[1]:
for i in al:
if i != a[1]:
print(1)
print(i,a[1] , sep = "")
break
else:
print(0)
print(a)
else:
i = 1
k = 0
if a[0] == a[1] and a[1] != a[2]:
k += 1
for j in al:
if j != a[1]:
a = j + a[1:]
while i < n - 1:
if a[i-1] == a[i]:
k += 1
for j in al:
if j != a[i+1] and j != a[i-1]:
a = a[:i] + j + a[i+1:]
i += 1
if a[n-1] == a[n-2]:
k += 1
for j in al:
if j != a[n-2]:
a = a[:n-1] + j
print(k)
print(a)
``` | instruction | 0 | 59,139 | 7 | 118,278 |
No | output | 1 | 59,139 | 7 | 118,279 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB
Submitted Solution:
```
import sys, os.path
from collections import*
from copy import*
import math
import string
mod=10**9+7
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt","r")
sys.stdout = open("output.txt","w")
def charc(a,b):
for i in alpha:
if(i!=a and i!=b):
return i
def charc2(a):
for i in alpha:
if(i!=a):
return i
n,k=map(int,input().split())
s=input()
res=[]
for i in range(n):
res.append(s[i])
alpha=string.ascii_uppercase[:k]
c=0
if(n==1):
print(0)
print(s)
elif(k==2):
if(n%2==0):
res1='AB'*n
res2='BA'*n
else:
res1='AB'*(n-1)+'A'
res2='BA'*(n-1)+'B'
c1=0
c2=0
for i in range(n):
if(s[i]==res1[i]):
c1+=1
if(s[i]==res2[i]):
c2+=1
final=min(c1,c2)
print(final)
if(final==c1):
print(res1)
else:
print(res2)
else:
for i in range(1,n-1):
temp=res[i]
if(res[i]!=res[i-1]):
continue
else:
res[i]=charc(res[i+1],res[i-1])
if(res[i]==None):
res[i]=temp
else:
c+=1
if(n>2):
if(res[-2]==res[-1]):
res[-1]=charc2(res[-2])
c+=1
print(c)
a=''
for i in res:
a+=i
print(a)
``` | instruction | 0 | 59,140 | 7 | 118,280 |
No | output | 1 | 59,140 | 7 | 118,281 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB
Submitted Solution:
```
R = lambda: map(int, input().split())
n, k = R()
s = input()
cnt = 0
for i in range(1, n):
if s[i] == s[i - 1]:
for j in range(k):
c = chr(ord('A') + j)
if c != s[i - 1] and (i == n - 1 or c != s[i + 1]):
s = s[:i] + c + (s[i + 1:] if i < n else '')
cnt += 1
print(cnt)
print(s)
``` | instruction | 0 | 59,141 | 7 | 118,282 |
No | output | 1 | 59,141 | 7 | 118,283 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 β€ n β€ 5Β·105; 2 β€ k β€ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB
Submitted Solution:
```
import sys
input = sys.stdin.readline
def gcd(a, b):
if a == 0:
return b
return gcd(b % a, a)
def lcm(a, b):
return (a * b) / gcd(a, b)
def main():
n,k=map(int, input().split())
s=input()
a=s
s=[]
for i in range(len(a)-1):
s.append(a[i])
#print(*s)
prev=0
alphs=[]
ans=0
for i in range(k):
alphs.append(chr(ord('A')+i))
#print(alphs)
for i in range(1,n):
#print(prev, i)
if s[i]!=s[prev]:
if i-prev>1:
#print('YES')
if (i-prev)%2:
ans+=(i-prev)//2
t=0
put=''
for j in alphs:
if j!=s[i]:
put=j
break
for j in range(prev, i):
if t%2:
s[j]=put
t+=1
else:
falpha=''
sealpha=''
for j in alphs: # leftXXXXXXXright always there is right right -> sealpha
if j!=s[i] and j!=s[prev]:
falpha=j
if prev:
if j!=s[prev-1] and j!=s[prev]:
sealpha=j
#print(sealpha, falpha)
if falpha!='':
t=0
for j in range(prev, i):
if t%2:
s[j]=falpha
ans+=1
t+=1
else:
t=0
for j in range(prev, i):
if t%2==0:
s[j]=sealpha
ans+=1
t+=1
prev=i
i=n-1
#print(prev, i)
if i - prev >0:
if (i - prev+1 ) % 2:
ans += (i - prev+1 ) // 2
t = 0
put = ''
for j in alphs:
if j != s[i]:
put = j
break
for j in range(prev, i+1):
if t % 2:
s[j] = put
t += 1
else:
falpha = ''
sealpha = ''
for j in alphs: # leftXXXXXXXright always there is right right -> sealph
if i+1<n:
if j != s[i+1] and j != s[prev]:
falpha = j
if prev:
if j != s[prev - 1] and j != s[prev]:
sealpha = j
if falpha != '':
t = 0
for j in range(prev, i+1):
if t % 2:
s[j] = falpha
ans += 1
t += 1
else:
t = 0
for j in range(prev, i+1):
if t % 2 == 0:
s[j] = sealpha
ans += 1
t += 1
print(ans)
for i in s:
print(i,end='')
return
if __name__=="__main__":
main()
``` | instruction | 0 | 59,142 | 7 | 118,284 |
No | output | 1 | 59,142 | 7 | 118,285 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have r red, g green and b blue balloons. To decorate a single table for the banquet you need exactly three balloons. Three balloons attached to some table shouldn't have the same color. What maximum number t of tables can be decorated if we know number of balloons of each color?
Your task is to write a program that for given values r, g and b will find the maximum number t of tables, that can be decorated in the required manner.
Input
The single line contains three integers r, g and b (0 β€ r, g, b β€ 2Β·109) β the number of red, green and blue baloons respectively. The numbers are separated by exactly one space.
Output
Print a single integer t β the maximum number of tables that can be decorated in the required manner.
Examples
Input
5 4 3
Output
4
Input
1 1 1
Output
1
Input
2 3 3
Output
2
Note
In the first sample you can decorate the tables with the following balloon sets: "rgg", "gbb", "brr", "rrg", where "r", "g" and "b" represent the red, green and blue balls, respectively. | instruction | 0 | 59,212 | 7 | 118,424 |
Tags: greedy
Correct Solution:
```
a=sorted(map(int,input().split()))
print(min(a[0]+a[1],sum(a)//3))
``` | output | 1 | 59,212 | 7 | 118,425 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have r red, g green and b blue balloons. To decorate a single table for the banquet you need exactly three balloons. Three balloons attached to some table shouldn't have the same color. What maximum number t of tables can be decorated if we know number of balloons of each color?
Your task is to write a program that for given values r, g and b will find the maximum number t of tables, that can be decorated in the required manner.
Input
The single line contains three integers r, g and b (0 β€ r, g, b β€ 2Β·109) β the number of red, green and blue baloons respectively. The numbers are separated by exactly one space.
Output
Print a single integer t β the maximum number of tables that can be decorated in the required manner.
Examples
Input
5 4 3
Output
4
Input
1 1 1
Output
1
Input
2 3 3
Output
2
Note
In the first sample you can decorate the tables with the following balloon sets: "rgg", "gbb", "brr", "rrg", where "r", "g" and "b" represent the red, green and blue balls, respectively. | instruction | 0 | 59,213 | 7 | 118,426 |
Tags: greedy
Correct Solution:
```
def get_answer(a, b, c):
return min((a+b+c)//3, a+b+c-max(a,b,c))
r, g, b = map(int, input().split())
print(get_answer(r, g, b))
``` | output | 1 | 59,213 | 7 | 118,427 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have r red, g green and b blue balloons. To decorate a single table for the banquet you need exactly three balloons. Three balloons attached to some table shouldn't have the same color. What maximum number t of tables can be decorated if we know number of balloons of each color?
Your task is to write a program that for given values r, g and b will find the maximum number t of tables, that can be decorated in the required manner.
Input
The single line contains three integers r, g and b (0 β€ r, g, b β€ 2Β·109) β the number of red, green and blue baloons respectively. The numbers are separated by exactly one space.
Output
Print a single integer t β the maximum number of tables that can be decorated in the required manner.
Examples
Input
5 4 3
Output
4
Input
1 1 1
Output
1
Input
2 3 3
Output
2
Note
In the first sample you can decorate the tables with the following balloon sets: "rgg", "gbb", "brr", "rrg", where "r", "g" and "b" represent the red, green and blue balls, respectively. | instruction | 0 | 59,214 | 7 | 118,428 |
Tags: greedy
Correct Solution:
```
a=list(map(int,input().split()))
a.sort()
if(2*(a[0]+a[1])<=a[2]):
print(a[0]+a[1])
else:
print((a[0]+a[1]+a[2])//3)
``` | output | 1 | 59,214 | 7 | 118,429 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have r red, g green and b blue balloons. To decorate a single table for the banquet you need exactly three balloons. Three balloons attached to some table shouldn't have the same color. What maximum number t of tables can be decorated if we know number of balloons of each color?
Your task is to write a program that for given values r, g and b will find the maximum number t of tables, that can be decorated in the required manner.
Input
The single line contains three integers r, g and b (0 β€ r, g, b β€ 2Β·109) β the number of red, green and blue baloons respectively. The numbers are separated by exactly one space.
Output
Print a single integer t β the maximum number of tables that can be decorated in the required manner.
Examples
Input
5 4 3
Output
4
Input
1 1 1
Output
1
Input
2 3 3
Output
2
Note
In the first sample you can decorate the tables with the following balloon sets: "rgg", "gbb", "brr", "rrg", where "r", "g" and "b" represent the red, green and blue balls, respectively. | instruction | 0 | 59,215 | 7 | 118,430 |
Tags: greedy
Correct Solution:
```
a = [int(i) for i in input().split()]
a.sort()
if a[0]+a[1] < a[2]/2 :
print(a[0]+a[1])
else:
print((a[0]+a[1]+a[2])//3)
``` | output | 1 | 59,215 | 7 | 118,431 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have r red, g green and b blue balloons. To decorate a single table for the banquet you need exactly three balloons. Three balloons attached to some table shouldn't have the same color. What maximum number t of tables can be decorated if we know number of balloons of each color?
Your task is to write a program that for given values r, g and b will find the maximum number t of tables, that can be decorated in the required manner.
Input
The single line contains three integers r, g and b (0 β€ r, g, b β€ 2Β·109) β the number of red, green and blue baloons respectively. The numbers are separated by exactly one space.
Output
Print a single integer t β the maximum number of tables that can be decorated in the required manner.
Examples
Input
5 4 3
Output
4
Input
1 1 1
Output
1
Input
2 3 3
Output
2
Note
In the first sample you can decorate the tables with the following balloon sets: "rgg", "gbb", "brr", "rrg", where "r", "g" and "b" represent the red, green and blue balls, respectively. | instruction | 0 | 59,216 | 7 | 118,432 |
Tags: greedy
Correct Solution:
```
r,g,b=[int(i) for i in input().split()]
r,g,b=sorted([r,g,b])
if 2*(r+g)<=b:
print(r+g)
else:
print((r+g+b)//3)
``` | output | 1 | 59,216 | 7 | 118,433 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.