HDU - 5744 Keep On Movin

题目:

Professor Zhang has kinds of characters and the quantity of the ii-th character is aiai. 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}{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. 

InputThere 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(1n105)(1≤n≤105) -- the number of kinds of characters. The second line contains nn integers a1,a2,...,ana1,a2,...,an (0ai104)(0≤ai≤104).OutputFor 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

题意:

有n种字符,给出每个字符个数,要求组成回文串,求组成的回文串的最长的长度。

做法:

贪心,全是偶数个,奇数个没有或者只有一个时,答案为全部的和。

否则将偶数类的字符一对一对地分给落单地字符,就是最长,详情见代码。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL a[320000];
int n; 
void deal(){
	scanf("%d",&n);
	LL sum=0,j=0;
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		sum+=a[i];
		if(a[i]%2) j+=1;
	} 
	if(j==0||j==1){
		printf("%lld\n",sum);
	}
	else {
		printf("%lld\n",(sum-j)/j/2*2+1);
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
		deal();
    return 0;
}

  

posted @ 2017-11-15 16:43  晓风微微  阅读(173)  评论(0编辑  收藏  举报