Codeforces 624B Making a String 【水题】
2016-02-13 18:19
387 查看
B. 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.
Sample test(s)
input
output
input
output
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个字母('a', 'a'+n)以及每个字母最多出现次数a[i],要求你组成一个最长的串使得串中任意两个字母出现次数不能相等。
AC代码:
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.
Sample test(s)
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个字母('a', 'a'+n)以及每个字母最多出现次数a[i],要求你组成一个最长的串使得串中任意两个字母出现次数不能相等。
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <string> #define INF 1000000 #define eps 1e-8 #define MAXN (10000+10) #define MAXM (2000000+10) #define Ri(a) scanf("%d", &a) #define Rl(a) scanf("%lld", &a) #define Rf(a) scanf("%lf", &a) #define Rs(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pf(a) printf("%.2lf\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while((a)--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define MOD 1000000007 #define LL long long #define lson o<<1, l, mid #define rson o<<1|1, mid+1, r #define ll o<<1 #define rr o<<1|1 #define PI acos(-1.0) #pragma comment(linker, "/STACK:102400000,102400000") #define fi first #define se second using namespace std; typedef pair<int, int> pii; map<int, bool> fp; int a[30]; int main() { int n; Ri(n); LL ans = 0; fp.clear(); for(int i = 1; i <= n; i++) Ri(a[i]); sort(a+1, a+n+1); for(int i = n; i >= 1; i--) { int temp = a[i]; while(temp > 0 && fp[temp]){ temp--; } if(temp > 0) { ans += 1LL*temp; fp[temp] = true; } } Pl(ans); return 0; }
相关文章推荐
- 141. Linked List Cycle LeetCode
- Codeforces 624A Save Luke 【数学】
- Ubuntu 安装 JAVA 8
- 137. Single Number II LeetCode
- Mysql的大字段问题
- CMD与AMD比较
- usaco 3.4 Electric Fence 2010.8.4
- Java日志框架
- 单片机keil C中的data、bdata、idata、xdata、hdata、pdata、code解释
- 1027. 打印沙漏
- 点击<li>标签触发<a>中的超链接
- 随机数生成之Math.Random()方法
- Python ftp上传文件
- Renascence架构原理——遗传规划算法
- spoj196 2010.8.2
- 回文字符
- 打印函数调用栈
- java&javaweb学习笔记(汇总)
- Bravo!Java学习笔记(1)---接口、抽象类、对象克隆、内部类
- 心經日語讀法