hdu 5744 Keep On Movin

Keep On Movin

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 265    Accepted Submission(s): 197


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 (1n105) -- the number of kinds of characters. The second line contains n integers a1,a2,...,an (0ai104).
 
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
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5746 5745 5743 5742 5741 
 
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int main(){
    int n;
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        int sum=0;
        int jishu=0;
        for(int i=0;i<n;i++){

            int tmp=0;
            scanf("%d",&tmp);
            sum+=tmp;
            if(tmp%2==1){
                sum--;
                jishu++;
            }
        }
        if(jishu==0){
            printf("%d
",sum);
        }else{
            printf("%d
",sum/2/jishu*2+1);
        }
    }
    return 0;
}
View Code

每一个奇数个数到字符必定拿出一个字符放在回文串的中间,剩余到偶数个字符先均分为两部分,即回文串的前一部分和后一部分。

然后尽量均分成奇数个字符类的数,得到到结果即是回文串的前部分,*2+1可得到回文串到长度。

如果全都是偶数个,则可以直接组成一个大到回文串。

 
 
原文地址:https://www.cnblogs.com/superxuezhazha/p/5694099.html