题目链接:HDU5744
Keep On Movin
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 530 Accepted Submission(s): 384
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.
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) .
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
Author
zimpha
Source
Recommend
wange2014
题目分析:应分多少组回文串由奇数得个数决定。如果没有奇数,所有的字符拼在一起就是回文串,否则有几个分几组,其余字符平均分配。注意如果除下来是偶数,则应该手动-1(以奇数为中心的回文串长度为奇数)。
//
// main.cpp
// Keep On Movin
//
// Created by teddywang on 2016/7/21.
// Copyright © 2016年 teddywang. All rights reserved.
//
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int od[100010],ou[100010];
int T,n,m;
int l1,l2;
int ans;
int main()
{
cin>>T;
while(T--)
{
cin>>n;
l1=0,l2=0;
ans=0;
for(int i=0;i<n;i++)
{
int buf;
cin>>buf;
ans+=buf;
if(buf%2) l1++;
else l2++;
}
if(l1)
{
ans/=l1;
if(ans%2==0) ans--;
cout<<ans<<endl;
}
else
cout<<ans<<endl;
}
}