message stringlengths 2 65.1k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 0 108k | cluster float64 14 14 | __index_level_0__ int64 0 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n different bloggers. Blogger numbered i has a_i followers.
Since Masha has a limited budget, she can only sign a contract with k different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.
Help her, find the number of ways to select k bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).
For example, if n=4, k=3, a=[1, 3, 1, 2], then Masha has two ways to select 3 bloggers with the maximum total number of followers:
* conclude contracts with bloggers with numbers 1, 2 and 4. In this case, the number of followers will be equal to a_1 + a_2 + a_4 = 6.
* conclude contracts with bloggers with numbers 2, 3 and 4. In this case, the number of followers will be equal to a_2 + a_3 + a_4 = 6.
Since the answer can be quite large, output it modulo 10^9+7.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 1000) β the number of bloggers and how many of them you can sign a contract with.
The second line of each test case contains n integers a_1, a_2, β¦ a_n (1 β€ a_i β€ n) β the number of followers of each blogger.
It is guaranteed that the sum of n over all test cases does not exceed 1000.
Output
For each test case, on a separate line output one integer β the number of ways to select k bloggers so that the total number of their followers is maximum possible.
Example
Input
3
4 3
1 3 1 2
4 2
1 1 1 1
2 1
1 2
Output
2
6
1
Note
The test case is explained in the statements.
In the second test case, the following ways are valid:
* conclude contracts with bloggers with numbers 1 and 2. In this case, the number of followers will be equal to a_1 + a_2 = 2;
* conclude contracts with bloggers with numbers 1 and 3. In this case, the number of followers will be equal to a_1 + a_3 = 2;
* conclude contracts with bloggers with numbers 1 and 4. In this case, the number of followers will be equal to a_1 + a_4 = 2;
* conclude contracts with bloggers with numbers 2 and 3. In this case, the number of followers will be equal to a_2 + a_3 = 2;
* conclude contracts with bloggers with numbers 2 and 4. In this case, the number of followers will be equal to a_2 + a_4 = 2;
* conclude contracts with bloggers with numbers 3 and 4. In this case, the number of followers will be equal to a_3 + a_4 = 2.
In the third test case, the following ways are valid:
* concludes a contract with a blogger with the number 2. In this case, the number of followers will be equal to a_2 = 2. | instruction | 0 | 104,667 | 14 | 209,334 |
Tags: combinatorics, math, sortings
Correct Solution:
```
import math,sys
input=sys.stdin.readline
from collections import Counter
def fact(n):
res = 1
for i in range(2, n+1):
res = res * i
return res
def nCr(n, r):
p=10**9 +7
# initialize numerator
# and denominator
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
for _ in range(int(input())):
n,k=map(int,input().split())
l=list(map(int,input().split()))
l.sort(reverse=True)
c=Counter(l)
#z=sum(l[:k])
s=1
if c[l[k-1]]>1:
s=nCr(c[l[k-1]],k-l.index(l[k-1]))
print(int(s)%(10**9+7))
``` | output | 1 | 104,667 | 14 | 209,335 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n different bloggers. Blogger numbered i has a_i followers.
Since Masha has a limited budget, she can only sign a contract with k different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.
Help her, find the number of ways to select k bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).
For example, if n=4, k=3, a=[1, 3, 1, 2], then Masha has two ways to select 3 bloggers with the maximum total number of followers:
* conclude contracts with bloggers with numbers 1, 2 and 4. In this case, the number of followers will be equal to a_1 + a_2 + a_4 = 6.
* conclude contracts with bloggers with numbers 2, 3 and 4. In this case, the number of followers will be equal to a_2 + a_3 + a_4 = 6.
Since the answer can be quite large, output it modulo 10^9+7.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 1000) β the number of bloggers and how many of them you can sign a contract with.
The second line of each test case contains n integers a_1, a_2, β¦ a_n (1 β€ a_i β€ n) β the number of followers of each blogger.
It is guaranteed that the sum of n over all test cases does not exceed 1000.
Output
For each test case, on a separate line output one integer β the number of ways to select k bloggers so that the total number of their followers is maximum possible.
Example
Input
3
4 3
1 3 1 2
4 2
1 1 1 1
2 1
1 2
Output
2
6
1
Note
The test case is explained in the statements.
In the second test case, the following ways are valid:
* conclude contracts with bloggers with numbers 1 and 2. In this case, the number of followers will be equal to a_1 + a_2 = 2;
* conclude contracts with bloggers with numbers 1 and 3. In this case, the number of followers will be equal to a_1 + a_3 = 2;
* conclude contracts with bloggers with numbers 1 and 4. In this case, the number of followers will be equal to a_1 + a_4 = 2;
* conclude contracts with bloggers with numbers 2 and 3. In this case, the number of followers will be equal to a_2 + a_3 = 2;
* conclude contracts with bloggers with numbers 2 and 4. In this case, the number of followers will be equal to a_2 + a_4 = 2;
* conclude contracts with bloggers with numbers 3 and 4. In this case, the number of followers will be equal to a_3 + a_4 = 2.
In the third test case, the following ways are valid:
* concludes a contract with a blogger with the number 2. In this case, the number of followers will be equal to a_2 = 2. | instruction | 0 | 104,668 | 14 | 209,336 |
Tags: combinatorics, math, sortings
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
##########################################################
def nCk(n, k):
res = 1
for i in range(1, k + 1):
res = res * (n - i + 1) // i
return res
from collections import Counter
for t in range(int(input())):
n,k=map(int, input().split())
arr=sorted(list(map(int,input().split())),reverse=True)
ans=1
d1=Counter(arr[:k])
d2=Counter(arr[k:])
#print(arr)
#print(f[:10])
vis=[0]*1001
for i in range(k):
if vis[arr[i]]==0 and d2[arr[i]]>=1:
ans*=nCk(d1[arr[i]]+d2[arr[i]],d1[arr[i]])
vis[arr[i]]=1
ans=max(ans,1)
print((ans)%(10**9+7))
``` | output | 1 | 104,668 | 14 | 209,337 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n different bloggers. Blogger numbered i has a_i followers.
Since Masha has a limited budget, she can only sign a contract with k different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.
Help her, find the number of ways to select k bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).
For example, if n=4, k=3, a=[1, 3, 1, 2], then Masha has two ways to select 3 bloggers with the maximum total number of followers:
* conclude contracts with bloggers with numbers 1, 2 and 4. In this case, the number of followers will be equal to a_1 + a_2 + a_4 = 6.
* conclude contracts with bloggers with numbers 2, 3 and 4. In this case, the number of followers will be equal to a_2 + a_3 + a_4 = 6.
Since the answer can be quite large, output it modulo 10^9+7.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 1000) β the number of bloggers and how many of them you can sign a contract with.
The second line of each test case contains n integers a_1, a_2, β¦ a_n (1 β€ a_i β€ n) β the number of followers of each blogger.
It is guaranteed that the sum of n over all test cases does not exceed 1000.
Output
For each test case, on a separate line output one integer β the number of ways to select k bloggers so that the total number of their followers is maximum possible.
Example
Input
3
4 3
1 3 1 2
4 2
1 1 1 1
2 1
1 2
Output
2
6
1
Note
The test case is explained in the statements.
In the second test case, the following ways are valid:
* conclude contracts with bloggers with numbers 1 and 2. In this case, the number of followers will be equal to a_1 + a_2 = 2;
* conclude contracts with bloggers with numbers 1 and 3. In this case, the number of followers will be equal to a_1 + a_3 = 2;
* conclude contracts with bloggers with numbers 1 and 4. In this case, the number of followers will be equal to a_1 + a_4 = 2;
* conclude contracts with bloggers with numbers 2 and 3. In this case, the number of followers will be equal to a_2 + a_3 = 2;
* conclude contracts with bloggers with numbers 2 and 4. In this case, the number of followers will be equal to a_2 + a_4 = 2;
* conclude contracts with bloggers with numbers 3 and 4. In this case, the number of followers will be equal to a_3 + a_4 = 2.
In the third test case, the following ways are valid:
* concludes a contract with a blogger with the number 2. In this case, the number of followers will be equal to a_2 = 2. | instruction | 0 | 104,669 | 14 | 209,338 |
Tags: combinatorics, math, sortings
Correct Solution:
```
mod=10**9+7
def ncr(a,b):
c=1
for i in range(a,b,-1):
c=(c*i)%mod
d=1
for i in range(1,a-b+1):
d=(d*i)%mod
d=pow(d,mod-2,mod)
return (c*d)%mod
t=int(input())
for _ in range(t):
n,k=map(int,input().split())
l=list(map(int,input().split()))
l.sort(reverse=True)
d={}
for i in range(k):
try:
d[l[i]]+=1
except:
d[l[i]]=1
e={}
for i in l:
try:
e[i]+=1
except:
e[i]=1
ans=1
for i in d:
ans=ans*ncr(e[i],d[i])
print(ans)
``` | output | 1 | 104,669 | 14 | 209,339 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n different bloggers. Blogger numbered i has a_i followers.
Since Masha has a limited budget, she can only sign a contract with k different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.
Help her, find the number of ways to select k bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).
For example, if n=4, k=3, a=[1, 3, 1, 2], then Masha has two ways to select 3 bloggers with the maximum total number of followers:
* conclude contracts with bloggers with numbers 1, 2 and 4. In this case, the number of followers will be equal to a_1 + a_2 + a_4 = 6.
* conclude contracts with bloggers with numbers 2, 3 and 4. In this case, the number of followers will be equal to a_2 + a_3 + a_4 = 6.
Since the answer can be quite large, output it modulo 10^9+7.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 1000) β the number of bloggers and how many of them you can sign a contract with.
The second line of each test case contains n integers a_1, a_2, β¦ a_n (1 β€ a_i β€ n) β the number of followers of each blogger.
It is guaranteed that the sum of n over all test cases does not exceed 1000.
Output
For each test case, on a separate line output one integer β the number of ways to select k bloggers so that the total number of their followers is maximum possible.
Example
Input
3
4 3
1 3 1 2
4 2
1 1 1 1
2 1
1 2
Output
2
6
1
Note
The test case is explained in the statements.
In the second test case, the following ways are valid:
* conclude contracts with bloggers with numbers 1 and 2. In this case, the number of followers will be equal to a_1 + a_2 = 2;
* conclude contracts with bloggers with numbers 1 and 3. In this case, the number of followers will be equal to a_1 + a_3 = 2;
* conclude contracts with bloggers with numbers 1 and 4. In this case, the number of followers will be equal to a_1 + a_4 = 2;
* conclude contracts with bloggers with numbers 2 and 3. In this case, the number of followers will be equal to a_2 + a_3 = 2;
* conclude contracts with bloggers with numbers 2 and 4. In this case, the number of followers will be equal to a_2 + a_4 = 2;
* conclude contracts with bloggers with numbers 3 and 4. In this case, the number of followers will be equal to a_3 + a_4 = 2.
In the third test case, the following ways are valid:
* concludes a contract with a blogger with the number 2. In this case, the number of followers will be equal to a_2 = 2. | instruction | 0 | 104,670 | 14 | 209,340 |
Tags: combinatorics, math, sortings
Correct Solution:
```
from math import factorial
n = int(input())
for k in range(n):
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b.sort()
temp = b[a[0]-a[1]:]
not_chosen = b[:a[0]-a[1]]
if not_chosen.count(b[a[0]-a[1]]) > 0:
possible = temp.count(b[a[0]-a[1]]) + not_chosen.count(b[a[0]-a[1]])
need = temp.count(b[a[0]-a[1]])
x = factorial(possible)//factorial(possible - need)
y = factorial(need)
z = x//y
print(z % 1000000007)
else:
print(1)
``` | output | 1 | 104,670 | 14 | 209,341 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n different bloggers. Blogger numbered i has a_i followers.
Since Masha has a limited budget, she can only sign a contract with k different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.
Help her, find the number of ways to select k bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).
For example, if n=4, k=3, a=[1, 3, 1, 2], then Masha has two ways to select 3 bloggers with the maximum total number of followers:
* conclude contracts with bloggers with numbers 1, 2 and 4. In this case, the number of followers will be equal to a_1 + a_2 + a_4 = 6.
* conclude contracts with bloggers with numbers 2, 3 and 4. In this case, the number of followers will be equal to a_2 + a_3 + a_4 = 6.
Since the answer can be quite large, output it modulo 10^9+7.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 1000) β the number of bloggers and how many of them you can sign a contract with.
The second line of each test case contains n integers a_1, a_2, β¦ a_n (1 β€ a_i β€ n) β the number of followers of each blogger.
It is guaranteed that the sum of n over all test cases does not exceed 1000.
Output
For each test case, on a separate line output one integer β the number of ways to select k bloggers so that the total number of their followers is maximum possible.
Example
Input
3
4 3
1 3 1 2
4 2
1 1 1 1
2 1
1 2
Output
2
6
1
Note
The test case is explained in the statements.
In the second test case, the following ways are valid:
* conclude contracts with bloggers with numbers 1 and 2. In this case, the number of followers will be equal to a_1 + a_2 = 2;
* conclude contracts with bloggers with numbers 1 and 3. In this case, the number of followers will be equal to a_1 + a_3 = 2;
* conclude contracts with bloggers with numbers 1 and 4. In this case, the number of followers will be equal to a_1 + a_4 = 2;
* conclude contracts with bloggers with numbers 2 and 3. In this case, the number of followers will be equal to a_2 + a_3 = 2;
* conclude contracts with bloggers with numbers 2 and 4. In this case, the number of followers will be equal to a_2 + a_4 = 2;
* conclude contracts with bloggers with numbers 3 and 4. In this case, the number of followers will be equal to a_3 + a_4 = 2.
In the third test case, the following ways are valid:
* concludes a contract with a blogger with the number 2. In this case, the number of followers will be equal to a_2 = 2. | instruction | 0 | 104,671 | 14 | 209,342 |
Tags: combinatorics, math, sortings
Correct Solution:
```
from itertools import repeat, groupby
def line():
return map(int, input().split())
def num():
return int(input())
def nfunc(f, n, *args, **kwargs):
return (f(*args, **kwargs) for _ in repeat(None, n))
t = num()
for _ in repeat(None, t):
n,k = line()
blogs = [len(list(g)) for _,g in groupby(sorted(line())[::-1])]
ans = 1
for m in blogs:
if k<=m:
for i in range(1, k+1):
ans *= (m-k+i)
for i in range(1, k+1):
ans //= i
break
else:
k -= m
print(ans % (10**9 + 7))
``` | output | 1 | 104,671 | 14 | 209,343 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n different bloggers. Blogger numbered i has a_i followers.
Since Masha has a limited budget, she can only sign a contract with k different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.
Help her, find the number of ways to select k bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).
For example, if n=4, k=3, a=[1, 3, 1, 2], then Masha has two ways to select 3 bloggers with the maximum total number of followers:
* conclude contracts with bloggers with numbers 1, 2 and 4. In this case, the number of followers will be equal to a_1 + a_2 + a_4 = 6.
* conclude contracts with bloggers with numbers 2, 3 and 4. In this case, the number of followers will be equal to a_2 + a_3 + a_4 = 6.
Since the answer can be quite large, output it modulo 10^9+7.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 1000) β the number of bloggers and how many of them you can sign a contract with.
The second line of each test case contains n integers a_1, a_2, β¦ a_n (1 β€ a_i β€ n) β the number of followers of each blogger.
It is guaranteed that the sum of n over all test cases does not exceed 1000.
Output
For each test case, on a separate line output one integer β the number of ways to select k bloggers so that the total number of their followers is maximum possible.
Example
Input
3
4 3
1 3 1 2
4 2
1 1 1 1
2 1
1 2
Output
2
6
1
Note
The test case is explained in the statements.
In the second test case, the following ways are valid:
* conclude contracts with bloggers with numbers 1 and 2. In this case, the number of followers will be equal to a_1 + a_2 = 2;
* conclude contracts with bloggers with numbers 1 and 3. In this case, the number of followers will be equal to a_1 + a_3 = 2;
* conclude contracts with bloggers with numbers 1 and 4. In this case, the number of followers will be equal to a_1 + a_4 = 2;
* conclude contracts with bloggers with numbers 2 and 3. In this case, the number of followers will be equal to a_2 + a_3 = 2;
* conclude contracts with bloggers with numbers 2 and 4. In this case, the number of followers will be equal to a_2 + a_4 = 2;
* conclude contracts with bloggers with numbers 3 and 4. In this case, the number of followers will be equal to a_3 + a_4 = 2.
In the third test case, the following ways are valid:
* concludes a contract with a blogger with the number 2. In this case, the number of followers will be equal to a_2 = 2. | instruction | 0 | 104,672 | 14 | 209,344 |
Tags: combinatorics, math, sortings
Correct Solution:
```
import io,os
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
N = 10001
# array to store inverse of 1 to N
factorialNumInverse = [None] * (N + 1)
# array to precompute inverse of 1! to N!
naturalNumInverse = [None] * (N + 1)
# array to store factorial of
# first N numbers
fact = [None] * (N + 1)
# Function to precompute inverse of numbers
def InverseofNumber(p):
naturalNumInverse[0] = naturalNumInverse[1] = 1
for i in range(2, N + 1, 1):
naturalNumInverse[i] = (naturalNumInverse[p % i] *
(p - int(p / i)) % p)
# Function to precompute inverse
# of factorials
def InverseofFactorial(p):
factorialNumInverse[0] = factorialNumInverse[1] = 1
# precompute inverse of natural numbers
for i in range(2, N + 1, 1):
factorialNumInverse[i] = (naturalNumInverse[i] *
factorialNumInverse[i - 1]) % p
# Function to calculate factorial of 1 to N
def factorial(p):
fact[0] = 1
# precompute factorials
for i in range(1, N + 1):
fact[i] = (fact[i - 1] * i) % p
# Function to return nCr % p in O(1) time
def Binomial(N, R, p):
# n C r = n!*inverse(r!)*inverse((n-r)!)
ans = ((fact[N] * factorialNumInverse[R])% p *
factorialNumInverse[N - R])% p
return ans
p = 1000000007
InverseofNumber(p)
InverseofFactorial(p)
factorial(p)
t=int(input())
for _ in range(t):
n,k=list(map(int,input().split()))
arr=list(map(int,input().split()))
arr.sort()
c=[0]*n
for i in range(n):
c[arr[i]-1]+=1
a=[0]*n
for i in range(k):
a[arr[-i-1]-1]+=1
ans=1
for i in range(n):
if a[i]>0:
ans*=Binomial(c[i],a[i],p)
ans%=p
print(ans)
``` | output | 1 | 104,672 | 14 | 209,345 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n different bloggers. Blogger numbered i has a_i followers.
Since Masha has a limited budget, she can only sign a contract with k different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.
Help her, find the number of ways to select k bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).
For example, if n=4, k=3, a=[1, 3, 1, 2], then Masha has two ways to select 3 bloggers with the maximum total number of followers:
* conclude contracts with bloggers with numbers 1, 2 and 4. In this case, the number of followers will be equal to a_1 + a_2 + a_4 = 6.
* conclude contracts with bloggers with numbers 2, 3 and 4. In this case, the number of followers will be equal to a_2 + a_3 + a_4 = 6.
Since the answer can be quite large, output it modulo 10^9+7.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 1000) β the number of bloggers and how many of them you can sign a contract with.
The second line of each test case contains n integers a_1, a_2, β¦ a_n (1 β€ a_i β€ n) β the number of followers of each blogger.
It is guaranteed that the sum of n over all test cases does not exceed 1000.
Output
For each test case, on a separate line output one integer β the number of ways to select k bloggers so that the total number of their followers is maximum possible.
Example
Input
3
4 3
1 3 1 2
4 2
1 1 1 1
2 1
1 2
Output
2
6
1
Note
The test case is explained in the statements.
In the second test case, the following ways are valid:
* conclude contracts with bloggers with numbers 1 and 2. In this case, the number of followers will be equal to a_1 + a_2 = 2;
* conclude contracts with bloggers with numbers 1 and 3. In this case, the number of followers will be equal to a_1 + a_3 = 2;
* conclude contracts with bloggers with numbers 1 and 4. In this case, the number of followers will be equal to a_1 + a_4 = 2;
* conclude contracts with bloggers with numbers 2 and 3. In this case, the number of followers will be equal to a_2 + a_3 = 2;
* conclude contracts with bloggers with numbers 2 and 4. In this case, the number of followers will be equal to a_2 + a_4 = 2;
* conclude contracts with bloggers with numbers 3 and 4. In this case, the number of followers will be equal to a_3 + a_4 = 2.
In the third test case, the following ways are valid:
* concludes a contract with a blogger with the number 2. In this case, the number of followers will be equal to a_2 = 2.
Submitted Solution:
```
import math
import sys
input=sys.stdin.readline
from collections import Counter, defaultdict, deque
MAX_N = 1007
MOD = 10**9 + 7
def modInverse(a, p):
# Fermat's little theorem, a**(p-1) = 1 mod p
# assert a % p != 0
return pow(a, p - 2, p)
# Precompute all factorials: i!
fact = [1]
for i in range(1, MAX_N + 1):
fact.append((fact[-1] * i) % MOD)
# Precompute all inverse factorials: 1 / (i!)
invFact = [0] * (MAX_N + 1)
invFact[MAX_N] = modInverse(fact[MAX_N], MOD)
for i in range(MAX_N - 1, -1, -1):
invFact[i] = (invFact[i + 1] * (i + 1)) % MOD
# assert fact[i] * invFact[i] % MOD == 1
# Precompute all inverses, 1 / i == (i - 1)! / i!
inv = [0] * (MAX_N + 1)
for i in range(1, MAX_N + 1):
inv[i] = fact[i - 1] * invFact[i] % MOD
# assert inv[i] * i % MOD == 1
def nCr(n, r): # mod'd
if n < r:
return 0
return (fact[n] * invFact[r] * invFact[n - r]) % MOD
def f(n, k, a):
l = list(set(a))
l.sort(reverse=True)
c = Counter(a)
el = 0
for i in range(len(l)):
el += c[l[i]]
if el >= k:
el -= c[l[i]]
last = i
break
r = l[last]
co = c[r]
#print(r, co, el)
return nCr(co, k - el)
t = int(input())
result = []
for i in range(t):
#n = int(input())
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
result.append(f(n, k, a))
for i in range(t):
print(result[i])
``` | instruction | 0 | 104,673 | 14 | 209,346 |
Yes | output | 1 | 104,673 | 14 | 209,347 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n different bloggers. Blogger numbered i has a_i followers.
Since Masha has a limited budget, she can only sign a contract with k different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.
Help her, find the number of ways to select k bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).
For example, if n=4, k=3, a=[1, 3, 1, 2], then Masha has two ways to select 3 bloggers with the maximum total number of followers:
* conclude contracts with bloggers with numbers 1, 2 and 4. In this case, the number of followers will be equal to a_1 + a_2 + a_4 = 6.
* conclude contracts with bloggers with numbers 2, 3 and 4. In this case, the number of followers will be equal to a_2 + a_3 + a_4 = 6.
Since the answer can be quite large, output it modulo 10^9+7.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 1000) β the number of bloggers and how many of them you can sign a contract with.
The second line of each test case contains n integers a_1, a_2, β¦ a_n (1 β€ a_i β€ n) β the number of followers of each blogger.
It is guaranteed that the sum of n over all test cases does not exceed 1000.
Output
For each test case, on a separate line output one integer β the number of ways to select k bloggers so that the total number of their followers is maximum possible.
Example
Input
3
4 3
1 3 1 2
4 2
1 1 1 1
2 1
1 2
Output
2
6
1
Note
The test case is explained in the statements.
In the second test case, the following ways are valid:
* conclude contracts with bloggers with numbers 1 and 2. In this case, the number of followers will be equal to a_1 + a_2 = 2;
* conclude contracts with bloggers with numbers 1 and 3. In this case, the number of followers will be equal to a_1 + a_3 = 2;
* conclude contracts with bloggers with numbers 1 and 4. In this case, the number of followers will be equal to a_1 + a_4 = 2;
* conclude contracts with bloggers with numbers 2 and 3. In this case, the number of followers will be equal to a_2 + a_3 = 2;
* conclude contracts with bloggers with numbers 2 and 4. In this case, the number of followers will be equal to a_2 + a_4 = 2;
* conclude contracts with bloggers with numbers 3 and 4. In this case, the number of followers will be equal to a_3 + a_4 = 2.
In the third test case, the following ways are valid:
* concludes a contract with a blogger with the number 2. In this case, the number of followers will be equal to a_2 = 2.
Submitted Solution:
```
from collections import Counter
T = int(input())
ans_ls = [0] * T
class Combination():
def __init__(self, n, mod=10**9+7):
self.mod = mod
self.fac = [1]*(n+1)
for i in range(1,n+1):
self.fac[i] = self.fac[i-1] * i % self.mod
self.invfac = [1]*(n+1)
self.invfac[n] = pow(self.fac[n], self.mod - 2, self.mod)
for i in range(n-1, 0, -1):
self.invfac[i] = self.invfac[i+1] * (i+1) % self.mod
def nCr(self, n, r, default_none = 0):
if n < r:
return default_none
return self.fac[n] * self.invfac[r] % self.mod * self.invfac[n-r] % self.mod
def permutation(self, n, r, default_none = 0):
if n < r:
return default_none
return self.factorial(n) * self.invfactorial(n-r) % self.mod
def factorial(self, i):
return self.fac[i]
def invfactorial(self, i):
return self.invfac[i]
c = Combination(10**3+10)
for t in range(T):
N,K = map(int,input().split())
a_ls = list(map(int, input().split()))
a_ls.sort(reverse=True)
selected = a_ls[:K]
best_min = selected[-1]
best_notmin_count = 0
for i in range(K):
if a_ls[i] != best_min:
best_notmin_count += 1
min_count_in_total = Counter(a_ls)[best_min]
ans_ls[t] = c.nCr(min_count_in_total, K - best_notmin_count)
for ans in ans_ls:
print(ans)
``` | instruction | 0 | 104,674 | 14 | 209,348 |
Yes | output | 1 | 104,674 | 14 | 209,349 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n different bloggers. Blogger numbered i has a_i followers.
Since Masha has a limited budget, she can only sign a contract with k different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.
Help her, find the number of ways to select k bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).
For example, if n=4, k=3, a=[1, 3, 1, 2], then Masha has two ways to select 3 bloggers with the maximum total number of followers:
* conclude contracts with bloggers with numbers 1, 2 and 4. In this case, the number of followers will be equal to a_1 + a_2 + a_4 = 6.
* conclude contracts with bloggers with numbers 2, 3 and 4. In this case, the number of followers will be equal to a_2 + a_3 + a_4 = 6.
Since the answer can be quite large, output it modulo 10^9+7.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 1000) β the number of bloggers and how many of them you can sign a contract with.
The second line of each test case contains n integers a_1, a_2, β¦ a_n (1 β€ a_i β€ n) β the number of followers of each blogger.
It is guaranteed that the sum of n over all test cases does not exceed 1000.
Output
For each test case, on a separate line output one integer β the number of ways to select k bloggers so that the total number of their followers is maximum possible.
Example
Input
3
4 3
1 3 1 2
4 2
1 1 1 1
2 1
1 2
Output
2
6
1
Note
The test case is explained in the statements.
In the second test case, the following ways are valid:
* conclude contracts with bloggers with numbers 1 and 2. In this case, the number of followers will be equal to a_1 + a_2 = 2;
* conclude contracts with bloggers with numbers 1 and 3. In this case, the number of followers will be equal to a_1 + a_3 = 2;
* conclude contracts with bloggers with numbers 1 and 4. In this case, the number of followers will be equal to a_1 + a_4 = 2;
* conclude contracts with bloggers with numbers 2 and 3. In this case, the number of followers will be equal to a_2 + a_3 = 2;
* conclude contracts with bloggers with numbers 2 and 4. In this case, the number of followers will be equal to a_2 + a_4 = 2;
* conclude contracts with bloggers with numbers 3 and 4. In this case, the number of followers will be equal to a_3 + a_4 = 2.
In the third test case, the following ways are valid:
* concludes a contract with a blogger with the number 2. In this case, the number of followers will be equal to a_2 = 2.
Submitted Solution:
```
from collections import Counter
mod = 1000000007
factorials = [1]
for i in range(1, 1000001):
factorials.append((factorials[-1] * i) % mod)
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse = True)
CA = Counter(A)
first_k = A[:k]
Ck = Counter(first_k)
ans = 1
for x in Ck:
n, r = CA[x], Ck[x]
fn, fr, fnr = factorials[n], factorials[r], factorials[n - r]
ncr = (fn * pow(fr*fnr, mod - 2, mod)) % mod
ans = (ans * ncr) % mod
print(ans)
``` | instruction | 0 | 104,675 | 14 | 209,350 |
Yes | output | 1 | 104,675 | 14 | 209,351 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n different bloggers. Blogger numbered i has a_i followers.
Since Masha has a limited budget, she can only sign a contract with k different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.
Help her, find the number of ways to select k bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).
For example, if n=4, k=3, a=[1, 3, 1, 2], then Masha has two ways to select 3 bloggers with the maximum total number of followers:
* conclude contracts with bloggers with numbers 1, 2 and 4. In this case, the number of followers will be equal to a_1 + a_2 + a_4 = 6.
* conclude contracts with bloggers with numbers 2, 3 and 4. In this case, the number of followers will be equal to a_2 + a_3 + a_4 = 6.
Since the answer can be quite large, output it modulo 10^9+7.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 1000) β the number of bloggers and how many of them you can sign a contract with.
The second line of each test case contains n integers a_1, a_2, β¦ a_n (1 β€ a_i β€ n) β the number of followers of each blogger.
It is guaranteed that the sum of n over all test cases does not exceed 1000.
Output
For each test case, on a separate line output one integer β the number of ways to select k bloggers so that the total number of their followers is maximum possible.
Example
Input
3
4 3
1 3 1 2
4 2
1 1 1 1
2 1
1 2
Output
2
6
1
Note
The test case is explained in the statements.
In the second test case, the following ways are valid:
* conclude contracts with bloggers with numbers 1 and 2. In this case, the number of followers will be equal to a_1 + a_2 = 2;
* conclude contracts with bloggers with numbers 1 and 3. In this case, the number of followers will be equal to a_1 + a_3 = 2;
* conclude contracts with bloggers with numbers 1 and 4. In this case, the number of followers will be equal to a_1 + a_4 = 2;
* conclude contracts with bloggers with numbers 2 and 3. In this case, the number of followers will be equal to a_2 + a_3 = 2;
* conclude contracts with bloggers with numbers 2 and 4. In this case, the number of followers will be equal to a_2 + a_4 = 2;
* conclude contracts with bloggers with numbers 3 and 4. In this case, the number of followers will be equal to a_3 + a_4 = 2.
In the third test case, the following ways are valid:
* concludes a contract with a blogger with the number 2. In this case, the number of followers will be equal to a_2 = 2.
Submitted Solution:
```
import math
from collections import Counter
def nCr(n,r):
f = math.factorial
return f(n) // f(r) // f(n-r)
for _ in range(int(input())):
n,k = map(int,input().split())
l = list(map(int,input().split()))
d = Counter(l)
l.sort(reverse = 1)
ans = 1
a = l[k-1]
i = k-1
c = 0
while i>-1:
if l[i] == a:
c+=1
else:
break
i-=1
x = nCr(d[a],c)
print(x%(10**9 + 7))
``` | instruction | 0 | 104,676 | 14 | 209,352 |
Yes | output | 1 | 104,676 | 14 | 209,353 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n different bloggers. Blogger numbered i has a_i followers.
Since Masha has a limited budget, she can only sign a contract with k different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.
Help her, find the number of ways to select k bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).
For example, if n=4, k=3, a=[1, 3, 1, 2], then Masha has two ways to select 3 bloggers with the maximum total number of followers:
* conclude contracts with bloggers with numbers 1, 2 and 4. In this case, the number of followers will be equal to a_1 + a_2 + a_4 = 6.
* conclude contracts with bloggers with numbers 2, 3 and 4. In this case, the number of followers will be equal to a_2 + a_3 + a_4 = 6.
Since the answer can be quite large, output it modulo 10^9+7.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 1000) β the number of bloggers and how many of them you can sign a contract with.
The second line of each test case contains n integers a_1, a_2, β¦ a_n (1 β€ a_i β€ n) β the number of followers of each blogger.
It is guaranteed that the sum of n over all test cases does not exceed 1000.
Output
For each test case, on a separate line output one integer β the number of ways to select k bloggers so that the total number of their followers is maximum possible.
Example
Input
3
4 3
1 3 1 2
4 2
1 1 1 1
2 1
1 2
Output
2
6
1
Note
The test case is explained in the statements.
In the second test case, the following ways are valid:
* conclude contracts with bloggers with numbers 1 and 2. In this case, the number of followers will be equal to a_1 + a_2 = 2;
* conclude contracts with bloggers with numbers 1 and 3. In this case, the number of followers will be equal to a_1 + a_3 = 2;
* conclude contracts with bloggers with numbers 1 and 4. In this case, the number of followers will be equal to a_1 + a_4 = 2;
* conclude contracts with bloggers with numbers 2 and 3. In this case, the number of followers will be equal to a_2 + a_3 = 2;
* conclude contracts with bloggers with numbers 2 and 4. In this case, the number of followers will be equal to a_2 + a_4 = 2;
* conclude contracts with bloggers with numbers 3 and 4. In this case, the number of followers will be equal to a_3 + a_4 = 2.
In the third test case, the following ways are valid:
* concludes a contract with a blogger with the number 2. In this case, the number of followers will be equal to a_2 = 2.
Submitted Solution:
```
for _ in range(int(input())):
n, k = map(int, input().split())
a = list(map(int, input().split())) # n bloggers
result = 1
if n > k:
d = {}
s = set()
for i in a:
if i not in d:
d[i] = 1
s.add(i)
else:
d[i] += 1
s = sorted(list(s))
s.reverse()
for i in s:
if k <= 0:
break
if d[i] <= k:
k -= d[i]
else:
n = d[i]
result1 = 1
if 2*k > n:
k = n-k
for j in range(n, k, -1):
result *= j
result1 *= n+1-j
result = round(result/result1)
break
print(result % (10**9+7))
``` | instruction | 0 | 104,677 | 14 | 209,354 |
No | output | 1 | 104,677 | 14 | 209,355 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n different bloggers. Blogger numbered i has a_i followers.
Since Masha has a limited budget, she can only sign a contract with k different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.
Help her, find the number of ways to select k bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).
For example, if n=4, k=3, a=[1, 3, 1, 2], then Masha has two ways to select 3 bloggers with the maximum total number of followers:
* conclude contracts with bloggers with numbers 1, 2 and 4. In this case, the number of followers will be equal to a_1 + a_2 + a_4 = 6.
* conclude contracts with bloggers with numbers 2, 3 and 4. In this case, the number of followers will be equal to a_2 + a_3 + a_4 = 6.
Since the answer can be quite large, output it modulo 10^9+7.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 1000) β the number of bloggers and how many of them you can sign a contract with.
The second line of each test case contains n integers a_1, a_2, β¦ a_n (1 β€ a_i β€ n) β the number of followers of each blogger.
It is guaranteed that the sum of n over all test cases does not exceed 1000.
Output
For each test case, on a separate line output one integer β the number of ways to select k bloggers so that the total number of their followers is maximum possible.
Example
Input
3
4 3
1 3 1 2
4 2
1 1 1 1
2 1
1 2
Output
2
6
1
Note
The test case is explained in the statements.
In the second test case, the following ways are valid:
* conclude contracts with bloggers with numbers 1 and 2. In this case, the number of followers will be equal to a_1 + a_2 = 2;
* conclude contracts with bloggers with numbers 1 and 3. In this case, the number of followers will be equal to a_1 + a_3 = 2;
* conclude contracts with bloggers with numbers 1 and 4. In this case, the number of followers will be equal to a_1 + a_4 = 2;
* conclude contracts with bloggers with numbers 2 and 3. In this case, the number of followers will be equal to a_2 + a_3 = 2;
* conclude contracts with bloggers with numbers 2 and 4. In this case, the number of followers will be equal to a_2 + a_4 = 2;
* conclude contracts with bloggers with numbers 3 and 4. In this case, the number of followers will be equal to a_3 + a_4 = 2.
In the third test case, the following ways are valid:
* concludes a contract with a blogger with the number 2. In this case, the number of followers will be equal to a_2 = 2.
Submitted Solution:
```
import math
for _ in range(int(input())):
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
t = a.count(a[k-1])
s = 0
j = k-1
while a[j] == a[k-1] and j>=0:
s += 1
j -= 1
if len(set(a)) == 1:
s = k
t = n
print(math.factorial(t)//(math.factorial(t-s)*math.factorial(s)))
``` | instruction | 0 | 104,678 | 14 | 209,356 |
No | output | 1 | 104,678 | 14 | 209,357 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n different bloggers. Blogger numbered i has a_i followers.
Since Masha has a limited budget, she can only sign a contract with k different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.
Help her, find the number of ways to select k bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).
For example, if n=4, k=3, a=[1, 3, 1, 2], then Masha has two ways to select 3 bloggers with the maximum total number of followers:
* conclude contracts with bloggers with numbers 1, 2 and 4. In this case, the number of followers will be equal to a_1 + a_2 + a_4 = 6.
* conclude contracts with bloggers with numbers 2, 3 and 4. In this case, the number of followers will be equal to a_2 + a_3 + a_4 = 6.
Since the answer can be quite large, output it modulo 10^9+7.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 1000) β the number of bloggers and how many of them you can sign a contract with.
The second line of each test case contains n integers a_1, a_2, β¦ a_n (1 β€ a_i β€ n) β the number of followers of each blogger.
It is guaranteed that the sum of n over all test cases does not exceed 1000.
Output
For each test case, on a separate line output one integer β the number of ways to select k bloggers so that the total number of their followers is maximum possible.
Example
Input
3
4 3
1 3 1 2
4 2
1 1 1 1
2 1
1 2
Output
2
6
1
Note
The test case is explained in the statements.
In the second test case, the following ways are valid:
* conclude contracts with bloggers with numbers 1 and 2. In this case, the number of followers will be equal to a_1 + a_2 = 2;
* conclude contracts with bloggers with numbers 1 and 3. In this case, the number of followers will be equal to a_1 + a_3 = 2;
* conclude contracts with bloggers with numbers 1 and 4. In this case, the number of followers will be equal to a_1 + a_4 = 2;
* conclude contracts with bloggers with numbers 2 and 3. In this case, the number of followers will be equal to a_2 + a_3 = 2;
* conclude contracts with bloggers with numbers 2 and 4. In this case, the number of followers will be equal to a_2 + a_4 = 2;
* conclude contracts with bloggers with numbers 3 and 4. In this case, the number of followers will be equal to a_3 + a_4 = 2.
In the third test case, the following ways are valid:
* concludes a contract with a blogger with the number 2. In this case, the number of followers will be equal to a_2 = 2.
Submitted Solution:
```
from math import *
from sys import *
from bisect import *
from collections import *
def fact(x):
a=1
for i in range(1,x+1):
a*=i
return a
def solve(n,k):
x=fact(n)
y=fact(k)
z=fact(n-k)
return x//(y*z)
t=int(stdin.readline())
for _ in range(t):
n,k=map(int,stdin.readline().split())
a=list(map(int,stdin.readline().split()))
a.sort()
a=a[::-1]
d={}
b=[]
for i in range(n):
if a[i] not in d:
d[a[i]]=0
b.append(a[i])
d[a[i]]+=1
f=0
j=0
for i in range(len(b)):
k -= d[b[i]]
if k<0:
f=1
j=i
k+=d[b[i]]
break
if k==0:
break
if f==1:
ans=solve(d[b[j]],k)
print(ans)
else:
print(1)
``` | instruction | 0 | 104,679 | 14 | 209,358 |
No | output | 1 | 104,679 | 14 | 209,359 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n different bloggers. Blogger numbered i has a_i followers.
Since Masha has a limited budget, she can only sign a contract with k different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.
Help her, find the number of ways to select k bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).
For example, if n=4, k=3, a=[1, 3, 1, 2], then Masha has two ways to select 3 bloggers with the maximum total number of followers:
* conclude contracts with bloggers with numbers 1, 2 and 4. In this case, the number of followers will be equal to a_1 + a_2 + a_4 = 6.
* conclude contracts with bloggers with numbers 2, 3 and 4. In this case, the number of followers will be equal to a_2 + a_3 + a_4 = 6.
Since the answer can be quite large, output it modulo 10^9+7.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 1000) β the number of bloggers and how many of them you can sign a contract with.
The second line of each test case contains n integers a_1, a_2, β¦ a_n (1 β€ a_i β€ n) β the number of followers of each blogger.
It is guaranteed that the sum of n over all test cases does not exceed 1000.
Output
For each test case, on a separate line output one integer β the number of ways to select k bloggers so that the total number of their followers is maximum possible.
Example
Input
3
4 3
1 3 1 2
4 2
1 1 1 1
2 1
1 2
Output
2
6
1
Note
The test case is explained in the statements.
In the second test case, the following ways are valid:
* conclude contracts with bloggers with numbers 1 and 2. In this case, the number of followers will be equal to a_1 + a_2 = 2;
* conclude contracts with bloggers with numbers 1 and 3. In this case, the number of followers will be equal to a_1 + a_3 = 2;
* conclude contracts with bloggers with numbers 1 and 4. In this case, the number of followers will be equal to a_1 + a_4 = 2;
* conclude contracts with bloggers with numbers 2 and 3. In this case, the number of followers will be equal to a_2 + a_3 = 2;
* conclude contracts with bloggers with numbers 2 and 4. In this case, the number of followers will be equal to a_2 + a_4 = 2;
* conclude contracts with bloggers with numbers 3 and 4. In this case, the number of followers will be equal to a_3 + a_4 = 2.
In the third test case, the following ways are valid:
* concludes a contract with a blogger with the number 2. In this case, the number of followers will be equal to a_2 = 2.
Submitted Solution:
```
from sys import stdin
#import math
input = stdin.readline
def ncr(n, r, p):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
tc = int(input())
for _ in range(tc):
n,k = map(int,input().split())
a=list(map(int,input().split()))
a.sort(reverse=True)
d = {}
y=a.count(a[k-1])
a = a[:k]
x=a.count(a[-1])
print(ncr(y, x, 1000000009))
``` | instruction | 0 | 104,680 | 14 | 209,360 |
No | output | 1 | 104,680 | 14 | 209,361 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima loves Inna very much. He decided to write a song for her. Dima has a magic guitar with n strings and m frets. Dima makes the guitar produce sounds like that: to play a note, he needs to hold one of the strings on one of the frets and then pull the string. When Dima pulls the i-th string holding it on the j-th fret the guitar produces a note, let's denote it as aij. We know that Dima's guitar can produce k distinct notes. It is possible that some notes can be produced in multiple ways. In other words, it is possible that aij = apq at (i, j) β (p, q).
Dima has already written a song β a sequence of s notes. In order to play the song, you need to consecutively produce the notes from the song on the guitar. You can produce each note in any available way. Dima understood that there are many ways to play a song and he wants to play it so as to make the song look as complicated as possible (try to act like Cobein).
We'll represent a way to play a song as a sequence of pairs (xi, yi) (1 β€ i β€ s), such that the xi-th string on the yi-th fret produces the i-th note from the song. The complexity of moving between pairs (x1, y1) and (x2, y2) equals <image> + <image>. The complexity of a way to play a song is the maximum of complexities of moving between adjacent pairs.
Help Dima determine the maximum complexity of the way to play his song! The guy's gotta look cool!
Input
The first line of the input contains four integers n, m, k and s (1 β€ n, m β€ 2000, 1 β€ k β€ 9, 2 β€ s β€ 105).
Then follow n lines, each containing m integers aij (1 β€ aij β€ k). The number in the i-th row and the j-th column (aij) means a note that the guitar produces on the i-th string and the j-th fret.
The last line of the input contains s integers qi (1 β€ qi β€ k) β the sequence of notes of the song.
Output
In a single line print a single number β the maximum possible complexity of the song.
Examples
Input
4 6 5 7
3 1 2 2 3 1
3 2 2 2 5 5
4 2 2 2 5 3
3 2 2 1 4 3
2 3 1 4 1 5 1
Output
8
Input
4 4 9 5
4 7 9 5
1 2 1 7
8 3 4 9
5 7 7 2
7 1 9 2 5
Output
4 | instruction | 0 | 104,794 | 14 | 209,588 |
Tags: brute force, implementation, math
Correct Solution:
```
def solution() : # ζε€§ηθ·η¦»ζ₯θͺδΊθ§θ½ιθΏηηΉ
n,m,k,s = map(int, input().split())
dis = lambda a,b : abs(a[0] - b[0]) + abs(a[1] - b[1])
corner = [(0,0), (0,m-1), (n-1,0), (n-1,m-1)]
vertex = [[(n,m), (n,-1), (-1,m), (-1,-1)] for _ in range(k+1)]
for i in range(n) :
for j,note in enumerate(map(int, input().split())) :
vertex[note] = [
(i,j) if dis((i,j), c) < dis(v, c) else v
for v,c in zip(vertex[note], corner)]
maxdis = [[-1] * (k+1) for _ in range(k+1)]
pairs = [(0,3),(3,0),(1,2),(2,1)]
for i in range(1, k+1) :
for j in range(i, k+1) :
vi,vj = vertex[i],vertex[j]
maxdis[i][j] = max(dis(vi[a], vj[b]) for a,b in pairs)
maxdis[j][i] = maxdis[i][j]
s = list(map(int, input().split()))
print(max(maxdis[s[i]][s[i+1]] for i in range(len(s) - 1)))
solution()
``` | output | 1 | 104,794 | 14 | 209,589 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima loves Inna very much. He decided to write a song for her. Dima has a magic guitar with n strings and m frets. Dima makes the guitar produce sounds like that: to play a note, he needs to hold one of the strings on one of the frets and then pull the string. When Dima pulls the i-th string holding it on the j-th fret the guitar produces a note, let's denote it as aij. We know that Dima's guitar can produce k distinct notes. It is possible that some notes can be produced in multiple ways. In other words, it is possible that aij = apq at (i, j) β (p, q).
Dima has already written a song β a sequence of s notes. In order to play the song, you need to consecutively produce the notes from the song on the guitar. You can produce each note in any available way. Dima understood that there are many ways to play a song and he wants to play it so as to make the song look as complicated as possible (try to act like Cobein).
We'll represent a way to play a song as a sequence of pairs (xi, yi) (1 β€ i β€ s), such that the xi-th string on the yi-th fret produces the i-th note from the song. The complexity of moving between pairs (x1, y1) and (x2, y2) equals <image> + <image>. The complexity of a way to play a song is the maximum of complexities of moving between adjacent pairs.
Help Dima determine the maximum complexity of the way to play his song! The guy's gotta look cool!
Input
The first line of the input contains four integers n, m, k and s (1 β€ n, m β€ 2000, 1 β€ k β€ 9, 2 β€ s β€ 105).
Then follow n lines, each containing m integers aij (1 β€ aij β€ k). The number in the i-th row and the j-th column (aij) means a note that the guitar produces on the i-th string and the j-th fret.
The last line of the input contains s integers qi (1 β€ qi β€ k) β the sequence of notes of the song.
Output
In a single line print a single number β the maximum possible complexity of the song.
Examples
Input
4 6 5 7
3 1 2 2 3 1
3 2 2 2 5 5
4 2 2 2 5 3
3 2 2 1 4 3
2 3 1 4 1 5 1
Output
8
Input
4 4 9 5
4 7 9 5
1 2 1 7
8 3 4 9
5 7 7 2
7 1 9 2 5
Output
4
Submitted Solution:
```
def solution() : # ζε€§ηθ·η¦»ζ₯θͺδΊθ§θ½ιθΏηηΉ
n,m,k,s = map(int, input().split())
dis = lambda a,b : abs(a[0] - b[0]) + abs(a[1] - b[1])
corner = [(0,0), (0,m-1), (n-1,0), (n-1,m-1)]
vertex = [[(n,m), (n,-1), (-1,m), (-1,-1)] for _ in range(k+1)]
for i in range(n) :
for j,note in enumerate(map(int, input().split())) :
vertex[note] = [
(i,j) if dis((i,j), c) < dis(v, c) else v
for v,c in zip(vertex[note], corner)]
maxdis = [[0] * (k+1) for _ in range(k+1)]
pairs = [(0,3),(3,0),(1,2),(2,1)]
for i in range(1, k+1) :
for j in range(i+1, k+1) :
vi,vj = vertex[i],vertex[j]
maxdis[i][j] = max(dis(vi[a], vj[b]) for a,b in pairs)
maxdis[j][i] = maxdis[i][j]
s = list(map(int, input().split()))
print(max(maxdis[s[i]][s[i+1]] for i in range(len(s) - 1)))
solution()
``` | instruction | 0 | 104,795 | 14 | 209,590 |
No | output | 1 | 104,795 | 14 | 209,591 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions are constraints on n and k.
You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 0).
Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.
You (suddenly!) have the ability to see the future. You know that during the day you will receive n messages, the i-th message will be received from the friend with ID id_i (1 β€ id_i β€ 10^9).
If you receive a message from id_i in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.
Otherwise (i.e. if there is no conversation with id_i on the screen):
* Firstly, if the number of conversations displayed on the screen is k, the last conversation (which has the position k) is removed from the screen.
* Now the number of conversations on the screen is guaranteed to be less than k and the conversation with the friend id_i is not displayed on the screen.
* The conversation with the friend id_i appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.
Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all n messages.
Input
The first line of the input contains two integers n and k (1 β€ n, k β€ 2 β
10^5) β the number of messages and the number of conversations your smartphone can show.
The second line of the input contains n integers id_1, id_2, ..., id_n (1 β€ id_i β€ 10^9), where id_i is the ID of the friend which sends you the i-th message.
Output
In the first line of the output print one integer m (1 β€ m β€ min(n, k)) β the number of conversations shown after receiving all n messages.
In the second line print m integers ids_1, ids_2, ..., ids_m, where ids_i should be equal to the ID of the friend corresponding to the conversation displayed on the position i after receiving all n messages.
Examples
Input
7 2
1 2 3 2 1 3 2
Output
2
2 1
Input
10 4
2 3 3 1 1 2 1 2 3 3
Output
3
1 3 2
Note
In the first example the list of conversations will change in the following way (in order from the first to last message):
* [];
* [1];
* [2, 1];
* [3, 2];
* [3, 2];
* [1, 3];
* [1, 3];
* [2, 1].
In the second example the list of conversations will change in the following way:
* [];
* [2];
* [3, 2];
* [3, 2];
* [1, 3, 2];
* and then the list will not change till the end. | instruction | 0 | 105,339 | 14 | 210,678 |
Tags: data structures, implementation
Correct Solution:
```
from collections import defaultdict
n,k=map(int,input().split())
l=list(map(int,input().split()))
s=[]
x=0
d=defaultdict(int)
for i in l:
if d[i]==0:
s.append(i)
d[i]=1
x+=1
if x>k:
q=s.pop(0)
d[q]=0
x-=1
print(x)
print(*s[::-1])
``` | output | 1 | 105,339 | 14 | 210,679 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions are constraints on n and k.
You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 0).
Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.
You (suddenly!) have the ability to see the future. You know that during the day you will receive n messages, the i-th message will be received from the friend with ID id_i (1 β€ id_i β€ 10^9).
If you receive a message from id_i in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.
Otherwise (i.e. if there is no conversation with id_i on the screen):
* Firstly, if the number of conversations displayed on the screen is k, the last conversation (which has the position k) is removed from the screen.
* Now the number of conversations on the screen is guaranteed to be less than k and the conversation with the friend id_i is not displayed on the screen.
* The conversation with the friend id_i appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.
Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all n messages.
Input
The first line of the input contains two integers n and k (1 β€ n, k β€ 2 β
10^5) β the number of messages and the number of conversations your smartphone can show.
The second line of the input contains n integers id_1, id_2, ..., id_n (1 β€ id_i β€ 10^9), where id_i is the ID of the friend which sends you the i-th message.
Output
In the first line of the output print one integer m (1 β€ m β€ min(n, k)) β the number of conversations shown after receiving all n messages.
In the second line print m integers ids_1, ids_2, ..., ids_m, where ids_i should be equal to the ID of the friend corresponding to the conversation displayed on the position i after receiving all n messages.
Examples
Input
7 2
1 2 3 2 1 3 2
Output
2
2 1
Input
10 4
2 3 3 1 1 2 1 2 3 3
Output
3
1 3 2
Note
In the first example the list of conversations will change in the following way (in order from the first to last message):
* [];
* [1];
* [2, 1];
* [3, 2];
* [3, 2];
* [1, 3];
* [1, 3];
* [2, 1].
In the second example the list of conversations will change in the following way:
* [];
* [2];
* [3, 2];
* [3, 2];
* [1, 3, 2];
* and then the list will not change till the end. | instruction | 0 | 105,340 | 14 | 210,680 |
Tags: data structures, implementation
Correct Solution:
```
def solve(nums, k):
l = list()
h = set()
for j in nums:
if j not in h:
if len(l) == k:
h.remove(l[0])
l.pop(0)
l.append(j)
h.add(j)
return l
def main():
_, k = list(map(int, input().split()))
nums = list(map(int, input().split()))
sol = solve(nums, k)
print(len(sol))
for i in reversed(sol):
print(i, end=" ")
main()
``` | output | 1 | 105,340 | 14 | 210,681 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions are constraints on n and k.
You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 0).
Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.
You (suddenly!) have the ability to see the future. You know that during the day you will receive n messages, the i-th message will be received from the friend with ID id_i (1 β€ id_i β€ 10^9).
If you receive a message from id_i in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.
Otherwise (i.e. if there is no conversation with id_i on the screen):
* Firstly, if the number of conversations displayed on the screen is k, the last conversation (which has the position k) is removed from the screen.
* Now the number of conversations on the screen is guaranteed to be less than k and the conversation with the friend id_i is not displayed on the screen.
* The conversation with the friend id_i appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.
Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all n messages.
Input
The first line of the input contains two integers n and k (1 β€ n, k β€ 2 β
10^5) β the number of messages and the number of conversations your smartphone can show.
The second line of the input contains n integers id_1, id_2, ..., id_n (1 β€ id_i β€ 10^9), where id_i is the ID of the friend which sends you the i-th message.
Output
In the first line of the output print one integer m (1 β€ m β€ min(n, k)) β the number of conversations shown after receiving all n messages.
In the second line print m integers ids_1, ids_2, ..., ids_m, where ids_i should be equal to the ID of the friend corresponding to the conversation displayed on the position i after receiving all n messages.
Examples
Input
7 2
1 2 3 2 1 3 2
Output
2
2 1
Input
10 4
2 3 3 1 1 2 1 2 3 3
Output
3
1 3 2
Note
In the first example the list of conversations will change in the following way (in order from the first to last message):
* [];
* [1];
* [2, 1];
* [3, 2];
* [3, 2];
* [1, 3];
* [1, 3];
* [2, 1].
In the second example the list of conversations will change in the following way:
* [];
* [2];
* [3, 2];
* [3, 2];
* [1, 3, 2];
* and then the list will not change till the end. | instruction | 0 | 105,341 | 14 | 210,682 |
Tags: data structures, implementation
Correct Solution:
```
from collections import deque
n, k = map(int, input().split())
arr = list(map(int, input().split()))
d = deque()
s = set()
for v in arr:
if v in s:
continue
d.append(v)
s.add(v)
if len(d) > k:
s.remove(d.popleft())
print(len(d))
print(*reversed(d))
``` | output | 1 | 105,341 | 14 | 210,683 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions are constraints on n and k.
You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 0).
Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.
You (suddenly!) have the ability to see the future. You know that during the day you will receive n messages, the i-th message will be received from the friend with ID id_i (1 β€ id_i β€ 10^9).
If you receive a message from id_i in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.
Otherwise (i.e. if there is no conversation with id_i on the screen):
* Firstly, if the number of conversations displayed on the screen is k, the last conversation (which has the position k) is removed from the screen.
* Now the number of conversations on the screen is guaranteed to be less than k and the conversation with the friend id_i is not displayed on the screen.
* The conversation with the friend id_i appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.
Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all n messages.
Input
The first line of the input contains two integers n and k (1 β€ n, k β€ 2 β
10^5) β the number of messages and the number of conversations your smartphone can show.
The second line of the input contains n integers id_1, id_2, ..., id_n (1 β€ id_i β€ 10^9), where id_i is the ID of the friend which sends you the i-th message.
Output
In the first line of the output print one integer m (1 β€ m β€ min(n, k)) β the number of conversations shown after receiving all n messages.
In the second line print m integers ids_1, ids_2, ..., ids_m, where ids_i should be equal to the ID of the friend corresponding to the conversation displayed on the position i after receiving all n messages.
Examples
Input
7 2
1 2 3 2 1 3 2
Output
2
2 1
Input
10 4
2 3 3 1 1 2 1 2 3 3
Output
3
1 3 2
Note
In the first example the list of conversations will change in the following way (in order from the first to last message):
* [];
* [1];
* [2, 1];
* [3, 2];
* [3, 2];
* [1, 3];
* [1, 3];
* [2, 1].
In the second example the list of conversations will change in the following way:
* [];
* [2];
* [3, 2];
* [3, 2];
* [1, 3, 2];
* and then the list will not change till the end. | instruction | 0 | 105,342 | 14 | 210,684 |
Tags: data structures, implementation
Correct Solution:
```
# Bhagwaan codeforces
n, k = map(int, input().split())
a = list(map(int, input().split()))
import queue
s = set()
q = queue.Queue()
count = 0
for x in a:
if x not in s:
if count < k:
q.put(x)
s.add(x)
count += 1
else:
s.remove(q.get())
q.put(x)
s.add(x)
ans = list()
for _ in range(count):
ans.append(q.get())
print(count)
print(' '.join(map(str, ans[::-1])))
``` | output | 1 | 105,342 | 14 | 210,685 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions are constraints on n and k.
You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 0).
Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.
You (suddenly!) have the ability to see the future. You know that during the day you will receive n messages, the i-th message will be received from the friend with ID id_i (1 β€ id_i β€ 10^9).
If you receive a message from id_i in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.
Otherwise (i.e. if there is no conversation with id_i on the screen):
* Firstly, if the number of conversations displayed on the screen is k, the last conversation (which has the position k) is removed from the screen.
* Now the number of conversations on the screen is guaranteed to be less than k and the conversation with the friend id_i is not displayed on the screen.
* The conversation with the friend id_i appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.
Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all n messages.
Input
The first line of the input contains two integers n and k (1 β€ n, k β€ 2 β
10^5) β the number of messages and the number of conversations your smartphone can show.
The second line of the input contains n integers id_1, id_2, ..., id_n (1 β€ id_i β€ 10^9), where id_i is the ID of the friend which sends you the i-th message.
Output
In the first line of the output print one integer m (1 β€ m β€ min(n, k)) β the number of conversations shown after receiving all n messages.
In the second line print m integers ids_1, ids_2, ..., ids_m, where ids_i should be equal to the ID of the friend corresponding to the conversation displayed on the position i after receiving all n messages.
Examples
Input
7 2
1 2 3 2 1 3 2
Output
2
2 1
Input
10 4
2 3 3 1 1 2 1 2 3 3
Output
3
1 3 2
Note
In the first example the list of conversations will change in the following way (in order from the first to last message):
* [];
* [1];
* [2, 1];
* [3, 2];
* [3, 2];
* [1, 3];
* [1, 3];
* [2, 1].
In the second example the list of conversations will change in the following way:
* [];
* [2];
* [3, 2];
* [3, 2];
* [1, 3, 2];
* and then the list will not change till the end. | instruction | 0 | 105,343 | 14 | 210,686 |
Tags: data structures, implementation
Correct Solution:
```
from collections import deque
n,k=map(int,input().split())
q=list(map(int,input().split()))
z={}
w=deque()
for i in q:
if not(i in z and z[i]==1):
z[i]=1
if len(w)<k: w.append(i)
else:
x=w.popleft()
z[x]=-1
w.append(i)
print(len(w))
print(*reversed(w))
``` | output | 1 | 105,343 | 14 | 210,687 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions are constraints on n and k.
You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 0).
Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.
You (suddenly!) have the ability to see the future. You know that during the day you will receive n messages, the i-th message will be received from the friend with ID id_i (1 β€ id_i β€ 10^9).
If you receive a message from id_i in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.
Otherwise (i.e. if there is no conversation with id_i on the screen):
* Firstly, if the number of conversations displayed on the screen is k, the last conversation (which has the position k) is removed from the screen.
* Now the number of conversations on the screen is guaranteed to be less than k and the conversation with the friend id_i is not displayed on the screen.
* The conversation with the friend id_i appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.
Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all n messages.
Input
The first line of the input contains two integers n and k (1 β€ n, k β€ 2 β
10^5) β the number of messages and the number of conversations your smartphone can show.
The second line of the input contains n integers id_1, id_2, ..., id_n (1 β€ id_i β€ 10^9), where id_i is the ID of the friend which sends you the i-th message.
Output
In the first line of the output print one integer m (1 β€ m β€ min(n, k)) β the number of conversations shown after receiving all n messages.
In the second line print m integers ids_1, ids_2, ..., ids_m, where ids_i should be equal to the ID of the friend corresponding to the conversation displayed on the position i after receiving all n messages.
Examples
Input
7 2
1 2 3 2 1 3 2
Output
2
2 1
Input
10 4
2 3 3 1 1 2 1 2 3 3
Output
3
1 3 2
Note
In the first example the list of conversations will change in the following way (in order from the first to last message):
* [];
* [1];
* [2, 1];
* [3, 2];
* [3, 2];
* [1, 3];
* [1, 3];
* [2, 1].
In the second example the list of conversations will change in the following way:
* [];
* [2];
* [3, 2];
* [3, 2];
* [1, 3, 2];
* and then the list will not change till the end. | instruction | 0 | 105,344 | 14 | 210,688 |
Tags: data structures, implementation
Correct Solution:
```
n,k=map(int,input().split())
a=list(map(int,input().split()))
d=[]
s=set()
for i in a:
if i not in s:
d.append(i)
s.add(i)
if len(d)>k:
s.remove(d.pop(0))
print(len(d))
print(*d[::-1])
``` | output | 1 | 105,344 | 14 | 210,689 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions are constraints on n and k.
You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 0).
Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.
You (suddenly!) have the ability to see the future. You know that during the day you will receive n messages, the i-th message will be received from the friend with ID id_i (1 β€ id_i β€ 10^9).
If you receive a message from id_i in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.
Otherwise (i.e. if there is no conversation with id_i on the screen):
* Firstly, if the number of conversations displayed on the screen is k, the last conversation (which has the position k) is removed from the screen.
* Now the number of conversations on the screen is guaranteed to be less than k and the conversation with the friend id_i is not displayed on the screen.
* The conversation with the friend id_i appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.
Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all n messages.
Input
The first line of the input contains two integers n and k (1 β€ n, k β€ 2 β
10^5) β the number of messages and the number of conversations your smartphone can show.
The second line of the input contains n integers id_1, id_2, ..., id_n (1 β€ id_i β€ 10^9), where id_i is the ID of the friend which sends you the i-th message.
Output
In the first line of the output print one integer m (1 β€ m β€ min(n, k)) β the number of conversations shown after receiving all n messages.
In the second line print m integers ids_1, ids_2, ..., ids_m, where ids_i should be equal to the ID of the friend corresponding to the conversation displayed on the position i after receiving all n messages.
Examples
Input
7 2
1 2 3 2 1 3 2
Output
2
2 1
Input
10 4
2 3 3 1 1 2 1 2 3 3
Output
3
1 3 2
Note
In the first example the list of conversations will change in the following way (in order from the first to last message):
* [];
* [1];
* [2, 1];
* [3, 2];
* [3, 2];
* [1, 3];
* [1, 3];
* [2, 1].
In the second example the list of conversations will change in the following way:
* [];
* [2];
* [3, 2];
* [3, 2];
* [1, 3, 2];
* and then the list will not change till the end. | instruction | 0 | 105,345 | 14 | 210,690 |
Tags: data structures, implementation
Correct Solution:
```
import collections
def ii(): return int(input())
def fi(): return float(input())
def si(): return input()
def mi(): return map(int,input().split())
def li(): return list(mi())
n,k=mi()
d=li()
b=collections.deque([])
s=set(d)
l1=list(s)
di={}
for i in range(len(l1)):
di[l1[i]]=0
for i in range(n):
if(len(b)<k and di[d[i]]!=1):
b.appendleft(d[i])
di[d[i]]=1
else:
if(di[d[i]]!=1):
#print(b)
di[b[-1]]=0
#print(di)
b.pop()
b.appendleft(d[i])
di[d[i]]=1
#print("xx ")
#print(b)
l=list(b)
print(len(l))
for i in l:
print(i,end=" ")
``` | output | 1 | 105,345 | 14 | 210,691 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions are constraints on n and k.
You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 0).
Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.
You (suddenly!) have the ability to see the future. You know that during the day you will receive n messages, the i-th message will be received from the friend with ID id_i (1 β€ id_i β€ 10^9).
If you receive a message from id_i in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.
Otherwise (i.e. if there is no conversation with id_i on the screen):
* Firstly, if the number of conversations displayed on the screen is k, the last conversation (which has the position k) is removed from the screen.
* Now the number of conversations on the screen is guaranteed to be less than k and the conversation with the friend id_i is not displayed on the screen.
* The conversation with the friend id_i appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.
Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all n messages.
Input
The first line of the input contains two integers n and k (1 β€ n, k β€ 2 β
10^5) β the number of messages and the number of conversations your smartphone can show.
The second line of the input contains n integers id_1, id_2, ..., id_n (1 β€ id_i β€ 10^9), where id_i is the ID of the friend which sends you the i-th message.
Output
In the first line of the output print one integer m (1 β€ m β€ min(n, k)) β the number of conversations shown after receiving all n messages.
In the second line print m integers ids_1, ids_2, ..., ids_m, where ids_i should be equal to the ID of the friend corresponding to the conversation displayed on the position i after receiving all n messages.
Examples
Input
7 2
1 2 3 2 1 3 2
Output
2
2 1
Input
10 4
2 3 3 1 1 2 1 2 3 3
Output
3
1 3 2
Note
In the first example the list of conversations will change in the following way (in order from the first to last message):
* [];
* [1];
* [2, 1];
* [3, 2];
* [3, 2];
* [1, 3];
* [1, 3];
* [2, 1].
In the second example the list of conversations will change in the following way:
* [];
* [2];
* [3, 2];
* [3, 2];
* [1, 3, 2];
* and then the list will not change till the end. | instruction | 0 | 105,346 | 14 | 210,692 |
Tags: data structures, implementation
Correct Solution:
```
'''Author- Akshit Monga'''
from collections import deque
from sys import stdin,stdout
input=stdin.readline
t=1
for _ in range(t):
n,k=map(int,input().split())
a=[int(x) for x in input().split()]
d={}
arr=deque()
for i in a:
if not(i in d and d[i]==1):
d[i]=1
if len(arr)<k:
arr.append(i)
else:
val=arr.popleft()
d[val]=-1
arr.append(i)
stdout.write(str(len(arr))+'\n')
for i in range(len(arr)-1,-1,-1):
stdout.write(str(arr[i])+" ")
stdout.write('\n')
``` | output | 1 | 105,346 | 14 | 210,693 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions are constraints on n and k.
You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 0).
Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.
You (suddenly!) have the ability to see the future. You know that during the day you will receive n messages, the i-th message will be received from the friend with ID id_i (1 β€ id_i β€ 10^9).
If you receive a message from id_i in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.
Otherwise (i.e. if there is no conversation with id_i on the screen):
* Firstly, if the number of conversations displayed on the screen is k, the last conversation (which has the position k) is removed from the screen.
* Now the number of conversations on the screen is guaranteed to be less than k and the conversation with the friend id_i is not displayed on the screen.
* The conversation with the friend id_i appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.
Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all n messages.
Input
The first line of the input contains two integers n and k (1 β€ n, k β€ 2 β
10^5) β the number of messages and the number of conversations your smartphone can show.
The second line of the input contains n integers id_1, id_2, ..., id_n (1 β€ id_i β€ 10^9), where id_i is the ID of the friend which sends you the i-th message.
Output
In the first line of the output print one integer m (1 β€ m β€ min(n, k)) β the number of conversations shown after receiving all n messages.
In the second line print m integers ids_1, ids_2, ..., ids_m, where ids_i should be equal to the ID of the friend corresponding to the conversation displayed on the position i after receiving all n messages.
Examples
Input
7 2
1 2 3 2 1 3 2
Output
2
2 1
Input
10 4
2 3 3 1 1 2 1 2 3 3
Output
3
1 3 2
Note
In the first example the list of conversations will change in the following way (in order from the first to last message):
* [];
* [1];
* [2, 1];
* [3, 2];
* [3, 2];
* [1, 3];
* [1, 3];
* [2, 1].
In the second example the list of conversations will change in the following way:
* [];
* [2];
* [3, 2];
* [3, 2];
* [1, 3, 2];
* and then the list will not change till the end.
Submitted Solution:
```
from collections import deque
se=set([])
s=deque([])
n,k=map(int,input().split())
li=list(map(int,input().split()))
cnt=0
for i in li:
if i in se:
continue
if cnt<k:
s.append(i)
se.add(i)
cnt+=1
else:
s.append(i)
se.add(i)
se.discard(s[0])
s.popleft()
print(len(s))
print(*list(s)[::-1])
``` | instruction | 0 | 105,347 | 14 | 210,694 |
Yes | output | 1 | 105,347 | 14 | 210,695 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions are constraints on n and k.
You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 0).
Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.
You (suddenly!) have the ability to see the future. You know that during the day you will receive n messages, the i-th message will be received from the friend with ID id_i (1 β€ id_i β€ 10^9).
If you receive a message from id_i in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.
Otherwise (i.e. if there is no conversation with id_i on the screen):
* Firstly, if the number of conversations displayed on the screen is k, the last conversation (which has the position k) is removed from the screen.
* Now the number of conversations on the screen is guaranteed to be less than k and the conversation with the friend id_i is not displayed on the screen.
* The conversation with the friend id_i appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.
Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all n messages.
Input
The first line of the input contains two integers n and k (1 β€ n, k β€ 2 β
10^5) β the number of messages and the number of conversations your smartphone can show.
The second line of the input contains n integers id_1, id_2, ..., id_n (1 β€ id_i β€ 10^9), where id_i is the ID of the friend which sends you the i-th message.
Output
In the first line of the output print one integer m (1 β€ m β€ min(n, k)) β the number of conversations shown after receiving all n messages.
In the second line print m integers ids_1, ids_2, ..., ids_m, where ids_i should be equal to the ID of the friend corresponding to the conversation displayed on the position i after receiving all n messages.
Examples
Input
7 2
1 2 3 2 1 3 2
Output
2
2 1
Input
10 4
2 3 3 1 1 2 1 2 3 3
Output
3
1 3 2
Note
In the first example the list of conversations will change in the following way (in order from the first to last message):
* [];
* [1];
* [2, 1];
* [3, 2];
* [3, 2];
* [1, 3];
* [1, 3];
* [2, 1].
In the second example the list of conversations will change in the following way:
* [];
* [2];
* [3, 2];
* [3, 2];
* [1, 3, 2];
* and then the list will not change till the end.
Submitted Solution:
```
n,k=map(int,input().split())
l=list(map(int,input().split()))
a=[]
d={}
for i in range(n):
if len(a)<k:
if d.get(l[i],0):
#print(d)
pass
else:
# a.insert(0,l[i])
a.append(l[i])
d[l[i]]=1
# print(d)
else:
if d.get(l[i],0):
pass
else:
c=a.pop(0)
#a.pop()
a.append(l[i])
d[c]=0
d[l[i]]=1
# print(d,a)
print(len(a))
print(*a[::-1])
``` | instruction | 0 | 105,348 | 14 | 210,696 |
Yes | output | 1 | 105,348 | 14 | 210,697 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions are constraints on n and k.
You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 0).
Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.
You (suddenly!) have the ability to see the future. You know that during the day you will receive n messages, the i-th message will be received from the friend with ID id_i (1 β€ id_i β€ 10^9).
If you receive a message from id_i in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.
Otherwise (i.e. if there is no conversation with id_i on the screen):
* Firstly, if the number of conversations displayed on the screen is k, the last conversation (which has the position k) is removed from the screen.
* Now the number of conversations on the screen is guaranteed to be less than k and the conversation with the friend id_i is not displayed on the screen.
* The conversation with the friend id_i appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.
Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all n messages.
Input
The first line of the input contains two integers n and k (1 β€ n, k β€ 2 β
10^5) β the number of messages and the number of conversations your smartphone can show.
The second line of the input contains n integers id_1, id_2, ..., id_n (1 β€ id_i β€ 10^9), where id_i is the ID of the friend which sends you the i-th message.
Output
In the first line of the output print one integer m (1 β€ m β€ min(n, k)) β the number of conversations shown after receiving all n messages.
In the second line print m integers ids_1, ids_2, ..., ids_m, where ids_i should be equal to the ID of the friend corresponding to the conversation displayed on the position i after receiving all n messages.
Examples
Input
7 2
1 2 3 2 1 3 2
Output
2
2 1
Input
10 4
2 3 3 1 1 2 1 2 3 3
Output
3
1 3 2
Note
In the first example the list of conversations will change in the following way (in order from the first to last message):
* [];
* [1];
* [2, 1];
* [3, 2];
* [3, 2];
* [1, 3];
* [1, 3];
* [2, 1].
In the second example the list of conversations will change in the following way:
* [];
* [2];
* [3, 2];
* [3, 2];
* [1, 3, 2];
* and then the list will not change till the end.
Submitted Solution:
```
from collections import OrderedDict
class LRUCache:
def __init__(self, capacity):
self.capacity = capacity
self.cache = OrderedDict()
def set(self, key, value):
if len(self.cache) == self.capacity:
self.cache.popitem(last=False)
self.cache[key] = value
n, k = map(int, input().strip().split())
ids = map(int, input().strip().split())
convs = LRUCache(k)
for i in ids:
if i in convs.cache:
continue
else:
convs.set(i, None)
print(len(convs.cache))
print(" ".join(map(str, reversed(convs.cache.keys()))))
``` | instruction | 0 | 105,349 | 14 | 210,698 |
Yes | output | 1 | 105,349 | 14 | 210,699 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions are constraints on n and k.
You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 0).
Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.
You (suddenly!) have the ability to see the future. You know that during the day you will receive n messages, the i-th message will be received from the friend with ID id_i (1 β€ id_i β€ 10^9).
If you receive a message from id_i in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.
Otherwise (i.e. if there is no conversation with id_i on the screen):
* Firstly, if the number of conversations displayed on the screen is k, the last conversation (which has the position k) is removed from the screen.
* Now the number of conversations on the screen is guaranteed to be less than k and the conversation with the friend id_i is not displayed on the screen.
* The conversation with the friend id_i appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.
Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all n messages.
Input
The first line of the input contains two integers n and k (1 β€ n, k β€ 2 β
10^5) β the number of messages and the number of conversations your smartphone can show.
The second line of the input contains n integers id_1, id_2, ..., id_n (1 β€ id_i β€ 10^9), where id_i is the ID of the friend which sends you the i-th message.
Output
In the first line of the output print one integer m (1 β€ m β€ min(n, k)) β the number of conversations shown after receiving all n messages.
In the second line print m integers ids_1, ids_2, ..., ids_m, where ids_i should be equal to the ID of the friend corresponding to the conversation displayed on the position i after receiving all n messages.
Examples
Input
7 2
1 2 3 2 1 3 2
Output
2
2 1
Input
10 4
2 3 3 1 1 2 1 2 3 3
Output
3
1 3 2
Note
In the first example the list of conversations will change in the following way (in order from the first to last message):
* [];
* [1];
* [2, 1];
* [3, 2];
* [3, 2];
* [1, 3];
* [1, 3];
* [2, 1].
In the second example the list of conversations will change in the following way:
* [];
* [2];
* [3, 2];
* [3, 2];
* [1, 3, 2];
* and then the list will not change till the end.
Submitted Solution:
```
from collections import deque
def solution(arr, k: int) -> None:
s = set()
d = deque()
for x in arr:
if x not in s:
if len(d) == k:
s.discard(d.popleft())
s.add(x)
d.append(x)
print(len(d))
print(" ".join(map(str, reversed(d))))
def main():
_, k = map(int, input().split())
arr = map(int, input().split())
solution(arr, k)
if __name__ == "__main__":
main()
``` | instruction | 0 | 105,350 | 14 | 210,700 |
Yes | output | 1 | 105,350 | 14 | 210,701 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions are constraints on n and k.
You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 0).
Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.
You (suddenly!) have the ability to see the future. You know that during the day you will receive n messages, the i-th message will be received from the friend with ID id_i (1 β€ id_i β€ 10^9).
If you receive a message from id_i in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.
Otherwise (i.e. if there is no conversation with id_i on the screen):
* Firstly, if the number of conversations displayed on the screen is k, the last conversation (which has the position k) is removed from the screen.
* Now the number of conversations on the screen is guaranteed to be less than k and the conversation with the friend id_i is not displayed on the screen.
* The conversation with the friend id_i appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.
Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all n messages.
Input
The first line of the input contains two integers n and k (1 β€ n, k β€ 2 β
10^5) β the number of messages and the number of conversations your smartphone can show.
The second line of the input contains n integers id_1, id_2, ..., id_n (1 β€ id_i β€ 10^9), where id_i is the ID of the friend which sends you the i-th message.
Output
In the first line of the output print one integer m (1 β€ m β€ min(n, k)) β the number of conversations shown after receiving all n messages.
In the second line print m integers ids_1, ids_2, ..., ids_m, where ids_i should be equal to the ID of the friend corresponding to the conversation displayed on the position i after receiving all n messages.
Examples
Input
7 2
1 2 3 2 1 3 2
Output
2
2 1
Input
10 4
2 3 3 1 1 2 1 2 3 3
Output
3
1 3 2
Note
In the first example the list of conversations will change in the following way (in order from the first to last message):
* [];
* [1];
* [2, 1];
* [3, 2];
* [3, 2];
* [1, 3];
* [1, 3];
* [2, 1].
In the second example the list of conversations will change in the following way:
* [];
* [2];
* [3, 2];
* [3, 2];
* [1, 3, 2];
* and then the list will not change till the end.
Submitted Solution:
```
from collections import defaultdict as dc
import sys
import math
mod=10**9 +7
def inp():
p=sys.stdin.readline()
return p
def out(z):
sys.st
def prf(a,n,k):
q=dc(int)
i=0
c=0
while(c<=k or i<n):
if q[a[i]]==0:
c+=1
q[a[i]]=1
if c==k:
break
i+=1
if i>=n:
break
#print(q)
if i>=n:
return q,c
for j in range(i+1,n):
if q[a[j]]==0:
#print(q)
q[a[j]]=1
l=next(iter(q))
del q[l]
#print(q,l)
return q,c
n,m=list(map(int,inp().split()))
a=list(map(int,inp().split()))
z,c=prf(a,n,m)
#print(z)
print(min(c,m))
s=''
for i,j in z.items():
if j==1:
s=s+str(i)+' '
s=s[::-1]
s=s[1:]
print(s)
``` | instruction | 0 | 105,351 | 14 | 210,702 |
No | output | 1 | 105,351 | 14 | 210,703 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions are constraints on n and k.
You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 0).
Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.
You (suddenly!) have the ability to see the future. You know that during the day you will receive n messages, the i-th message will be received from the friend with ID id_i (1 β€ id_i β€ 10^9).
If you receive a message from id_i in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.
Otherwise (i.e. if there is no conversation with id_i on the screen):
* Firstly, if the number of conversations displayed on the screen is k, the last conversation (which has the position k) is removed from the screen.
* Now the number of conversations on the screen is guaranteed to be less than k and the conversation with the friend id_i is not displayed on the screen.
* The conversation with the friend id_i appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.
Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all n messages.
Input
The first line of the input contains two integers n and k (1 β€ n, k β€ 2 β
10^5) β the number of messages and the number of conversations your smartphone can show.
The second line of the input contains n integers id_1, id_2, ..., id_n (1 β€ id_i β€ 10^9), where id_i is the ID of the friend which sends you the i-th message.
Output
In the first line of the output print one integer m (1 β€ m β€ min(n, k)) β the number of conversations shown after receiving all n messages.
In the second line print m integers ids_1, ids_2, ..., ids_m, where ids_i should be equal to the ID of the friend corresponding to the conversation displayed on the position i after receiving all n messages.
Examples
Input
7 2
1 2 3 2 1 3 2
Output
2
2 1
Input
10 4
2 3 3 1 1 2 1 2 3 3
Output
3
1 3 2
Note
In the first example the list of conversations will change in the following way (in order from the first to last message):
* [];
* [1];
* [2, 1];
* [3, 2];
* [3, 2];
* [1, 3];
* [1, 3];
* [2, 1].
In the second example the list of conversations will change in the following way:
* [];
* [2];
* [3, 2];
* [3, 2];
* [1, 3, 2];
* and then the list will not change till the end.
Submitted Solution:
```
n, k = [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
scr = []
s = set()
for msg in l:
if msg not in s:
s.add(msg)
scr.append(msg)
if len(scr) > k:
s -= {scr[0]}
scr = scr[:-1]
print(len(scr))
print(*reversed(scr))
``` | instruction | 0 | 105,352 | 14 | 210,704 |
No | output | 1 | 105,352 | 14 | 210,705 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions are constraints on n and k.
You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 0).
Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.
You (suddenly!) have the ability to see the future. You know that during the day you will receive n messages, the i-th message will be received from the friend with ID id_i (1 β€ id_i β€ 10^9).
If you receive a message from id_i in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.
Otherwise (i.e. if there is no conversation with id_i on the screen):
* Firstly, if the number of conversations displayed on the screen is k, the last conversation (which has the position k) is removed from the screen.
* Now the number of conversations on the screen is guaranteed to be less than k and the conversation with the friend id_i is not displayed on the screen.
* The conversation with the friend id_i appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.
Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all n messages.
Input
The first line of the input contains two integers n and k (1 β€ n, k β€ 2 β
10^5) β the number of messages and the number of conversations your smartphone can show.
The second line of the input contains n integers id_1, id_2, ..., id_n (1 β€ id_i β€ 10^9), where id_i is the ID of the friend which sends you the i-th message.
Output
In the first line of the output print one integer m (1 β€ m β€ min(n, k)) β the number of conversations shown after receiving all n messages.
In the second line print m integers ids_1, ids_2, ..., ids_m, where ids_i should be equal to the ID of the friend corresponding to the conversation displayed on the position i after receiving all n messages.
Examples
Input
7 2
1 2 3 2 1 3 2
Output
2
2 1
Input
10 4
2 3 3 1 1 2 1 2 3 3
Output
3
1 3 2
Note
In the first example the list of conversations will change in the following way (in order from the first to last message):
* [];
* [1];
* [2, 1];
* [3, 2];
* [3, 2];
* [1, 3];
* [1, 3];
* [2, 1].
In the second example the list of conversations will change in the following way:
* [];
* [2];
* [3, 2];
* [3, 2];
* [1, 3, 2];
* and then the list will not change till the end.
Submitted Solution:
```
import queue
def ii(): return int(input())
def fi(): return float(input())
def si(): return input()
def mi(): return map(int,input().split())
def li(): return list(mi())
n,k=mi()
d=li()
b=[]
s=set(d)
l1=list(s)
di={}
l=queue.LifoQueue(maxsize=k+1)
for i in range(len(l1)):
di[l1[i]]=0
for i in range(n):
if(l.qsize()<k and di[d[i]]!=1):
l.put(d[i])
di[d[i]]=1
else:
if(di[d[i]]!=1):
#print(b)
p=l.get()
di[p]=0
#print(di
l.put(d[i])
di[d[i]]=1
#print(b)
print(l.qsize())
for i in range(l.qsize()):
print(l.get(),end=" ")
``` | instruction | 0 | 105,353 | 14 | 210,706 |
No | output | 1 | 105,353 | 14 | 210,707 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions are constraints on n and k.
You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 0).
Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.
You (suddenly!) have the ability to see the future. You know that during the day you will receive n messages, the i-th message will be received from the friend with ID id_i (1 β€ id_i β€ 10^9).
If you receive a message from id_i in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.
Otherwise (i.e. if there is no conversation with id_i on the screen):
* Firstly, if the number of conversations displayed on the screen is k, the last conversation (which has the position k) is removed from the screen.
* Now the number of conversations on the screen is guaranteed to be less than k and the conversation with the friend id_i is not displayed on the screen.
* The conversation with the friend id_i appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.
Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all n messages.
Input
The first line of the input contains two integers n and k (1 β€ n, k β€ 2 β
10^5) β the number of messages and the number of conversations your smartphone can show.
The second line of the input contains n integers id_1, id_2, ..., id_n (1 β€ id_i β€ 10^9), where id_i is the ID of the friend which sends you the i-th message.
Output
In the first line of the output print one integer m (1 β€ m β€ min(n, k)) β the number of conversations shown after receiving all n messages.
In the second line print m integers ids_1, ids_2, ..., ids_m, where ids_i should be equal to the ID of the friend corresponding to the conversation displayed on the position i after receiving all n messages.
Examples
Input
7 2
1 2 3 2 1 3 2
Output
2
2 1
Input
10 4
2 3 3 1 1 2 1 2 3 3
Output
3
1 3 2
Note
In the first example the list of conversations will change in the following way (in order from the first to last message):
* [];
* [1];
* [2, 1];
* [3, 2];
* [3, 2];
* [1, 3];
* [1, 3];
* [2, 1].
In the second example the list of conversations will change in the following way:
* [];
* [2];
* [3, 2];
* [3, 2];
* [1, 3, 2];
* and then the list will not change till the end.
Submitted Solution:
```
n, k = list(map(int, input().split(' ')))
lastSeen = {}
curr = 0
arr = list(map(int, input().split(' ')))
for i in arr:
if i in lastSeen:
if(curr - lastSeen[i] > k):
lastSeen[i] = curr
curr += 1
else:
lastSeen[i] = curr
curr += 1
import operator
sorted_x = sorted(lastSeen.items(), key=operator.itemgetter(1), reverse=True)
ans = list(map(lambda x:str(x[0]), sorted_x))
print(len(ans))
print(' '.join(ans))
``` | instruction | 0 | 105,354 | 14 | 210,708 |
No | output | 1 | 105,354 | 14 | 210,709 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum.
For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny.
Write a program that will print the name of a man who will drink the n-th can.
Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon.
Input
The input data consist of a single integer n (1 β€ n β€ 109).
It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers.
Output
Print the single line β the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the quotes). In that order precisely the friends are in the queue initially.
Examples
Input
1
Output
Sheldon
Input
6
Output
Sheldon
Input
1802
Output
Penny | instruction | 0 | 105,804 | 14 | 211,608 |
Tags: implementation, math
Correct Solution:
```
import math
name=['Sheldon','Leonard','Penny','Rajesh','Howard']
n=int(input())
for i in range(100000000):
if math.floor(n/5)-(2**i-1)<2**i:
h=i
#print(h)
print(name[math.ceil((n-5*(2**h-1))/2**h-1)])
exit()
``` | output | 1 | 105,804 | 14 | 211,609 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum.
For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny.
Write a program that will print the name of a man who will drink the n-th can.
Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon.
Input
The input data consist of a single integer n (1 β€ n β€ 109).
It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers.
Output
Print the single line β the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the quotes). In that order precisely the friends are in the queue initially.
Examples
Input
1
Output
Sheldon
Input
6
Output
Sheldon
Input
1802
Output
Penny | instruction | 0 | 105,805 | 14 | 211,610 |
Tags: implementation, math
Correct Solution:
```
n = int(input())
while(n>5):
n = n-4
n = n/2
n = int(n)
l = ["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"]
print(l[n-1])
``` | output | 1 | 105,805 | 14 | 211,611 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum.
For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny.
Write a program that will print the name of a man who will drink the n-th can.
Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon.
Input
The input data consist of a single integer n (1 β€ n β€ 109).
It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers.
Output
Print the single line β the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the quotes). In that order precisely the friends are in the queue initially.
Examples
Input
1
Output
Sheldon
Input
6
Output
Sheldon
Input
1802
Output
Penny | instruction | 0 | 105,806 | 14 | 211,612 |
Tags: implementation, math
Correct Solution:
```
n = int(input())
s = 1+float(n)/5
import math
k = math.ceil((math.log(s,2)))
deta = n-5*(2**(k-1)-1)
test = math.ceil(deta/(2**(k-1)))
if test == 1:
print("Sheldon")
if test == 2:
print("Leonard")
if test == 3:
print("Penny")
if test == 4:
print("Rajesh")
if test == 5:
print("Howard")
``` | output | 1 | 105,806 | 14 | 211,613 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum.
For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny.
Write a program that will print the name of a man who will drink the n-th can.
Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon.
Input
The input data consist of a single integer n (1 β€ n β€ 109).
It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers.
Output
Print the single line β the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the quotes). In that order precisely the friends are in the queue initially.
Examples
Input
1
Output
Sheldon
Input
6
Output
Sheldon
Input
1802
Output
Penny | instruction | 0 | 105,807 | 14 | 211,614 |
Tags: implementation, math
Correct Solution:
```
import sys
class Scanner:
def __init__(self):
self.current_tokens = []
def remaining_tokens(self):
return len(self.current_tokens)
def nextline(self):
assert self.remaining_tokens() == 0, "Reading next line with remaining tokens"
return input()
def nexttokens(self):
return self.nextline().split()
def nexttoken(self):
if len(self.current_tokens) == 0:
self.current_tokens = self.nexttokens()
assert self.remaining_tokens() > 0, "Not enough tokens to parse."
return self.current_tokens.pop(0)
def nextints(self, n=-1):
if n == -1:
return list(map(int, self.nexttokens()))
else:
return (self.nextint() for i in range(n))
def nextint(self):
return int(self.nexttoken())
def quit():
sys.exit(0)
stdin = Scanner()
nextint = stdin.nextint
nextints = stdin.nextints
nextline = stdin.nextline
n = nextint() - 1
itr = 0
i = 0
r = ['Sheldon', 'Leonard', 'Penny', 'Rajesh', 'Howard']
while i <= n:
for j in range(5):
if n in range(i, i + pow(2, itr)):
print(r[j])
quit()
i += pow(2, itr)
itr += 1
``` | output | 1 | 105,807 | 14 | 211,615 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum.
For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny.
Write a program that will print the name of a man who will drink the n-th can.
Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon.
Input
The input data consist of a single integer n (1 β€ n β€ 109).
It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers.
Output
Print the single line β the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the quotes). In that order precisely the friends are in the queue initially.
Examples
Input
1
Output
Sheldon
Input
6
Output
Sheldon
Input
1802
Output
Penny | instruction | 0 | 105,808 | 14 | 211,616 |
Tags: implementation, math
Correct Solution:
```
import math
def doubleCola(n: int) -> str:
dictionary = {
1:'Sheldon',
2:'Leonard',
3:'Penny',
4:'Rajesh',
5:'Howard'
}
lowerBound = 1
upperBound = 5
currentNumber = 1
while n > upperBound:
currentNumber *= 2
lowerBound = 1 + upperBound
upperBound += 5 * currentNumber
difference = lowerBound - 1
return dictionary.get(math.ceil((n-difference)/currentNumber))
def main():
n = int(input().strip())
print(doubleCola(n))
main()
``` | output | 1 | 105,808 | 14 | 211,617 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum.
For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny.
Write a program that will print the name of a man who will drink the n-th can.
Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon.
Input
The input data consist of a single integer n (1 β€ n β€ 109).
It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers.
Output
Print the single line β the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the quotes). In that order precisely the friends are in the queue initially.
Examples
Input
1
Output
Sheldon
Input
6
Output
Sheldon
Input
1802
Output
Penny | instruction | 0 | 105,809 | 14 | 211,618 |
Tags: implementation, math
Correct Solution:
```
# A. Double Cola
# 82A
names = ['Sheldon', 'Leonard', 'Penny', 'Rajesh', 'Howard']
n = int(input()) - 1
# print(f'n is initially {n}')
r = 0
while n >= 5 * 2**r:
# print()
n -= 5 * 2**r
# print(f'subtracting n by {5 * 2**r}')
r += 1
# print(f'Now n is {n} and r is {r}')
n //= 2**r
# print(f'Since 2**r is {2**r} and r is {r}, changing n to {n}')
assert(n < 5)
print(names[n])
# print(names)
``` | output | 1 | 105,809 | 14 | 211,619 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum.
For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny.
Write a program that will print the name of a man who will drink the n-th can.
Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon.
Input
The input data consist of a single integer n (1 β€ n β€ 109).
It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers.
Output
Print the single line β the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the quotes). In that order precisely the friends are in the queue initially.
Examples
Input
1
Output
Sheldon
Input
6
Output
Sheldon
Input
1802
Output
Penny | instruction | 0 | 105,810 | 14 | 211,620 |
Tags: implementation, math
Correct Solution:
```
import math
n = int(input())
for i in range(1, 30):
if 5 * (2 ** (i - 1) - 1) < n <= 5 * (2 ** i - 1):
k = i
break
man = ["null","Sheldon","Leonard","Penny","Rajesh","Howard"]
c = math.ceil((n - 5 * (2 ** (k - 1) - 1)) / (2 ** (k - 1)))
print(man[c])
``` | output | 1 | 105,810 | 14 | 211,621 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum.
For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny.
Write a program that will print the name of a man who will drink the n-th can.
Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon.
Input
The input data consist of a single integer n (1 β€ n β€ 109).
It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers.
Output
Print the single line β the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the quotes). In that order precisely the friends are in the queue initially.
Examples
Input
1
Output
Sheldon
Input
6
Output
Sheldon
Input
1802
Output
Penny | instruction | 0 | 105,811 | 14 | 211,622 |
Tags: implementation, math
Correct Solution:
```
from math import ceil
d = {0: "Sheldon", 1: "Leonard", 2: "Penny", 3: "Rajesh", 4: "Howard"}
n = int(input())
i = 1
while n > i*5:
n -= i*5
i *= 2
print(d[ceil(n/i)-1])
``` | output | 1 | 105,811 | 14 | 211,623 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum.
For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny.
Write a program that will print the name of a man who will drink the n-th can.
Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon.
Input
The input data consist of a single integer n (1 β€ n β€ 109).
It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers.
Output
Print the single line β the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the quotes). In that order precisely the friends are in the queue initially.
Examples
Input
1
Output
Sheldon
Input
6
Output
Sheldon
Input
1802
Output
Penny
Submitted Solution:
```
a=["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"]
n=int(input())
n-=1
while(n>=5):
n=(n-5)//2
print(a[n])
``` | instruction | 0 | 105,812 | 14 | 211,624 |
Yes | output | 1 | 105,812 | 14 | 211,625 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum.
For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny.
Write a program that will print the name of a man who will drink the n-th can.
Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon.
Input
The input data consist of a single integer n (1 β€ n β€ 109).
It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers.
Output
Print the single line β the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the quotes). In that order precisely the friends are in the queue initially.
Examples
Input
1
Output
Sheldon
Input
6
Output
Sheldon
Input
1802
Output
Penny
Submitted Solution:
```
import math
n =int(input())
a=["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"]
if n<=5:
print(a[n-1])
else:
sum=5
z=0
while(sum<n):
z+=1
sum+=2**z*5
# # print(z)
# if(n>=2**(z)*5):
# else:
# k=n-2**(z-1)*5
# print(sum,z)
k=n-sum+(2**(z)*5)
# print(k)
l=math.ceil(k/2**(z))
# print(l)
print(a[l-1])
``` | instruction | 0 | 105,813 | 14 | 211,626 |
Yes | output | 1 | 105,813 | 14 | 211,627 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum.
For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny.
Write a program that will print the name of a man who will drink the n-th can.
Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon.
Input
The input data consist of a single integer n (1 β€ n β€ 109).
It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers.
Output
Print the single line β the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the quotes). In that order precisely the friends are in the queue initially.
Examples
Input
1
Output
Sheldon
Input
6
Output
Sheldon
Input
1802
Output
Penny
Submitted Solution:
```
from collections import deque
def f(n):
q = ["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"]
while n >5:
if n%2 == 1:
n -= 5
n //= 2
else:
n -=4
n //=2
return q[n-1]
n = int(input())
print(f(n))
``` | instruction | 0 | 105,814 | 14 | 211,628 |
Yes | output | 1 | 105,814 | 14 | 211,629 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum.
For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny.
Write a program that will print the name of a man who will drink the n-th can.
Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon.
Input
The input data consist of a single integer n (1 β€ n β€ 109).
It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers.
Output
Print the single line β the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the quotes). In that order precisely the friends are in the queue initially.
Examples
Input
1
Output
Sheldon
Input
6
Output
Sheldon
Input
1802
Output
Penny
Submitted Solution:
```
# WHERE: https://codeforces.com/problemset/page/8?order=BY_RATING_ASC
# Taxi
# https://codeforces.com/problemset/problem/158/B
# Input
# The first line contains integer n (1ββ€βnββ€β105) β the number of groups of schoolchildren. The second line contains a sequence of integers s1,βs2,β...,βsn (1ββ€βsiββ€β4). The integers are separated by a space, si is the number of children in the i-th group.
# Output
# Print the single number β the minimum number of taxis necessary to drive all children to Polycarpus.
# ignoreInput = input()
# groups = list(map(int, input().split()))
# i = 0
# j = len(groups) - 1 # last position of the array
# counter = 0
# counters = [0, 0, 0, 0]
# for group in groups:
# counters[(group-1)] += 1
# fours = counters[3]
# threes = counters[2]
# ones = 0
# if counters[0] > threes:
# ones = counters[0] - threes
# twos = (counters[1]//2) + (((counters[1] % 2)*2+ones)//4)
# # left = 0
# if (((counters[1] % 2)*2+ones) % 4) != 0:
# twos += 1
# print(fours+threes+twos)
# ---------------------------------------------------------------------------------------------------------------------
# Fancy Fence
# https://codeforces.com/problemset/problem/270/A
# Input
# The first line of input contains an integer t (0β<βtβ<β180) β the number of tests. Each of the following t lines contains a single integer a (0β<βaβ<β180) β the angle the robot can make corners at measured in degrees.
# Output
# For each test, output on a single line "YES" (without quotes), if the robot can build a fence Emuskald wants, and "NO" (without quotes), if it is impossible.
# times = int(input())
# answers = []
# for time in range(times):
# a = int(input())
# if 360 % (180 - a) == 0:
# answers.append("YES")
# else:
# answers.append("NO")
# for answer in answers:
# print(answer)
# ---------------------------------------------------------------------------------------------------------------------
# Interesting drink
# https://codeforces.com/problemset/problem/706/B
# Input
# The first line of the input contains a single integer n (1ββ€βnββ€β100β000) β the number of shops in the city that sell Vasiliy's favourite drink.
# The second line contains n integers xi (1ββ€βxiββ€β100β000) β prices of the bottles of the drink in the i-th shop.
# The third line contains a single integer q (1ββ€βqββ€β100β000) β the number of days Vasiliy plans to buy the drink.
# Then follow q lines each containing one integer mi (1ββ€βmiββ€β109) β the number of coins Vasiliy can spent on the i-th day.
# Output
# Print q integers. The i-th of them should be equal to the number of shops where Vasiliy will be able to buy a bottle of the drink on the i-th day.
# nShops = int(input())
# shopPrices = list(map(int, input().split()))
# shopPrices.sort()
# times = int(input())
# answers = []
# def binarySearch(array, target, carry):
# index = len(array)
# if len(array) == 0:
# return carry
# if index == 1:
# if target < array[0]:
# return carry
# else:
# return carry + 1
# # position in the middle
# index = index//2
# if target < array[index]:
# # return the left
# newPrices = array[0:index]
# return binarySearch(newPrices, target, carry)
# else:
# # return the right
# carry += (index)
# newPrices = array[index:]
# return binarySearch(newPrices, target, carry)
# def iterativeBinary(array, target):
# low = 0
# high = len(array) - 1
# while (low <= high):
# mid = low + ((high-low)//2)
# if array[mid] > target:
# high = mid - 1
# else:
# low = mid + 1
# return low
# for time in range(times):
# money = int(input())
# # looks like the way i implemented the binary search isnt logN :(
# # buys = binarySearch(shopPrices, money, 0)
# buys = iterativeBinary(shopPrices, money)
# answers.append(buys)
# for answer in answers:
# print(answer)
# ---------------------------------------------------------------------------------------------------------------------
# A and B and Compilation Errors
# https://codeforces.com/problemset/problem/519/B
# Output
# Print two numbers on a single line: the numbers of the compilation errors that disappeared after B made the first and the second correction, respectively.
# compilationTimes = int(input())
# errors1 = list(map(int, input().split()))
# errors2 = list(map(int, input().split()))
# errors3 = list(map(int, input().split()))
# errors1.sort()
# errors2.sort()
# errors3.sort()
# n = -1
# isNFound = False
# m = 1
# isMFound = False
# for time in range(compilationTimes):
# if isNFound and isMFound:
# break
# if not isNFound:
# if time < len(errors2):
# if errors1[time] != errors2[time]:
# n = errors1[time]
# isNFound = True
# else:
# n = errors1[time]
# isNFound = True
# if not isMFound:
# if time < len(errors3):
# if errors2[time] != errors3[time]:
# m = errors2[time]
# isMFound = True
# else:
# m = errors2[time]
# isNFound = True
# print(n)
# print(m)
# ---------------------------------------------------------------------------------------------------------------------
target = int(input())
people = ["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"]
target -= 1
k = 0
while True:
if 5 * (2**k - 1) > target:
break
k += 1
k -= 1
# print(k)
# print((5 * (2**k - 1)))
corrimiento = target - (5 * (2**k - 1))
realK = 2**(k)
positionArray = (corrimiento)//realK
# print(realK)
# print(corrimiento)
print(people[positionArray])
# ---------------------------------------------------------------------------------------------------------------------
``` | instruction | 0 | 105,815 | 14 | 211,630 |
Yes | output | 1 | 105,815 | 14 | 211,631 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum.
For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny.
Write a program that will print the name of a man who will drink the n-th can.
Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon.
Input
The input data consist of a single integer n (1 β€ n β€ 109).
It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers.
Output
Print the single line β the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the quotes). In that order precisely the friends are in the queue initially.
Examples
Input
1
Output
Sheldon
Input
6
Output
Sheldon
Input
1802
Output
Penny
Submitted Solution:
```
n = int(input())
k = 1
max = 0
while True:
max = max + 2**k * 5
min = max - (2**k * 5)
if min <= n <= max:
break
k += 1
if (n - min) % 2**k == 0:
who = (n - min) // 2**k
elif (n - min) % 2**k != 0:
who = ((n - min) // 2**k) + 1
if who == 1:
print("Sheldon")
elif who == 2:
print("Leonard")
elif who == 3:
print("Penny")
elif who == 4:
print("Rajesh")
elif who == 5:
print("Howard" )
``` | instruction | 0 | 105,816 | 14 | 211,632 |
No | output | 1 | 105,816 | 14 | 211,633 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum.
For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny.
Write a program that will print the name of a man who will drink the n-th can.
Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon.
Input
The input data consist of a single integer n (1 β€ n β€ 109).
It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers.
Output
Print the single line β the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the quotes). In that order precisely the friends are in the queue initially.
Examples
Input
1
Output
Sheldon
Input
6
Output
Sheldon
Input
1802
Output
Penny
Submitted Solution:
```
n = int(input())
text = ["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"]
while n > len(text):
n -= len(text)
print(text[n - 1])
``` | instruction | 0 | 105,817 | 14 | 211,634 |
No | output | 1 | 105,817 | 14 | 211,635 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum.
For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny.
Write a program that will print the name of a man who will drink the n-th can.
Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon.
Input
The input data consist of a single integer n (1 β€ n β€ 109).
It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers.
Output
Print the single line β the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the quotes). In that order precisely the friends are in the queue initially.
Examples
Input
1
Output
Sheldon
Input
6
Output
Sheldon
Input
1802
Output
Penny
Submitted Solution:
```
import sys
import math
import os
names=["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"]
n=int(input())
k=1
cur=5*k
while cur < n:
k+=1
cur+=5*k
#print(cur)
n-=(cur-k*5)
#print(n,k)
#print(int(n/k))
delta=0
if n%k>0:
delta=1
print(names[int(n/k)+delta-1])
``` | instruction | 0 | 105,818 | 14 | 211,636 |
No | output | 1 | 105,818 | 14 | 211,637 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum.
For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny.
Write a program that will print the name of a man who will drink the n-th can.
Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon.
Input
The input data consist of a single integer n (1 β€ n β€ 109).
It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers.
Output
Print the single line β the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the quotes). In that order precisely the friends are in the queue initially.
Examples
Input
1
Output
Sheldon
Input
6
Output
Sheldon
Input
1802
Output
Penny
Submitted Solution:
```
ans=["Sheldon", "Leonard", "Penny", "Rajesh" ,"Howard"]
x=int(input())
cur=int(1)
while cur*5<=x:
x-=(cur*5)
cur*=2
print(ans[x%5-1])
``` | instruction | 0 | 105,819 | 14 | 211,638 |
No | output | 1 | 105,819 | 14 | 211,639 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a set of n weights. You know that their masses are a_1, a_2, ..., a_n grams, but you don't know which of them has which mass. You can't distinguish the weights.
However, your friend does know the mass of each weight. You can ask your friend to give you exactly k weights with the total mass m (both parameters k and m are chosen by you), and your friend will point to any valid subset of weights, if it is possible.
You are allowed to make this query only once. Find the maximum possible number of weights you can reveal after this query.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of weights.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 100) β the masses of the weights.
Output
Print the maximum number of weights you can learn the masses for after making a single query.
Examples
Input
4
1 4 2 2
Output
2
Input
6
1 2 4 4 4 9
Output
2
Note
In the first example we can ask for a subset of two weights with total mass being equal to 4, and the only option is to get \{2, 2\}.
Another way to obtain the same result is to ask for a subset of two weights with the total mass of 5 and get \{1, 4\}. It is easy to see that the two remaining weights have mass of 2 grams each.
In the second example we can ask for a subset of two weights with total mass being 8, and the only answer is \{4, 4\}. We can prove it is not possible to learn masses for three weights in one query, but we won't put the proof here. | instruction | 0 | 106,147 | 14 | 212,294 |
Tags: dp, math
Correct Solution:
```
from collections import defaultdict
def calcBinomials(N):
global binom
N += 1
binom = [[0]*N for _ in range(N)]
for n in range(N):
binom[n][0] = binom[n][n] = 1
for k in range(1, n):
binom[n][k] = binom[n-1][k] + binom[n-1][k-1]
n = int(input())
a = list(map(int, input().split()))
S, res = sum(a), 0
dp = [defaultdict(lambda: 0) for _ in range(S+1)]
dp[0][0] = 1
cnt = {_:a.count(_) for _ in a}
for x in a:
for i in range(len(dp)-1-x, -1, -1):
for k, v in dp[i].items():
dp[i+x][k+1] += v
calcBinomials(n)
for x, c in cnt.items():
for i in range(1, c+1):
if dp[x*i][i] == binom[c][i] or dp[S - x*i][n-i] == binom[c][c-i]:
res = max(res, i)
if len(cnt) <= 2: res = n
print(res)
``` | output | 1 | 106,147 | 14 | 212,295 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.
In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.
Du will chat in the group for n days. On the i-th day:
* If Du can speak, he'll make fun of Boboniu with fun factor a_i. But after that, he may be muzzled depending on Boboniu's mood.
* Otherwise, Du won't do anything.
Boboniu's mood is a constant m. On the i-th day:
* If Du can speak and a_i>m, then Boboniu will be angry and muzzle him for d days, which means that Du won't be able to speak on the i+1, i+2, β
β
β
, min(i+d,n)-th days.
* Otherwise, Boboniu won't do anything.
The total fun factor is the sum of the fun factors on the days when Du can speak.
Du asked you to find the maximum total fun factor among all possible permutations of a.
Input
The first line contains three integers n, d and m (1β€ dβ€ nβ€ 10^5,0β€ mβ€ 10^9).
The next line contains n integers a_1, a_2, β¦,a_n (0β€ a_iβ€ 10^9).
Output
Print one integer: the maximum total fun factor among all permutations of a.
Examples
Input
5 2 11
8 10 15 23 5
Output
48
Input
20 2 16
20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7
Output
195
Note
In the first example, you can set a'=[15, 5, 8, 10, 23]. Then Du's chatting record will be:
1. Make fun of Boboniu with fun factor 15.
2. Be muzzled.
3. Be muzzled.
4. Make fun of Boboniu with fun factor 10.
5. Make fun of Boboniu with fun factor 23.
Thus the total fun factor is 48. | instruction | 0 | 106,321 | 14 | 212,642 |
Tags: dp, greedy, sortings, two pointers
Correct Solution:
```
import math,sys,bisect,heapq,os
from collections import defaultdict,Counter,deque
from itertools import groupby,accumulate
from functools import lru_cache
#sys.setrecursionlimit(200000000)
pr = lambda x: x
def input(): return sys.stdin.readline().rstrip('\r\n')
#input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
aj = lambda: list(map(int, input().split()))
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
#MOD = 1000000000 + 7
def Y(c): print(["NO","YES"][c])
def y(c): print(["no","yes"][c])
def Yy(c): print(["No","Yes"][c])
def solve():
n,d,m = aj()
A = aj()
P = []
M = []
for i in A:
if i > m:
P.append(i)
else:
M.append(i)
P.sort(reverse = True)
M.sort(reverse = True)
if not P:
print(sum(M))
return
ans = sum(M) + P[0]
usedp =1
usedm =len(M)
tot = ans
while 0<= usedp <= len(P) and 0 <= usedm <= len(M):
rem = n - (usedp+usedm+1) #if we used 1 extra
if rem - usedp*d >= 0:
usedp += 1
if usedp > len(P):
break
tot += P[usedp - 1]
else:
if usedm == 0:
break
tot -= M[usedm-1]
usedm -=1
ans= max(ans,tot)
print(ans)
try:
#os.system("online_judge.py")
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
from aj import *
except:
pass
solve()
``` | output | 1 | 106,321 | 14 | 212,643 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.