您的位置:首页 > 其它

hdu 5744 Keep On Movin

2016-07-21 22:26 281 查看
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
如果每个字符出现次数都是偶数, 那么答案显然就是所有数的和. 对于奇数部分, 显然需要把其他字符均匀分配给这写奇数字符. 随便计算下就好了.
如果都是奇数,那结果直接出来了,就是该字符串长度;
否则,把偶数一次分给个个奇数,分成一次,长度加一次,分不成时输出当前长度就行了;
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
int a[100005];
int main(){
int T;
cin>>T;
while(T--){
memset(a,0,sizeof(a));
int n,ans=0,sum=0;
cin>>n;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]%2!=0)ans++;
sum+=a[i];
}
if(ans==0) ans=1;
if(ans==1)
cout<<sum<<endl;
else{
int t=1;
sum-=ans;
while(sum-ans*2>=0){
sum-=ans*2;
t+=2;
}
cout<<t<<endl;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: