AIM Tech Round (Div. 2) B. Making a String 贪心
2016-02-05 13:48
471 查看
B. Making a String
题目连接:
http://codeforces.com/contest/624/problem/BDescription
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.Sample Input
32 5 5
Sample Output
11Hint
题意
有n个字母,每个字母最多出现ai次,但是每个字母的出现次数都要求不一样问你这个字符串最长多长
题解:
显然从大到小排序,然后贪心就好了,能选就选,不能选就--就行了注意小心被减到负数了
代码
#include<bits/stdc++.h> using namespace std; int a[30]; map<int,int> H; int main() { int n;scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); sort(a+1,a+1+n); int flag = 0; long long ans = 0; for(int i=n;i>=1;i--) { int tmp = a[i]; while(H[tmp])tmp--; if(tmp) { H[tmp]=1; ans+=tmp; } } cout<<ans<<endl; }
相关文章推荐
- AIM Tech Round (Div. 2)(B)模拟
- AIM Tech Round (Div. 2) A. Save Luke 水题
- AIM Tech Round (Div. 2)(A)数学
- AIM Tech Round (Div. 2)-B. Making a String(贪心)
- AIM Tech Round (Div. 2)-A. Save Luke(数学题)
- 人工智能、应用、程序
- 【slighttpd】基于lighttpd架构的Server项目实战(5)—TCP的TIME_WAIT状态
- ubuntu sendmail
- AIM Tech Round (Div. 2)
- Codeforces AIM Tech Round (Div. 2) B. Making a String
- Codeforces AIM Tech Round (Div. 2) A. Save Luke
- 行链接(Row chaining) 与行迁移(Row Migration)
- zabbix监控 不同版本安装和raid监控,mysql主从监控
- 论文《FDDB: A Benchmark for Face Detection in Unconstrained Settings》导读
- My GoogleMail c9
- AIM Tech Round (Div. 2)--A. Save Luke
- AIM Tech Round (Div. 2)--B. Making a String
- AIM Tech Round (Div. 2) C. Graph and String(二分图染色 | 贪心)
- 集训队专题(3)1010 Here We Go(relians) Again
- Rails UVA 514(栈模拟,水题)