P4447[AHOI2018初中组]分组 贪心+map
#include<bits/stdc++.h>
using namespace std;
map<int,int> m;
int ans=101000;
void rule(int n)
{
while(n>0)
{
int tj=0;
map<int,int>::iterator it,it2;
it=m.begin();
while(it->second==0&&it!=m.end()) it++;
it2=it;
if(it2!=m.end()) it2++;
for(;;)
{
if((it2->first)==(it->first+1)&&(it2->second)>=(it->second))
{
tj++;
it->second--;
if(it!=m.end()) it++;
if(it2!=m.end()) it2++;
}
else
{
it->second--;
break;
}
}
if(tj>0) tj++;
n-=tj;
if(tj<ans) ans=tj;
}
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
int a;
scanf("%d",&a);
m[a]++;
}
rule(n);
cout<<ans;
}
浙公网安备 33010602011771号