题解:CF2057B Gorilla and the Exam
zhangxiaohan007 · · 题解
这道题他给你写了很长一段,实际上就是求改
下面给出代码理解:
#include<bits/stdc++.h>
#define ll long long
#define db double
#define inf 2e9
using namespace std;
int n,k,a[114514],tot,s[114514];
map<int,int>mp;//用map离散化
void solve()
{
mp.clear();
tot=0;
cin>>n>>k;
for(int i=0;i<=n;i++)
{
s[i]=0;
}//多测不清空,爆零两行泪
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(mp.count(a[i])==0)mp[a[i]]=++tot;
a[i]=mp[a[i]];
s[a[i]]++;//桶,存每种数的数量
}
sort(s+1,s+n+1);//按数量从小到大排序
int i=1,ans=0;
while(s[i]==0)i++;//除去没有的那些数
for(i;i<n;i++)
{
if(k>=s[i])//如果还能换
{
ans++;
k-=s[i];
//把这种数给变成数量最多的那种数(最后那种数是不被
//遍历的,因为至少要删一次,所以最后的数不用加桶)
}
}
cout<<tot-ans<<"\n";//总的数的种数数量减去变掉的
}
int main()
{
int t;
cin>>t;
while(t--)//多测
{
solve();
}
return 0;
}