message stringlengths 2 59.7k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 37 108k | cluster float64 20 20 | __index_level_0__ int64 74 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence a_1, a_2, β¦, a_n of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.
Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.
Let's denote the fingers of hand by numbers from 1 to 5. We call a fingering any sequence b_1, β¦, b_n of fingers numbers. A fingering is convenient if for all 1β€ i β€ n - 1 the following holds:
* if a_i < a_{i+1} then b_i < b_{i+1}, because otherwise Paul needs to take his hand off the keyboard to play the (i+1)-st note;
* if a_i > a_{i+1} then b_i > b_{i+1}, because of the same;
* if a_i = a_{i+1} then b_iβ b_{i+1}, because using the same finger twice in a row is dumb. Please note that there is β , not = between b_i and b_{i+1}.
Please provide any convenient fingering or find out that there is none.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) denoting the number of notes.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2β
10^5) denoting the positions of notes on the keyboard.
Output
If there is no convenient fingering, print -1. Otherwise, print n numbers b_1, b_2, β¦, b_n, each from 1 to 5, denoting a convenient fingering, separated by spaces.
Examples
Input
5
1 1 4 2 2
Output
1 4 5 4 5
Input
7
1 5 7 8 10 3 1
Output
1 2 3 4 5 4 3
Input
19
3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8
Output
1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4
Note
The third sample test is kinda "Non stop" song by Reflex. | instruction | 0 | 46,180 | 20 | 92,360 |
Tags: constructive algorithms, dp
Correct Solution:
```
#Code by Sounak, IIESTS
#------------------------------warmup----------------------------
import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading
#sys.setrecursionlimit(300000)
#threading.stack_size(10**8)
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")
#-------------------------------------------------------------------------
#mod = 9223372036854775807
class SegmentTree:
def __init__(self, data, default=-10**6, func=lambda a, b: max(a,b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
class SegmentTree1:
def __init__(self, data, default=10**6, func=lambda a, b: min(a,b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
MOD=10**9+7
class Factorial:
def __init__(self, MOD):
self.MOD = MOD
self.factorials = [1, 1]
self.invModulos = [0, 1]
self.invFactorial_ = [1, 1]
def calc(self, n):
if n <= -1:
print("Invalid argument to calculate n!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.factorials):
return self.factorials[n]
nextArr = [0] * (n + 1 - len(self.factorials))
initialI = len(self.factorials)
prev = self.factorials[-1]
m = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = prev * i % m
self.factorials += nextArr
return self.factorials[n]
def inv(self, n):
if n <= -1:
print("Invalid argument to calculate n^(-1)")
print("n must be non-negative value. But the argument was " + str(n))
exit()
p = self.MOD
pi = n % p
if pi < len(self.invModulos):
return self.invModulos[pi]
nextArr = [0] * (n + 1 - len(self.invModulos))
initialI = len(self.invModulos)
for i in range(initialI, min(p, n + 1)):
next = -self.invModulos[p % i] * (p // i) % p
self.invModulos.append(next)
return self.invModulos[pi]
def invFactorial(self, n):
if n <= -1:
print("Invalid argument to calculate (n^(-1))!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.invFactorial_):
return self.invFactorial_[n]
self.inv(n) # To make sure already calculated n^-1
nextArr = [0] * (n + 1 - len(self.invFactorial_))
initialI = len(self.invFactorial_)
prev = self.invFactorial_[-1]
p = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p
self.invFactorial_ += nextArr
return self.invFactorial_[n]
class Combination:
def __init__(self, MOD):
self.MOD = MOD
self.factorial = Factorial(MOD)
def ncr(self, n, k):
if k < 0 or n < k:
return 0
k = min(k, n - k)
f = self.factorial
return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
mod=10**9+7
omod=998244353
#-------------------------------------------------------------------------
prime = [True for i in range(200001)]
pp=[0]*200001
def SieveOfEratosthenes(n=200000):
# Create a boolean array "prime[0..n]" and initialize
# all entries it as true. A value in prime[i] will
# finally be false if i is Not a prime, else true.
p = 2
while (p * p <= n):
# If prime[p] is not changed, then it is a prime
if (prime[p] == True):
# Update all multiples of p
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
#---------------------------------running code------------------------------------------
n=int(input())
a=list(map(int,input().split()))
if n==1:
print(1)
sys.exit(0)
dp=[[-1 for j in range (5)] for i in range (n)]
for i in range (1,min(2,n)):
if a[i]<a[i-1]:
for j in range (5):
ch=-1
for k in range (5):
if k>j:
ch=k
dp[i][j]=ch
elif a[i]>a[i-1]:
for j in range (5):
ch=-1
for k in range (5):
if k<j:
ch=k
dp[i][j]=ch
else:
for j in range (5):
ch=-1
for k in range (5):
if k!=j:
ch=k
dp[i][j]=ch
for i in range (2,n):
if a[i]<a[i-1]:
for j in range (5):
ch=-1
for k in range (5):
if k>j and dp[i-1][k]!=-1:
ch=k
dp[i][j]=ch
elif a[i]>a[i-1]:
for j in range (5):
ch=-1
for k in range (5):
if k<j and dp[i-1][k]!=-1:
ch=k
dp[i][j]=ch
else:
for j in range (5):
ch=-1
for k in range (5):
if k!=j and dp[i-1][k]!=-1:
ch=k
dp[i][j]=ch
#print(dp)
ind=-1
for i in range (5):
if dp[-1][i]!=-1:
ind=i
if ind==-1:
print(ind)
sys.exit(0)
res=[ind+1]
for i in range (n-1,0,-1):
res.append(dp[i][ind]+1)
ind=dp[i][ind]
print(*res[::-1])
``` | output | 1 | 46,180 | 20 | 92,361 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence a_1, a_2, β¦, a_n of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.
Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.
Let's denote the fingers of hand by numbers from 1 to 5. We call a fingering any sequence b_1, β¦, b_n of fingers numbers. A fingering is convenient if for all 1β€ i β€ n - 1 the following holds:
* if a_i < a_{i+1} then b_i < b_{i+1}, because otherwise Paul needs to take his hand off the keyboard to play the (i+1)-st note;
* if a_i > a_{i+1} then b_i > b_{i+1}, because of the same;
* if a_i = a_{i+1} then b_iβ b_{i+1}, because using the same finger twice in a row is dumb. Please note that there is β , not = between b_i and b_{i+1}.
Please provide any convenient fingering or find out that there is none.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) denoting the number of notes.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2β
10^5) denoting the positions of notes on the keyboard.
Output
If there is no convenient fingering, print -1. Otherwise, print n numbers b_1, b_2, β¦, b_n, each from 1 to 5, denoting a convenient fingering, separated by spaces.
Examples
Input
5
1 1 4 2 2
Output
1 4 5 4 5
Input
7
1 5 7 8 10 3 1
Output
1 2 3 4 5 4 3
Input
19
3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8
Output
1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4
Note
The third sample test is kinda "Non stop" song by Reflex. | instruction | 0 | 46,181 | 20 | 92,362 |
Tags: constructive algorithms, dp
Correct Solution:
```
n=int(input())
arr=list(map(int,input().split()))
dp=[[-1 for i in range(5+1)] for j in range(n)]
for i in range(1,6):
dp[0][i] =1
for i in range(1,n):
if arr[i] > arr[i - 1]:
for j in range(1,6):
for k in range(1,j):
if dp[i-1][k]==1:
dp[i][j] =1
break
elif arr[i] <arr[i-1]:
for j in range(1,6):
for k in range(j+1,6):
if dp[i-1][k] ==1:
dp[i][j]=1
break
else:
for j in range(1,6):
for k in range(1,6):
if j ==k:
continue
if dp[i-1][k] ==1:
dp[i][j] =1
break
ans=[]
for i in range(1,6):
if dp[n-1][i]==1:
ans.append(i)
break
if len(ans) ==0:
print(-1)
exit()
for i in range(n-2,-1,-1):
curr=ans[-1]
if arr[i] >arr[i+1]:
for j in range(curr+1,6):
if dp[i][j] ==1:
ans.append(j)
break
elif arr[i] <arr[i+1]:
for j in range(1,curr):
if dp[i][j] ==1:
ans.append(j)
break
else:
for j in range(1,6):
if j ==curr:
continue
if dp[i][j] ==1:
ans.append(j)
break
ans=ans[::-1]
print(*ans)
``` | output | 1 | 46,181 | 20 | 92,363 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence a_1, a_2, β¦, a_n of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.
Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.
Let's denote the fingers of hand by numbers from 1 to 5. We call a fingering any sequence b_1, β¦, b_n of fingers numbers. A fingering is convenient if for all 1β€ i β€ n - 1 the following holds:
* if a_i < a_{i+1} then b_i < b_{i+1}, because otherwise Paul needs to take his hand off the keyboard to play the (i+1)-st note;
* if a_i > a_{i+1} then b_i > b_{i+1}, because of the same;
* if a_i = a_{i+1} then b_iβ b_{i+1}, because using the same finger twice in a row is dumb. Please note that there is β , not = between b_i and b_{i+1}.
Please provide any convenient fingering or find out that there is none.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) denoting the number of notes.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2β
10^5) denoting the positions of notes on the keyboard.
Output
If there is no convenient fingering, print -1. Otherwise, print n numbers b_1, b_2, β¦, b_n, each from 1 to 5, denoting a convenient fingering, separated by spaces.
Examples
Input
5
1 1 4 2 2
Output
1 4 5 4 5
Input
7
1 5 7 8 10 3 1
Output
1 2 3 4 5 4 3
Input
19
3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8
Output
1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4
Note
The third sample test is kinda "Non stop" song by Reflex. | instruction | 0 | 46,182 | 20 | 92,364 |
Tags: constructive algorithms, dp
Correct Solution:
```
import math
from collections import Counter, defaultdict
R = lambda: map(int, input().split())
n = int(input())
arr = list(R())
if n == 1:
print(1)
exit(0)
dp = [[0] * 6 for i in range(n + 1)]
for i in range(1, n):
for j in range(1, 6):
if arr[i] > arr[i - 1]:
for k in range(1, j):
if i == 1 or dp[i - 1][k]:
dp[i][j] = k
break
elif arr[i] < arr[i - 1]:
for k in range(j + 1, 6):
if i == 1 or dp[i - 1][k]:
dp[i][j] = k
break
else:
for k in range(1, 6):
if k != j and (i == 1 or dp[i - 1][k]):
dp[i][j] = k
if not any(dp[n - 1]):
print(-1)
exit(0)
res = []
for j in range(1, 6):
if dp[n - 1][j]:
res.append(j)
break
for i in range(n - 1, 0, -1):
res.append(dp[i][res[-1]])
print(' '.join(map(str, res[::-1])))
``` | output | 1 | 46,182 | 20 | 92,365 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence a_1, a_2, β¦, a_n of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.
Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.
Let's denote the fingers of hand by numbers from 1 to 5. We call a fingering any sequence b_1, β¦, b_n of fingers numbers. A fingering is convenient if for all 1β€ i β€ n - 1 the following holds:
* if a_i < a_{i+1} then b_i < b_{i+1}, because otherwise Paul needs to take his hand off the keyboard to play the (i+1)-st note;
* if a_i > a_{i+1} then b_i > b_{i+1}, because of the same;
* if a_i = a_{i+1} then b_iβ b_{i+1}, because using the same finger twice in a row is dumb. Please note that there is β , not = between b_i and b_{i+1}.
Please provide any convenient fingering or find out that there is none.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) denoting the number of notes.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2β
10^5) denoting the positions of notes on the keyboard.
Output
If there is no convenient fingering, print -1. Otherwise, print n numbers b_1, b_2, β¦, b_n, each from 1 to 5, denoting a convenient fingering, separated by spaces.
Examples
Input
5
1 1 4 2 2
Output
1 4 5 4 5
Input
7
1 5 7 8 10 3 1
Output
1 2 3 4 5 4 3
Input
19
3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8
Output
1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4
Note
The third sample test is kinda "Non stop" song by Reflex. | instruction | 0 | 46,183 | 20 | 92,366 |
Tags: constructive algorithms, dp
Correct Solution:
```
n = int(input())
arr = list(map(int, input().split()))
pal = 1 if arr[1] > arr[0] else 3 if arr[0] == arr[1] else 5
b = True
arr_pal = [pal]
for i in range(n - 2):
if arr[i + 1] > arr[i]:
if pal == 5:
b = False
break
if arr[i + 2] < arr[i + 1]:
pal = 5
arr_pal.append(pal)
else:
pal += 1
arr_pal.append(pal)
elif arr[i + 1] < arr[i]:
if pal == 1:
b = False
break
if arr[i + 2] > arr[i + 1]:
pal = 1
arr_pal.append(pal)
else:
pal -= 1
arr_pal.append(pal)
else:
if arr[i + 2] > arr[i + 1]:
pal = 2 if pal == 1 else 1
arr_pal.append(pal)
elif arr[i + 2] < arr[i + 1]:
pal = 4 if pal == 5 else 5
arr_pal.append(pal)
else:
pal = 4 if pal < 4 else 3
arr_pal.append(pal)
if arr[-2] < arr[-1]:
if pal == 5:
b = False
else:
pal += 1
arr_pal.append(pal)
elif arr[-2] > arr[-1]:
if pal == 1:
b = False
else:
pal -= 1
arr_pal.append(pal)
else:
pal = 3 if pal == 5 else 5
arr_pal.append(pal)
if b:
print(*arr_pal)
else:
print(-1)
``` | output | 1 | 46,183 | 20 | 92,367 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence a_1, a_2, β¦, a_n of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.
Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.
Let's denote the fingers of hand by numbers from 1 to 5. We call a fingering any sequence b_1, β¦, b_n of fingers numbers. A fingering is convenient if for all 1β€ i β€ n - 1 the following holds:
* if a_i < a_{i+1} then b_i < b_{i+1}, because otherwise Paul needs to take his hand off the keyboard to play the (i+1)-st note;
* if a_i > a_{i+1} then b_i > b_{i+1}, because of the same;
* if a_i = a_{i+1} then b_iβ b_{i+1}, because using the same finger twice in a row is dumb. Please note that there is β , not = between b_i and b_{i+1}.
Please provide any convenient fingering or find out that there is none.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) denoting the number of notes.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2β
10^5) denoting the positions of notes on the keyboard.
Output
If there is no convenient fingering, print -1. Otherwise, print n numbers b_1, b_2, β¦, b_n, each from 1 to 5, denoting a convenient fingering, separated by spaces.
Examples
Input
5
1 1 4 2 2
Output
1 4 5 4 5
Input
7
1 5 7 8 10 3 1
Output
1 2 3 4 5 4 3
Input
19
3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8
Output
1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4
Note
The third sample test is kinda "Non stop" song by Reflex. | instruction | 0 | 46,184 | 20 | 92,368 |
Tags: constructive algorithms, dp
Correct Solution:
```
n = int(input())
ar = [int(i) for i in input().split()]
if n == 1:
print(1)
exit()
if ar[1] > ar[0]:
li = [1]
elif ar[1] < ar[0]:
li = [5]
else:
li = [3]
c = 1
while c != n:
j = 0
if ar[c] > ar[c - 1]:
while c != n and ar[c] > ar[c - 1]:
c += 1
j += 1
for i in range(j-1):
li.append(li[-1] + 1)
if li[-1] == 6:
print(-1)
# print(*li)
exit()
if c != n and ar[c] == ar[c - 1]:
li.append(li[-1] + 1)
else:
li.append(5)
elif ar[c] < ar[c - 1]:
while c != n and ar[c] < ar[c - 1]:
c += 1
j += 1
for i in range(j-1):
li.append(li[-1] - 1)
if li[-1] == 0:
print(-1)
# print(*li)
exit()
if c != n and ar[c] == ar[c - 1]:
li.append(li[-1] - 1)
else:
li.append(1)
else:
while c != n and ar[c] == ar[c - 1]:
c += 1
j += 1
for i in range(j):
if li[-1] > 3:
li.append(li[-1] - 1)
else:
li.append(li[-1] + 1)
if c != n and ar[c] > ar[c - 1]:
if li[-2] == 1:
li[-1] = 2
else:
li[-1] = 1
elif c != n and ar[c] < ar[c - 1]:
if li[-2] == 5:
li[-1] = 4
else:
li[-1] = 5
#print(c)
if max(li) > 5 or min(li) < 1:
print(-1)
else:
print(*li)
``` | output | 1 | 46,184 | 20 | 92,369 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence a_1, a_2, β¦, a_n of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.
Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.
Let's denote the fingers of hand by numbers from 1 to 5. We call a fingering any sequence b_1, β¦, b_n of fingers numbers. A fingering is convenient if for all 1β€ i β€ n - 1 the following holds:
* if a_i < a_{i+1} then b_i < b_{i+1}, because otherwise Paul needs to take his hand off the keyboard to play the (i+1)-st note;
* if a_i > a_{i+1} then b_i > b_{i+1}, because of the same;
* if a_i = a_{i+1} then b_iβ b_{i+1}, because using the same finger twice in a row is dumb. Please note that there is β , not = between b_i and b_{i+1}.
Please provide any convenient fingering or find out that there is none.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) denoting the number of notes.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2β
10^5) denoting the positions of notes on the keyboard.
Output
If there is no convenient fingering, print -1. Otherwise, print n numbers b_1, b_2, β¦, b_n, each from 1 to 5, denoting a convenient fingering, separated by spaces.
Examples
Input
5
1 1 4 2 2
Output
1 4 5 4 5
Input
7
1 5 7 8 10 3 1
Output
1 2 3 4 5 4 3
Input
19
3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8
Output
1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4
Note
The third sample test is kinda "Non stop" song by Reflex. | instruction | 0 | 46,185 | 20 | 92,370 |
Tags: constructive algorithms, dp
Correct Solution:
```
from collections import deque
n=int(input())
l=list(map(int,input().split()))
dp=[[-1 for j in range(5)] for i in range(n)]
dp[0][0]=1
dp[0][1]=1
dp[0][2]=1
dp[0][3]=1
dp[0][4]=1
f=0
for i in range(1,n):
for j in range(5):
for k in range(5):
if dp[i-1][k]!=-1:
if l[i-1]>l[i] and k>j:
dp[i][j]=k
break
elif l[i-1]<l[i] and k<j:
dp[i][j]=k
break
elif l[i-1]==l[i] and j!=k:
dp[i][j]=k
break
for i in range(n):
if sum(dp[i])==-5:
f=1
break
#print(dp)
if f==1:
print(-1)
else:
lu=deque([])
for i in range(5):
if dp[n-1][i]!=-1:
k=i
break
for i in range(n,0,-1):
lu.appendleft(k+1)
k=dp[i-1][k]
print(*lu,sep=" ")
``` | output | 1 | 46,185 | 20 | 92,371 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence a_1, a_2, β¦, a_n of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.
Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.
Let's denote the fingers of hand by numbers from 1 to 5. We call a fingering any sequence b_1, β¦, b_n of fingers numbers. A fingering is convenient if for all 1β€ i β€ n - 1 the following holds:
* if a_i < a_{i+1} then b_i < b_{i+1}, because otherwise Paul needs to take his hand off the keyboard to play the (i+1)-st note;
* if a_i > a_{i+1} then b_i > b_{i+1}, because of the same;
* if a_i = a_{i+1} then b_iβ b_{i+1}, because using the same finger twice in a row is dumb. Please note that there is β , not = between b_i and b_{i+1}.
Please provide any convenient fingering or find out that there is none.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) denoting the number of notes.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2β
10^5) denoting the positions of notes on the keyboard.
Output
If there is no convenient fingering, print -1. Otherwise, print n numbers b_1, b_2, β¦, b_n, each from 1 to 5, denoting a convenient fingering, separated by spaces.
Examples
Input
5
1 1 4 2 2
Output
1 4 5 4 5
Input
7
1 5 7 8 10 3 1
Output
1 2 3 4 5 4 3
Input
19
3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8
Output
1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4
Note
The third sample test is kinda "Non stop" song by Reflex. | instruction | 0 | 46,186 | 20 | 92,372 |
Tags: constructive algorithms, dp
Correct Solution:
```
n = int(input())
arr = list(map(int, input().split()))
dp = [[0] * 5 for i in range(n)]
dp[0] = [1, 1, 1, 1, 1]
for i in range(1, n):
if arr[i] > arr[i - 1]:
for j in range(1, 5):
dp[i][j] = max(dp[i][j - 1], dp[i - 1][j - 1])
elif arr[i] < arr[i - 1]:
for j in range(3, -1, -1):
dp[i][j] = max(dp[i - 1][j + 1], dp[i][j + 1])
else:
for j in range(5):
dp[i][j] += (sum(dp[i - 1]) > 0) * (dp[i - 1][j] == 0 or sum(dp[i - 1]) > 1)
if dp[-1] == [0, 0, 0, 0, 0]:
print(-1)
else:
ans = [dp[-1].index(1) + 1]
for i in range(n - 2, -1, -1):
for j in range(5):
if dp[i][j] > 0 and ((j + 1 > ans[-1] and arr[i] > arr[i + 1])
or (j + 1 < ans[-1] and arr[i] < arr[i + 1])
or (j + 1 != ans[-1] and arr[i] == arr[i + 1])):
ans.append(j + 1)
break
print(*reversed(ans))
``` | output | 1 | 46,186 | 20 | 92,373 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence a_1, a_2, β¦, a_n of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.
Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.
Let's denote the fingers of hand by numbers from 1 to 5. We call a fingering any sequence b_1, β¦, b_n of fingers numbers. A fingering is convenient if for all 1β€ i β€ n - 1 the following holds:
* if a_i < a_{i+1} then b_i < b_{i+1}, because otherwise Paul needs to take his hand off the keyboard to play the (i+1)-st note;
* if a_i > a_{i+1} then b_i > b_{i+1}, because of the same;
* if a_i = a_{i+1} then b_iβ b_{i+1}, because using the same finger twice in a row is dumb. Please note that there is β , not = between b_i and b_{i+1}.
Please provide any convenient fingering or find out that there is none.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) denoting the number of notes.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2β
10^5) denoting the positions of notes on the keyboard.
Output
If there is no convenient fingering, print -1. Otherwise, print n numbers b_1, b_2, β¦, b_n, each from 1 to 5, denoting a convenient fingering, separated by spaces.
Examples
Input
5
1 1 4 2 2
Output
1 4 5 4 5
Input
7
1 5 7 8 10 3 1
Output
1 2 3 4 5 4 3
Input
19
3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8
Output
1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4
Note
The third sample test is kinda "Non stop" song by Reflex. | instruction | 0 | 46,187 | 20 | 92,374 |
Tags: constructive algorithms, dp
Correct Solution:
```
n = int(input())
a = list(map(int,input().split()))
dp = [[0,1,2,3,4] for i in range(n+1)]
for i in range(2,n+1):
if a[i-1]==a[i-2]:
flag = -1
for k in range(5):
for j in range(5):
if dp[i-1][j]!=-1 and j!=k:
flag = j
break
if flag==-1:
dp[i][k] = -1
else:
dp[i][k] = j
elif a[i-1]>a[i-2]:
dp[i][0] = -1
for j in range(1,5):
flag = -1
for k in range(j):
if dp[i-1][k]!=-1:
flag = k
break
if flag==-1:
dp[i][j] = -1
else:
dp[i][j] = k
else:
dp[i][4] = -1
for j in range(4):
flag = -1
for k in range(j+1,5):
if dp[i-1][k]!=-1:
flag = k
break
if flag == -1:
dp[i][j] = -1
else:
dp[i][j] = k
# for i in dp:
# print (*i)
flag = 0
for i in range(5):
if dp[-1][i]!=-1:
k = n
j = i
ans = [i+1]
while k!=1:
# print (k,j)
ans.append(dp[k][j]+1)
j = dp[k][j]
k -= 1
print (*(ans[::-1]))
flag = 1
break
if not flag:
print (-1)
``` | output | 1 | 46,187 | 20 | 92,375 |
Provide tags and a correct Python 2 solution for this coding contest problem.
Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence a_1, a_2, β¦, a_n of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.
Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.
Let's denote the fingers of hand by numbers from 1 to 5. We call a fingering any sequence b_1, β¦, b_n of fingers numbers. A fingering is convenient if for all 1β€ i β€ n - 1 the following holds:
* if a_i < a_{i+1} then b_i < b_{i+1}, because otherwise Paul needs to take his hand off the keyboard to play the (i+1)-st note;
* if a_i > a_{i+1} then b_i > b_{i+1}, because of the same;
* if a_i = a_{i+1} then b_iβ b_{i+1}, because using the same finger twice in a row is dumb. Please note that there is β , not = between b_i and b_{i+1}.
Please provide any convenient fingering or find out that there is none.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) denoting the number of notes.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2β
10^5) denoting the positions of notes on the keyboard.
Output
If there is no convenient fingering, print -1. Otherwise, print n numbers b_1, b_2, β¦, b_n, each from 1 to 5, denoting a convenient fingering, separated by spaces.
Examples
Input
5
1 1 4 2 2
Output
1 4 5 4 5
Input
7
1 5 7 8 10 3 1
Output
1 2 3 4 5 4 3
Input
19
3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8
Output
1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4
Note
The third sample test is kinda "Non stop" song by Reflex. | instruction | 0 | 46,188 | 20 | 92,376 |
Tags: constructive algorithms, dp
Correct Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
def in_arr():
return map(int,raw_input().split())
def pr_num(n):
stdout.write(str(n)+'\n')
def pr_arr(arr):
for i in arr:
stdout.write(str(i)+' ')
stdout.write('\n')
range = xrange # not for python 3.0+
# main code
n=int(raw_input())
l=in_arr()
dp=[[0 for i in range(5)] for j in range(n)]
prev=[[-1 for i in range(5)] for j in range(n)]
for i in range(5):
dp[0][i]=1
for i in range(1,n):
if l[i]>l[i-1]:
for j in range(5):
f=0
for k in range(j):
if dp[i-1][k]:
f=1
prev[i][j]=k
break
dp[i][j]=f
elif l[i]<l[i-1]:
for j in range(5):
f=0
for k in range(j+1,5):
if dp[i-1][k]:
f=1
prev[i][j]=k
break
dp[i][j]=f
else:
for j in range(5):
f=0
for k in range(5):
if j!=k and dp[i-1][k]:
f=1
prev[i][j]=k
break
dp[i][j]=f
"""
for i in range(n):
for j in range(5):
print dp[i][j],
print
#"""
ans=[]
if 1:
for num in range(5):
if dp[-1][num]:
#print num
ans=[0]*n
curr=num
for i in range(n-1,-1,-1):
#print curr,prev[i][curr]
ans[i]=curr+1
curr=prev[i][curr]
pr_arr(ans)
exit()
print -1
``` | output | 1 | 46,188 | 20 | 92,377 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence a_1, a_2, β¦, a_n of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.
Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.
Let's denote the fingers of hand by numbers from 1 to 5. We call a fingering any sequence b_1, β¦, b_n of fingers numbers. A fingering is convenient if for all 1β€ i β€ n - 1 the following holds:
* if a_i < a_{i+1} then b_i < b_{i+1}, because otherwise Paul needs to take his hand off the keyboard to play the (i+1)-st note;
* if a_i > a_{i+1} then b_i > b_{i+1}, because of the same;
* if a_i = a_{i+1} then b_iβ b_{i+1}, because using the same finger twice in a row is dumb. Please note that there is β , not = between b_i and b_{i+1}.
Please provide any convenient fingering or find out that there is none.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) denoting the number of notes.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2β
10^5) denoting the positions of notes on the keyboard.
Output
If there is no convenient fingering, print -1. Otherwise, print n numbers b_1, b_2, β¦, b_n, each from 1 to 5, denoting a convenient fingering, separated by spaces.
Examples
Input
5
1 1 4 2 2
Output
1 4 5 4 5
Input
7
1 5 7 8 10 3 1
Output
1 2 3 4 5 4 3
Input
19
3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8
Output
1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4
Note
The third sample test is kinda "Non stop" song by Reflex.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
res = []
if n == 1:
print(1)
exit(0)
i = 0
if a[0] < a[1]:
if i >= n - 2:
res = [1]
cur = 2
else:
if a[i + 1] < a[i + 2]:
res = [1]
cur = 2
elif a[i + 1] > a[i + 2]:
res = [1]
cur = 5
else:
res = [1]
cur = 2
elif a[0] > a[1]:
if i >= n - 2:
res = [5]
cur = 4
else:
if a[i + 1] < a[i + 2]:
res = [5]
cur = 1
elif a[i + 1] > a[i + 2]:
res = [5]
cur = 4
else:
res = [5]
cur = 4
else:
if i >= n - 2:
res.append(1)
cur = 2
else:
if a[i + 1] < a[i + 2]:
res.append(2)
cur = 1
elif a[i + 1] > a[i + 2]:
res.append(4)
cur = 5
else:
res.append(2)
cur = 3
for i in range(1, n - 1):
if not (1 <= cur <= 5):
print(-1)
exit(0)
res.append(cur)
if a[i] > a[i + 1]:
if i >= n - 2:
cur -= 1
else:
if a[i + 1] < a[i + 2]:
cur = min(cur - 1, 1)
elif a[i + 1] > a[i + 2]:
cur -= 1
else:
cur -= 1
elif a[i] < a[i + 1]:
if i >= n - 2:
cur += 1
else:
if a[i + 1] < a[i + 2]:
cur += 1
elif a[i + 1] > a[i + 2]:
cur = max(cur + 1, 5)
else:
cur += 1
else:
if i >= n - 2:
if cur != 3:
cur = 3
else:
cur = 2
else:
if a[i + 1] < a[i + 2]:
if cur == 1:
cur = 2
else:
cur = 1
elif a[i + 1] > a[i + 2]:
if cur == 5:
cur = 4
else:
cur = 5
else:
if cur != 3:
cur = 3
else:
cur = 2
if not (1 <= cur <= 5):
print(-1)
exit(0)
res.append(cur)
print(*res)
``` | instruction | 0 | 46,189 | 20 | 92,378 |
Yes | output | 1 | 46,189 | 20 | 92,379 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence a_1, a_2, β¦, a_n of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.
Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.
Let's denote the fingers of hand by numbers from 1 to 5. We call a fingering any sequence b_1, β¦, b_n of fingers numbers. A fingering is convenient if for all 1β€ i β€ n - 1 the following holds:
* if a_i < a_{i+1} then b_i < b_{i+1}, because otherwise Paul needs to take his hand off the keyboard to play the (i+1)-st note;
* if a_i > a_{i+1} then b_i > b_{i+1}, because of the same;
* if a_i = a_{i+1} then b_iβ b_{i+1}, because using the same finger twice in a row is dumb. Please note that there is β , not = between b_i and b_{i+1}.
Please provide any convenient fingering or find out that there is none.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) denoting the number of notes.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2β
10^5) denoting the positions of notes on the keyboard.
Output
If there is no convenient fingering, print -1. Otherwise, print n numbers b_1, b_2, β¦, b_n, each from 1 to 5, denoting a convenient fingering, separated by spaces.
Examples
Input
5
1 1 4 2 2
Output
1 4 5 4 5
Input
7
1 5 7 8 10 3 1
Output
1 2 3 4 5 4 3
Input
19
3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8
Output
1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4
Note
The third sample test is kinda "Non stop" song by Reflex.
Submitted Solution:
```
import sys
from time import *
n = int(input())
A = list(map(int, sys.stdin.readline().split()))
Cand = [[i for i in range(1, 6)]]
for i in range(1, len(A)):
if Cand[-1] == []:
break
if A[i] > A[i - 1]:
t = []
for k in range(Cand[-1][0] + 1, 6):
t += [k]
Cand += [t]
elif A[i] < A[i - 1]:
t = []
for k in range(1, Cand[-1][-1]):
t += [k]
Cand += [t]
else:
t = []
if len(Cand[-1]) == 1:
for k in range(1, 6):
if k != Cand[-1][-1]:
t += [k]
else:
t = [k for k in range(1, 6)]
Cand += [t]
if Cand[-1] == []:
print(-1)
else:
ans = [Cand[-1][0]]
for i in range(len(Cand) - 2, -1, -1):
if A[i] < A[i + 1]:
for j in range(1, ans[-1]):
if j in Cand[i]:
ans += [j]
break
elif A[i] > A[i + 1]:
for j in range(ans[-1] + 1, 6):
if j in Cand[i]:
ans += [j]
break
else:
for j in range(1, 6):
if j != ans[-1] and j in Cand[i]:
ans += [j]
break
for i in range(len(ans) - 1, -1, -1):
print(ans[i], end= ' ')
``` | instruction | 0 | 46,190 | 20 | 92,380 |
Yes | output | 1 | 46,190 | 20 | 92,381 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence a_1, a_2, β¦, a_n of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.
Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.
Let's denote the fingers of hand by numbers from 1 to 5. We call a fingering any sequence b_1, β¦, b_n of fingers numbers. A fingering is convenient if for all 1β€ i β€ n - 1 the following holds:
* if a_i < a_{i+1} then b_i < b_{i+1}, because otherwise Paul needs to take his hand off the keyboard to play the (i+1)-st note;
* if a_i > a_{i+1} then b_i > b_{i+1}, because of the same;
* if a_i = a_{i+1} then b_iβ b_{i+1}, because using the same finger twice in a row is dumb. Please note that there is β , not = between b_i and b_{i+1}.
Please provide any convenient fingering or find out that there is none.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) denoting the number of notes.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2β
10^5) denoting the positions of notes on the keyboard.
Output
If there is no convenient fingering, print -1. Otherwise, print n numbers b_1, b_2, β¦, b_n, each from 1 to 5, denoting a convenient fingering, separated by spaces.
Examples
Input
5
1 1 4 2 2
Output
1 4 5 4 5
Input
7
1 5 7 8 10 3 1
Output
1 2 3 4 5 4 3
Input
19
3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8
Output
1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4
Note
The third sample test is kinda "Non stop" song by Reflex.
Submitted Solution:
```
n = int(input())
melody = [int(x) for x in input().split()]
ref = [[-1] * 5 for _ in range(n)]
can_finish = [[False] * 5 for _ in range(n)]
can_finish[0] = [True] * 5
for idx, key in enumerate(melody[:-1]):
if not any(can_finish[idx]):
break
for finger in range(5):
if melody[idx] < melody[idx + 1] and can_finish[idx][finger]:
for i in range(finger + 1, 5):
can_finish[idx + 1][i] = True
ref[idx + 1][i] = finger
break
elif melody[idx] > melody[idx + 1] and can_finish[idx][finger] and finger > 0:
for i in range(finger):
can_finish[idx + 1][i] = True
ref[idx + 1][i] = finger
elif melody[idx] == melody[idx + 1] and can_finish[idx][finger]:
tmp_val, tmp_ref = can_finish[idx + 1][finger], ref[idx + 1][finger]
can_finish[idx + 1] = [True] * 5
ref[idx + 1] = [finger] * 5
can_finish[idx + 1][finger], ref[idx + 1][finger] = tmp_val, tmp_ref
finger = next((i for i in range(5) if can_finish[n - 1][i]), None)
if finger is None:
print(-1)
else:
seq = [finger]
for i in range(n - 1, 0, -1):
finger = ref[i][finger]
seq.append(finger)
print(' '.join(str(x + 1) for x in seq[::-1]))
``` | instruction | 0 | 46,191 | 20 | 92,382 |
Yes | output | 1 | 46,191 | 20 | 92,383 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence a_1, a_2, β¦, a_n of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.
Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.
Let's denote the fingers of hand by numbers from 1 to 5. We call a fingering any sequence b_1, β¦, b_n of fingers numbers. A fingering is convenient if for all 1β€ i β€ n - 1 the following holds:
* if a_i < a_{i+1} then b_i < b_{i+1}, because otherwise Paul needs to take his hand off the keyboard to play the (i+1)-st note;
* if a_i > a_{i+1} then b_i > b_{i+1}, because of the same;
* if a_i = a_{i+1} then b_iβ b_{i+1}, because using the same finger twice in a row is dumb. Please note that there is β , not = between b_i and b_{i+1}.
Please provide any convenient fingering or find out that there is none.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) denoting the number of notes.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2β
10^5) denoting the positions of notes on the keyboard.
Output
If there is no convenient fingering, print -1. Otherwise, print n numbers b_1, b_2, β¦, b_n, each from 1 to 5, denoting a convenient fingering, separated by spaces.
Examples
Input
5
1 1 4 2 2
Output
1 4 5 4 5
Input
7
1 5 7 8 10 3 1
Output
1 2 3 4 5 4 3
Input
19
3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8
Output
1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4
Note
The third sample test is kinda "Non stop" song by Reflex.
Submitted Solution:
```
n = int(input())
u = list(map(int, input().split()))
d = []
for i in range(5):
d.append([0] * n)
for i in range(5):
d[i][0] = 1
for i in range(1, n):
if u[i] == u[i - 1]:
s1 = 0
for j in range(5):
if d[j][i - 1] == 1:
s1 += 1
if s1 > 1:
for j in range(5):
d[j][i] = 1
else:
for j in range(5):
if d[j][i - 1] != 1:
d[j][i] = 1
elif u[i] < u[i - 1]:
k = 4
while d[k][i - 1] == 0:
k -= 1
for j in range(k):
d[j][i] = 1
else:
k = 0
while d[k][i - 1] == 0:
k += 1
for j in range(k + 1, 5):
d[j][i] = 1
ok = True
for j in range(5):
if d[j][i] == 1:
ok = False
break
if ok:
print(-1)
exit()
k = 4
while d[k][-1] == 0:
k -= 1
ans = [k + 1]
for i in range(n - 1, 0, -1):
if u[i] == u[i - 1]:
for j in range(5):
if d[j][i - 1] == 1 and j != k:
k = j
break
elif u[i] > u[i - 1]:
for j in range(k):
if d[j][i - 1] == 1:
k = j
break
else:
for j in range(k + 1, 5):
if d[j][i - 1] == 1:
k = j
break
ans.append(k + 1)
ans.reverse()
print(' '.join(map(str, ans)))
``` | instruction | 0 | 46,192 | 20 | 92,384 |
Yes | output | 1 | 46,192 | 20 | 92,385 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence a_1, a_2, β¦, a_n of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.
Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.
Let's denote the fingers of hand by numbers from 1 to 5. We call a fingering any sequence b_1, β¦, b_n of fingers numbers. A fingering is convenient if for all 1β€ i β€ n - 1 the following holds:
* if a_i < a_{i+1} then b_i < b_{i+1}, because otherwise Paul needs to take his hand off the keyboard to play the (i+1)-st note;
* if a_i > a_{i+1} then b_i > b_{i+1}, because of the same;
* if a_i = a_{i+1} then b_iβ b_{i+1}, because using the same finger twice in a row is dumb. Please note that there is β , not = between b_i and b_{i+1}.
Please provide any convenient fingering or find out that there is none.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) denoting the number of notes.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2β
10^5) denoting the positions of notes on the keyboard.
Output
If there is no convenient fingering, print -1. Otherwise, print n numbers b_1, b_2, β¦, b_n, each from 1 to 5, denoting a convenient fingering, separated by spaces.
Examples
Input
5
1 1 4 2 2
Output
1 4 5 4 5
Input
7
1 5 7 8 10 3 1
Output
1 2 3 4 5 4 3
Input
19
3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8
Output
1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4
Note
The third sample test is kinda "Non stop" song by Reflex.
Submitted Solution:
```
n = int(input())
arr = list(map(int, input().split()))
dp = [[0] * 5 for i in range(n)]
dp[0] = [1, 1, 1, 1, 1]
for i in range(1, n):
if arr[i] > arr[i - 1]:
for j in range(1, 5):
dp[i][j] = max(dp[i][j - 1], dp[i - 1][j - 1])
elif arr[i] < arr[i - 1]:
for j in range(4):
dp[i][j] = max(dp[i - 1][j + 1], dp[i][j + 1])
else:
for j in range(5):
dp[i][j] += (sum(dp[i - 1]) > 0) * (dp[i - 1][j] == 0 or sum(dp[i - 1]) > 1)
if dp[-1] == [0, 0, 0, 0, 0]:
print(-1)
else:
ans = [dp[-1].index(1) + 1]
for i in range(n - 2, -1, -1):
for j in range(5):
if dp[i][j] > 0 and ((j >= ans[-1] and arr[i] > arr[i + 1])
or (j <= ans[-1] and arr[i] < arr[i + 1])
or (j + 1 != ans[-1] and arr[i] == arr[i + 1])):
ans.append(j + 1)
break
print(*reversed(ans))
``` | instruction | 0 | 46,193 | 20 | 92,386 |
No | output | 1 | 46,193 | 20 | 92,387 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence a_1, a_2, β¦, a_n of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.
Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.
Let's denote the fingers of hand by numbers from 1 to 5. We call a fingering any sequence b_1, β¦, b_n of fingers numbers. A fingering is convenient if for all 1β€ i β€ n - 1 the following holds:
* if a_i < a_{i+1} then b_i < b_{i+1}, because otherwise Paul needs to take his hand off the keyboard to play the (i+1)-st note;
* if a_i > a_{i+1} then b_i > b_{i+1}, because of the same;
* if a_i = a_{i+1} then b_iβ b_{i+1}, because using the same finger twice in a row is dumb. Please note that there is β , not = between b_i and b_{i+1}.
Please provide any convenient fingering or find out that there is none.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) denoting the number of notes.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2β
10^5) denoting the positions of notes on the keyboard.
Output
If there is no convenient fingering, print -1. Otherwise, print n numbers b_1, b_2, β¦, b_n, each from 1 to 5, denoting a convenient fingering, separated by spaces.
Examples
Input
5
1 1 4 2 2
Output
1 4 5 4 5
Input
7
1 5 7 8 10 3 1
Output
1 2 3 4 5 4 3
Input
19
3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8
Output
1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4
Note
The third sample test is kinda "Non stop" song by Reflex.
Submitted Solution:
```
n=int(input())
a=list(map(int, input().split()))
x=0
c=dict()
b=[0]
for i in range(len(a)):
c[a[i]]=0
for i in range(1, n):
if a[i]>a[i-1]:
x+=1
elif c[a[i]]==0:
x-=1
c[a[i]]=1
else:
x+=1
c[a[i]]=0
b.append(x)
k=min(b)
for i in range(len(b)):
b[i]-=k-1
if max(b)-min(b)>=5:
print(-1)
else:
print(*b)
``` | instruction | 0 | 46,194 | 20 | 92,388 |
No | output | 1 | 46,194 | 20 | 92,389 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence a_1, a_2, β¦, a_n of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.
Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.
Let's denote the fingers of hand by numbers from 1 to 5. We call a fingering any sequence b_1, β¦, b_n of fingers numbers. A fingering is convenient if for all 1β€ i β€ n - 1 the following holds:
* if a_i < a_{i+1} then b_i < b_{i+1}, because otherwise Paul needs to take his hand off the keyboard to play the (i+1)-st note;
* if a_i > a_{i+1} then b_i > b_{i+1}, because of the same;
* if a_i = a_{i+1} then b_iβ b_{i+1}, because using the same finger twice in a row is dumb. Please note that there is β , not = between b_i and b_{i+1}.
Please provide any convenient fingering or find out that there is none.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) denoting the number of notes.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2β
10^5) denoting the positions of notes on the keyboard.
Output
If there is no convenient fingering, print -1. Otherwise, print n numbers b_1, b_2, β¦, b_n, each from 1 to 5, denoting a convenient fingering, separated by spaces.
Examples
Input
5
1 1 4 2 2
Output
1 4 5 4 5
Input
7
1 5 7 8 10 3 1
Output
1 2 3 4 5 4 3
Input
19
3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8
Output
1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4
Note
The third sample test is kinda "Non stop" song by Reflex.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
nxt = [1] * n
inc = 1
des = 1
nxt[-1] = -1
for i in range(1, n):
if a[i] > a[i - 1]:
inc += 1
else:
inc = 1
nxt[i - 1] = -1
if a[i] < a[i - 1]:
des += 1
else:
des = 1
if inc > 5 or des > 5:
print(-1)
exit(0)
if nxt[0] == 1:
lst = 1
else:
lst = 5
print(lst, end=' ')
for i in range(1, n):
if a[i] > a[i - 1]:
if nxt[i] == 1:
lst += 1
else:
lst = 5
elif a[i] < a[i - 1]:
if nxt[i] == 1:
lst = 1
else:
lst -= 1
else:
if nxt[i] == 1:
if lst != 1:
lst = 1
else:
lst = 2
else:
if lst != 5:
lst = 5
else:
lst = 4
print(lst, end=' ')
``` | instruction | 0 | 46,195 | 20 | 92,390 |
No | output | 1 | 46,195 | 20 | 92,391 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence a_1, a_2, β¦, a_n of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.
Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.
Let's denote the fingers of hand by numbers from 1 to 5. We call a fingering any sequence b_1, β¦, b_n of fingers numbers. A fingering is convenient if for all 1β€ i β€ n - 1 the following holds:
* if a_i < a_{i+1} then b_i < b_{i+1}, because otherwise Paul needs to take his hand off the keyboard to play the (i+1)-st note;
* if a_i > a_{i+1} then b_i > b_{i+1}, because of the same;
* if a_i = a_{i+1} then b_iβ b_{i+1}, because using the same finger twice in a row is dumb. Please note that there is β , not = between b_i and b_{i+1}.
Please provide any convenient fingering or find out that there is none.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) denoting the number of notes.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2β
10^5) denoting the positions of notes on the keyboard.
Output
If there is no convenient fingering, print -1. Otherwise, print n numbers b_1, b_2, β¦, b_n, each from 1 to 5, denoting a convenient fingering, separated by spaces.
Examples
Input
5
1 1 4 2 2
Output
1 4 5 4 5
Input
7
1 5 7 8 10 3 1
Output
1 2 3 4 5 4 3
Input
19
3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8
Output
1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4
Note
The third sample test is kinda "Non stop" song by Reflex.
Submitted Solution:
```
import sys
from time import *
n = int(input())
A = list(map(int, sys.stdin.readline().split()))
Cand = [[i for i in range(1, 6)]]
for i in range(1, len(A)):
if Cand[-1] == []:
break
if A[i] > A[i - 1]:
t = []
for k in range(Cand[-1][0] + 1, 6):
t += [k]
Cand += [t]
elif A[i] < A[i - 1]:
t = []
for k in range(k, Cand[-1][-1]):
t += [k]
Cand += [t]
else:
t = []
if len(Cand[-1]) == 1:
for k in range(1, 6):
if k != Cand[-1][-1]:
t += [k]
Cand += [t]
else:
t = [k for k in range(1, 6)]
Cand += [t]
if Cand[-1] == []:
print(-1)
else:
ans = [Cand[-1][0]]
for i in range(len(Cand) - 2, -1, -1):
if A[i] < A[i + 1]:
for j in range(1, ans[-1]):
if j in Cand[i]:
ans += [j]
break
elif A[i] > A[i + 1]:
for j in range(ans[-1] + 1, 6):
if j in Cand[i]:
ans += [j]
break
else:
for j in range(1, 6):
if j != ans[-1] and j in Cand[i]:
ans += [j]
break
for i in range(len(ans) - 1, -1, -1):
print(ans[i], end= ' ')
``` | instruction | 0 | 46,196 | 20 | 92,392 |
No | output | 1 | 46,196 | 20 | 92,393 |
Evaluate the correctness of the submitted Python 2 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence a_1, a_2, β¦, a_n of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.
Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.
Let's denote the fingers of hand by numbers from 1 to 5. We call a fingering any sequence b_1, β¦, b_n of fingers numbers. A fingering is convenient if for all 1β€ i β€ n - 1 the following holds:
* if a_i < a_{i+1} then b_i < b_{i+1}, because otherwise Paul needs to take his hand off the keyboard to play the (i+1)-st note;
* if a_i > a_{i+1} then b_i > b_{i+1}, because of the same;
* if a_i = a_{i+1} then b_iβ b_{i+1}, because using the same finger twice in a row is dumb. Please note that there is β , not = between b_i and b_{i+1}.
Please provide any convenient fingering or find out that there is none.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) denoting the number of notes.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2β
10^5) denoting the positions of notes on the keyboard.
Output
If there is no convenient fingering, print -1. Otherwise, print n numbers b_1, b_2, β¦, b_n, each from 1 to 5, denoting a convenient fingering, separated by spaces.
Examples
Input
5
1 1 4 2 2
Output
1 4 5 4 5
Input
7
1 5 7 8 10 3 1
Output
1 2 3 4 5 4 3
Input
19
3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8
Output
1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4
Note
The third sample test is kinda "Non stop" song by Reflex.
Submitted Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
def in_arr():
return map(int,raw_input().split())
def pr_num(n):
stdout.write(str(n)+'\n')
def pr_arr(arr):
for i in arr:
stdout.write(str(i)+' ')
stdout.write('\n')
range = xrange # not for python 3.0+
# main code
n=int(raw_input())
l=in_arr()
dp=[[0 for i in range(5)] for j in range(n)]
for i in range(5):
dp[0][i]=1
for i in range(1,n):
if l[i]>l[i-1]:
for j in range(5):
f=0
for k in range(j):
if dp[i-1][k]:
f=1
break
dp[i][j]=f
elif l[i]<l[i-1]:
for j in range(5):
f=0
for k in range(j+1,5):
if dp[i-1][k]:
f=1
break
dp[i][j]=f
else:
for j in range(5):
f=0
for k in range(5):
if j!=k:
f=1
break
dp[i][j]=f
"""
for i in range(n):
for j in range(5):
print dp[i][j],
print
"""
ans=[]
for i in range(5):
if dp[-1][i]:
ans.append(i+1)
break
if not ans:
pr_num(-1)
else:
for i in range(n-2,-1,-1):
x=ans[0]-1
if l[i]<l[i+1]:
for j in range(x):
if dp[i][j]:
ans=[j+1]+ans
break
elif l[i]>l[i+1]:
for j in range(x+1,5):
if dp[i][j]:
ans=[j+1]+ans
break
else:
for j in range(5):
if dp[i][j] and j!=x:
ans=[j+1]+ans
break
pr_arr(ans)
``` | instruction | 0 | 46,197 | 20 | 92,394 |
No | output | 1 | 46,197 | 20 | 92,395 |
Evaluate the correctness of the submitted Python 2 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence a_1, a_2, β¦, a_n of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.
Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.
Let's denote the fingers of hand by numbers from 1 to 5. We call a fingering any sequence b_1, β¦, b_n of fingers numbers. A fingering is convenient if for all 1β€ i β€ n - 1 the following holds:
* if a_i < a_{i+1} then b_i < b_{i+1}, because otherwise Paul needs to take his hand off the keyboard to play the (i+1)-st note;
* if a_i > a_{i+1} then b_i > b_{i+1}, because of the same;
* if a_i = a_{i+1} then b_iβ b_{i+1}, because using the same finger twice in a row is dumb. Please note that there is β , not = between b_i and b_{i+1}.
Please provide any convenient fingering or find out that there is none.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) denoting the number of notes.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2β
10^5) denoting the positions of notes on the keyboard.
Output
If there is no convenient fingering, print -1. Otherwise, print n numbers b_1, b_2, β¦, b_n, each from 1 to 5, denoting a convenient fingering, separated by spaces.
Examples
Input
5
1 1 4 2 2
Output
1 4 5 4 5
Input
7
1 5 7 8 10 3 1
Output
1 2 3 4 5 4 3
Input
19
3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8
Output
1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4
Note
The third sample test is kinda "Non stop" song by Reflex.
Submitted Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
def in_arr():
return map(int,raw_input().split())
def pr_num(n):
stdout.write(str(n)+'\n')
def pr_arr(arr):
for i in arr:
stdout.write(str(i)+' ')
stdout.write('\n')
range = xrange # not for python 3.0+
# main code
n=int(raw_input())
l=in_arr()
dp=[[0 for i in range(5)] for j in range(n)]
prev=[[-1 for i in range(5)] for j in range(n)]
for i in range(5):
dp[0][i]=1
for i in range(1,n):
if l[i]>l[i-1]:
for j in range(5):
f=0
for k in range(j):
if dp[i-1][k]:
f=1
prev[i][j]=k
break
dp[i][j]=f
elif l[i]<l[i-1]:
for j in range(5):
f=0
for k in range(j+1,5):
if dp[i-1][k]:
f=1
prev[i][j]=k
break
dp[i][j]=f
else:
for j in range(5):
f=0
for k in range(5):
if j!=k:
f=1
prev[i][j]=k
break
dp[i][j]=f
"""
for i in range(n):
for j in range(5):
print dp[i][j],
print
#"""
ans=[]
if 1:
for num in range(5):
if dp[-1][num]:
#print num
ans=[0]*n
curr=num
for i in range(n-1,-1,-1):
#print curr,prev[i][curr]
ans[i]=curr+1
curr=prev[i][curr]
pr_arr(ans)
exit()
print -1
``` | instruction | 0 | 46,198 | 20 | 92,396 |
No | output | 1 | 46,198 | 20 | 92,397 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer.
For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9).
The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2.
And if p=-9 we can represent 7 as one number (2^4-9).
Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example).
Input
The only line contains two integers n and p (1 β€ n β€ 10^9, -1000 β€ p β€ 1000).
Output
If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands.
Examples
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
Note
0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0).
In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1).
In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed.
In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | instruction | 0 | 46,334 | 20 | 92,668 |
Tags: bitmasks, brute force, math
Correct Solution:
```
n, p = map(int, input().split())
for i in range(1, 1000):
x = n - i * p
b = bin(x)
if x <= 0: break
if b.count('1') <= i <= x:
print(i)
exit()
print(-1)
``` | output | 1 | 46,334 | 20 | 92,669 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer.
For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9).
The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2.
And if p=-9 we can represent 7 as one number (2^4-9).
Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example).
Input
The only line contains two integers n and p (1 β€ n β€ 10^9, -1000 β€ p β€ 1000).
Output
If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands.
Examples
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
Note
0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0).
In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1).
In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed.
In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | instruction | 0 | 46,335 | 20 | 92,670 |
Tags: bitmasks, brute force, math
Correct Solution:
```
n,p = map(int,input().split())
if n <= p:
print(-1)
exit()
for ans in range(1,10**6):
x = n-p*ans
if x >= ans and bin(x).count("1") <= ans:
print(ans)
break
else:
print(-1)
``` | output | 1 | 46,335 | 20 | 92,671 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer.
For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9).
The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2.
And if p=-9 we can represent 7 as one number (2^4-9).
Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example).
Input
The only line contains two integers n and p (1 β€ n β€ 10^9, -1000 β€ p β€ 1000).
Output
If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands.
Examples
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
Note
0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0).
In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1).
In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed.
In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | instruction | 0 | 46,336 | 20 | 92,672 |
Tags: bitmasks, brute force, math
Correct Solution:
```
def bitcount(n):
count = 0
while (n > 0):
count = count + 1
n = n & (n-1)
return count
n,p=input("").split()
n=int(n)
p=int(p)
for i in range(1,31):
m=n
m=m-(i*p)
count=bitcount(m)
if(m<=0):
print(-1)
break
elif(count==i):
print(i)
break
elif(count<i):
if(m>i):
print(i)
break
else:
print(-1)
break
``` | output | 1 | 46,336 | 20 | 92,673 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer.
For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9).
The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2.
And if p=-9 we can represent 7 as one number (2^4-9).
Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example).
Input
The only line contains two integers n and p (1 β€ n β€ 10^9, -1000 β€ p β€ 1000).
Output
If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands.
Examples
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
Note
0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0).
In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1).
In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed.
In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | instruction | 0 | 46,337 | 20 | 92,674 |
Tags: bitmasks, brute force, math
Correct Solution:
```
N, P = map(int, input().split())
def chk(k):
x = N - k*P
if x > 0 and sum(map(int, bin(x)[2:])) <= k <= x:
return 1
return 0
for i in range(1, 100):
if chk(i):
print(i)
break
else:
print(-1)
``` | output | 1 | 46,337 | 20 | 92,675 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer.
For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9).
The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2.
And if p=-9 we can represent 7 as one number (2^4-9).
Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example).
Input
The only line contains two integers n and p (1 β€ n β€ 10^9, -1000 β€ p β€ 1000).
Output
If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands.
Examples
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
Note
0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0).
In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1).
In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed.
In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | instruction | 0 | 46,338 | 20 | 92,676 |
Tags: bitmasks, brute force, math
Correct Solution:
```
n,p =map(int, input().split())
ans = -1
for i in range(40):
t = n - p*i
if t < 0:
break
cnt = 0
while t:
if t&1:
cnt += 1
t >>= 1
if n-p*i>=i and cnt <= i:
print(i)
exit()
print(ans)
``` | output | 1 | 46,338 | 20 | 92,677 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer.
For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9).
The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2.
And if p=-9 we can represent 7 as one number (2^4-9).
Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example).
Input
The only line contains two integers n and p (1 β€ n β€ 10^9, -1000 β€ p β€ 1000).
Output
If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands.
Examples
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
Note
0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0).
In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1).
In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed.
In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | instruction | 0 | 46,339 | 20 | 92,678 |
Tags: bitmasks, brute force, math
Correct Solution:
```
n, p = map(int, input().split())
def count(s):
c = 0
b = list(bin(s))
b.pop(0)
b.pop(0)
for i in b:
if i == '1':
c += 1
return c
i = 1
if p > 0:
l = n/(p+1)
while i <= l:
if count(n - i*p) <= i:
print(i)
break
else:
i += 1
else:
print('-1')
else:
i = 1
while True:
if count(n - i*p) <= i:
print(i)
break
else:
i += 1
``` | output | 1 | 46,339 | 20 | 92,679 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer.
For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9).
The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2.
And if p=-9 we can represent 7 as one number (2^4-9).
Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example).
Input
The only line contains two integers n and p (1 β€ n β€ 10^9, -1000 β€ p β€ 1000).
Output
If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands.
Examples
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
Note
0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0).
In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1).
In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed.
In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | instruction | 0 | 46,340 | 20 | 92,680 |
Tags: bitmasks, brute force, math
Correct Solution:
```
a,b=map(int,input().split())
def bn(x):
m=0
while x>0:
m+=x%2
x//=2
return m
mn=100000000
idx=0
if b==0:
print(bn(a))
exit(0)
else:
for n in range(1,100):
j=n*b
if a<=j:
break
elif bn(a-j)<=n and a-j>=n:
print(n)
exit(0)
print(-1)
``` | output | 1 | 46,340 | 20 | 92,681 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer.
For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9).
The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2.
And if p=-9 we can represent 7 as one number (2^4-9).
Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example).
Input
The only line contains two integers n and p (1 β€ n β€ 10^9, -1000 β€ p β€ 1000).
Output
If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands.
Examples
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
Note
0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0).
In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1).
In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed.
In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible. | instruction | 0 | 46,341 | 20 | 92,682 |
Tags: bitmasks, brute force, math
Correct Solution:
```
n,p=map(int,input().split())
#print("{0:b}".format(n).count('1'))
t=0
while (("{0:b}".format(n).count('1'))>t or n<t) and n>=0:
t+=1
n-=p
if n<0:
print(-1)
else:
print(t)
``` | output | 1 | 46,341 | 20 | 92,683 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer.
For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9).
The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2.
And if p=-9 we can represent 7 as one number (2^4-9).
Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example).
Input
The only line contains two integers n and p (1 β€ n β€ 10^9, -1000 β€ p β€ 1000).
Output
If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands.
Examples
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
Note
0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0).
In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1).
In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed.
In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible.
Submitted Solution:
```
def ones(n):
result = 0
while n > 0:
n = n & (n - 1)
result = result + 1
return result
def can(n, p, cnt):
nn = n - p * cnt
if nn < cnt:
return False
if ones(nn) > cnt:
return False
return True
def solve(n, p):
i = 1
while i * p <= n:
if can(n, p, i):
return i
i = i + 1
return -1
def main():
n, p = [int(s) for s in input().split()]
answer = solve(n, p)
print(answer)
if __name__ == "__main__":
main()
``` | instruction | 0 | 46,342 | 20 | 92,684 |
Yes | output | 1 | 46,342 | 20 | 92,685 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer.
For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9).
The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2.
And if p=-9 we can represent 7 as one number (2^4-9).
Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example).
Input
The only line contains two integers n and p (1 β€ n β€ 10^9, -1000 β€ p β€ 1000).
Output
If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands.
Examples
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
Note
0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0).
In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1).
In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed.
In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible.
Submitted Solution:
```
n,p=input("").split()
n=int(n)
p=int(p)
for i in range(1,31):
m=n
m=m-(i*p)
b=bin(m)
str1=str(b)
count=str1.count("1",0,len(str1))
if(m<=0):
print(-1)
break
elif(count==i):
print(i)
break
elif(count<i):
if(m>i):
print(i)
break
else:
print(-1)
break
``` | instruction | 0 | 46,343 | 20 | 92,686 |
Yes | output | 1 | 46,343 | 20 | 92,687 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer.
For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9).
The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2.
And if p=-9 we can represent 7 as one number (2^4-9).
Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example).
Input
The only line contains two integers n and p (1 β€ n β€ 10^9, -1000 β€ p β€ 1000).
Output
If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands.
Examples
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
Note
0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0).
In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1).
In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed.
In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible.
Submitted Solution:
```
n, p = map(int, input().split())
for m in range(1, 10000):
x = n - m * p
b = bin(x)
if b.count('1') <= m <= x:
print(m)
exit()
print(-1)
``` | instruction | 0 | 46,344 | 20 | 92,688 |
Yes | output | 1 | 46,344 | 20 | 92,689 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer.
For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9).
The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2.
And if p=-9 we can represent 7 as one number (2^4-9).
Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example).
Input
The only line contains two integers n and p (1 β€ n β€ 10^9, -1000 β€ p β€ 1000).
Output
If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands.
Examples
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
Note
0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0).
In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1).
In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed.
In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible.
Submitted Solution:
```
n, p = map(int, input().split())
for i in range(1, 100):
x = n - i * p
if x <= 0:
print(-1)
exit()
if bin(x).count('1') <= i <= x:
print(i)
exit()
print(-1)
exit()
``` | instruction | 0 | 46,345 | 20 | 92,690 |
Yes | output | 1 | 46,345 | 20 | 92,691 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer.
For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9).
The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2.
And if p=-9 we can represent 7 as one number (2^4-9).
Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example).
Input
The only line contains two integers n and p (1 β€ n β€ 10^9, -1000 β€ p β€ 1000).
Output
If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands.
Examples
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
Note
0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0).
In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1).
In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed.
In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible.
Submitted Solution:
```
n,p=map(int,input().split())
#print("{0:b}".format(n).count('1'))
t=0
while ("{0:b}".format(n).count('1'))>t and (n-p)>=0:
t+=1
n-=p
if n==0:
print(-1)
else:
print(t)
``` | instruction | 0 | 46,346 | 20 | 92,692 |
No | output | 1 | 46,346 | 20 | 92,693 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer.
For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9).
The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2.
And if p=-9 we can represent 7 as one number (2^4-9).
Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example).
Input
The only line contains two integers n and p (1 β€ n β€ 10^9, -1000 β€ p β€ 1000).
Output
If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands.
Examples
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
Note
0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0).
In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1).
In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed.
In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible.
Submitted Solution:
```
n,p=input("").split()
n=int(n)
p=int(p)
for i in range(0,31):
m=n
m=m-(i*p)
b=bin(m)
str1=str(b)
count=str1.count("1",0,len(str1))
if(m<=0):
print(-1)
break
elif(count<=i):
print(i)
break
``` | instruction | 0 | 46,347 | 20 | 92,694 |
No | output | 1 | 46,347 | 20 | 92,695 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer.
For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9).
The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2.
And if p=-9 we can represent 7 as one number (2^4-9).
Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example).
Input
The only line contains two integers n and p (1 β€ n β€ 10^9, -1000 β€ p β€ 1000).
Output
If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands.
Examples
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
Note
0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0).
In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1).
In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed.
In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible.
Submitted Solution:
```
n,p = map(int,input().split())
if n <= p:
print(-1)
exit()
for ans in range(1,10**6):
x = n-p*ans
if bin(x).count("1") <= ans:
print(ans)
break
``` | instruction | 0 | 46,348 | 20 | 92,696 |
No | output | 1 | 46,348 | 20 | 92,697 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer p (which may be positive, negative, or zero). To combine their tastes, they invented p-binary numbers of the form 2^x + p, where x is a non-negative integer.
For example, some -9-binary ("minus nine" binary) numbers are: -8 (minus eight), 7 and 1015 (-8=2^0-9, 7=2^4-9, 1015=2^{10}-9).
The boys now use p-binary numbers to represent everything. They now face a problem: given a positive integer n, what's the smallest number of p-binary numbers (not necessarily distinct) they need to represent n as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.
For example, if p=0 we can represent 7 as 2^0 + 2^1 + 2^2.
And if p=-9 we can represent 7 as one number (2^4-9).
Note that negative p-binary numbers are allowed to be in the sum (see the Notes section for an example).
Input
The only line contains two integers n and p (1 β€ n β€ 10^9, -1000 β€ p β€ 1000).
Output
If it is impossible to represent n as the sum of any number of p-binary numbers, print a single integer -1. Otherwise, print the smallest possible number of summands.
Examples
Input
24 0
Output
2
Input
24 1
Output
3
Input
24 -1
Output
4
Input
4 -7
Output
2
Input
1 1
Output
-1
Note
0-binary numbers are just regular binary powers, thus in the first sample case we can represent 24 = (2^4 + 0) + (2^3 + 0).
In the second sample case, we can represent 24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1).
In the third sample case, we can represent 24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1). Note that repeated summands are allowed.
In the fourth sample case, we can represent 4 = (2^4 - 7) + (2^1 - 7). Note that the second summand is negative, which is allowed.
In the fifth sample case, no representation is possible.
Submitted Solution:
```
n,p=map(int,input().split())
#print("{0:b}".format(n).count('1'))
t=0
while (("{0:b}".format(n).count('1'))>t or n<t) and (n-p)>=0:
t+=1
n-=p
if n==0:
print(-1)
else:
print(t)
``` | instruction | 0 | 46,349 | 20 | 92,698 |
No | output | 1 | 46,349 | 20 | 92,699 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has two strings a and b of the same length n. The strings consist only of lucky digits. Petya can perform operations of two types:
* replace any one digit from string a by its opposite (i.e., replace 4 by 7 and 7 by 4);
* swap any pair of digits in string a.
Petya is interested in the minimum number of operations that are needed to make string a equal to string b. Help him with the task.
Input
The first and the second line contains strings a and b, correspondingly. Strings a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105.
Output
Print on the single line the single number β the minimum number of operations needed to convert string a into string b.
Examples
Input
47
74
Output
1
Input
774
744
Output
1
Input
777
444
Output
3
Note
In the first sample it is enough simply to swap the first and the second digit.
In the second sample we should replace the second digit with its opposite.
In the third number we should replace all three digits with their opposites. | instruction | 0 | 46,480 | 20 | 92,960 |
Tags: greedy, implementation
Correct Solution:
```
a = [int(i) for i in list(input())]
b = [int(i) for i in list(input())]
c = zip(a, b)
indices = [i for i, v in enumerate(c) if v[0] != v[1]]
cnt4 = sum([a[i]==4 for i in indices])
cnt7 = len(indices) - cnt4
print(max([cnt4, cnt7]))
``` | output | 1 | 46,480 | 20 | 92,961 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has two strings a and b of the same length n. The strings consist only of lucky digits. Petya can perform operations of two types:
* replace any one digit from string a by its opposite (i.e., replace 4 by 7 and 7 by 4);
* swap any pair of digits in string a.
Petya is interested in the minimum number of operations that are needed to make string a equal to string b. Help him with the task.
Input
The first and the second line contains strings a and b, correspondingly. Strings a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105.
Output
Print on the single line the single number β the minimum number of operations needed to convert string a into string b.
Examples
Input
47
74
Output
1
Input
774
744
Output
1
Input
777
444
Output
3
Note
In the first sample it is enough simply to swap the first and the second digit.
In the second sample we should replace the second digit with its opposite.
In the third number we should replace all three digits with their opposites. | instruction | 0 | 46,481 | 20 | 92,962 |
Tags: greedy, implementation
Correct Solution:
```
import sys
read=sys.stdin.buffer.readline
mi=lambda:map(int,read().split())
li=lambda:list(mi())
cin=lambda:int(read())
a=input()
b=input()
d={'4':0,'7':0}
for i in range(len(a)):
if a[i]!=b[i]:
d[b[i]]+=1
print(max(d['4'],d['7']))
``` | output | 1 | 46,481 | 20 | 92,963 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has two strings a and b of the same length n. The strings consist only of lucky digits. Petya can perform operations of two types:
* replace any one digit from string a by its opposite (i.e., replace 4 by 7 and 7 by 4);
* swap any pair of digits in string a.
Petya is interested in the minimum number of operations that are needed to make string a equal to string b. Help him with the task.
Input
The first and the second line contains strings a and b, correspondingly. Strings a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105.
Output
Print on the single line the single number β the minimum number of operations needed to convert string a into string b.
Examples
Input
47
74
Output
1
Input
774
744
Output
1
Input
777
444
Output
3
Note
In the first sample it is enough simply to swap the first and the second digit.
In the second sample we should replace the second digit with its opposite.
In the third number we should replace all three digits with their opposites. | instruction | 0 | 46,482 | 20 | 92,964 |
Tags: greedy, implementation
Correct Solution:
```
a=str(input())
b=str(input())
a1=list(a)
b1=list(b)
k=0
for i in range(0,len(a)):
if(a1[i]!=b1[i]):
k=k+1
ka=a1.count('7')
kb=b1.count('7')
if(ka>kb):
f=(k-(ka-kb))//2
print(f+ka-kb)
else:
f=(k-(kb-ka))//2
print(f+kb-ka)
``` | output | 1 | 46,482 | 20 | 92,965 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has two strings a and b of the same length n. The strings consist only of lucky digits. Petya can perform operations of two types:
* replace any one digit from string a by its opposite (i.e., replace 4 by 7 and 7 by 4);
* swap any pair of digits in string a.
Petya is interested in the minimum number of operations that are needed to make string a equal to string b. Help him with the task.
Input
The first and the second line contains strings a and b, correspondingly. Strings a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105.
Output
Print on the single line the single number β the minimum number of operations needed to convert string a into string b.
Examples
Input
47
74
Output
1
Input
774
744
Output
1
Input
777
444
Output
3
Note
In the first sample it is enough simply to swap the first and the second digit.
In the second sample we should replace the second digit with its opposite.
In the third number we should replace all three digits with their opposites. | instruction | 0 | 46,483 | 20 | 92,966 |
Tags: greedy, implementation
Correct Solution:
```
s = input()
t = input()
len_s = len(s)
len_t = len(t)
s4 = s.count('4')
t4 = t.count('4')
swap = abs(s4-t4)
sp = ''
st = ''
if s4 > t4:
for i in s:
if i == '7':
sp += '4'
else:
sp += '7'
for i in t:
if i == '7':
st += '4'
else:
st += '7'
s = sp
t = st
pairs = [[s[i], t[i]] for i in range(len_s)]
x74 = 0
x77 = 0
x47 = 0
x44 = 0
for i in pairs:
if i == ['7', '4']:
x74 += 1
if i == ['4', '4']:
x44 += 1
if i == ['4', '7']:
x47 += 1
if i == ['7', '7']:
x77 += 1
ch = 0
if x74 >= swap:
x74 -= swap
x44 += swap
else:
x74 = 0
x44 += x74
swap -= x74
x77 -= swap
x74 += swap
print((x74+x47)//2 + swap)
``` | output | 1 | 46,483 | 20 | 92,967 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has two strings a and b of the same length n. The strings consist only of lucky digits. Petya can perform operations of two types:
* replace any one digit from string a by its opposite (i.e., replace 4 by 7 and 7 by 4);
* swap any pair of digits in string a.
Petya is interested in the minimum number of operations that are needed to make string a equal to string b. Help him with the task.
Input
The first and the second line contains strings a and b, correspondingly. Strings a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105.
Output
Print on the single line the single number β the minimum number of operations needed to convert string a into string b.
Examples
Input
47
74
Output
1
Input
774
744
Output
1
Input
777
444
Output
3
Note
In the first sample it is enough simply to swap the first and the second digit.
In the second sample we should replace the second digit with its opposite.
In the third number we should replace all three digits with their opposites. | instruction | 0 | 46,484 | 20 | 92,968 |
Tags: greedy, implementation
Correct Solution:
```
a = input()
b = input()
a7,a4 = a.count('7'),a.count('4')
b7,b4 = b.count('7'),b.count('4')
c = 0
for i in range(len(a)):
if a[i]!=b[i]:
c += 1
if a7==b7 and a4==b4:
if c%2:
print(c//2+1)
else:
print(c//2)
else:
if b7>a7:
d = b7-a7
else:
d = b4-a4
tot = d
c -= d
if c%2:
print(tot+c//2+1)
else:
print(tot+c//2)
``` | output | 1 | 46,484 | 20 | 92,969 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has two strings a and b of the same length n. The strings consist only of lucky digits. Petya can perform operations of two types:
* replace any one digit from string a by its opposite (i.e., replace 4 by 7 and 7 by 4);
* swap any pair of digits in string a.
Petya is interested in the minimum number of operations that are needed to make string a equal to string b. Help him with the task.
Input
The first and the second line contains strings a and b, correspondingly. Strings a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105.
Output
Print on the single line the single number β the minimum number of operations needed to convert string a into string b.
Examples
Input
47
74
Output
1
Input
774
744
Output
1
Input
777
444
Output
3
Note
In the first sample it is enough simply to swap the first and the second digit.
In the second sample we should replace the second digit with its opposite.
In the third number we should replace all three digits with their opposites. | instruction | 0 | 46,485 | 20 | 92,970 |
Tags: greedy, implementation
Correct Solution:
```
x = input("")
y = input("")
count = 0
count1 = 0
if x == y:
print(0)
else:
for i in range(len(x)):
if(x[i] == '4' and y[i] == '7'):
count+=1
elif (x[i] == '7' and y[i] == '4'):
count1+=1
m = max(count,count1)
print(m)
``` | output | 1 | 46,485 | 20 | 92,971 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has two strings a and b of the same length n. The strings consist only of lucky digits. Petya can perform operations of two types:
* replace any one digit from string a by its opposite (i.e., replace 4 by 7 and 7 by 4);
* swap any pair of digits in string a.
Petya is interested in the minimum number of operations that are needed to make string a equal to string b. Help him with the task.
Input
The first and the second line contains strings a and b, correspondingly. Strings a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105.
Output
Print on the single line the single number β the minimum number of operations needed to convert string a into string b.
Examples
Input
47
74
Output
1
Input
774
744
Output
1
Input
777
444
Output
3
Note
In the first sample it is enough simply to swap the first and the second digit.
In the second sample we should replace the second digit with its opposite.
In the third number we should replace all three digits with their opposites. | instruction | 0 | 46,486 | 20 | 92,972 |
Tags: greedy, implementation
Correct Solution:
```
a=list(input())
t=list(input())
cnta=0
cntb=0
for i in range(len(a)):
if a[i]=='7' and t[i]=='4':
cnta+=1
if a[i]=='4' and t[i]=='7':
cntb+=1
print(max(cnta,cntb))
``` | output | 1 | 46,486 | 20 | 92,973 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has two strings a and b of the same length n. The strings consist only of lucky digits. Petya can perform operations of two types:
* replace any one digit from string a by its opposite (i.e., replace 4 by 7 and 7 by 4);
* swap any pair of digits in string a.
Petya is interested in the minimum number of operations that are needed to make string a equal to string b. Help him with the task.
Input
The first and the second line contains strings a and b, correspondingly. Strings a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105.
Output
Print on the single line the single number β the minimum number of operations needed to convert string a into string b.
Examples
Input
47
74
Output
1
Input
774
744
Output
1
Input
777
444
Output
3
Note
In the first sample it is enough simply to swap the first and the second digit.
In the second sample we should replace the second digit with its opposite.
In the third number we should replace all three digits with their opposites. | instruction | 0 | 46,487 | 20 | 92,974 |
Tags: greedy, implementation
Correct Solution:
```
x=input()
y=input()
d=abs(x.count("4")-y.count("4"))
c=0
for i in range(len(x)):
if x[i]!=y[i]:
c+=1
c-=d
print(c//2+d)
``` | output | 1 | 46,487 | 20 | 92,975 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has two strings a and b of the same length n. The strings consist only of lucky digits. Petya can perform operations of two types:
* replace any one digit from string a by its opposite (i.e., replace 4 by 7 and 7 by 4);
* swap any pair of digits in string a.
Petya is interested in the minimum number of operations that are needed to make string a equal to string b. Help him with the task.
Input
The first and the second line contains strings a and b, correspondingly. Strings a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105.
Output
Print on the single line the single number β the minimum number of operations needed to convert string a into string b.
Examples
Input
47
74
Output
1
Input
774
744
Output
1
Input
777
444
Output
3
Note
In the first sample it is enough simply to swap the first and the second digit.
In the second sample we should replace the second digit with its opposite.
In the third number we should replace all three digits with their opposites.
Submitted Solution:
```
a=input()
b=input()
fou=0; sev=0
for i in range(len(a)):
if a[i]!=b[i]:
if a[i]=='4': fou+=1
else: sev+=1
print(max(fou,sev))
``` | instruction | 0 | 46,488 | 20 | 92,976 |
Yes | output | 1 | 46,488 | 20 | 92,977 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has two strings a and b of the same length n. The strings consist only of lucky digits. Petya can perform operations of two types:
* replace any one digit from string a by its opposite (i.e., replace 4 by 7 and 7 by 4);
* swap any pair of digits in string a.
Petya is interested in the minimum number of operations that are needed to make string a equal to string b. Help him with the task.
Input
The first and the second line contains strings a and b, correspondingly. Strings a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105.
Output
Print on the single line the single number β the minimum number of operations needed to convert string a into string b.
Examples
Input
47
74
Output
1
Input
774
744
Output
1
Input
777
444
Output
3
Note
In the first sample it is enough simply to swap the first and the second digit.
In the second sample we should replace the second digit with its opposite.
In the third number we should replace all three digits with their opposites.
Submitted Solution:
```
s=input()
s1=input()
k1=0
k=0
for i in range(len(s)) :
if s[i]!=s1[i] :
if s[i]=="4" :
k+=1
else :
k1+=1
print(max(k,k1))
``` | instruction | 0 | 46,489 | 20 | 92,978 |
Yes | output | 1 | 46,489 | 20 | 92,979 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has two strings a and b of the same length n. The strings consist only of lucky digits. Petya can perform operations of two types:
* replace any one digit from string a by its opposite (i.e., replace 4 by 7 and 7 by 4);
* swap any pair of digits in string a.
Petya is interested in the minimum number of operations that are needed to make string a equal to string b. Help him with the task.
Input
The first and the second line contains strings a and b, correspondingly. Strings a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105.
Output
Print on the single line the single number β the minimum number of operations needed to convert string a into string b.
Examples
Input
47
74
Output
1
Input
774
744
Output
1
Input
777
444
Output
3
Note
In the first sample it is enough simply to swap the first and the second digit.
In the second sample we should replace the second digit with its opposite.
In the third number we should replace all three digits with their opposites.
Submitted Solution:
```
a, b = input(), input()
cnt4 = sum([a[i] == '4' and b[i] == '7' for i in range(len(a))])
cnt7 = sum([a[i] == '7' and b[i] == '4' for i in range(len(a))])
print(cnt4 + cnt7 - min(cnt4, cnt7))
``` | instruction | 0 | 46,490 | 20 | 92,980 |
Yes | output | 1 | 46,490 | 20 | 92,981 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has two strings a and b of the same length n. The strings consist only of lucky digits. Petya can perform operations of two types:
* replace any one digit from string a by its opposite (i.e., replace 4 by 7 and 7 by 4);
* swap any pair of digits in string a.
Petya is interested in the minimum number of operations that are needed to make string a equal to string b. Help him with the task.
Input
The first and the second line contains strings a and b, correspondingly. Strings a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105.
Output
Print on the single line the single number β the minimum number of operations needed to convert string a into string b.
Examples
Input
47
74
Output
1
Input
774
744
Output
1
Input
777
444
Output
3
Note
In the first sample it is enough simply to swap the first and the second digit.
In the second sample we should replace the second digit with its opposite.
In the third number we should replace all three digits with their opposites.
Submitted Solution:
```
z = list(map(''.join, zip(input(), input())))
f, s = z.count('47'), z.count('74')
print(f + s - min(f, s))
``` | instruction | 0 | 46,491 | 20 | 92,982 |
Yes | output | 1 | 46,491 | 20 | 92,983 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has two strings a and b of the same length n. The strings consist only of lucky digits. Petya can perform operations of two types:
* replace any one digit from string a by its opposite (i.e., replace 4 by 7 and 7 by 4);
* swap any pair of digits in string a.
Petya is interested in the minimum number of operations that are needed to make string a equal to string b. Help him with the task.
Input
The first and the second line contains strings a and b, correspondingly. Strings a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105.
Output
Print on the single line the single number β the minimum number of operations needed to convert string a into string b.
Examples
Input
47
74
Output
1
Input
774
744
Output
1
Input
777
444
Output
3
Note
In the first sample it is enough simply to swap the first and the second digit.
In the second sample we should replace the second digit with its opposite.
In the third number we should replace all three digits with their opposites.
Submitted Solution:
```
a=str(input())
b=str(input())
p=''
q=''
if (a==b):
print("0")
else:
l=[]
u=a.count('4')
x=b.count('4')
v=a.count('7')
y=b.count('7')
l=[u,v,x,y]
l1=[u,v]
l2=[x,y]
w=min(l)
if (u==0 and y==0) or (v==0 and x==0):
print(max(l1))
elif w in l1:
print(w+max(l1)-max(l2))
else:
print(w+max(l2)-max(l1))
``` | instruction | 0 | 46,492 | 20 | 92,984 |
No | output | 1 | 46,492 | 20 | 92,985 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has two strings a and b of the same length n. The strings consist only of lucky digits. Petya can perform operations of two types:
* replace any one digit from string a by its opposite (i.e., replace 4 by 7 and 7 by 4);
* swap any pair of digits in string a.
Petya is interested in the minimum number of operations that are needed to make string a equal to string b. Help him with the task.
Input
The first and the second line contains strings a and b, correspondingly. Strings a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105.
Output
Print on the single line the single number β the minimum number of operations needed to convert string a into string b.
Examples
Input
47
74
Output
1
Input
774
744
Output
1
Input
777
444
Output
3
Note
In the first sample it is enough simply to swap the first and the second digit.
In the second sample we should replace the second digit with its opposite.
In the third number we should replace all three digits with their opposites.
Submitted Solution:
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 27 18:54:50 2019
@author: pallavi
"""
a = input()
b = input()
if(a==b):
print(0)
else:
a1 = list(a)
b1 = list(b)
l = len(a1)
i=0
c=0
f1=0
while(i<l):
if(a1[i]!=b1[i]):
if(b1[i]=='4'):
a1[i] = '4'
c=c+1
if(b1[i]=='7'):
a1[i] = '7'
c=c+1
i=i+1
if(a1==b1):
f1 = 1
#print(c)
d=0
i=0
j=0
a1 = list(a)
b1 = list(b)
while(i<l):
while(j<l):
if(a[i]!=a[j]):
t = a1[i]
a1[i] = a1[j]
a1[j] = t
d=d+1
if(a1[i]!=b1[i] or a1[j]!=b1[j]):
t = a1[i]
a1[i] = a1[j]
a1[j] = t
d=d-1
j=j+1
i=i+1
f2 = 0
if(a1==b1):
f2=1
if(f1==1 and f2==1):
if(c<d):
print(c)
else:
print(d)
if(f1==1 and f2==0):
print(c)
if(f1==0 and f2==1):
print(d)
``` | instruction | 0 | 46,493 | 20 | 92,986 |
No | output | 1 | 46,493 | 20 | 92,987 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has two strings a and b of the same length n. The strings consist only of lucky digits. Petya can perform operations of two types:
* replace any one digit from string a by its opposite (i.e., replace 4 by 7 and 7 by 4);
* swap any pair of digits in string a.
Petya is interested in the minimum number of operations that are needed to make string a equal to string b. Help him with the task.
Input
The first and the second line contains strings a and b, correspondingly. Strings a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105.
Output
Print on the single line the single number β the minimum number of operations needed to convert string a into string b.
Examples
Input
47
74
Output
1
Input
774
744
Output
1
Input
777
444
Output
3
Note
In the first sample it is enough simply to swap the first and the second digit.
In the second sample we should replace the second digit with its opposite.
In the third number we should replace all three digits with their opposites.
Submitted Solution:
```
string1 = input()
string2 = input()
cnt = 0
from math import ceil
for i in range(len(string2)):
cnt += string1[i] != string2[i]
print(ceil(cnt / 2))
``` | instruction | 0 | 46,494 | 20 | 92,988 |
No | output | 1 | 46,494 | 20 | 92,989 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.