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

猜你喜欢

转载自blog.csdn.net/XxxxxM1/article/details/81564770