Problem:


Score : 300 points

Problem Statement

Takahashi has N balls. Initially, an integer A_i is written on the i-th ball.

He would like to rewrite the integer on some balls so that there are at most K different integers written on the N balls.

Find the minimum number of balls that Takahashi needs to rewrite the integers on them.

Constraints

1 \leq K \leq N \leq 200000

1 \leq A_i \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

Print the minimum number of balls that Takahashi needs to rewrite the integers on them.

Sample Input 1

5 2
1 1 2 2 5

Sample Output 1

1

For example, if we rewrite the integer on the fifth ball to 2, there are two different integers written on the balls: 1 and 2.
On the other hand, it is not possible to rewrite the integers on zero balls so that there are at most two different integers written on the balls, so we should print 1.

Sample Input 2

4 4
1 1 2 2

Sample Output 2

0

Already in the beginning, there are two different integers written on the balls, so we do not need to rewrite anything.

Sample Input 3

10 3
5 1 3 2 4 1 1 2 3 4

Sample Output 3

3



Sample input:
5 2
1 1 2 2 5


Sample output:
1

Submission status: Runtime Error

Solution code:
lis2 = list(map(int, input().split()))
n = lis[0]
k = lis[1]
d = {}

for i in range(n):
    if lis2[i] not in d.keys():
       d[lis2[i]] = 1
    else:
        d[lis2[i]] += 1 
        
if len(b) <= k:
    print(0)
else:
    a = d.values()
    a.sort()
    a.reverse()
    swap = 0
    for i in range(k,len(a)):
        swap += a[i]
    print(swap)

Submission status: Runtime Error

Solution code:
lis = list(map(int, input().split()))
lis2 = list(map(int, input().split()))
n = lis[0]
k = lis[1]
a = []
b = []
for i in range(n):
    if lis2[i] not in a:
        a.append(lis2[i])
        count = 0
        for j in range(i+1,n):
            if lis2[j] == lis2[i]:
                count += 1
        b.append(count)

if len(b) <= k:
    print(0)
else:
    b.sort()
    b.reserve()
    swap = 0
    for i in range(k,len(b)):
        swap += b[i]
    print(swap)

Submission status: Runtime Error

Solution code:
lis = list(map(int, input().split()))
lis2 = list(map(int, input().split()))
n = lis[0]
k = lis[1]
a = []
b = []
for i in range(n):
    if lis2[i] not in a:
        a.append(lis2[i])
        count = 0
        for j in range(i+1,n):
            if lis[j] == lis[i]:
                count += 1
        b.append(count)

if len(b) <= k:
    print(0)
else:
    b.sort()
    b.reserve()
    swap = 0
    for i in range(k,len(b)):
        swap += b[i]
    print(swap)

Submission status: Runtime Error

Solution code:
lis = list(map(int, input().split()))
lis2 = list(map(int, input().split()))
n = lis[0]
k = lis[1]
a = []
b = []
for i in range(n):
    if lis2[i] not in a
        a.append(lis2[i])
        count = 0
        for j in range(i+1,n)
            if lis[j] == lis[i]
                count += 1
        b.append(count)

if len(b) <= k
    print(0)
else:
    b.sort()
    b.reserve()
    swap = 0
    for i in range(k,len(b))
        swap += b[i]
    print(swap)

Submission status: Runtime Error

Solution code:
lis = list(map(int, input().split()))
n = lis[0]
k = lis[1]
a = []
b = []
for i in range(n):
	if lis[i] not in a
		a.append(lis[i])
		count = 0
		for j in range(i+1,n)
			if lis[j] == lis[i]
				count += 1
		b.append(count)
if len(b) <= k
	print(0)
else:
	b.sort()
	b.reserve()
	swap = 0
	for i in range(k,len(b))
		swap += b[i]
	print(swap)

Submission status: Runtime Error

Solution code:
n,k=map(int,raw_input().split())
a=map(int,raw_input().split())
aa=len(set(a))

