Keep On Movin
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1016 Accepted Submission(s): 716
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
若使分的没堆数量大一些,尽量要分的堆数少一些,最少堆数是由奇数个数决定的,并且每堆里最中间的肯定是这个奇数个的字符,我们从奇数个的字符里每个都拿出一个字符作为底,剩下的全都是偶数个了,然后添加在到那些底里,最后特殊考虑一下没有奇数的情况就好了
#include<stdio.h>
int main()
{
int t,n,ji,sum,i,a;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(ji=0,sum=0,i=0;i<n;i++)
{
scanf("%d",&a);
if(a&1)
{
ji++;
a--;
}
sum+=(a/2);
}
if(ji!=0)
{
sum/=ji;
sum<<=1;
sum++;
}
else
{
sum<<=1;
}
printf("%d\n",sum);
}
return 0;
}