codeforces 624B Making a String

Making a String
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:

  • the i-th letter occurs in the string no more than ai times;
  • the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
Input

The first line of the input contains a single integer n (2  ≤  n  ≤  26) — the number of letters in the alphabet.

The next line contains n integers ai (1 ≤ ai ≤ 109) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.

Output

Print a single integer — the maximum length of the string that meets all the requirements.

Examples
input
3
2 5 5
output
11
input
3
1 1 2
output
3
Note

For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc".

题意:你可以用n个字母来构成一个字符串,然后给你n个值ai代表分别对应的第i个字符使用次数不得超过ai且所有字母出现的次数都不得相同,求最长的字符串长度

题解:用一个数组记录使用过的字母出现的次数,重复的次数不可使用

#include<stdio.h>
#include<string.h>
#include<string>
#include<math.h>
#include<algorithm>
#define LL long long
#define PI atan(1.0)*4
#define DD double
#define MAX 100100
#define mod 100
#define dian 1.000000011
#define INF 0x3f3f3f
using namespace std;
LL s[MAX];
LL vis[30];//记录使用过的字母的使用次数 
bool cmp(int a,int b)
{
	return a>b;
}
int main()
{
    int n,j,i,t,k,l;
    LL m;
	while(scanf("%d",&n)!=EOF)
	{
		for(i=0;i<n;i++)
		    scanf("%lld",&s[i]);
		LL sum=0;k=0;
		for(i=0;i<n;i++)
		{
			m=s[i];
			sort(vis,vis+k,cmp);
			for(j=0;j<k;j++)
			{
				if(m==0)  
					break;
				if(m==vis[j])//判断当前字母可出现的次数是否被使用过 
					m--;
			}
			vis[k++]=m;//更新使用过的次数 
			sum+=m;
		}
		printf("%lld\n",sum);
	} 
	return 0;
}

  

你可能感兴趣的:(codeforces 624B Making a String)