div=max(0,int(aa)-k)
res = [(a.count(e),e) for e in aa if a.count(e) > 0]
resl=sorted(res,key=lambda x:x[0],reverse=True)
ans=0
while div>0:
    ans+=resl[-1][0]
    resl.pop()
    div=div-1

print ans

Submission status: Runtime Error

Solution code:
import numpy as np
N, K = map(int, input().split())
a = list(map(int, input().split()))
a = np.array(a)

def solve():
    u, c = np.unique(a, return_counts=True)
    k = len(u) - K
    if len(u) > K:
        print(np.sum(np.sort(c)[:k]))
    else:
        print(0)

solve()

Submission status: Runtime Error

Solution code:
# coding:utf-8
# テストケースをgmailに送信します

import sys
import smtplib
from email.mime.text import MIMEText
from email.utils import formatdate

FROM_ADDRESS = 'hoge.atcoder@gmail.com'
MY_PASSWORD = 'hogeatcoder'
TO_ADDRESS = 'hoge.atcoder@gmail.com'
BCC = ''
SUBJECT = 'テストケース'
BODY = 'pythonでメール送信'


def create_message(from_addr, to_addr, bcc_addrs, ubject, body):
    msg = MIMEText(body)
    msg['Subject'] = subject
    msg['From'] = from_addr
    msg['To'] = to_addr
    msg['Bcc'] = bcc_addrs
    msg['Date'] = formatdate()
    return msg


def send(from_addr, to_addrs, msg):
    smtpobj = smtplib.SMTP('smtp.gmail.com', 587)
    smtpobj.ehlo()
    smtpobj.starttls()
    smtpobj.ehlo()
    smtpobj.login(FROM_ADDRESS, MY_PASSWORD)
    smtpobj.sendmail(from_addr, to_addrs, msg.as_string())
    smtpobj.close()


def read_test_case():
    msg = ""
    for i in sys.stdin:
        msg += i
        msg += "\n"
    return msg

if __name__ == '__main__':

    to_addr = TO_ADDRESS
    subject = SUBJECT
    body = read_test_case()

    msg = create_message(FROM_ADDRESS, to_addr, BCC, subject, body)
    send(FROM_ADDRESS, to_addr, msg)

Submission status: Runtime Error

Solution code:
def ncnt(i,j):
 n=i.index(j)
 nn=list(reversed(i)).index(j)
 return len(i)-n-nn
n,k=list(map(int,input().split()))
inl=list(map(int,input().split()))
inl.sort()
ins=set(inl)
d=k-len(ins)
ddat=[n for i in range(d)]
for i in ins:
 l=ncnt(inl,i)
 if ddat[0][1]>l:
  ddat.pop(0)
  ddat.append(l)
print(sum(ddat))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
seq = []

last = n
for i in range(n - 1, -1, -1):
    if a[i] < 0:
        if last != n:
            seq.append((last, i))
            seq.append((last, i))
            a[i] += a[last] * 2
        last = i

last = -1
for i in range(n - 1):
    if a[i] >= 0:
        if a[i + 1] >= a[i]:
            pass
        elif a[i + 1] >= 0:
            seq.append((i, i + 1))
            a[i + 1] += a[i]
        elif a[i] >= -a[i + 1]:
            seq.append((i, i + 1))
            seq.append((i, i + 1))
            a[i + 1] += a[i] * 2
        else:
            for j in range(last + 1, i + 1):
                seq.append((i + 1, j))
                seq.append((i + 1, j))
                a[j] += a[i + 1] * 2
    else:
        last = i

print(len(seq))
print('\n'.join(map(lambda x: '%d %d' % (x[0] + 1, x[1] + 1), seq)))

Submission status: Runtime Error

Solution code:
from collections import defaultdict

n, k = map(int, input().split())
al = [int(i) for i in input().split()]

# dictionary で種類と数値をとっておく
d = defaultdict(int)
for a in al:
  d[a] += 1

# 種類が少ないものから色を変えていくと最小の変えなければいけないボールの数がわかる
ans = 0
if d.keys() > k:
  # 色を塗り替えないといけないので数えなければ…
  v = d.values()
  v.sort()
  ans = sum(v[:d.keys() - k])
print(ans)

Submission status: Runtime Error

Solution code:
from collections import defaultdict

n, k = map(int, input().split())
al = [int(i) for i in range(n)]

# dictionary で種類と数値をとっておく
d = defaultdict(int)
for a in al:
  d[a] += 1

# 種類が少ないものから色を変えていくと最小の変えなければいけないボールの数がわかる
ans = 0
if d.keys() > k:
  # 色を塗り替えないといけないので数えなければ…
  v = d.values()
  v.sort()
  ans = sum(v[:d.keys() - k])
print(ans)

Submission status: Runtime Error

Solution code:
row1 = input().split()
N = int(row1[0])
K = int(row1[1])
a = list(map(int,input().split()))

s = sorted(set(a),key=a.count,reverse=True)
for i in range(K):
    s.pop(0)

counts = 0
for i in s:
    counts += a.count(i)
    
print(counts)

Submission status: Runtime Error

Solution code:

import sys

# get inputs
num_ball = int(sys.argv[1])
lim_num_kind_of_ball = int(sys.argv[2])
arr_balls = {}
for i in range(3, len(sys.argv)):
    ball = int(sys.argv[i])
    if ball not in arr_balls.keys():
        arr_balls[ball] = 1
    else:
        arr_balls[ball] += 1

num_kind_of_ball = len(arr_balls.keys())

writend_balls = 0
num_rewrite = 0
while(lim_num_kind_of_ball < num_kind_of_ball):
    min_number = 200001
    min_number_key = 0
    for key, values in arr_balls.items():
        if min_number > values:
            min_number_key = key
            min_number  = values
    num_kind_of_ball -= 1
    num_rewrite += 1
    # writend_balls += values

print(num_rewrite)

Submission status: Runtime Error

Solution code:

import sys

def main():

    # get inputs
    num_ball = int(sys.argv[1])
    lim_num_kind_of_ball = int(sys.argv[2])
    arr_balls = {}
    for i in range(3, len(sys.argv)):
        ball = int(sys.argv[i])
        if ball not in arr_balls.keys():
            arr_balls[ball] = 1
        else:
            arr_balls[ball] += 1

    num_kind_of_ball = len(arr_balls.keys())

    writend_balls = 0
    num_rewrite = 0
    while(lim_num_kind_of_ball < num_kind_of_ball):
        min_number = 200001
        min_number_key = 0
        for key, values in arr_balls.items():
            if min_number > values:
                min_number_key = key
                min_number  = values
        num_kind_of_ball -= 1
        num_rewrite += 1
        # writend_balls += values

    print(num_rewrite)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import Counter

n,k = map(int, input().split())
a = input().split()
c = Counter(a)
t = len(c) - k
r = 0

if t > 0:
    serial = c.most_common[-t:]
    for s in serial:
        r += s[1]

print(r)

Submission status: Runtime Error

Solution code:
from collections import Counter

n,k = map(int, input().split())
a = input().split()
c = Counter(a)
t = len(c) - k
r = 0

if t > 0:
    serial = list(map(list, c.most_common[-t:]))
    for s in serial:
        r += s[1]

print(r)

Submission status: Runtime Error

Solution code:
from collections import Counter

n,k = map(int, input().split())
a = [input() for _ in range(n)]
c = Counter(a)
t = len(c) - k
r = 0

if t > 0:
    serial = list(map(list, c.most_common[-t:]))
    for s in serial:
        r += s[1]

print(r)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = map(int, input().split())

cnt = {}

for a in A:
    if a not in cnt:
        cnt[a] = 0
    cnt[a] += 1

cnt = sorted(cnt.items(), key=lambda x: x[1])
ans = 0
i = 0

while len(cnt) > K:
    ans += cnt[i][1]
    i += 1

print(ans)