您的位置:首页 > 其它

多校联赛第二场 1011 Keep On Movin

2016-07-23 08:26 323 查看


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 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 (1≤n≤105) --
the number of kinds of characters. The second line contains n integers a1,a2,...,an (0≤ai≤104).

 

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

 

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

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int t,n,a,k;
scanf("%d",&t);
while(t--)
{
int count=0,sum=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&a);
if(a%2)
count++;
sum+=a;
}
if(count==0)
printf("%d\n",sum);
else
{
int ans=sum/count;
if(ans%2==0)
printf("%d\n",ans-1);
else
printf("%d\n",ans);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  基础题