http://acm.hdu.edu.cn/showproblem.php?pid=5744
Keep On Movin
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 50 Accepted Submission(s): 46
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
题目思路:虽然是一道水题,但是在这里给大家说下我的思路吧。
奇数的话一定可以拆分成1和某个偶数的和,然后把奇数的个数记录下来,偶数的对数记录下来,然后把偶数的对数平均分给每个奇数,这样可以使得最小值变大。
然后下面是AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int sum1,sum2,t,n;
scanf("%d",&t);
while(t--)
{
sum1=0,sum2=0;
int num;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&num);
if(num%2==0)
{
sum2+=num;
}
else
{
sum1++;
sum2+=(num-1);
}
}
if(sum1==0)
{
printf("%d\n",sum2);
continue;
}
if(sum2/2<sum1)
{
printf("1\n");
continue;
}
else
{
int dd=(sum2/2)/sum1;
int dd1=(sum2/2)%sum1;
printf("%d\n",dd*2+1);
}
}
return 0;
}