Making a String

链接: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 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.

题意:给最多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;
}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/veget_chicken/article/details/52353083
文章标签: 贪心
个人分类: 贪心
相关热词: 军刀making
上一篇A Problem about Polyline
下一篇敌兵布阵
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