ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 483 ms):
from sys import stdin

input = lambda: stdin.readline().strip()



n = int(input())

a = list(map(int, input().split()))

ans = []



for _ in range(n):

    for i in range(n - 1):

        if a[i] < a[i + 1]:

            ans.append((a[i], a[i + 1]))

            a[i], a[i + 1] = a[i + 1], a[i]

for _ in range(n):

    for i in range(n - 1):

        if a[i] > a[i + 1]:

            ans.append((a[i], a[i + 1]))

            a[i], a[i + 1] = a[i + 1], a[i]

ans.reverse()

print(len(ans))

for x, y in ans:

    print(f'{x} {y}')

Solution 2 (runtime: 1311 ms):
import math
mod=1000000007
mod1=998244353
inf=10**18+10

def swap(l1,x,y):
    a=l1[x]
    l1[x]=l1[y]
    l1[y]=a

t=1
for i in range(t):
    n=int(input())
    l1=list(map(int,input().split()))

    ind=[-1]*(n+1)
    for j in range(n):
        ind[l1[j]]=j

    l2=[j+1 for j in range(n)]
    st=set()

    ans=[]
    flag=1
    # print(l1)
    while flag:
        flag=0
        x=-1
        y=-1
        for j in range(n):
            for j1 in range(n-1):
                if l1[j1]<l1[j1+1]:
                    ans.append([l1[j1], l1[j1+1]])
                    swap(l1, j1, j1+1)
                if ind[l2[j1+1]]<ind[l2[j1]] and flag==0 and (l2[j1], l2[j1+1]) not in st:
                    # flag=1
                    x,y=j1, j1+1
                if ind[l2[j1+1]]>ind[l2[j1]]:
                    if (l2[j1], l2[j1+1]) not in st:
                        st.add((l2[j1+1],l2[j1]))
                        st.add((l2[j1],l2[j1+1]))
        # print(l1)
        for j in range(n):
            for j1 in range(n-1-j):
                if l1[j1]>l1[j1+1]:
                    ans.append([l1[j1], l1[j1+1]])
                    swap(l1, j1+1, j1)
        ans.reverse()
        if x!=-1:
            st.add((l2[y], l2[x]))
            # print(l2, ans)
            swap(l2, x, y)

    print(len(ans))
    for j in ans:
        print(*j)
    # print(ans)
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 2265 ms):
n=int(input())

l=list(map(int,input().split()))

liste=[]

if n==1:

    print(0)

else:

    lcurrent=[]

    lchange=[0]*n

    for i in range(n):

        lcurrent.append(i+1)

    for  i in range(n):

        j=n-1-i

        val=l[j]

        y=lcurrent.index(val)

        if y<j:

            lchange[j]=1

            for k in range(y+1,j+1):

                liste.append((lcurrent[k],val))

                lcurrent[k-1]=lcurrent[k]

            lcurrent[j]=val

        if y>j:

            lchange[j]=1

            for k in range(j,y):

                h=y-k

                liste.append((val,lcurrent[h]))

                lcurrent[h+1]=lcurrent[h]

            lcurrent[j]=val

    

    for  i in range(1,n):

     

        j=n-1-i+1

        

        if  lchange[j]==0:

            liste.append((lcurrent[j],lcurrent[j-1]))

            liste.append((lcurrent[j-1],lcurrent[j]))

    print(len(liste))

    for i in liste:

        a,b=i[0],i[1]

        print(a,b)

            

    

Solution 2 (runtime: 1093 ms):

n=int(input())
a=[int(i) for i in input().split()]
res=n*(n-1)//2
j=n
r=[]
for i in range(2,n+1):
    for j in range(1,i):
        r.append([i,j])
b=[None]*n
for i in range(n):
    b[a[i]-1]=i+1
c=[n-i for i in range(n)]

for i in range(n-1):
    for j in range(1,n-i):
        if b[c[j]-1]<b[c[j-1]-1]:
            res+=1
            r.append([c[j],c[j-1]])
            c[j],c[j-1]=c[j-1],c[j]
print(res)
for i in r:
    print(i[0],i[1])
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 2265 ms):
n=int(input())

l=list(map(int,input().split()))

