HDU 5744 Keep On Movin(思维+贪心)

来源:互联网 发布:virtualbox安装ubuntu 编辑:程序博客网 时间:2024/06/06 03:49


Keep On Movin

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


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
441 1 2 432 2 251 1 1 1 151 1 2 2 3
 

Sample Output
3613
 

Author
zimpha

题意:给出每种字符的个数,组成回文串,找出所有回文串中最小的,要尽量是它最大。

思路:加入只有一个奇数串,或者没有奇数串,肯定可以拼成一个回文串。。。如果有n个奇数串,可以把那些奇数串分成1+偶数个字符串两部分,然后最开始只有n个字符,剩下的肯定都是偶数个,所以n个字符每个字符都两边放上一对字符。。。这样肯定就是最短的最大了。。一开始想不到。。。唉。。。还是菜。。。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;int main(){    int t, n;    scanf("%d", &t);    while(t--)    {        scanf("%d", &n);        int x, cnt = 0, sum = 0;        for(int i = 1; i <= n; i++)        {            scanf("%d", &x);            if(x%2) cnt++;  //记录奇数的个数            sum += x;  //        }        if(cnt == 0 || cnt == 1)        {            printf("%d\n", sum);            continue;        }        sum -= cnt;        int a = sum / 2;        int ans = a/cnt*2 + 1;        printf("%d\n", ans);    }    return 0;}


0 0
原创粉丝点击