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): 76    Accepted Submission(s): 68


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
 
Author
zimpha
 
Source
 
 
 
解析:如果每个字符出现的次数都是偶数,结果显然为各字符出现的次数之和。如果含有奇数,则奇数1+2k(k>=0)可分为1和2k,2k和其他偶数相加得到总和,再把得到的和平均分给奇数字符即可。
 
 
 
#include <cstdio>

int main()
{
    int T, n;
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        int odd = 0;    //记录奇数字符有多少个
        int sum = 0;    //记录总和
        int num;
        while(n--){
            scanf("%d", &num);
            if(num&1){
                ++odd;
                sum += num-1;
            }
            else{
                sum += num;
            }
        }
        if(odd == 0){   //只有偶数字符
            printf("%d
", sum);
        }
        else{
            sum >>= 1;  //得到能够分配的对数
            printf("%d
", sum/odd*2+1);
        }
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/inmoonlight/p/5692896.html