Keep On Movin
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5744
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
Source
2016 Multi-University Training Contest 2
##题意: 给出n种字符的各自的数量,用它们构成多个回文串,求最短回文串的最大长度.
##题解: 首先,如果某个字符有奇数个,那么单出来的那个肯定要放在某个回文串的中心位置. 那么可以先找出最少有多少个这样的中心点. 对于剩下的字符(肯定是偶数个), 要把它们平均分配到这些中心点两边. 这里要注意:每个中心点被额外分配的字符个数一定是偶数.
##代码: ``` cpp #include