HDU5744 Keep On Movin(贪心)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5744
Time Limit:2000MS Memory Limit:65536KB Description Professor Zhang has kinds of characters and the quantity of the i -th character is ai. Professor Zhang wants to use all the characters build several palindromic strings. He also wants to maximize the length of the shortest palindromic string.
For example, there are 4 kinds of characters denoted as ‘a’, ‘b’, ‘c’, ‘d’ and the quantity of each character is {2,3,2,2} . Professor Zhang can build {“acdbbbdca”}, {“abbba”, “cddc”}, {“aca”, “bbb”, “dcd”}, or {“acdbdca”, “bb”}. The first is the optimal solution where the length of the shortest palindromic string is 9.
Note that a string is called palindromic if it can be read the same way in either direction.
Input There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤105) – the number of kinds of characters. The second line contains n integers a1,a2,...,an (0≤ai≤104) .
Output For each test case, output an integer denoting the answer.
Sample Input 4 4 1 1 2 4 3 2 2 2 5 1 1 1 1 1 5 1 1 2 2 3
Sample Output 3 6 1 3
告诉你有n个字符,然后给出你每个字符的数量,你来用他们组成若干字符串,保证每个字符串都是回文串。问你如何组字符串,可以使他们中最短的那串的长度最长。
对于数量为1的字符,我们可以用2个相同字符来“嵌套”它。如{a,b和b}可以形成bab。故每2个相同字符可以形成一个“嵌套”。而大于1的奇数则可以转化为1个字符和若干对嵌套层。 故问题转化为计算嵌套层和单独字符数量的问题。见代码