Keep On Movin
HDU - 5744Professor 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.
Note that a string is called palindromic if it can be read the same way in either direction.
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)
4
4
1 1 2 4
3
2 2 2
5
1 1 1 1 1
5
1 1 2 2 3
3
6
1
3
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int t;
int sum,odd,n,k;
int main(){
int i;
scanf("%d",&t);
while(t--){
sum = 0;
odd = 0;
scanf("%d",&n);
for(i = 1; i <= n; i++){
scanf("%d",&k);
if(k%2==0){
sum += k;
}
else{
sum += k-1;
odd++;
}
}
if(odd==0)printf("%d\n",sum);
else printf("%d\n",sum/2/odd*2+1);//这是最关键的一句,sum是总的偶数的个数sum/2就是能分成多少组去填充奇数字符串,每一个实际上代表左右两边整体
//sum/2/odd代表这用这些组去填充这些个奇数串每个奇数串可以得到多少组去填充,sum/2/odd*2就像刚才说的sum/2的时候实际上是一个整体,代表了两边,所以实际
//长度要乘2才是填充上的长度,最后加1就是加的原本字符串的那一个字符,最后就是总的长度
}
return 0;
}