Problem Description
Takahashi has N balls. Initially, an integer Ai 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≤K≤N≤200000
- 1≤Ai≤N
- All input values are integers.
Input
Input is given from Standard Input in the following format:
N K
A1 A2 ... ANOutput
Print the minimum number of balls that Takahashi needs to rewrite the integers on them.
Example
Sample Input 1
5 2
1 1 2 2 5Sample 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 2Sample 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 4Sample Output 3
3
题意:有 n 个球,每个球上有一个序号 a[i],现在要在球上重写序号,要求重写后,球上的新序号不同的个数不能大于 k,求最小的要重写的球的个数
思路:使用桶排简单统计即可
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 200000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
int a[N];
int bucket[N];
bool cmp(int a,int b){
return a>b;
}
int main(){
int n,k;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
bucket[a[i]]++;
}
int cnt=0;
for(int i=1;i<=n;i++){
if(bucket[i]!=0){
a[++cnt]=bucket[i];
}
}
sort(a+1,a+1+cnt,cmp);
int res=0;
for(int i=k+1;i<=cnt;i++)
res+=a[i];
printf("%d\n",res);
return 0;
}