cf#AIM Tech Round -B. Making a String-贪心/set

n个字母,每个最多出现A[I]次,每个字母出现次数必须是唯一的,问这个字符串最长多长


set判重。排个序,显然从最大的开始选,如果不能选,则减减后再判断,直到为零

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;
#define ptf(ar1,ar2)  pr__int64f("%I64d:%I64d\n",ar1,ar2);
typedef __int64 ll;
const ll maxn = 131707+500; 

__int64 tm[56];
int main()
{
	
	int i,n; 
	
	cin>>n;
	for (i=1;i<=n;i++)
		scanf("%I64d",&tm[i]);
	sort(tm+1,tm+1+n);
	set<__int64 > sb;
	__int64 sum=0;
	for (i=n;i>=1;i--)
	{
		__int64 tmp=tm[i];
		while(tmp&&sb.find(tmp)!=sb.end())
			tmp--;
		if (tmp<=0) continue;
		
		if (sb.find(tmp)==sb.end())
		{
			sum+=tmp;
			sb.insert(tmp);
		}
	}
	
	printf("%I64d\n",sum );
}


你可能感兴趣的:(cf#AIM Tech Round -B. Making a String-贪心/set)