hdu5744(16多校第2场,思维题)

来源:互联网 发布:马云大数据时代演讲 编辑:程序博客网 时间:2024/06/04 19:27

Keep On Movin

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


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

如果每个字符出现次数都是偶数, 那么答案显然就是所有数的和. 对于奇数部分, 显然需要把其他字符均匀分配给这写奇数字符. 随便计算下就好了.

#include <iostream>#include <stdio.h>#define ll long longusing namespace std;int main(){    int T,n,a;    while(~scanf("%d",&T))    {        while(T--)        {            ll sum=0,num=0;            int flag=0;            scanf("%d",&n);            for(int i=0; i<n; i++)            {                scanf("%d",&a);                if(a%2==0) sum+=(ll)a;                else num++,sum+=(ll)(a-1),flag=1;            }            if(flag==1)            {                ll ans=sum/(num*2);                printf("%I64d\n",ans*2+1);            }            else printf("%I64d\n",sum);        }    }    return 0;}


0 0
原创粉丝点击