liste=[]

if n==1:

    print(0)

else:

    lcurrent=[]

    lchange=[0]*n

    for i in range(n):

        lcurrent.append(i+1)

    for  i in range(n):

        j=n-1-i

        val=l[j]

        y=lcurrent.index(val)

        if y<j:

            lchange[j]=1

            for k in range(y+1,j+1):

                liste.append((lcurrent[k],val))

                lcurrent[k-1]=lcurrent[k]

            lcurrent[j]=val

        if y>j:

            lchange[j]=1

            for k in range(j,y):

                h=y-k

                liste.append((val,lcurrent[h]))

                lcurrent[h+1]=lcurrent[h]

            lcurrent[j]=val

    

    for  i in range(1,n):

     

        j=n-1-i+1

        

        if  lchange[j]==0:

            liste.append((lcurrent[j],lcurrent[j-1]))

            liste.append((lcurrent[j-1],lcurrent[j]))

    print(len(liste))

    for i in liste:

        a,b=i[0],i[1]

        print(a,b)

            

    

Solution 2 (runtime: 1311 ms):
import math
mod=1000000007
mod1=998244353
inf=10**18+10

def swap(l1,x,y):
    a=l1[x]
    l1[x]=l1[y]
    l1[y]=a

t=1
for i in range(t):
    n=int(input())
    l1=list(map(int,input().split()))

    ind=[-1]*(n+1)
    for j in range(n):
        ind[l1[j]]=j

    l2=[j+1 for j in range(n)]
    st=set()

    ans=[]
    flag=1
    # print(l1)
    while flag:
        flag=0
        x=-1
        y=-1
        for j in range(n):
            for j1 in range(n-1):
                if l1[j1]<l1[j1+1]:
                    ans.append([l1[j1], l1[j1+1]])
                    swap(l1, j1, j1+1)
                if ind[l2[j1+1]]<ind[l2[j1]] and flag==0 and (l2[j1], l2[j1+1]) not in st:
                    # flag=1
                    x,y=j1, j1+1
                if ind[l2[j1+1]]>ind[l2[j1]]:
                    if (l2[j1], l2[j1+1]) not in st:
                        st.add((l2[j1+1],l2[j1]))
                        st.add((l2[j1],l2[j1+1]))
        # print(l1)
        for j in range(n):
            for j1 in range(n-1-j):
                if l1[j1]>l1[j1+1]:
                    ans.append([l1[j1], l1[j1+1]])
                    swap(l1, j1+1, j1)
        ans.reverse()
        if x!=-1:
            st.add((l2[y], l2[x]))
            # print(l2, ans)
            swap(l2, x, y)

    print(len(ans))
    for j in ans:
        print(*j)
    # print(ans)
Problem: 2052A - Adrenaline Rush
Language: python

Solution 1 (runtime: 1093 ms):

n=int(input())
a=[int(i) for i in input().split()]
res=n*(n-1)//2
j=n
r=[]
for i in range(2,n+1):
    for j in range(1,i):
        r.append([i,j])
b=[None]*n
for i in range(n):
    b[a[i]-1]=i+1
c=[n-i for i in range(n)]

for i in range(n-1):
    for j in range(1,n-i):
        if b[c[j]-1]<b[c[j-1]-1]:
            res+=1
            r.append([c[j],c[j-1]])
            c[j],c[j-1]=c[j-1],c[j]
print(res)
for i in r:
    print(i[0],i[1])

Solution 2 (runtime: 1311 ms):
import math
mod=1000000007
mod1=998244353
inf=10**18+10

def swap(l1,x,y):
    a=l1[x]
    l1[x]=l1[y]
    l1[y]=a

