Keep On Movin

来源:互联网 发布:java开源网盘系统 编辑:程序博客网 时间:2024/06/05 21:00
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


题意说的可能比较模糊,所以我尽量再来解释一遍。说的就是“He also wants to maximize the length of the shortest palindromic string.”,他不是要改变这个最短的长度,他只是想要找到所有的分组情况中,最短的那个字符串,然后再从这些短的字符串中找到最长的长度。所以我们能直接加到最长最好,只有一个串,自然就是最长。可是如果有两个串的话,我们怎么办呢? 比如 1 1 2 2,我们必须创建两个串,这样才能用掉所有的字符。也就是以奇数在中间。我们要最长,所以我们应该尽量的使所有的串最长,这样的话,才能使该组中最短的最长,剩下的就是除了奇数,奇数我们变为1+偶数,这样的话,我们就可以用多余的字符,再来平均。

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int maxn=1e5+10;int val[maxn];int main(){    int T;    scanf("%d",&T);    while(T--)    {        int n;        scanf("%d",&n);        int odd_num=0;        int sum_even=0;        for(int i=1;i<=n;i++)        {              scanf("%d",&val[i]);              int temp=val[i];              if(temp%2)              {                  odd_num++;                  sum_even += (temp-1);                  continue;              }              sum_even += temp;        }        if(!odd_num)        {            printf("%d\n",sum_even);            continue;        }        while((sum_even/odd_num)%2!=0)        {            sum_even -= 2;        }        printf("%d\n",sum_even/odd_num+1);    }    return 0;}


原创粉丝点击