Keep On Movin
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 275 Accepted Submission(s): 204
Problem 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.
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) .
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
Author
zimpha
题意:给出每种字符的个数,组成回文串,找出所有回文串中最小的,要尽量是它最大。
水题,注意理解题意。没有奇数的字母或者一个奇数的字母则直接连成一个回文串,直接输出所有字母的个数。若出现多(a)个奇数的字母,则分成a个回文串,平分剩下的偶数字母,输出此时最短的回文串数目。
附上代码:
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int main() 5 { 6 int T,i,j,n,m; 7 scanf("%d",&T); 8 while(T--) 9 { 10 int a=0,b=0,s=0; 11 scanf("%d",&n); 12 for(i=0;i<n;i++) 13 { 14 scanf("%d",&m); 15 if(m%2) a++; ///记录奇数出现的个数 16 s+=m; ///字母的总个数 17 } 18 if(!a||a==1) ///如果只有一组奇数字母组或者没有奇数的字母组,则可以拼成一个回文串 19 { 20 printf("%d\n",s); 21 continue; 22 } 23 b=(s-a)/a/2*2+1; ///出现多组奇数字母组的情况 24 printf("%d\n",b); 25 } 26 return 0; 27 }