UPC 问题 F: Not so Diverse

问题 F: Not so Diverse
时间限制: 1 Sec 内存限制: 128 MB
提交: 401 解决: 209
[提交] [状态] [讨论版] [命题人: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.
这道题可以按照统计每种数字有几个小球,然后按照每种数字下小球数目由少到多的顺序排一下序,比较现有数字种类与k的关系,如果大于k,则更改k-cnt中数字就可以,所以就从前往后,更改的小球数最少。

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int x;
    int cnt;
}p[200005];
bool cmp(node p1,node p2)
{
    return p1.cnt<p2.cnt;
}
int main()
{
    int n,k;cin>>n>>k;
    int num[n],a[200005];
    int cnts=0,ans=0;
    memset(a,0,sizeof(a));
    for(int i=0;i<n;i++)
    {
        cin>>num[i];
        a[num[i]]++;
    }
    memset(p,0,sizeof(p));
    for(int i=0;i<=200000;i++)
    {
        if(a[i]!=0)
        {
            p[cnts].x=i;
            p[cnts].cnt=a[i];
            cnts++;
        }
    }
    sort(p,p+cnts,cmp);
    if(cnts<=k)
        cout<<"0"<<endl;
    else
    {
        int sum=0;
        for(int i=0;i<cnts-k;i++)
            sum+=p[i].cnt;
        cout<<sum<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/a17865569022/article/details/81568571
UPC
so
SO?