t=1
for i in range(t):
    n=int(input())
    l1=list(map(int,input().split()))

    ind=[-1]*(n+1)
    for j in range(n):
        ind[l1[j]]=j

    l2=[j+1 for j in range(n)]
    st=set()

    ans=[]
    flag=1
    # print(l1)
    while flag:
        flag=0
        x=-1
        y=-1
        for j in range(n):
            for j1 in range(n-1):
                if l1[j1]<l1[j1+1]:
                    ans.append([l1[j1], l1[j1+1]])
                    swap(l1, j1, j1+1)
                if ind[l2[j1+1]]<ind[l2[j1]] and flag==0 and (l2[j1], l2[j1+1]) not in st:
                    # flag=1
                    x,y=j1, j1+1
                if ind[l2[j1+1]]>ind[l2[j1]]:
                    if (l2[j1], l2[j1+1]) not in st:
                        st.add((l2[j1+1],l2[j1]))
                        st.add((l2[j1],l2[j1+1]))
        # print(l1)
        for j in range(n):
            for j1 in range(n-1-j):
                if l1[j1]>l1[j1+1]:
                    ans.append([l1[j1], l1[j1+1]])
                    swap(l1, j1+1, j1)
        ans.reverse()
        if x!=-1:
            st.add((l2[y], l2[x]))
            # print(l2, ans)
            swap(l2, x, y)

    print(len(ans))
    for j in ans:
        print(*j)
    # print(ans)
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 217 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
 
    for key in sorted(freq, key = lambda x: freq[x]):
        if freq[key]<=k:
            k-=freq[key]
            del freq[key]
        else:
            break
    print(max(len(freq),1))
 
 
for _ in range(int(input())):
    solve()

Solution 2 (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)
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 217 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
 
    for key in sorted(freq, key = lambda x: freq[x]):
        if freq[key]<=k:
            k-=freq[key]
            del freq[key]
        else:
            break
    print(max(len(freq),1))
 
 
for _ in range(int(input())):
    solve()

Solution 2 (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))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 217 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
 
    for key in sorted(freq, key = lambda x: freq[x]):
        if freq[key]<=k:
            k-=freq[key]
            del freq[key]
        else:
            break
    print(max(len(freq),1))
 
 
for _ in range(int(input())):
    solve()

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: 217 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
 
    for key in sorted(freq, key = lambda x: freq[x]):
        if freq[key]<=k:
            k-=freq[key]
            del freq[key]
        else:
            break
    print(max(len(freq),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))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 217 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
 
    for key in sorted(freq, key = lambda x: freq[x]):
        if freq[key]<=k:
            k-=freq[key]
            del freq[key]
        else:
            break
    print(max(len(freq),1))
 
 
for _ in range(int(input())):
    solve()

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: 217 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
 
    for key in sorted(freq, key = lambda x: freq[x]):
        if freq[key]<=k:
            k-=freq[key]
            del freq[key]
        else:
            break
    print(max(len(freq),1))
 
 
for _ in range(int(input())):
    solve()

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: 217 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
 
    for key in sorted(freq, key = lambda x: freq[x]):
        if freq[key]<=k:
            k-=freq[key]
            del freq[key]
        else:
            break
    print(max(len(freq),1))
 
 
for _ in range(int(input())):
    solve()

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: 217 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
 
    for key in sorted(freq, key = lambda x: freq[x]):
        if freq[key]<=k:
            k-=freq[key]
            del freq[key]
        else:
            break
    print(max(len(freq),1))
 
 
for _ in range(int(input())):
    solve()

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: 217 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
 
    for key in sorted(freq, key = lambda x: freq[x]):
        if freq[key]<=k:
            k-=freq[key]
            del freq[key]
        else:
            break
    print(max(len(freq),1))
 
 
for _ in range(int(input())):
    solve()

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: 217 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
 
    for key in sorted(freq, key = lambda x: freq[x]):
        if freq[key]<=k:
            k-=freq[key]
            del freq[key]
        else:
            break
    print(max(len(freq),1))
 
 
for _ in range(int(input())):
    solve()

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: 217 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
 
    for key in sorted(freq, key = lambda x: freq[x]):
        if freq[key]<=k:
            k-=freq[key]
            del freq[key]
        else:
            break
    print(max(len(freq),1))
 
 
for _ in range(int(input())):
    solve()

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: 217 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
 
    for key in sorted(freq, key = lambda x: freq[x]):
        if freq[key]<=k:
            k-=freq[key]
            del freq[key]
        else:
            break
    print(max(len(freq),1))
 
 
for _ in range(int(input())):
    solve()

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: 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: 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))
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: 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()