ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 140 ms):
from collections import Counter
n=int(input())
for i in range(n):
    a,b = list(map(int, input().split()))
    arr = Counter(input().split())
    new_arr = sorted(list(arr.values()))
    for i in range(len(new_arr)):
        if b == 0:
            break
        if b >= new_arr[i]:
            b = b - new_arr[i]
        else:
            break
    print(len(new_arr) - i)

Solution 2 (runtime: 155 ms):
from collections import defaultdict
t = int(input())
for i in range(t):
    n, k = map(int, input().split())
    a = sorted(map(int, input().split()))
    groups = []
    counter = 1
    for i in range(1, len(a)):
        if a[i] != a[i - 1]:
            groups.append(counter)
            counter = 1
        else:
            counter += 1
    groups.append(counter)
    groups = sorted(groups)
    ans = len(groups)
    for group in groups:
        if group > k:
            break
        k -= group
        ans -= 1
    print(max(ans, 1))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 140 ms):
from collections import Counter
n=int(input())
for i in range(n):
    a,b = list(map(int, input().split()))
    arr = Counter(input().split())
    new_arr = sorted(list(arr.values()))
    for i in range(len(new_arr)):
        if b == 0:
            break
        if b >= new_arr[i]:
            b = b - new_arr[i]
        else:
            break
    print(len(new_arr) - i)

Solution 2 (runtime: 296 ms):
from collections import Counter
def solve(a,n,k):
    a.sort()
    cnt = Counter(a)
    counts = sorted(cnt.values())
    m = len(counts)

    for i in range(m - 1):
        if counts[i] > k:
            print(m - i)
            return
        k -= counts[i]
    
    print(1)
t=int(input())
for i in range(t):
    n,k=map(int,input().split())
    a=list(map(int,input().split()))
    solve(a,n,k)
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 140 ms):
from collections import Counter
n=int(input())
for i in range(n):
    a,b = list(map(int, input().split()))
    arr = Counter(input().split())
    new_arr = sorted(list(arr.values()))
    for i in range(len(new_arr)):
        if b == 0:
            break
        if b >= new_arr[i]:
            b = b - new_arr[i]
        else:
            break
    print(len(new_arr) - i)

Solution 2 (runtime: 171 ms):
n = int(input())
for _ in range(n):
    l, k = map(int, input().split())
    input_str = input()
    arr = list(map(int, input_str.split()))

    # 先排序
    arr.sort()

    # 存储不同的数字有几组
    count = []
    curnum = 1

    for i in range(1, l):
        if arr[i] == arr[i - 1]:
            curnum += 1
        else:
            count.append(curnum)
            curnum = 1
    count.append(curnum)

    count.sort()
    sum = 0
    ans = len(count)
    for c in count:
        if ans == 1:
            break
        sum += c
        if k >= sum:
            ans -= 1
        else:
            break
    print(ans)
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 140 ms):
from collections import Counter
n=int(input())
for i in range(n):
    a,b = list(map(int, input().split()))
    arr = Counter(input().split())
    new_arr = sorted(list(arr.values()))
    for i in range(len(new_arr)):
        if b == 0:
            break
        if b >= new_arr[i]:
            b = b - new_arr[i]
        else:
            break
    print(len(new_arr) - i)

Solution 2 (runtime: 140 ms):
if __name__ == "__main__":
    tc = 0
    tc = int(input())
    lres = []
    for i in range(tc):
        nkRaw = input()
        n = int(nkRaw.split(" ")[0])
        k = int(nkRaw.split(" ")[1])
        minimum = 0
        maximum = 1
        arrayRaw = input()
        array = arrayRaw.split(" ")
        diccionario = {}
        for num in range(int(n)):
            if(array[num] not in diccionario):
                diccionario[array[num]] = 1
            else:
                diccionario[array[num]] += 1;
        if(k >= n):
            lres.append(1)
        else:
            lista_diccionario = sorted(diccionario.values())
            aux = 0
            for num in lista_diccionario:
                if(num <= k):
                    k -= num
                    aux += 1
                else:
                    break
            lres.append(len(lista_diccionario) - aux)
    for res in lres:
        print(res)
