P4447[AHOI2018初中组]分组 贪心+map

P4447[AHOI2018初中组]分组 

 

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

  

 

posted @ 2022-03-19 19:38  心悟&&星际  阅读(51)  评论(0编辑  收藏  举报