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.
BerPhone X is almost ready for release with n applications being preinstalled on the phone. A category of an application characterizes a genre or a theme of this application (like "game", "business", or "education"). The categories are given as integers between 1 and n, inclusive; the i-th application has category c_i.
You can choose m — the number of screens and s — the size of each screen. You need to fit all n icons of the applications (one icon representing one application) meeting the following requirements:
* On each screen, all the icons must belong to applications of the same category (but different screens can contain icons of applications of the same category);
* Each screen must be either completely filled with icons (the number of icons on the screen is equal to s) or almost filled with icons (the number of icons is equal to s-1).
Your task is to find the minimal possible number of screens m.
Input
The first line contains an integer t (1 ≤ t ≤ 10 000) — the number of test cases in the input. Then t test cases follow.
The first line of each test case contains an integer n (1 ≤ n ≤ 2⋅10^6) — the number of the icons. The second line contains n integers c_1, c_2, ..., c_n (1 ≤ c_i ≤ n), where c_i is the category of the i-th application.
It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 2⋅10^6.
Output
Print t integers — the answers to the given test cases in the order they follow in the input. The answer to a test case is an integer m — the minimum number of screens on which all n icons can be placed satisfying the given requirements.
Example
Input
3
11
1 5 1 5 1 5 1 1 1 1 5
6
1 2 2 2 2 1
5
4 3 3 1 2
Output
3
3
4
Note
In the first test case of the example, all the icons can be placed on three screens of size 4: a screen with 4 icons of the category 1, a screen with 3 icons of the category 1, and a screen with 4 icons of the category 5. | instruction | 0 | 101,322 | 14 | 202,644 |
Tags: greedy, implementation, math
Correct Solution:
```
from math import ceil, inf
for _ in range(int(input())):
n=int(input())
a=[int(X)-1 for X in input().split()]
x=[]
z=[0]*n
for i in range(n):
z[a[i]]+=1
for i in z:
if i:
x.append(i)
x.sort()
# print(x)
an=inf
for i in range(1,x[0]+2):
mi=0
for j in x :
if j>=ceil(j/i)*(i-1):
mi+=ceil(j/i)
else:
mi=inf
an=min(mi,an)
print(an)
``` | output | 1 | 101,322 | 14 | 202,645 |
Provide tags and a correct Python 3 solution for this coding contest problem.
BerPhone X is almost ready for release with n applications being preinstalled on the phone. A category of an application characterizes a genre or a theme of this application (like "game", "business", or "education"). The categories are given as integers between 1 and n, inclusive; the i-th application has category c_i.
You can choose m — the number of screens and s — the size of each screen. You need to fit all n icons of the applications (one icon representing one application) meeting the following requirements:
* On each screen, all the icons must belong to applications of the same category (but different screens can contain icons of applications of the same category);
* Each screen must be either completely filled with icons (the number of icons on the screen is equal to s) or almost filled with icons (the number of icons is equal to s-1).
Your task is to find the minimal possible number of screens m.
Input
The first line contains an integer t (1 ≤ t ≤ 10 000) — the number of test cases in the input. Then t test cases follow.
The first line of each test case contains an integer n (1 ≤ n ≤ 2⋅10^6) — the number of the icons. The second line contains n integers c_1, c_2, ..., c_n (1 ≤ c_i ≤ n), where c_i is the category of the i-th application.
It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 2⋅10^6.
Output
Print t integers — the answers to the given test cases in the order they follow in the input. The answer to a test case is an integer m — the minimum number of screens on which all n icons can be placed satisfying the given requirements.
Example
Input
3
11
1 5 1 5 1 5 1 1 1 1 5
6
1 2 2 2 2 1
5
4 3 3 1 2
Output
3
3
4
Note
In the first test case of the example, all the icons can be placed on three screens of size 4: a screen with 4 icons of the category 1, a screen with 3 icons of the category 1, and a screen with 4 icons of the category 5. | instruction | 0 | 101,323 | 14 | 202,646 |
Tags: greedy, implementation, math
Correct Solution:
```
"""
Satwik_Tiwari ;) .
12th Sept , 2020 - Saturday
"""
#===============================================================================================
#importing some useful libraries.
from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter # Counter(list) return a dict with {key: count}
from itertools import combinations as comb # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)]
from itertools import permutations as permutate
from bisect import bisect_left as bl
#If the element is already present in the list,
# the left most position where element has to be inserted is returned.
from bisect import bisect_right as br
from bisect import bisect
#If the element is already present in the list,
# the right most position where element has to be inserted is returned
#==============================================================================================
#fast I/O region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
# inp = lambda: sys.stdin.readline().rstrip("\r\n")
#===============================================================================================
### START ITERATE RECURSION ###
from types import GeneratorType
def iterative(f, stack=[]):
def wrapped_func(*args, **kwargs):
if stack: return f(*args, **kwargs)
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
continue
stack.pop()
if not stack: break
to = stack[-1].send(to)
return to
return wrapped_func
#### END ITERATE RECURSION ####
#===============================================================================================
#some shortcuts
mod = 1000000007
def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input
def out(var): sys.stdout.write(str(var)) #for fast output, always take string
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
# def graph(vertex): return [[] for i in range(0,vertex+1)]
def zerolist(n): return [0]*n
def nextline(): out("\n") #as stdout.write always print sring.
def testcase(t):
for pp in range(t):
solve(pp)
def printlist(a) :
for p in range(0,len(a)):
out(str(a[p]) + ' ')
def google(p):
print('Case #'+str(p)+': ',end='')
def lcm(a,b): return (a*b)//gcd(a,b)
def power(x, y, p) :
res = 1 # Initialize result
x = x % p # Update x if it is more , than or equal to p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) : # If y is odd, multiply, x with result
res = (res * x) % p
y = y >> 1 # y = y/2
x = (x * x) % p
return res
def ncr(n,r): return factorial(n)//(factorial(r)*factorial(max(n-r,1)))
def isPrime(n) :
if (n <= 1) : return False
if (n <= 3) : return True
if (n % 2 == 0 or n % 3 == 0) : return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
#===============================================================================================
# code here ;))
def solve(case):
n = int(inp())
aa = lis()
cnt = {}
for i in range(n):
if(aa[i] in cnt):
cnt[aa[i]] +=1
else:
cnt[aa[i]] = 1
a = []
for i in cnt:
a.append(cnt[i])
a = sorted(a)
n = len(a)
mx = 0
for i in range(a[0]+1,0,-1):
f = True
for j in range(n):
temp = ceil(a[j]/i)
# if(i == 2):
# print(temp*(i-1),'...')
if(temp*(i-1)>a[j]):
f = False
break
if(f):
mx = i
break
# print(mx)
ans = 0
for i in range(n):
ans+=ceil(a[i]/mx)
print(ans)
# testcase(1)
testcase(int(inp()))
``` | output | 1 | 101,323 | 14 | 202,647 |
Provide tags and a correct Python 3 solution for this coding contest problem.
BerPhone X is almost ready for release with n applications being preinstalled on the phone. A category of an application characterizes a genre or a theme of this application (like "game", "business", or "education"). The categories are given as integers between 1 and n, inclusive; the i-th application has category c_i.
You can choose m — the number of screens and s — the size of each screen. You need to fit all n icons of the applications (one icon representing one application) meeting the following requirements:
* On each screen, all the icons must belong to applications of the same category (but different screens can contain icons of applications of the same category);
* Each screen must be either completely filled with icons (the number of icons on the screen is equal to s) or almost filled with icons (the number of icons is equal to s-1).
Your task is to find the minimal possible number of screens m.
Input
The first line contains an integer t (1 ≤ t ≤ 10 000) — the number of test cases in the input. Then t test cases follow.
The first line of each test case contains an integer n (1 ≤ n ≤ 2⋅10^6) — the number of the icons. The second line contains n integers c_1, c_2, ..., c_n (1 ≤ c_i ≤ n), where c_i is the category of the i-th application.
It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 2⋅10^6.
Output
Print t integers — the answers to the given test cases in the order they follow in the input. The answer to a test case is an integer m — the minimum number of screens on which all n icons can be placed satisfying the given requirements.
Example
Input
3
11
1 5 1 5 1 5 1 1 1 1 5
6
1 2 2 2 2 1
5
4 3 3 1 2
Output
3
3
4
Note
In the first test case of the example, all the icons can be placed on three screens of size 4: a screen with 4 icons of the category 1, a screen with 3 icons of the category 1, and a screen with 4 icons of the category 5. | instruction | 0 | 101,324 | 14 | 202,648 |
Tags: greedy, implementation, math
Correct Solution:
```
from math import ceil
import sys
input=sys.stdin.readline
from collections import defaultdict as dd
t=int(input())
while t:
n=int(input())
d=dd(int)
l=list(map(int,input().split()))
for i in l:
d[i]+=1
li=[]
for i in d:
li.append(d[i])
mi=10000000000
for m in range(1,min(li)+2):
d=dd(int)
lol=0
for i in li:
if(i%m==0):
d[m]+=i//m
elif(i%m!=0):
ex=m-i%m
if(ex*(m-1)+(ceil(i/m)-ex)*m)!=i or (ceil(i/m)-ex)<0:
lol=1
break
d[m-1]+=ex
d[m]+=(ceil(i/m)-ex)
#if(m==5):
#print(d,ex*(m-1),(ceil(i/m)-ex))
#print(d,lol)
if(lol==0 and d[m]>=0 and d[m-1]>=0):
if(d[m]+d[m-1]<mi):
mi=d[m]+d[m-1]
print(mi)
t-=1
``` | output | 1 | 101,324 | 14 | 202,649 |
Provide tags and a correct Python 3 solution for this coding contest problem.
BerPhone X is almost ready for release with n applications being preinstalled on the phone. A category of an application characterizes a genre or a theme of this application (like "game", "business", or "education"). The categories are given as integers between 1 and n, inclusive; the i-th application has category c_i.
You can choose m — the number of screens and s — the size of each screen. You need to fit all n icons of the applications (one icon representing one application) meeting the following requirements:
* On each screen, all the icons must belong to applications of the same category (but different screens can contain icons of applications of the same category);
* Each screen must be either completely filled with icons (the number of icons on the screen is equal to s) or almost filled with icons (the number of icons is equal to s-1).
Your task is to find the minimal possible number of screens m.
Input
The first line contains an integer t (1 ≤ t ≤ 10 000) — the number of test cases in the input. Then t test cases follow.
The first line of each test case contains an integer n (1 ≤ n ≤ 2⋅10^6) — the number of the icons. The second line contains n integers c_1, c_2, ..., c_n (1 ≤ c_i ≤ n), where c_i is the category of the i-th application.
It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 2⋅10^6.
Output
Print t integers — the answers to the given test cases in the order they follow in the input. The answer to a test case is an integer m — the minimum number of screens on which all n icons can be placed satisfying the given requirements.
Example
Input
3
11
1 5 1 5 1 5 1 1 1 1 5
6
1 2 2 2 2 1
5
4 3 3 1 2
Output
3
3
4
Note
In the first test case of the example, all the icons can be placed on three screens of size 4: a screen with 4 icons of the category 1, a screen with 3 icons of the category 1, and a screen with 4 icons of the category 5. | instruction | 0 | 101,325 | 14 | 202,650 |
Tags: greedy, implementation, math
Correct Solution:
```
import sys
input = sys.stdin.readline
res = []
t = int(input())
for _ in range(t):
n = int(input())
l = map(int, input().split())
c = [0] * n
for v in l:
c[v-1] += 1
c.sort()
c.reverse()
while c[-1] == 0:
c.pop()
best = n
for i in range(2, c[-1] + 2):
out = 0
for v in c:
smol = (-v) % i
tol = (v + smol)//i - smol
if tol >= 0:
out += smol + tol
else:
out += n
best = min(best, out)
res.append(best)
print('\n'.join(map(str,res)))
``` | output | 1 | 101,325 | 14 | 202,651 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Anna got the following task at school: to arrange several numbers in a circle so that any two neighboring numbers differs exactly by 1. Anna was given several numbers and arranged them in a circle to fulfill the task. Then she wanted to check if she had arranged the numbers correctly, but at this point her younger sister Maria came and shuffled all numbers. Anna got sick with anger but what's done is done and the results of her work had been destroyed. But please tell Anna: could she have hypothetically completed the task using all those given numbers?
Input
The first line contains an integer n — how many numbers Anna had (3 ≤ n ≤ 105). The next line contains those numbers, separated by a space. All numbers are integers and belong to the range from 1 to 109.
Output
Print the single line "YES" (without the quotes), if Anna could have completed the task correctly using all those numbers (using all of them is necessary). If Anna couldn't have fulfilled the task, no matter how hard she would try, print "NO" (without the quotes).
Examples
Input
4
1 2 3 2
Output
YES
Input
6
1 1 2 2 2 3
Output
YES
Input
6
2 4 1 1 2 2
Output
NO | instruction | 0 | 101,334 | 14 | 202,668 |
Tags: constructive algorithms, implementation
Correct Solution:
```
n = int(input())
*a, = map(int, input().split())
j = max(a)
mn = min(a)
d = {}
for i in a:
d[i] = d.get(i, 0) + 1
s = d[j]
while j > mn:
j -= 1
if d.get(j, 0) < s or not s:
print('NO')
exit()
s *= -1
s += d.get(j, 0)
if s:
print('NO')
else:
print('YES')
``` | output | 1 | 101,334 | 14 | 202,669 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Anna got the following task at school: to arrange several numbers in a circle so that any two neighboring numbers differs exactly by 1. Anna was given several numbers and arranged them in a circle to fulfill the task. Then she wanted to check if she had arranged the numbers correctly, but at this point her younger sister Maria came and shuffled all numbers. Anna got sick with anger but what's done is done and the results of her work had been destroyed. But please tell Anna: could she have hypothetically completed the task using all those given numbers?
Input
The first line contains an integer n — how many numbers Anna had (3 ≤ n ≤ 105). The next line contains those numbers, separated by a space. All numbers are integers and belong to the range from 1 to 109.
Output
Print the single line "YES" (without the quotes), if Anna could have completed the task correctly using all those numbers (using all of them is necessary). If Anna couldn't have fulfilled the task, no matter how hard she would try, print "NO" (without the quotes).
Examples
Input
4
1 2 3 2
Output
YES
Input
6
1 1 2 2 2 3
Output
YES
Input
6
2 4 1 1 2 2
Output
NO | instruction | 0 | 101,337 | 14 | 202,674 |
Tags: constructive algorithms, implementation
Correct Solution:
```
#https://codeforces.com/problemset/problem/128/D
n = int(input())
a = list(map(int, input().split()))
d = {}
def push(d, x):
if x not in d:
d[x] = 0
d[x] += 1
def sub(d, x):
d[x] -= 1
if d[x] == 0:
del d[x]
for x in a:
push(d, x)
cur = min(list(d.keys()))
sub(d, cur)
ans = [cur]
while True:
if cur+1 in d:
cur+=1
ans.append(cur)
sub(d, cur)
elif cur - 1 in d:
cur-=1
ans.append(cur)
sub(d, cur)
else:
break
if len(d) == 0 and abs(ans[0]-ans[-1]) == 1:
print('YES')
else:
print('NO')
#6
#1 1 2 2 2 3
#6
#2 4 1 1 2 2
``` | output | 1 | 101,337 | 14 | 202,675 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Anna got the following task at school: to arrange several numbers in a circle so that any two neighboring numbers differs exactly by 1. Anna was given several numbers and arranged them in a circle to fulfill the task. Then she wanted to check if she had arranged the numbers correctly, but at this point her younger sister Maria came and shuffled all numbers. Anna got sick with anger but what's done is done and the results of her work had been destroyed. But please tell Anna: could she have hypothetically completed the task using all those given numbers?
Input
The first line contains an integer n — how many numbers Anna had (3 ≤ n ≤ 105). The next line contains those numbers, separated by a space. All numbers are integers and belong to the range from 1 to 109.
Output
Print the single line "YES" (without the quotes), if Anna could have completed the task correctly using all those numbers (using all of them is necessary). If Anna couldn't have fulfilled the task, no matter how hard she would try, print "NO" (without the quotes).
Examples
Input
4
1 2 3 2
Output
YES
Input
6
1 1 2 2 2 3
Output
YES
Input
6
2 4 1 1 2 2
Output
NO | instruction | 0 | 101,338 | 14 | 202,676 |
Tags: constructive algorithms, implementation
Correct Solution:
```
n=int(input())
g={}
for i in list(map(int,input().split())):g[i]=g.get(i,0)+2
mx=max(g)
for i in sorted(g)[:-1]:
if i+1 not in g:exit(print('NO'))
g[i+1]-=g[i]
if g[i+1]<0:exit(print('NO'))
print('YES'if g[mx]==0 and list(g.values()).count(0)==1else'NO')
# Wed Oct 14 2020 14:40:06 GMT+0300 (Москва, стандартное время)
``` | output | 1 | 101,338 | 14 | 202,677 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Anna got the following task at school: to arrange several numbers in a circle so that any two neighboring numbers differs exactly by 1. Anna was given several numbers and arranged them in a circle to fulfill the task. Then she wanted to check if she had arranged the numbers correctly, but at this point her younger sister Maria came and shuffled all numbers. Anna got sick with anger but what's done is done and the results of her work had been destroyed. But please tell Anna: could she have hypothetically completed the task using all those given numbers?
Input
The first line contains an integer n — how many numbers Anna had (3 ≤ n ≤ 105). The next line contains those numbers, separated by a space. All numbers are integers and belong to the range from 1 to 109.
Output
Print the single line "YES" (without the quotes), if Anna could have completed the task correctly using all those numbers (using all of them is necessary). If Anna couldn't have fulfilled the task, no matter how hard she would try, print "NO" (without the quotes).
Examples
Input
4
1 2 3 2
Output
YES
Input
6
1 1 2 2 2 3
Output
YES
Input
6
2 4 1 1 2 2
Output
NO | instruction | 0 | 101,339 | 14 | 202,678 |
Tags: constructive algorithms, implementation
Correct Solution:
```
from collections import Counter
import string
import bisect
#import random
import math
import sys
# sys.setrecursionlimit(10**6)
from fractions import Fraction
def array_int():
return [int(i) for i in sys.stdin.readline().split()]
def vary(arrber_of_variables):
if arrber_of_variables==1:
return int(sys.stdin.readline())
if arrber_of_variables>=2:
return map(int,sys.stdin.readline().split())
def makedict(var):
return dict(Counter(var))
testcases=1
for _ in range(testcases):
n=vary(1)
num=array_int()
ct=makedict(num)
mini=min(num)
ct[mini]-=1
ans=1
while 1:
if ans==n:
break
if ct.get(mini+1,0)>0:
ct[mini+1]-=1
ans+=1
# print('hello',mini)
mini+=1
elif ct.get(mini-1,0)>0:
ct[mini-1]-=1
ans+=1
# print(mini)
mini-=1
else:
break
# print(mini)
# print(ans,mini)
if ans==n and mini==min(num)+1:
print('YES')
else:
print('NO')
``` | output | 1 | 101,339 | 14 | 202,679 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Anna got the following task at school: to arrange several numbers in a circle so that any two neighboring numbers differs exactly by 1. Anna was given several numbers and arranged them in a circle to fulfill the task. Then she wanted to check if she had arranged the numbers correctly, but at this point her younger sister Maria came and shuffled all numbers. Anna got sick with anger but what's done is done and the results of her work had been destroyed. But please tell Anna: could she have hypothetically completed the task using all those given numbers?
Input
The first line contains an integer n — how many numbers Anna had (3 ≤ n ≤ 105). The next line contains those numbers, separated by a space. All numbers are integers and belong to the range from 1 to 109.
Output
Print the single line "YES" (without the quotes), if Anna could have completed the task correctly using all those numbers (using all of them is necessary). If Anna couldn't have fulfilled the task, no matter how hard she would try, print "NO" (without the quotes).
Examples
Input
4
1 2 3 2
Output
YES
Input
6
1 1 2 2 2 3
Output
YES
Input
6
2 4 1 1 2 2
Output
NO | instruction | 0 | 101,340 | 14 | 202,680 |
Tags: constructive algorithms, implementation
Correct Solution:
```
n=int(input())
g={}
for i in list(map(int,input().split())):g[i]=g.get(i,0)+2
mx=max(g)
for i in sorted(g)[:-1]:
if i+1 not in g:exit(print('NO'))
g[i+1]-=g[i]
if g[i+1]<0:exit(print('NO'))
print('YES'if g[mx]==0 and list(g.values()).count(0)==1else'NO')
``` | output | 1 | 101,340 | 14 | 202,681 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Anna got the following task at school: to arrange several numbers in a circle so that any two neighboring numbers differs exactly by 1. Anna was given several numbers and arranged them in a circle to fulfill the task. Then she wanted to check if she had arranged the numbers correctly, but at this point her younger sister Maria came and shuffled all numbers. Anna got sick with anger but what's done is done and the results of her work had been destroyed. But please tell Anna: could she have hypothetically completed the task using all those given numbers?
Input
The first line contains an integer n — how many numbers Anna had (3 ≤ n ≤ 105). The next line contains those numbers, separated by a space. All numbers are integers and belong to the range from 1 to 109.
Output
Print the single line "YES" (without the quotes), if Anna could have completed the task correctly using all those numbers (using all of them is necessary). If Anna couldn't have fulfilled the task, no matter how hard she would try, print "NO" (without the quotes).
Examples
Input
4
1 2 3 2
Output
YES
Input
6
1 1 2 2 2 3
Output
YES
Input
6
2 4 1 1 2 2
Output
NO | instruction | 0 | 101,341 | 14 | 202,682 |
Tags: constructive algorithms, implementation
Correct Solution:
```
n=int(input())
g={}
for i in list(map(int,input().split())):g[i]=g.get(i,0)+1
mx=max(g)
for i in sorted(g)[:-1]:
if i+1 not in g:exit(print('NO'))
g[i+1]-=g[i]
if g[i+1]<0:exit(print('NO'))
print('YES'if g[mx]==0 and list(g.values()).count(0)==1else'NO')
``` | output | 1 | 101,341 | 14 | 202,683 |
Provide a correct Python 3 solution for this coding contest problem.
Problem
Yamano Mifune Gakuen's 1st grade G group is a class where female students carrying misfortune gather. They face various challenges every day with the goal of being happy.
In their class, they can take practical happiness courses as part of the test of happiness.
From Monday to Friday, there are classes from 1st to Nth, and there are M courses that can be taken.
Subject i starts from the ai period of the day of the week di (di = 0, 1, 2, 3, 4 corresponds to Monday, Tuesday, Wednesday, Thursday, and Friday, respectively), and is performed in consecutive ki frames. The degree of happiness obtained when taking the course is ti.
Each student is free to choose up to L subjects so that they do not overlap each other. How do you choose the subject to get the highest level of happiness? Please find the maximum value of happiness that can be obtained from the information of the given subject.
Constraints
* 2 ≤ N ≤ 8
* 0 ≤ M ≤ 300
* 0 ≤ L ≤ min (N × 5, M)
* 0 ≤ di ≤ 4
* 1 ≤ ai ≤ N
* 1 ≤ ki
* ai + ki --1 ≤ N
* 1 ≤ ti ≤ 100
Input
The input is given in the following format.
N M L
d1 a1 k1 t1
d2 a2 k2 t2
...
dM aM kM tM
The first line is given three integers N, M, L separated by blanks.
The four integers di, ai, ki, and ti are given on the 2nd to M + 1th lines, separated by blanks.
Output
Output the maximum value of the sum of happiness on one line.
Examples
Input
3 7 3
0 1 1 1
0 1 1 2
1 1 3 4
1 1 1 1
1 2 1 2
2 1 1 3
2 2 2 1
Output
9
Input
5 10 5
0 1 1 2
0 2 1 2
0 1 2 3
1 2 1 2
1 4 2 3
2 1 1 1
2 1 1 2
3 3 2 3
4 1 1 2
4 2 1 2
Output
13 | instruction | 0 | 101,997 | 14 | 203,994 |
"Correct Solution:
```
from itertools import product
n, m, l = map(int, input().split())
subjects = [[] for _ in range(5)]
for _ in range(m):
d, a, k, t = map(int, input().split())
subjects[d].append((a, a + k - 1, t))
for i in range(5):
subjects[i].sort(key=lambda x:x[1])
#各曜日の科目数ごとの最大幸福
def calcDp(i):
#dp[y][x] ... y科目xコマまでの最大幸福
dp = [[0] * (n + 1) for _ in range(n + 1)]
sub = subjects[i]
for init, end, value in sub:
for y in range(n):
new_score = dp[y][init - 1] + value
for x in range(end, n + 1):
dp[y + 1][x] = max(dp[y + 1][x], new_score)
ret = []
for i in range(n + 1):
ret.append(max(dp[i]))
return ret
lst = [calcDp(i) for i in range(5)]
ans = 0
for t in product(range(n + 1), repeat=5):
if sum(t) > l:continue
score = 0
for i, v in enumerate(t):
score += lst[i][v]
ans = max(score, ans)
print(ans)
``` | output | 1 | 101,997 | 14 | 203,995 |
Provide a correct Python 3 solution for this coding contest problem.
Problem
Yamano Mifune Gakuen's 1st grade G group is a class where female students carrying misfortune gather. They face various challenges every day with the goal of being happy.
In their class, they can take practical happiness courses as part of the test of happiness.
From Monday to Friday, there are classes from 1st to Nth, and there are M courses that can be taken.
Subject i starts from the ai period of the day of the week di (di = 0, 1, 2, 3, 4 corresponds to Monday, Tuesday, Wednesday, Thursday, and Friday, respectively), and is performed in consecutive ki frames. The degree of happiness obtained when taking the course is ti.
Each student is free to choose up to L subjects so that they do not overlap each other. How do you choose the subject to get the highest level of happiness? Please find the maximum value of happiness that can be obtained from the information of the given subject.
Constraints
* 2 ≤ N ≤ 8
* 0 ≤ M ≤ 300
* 0 ≤ L ≤ min (N × 5, M)
* 0 ≤ di ≤ 4
* 1 ≤ ai ≤ N
* 1 ≤ ki
* ai + ki --1 ≤ N
* 1 ≤ ti ≤ 100
Input
The input is given in the following format.
N M L
d1 a1 k1 t1
d2 a2 k2 t2
...
dM aM kM tM
The first line is given three integers N, M, L separated by blanks.
The four integers di, ai, ki, and ti are given on the 2nd to M + 1th lines, separated by blanks.
Output
Output the maximum value of the sum of happiness on one line.
Examples
Input
3 7 3
0 1 1 1
0 1 1 2
1 1 3 4
1 1 1 1
1 2 1 2
2 1 1 3
2 2 2 1
Output
9
Input
5 10 5
0 1 1 2
0 2 1 2
0 1 2 3
1 2 1 2
1 4 2 3
2 1 1 1
2 1 1 2
3 3 2 3
4 1 1 2
4 2 1 2
Output
13 | instruction | 0 | 101,998 | 14 | 203,996 |
"Correct Solution:
```
# AOJ 1589 Unhappy Class
# Python3 2018.7.13 bal4u
N, M, L = map(int, input().split())
tbl = [[] for i in range(45)]
for i in range(M):
d, a, k, t = map(int, input().split())
tbl[d*N+a-1].append((k, t))
dp = [[0 for i in range(45)] for j in range(45)]
for da in range(5*N):
for i in range(L+1):
if i < L:
for k, t in tbl[da]:
dp[da+k][i+1] = max(dp[da+k][i+1], dp[da][i]+t)
dp[da+1][i] = max(dp[da+1][i], dp[da][i])
print(dp[5*N][L])
``` | output | 1 | 101,998 | 14 | 203,997 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are an upcoming movie director, and you have just released your first movie. You have also launched a simple review site with two buttons to press — upvote and downvote.
However, the site is not so simple on the inside. There are two servers, each with its separate counts for the upvotes and the downvotes.
n reviewers enter the site one by one. Each reviewer is one of the following types:
* type 1: a reviewer has watched the movie, and they like it — they press the upvote button;
* type 2: a reviewer has watched the movie, and they dislike it — they press the downvote button;
* type 3: a reviewer hasn't watched the movie — they look at the current number of upvotes and downvotes of the movie on the server they are in and decide what button to press. If there are more downvotes than upvotes, then a reviewer downvotes the movie. Otherwise, they upvote the movie.
Each reviewer votes on the movie exactly once.
Since you have two servers, you can actually manipulate the votes so that your movie gets as many upvotes as possible. When a reviewer enters a site, you know their type, and you can send them either to the first server or to the second one.
What is the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to?
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of testcases.
Then the descriptions of t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 50) — the number of reviewers.
The second line of each testcase contains n integers r_1, r_2, ..., r_n (1 ≤ r_i ≤ 3) — the types of the reviewers in the same order they enter the site.
Output
For each testcase print a single integer — the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to.
Example
Input
4
1
2
3
1 2 3
5
1 1 1 1 1
3
3 3 2
Output
0
2
5
2
Note
In the first testcase of the example you can send the only reviewer to either of the servers — they'll downvote anyway. The movie won't receive any upvotes.
In the second testcase of the example you can send all reviewers to the first server:
* the first reviewer upvotes;
* the second reviewer downvotes;
* the last reviewer sees that the number of downvotes is not greater than the number of upvotes — upvote themselves.
There are two upvotes in total. Alternatevely, you can send the first and the second reviewers to the first server and the last reviewer — to the second server:
* the first reviewer upvotes on the first server;
* the second reviewer downvotes on the first server;
* the last reviewer sees no upvotes or downvotes on the second server — upvote themselves. | instruction | 0 | 102,285 | 14 | 204,570 |
Tags: greedy
Correct Solution:
```
for i in range(int(input())): n=int(input()); print(n-input().split().count('2'))
``` | output | 1 | 102,285 | 14 | 204,571 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are an upcoming movie director, and you have just released your first movie. You have also launched a simple review site with two buttons to press — upvote and downvote.
However, the site is not so simple on the inside. There are two servers, each with its separate counts for the upvotes and the downvotes.
n reviewers enter the site one by one. Each reviewer is one of the following types:
* type 1: a reviewer has watched the movie, and they like it — they press the upvote button;
* type 2: a reviewer has watched the movie, and they dislike it — they press the downvote button;
* type 3: a reviewer hasn't watched the movie — they look at the current number of upvotes and downvotes of the movie on the server they are in and decide what button to press. If there are more downvotes than upvotes, then a reviewer downvotes the movie. Otherwise, they upvote the movie.
Each reviewer votes on the movie exactly once.
Since you have two servers, you can actually manipulate the votes so that your movie gets as many upvotes as possible. When a reviewer enters a site, you know their type, and you can send them either to the first server or to the second one.
What is the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to?
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of testcases.
Then the descriptions of t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 50) — the number of reviewers.
The second line of each testcase contains n integers r_1, r_2, ..., r_n (1 ≤ r_i ≤ 3) — the types of the reviewers in the same order they enter the site.
Output
For each testcase print a single integer — the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to.
Example
Input
4
1
2
3
1 2 3
5
1 1 1 1 1
3
3 3 2
Output
0
2
5
2
Note
In the first testcase of the example you can send the only reviewer to either of the servers — they'll downvote anyway. The movie won't receive any upvotes.
In the second testcase of the example you can send all reviewers to the first server:
* the first reviewer upvotes;
* the second reviewer downvotes;
* the last reviewer sees that the number of downvotes is not greater than the number of upvotes — upvote themselves.
There are two upvotes in total. Alternatevely, you can send the first and the second reviewers to the first server and the last reviewer — to the second server:
* the first reviewer upvotes on the first server;
* the second reviewer downvotes on the first server;
* the last reviewer sees no upvotes or downvotes on the second server — upvote themselves. | instruction | 0 | 102,286 | 14 | 204,572 |
Tags: greedy
Correct Solution:
```
for _ in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
d={1:0,2:0,3:0}
for i in l:
d[i]+=1
print(d[1]+d[3])
``` | output | 1 | 102,286 | 14 | 204,573 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are an upcoming movie director, and you have just released your first movie. You have also launched a simple review site with two buttons to press — upvote and downvote.
However, the site is not so simple on the inside. There are two servers, each with its separate counts for the upvotes and the downvotes.
n reviewers enter the site one by one. Each reviewer is one of the following types:
* type 1: a reviewer has watched the movie, and they like it — they press the upvote button;
* type 2: a reviewer has watched the movie, and they dislike it — they press the downvote button;
* type 3: a reviewer hasn't watched the movie — they look at the current number of upvotes and downvotes of the movie on the server they are in and decide what button to press. If there are more downvotes than upvotes, then a reviewer downvotes the movie. Otherwise, they upvote the movie.
Each reviewer votes on the movie exactly once.
Since you have two servers, you can actually manipulate the votes so that your movie gets as many upvotes as possible. When a reviewer enters a site, you know their type, and you can send them either to the first server or to the second one.
What is the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to?
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of testcases.
Then the descriptions of t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 50) — the number of reviewers.
The second line of each testcase contains n integers r_1, r_2, ..., r_n (1 ≤ r_i ≤ 3) — the types of the reviewers in the same order they enter the site.
Output
For each testcase print a single integer — the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to.
Example
Input
4
1
2
3
1 2 3
5
1 1 1 1 1
3
3 3 2
Output
0
2
5
2
Note
In the first testcase of the example you can send the only reviewer to either of the servers — they'll downvote anyway. The movie won't receive any upvotes.
In the second testcase of the example you can send all reviewers to the first server:
* the first reviewer upvotes;
* the second reviewer downvotes;
* the last reviewer sees that the number of downvotes is not greater than the number of upvotes — upvote themselves.
There are two upvotes in total. Alternatevely, you can send the first and the second reviewers to the first server and the last reviewer — to the second server:
* the first reviewer upvotes on the first server;
* the second reviewer downvotes on the first server;
* the last reviewer sees no upvotes or downvotes on the second server — upvote themselves. | instruction | 0 | 102,287 | 14 | 204,574 |
Tags: greedy
Correct Solution:
```
from sys import stdin
input = stdin.readline
def solution():
N = int(input())
R = list(map(int, input().split()))
print(sum(r in [1, 3] for r in R))
if __name__ == '__main__':
T = int(input())
for _ in range(T):
solution()
``` | output | 1 | 102,287 | 14 | 204,575 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are an upcoming movie director, and you have just released your first movie. You have also launched a simple review site with two buttons to press — upvote and downvote.
However, the site is not so simple on the inside. There are two servers, each with its separate counts for the upvotes and the downvotes.
n reviewers enter the site one by one. Each reviewer is one of the following types:
* type 1: a reviewer has watched the movie, and they like it — they press the upvote button;
* type 2: a reviewer has watched the movie, and they dislike it — they press the downvote button;
* type 3: a reviewer hasn't watched the movie — they look at the current number of upvotes and downvotes of the movie on the server they are in and decide what button to press. If there are more downvotes than upvotes, then a reviewer downvotes the movie. Otherwise, they upvote the movie.
Each reviewer votes on the movie exactly once.
Since you have two servers, you can actually manipulate the votes so that your movie gets as many upvotes as possible. When a reviewer enters a site, you know their type, and you can send them either to the first server or to the second one.
What is the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to?
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of testcases.
Then the descriptions of t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 50) — the number of reviewers.
The second line of each testcase contains n integers r_1, r_2, ..., r_n (1 ≤ r_i ≤ 3) — the types of the reviewers in the same order they enter the site.
Output
For each testcase print a single integer — the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to.
Example
Input
4
1
2
3
1 2 3
5
1 1 1 1 1
3
3 3 2
Output
0
2
5
2
Note
In the first testcase of the example you can send the only reviewer to either of the servers — they'll downvote anyway. The movie won't receive any upvotes.
In the second testcase of the example you can send all reviewers to the first server:
* the first reviewer upvotes;
* the second reviewer downvotes;
* the last reviewer sees that the number of downvotes is not greater than the number of upvotes — upvote themselves.
There are two upvotes in total. Alternatevely, you can send the first and the second reviewers to the first server and the last reviewer — to the second server:
* the first reviewer upvotes on the first server;
* the second reviewer downvotes on the first server;
* the last reviewer sees no upvotes or downvotes on the second server — upvote themselves. | instruction | 0 | 102,288 | 14 | 204,576 |
Tags: greedy
Correct Solution:
```
# cook your dish here
for _ in range(int(input())):
n = int(input())
r = list(map(int,input().split()))
ans = 0
for x in r:
if x == 1 or x == 3:
ans += 1
print(ans)
``` | output | 1 | 102,288 | 14 | 204,577 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are an upcoming movie director, and you have just released your first movie. You have also launched a simple review site with two buttons to press — upvote and downvote.
However, the site is not so simple on the inside. There are two servers, each with its separate counts for the upvotes and the downvotes.
n reviewers enter the site one by one. Each reviewer is one of the following types:
* type 1: a reviewer has watched the movie, and they like it — they press the upvote button;
* type 2: a reviewer has watched the movie, and they dislike it — they press the downvote button;
* type 3: a reviewer hasn't watched the movie — they look at the current number of upvotes and downvotes of the movie on the server they are in and decide what button to press. If there are more downvotes than upvotes, then a reviewer downvotes the movie. Otherwise, they upvote the movie.
Each reviewer votes on the movie exactly once.
Since you have two servers, you can actually manipulate the votes so that your movie gets as many upvotes as possible. When a reviewer enters a site, you know their type, and you can send them either to the first server or to the second one.
What is the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to?
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of testcases.
Then the descriptions of t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 50) — the number of reviewers.
The second line of each testcase contains n integers r_1, r_2, ..., r_n (1 ≤ r_i ≤ 3) — the types of the reviewers in the same order they enter the site.
Output
For each testcase print a single integer — the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to.
Example
Input
4
1
2
3
1 2 3
5
1 1 1 1 1
3
3 3 2
Output
0
2
5
2
Note
In the first testcase of the example you can send the only reviewer to either of the servers — they'll downvote anyway. The movie won't receive any upvotes.
In the second testcase of the example you can send all reviewers to the first server:
* the first reviewer upvotes;
* the second reviewer downvotes;
* the last reviewer sees that the number of downvotes is not greater than the number of upvotes — upvote themselves.
There are two upvotes in total. Alternatevely, you can send the first and the second reviewers to the first server and the last reviewer — to the second server:
* the first reviewer upvotes on the first server;
* the second reviewer downvotes on the first server;
* the last reviewer sees no upvotes or downvotes on the second server — upvote themselves. | instruction | 0 | 102,289 | 14 | 204,578 |
Tags: greedy
Correct Solution:
```
for _ in range(int(input())):
n= int(input())
arr= list(map(int, input().split()))
res = 0
for i in arr:
if i ==1 or i==3:
res+= 1
print(res)
``` | output | 1 | 102,289 | 14 | 204,579 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are an upcoming movie director, and you have just released your first movie. You have also launched a simple review site with two buttons to press — upvote and downvote.
However, the site is not so simple on the inside. There are two servers, each with its separate counts for the upvotes and the downvotes.
n reviewers enter the site one by one. Each reviewer is one of the following types:
* type 1: a reviewer has watched the movie, and they like it — they press the upvote button;
* type 2: a reviewer has watched the movie, and they dislike it — they press the downvote button;
* type 3: a reviewer hasn't watched the movie — they look at the current number of upvotes and downvotes of the movie on the server they are in and decide what button to press. If there are more downvotes than upvotes, then a reviewer downvotes the movie. Otherwise, they upvote the movie.
Each reviewer votes on the movie exactly once.
Since you have two servers, you can actually manipulate the votes so that your movie gets as many upvotes as possible. When a reviewer enters a site, you know their type, and you can send them either to the first server or to the second one.
What is the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to?
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of testcases.
Then the descriptions of t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 50) — the number of reviewers.
The second line of each testcase contains n integers r_1, r_2, ..., r_n (1 ≤ r_i ≤ 3) — the types of the reviewers in the same order they enter the site.
Output
For each testcase print a single integer — the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to.
Example
Input
4
1
2
3
1 2 3
5
1 1 1 1 1
3
3 3 2
Output
0
2
5
2
Note
In the first testcase of the example you can send the only reviewer to either of the servers — they'll downvote anyway. The movie won't receive any upvotes.
In the second testcase of the example you can send all reviewers to the first server:
* the first reviewer upvotes;
* the second reviewer downvotes;
* the last reviewer sees that the number of downvotes is not greater than the number of upvotes — upvote themselves.
There are two upvotes in total. Alternatevely, you can send the first and the second reviewers to the first server and the last reviewer — to the second server:
* the first reviewer upvotes on the first server;
* the second reviewer downvotes on the first server;
* the last reviewer sees no upvotes or downvotes on the second server — upvote themselves. | instruction | 0 | 102,290 | 14 | 204,580 |
Tags: greedy
Correct Solution:
```
t = int(input())
while t > 0:
t -= 1
n = int(input())
reviewer = [int(i) for i in input().strip().split(" ")]
countLikes = 0
countDisLikes = 0
for r in reviewer:
if r == 1:
countLikes += 1
elif r == 2:
countDisLikes += 1
else:
# if r == 3:
# if countLikes >= countDisLikes:
countLikes += 1
# else:
# countDisLikes += 1
print(f"{countLikes}")
``` | output | 1 | 102,290 | 14 | 204,581 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are an upcoming movie director, and you have just released your first movie. You have also launched a simple review site with two buttons to press — upvote and downvote.
However, the site is not so simple on the inside. There are two servers, each with its separate counts for the upvotes and the downvotes.
n reviewers enter the site one by one. Each reviewer is one of the following types:
* type 1: a reviewer has watched the movie, and they like it — they press the upvote button;
* type 2: a reviewer has watched the movie, and they dislike it — they press the downvote button;
* type 3: a reviewer hasn't watched the movie — they look at the current number of upvotes and downvotes of the movie on the server they are in and decide what button to press. If there are more downvotes than upvotes, then a reviewer downvotes the movie. Otherwise, they upvote the movie.
Each reviewer votes on the movie exactly once.
Since you have two servers, you can actually manipulate the votes so that your movie gets as many upvotes as possible. When a reviewer enters a site, you know their type, and you can send them either to the first server or to the second one.
What is the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to?
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of testcases.
Then the descriptions of t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 50) — the number of reviewers.
The second line of each testcase contains n integers r_1, r_2, ..., r_n (1 ≤ r_i ≤ 3) — the types of the reviewers in the same order they enter the site.
Output
For each testcase print a single integer — the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to.
Example
Input
4
1
2
3
1 2 3
5
1 1 1 1 1
3
3 3 2
Output
0
2
5
2
Note
In the first testcase of the example you can send the only reviewer to either of the servers — they'll downvote anyway. The movie won't receive any upvotes.
In the second testcase of the example you can send all reviewers to the first server:
* the first reviewer upvotes;
* the second reviewer downvotes;
* the last reviewer sees that the number of downvotes is not greater than the number of upvotes — upvote themselves.
There are two upvotes in total. Alternatevely, you can send the first and the second reviewers to the first server and the last reviewer — to the second server:
* the first reviewer upvotes on the first server;
* the second reviewer downvotes on the first server;
* the last reviewer sees no upvotes or downvotes on the second server — upvote themselves. | instruction | 0 | 102,291 | 14 | 204,582 |
Tags: greedy
Correct Solution:
```
from sys import stdin,stdout
stdin.readline
def mp(): return list(map(int, stdin.readline().strip().split()))
def it():return int(stdin.readline().strip())
from collections import defaultdict as dd,Counter as C,deque
from math import ceil,gcd,sqrt,factorial
for _ in range(it()):
n = it()
l = mp()
u = 0
for i in range(n):
if l[i] == 1 or l[i] == 3:
u += 1
print(u)
``` | output | 1 | 102,291 | 14 | 204,583 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are an upcoming movie director, and you have just released your first movie. You have also launched a simple review site with two buttons to press — upvote and downvote.
However, the site is not so simple on the inside. There are two servers, each with its separate counts for the upvotes and the downvotes.
n reviewers enter the site one by one. Each reviewer is one of the following types:
* type 1: a reviewer has watched the movie, and they like it — they press the upvote button;
* type 2: a reviewer has watched the movie, and they dislike it — they press the downvote button;
* type 3: a reviewer hasn't watched the movie — they look at the current number of upvotes and downvotes of the movie on the server they are in and decide what button to press. If there are more downvotes than upvotes, then a reviewer downvotes the movie. Otherwise, they upvote the movie.
Each reviewer votes on the movie exactly once.
Since you have two servers, you can actually manipulate the votes so that your movie gets as many upvotes as possible. When a reviewer enters a site, you know their type, and you can send them either to the first server or to the second one.
What is the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to?
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of testcases.
Then the descriptions of t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 50) — the number of reviewers.
The second line of each testcase contains n integers r_1, r_2, ..., r_n (1 ≤ r_i ≤ 3) — the types of the reviewers in the same order they enter the site.
Output
For each testcase print a single integer — the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to.
Example
Input
4
1
2
3
1 2 3
5
1 1 1 1 1
3
3 3 2
Output
0
2
5
2
Note
In the first testcase of the example you can send the only reviewer to either of the servers — they'll downvote anyway. The movie won't receive any upvotes.
In the second testcase of the example you can send all reviewers to the first server:
* the first reviewer upvotes;
* the second reviewer downvotes;
* the last reviewer sees that the number of downvotes is not greater than the number of upvotes — upvote themselves.
There are two upvotes in total. Alternatevely, you can send the first and the second reviewers to the first server and the last reviewer — to the second server:
* the first reviewer upvotes on the first server;
* the second reviewer downvotes on the first server;
* the last reviewer sees no upvotes or downvotes on the second server — upvote themselves. | instruction | 0 | 102,292 | 14 | 204,584 |
Tags: greedy
Correct Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
ans = 0
a = [int(i) for i in input().split()]
for i in a:
if i == 1 or i == 3:
ans += 1
print(ans)
``` | output | 1 | 102,292 | 14 | 204,585 |
Provide a correct Python 3 solution for this coding contest problem.
Cosmic market, commonly known as Kozumike, is the largest coterie spot sale in the universe. Doujin lovers of all genres gather at Kozumike. In recent years, the number of visitors to Kozumike has been increasing. If everyone can enter from the beginning, it will be very crowded and dangerous, so admission is restricted immediately after the opening. Only a certain number of people can enter at the same time as the opening. Others have to wait for a while before entering.
However, if you decide to enter the venue on a first-come, first-served basis, some people will line up all night from the day before. Many of Kozumike's participants are minors, and there are security issues, so it is not very good to decide on a first-come, first-served basis. For that reason, Kozumike can play some kind of game among the participants, and the person who survives the game gets the right to enter first. To be fair, we use highly random games every year.
I plan to participate in Kozumike again this time. I have a douujinshi that I really want to get at this Kozumike. A doujinshi distributed by a popular circle, which deals with the magical girl anime that became a hot topic this spring. However, this circle is very popular and its distribution ends immediately every time. It would be almost impossible to get it if you couldn't enter at the same time as the opening. Of course, there is no doubt that it will be available at douujin shops at a later date. But I can't put up with it until then. I have to get it on the day of Kozumike with any hand. Unfortunately, I've never been the first to enter Kozumike.
According to Kozumike's catalog, this time we will select the first person to enter in the following games. First, the first r × c of the participants sit in any of the seats arranged like the r × c grid. You can choose the location on a first-come, first-served basis, so there are many options if you go early. The game starts when r × c people sit down. In this game, all people in a seat in a row (row) are instructed to sit or stand a certain number of times. If a person who is already seated is instructed to sit down, just wait. The same is true for those who are standing up. The column (row) to which this instruction is issued and the content of the instruction are randomly determined by roulette. Only those who stand up after a certain number of instructions will be able to enter the venue first. Participants are not informed of the number of instructions, so they do not know when the game will end. So at some point neither standing nor sitting knows if they can enter first. That's why the catalog emphasizes that you can enjoy the thrill.
At 1:00 pm the day before Kozumike, I got all the data about this year's game. According to the information I got, the number of times this instruction, q, has already been decided. On the contrary, all the instructions for q times have already been decided. It is said that it has a high degree of randomness, but that was a lie. I don't know why it was decided in advance. But this information seems to me a godsend.
If the game is played according to this data, you will know all the seating locations where you can enter first. In the worst case, let's say all the participants except me had this information. Even in such a case, you should know the number of arrivals at the latest to enter the venue first. However, it seems impossible to analyze this instruction by hand from now on because it is too much. No matter how hard I try, I can't make it in time for Kozumike.
So I decided to leave this calculation to you. You often participate in programming contests and you should be good at writing math programs like this. Of course I wouldn't ask you to do it for free. According to the catalog, this year's Kozumike will be attended by a circle that publishes a doujinshi about programming contests. If I can enter at the same time as the opening, I will get the douujinshi as a reward and give it to you. Well, I'll take a nap for about 5 hours from now on. In the meantime, I believe you will give me an answer.
Input
The input consists of multiple cases. Each case is given in the following format.
r c q
A0 B0 order0
A1 B1 order1
..
..
..
Aq-1 Bq-1 orderq-1
When Ai = 0, the orderi instruction is executed for the Bi line.
When Ai = 1, the orderi instruction is executed for the Bi column.
When orderi = 0, all people in the specified row or column are seated.
When orderi = 1, make everyone stand in the specified row or column.
The end of the input is given by r = 0 c = 0 q = 0.
The input value satisfies the following conditions.
1 ≤ r ≤ 50,000
1 ≤ c ≤ 50,000
1 ≤ q ≤ 50,000
orderi = 0 or 1
When Ai = 0
0 ≤ Bi <r
When Ai = 1
0 ≤ Bi <c
The number of test cases does not exceed 100
Output
In the worst case, print out in one line how many times you should arrive to get the right to enter the venue first.
Example
Input
5 5 5
0 0 1
0 0 0
0 2 0
1 2 1
0 0 0
5 5 5
1 4 0
0 1 1
0 4 1
1 3 0
0 3 1
5 5 5
1 0 0
0 3 0
1 2 1
0 4 0
0 1 1
0 0 0
Output
4
13
8 | instruction | 0 | 102,844 | 14 | 205,688 |
"Correct Solution:
```
while True:
r, c, q = map(int, input().split())
if r == 0: break
querys = [list(map(int, input().split())) for _ in range(q)]
querys.reverse()
r_used = [False] * r
c_used = [False] * c
r_cnt = c
c_cnt = r
ans = 0
for a, b, o in querys:
if a == 0:
if not r_used[b]:
r_used[b] = True
c_cnt -= 1
if o:
ans += r_cnt
else:
if not c_used[b]:
c_used[b] = True
r_cnt -= 1
if o:
ans += c_cnt
print(ans)
``` | output | 1 | 102,844 | 14 | 205,689 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let x be the number of such pairs of students in a split. Pairs (a, b) and (b, a) are the same and counted only once.
For example, if there are 6 students: "olivia", "jacob", "tanya", "jack", "oliver" and "jessica", then:
* splitting into two classrooms ("jack", "jacob", "jessica", "tanya") and ("olivia", "oliver") will give x=4 (3 chatting pairs in the first classroom, 1 chatting pair in the second classroom),
* splitting into two classrooms ("jack", "tanya", "olivia") and ("jessica", "oliver", "jacob") will give x=1 (0 chatting pairs in the first classroom, 1 chatting pair in the second classroom).
You are given the list of the n names. What is the minimum x we can obtain by splitting the students into classrooms?
Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty.
Input
The first line contains a single integer n (1≤ n ≤ 100) — the number of students.
After this n lines follow.
The i-th line contains the name of the i-th student.
It is guaranteed each name is a string of lowercase English letters of length at most 20. Note that multiple students may share the same name.
Output
The output must consist of a single integer x — the minimum possible number of chatty pairs.
Examples
Input
4
jorge
jose
oscar
jerry
Output
1
Input
7
kambei
gorobei
shichiroji
kyuzo
heihachi
katsushiro
kikuchiyo
Output
2
Input
5
mike
mike
mike
mike
mike
Output
4
Note
In the first sample the minimum number of pairs is 1. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.
In the second sample the minimum number of pairs is 2. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.
In the third sample the minimum number of pairs is 4. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom. | instruction | 0 | 102,956 | 14 | 205,912 |
Tags: combinatorics, greedy
Correct Solution:
```
import math
def combination2(a):
return math.factorial(a)/(math.factorial(2)*math.factorial(a-2))
n = int(input())
first_letter_table = {
'a' : 0,
'b' : 0,
'c' : 0,
'd' : 0,
'e' : 0,
'f' : 0,
'g' : 0,
'h' : 0,
'i' : 0,
'j' : 0,
'k' : 0,
'l' : 0,
'm' : 0,
'n' : 0,
'o' : 0,
'p' : 0,
'q' : 0,
'r' : 0,
's' : 0,
't' : 0,
'u' : 0,
'v' : 0,
'w' : 0,
'x' : 0,
'y' : 0,
'z' : 0,
}
for i in range(n):
masukan = input()
first_letter_table[masukan[0]] += 1
x = 0
for alphabet in first_letter_table:
num = first_letter_table[alphabet]
if num==0 or num==1 or num==2:
pass
elif num==3:
x+= 1
elif num>3:
num1 = num//2
num2 = num-num1
x += combination2(num1) + combination2(num2)
print(int(x))
``` | output | 1 | 102,956 | 14 | 205,913 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let x be the number of such pairs of students in a split. Pairs (a, b) and (b, a) are the same and counted only once.
For example, if there are 6 students: "olivia", "jacob", "tanya", "jack", "oliver" and "jessica", then:
* splitting into two classrooms ("jack", "jacob", "jessica", "tanya") and ("olivia", "oliver") will give x=4 (3 chatting pairs in the first classroom, 1 chatting pair in the second classroom),
* splitting into two classrooms ("jack", "tanya", "olivia") and ("jessica", "oliver", "jacob") will give x=1 (0 chatting pairs in the first classroom, 1 chatting pair in the second classroom).
You are given the list of the n names. What is the minimum x we can obtain by splitting the students into classrooms?
Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty.
Input
The first line contains a single integer n (1≤ n ≤ 100) — the number of students.
After this n lines follow.
The i-th line contains the name of the i-th student.
It is guaranteed each name is a string of lowercase English letters of length at most 20. Note that multiple students may share the same name.
Output
The output must consist of a single integer x — the minimum possible number of chatty pairs.
Examples
Input
4
jorge
jose
oscar
jerry
Output
1
Input
7
kambei
gorobei
shichiroji
kyuzo
heihachi
katsushiro
kikuchiyo
Output
2
Input
5
mike
mike
mike
mike
mike
Output
4
Note
In the first sample the minimum number of pairs is 1. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.
In the second sample the minimum number of pairs is 2. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.
In the third sample the minimum number of pairs is 4. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom. | instruction | 0 | 102,957 | 14 | 205,914 |
Tags: combinatorics, greedy
Correct Solution:
```
n = int(input())
d = {}
for i in range(n):
s = input().strip()[0]
if s not in d:
d[s] = 0
d[s] += 1
ans = 0
for key in d.keys():
if d[key] <= 2:
continue
else:
gr1 = d[key]//2
gr2 = d[key]-gr1
ans += gr1*(gr1-1)//2+gr2*(gr2-1)//2
print(ans)
``` | output | 1 | 102,957 | 14 | 205,915 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let x be the number of such pairs of students in a split. Pairs (a, b) and (b, a) are the same and counted only once.
For example, if there are 6 students: "olivia", "jacob", "tanya", "jack", "oliver" and "jessica", then:
* splitting into two classrooms ("jack", "jacob", "jessica", "tanya") and ("olivia", "oliver") will give x=4 (3 chatting pairs in the first classroom, 1 chatting pair in the second classroom),
* splitting into two classrooms ("jack", "tanya", "olivia") and ("jessica", "oliver", "jacob") will give x=1 (0 chatting pairs in the first classroom, 1 chatting pair in the second classroom).
You are given the list of the n names. What is the minimum x we can obtain by splitting the students into classrooms?
Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty.
Input
The first line contains a single integer n (1≤ n ≤ 100) — the number of students.
After this n lines follow.
The i-th line contains the name of the i-th student.
It is guaranteed each name is a string of lowercase English letters of length at most 20. Note that multiple students may share the same name.
Output
The output must consist of a single integer x — the minimum possible number of chatty pairs.
Examples
Input
4
jorge
jose
oscar
jerry
Output
1
Input
7
kambei
gorobei
shichiroji
kyuzo
heihachi
katsushiro
kikuchiyo
Output
2
Input
5
mike
mike
mike
mike
mike
Output
4
Note
In the first sample the minimum number of pairs is 1. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.
In the second sample the minimum number of pairs is 2. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.
In the third sample the minimum number of pairs is 4. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom. | instruction | 0 | 102,958 | 14 | 205,916 |
Tags: combinatorics, greedy
Correct Solution:
```
from collections import defaultdict
def C2(n):
return n*(n-1)/2
m = defaultdict(int)
n = int(input())
for x in range(n):
s = input()
m[s[0]] += 1
ans = 0
for c in m:
amt = m[c]
mn = 1e9
for i in range(amt//2 + 1):
mn = min(mn, C2(i) + C2(amt-i))
ans += mn
print(int(ans))
``` | output | 1 | 102,958 | 14 | 205,917 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let x be the number of such pairs of students in a split. Pairs (a, b) and (b, a) are the same and counted only once.
For example, if there are 6 students: "olivia", "jacob", "tanya", "jack", "oliver" and "jessica", then:
* splitting into two classrooms ("jack", "jacob", "jessica", "tanya") and ("olivia", "oliver") will give x=4 (3 chatting pairs in the first classroom, 1 chatting pair in the second classroom),
* splitting into two classrooms ("jack", "tanya", "olivia") and ("jessica", "oliver", "jacob") will give x=1 (0 chatting pairs in the first classroom, 1 chatting pair in the second classroom).
You are given the list of the n names. What is the minimum x we can obtain by splitting the students into classrooms?
Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty.
Input
The first line contains a single integer n (1≤ n ≤ 100) — the number of students.
After this n lines follow.
The i-th line contains the name of the i-th student.
It is guaranteed each name is a string of lowercase English letters of length at most 20. Note that multiple students may share the same name.
Output
The output must consist of a single integer x — the minimum possible number of chatty pairs.
Examples
Input
4
jorge
jose
oscar
jerry
Output
1
Input
7
kambei
gorobei
shichiroji
kyuzo
heihachi
katsushiro
kikuchiyo
Output
2
Input
5
mike
mike
mike
mike
mike
Output
4
Note
In the first sample the minimum number of pairs is 1. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.
In the second sample the minimum number of pairs is 2. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.
In the third sample the minimum number of pairs is 4. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom. | instruction | 0 | 102,959 | 14 | 205,918 |
Tags: combinatorics, greedy
Correct Solution:
```
from collections import*
a=Counter(input()[0]for _ in[0]*int(input()))
r=0
for k in a:i=a[k]//2;j=a[k]-i;r+=i*(i-1)+j*(j-1)
print(r//2)
``` | output | 1 | 102,959 | 14 | 205,919 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let x be the number of such pairs of students in a split. Pairs (a, b) and (b, a) are the same and counted only once.
For example, if there are 6 students: "olivia", "jacob", "tanya", "jack", "oliver" and "jessica", then:
* splitting into two classrooms ("jack", "jacob", "jessica", "tanya") and ("olivia", "oliver") will give x=4 (3 chatting pairs in the first classroom, 1 chatting pair in the second classroom),
* splitting into two classrooms ("jack", "tanya", "olivia") and ("jessica", "oliver", "jacob") will give x=1 (0 chatting pairs in the first classroom, 1 chatting pair in the second classroom).
You are given the list of the n names. What is the minimum x we can obtain by splitting the students into classrooms?
Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty.
Input
The first line contains a single integer n (1≤ n ≤ 100) — the number of students.
After this n lines follow.
The i-th line contains the name of the i-th student.
It is guaranteed each name is a string of lowercase English letters of length at most 20. Note that multiple students may share the same name.
Output
The output must consist of a single integer x — the minimum possible number of chatty pairs.
Examples
Input
4
jorge
jose
oscar
jerry
Output
1
Input
7
kambei
gorobei
shichiroji
kyuzo
heihachi
katsushiro
kikuchiyo
Output
2
Input
5
mike
mike
mike
mike
mike
Output
4
Note
In the first sample the minimum number of pairs is 1. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.
In the second sample the minimum number of pairs is 2. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.
In the third sample the minimum number of pairs is 4. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom. | instruction | 0 | 102,960 | 14 | 205,920 |
Tags: combinatorics, greedy
Correct Solution:
```
fact = [0] * 101
fact[1] = 1
for i in range(2, 101):
fact[i] = fact[i - 1] * i
def nCr(n, r):
if n == r:
return 1
return fact[n] // (fact[r] * fact[n - r])
def main():
n = int(input())
arr = [0] * n
for i in range(n):
arr[i] = input()[:1]
from collections import Counter
d = Counter(arr)
ans = 0
for i in d:
if d[i] <= 2:
continue
x = d[i] // 2
if d[i] % 2 == 1:
ans += nCr(x + 1, 2)
ans += nCr(x, 2)
else:
ans += 2 * nCr(x, 2)
print(ans)
if __name__ == "__main__":
main()
``` | output | 1 | 102,960 | 14 | 205,921 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let x be the number of such pairs of students in a split. Pairs (a, b) and (b, a) are the same and counted only once.
For example, if there are 6 students: "olivia", "jacob", "tanya", "jack", "oliver" and "jessica", then:
* splitting into two classrooms ("jack", "jacob", "jessica", "tanya") and ("olivia", "oliver") will give x=4 (3 chatting pairs in the first classroom, 1 chatting pair in the second classroom),
* splitting into two classrooms ("jack", "tanya", "olivia") and ("jessica", "oliver", "jacob") will give x=1 (0 chatting pairs in the first classroom, 1 chatting pair in the second classroom).
You are given the list of the n names. What is the minimum x we can obtain by splitting the students into classrooms?
Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty.
Input
The first line contains a single integer n (1≤ n ≤ 100) — the number of students.
After this n lines follow.
The i-th line contains the name of the i-th student.
It is guaranteed each name is a string of lowercase English letters of length at most 20. Note that multiple students may share the same name.
Output
The output must consist of a single integer x — the minimum possible number of chatty pairs.
Examples
Input
4
jorge
jose
oscar
jerry
Output
1
Input
7
kambei
gorobei
shichiroji
kyuzo
heihachi
katsushiro
kikuchiyo
Output
2
Input
5
mike
mike
mike
mike
mike
Output
4
Note
In the first sample the minimum number of pairs is 1. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.
In the second sample the minimum number of pairs is 2. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.
In the third sample the minimum number of pairs is 4. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom. | instruction | 0 | 102,961 | 14 | 205,922 |
Tags: combinatorics, greedy
Correct Solution:
```
def chatty(N):
return sum(e for e in range(1,N))
n = int(input())
a =[]
b = []
for i in range(n):
s = input()
a.append(s)
k = 0
while k<26:
c = 0
for i in range(len(a)):
if a[i][0]==chr(97+k):
c = c+1
b.append(c)
k = k+1
c = 0
p1 = 0
p2 = 0
for i in range(len(b)):
if b[i]>2:
p1 = b[i]//2
p2 = b[i]-p1
c = c+(chatty(p1)+chatty(p2))
print(c)
``` | output | 1 | 102,961 | 14 | 205,923 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let x be the number of such pairs of students in a split. Pairs (a, b) and (b, a) are the same and counted only once.
For example, if there are 6 students: "olivia", "jacob", "tanya", "jack", "oliver" and "jessica", then:
* splitting into two classrooms ("jack", "jacob", "jessica", "tanya") and ("olivia", "oliver") will give x=4 (3 chatting pairs in the first classroom, 1 chatting pair in the second classroom),
* splitting into two classrooms ("jack", "tanya", "olivia") and ("jessica", "oliver", "jacob") will give x=1 (0 chatting pairs in the first classroom, 1 chatting pair in the second classroom).
You are given the list of the n names. What is the minimum x we can obtain by splitting the students into classrooms?
Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty.
Input
The first line contains a single integer n (1≤ n ≤ 100) — the number of students.
After this n lines follow.
The i-th line contains the name of the i-th student.
It is guaranteed each name is a string of lowercase English letters of length at most 20. Note that multiple students may share the same name.
Output
The output must consist of a single integer x — the minimum possible number of chatty pairs.
Examples
Input
4
jorge
jose
oscar
jerry
Output
1
Input
7
kambei
gorobei
shichiroji
kyuzo
heihachi
katsushiro
kikuchiyo
Output
2
Input
5
mike
mike
mike
mike
mike
Output
4
Note
In the first sample the minimum number of pairs is 1. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.
In the second sample the minimum number of pairs is 2. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.
In the third sample the minimum number of pairs is 4. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom. | instruction | 0 | 102,962 | 14 | 205,924 |
Tags: combinatorics, greedy
Correct Solution:
```
a=[];b=[];p=0
for i in range(int(input())):
x=input()[0]
if a.count(x)<=b.count(x):
a.append(x)
else:
b.append(x)
for i in range(len(a)-1):
for t in range(i+1,len(a)):
if a[i]==a[t]:
p+=1
for i in range(len(b)-1):
for t in range(i+1,len(b)):
if b[i]==b[t]:
p+=1
print(p)
``` | output | 1 | 102,962 | 14 | 205,925 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let x be the number of such pairs of students in a split. Pairs (a, b) and (b, a) are the same and counted only once.
For example, if there are 6 students: "olivia", "jacob", "tanya", "jack", "oliver" and "jessica", then:
* splitting into two classrooms ("jack", "jacob", "jessica", "tanya") and ("olivia", "oliver") will give x=4 (3 chatting pairs in the first classroom, 1 chatting pair in the second classroom),
* splitting into two classrooms ("jack", "tanya", "olivia") and ("jessica", "oliver", "jacob") will give x=1 (0 chatting pairs in the first classroom, 1 chatting pair in the second classroom).
You are given the list of the n names. What is the minimum x we can obtain by splitting the students into classrooms?
Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty.
Input
The first line contains a single integer n (1≤ n ≤ 100) — the number of students.
After this n lines follow.
The i-th line contains the name of the i-th student.
It is guaranteed each name is a string of lowercase English letters of length at most 20. Note that multiple students may share the same name.
Output
The output must consist of a single integer x — the minimum possible number of chatty pairs.
Examples
Input
4
jorge
jose
oscar
jerry
Output
1
Input
7
kambei
gorobei
shichiroji
kyuzo
heihachi
katsushiro
kikuchiyo
Output
2
Input
5
mike
mike
mike
mike
mike
Output
4
Note
In the first sample the minimum number of pairs is 1. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.
In the second sample the minimum number of pairs is 2. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.
In the third sample the minimum number of pairs is 4. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom. | instruction | 0 | 102,963 | 14 | 205,926 |
Tags: combinatorics, greedy
Correct Solution:
```
n = int(input())
l = [0] * 1000
for i in range(n):
s = input()
l[ord(s[0])] += 1
wyn = 0
for i in range(1000):
if l[i] % 2 == 0:
wyn += (l[i]//2)*(l[i]//2 - 1)
else:
wyn += (l[i]//2)*(l[i]//2 - 1)//2
wyn += ((l[i] + 1)//2)*((l[i]+ 1)//2 - 1)//2
print(wyn)
``` | output | 1 | 102,963 | 14 | 205,927 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let x be the number of such pairs of students in a split. Pairs (a, b) and (b, a) are the same and counted only once.
For example, if there are 6 students: "olivia", "jacob", "tanya", "jack", "oliver" and "jessica", then:
* splitting into two classrooms ("jack", "jacob", "jessica", "tanya") and ("olivia", "oliver") will give x=4 (3 chatting pairs in the first classroom, 1 chatting pair in the second classroom),
* splitting into two classrooms ("jack", "tanya", "olivia") and ("jessica", "oliver", "jacob") will give x=1 (0 chatting pairs in the first classroom, 1 chatting pair in the second classroom).
You are given the list of the n names. What is the minimum x we can obtain by splitting the students into classrooms?
Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty.
Input
The first line contains a single integer n (1≤ n ≤ 100) — the number of students.
After this n lines follow.
The i-th line contains the name of the i-th student.
It is guaranteed each name is a string of lowercase English letters of length at most 20. Note that multiple students may share the same name.
Output
The output must consist of a single integer x — the minimum possible number of chatty pairs.
Examples
Input
4
jorge
jose
oscar
jerry
Output
1
Input
7
kambei
gorobei
shichiroji
kyuzo
heihachi
katsushiro
kikuchiyo
Output
2
Input
5
mike
mike
mike
mike
mike
Output
4
Note
In the first sample the minimum number of pairs is 1. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.
In the second sample the minimum number of pairs is 2. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.
In the third sample the minimum number of pairs is 4. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom.
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 26 18:27:38 2020
@author: MridulSachdeva
"""
from collections import Counter
n = int(input())
chatty_pairs = 0
students = []
for _ in range(n):
students.append(input()[0])
s = Counter(students)
for i in s.values():
if i <= 2:
pass
else:
k = i // 2
if i % 2 == 0:
chatty_pairs += k * (k - 1)
else:
chatty_pairs += k * k
print(chatty_pairs)
``` | instruction | 0 | 102,964 | 14 | 205,928 |
Yes | output | 1 | 102,964 | 14 | 205,929 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let x be the number of such pairs of students in a split. Pairs (a, b) and (b, a) are the same and counted only once.
For example, if there are 6 students: "olivia", "jacob", "tanya", "jack", "oliver" and "jessica", then:
* splitting into two classrooms ("jack", "jacob", "jessica", "tanya") and ("olivia", "oliver") will give x=4 (3 chatting pairs in the first classroom, 1 chatting pair in the second classroom),
* splitting into two classrooms ("jack", "tanya", "olivia") and ("jessica", "oliver", "jacob") will give x=1 (0 chatting pairs in the first classroom, 1 chatting pair in the second classroom).
You are given the list of the n names. What is the minimum x we can obtain by splitting the students into classrooms?
Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty.
Input
The first line contains a single integer n (1≤ n ≤ 100) — the number of students.
After this n lines follow.
The i-th line contains the name of the i-th student.
It is guaranteed each name is a string of lowercase English letters of length at most 20. Note that multiple students may share the same name.
Output
The output must consist of a single integer x — the minimum possible number of chatty pairs.
Examples
Input
4
jorge
jose
oscar
jerry
Output
1
Input
7
kambei
gorobei
shichiroji
kyuzo
heihachi
katsushiro
kikuchiyo
Output
2
Input
5
mike
mike
mike
mike
mike
Output
4
Note
In the first sample the minimum number of pairs is 1. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.
In the second sample the minimum number of pairs is 2. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.
In the third sample the minimum number of pairs is 4. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom.
Submitted Solution:
```
n = int(input())
di = dict()
count = 0
for i in range(n):
string = input()
char = string[0]
if char in di:
di[char] = di[char]+1
else:
di[char] = 1
for j in di:
if di[j] == 2:
count = count + 0
elif di[j] <= 4:
count = count + (di[j]//2)
else:
co = di[j]-(di[j]//2)-1
co = (co*(co+1))//2
count = count+co
co = (di[j]//2)-1
co = (co*(co+1))//2
count = count+co
print(count)
``` | instruction | 0 | 102,965 | 14 | 205,930 |
Yes | output | 1 | 102,965 | 14 | 205,931 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let x be the number of such pairs of students in a split. Pairs (a, b) and (b, a) are the same and counted only once.
For example, if there are 6 students: "olivia", "jacob", "tanya", "jack", "oliver" and "jessica", then:
* splitting into two classrooms ("jack", "jacob", "jessica", "tanya") and ("olivia", "oliver") will give x=4 (3 chatting pairs in the first classroom, 1 chatting pair in the second classroom),
* splitting into two classrooms ("jack", "tanya", "olivia") and ("jessica", "oliver", "jacob") will give x=1 (0 chatting pairs in the first classroom, 1 chatting pair in the second classroom).
You are given the list of the n names. What is the minimum x we can obtain by splitting the students into classrooms?
Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty.
Input
The first line contains a single integer n (1≤ n ≤ 100) — the number of students.
After this n lines follow.
The i-th line contains the name of the i-th student.
It is guaranteed each name is a string of lowercase English letters of length at most 20. Note that multiple students may share the same name.
Output
The output must consist of a single integer x — the minimum possible number of chatty pairs.
Examples
Input
4
jorge
jose
oscar
jerry
Output
1
Input
7
kambei
gorobei
shichiroji
kyuzo
heihachi
katsushiro
kikuchiyo
Output
2
Input
5
mike
mike
mike
mike
mike
Output
4
Note
In the first sample the minimum number of pairs is 1. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.
In the second sample the minimum number of pairs is 2. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.
In the third sample the minimum number of pairs is 4. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom.
Submitted Solution:
```
#Chatty Pairs
from itertools import combinations
n = int(input())
names = []
for _ in range(n):
names.append(input()[0])
class1 = []
class2 = []
names.sort()
for i in range(len(names)):
if i%2 == 0:
class1.append(names[i])
else:
class2.append(names[i])
x = 0
y = 0
s_class1 = list(set(class1))
s_class2 = list(set(class2))
for i in range(len(s_class1)):
if class1.count(s_class1[i]) > 1:
x = x + len(list(combinations('-'*class1.count(s_class1[i]),2)))
for i in range(len(s_class2)):
if class2.count(s_class2[i]) > 1:
y = y + len(list(combinations('-'*class2.count(s_class2[i]),2)))
print(x+y)
``` | instruction | 0 | 102,966 | 14 | 205,932 |
Yes | output | 1 | 102,966 | 14 | 205,933 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let x be the number of such pairs of students in a split. Pairs (a, b) and (b, a) are the same and counted only once.
For example, if there are 6 students: "olivia", "jacob", "tanya", "jack", "oliver" and "jessica", then:
* splitting into two classrooms ("jack", "jacob", "jessica", "tanya") and ("olivia", "oliver") will give x=4 (3 chatting pairs in the first classroom, 1 chatting pair in the second classroom),
* splitting into two classrooms ("jack", "tanya", "olivia") and ("jessica", "oliver", "jacob") will give x=1 (0 chatting pairs in the first classroom, 1 chatting pair in the second classroom).
You are given the list of the n names. What is the minimum x we can obtain by splitting the students into classrooms?
Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty.
Input
The first line contains a single integer n (1≤ n ≤ 100) — the number of students.
After this n lines follow.
The i-th line contains the name of the i-th student.
It is guaranteed each name is a string of lowercase English letters of length at most 20. Note that multiple students may share the same name.
Output
The output must consist of a single integer x — the minimum possible number of chatty pairs.
Examples
Input
4
jorge
jose
oscar
jerry
Output
1
Input
7
kambei
gorobei
shichiroji
kyuzo
heihachi
katsushiro
kikuchiyo
Output
2
Input
5
mike
mike
mike
mike
mike
Output
4
Note
In the first sample the minimum number of pairs is 1. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.
In the second sample the minimum number of pairs is 2. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.
In the third sample the minimum number of pairs is 4. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom.
Submitted Solution:
```
from math import factorial
def socitania(n, k):
if n - k >= 0:
return factorial(n) // (factorial(k) * factorial(n - k))
return 0
n = int(input())
x = 0
d = dict()
for i in range(n):
s = input()[0]
d[s] = d.get(s, 0) + 1
for val, key in d.items():
if key != 1:
x += socitania(key // 2, 2) + socitania(key // 2 + key % 2, 2)
print(x)
'''
x = 5, k = 1
12 11 10 -- a
0 1 2 -- r(sort)
2 2 2 -- c(sort)
d = 6 == (sum(c)) - ������ ���� (����)
���� sum(r) / k >= d:
����� d ����������� ���� ������ �� r (� �����������) -- for �������
�����
�������� �������� ����� ������ ������� x (������� ������ ������ ����)
14 14 14
4 4 4
0 2 4
0 1 0
0 0 0
'''
``` | instruction | 0 | 102,967 | 14 | 205,934 |
Yes | output | 1 | 102,967 | 14 | 205,935 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let x be the number of such pairs of students in a split. Pairs (a, b) and (b, a) are the same and counted only once.
For example, if there are 6 students: "olivia", "jacob", "tanya", "jack", "oliver" and "jessica", then:
* splitting into two classrooms ("jack", "jacob", "jessica", "tanya") and ("olivia", "oliver") will give x=4 (3 chatting pairs in the first classroom, 1 chatting pair in the second classroom),
* splitting into two classrooms ("jack", "tanya", "olivia") and ("jessica", "oliver", "jacob") will give x=1 (0 chatting pairs in the first classroom, 1 chatting pair in the second classroom).
You are given the list of the n names. What is the minimum x we can obtain by splitting the students into classrooms?
Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty.
Input
The first line contains a single integer n (1≤ n ≤ 100) — the number of students.
After this n lines follow.
The i-th line contains the name of the i-th student.
It is guaranteed each name is a string of lowercase English letters of length at most 20. Note that multiple students may share the same name.
Output
The output must consist of a single integer x — the minimum possible number of chatty pairs.
Examples
Input
4
jorge
jose
oscar
jerry
Output
1
Input
7
kambei
gorobei
shichiroji
kyuzo
heihachi
katsushiro
kikuchiyo
Output
2
Input
5
mike
mike
mike
mike
mike
Output
4
Note
In the first sample the minimum number of pairs is 1. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.
In the second sample the minimum number of pairs is 2. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.
In the third sample the minimum number of pairs is 4. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom.
Submitted Solution:
```
d=dict()
n=int(input())
for i in range(n):
s=input()
t=s[0]
if not t in d:
d[t]=1
else:
d[t]+=1
ans=0
for i in d:
ans=max(ans,d[i])
t=(ans+1)//2
t2=ans//2
print(t*(t-1)//2+t2*(t2-1)//2)
``` | instruction | 0 | 102,968 | 14 | 205,936 |
No | output | 1 | 102,968 | 14 | 205,937 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let x be the number of such pairs of students in a split. Pairs (a, b) and (b, a) are the same and counted only once.
For example, if there are 6 students: "olivia", "jacob", "tanya", "jack", "oliver" and "jessica", then:
* splitting into two classrooms ("jack", "jacob", "jessica", "tanya") and ("olivia", "oliver") will give x=4 (3 chatting pairs in the first classroom, 1 chatting pair in the second classroom),
* splitting into two classrooms ("jack", "tanya", "olivia") and ("jessica", "oliver", "jacob") will give x=1 (0 chatting pairs in the first classroom, 1 chatting pair in the second classroom).
You are given the list of the n names. What is the minimum x we can obtain by splitting the students into classrooms?
Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty.
Input
The first line contains a single integer n (1≤ n ≤ 100) — the number of students.
After this n lines follow.
The i-th line contains the name of the i-th student.
It is guaranteed each name is a string of lowercase English letters of length at most 20. Note that multiple students may share the same name.
Output
The output must consist of a single integer x — the minimum possible number of chatty pairs.
Examples
Input
4
jorge
jose
oscar
jerry
Output
1
Input
7
kambei
gorobei
shichiroji
kyuzo
heihachi
katsushiro
kikuchiyo
Output
2
Input
5
mike
mike
mike
mike
mike
Output
4
Note
In the first sample the minimum number of pairs is 1. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.
In the second sample the minimum number of pairs is 2. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.
In the third sample the minimum number of pairs is 4. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom.
Submitted Solution:
```
from collections import Counter
n = int(input())
s = []
for _ in range(n):
s.append(input()[0])
done = [0] * n
first = []
second = []
for cc in Counter(s).most_common():
for i in range(cc[1]):
if i % 2 == 0:
first.append(cc[0])
else:
second.append(cc[0])
# for i in range(n):
# found = False
# if done[i]:
# found = True
# continue
# for j in range(i+1, n):
# if done[j]:
# continue
# if s[i][0] == s[j][0]:
# first.append(s[i][0])
# second.append(s[j][0])
# done[i] = True
# done[j] = True
# found = True
# break
# if not found and len(first) < len(second):
# done[i] = True
# first.append(s[i][0])
# elif not found:
# done[i] = True
# second.append(s[i][0])
c1 = 0
c2 = 0
for cc in Counter(first).most_common():
if cc[1] == 2:
c1 += 1
elif cc[1] > 2:
c1 += cc[1] #// 2 + cc[1]%2
for cc in Counter(second).most_common():
if cc[1] == 2:
c2 += 1
if cc[1] > 2:
c2 += cc[1]# // 2 + cc[1]%2
# paired = [-1] * len(first)
# for i in range(len(first)):
# for j in range(i+1, len(first)):
# if first[i][0] == first[j][0] and not (paired[i]==1 or paired[j]==1):
# c1 += 1
# paired[i] = 1
# paired[j] = 1
# print(Counter(second))
# paired = [-1] * len(second)
# for i in range(len(second)):
# for j in range(i+1, len(second)):
# if second[i][0] == second[j][0] and not (paired[i]==1 or paired[j]==1):
# c2 += 1
# paired[i] = 1
# paired[j] = 1
# print(first)
# print(second)
# print(c1,c2)
print(c1+c2)
# a,a
# b,b
# c,c
``` | instruction | 0 | 102,969 | 14 | 205,938 |
No | output | 1 | 102,969 | 14 | 205,939 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let x be the number of such pairs of students in a split. Pairs (a, b) and (b, a) are the same and counted only once.
For example, if there are 6 students: "olivia", "jacob", "tanya", "jack", "oliver" and "jessica", then:
* splitting into two classrooms ("jack", "jacob", "jessica", "tanya") and ("olivia", "oliver") will give x=4 (3 chatting pairs in the first classroom, 1 chatting pair in the second classroom),
* splitting into two classrooms ("jack", "tanya", "olivia") and ("jessica", "oliver", "jacob") will give x=1 (0 chatting pairs in the first classroom, 1 chatting pair in the second classroom).
You are given the list of the n names. What is the minimum x we can obtain by splitting the students into classrooms?
Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty.
Input
The first line contains a single integer n (1≤ n ≤ 100) — the number of students.
After this n lines follow.
The i-th line contains the name of the i-th student.
It is guaranteed each name is a string of lowercase English letters of length at most 20. Note that multiple students may share the same name.
Output
The output must consist of a single integer x — the minimum possible number of chatty pairs.
Examples
Input
4
jorge
jose
oscar
jerry
Output
1
Input
7
kambei
gorobei
shichiroji
kyuzo
heihachi
katsushiro
kikuchiyo
Output
2
Input
5
mike
mike
mike
mike
mike
Output
4
Note
In the first sample the minimum number of pairs is 1. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.
In the second sample the minimum number of pairs is 2. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.
In the third sample the minimum number of pairs is 4. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom.
Submitted Solution:
```
import math
n = int(input())
mark =[0 for _ in range(26)]
for i in range(n):
next = input()
mark[ord(next[0])-97]+=1
t = 0
for v in mark:
a = v/2
a1 = int(math.ceil(a))
a2 = int(math.floor(a))
# print(v,a1,a2)
if a1>1:
if a1==2:
t+=1
else:
t+=a1
if a2>1:
if a2==2:
t+=1
else:
t+=a2
print(t)
``` | instruction | 0 | 102,970 | 14 | 205,940 |
No | output | 1 | 102,970 | 14 | 205,941 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let x be the number of such pairs of students in a split. Pairs (a, b) and (b, a) are the same and counted only once.
For example, if there are 6 students: "olivia", "jacob", "tanya", "jack", "oliver" and "jessica", then:
* splitting into two classrooms ("jack", "jacob", "jessica", "tanya") and ("olivia", "oliver") will give x=4 (3 chatting pairs in the first classroom, 1 chatting pair in the second classroom),
* splitting into two classrooms ("jack", "tanya", "olivia") and ("jessica", "oliver", "jacob") will give x=1 (0 chatting pairs in the first classroom, 1 chatting pair in the second classroom).
You are given the list of the n names. What is the minimum x we can obtain by splitting the students into classrooms?
Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty.
Input
The first line contains a single integer n (1≤ n ≤ 100) — the number of students.
After this n lines follow.
The i-th line contains the name of the i-th student.
It is guaranteed each name is a string of lowercase English letters of length at most 20. Note that multiple students may share the same name.
Output
The output must consist of a single integer x — the minimum possible number of chatty pairs.
Examples
Input
4
jorge
jose
oscar
jerry
Output
1
Input
7
kambei
gorobei
shichiroji
kyuzo
heihachi
katsushiro
kikuchiyo
Output
2
Input
5
mike
mike
mike
mike
mike
Output
4
Note
In the first sample the minimum number of pairs is 1. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.
In the second sample the minimum number of pairs is 2. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.
In the third sample the minimum number of pairs is 4. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom.
Submitted Solution:
```
l = []
for _ in range(int(input())):
s= input()
l.append(s[0])
t = set(l)
counts = []
for i in t:
c = l.count(i)
counts.append(c)
x = 0
for i in counts:
print(i)
if(i>2):
#if(i%2==0):
x=x+(i//2)
#else:
#x = x+((i+1)//2)
print(x)
``` | instruction | 0 | 102,971 | 14 | 205,942 |
No | output | 1 | 102,971 | 14 | 205,943 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications). | instruction | 0 | 103,344 | 14 | 206,688 |
Tags: brute force, data structures, implementation
Correct Solution:
```
import os
import io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n, q = map(int, input().split())
last_2 = [-1] * n
unread_count = [0] * n
total_unread = 0
ev = []
max_3 = 0
ans = []
for i in range(q):
t, x = map(int, input().split())
x -= 1
if t==1:
ev.append(x)
unread_count[x] += 1
total_unread += 1
elif t==2:
total_unread -= unread_count[x]
unread_count[x] = 0
last_2[x] = len(ev)-1
else:
for j in range(max_3, x+1):
el = ev[j]
if last_2[el] < j:
total_unread -= 1
unread_count[el] -= 1
max_3 = max(max_3, x+1)
ans.append(total_unread)
for a in ans:
print(a)
``` | output | 1 | 103,344 | 14 | 206,689 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications). | instruction | 0 | 103,345 | 14 | 206,690 |
Tags: brute force, data structures, implementation
Correct Solution:
```
from collections import defaultdict, deque
q = deque()
idx, total = 0, 0
vis = defaultdict(deque)
not_valid = set()
max_x = -1
ans = []
N, Q = map(int, input().split())
for _ in range(Q):
t, x = map(int, input().split())
if t == 1:
q.append((idx, x))
vis[x].append(idx)
idx += 1
total += 1
elif t == 2:
while vis[x]:
# All element in vis[x] should be popped out from q, and It will do it later.
# The index are collected in the not_valid set.
i = vis[x].pop()
not_valid.add(i)
# If the element is expired, then skip it.
if i <= max_x - 1:
continue
total -= 1
else:
# Check the element should be expired or popped out.
while q and (q[0][0] <= x - 1 or q[0][0] in not_valid):
j, y = q.popleft()
if j not in not_valid:
total -= 1
vis[y].popleft()
max_x = max(max_x, x)
ans.append(str(total))
# Trick: print the answer at the end to avoid TLE
# by keep commnicating with kernel space.
print("\n".join(ans))
``` | output | 1 | 103,345 | 14 | 206,691 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications). | instruction | 0 | 103,346 | 14 | 206,692 |
Tags: brute force, data structures, implementation
Correct Solution:
```
from heapq import *
from collections import defaultdict, deque
total = 0
N, Q = map(int, input().split())
q = deque()
idx = 0
vis = defaultdict(deque)
total = 0
not_valid = set()
max_d = -1
ans = []
for _ in range(Q):
t, x = map(int, input().split())
if t == 1:
q.append((idx, x))
vis[x].append(idx)
idx += 1
total += 1
elif t == 2:
while vis[x]:
i = vis[x].pop()
not_valid.add(i)
if i <= max_d:
continue
total -= 1
else:
while q and (q[0][0] <= x - 1 or q[0][0] in not_valid):
j, y = q.popleft()
if j not in not_valid:
total -= 1
if len(vis[y]):
vis[y].popleft()
max_d = max(max_d, x - 1)
ans.append(str(total))
print("\n".join(ans))
``` | output | 1 | 103,346 | 14 | 206,693 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications). | instruction | 0 | 103,347 | 14 | 206,694 |
Tags: brute force, data structures, implementation
Correct Solution:
```
import collections
import sys
n, q = map(int, input().split())
# Key: app number, value: list of indexes in the arr
hash = {}
# arr = []
deque = collections.deque()
messageNumber = 1
unread = 0
L = []
for i in range(q):
c, x = map(int, input().split())
if c == 1:
if x not in hash:
hash[x] = set()
hash[x].add(messageNumber)
deque.append((x, messageNumber))
messageNumber += 1
unread += 1
# print("case 1")
# print(unread)
elif c == 2:
if x in hash:
xUnread = len(hash[x])
hash[x] = set()
unread -= xUnread
# print("case 2")
# print(unread)
else:
t = x
read = 0
while len(deque) != 0 and deque[0][1] <= t:
app, message = deque.popleft()
if message in hash[app]:
read += 1
hash[app].remove(message)
unread -= read
# print("case 3")
# print(unread)
L.append(unread)
sys.stdout.write('\n'.join(map(str, L)))
``` | output | 1 | 103,347 | 14 | 206,695 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications). | instruction | 0 | 103,348 | 14 | 206,696 |
Tags: brute force, data structures, implementation
Correct Solution:
```
#!/usr/bin/env python
#-*-coding:utf-8 -*-
import collections
n,q=map(int,input().split())
Q=collections.deque()
A=n*[0]
B=A[:]
L=[]
s=n=0
for _ in range(q):
y,x=map(int,input().split())
if 2>y:
x-=1
Q.append(x)
B[x]+=1
A[x]+=1
s+=1
elif 3>y:
x-=1
s-=A[x]
A[x]=0
else:
while x>n:
n+=1
y=Q.popleft()
B[y]-=1
if(B[y]<A[y]):
A[y]-=1
s-=1
L.append(s)
print('\n'.join(map(str,L)))
``` | output | 1 | 103,348 | 14 | 206,697 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications). | instruction | 0 | 103,349 | 14 | 206,698 |
Tags: brute force, data structures, implementation
Correct Solution:
```
#!/usr/bin/env python
#-*-coding:utf-8 -*-
import sys,collections
n,q=map(int,input().split())
M=collections.defaultdict(collections.deque)
Q=collections.deque()
L=[]
s=n=m=0
for _ in range(q):
y,x=map(int,input().split())
if 2>y:
s+=1
Q.append(x)
M[x].append(n)
n+=1
elif 3>y:
y=M.get(x)
if y:
s-=len(y)
del M[x]
else:
while x>m:
z=Q.popleft()
y=M.get(z)
if y and y[0]<x:
s-=1
y.popleft()
if not y:del M[z]
m+=1
L.append(s)
sys.stdout.write('\n'.join(map(str,L)))
``` | output | 1 | 103,349 | 14 | 206,699 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications). | instruction | 0 | 103,350 | 14 | 206,700 |
Tags: brute force, data structures, implementation
Correct Solution:
```
import collections
n, q = map(int ,input().split())
Q = collections.deque()
A = [0] * n
B = A[:]
L = []
s = n = 0
for k in range(q):
type1, x = map(int, input().split())
if type1 == 1:
x -= 1
Q.append(x)
B[x] += 1
A[x] += 1
s += 1
if type1 == 2:
x -= 1
s -= A[x]
A[x] = 0
if type1 == 3:
while x > n:
n += 1
y = Q.popleft()
B[y] -= 1
if B[y] < A[y]:
A[y] -= 1
s -= 1
L.append(str(s))
print('\n'.join(L))
``` | output | 1 | 103,350 | 14 | 206,701 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications). | instruction | 0 | 103,351 | 14 | 206,702 |
Tags: brute force, data structures, implementation
Correct Solution:
```
from sys import stdin
input=stdin.readline
n,q=map(int,input().split())
is_read_index=0
is_read=[]
l=[[] for i in range(n)]
ans=0
prev=0
for _ in range(q):
t,v=map(int,input().split())
if t==1:
l[v-1].append(is_read_index)
is_read_index+=1
is_read.append(False)
ans+=1
elif t==2:
for idx in l[v-1]:
if not is_read[idx]:
is_read[idx]=True
ans-=1
l[v-1]=[]
else:
if v>prev:
for idx in range(prev,v):
if not is_read[idx]:
is_read[idx]=True
ans-=1
prev=v
print(ans)
``` | output | 1 | 103,351 | 14 | 206,703 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Submitted Solution:
```
def main():
n, q = map(int, input().split())
vol, tot, l, res = [0] * (n + 1), [0] * (n + 1), [], []
z = m = 0
for _ in range(q):
t, x = map(int, input().split())
if t == 1:
l.append(x)
tot[x] += 1
vol[x] += 1
z += 1
elif t == 2:
z -= vol[x]
vol[x] = 0
else:
if m < x:
r, m = range(m, x), x
for i in r:
x = l[i]
tot[x] -= 1
if vol[x] > tot[x]:
vol[x] -= 1
z -= 1
res.append(z)
print('\n'.join(map(str, res)))
if __name__ == '__main__':
main()
``` | instruction | 0 | 103,352 | 14 | 206,704 |
Yes | output | 1 | 103,352 | 14 | 206,705 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Submitted Solution:
```
n,q = map(int,input().split())
xi = [[0]*(n+1) for i in range(2)]
noti = []
ans = ""
num = 0
num2 = 0
num3 = 0
while q > 0:
typ,xt = map(int,input().split())
if typ == 1:
xi[0][xt] += 1
noti += [xt]
num += 1
num2 += 1
elif typ == 3:
for i in range(num3,xt):
if i+1 > xi[1][noti[i]]:
xi[0][noti[i]] -= 1
num -= 1
num3 = max(num3,xt)
else:
num -= xi[0][xt]
xi[0][xt] = 0
xi[1][xt] = num2
ans += str(num)
ans += "\n"
q -= 1
print(ans)
``` | instruction | 0 | 103,353 | 14 | 206,706 |
Yes | output | 1 | 103,353 | 14 | 206,707 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Submitted Solution:
```
"""
#If FastIO not needed, used this and don't forget to strip
#import sys, math
#input = sys.stdin.readline
"""
import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
from types import GeneratorType
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
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 = self.os.read(self._fd, max(self.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 = self.os.read(self._fd, max(self.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:
self.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")
from collections import defaultdict as dd, deque as dq
import math, string
def getInts():
return [int(s) for s in input().split()]
def getInt():
return int(input())
def getStrs():
return [s for s in input().split()]
def getStr():
return input()
def listStr():
return list(input())
MOD = 10**9+7
"""
We need to maintain a set of unread notifications from which we can remove the first max(ti), and also the first xi for each application
[3,4,4,1,1,1,2,2,1,3,3,2,4,4,1,2,3,2,4,4]
N applications. At any given time the number of unread notifications is the sum of the number of unread notifications for each application, which lends itself to a Fenwick Tree
But how do we maintain the order?
As we delete the first N elements, we can also subtract one from the rolling sum and from the element total.
As we clear an element of type X, we can subtract that many from the rolling total
"""
def solve():
N, Q = getInts()
order = dq([])
cur_total = 0
cur_del = -1
els = [0]*(N+1)
del_up_to = [-1]*(N+1)
for q in range(Q):
T, X = getInts()
if T == 1:
order.append(X)
els[X] += 1
cur_total += 1
print(cur_total)
elif T == 2:
cur_total -= els[X]
els[X] = 0
del_up_to[X] = max(len(order)-1,del_up_to[X])
print(cur_total)
elif T == 3:
if X-1 > cur_del:
for i in range(cur_del+1,X):
if del_up_to[order[i]] < i:
cur_total -= 1
del_up_to[order[i]] = i
els[order[i]] -= 1
#print(order)
#print(els)
#print(del_up_to)
cur_del = X-1
print(cur_total)
return
#for _ in range(getInt()):
solve()
``` | instruction | 0 | 103,354 | 14 | 206,708 |
Yes | output | 1 | 103,354 | 14 | 206,709 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Submitted Solution:
```
from sys import stdout
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
print = stdout.write
n, q = map(int, input().split())
d = [[] for i in range(n+1)]
mark = []
ans = 0
inde = 0
t = 0
for i in range(q):
event, x = map(int, input().split())
if event == 1:
ans += 1
d[x].append(inde)
inde += 1
mark.append(False)
elif event == 2:
for j in d[x]:
if not mark[j]:
mark[j] = True
ans -= 1
d[x] = []
else:
for j in range(t, x):
if not mark[j]:
mark[j] = True
ans -= 1
if x > t: t = x
print(str(ans) + '\n')
``` | instruction | 0 | 103,355 | 14 | 206,710 |
Yes | output | 1 | 103,355 | 14 | 206,711 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Submitted Solution:
```
from collections import deque
n,u=map(int,input().split())
count=0
d={i:0 for i in range(1,n+1)}
q=deque([])
for i in range(u):
t,x=map(int,input().split())
if t==1:
count+=1
d[x]+=1
q.append(x)
elif t==2:
val=d[x]
d[x]=0
count-=val
else:
while x>0 and q:
ele=q.popleft()
if d[ele]>0:
d[ele]-=1
count-=1
x-=1
print(count)
``` | instruction | 0 | 103,356 | 14 | 206,712 |
No | output | 1 | 103,356 | 14 | 206,713 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.