#end
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 140 ms):
from collections import Counter
n=int(input())
for i in range(n):
    a,b = list(map(int, input().split()))
    arr = Counter(input().split())
    new_arr = sorted(list(arr.values()))
    for i in range(len(new_arr)):
        if b == 0:
            break
        if b >= new_arr[i]:
            b = b - new_arr[i]
        else:
            break
    print(len(new_arr) - i)

Solution 2 (runtime: 187 ms):
for q in range(int(input())):
    n,k=list(map(int,input().split(' ')))
    a=list(map(int,input().split(' ')))
    b=set(a)
    d={}
    for i in b:
        d[i]=0
    for i in a:
        d[i]+=1
    l=[]
    for i in d:
        l.append(d[i])
    pre=0
    ans=len(l)
    l.sort()
    for i in range(len(l)):
        if(pre+l[i]<=k):
            pre+=l[i]
            ans-=1
    print(max(ans,1))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 140 ms):
from collections import Counter
n=int(input())
for i in range(n):
    a,b = list(map(int, input().split()))
    arr = Counter(input().split())
    new_arr = sorted(list(arr.values()))
    for i in range(len(new_arr)):
        if b == 0:
            break
        if b >= new_arr[i]:
            b = b - new_arr[i]
        else:
            break
    print(len(new_arr) - i)

Solution 2 (runtime: 312 ms):
import bisect
t=int(input())
for i in range(t):
    n,k=map(int,input().split())
    l=list(map(int,input().split()))
    l.sort()
    f=[]
    c=1
    for i in range(1,n):
        if(l[i]==l[i-1]):
            c+=1
        else:
            f.append(c)
            c=1
    f.append(c)
    f.sort()
    x=0

    pf=[]
    for j in f:
        x+=j
        pf.append(x)
    v=bisect.bisect_left(pf,k)
    y=0
    #print(pf,v)
    if(v==n):
        print(1)
    elif(v==0):
        if(k!=0):
            if(k<pf[0]):
                y=1
                print(max(1,len(f)-1-v+y))
            else:
                print(max(1,len(f)-1-v))
        else:
            print(len(f))
    elif(k<pf[v]):
        y=1
        print(len(f)-1-v+y)
    
    else:
        print(max(1,len(f)-1-v))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 140 ms):
from collections import Counter
n=int(input())
for i in range(n):
    a,b = list(map(int, input().split()))
    arr = Counter(input().split())
    new_arr = sorted(list(arr.values()))
    for i in range(len(new_arr)):
        if b == 0:
            break
        if b >= new_arr[i]:
            b = b - new_arr[i]
        else:
            break
    print(len(new_arr) - i)

Solution 2 (runtime: 187 ms):
import sys
from collections import Counter

input = sys.stdin.readline
output = sys.stdout.write
str_input = lambda: input().strip()
int_input = lambda: int(input())
multi_int_input = lambda: map(int, input().split())
list_input = lambda: list(map(int, input().split()))
P1 = lambda X: result_array.append(f"{X}\n")
P2 = lambda X,Y: result_array.append(f"{X} {Y}\n")
P3 = lambda X,Y,Z: result_array.append(f"{X} {Y} {Z}\n")
PA = lambda X: result_array.append(f"{' '.join(map(str,X))}\n")


def solve(test_case):
    N,K = multi_int_input()
    arr = list_input()

    check = Counter()
    for i in arr:
        check[str(i)] += 1
    check = sorted(check.values(),reverse=True)

    while check:
        if check[-1] <= K:
            K -= check.pop()
        else:
            break
    
    P1(max(1,len(check)))


tc = 1
tc = int_input()
result_array = []
for test_case in range(1,tc+1):
    solve(test_case)
