Keep On Movin
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 343 Accepted Submission(s): 248
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.
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
Author
zimpha
Source
2016 Multi-University Training Contest 2
给出n个数字,代表n种字符的数量,把这些字符全部使用,来组建成回文的字符串,可能会有很多种方式,问这些方式中,其中最短的回文字符串最长有多长
解释的有点绕口....
1,对于偶数个数的字符来说,只有每次组合都在两边同时添加某种字符,肯定还是回文的,但是奇数的不能加到别的字符旁边,所以把奇数单独拿出来讨论
2,奇数可以由偶数+1得到,所以把奇数全部变成1,其他的都是偶数个了,并且都是成对存在的
3,平均分配这些成对的字符串,计算其中最少的
原先自己只想到第1步,还想着用二分去做,后来才发现自己太局限了,思路没有打开,大水题都卡了半天
#include<cstdio> #include<cstring> using namespace std; int main() { int t; scanf("%d",&t); while(t--) { int n,cnt=0,sum=0; scanf("%d",&n); for(int i=0;i<n;++i) { int tp; scanf("%d",&tp); if(tp&1) { ++cnt; } sum+=tp/2*2; } int ans=sum; if(cnt) { ans=sum/2/cnt*2+1; } printf("%d\n",ans); } }