2016 Multi-University Training Contest 2 Keep On Movin

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dd_lucky/article/details/51992478
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
 
  
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
 

Author
zimpha

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

#include<iostream>
#include<cstdio>
using namespace std;
int a[100005];
int main()
{
int t,n;
cin>>t;
while(t--)
{
cin>>n;
int sum=0,odd=0,num=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum+=a[i];
if(a[i]%2)
{
odd++;
num+=(a[i]-1)/2;
}
else
{
num+=a[i]/2;
}
}
if(odd==0)
printf("%d\n",sum);
else
{
printf("%d\n",1+2*(num/odd));
}

}
return 0;
}

猜你喜欢

转载自blog.csdn.net/dd_lucky/article/details/51992478