output(''.join(result_array))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 140 ms):
from collections import Counter
n=int(input())
for i in range(n):
    a,b = list(map(int, input().split()))
    arr = Counter(input().split())
    new_arr = sorted(list(arr.values()))
    for i in range(len(new_arr)):
        if b == 0:
            break
        if b >= new_arr[i]:
            b = b - new_arr[i]
        else:
            break
    print(len(new_arr) - i)

Solution 2 (runtime: 249 ms):
t = int(input())
while t:
	t -= 1
	n, k =  map(int, input().split())
	ordem = {}
	array = list(map(int, input().split()))
	array.sort()
	freq = []

	prev = array[0]
	count = 0
	for num in array:
		if num != prev:
			freq.append(count)
			count = 0
		count += 1
		prev = num		
	freq.append(count)
	freq.sort()
	
	sum = 0
	count = 0
	for num in freq:
		sum += num
		if k >= sum:
			count += 1
		else:
			break
	
	print(max(1, len(freq)-count))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 140 ms):
from collections import Counter
n=int(input())
for i in range(n):
    a,b = list(map(int, input().split()))
    arr = Counter(input().split())
    new_arr = sorted(list(arr.values()))
    for i in range(len(new_arr)):
        if b == 0:
            break
        if b >= new_arr[i]:
            b = b - new_arr[i]
        else:
            break
    print(len(new_arr) - i)

Solution 2 (runtime: 249 ms):
for _ in range(int(input())):
  n, k = list(map(int, input().split()))
  a = list(map(int, input().split()))
  
  a.sort()
  # store the freq of nums
  cnt = [1]
  for i in range(1, n):
    if a[i] == a[i-1]:
      cnt[-1] += 1
    else:
      cnt.append(1)
  
  # sort the cnts to check with k and convert them to min
  cnt.sort()
  m = len(cnt)
  flag = False
  for i in range(m-1):
    if cnt[i] > k:
      # we can't change any ai so
      print(m-i)
      flag = True
      break
    
    k -= cnt[i]
  
  if flag == False:
    print(1)
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 233 ms):
n = int(input())
for _ in range(n):
    m, k = map(int, input().split())
    arr = list(map(int, input().split()))
    arr.sort()
    freqencies = list()
    count = 0
    prev = arr[0]
    for i in arr:
        if i == prev:
            count+=1
        else:
            prev = i
            freqencies.append(count)
            count = 1
    freqencies.append(count)
    freqencies.sort(reverse=True)            
    remove = 0
    # print(freqencies, k)
    while len(freqencies) > 1 and remove + freqencies[-1] <= k:
        remove += freqencies.pop()
    print(len(freqencies))


Solution 2 (runtime: 234 ms):
import sys
input = sys.stdin.readline #input
from collections import defaultdict
 
def solve():
    n,k = map(int,input().split())
    arr = input().split()
    freq = defaultdict(lambda: 0)
    for num in arr:
        freq[num]+=1
    total=len(freq)
 
    for key in sorted(freq, key = lambda x: freq[x]):
        if freq[key]<=k:
            k-=freq[key]
            total-=1
        else:
            break
    print(max(total,1))
 
 
for _ in range(int(input())):
    solve()
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 233 ms):
n = int(input())
for _ in range(n):
    m, k = map(int, input().split())
    arr = list(map(int, input().split()))
    arr.sort()
    freqencies = list()
    count = 0
    prev = arr[0]
    for i in arr:
        if i == prev:
            count+=1
        else:
            prev = i
            freqencies.append(count)
            count = 1
    freqencies.append(count)
    freqencies.sort(reverse=True)            
    remove = 0
    # print(freqencies, k)
    while len(freqencies) > 1 and remove + freqencies[-1] <= k:
        remove += freqencies.pop()
    print(len(freqencies))


Solution 2 (runtime: 155 ms):
from collections import defaultdict
t = int(input())
for i in range(t):
    n, k = map(int, input().split())
    a = sorted(map(int, input().split()))
    groups = []
    counter = 1
    for i in range(1, len(a)):
        if a[i] != a[i - 1]:
            groups.append(counter)
            counter = 1
        else:
            counter += 1
    groups.append(counter)
    groups = sorted(groups)
    ans = len(groups)
    for group in groups:
        if group > k:
            break
        k -= group
        ans -= 1
    print(max(ans, 1))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 233 ms):
