hdu5744——Keep On Movin(构造回文)

来源:互联网 发布:python大数据 编辑:程序博客网 时间:2024/06/07 02:43

Problem Description
Professor Zhang has kinds of characters and the quantity of the -th character is . 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 . 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 – the number of kinds of characters. The second line contains integers .

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

真是菜的不行,这种题都想了半天。反正就是把偶数个数的字符平均分到奇数(由于奇数能转化为1+偶数,所以在中间的基本上可以看做一个奇数)个字符的两边,C++除法的特性能够得出长度最小的那个

#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <cstdio>#include <map>#include <cmath>#include <algorithm>#define INF 0x3f3f3f3f#define MAXN 1010#define mod 1000000007using namespace std;int main(){    int t,n;    scanf("%d",&t);    while(t--)    {        int odd=0,even=0,m;        scanf("%d",&n);        for(int i=0;i<n;++i)        {            scanf("%d",&m);            if(m%2==0)                even+=m/2;            else            {                odd++;                even+=(m-1)/2;            }        }        if(odd==0)            printf("%d\n",even*2);        else        {            if(odd>even)                printf("1\n");            else            {                int ans=even/odd;                printf("%d\n",ans*2+1);            }        }    }    return 0;}
0 0