Making a String

来源:互联网 发布:上海人工智能公司排名 编辑:程序博客网 时间:2024/05/18 01:54
链接:http://codeforces.com/problemset/problem/624/B

题目:

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 thanai 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.

题意:给最多26个字母的出现次序,要求出现次数不同,且每个字母出现的次数不超过所给的次数的最大总数。

分析:贪心,先排个序,从最大的开始考虑,如果相同依次递减。

题解:
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <vector>#include <map>#include <string>#include <cstring>#include <functional>#include <cmath>#include <cctype>#include <cfloat>#include <climits>#include <complex>#include <deque>#include <list>#include <set>#include <utility>using namespace std;int n;int a[27];int main(){//freopen("in.txt","r",stdin);//while(true){cin>>n;for(int i=1;i<=n;i++){cin>>a[i];}sort(a+1,a+n+1);__int64 sum=a[n];for(int i=n-1;i>0;i--){if(a[i]>=a[i+1]&&a[i+1]!=0)a[i]=a[i+1]-1;if(a[i+1]==0)a[i]=0;sum+=(__int64)a[i];}cout<<sum<<endl;//}return 0;}

0 0
原创粉丝点击