n = int(input())
for _ in range(n):
    m, k = map(int, input().split())
    arr = list(map(int, input().split()))
    arr.sort()
    freqencies = list()
    count = 0
    prev = arr[0]
    for i in arr:
        if i == prev:
            count+=1
        else:
            prev = i
            freqencies.append(count)
            count = 1
    freqencies.append(count)
    freqencies.sort(reverse=True)            
    remove = 0
    # print(freqencies, k)
    while len(freqencies) > 1 and remove + freqencies[-1] <= k:
        remove += freqencies.pop()
    print(len(freqencies))


Solution 2 (runtime: 296 ms):
from collections import Counter
def solve(a,n,k):
    a.sort()
    cnt = Counter(a)
    counts = sorted(cnt.values())
    m = len(counts)

    for i in range(m - 1):
        if counts[i] > k:
            print(m - i)
            return
        k -= counts[i]
    
    print(1)
t=int(input())
for i in range(t):
    n,k=map(int,input().split())
    a=list(map(int,input().split()))
    solve(a,n,k)
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 233 ms):
n = int(input())
for _ in range(n):
    m, k = map(int, input().split())
    arr = list(map(int, input().split()))
    arr.sort()
    freqencies = list()
    count = 0
    prev = arr[0]
    for i in arr:
        if i == prev:
            count+=1
        else:
            prev = i
            freqencies.append(count)
            count = 1
    freqencies.append(count)
    freqencies.sort(reverse=True)            
    remove = 0
    # print(freqencies, k)
    while len(freqencies) > 1 and remove + freqencies[-1] <= k:
        remove += freqencies.pop()
    print(len(freqencies))


Solution 2 (runtime: 171 ms):
n = int(input())
for _ in range(n):
    l, k = map(int, input().split())
    input_str = input()
    arr = list(map(int, input_str.split()))

    # 先排序
    arr.sort()

    # 存储不同的数字有几组
    count = []
    curnum = 1

    for i in range(1, l):
        if arr[i] == arr[i - 1]:
            curnum += 1
        else:
            count.append(curnum)
            curnum = 1
    count.append(curnum)

    count.sort()
    sum = 0
    ans = len(count)
    for c in count:
        if ans == 1:
            break
        sum += c
        if k >= sum:
            ans -= 1
        else:
            break
    print(ans)
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 233 ms):
n = int(input())
for _ in range(n):
    m, k = map(int, input().split())
    arr = list(map(int, input().split()))
    arr.sort()
    freqencies = list()
    count = 0
    prev = arr[0]
    for i in arr:
        if i == prev:
            count+=1
        else:
            prev = i
            freqencies.append(count)
            count = 1
    freqencies.append(count)
    freqencies.sort(reverse=True)            
    remove = 0
    # print(freqencies, k)
    while len(freqencies) > 1 and remove + freqencies[-1] <= k:
        remove += freqencies.pop()
    print(len(freqencies))


Solution 2 (runtime: 140 ms):
if __name__ == "__main__":
    tc = 0
    tc = int(input())
    lres = []
    for i in range(tc):
        nkRaw = input()
        n = int(nkRaw.split(" ")[0])
        k = int(nkRaw.split(" ")[1])
        minimum = 0
        maximum = 1
        arrayRaw = input()
        array = arrayRaw.split(" ")
        diccionario = {}
        for num in range(int(n)):
            if(array[num] not in diccionario):
                diccionario[array[num]] = 1
            else:
                diccionario[array[num]] += 1;
        if(k >= n):
            lres.append(1)
        else:
            lista_diccionario = sorted(diccionario.values())
            aux = 0
            for num in lista_diccionario:
                if(num <= k):
                    k -= num
                    aux += 1
                else:
                    break
            lres.append(len(lista_diccionario) - aux)
    for res in lres:
        print(res)
