Codeforces 624
2016-05-05 13:07
309 查看
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.
Examples
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".
题目的意思就是,给定你每个字母可以出现的次数,然后根据题目中的两个条件,计算最后字符串可能的最大长度。
条件一,每个字母最多出现给定的 ai 次;条件二,不能有两个字母出现的次数相同。ok,我用了一个set。
View Code
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".
题目的意思就是,给定你每个字母可以出现的次数,然后根据题目中的两个条件,计算最后字符串可能的最大长度。
条件一,每个字母最多出现给定的 ai 次;条件二,不能有两个字母出现的次数相同。ok,我用了一个set。
package Sunny; import java.io.InputStreamReader; import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; import java.util.Scanner; /** * Created by lenovo on 2016-05-05. */ public class CF624B { public static void main(String[] args){ Scanner scanner = new Scanner(new InputStreamReader(System.in)); int n = scanner.nextInt(); long[] arr = new long[26]; for(int i = 0; i < n; ++i){ arr[i] = scanner.nextLong(); } Arrays.sort(arr); long num = 0; long ans = 0; HashSet<Long> set = new HashSet<Long>(); for(int i = 25; i >= 0; --i){ while(set.contains(arr[i])){ arr[i]--; } set.add(arr[i]); } long tmp; Iterator<Long> iterator = set.iterator(); while(iterator.hasNext()){ tmp = iterator.next(); if(tmp > 0) ans += tmp; } System.out.println(ans); } }
View Code
相关文章推荐
- 普及下病毒性感冒和细菌性感冒的区别(如何看血常规)
- 线性回归
- 如何使用csapp文件
- 【西西学FPGA】Lesson9 Uart
- android 布局
- Ubuntu Web服务器的搭建
- eclipse怎样修改默认设置java类文件import部分不隐藏于加号当中?
- hdu3434 Sequence Adjustment
- 《java入门第一季》之面向对象面试题(继承中构造方法的关系)
- Connection类 java
- 《java入门第一季》之面向对象面试题(继承中构造方法的关系)
- 【西西学FPGA】Lesson8
- 【西西学FPGA】lesson7
- iOS开发Xcode使用github托管代码
- 给各小组的的一些评价及小建议
- Angular和jQuery之间的区别
- eclipse中的SVN插件的导入和连接
- 泊车
- JNI调用错误: No implementation found for native
- R %operator% 含义