Not so Diverse(AtCoder-3719)

本文探讨了在有限重写次数下,使球上序号种类不超过特定数量的算法问题。通过桶排序统计不同序号出现频率,实现最小化重写次数的目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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≤KN≤200000
  • 1≤AiN
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
A1 A2 ... AN

Output

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 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

题意:有 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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值