#end
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 233 ms):
n = int(input())
for _ in range(n):
    m, k = map(int, input().split())
    arr = list(map(int, input().split()))
    arr.sort()
    freqencies = list()
    count = 0
    prev = arr[0]
    for i in arr:
        if i == prev:
            count+=1
        else:
            prev = i
            freqencies.append(count)
            count = 1
    freqencies.append(count)
    freqencies.sort(reverse=True)            
    remove = 0
    # print(freqencies, k)
    while len(freqencies) > 1 and remove + freqencies[-1] <= k:
        remove += freqencies.pop()
    print(len(freqencies))


Solution 2 (runtime: 187 ms):
for q in range(int(input())):
    n,k=list(map(int,input().split(' ')))
    a=list(map(int,input().split(' ')))
    b=set(a)
    d={}
    for i in b:
        d[i]=0
    for i in a:
        d[i]+=1
    l=[]
    for i in d:
        l.append(d[i])
    pre=0
    ans=len(l)
    l.sort()
    for i in range(len(l)):
        if(pre+l[i]<=k):
            pre+=l[i]
            ans-=1
    print(max(ans,1))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 233 ms):
n = int(input())
for _ in range(n):
    m, k = map(int, input().split())
    arr = list(map(int, input().split()))
    arr.sort()
    freqencies = list()
    count = 0
    prev = arr[0]
    for i in arr:
        if i == prev:
            count+=1
        else:
            prev = i
            freqencies.append(count)
            count = 1
    freqencies.append(count)
    freqencies.sort(reverse=True)            
    remove = 0
    # print(freqencies, k)
    while len(freqencies) > 1 and remove + freqencies[-1] <= k:
        remove += freqencies.pop()
    print(len(freqencies))


Solution 2 (runtime: 312 ms):
import bisect
t=int(input())
for i in range(t):
    n,k=map(int,input().split())
    l=list(map(int,input().split()))
    l.sort()
    f=[]
    c=1
    for i in range(1,n):
        if(l[i]==l[i-1]):
            c+=1
        else:
            f.append(c)
            c=1
    f.append(c)
    f.sort()
    x=0

    pf=[]
    for j in f:
        x+=j
        pf.append(x)
    v=bisect.bisect_left(pf,k)
    y=0
    #print(pf,v)
    if(v==n):
        print(1)
    elif(v==0):
        if(k!=0):
            if(k<pf[0]):
                y=1
                print(max(1,len(f)-1-v+y))
            else:
                print(max(1,len(f)-1-v))
        else:
            print(len(f))
    elif(k<pf[v]):
        y=1
        print(len(f)-1-v+y)
    
    else:
        print(max(1,len(f)-1-v))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 233 ms):
n = int(input())
for _ in range(n):
    m, k = map(int, input().split())
    arr = list(map(int, input().split()))
    arr.sort()
    freqencies = list()
    count = 0
    prev = arr[0]
    for i in arr:
        if i == prev:
            count+=1
        else:
            prev = i
            freqencies.append(count)
            count = 1
    freqencies.append(count)
    freqencies.sort(reverse=True)            
    remove = 0
    # print(freqencies, k)
    while len(freqencies) > 1 and remove + freqencies[-1] <= k:
        remove += freqencies.pop()
    print(len(freqencies))


Solution 2 (runtime: 187 ms):
import sys
from collections import Counter

input = sys.stdin.readline
output = sys.stdout.write
str_input = lambda: input().strip()
int_input = lambda: int(input())
multi_int_input = lambda: map(int, input().split())
list_input = lambda: list(map(int, input().split()))
P1 = lambda X: result_array.append(f"{X}\n")
P2 = lambda X,Y: result_array.append(f"{X} {Y}\n")
P3 = lambda X,Y,Z: result_array.append(f"{X} {Y} {Z}\n")
PA = lambda X: result_array.append(f"{' '.join(map(str,X))}\n")


