HDOJ 5744 Keep On Movin(回文串)

来源:互联网 发布:淘宝男士护肤 编辑:程序博客网 时间:2024/06/08 05:22

Keep On Movin

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


Problem Description
Professor Zhang has kinds of characters and the quantity of thei-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
441 1 2 432 2 251 1 1 1 151 1 2 2 3
 

Sample Output
3613
 

Author
zimpha
 

Source
2016 Multi-University Training Contest 2 



思路:
题意就是说,给定你几个字母,让你看看他们能不能构成n个回文串,并且找每个回文串组中的长度最小值,然后再在这些最小值中找最大值。这就是对字母个数分组,如果字母都是偶数个,那很好,能平分成两个长度等长的。如果有字母的个数为奇数,那么先把这个1拿出来,剩下的还是能均等分成两份,再把这个1加到其中一个的中间位置。如果有n个奇数,那么先把n个1先拿出来,再剩下的均分成n份,再加进去1。

代码:
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int N=100010;int a[N],b[N];int main(){    int T,n;    scanf("%d",&T);    while (T--)    {        scanf("%d",&n);        int x,odd=0,sum=0;        for (int i=1;i<=n;i++)        {            scanf("%d",&x);            if (x&1) odd++,sum+=x-1;            else sum+=x;        }        if (odd==0) printf("%d\n",sum);        else printf("%d\n",sum/odd/2*2+1);    }    return 0;}




0 0
原创粉丝点击