2016HDU多校联赛-HDU-5744-Keep On Movin(水题)

来源:互联网 发布:手机淘宝怎么代购 编辑:程序博客网 时间:2024/06/07 23:16

Keep On Movin

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

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

题意:每个数字代表有多少个字符,而且每个数字代表的字符都是唯一的(不妨假设这些字符是a,b,c,d……)使这些字符排列成回文串,找到每一种组合中最短回文串的最大长度

思路:当全是偶数时,输出字符串总长度,当有奇数时,很明显这些奇数需要单独成串,然后剩下的偶数再平均分给这些串。

代码

#include<stdio.h>#include<iostream>#include<algorithm>#include<math.h>#include<string.h>#include<iomanip>using namespace std;int main(){    int T;    scanf("%d",&T);    while(T--)    {        int N;        scanf("%d",&N);        int group_num=0;//奇数数量=字符串数量        int str_num=0;//可分配字符数量        while(N--)        {            int flag;            scanf("%d",&flag);            if(flag%2==0)//如果是个偶数                str_num+=flag;            else            {                group_num++;                str_num+=flag-1;            }        }        if(group_num==0)        {            printf("%d\n",str_num);            continue;        }        if(str_num==0)        {            printf("1\n");            continue;        }        str_num=str_num/2;//可分配组数        int reuslt=str_num/group_num;        reuslt=reuslt*2+1;        printf("%d\n",reuslt);    }    return 0;}
0 0