def solve(test_case):
    N,K = multi_int_input()
    arr = list_input()

    check = Counter()
    for i in arr:
        check[str(i)] += 1
    check = sorted(check.values(),reverse=True)

    while check:
        if check[-1] <= K:
            K -= check.pop()
        else:
            break
    
    P1(max(1,len(check)))


tc = 1
tc = int_input()
result_array = []
for test_case in range(1,tc+1):
    solve(test_case)
output(''.join(result_array))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 233 ms):
n = int(input())
for _ in range(n):
    m, k = map(int, input().split())
    arr = list(map(int, input().split()))
    arr.sort()
    freqencies = list()
    count = 0
    prev = arr[0]
    for i in arr:
        if i == prev:
            count+=1
        else:
            prev = i
            freqencies.append(count)
            count = 1
    freqencies.append(count)
    freqencies.sort(reverse=True)            
    remove = 0
    # print(freqencies, k)
    while len(freqencies) > 1 and remove + freqencies[-1] <= k:
        remove += freqencies.pop()
    print(len(freqencies))


Solution 2 (runtime: 249 ms):
t = int(input())
while t:
	t -= 1
	n, k =  map(int, input().split())
	ordem = {}
	array = list(map(int, input().split()))
	array.sort()
	freq = []

	prev = array[0]
	count = 0
	for num in array:
		if num != prev:
			freq.append(count)
			count = 0
		count += 1
		prev = num		
	freq.append(count)
	freq.sort()
	
	sum = 0
	count = 0
	for num in freq:
		sum += num
		if k >= sum:
			count += 1
		else:
			break
	
	print(max(1, len(freq)-count))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 233 ms):
n = int(input())
for _ in range(n):
    m, k = map(int, input().split())
    arr = list(map(int, input().split()))
    arr.sort()
    freqencies = list()
    count = 0
    prev = arr[0]
    for i in arr:
        if i == prev:
            count+=1
        else:
            prev = i
            freqencies.append(count)
            count = 1
    freqencies.append(count)
    freqencies.sort(reverse=True)            
    remove = 0
    # print(freqencies, k)
    while len(freqencies) > 1 and remove + freqencies[-1] <= k:
        remove += freqencies.pop()
    print(len(freqencies))


Solution 2 (runtime: 249 ms):
for _ in range(int(input())):
  n, k = list(map(int, input().split()))
  a = list(map(int, input().split()))
  
  a.sort()
  # store the freq of nums
  cnt = [1]
  for i in range(1, n):
    if a[i] == a[i-1]:
      cnt[-1] += 1
    else:
      cnt.append(1)
  
  # sort the cnts to check with k and convert them to min
  cnt.sort()
  m = len(cnt)
  flag = False
  for i in range(m-1):
    if cnt[i] > k:
      # we can't change any ai so
      print(m-i)
      flag = True
      break
    
    k -= cnt[i]
  
  if flag == False:
    print(1)
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 234 ms):
import sys
input = sys.stdin.readline #input
from collections import defaultdict
 
def solve():
    n,k = map(int,input().split())
    arr = input().split()
    freq = defaultdict(lambda: 0)
    for num in arr:
        freq[num]+=1
    total=len(freq)
 
    for key in sorted(freq, key = lambda x: freq[x]):
        if freq[key]<=k:
            k-=freq[key]
            total-=1
        else:
            break
    print(max(total,1))
 
 
for _ in range(int(input())):
    solve()

Solution 2 (runtime: 155 ms):
from collections import defaultdict
t = int(input())
for i in range(t):
    n, k = map(int, input().split())
    a = sorted(map(int, input().split()))
    groups = []
    counter = 1
    for i in range(1, len(a)):
        if a[i] != a[i - 1]:
            groups.append(counter)
            counter = 1
        else:
            counter += 1
    groups.append(counter)
    groups = sorted(groups)
    ans = len(groups)
    for group in groups:
        if group > k:
            break
        k -= group
        ans -= 1
    print(max(ans, 1))