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


P4447[AHOI2018初中组]分组 

#include
using namespace std;
map m;
int ans=101000;
void rule(int n)
{
	while(n>0)
	{
		int tj=0;
		map::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>n;
	for(int i=1;i<=n;i++)
	{
		int a;
		scanf("%d",&a);
		m[a]++;
	}
	rule(n);
	cout<