Contest1444 - 2018年第三阶段个人训练赛第十场.Not so Diverse(STL)

问题 F: Not so Diverse

时间限制: 1 Sec  内存限制: 128 MB
提交: 391  解决: 205
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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 is given from Standard Input in the following format:
N K
A1 A2 ... AN

 

输出

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

 

样例输入

5 2
1 1 2 2 5

 

样例输出

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.

解题思路:题目过了之后发现如果用 STL 中的 #include<set> 和  #include<multiset> 应该会更简单些。

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=b;i<=a;i--)
#define maxn 200010

int main()
{
    int n,k;
    while(cin>>n>>k)
    {
        int a[maxn],b[maxn],ans=0,cnt=0;
        rep(i,0,n-1) cin>>a[i];
        sort(a,a+n);
        memset(b,0,sizeof(b));
        
        b[0]=1;
        rep(i,1,n-1)
        {
            if(a[i]!=a[i-1]) cnt++;
            b[cnt]++;
        }
        sort(b,b+cnt+1);
        int q=cnt+1;

        rep(i,0,cnt)
        {
            if(q<=k) break;
            else
                ans+=b[i],q--;
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值