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
2016 Multi-University Training Contest 2
题意:给你n个字符,第i个字符有a[i]个,现在让你把这些字符构造成一些回文串,问你所有划分方案中最短的回文串的最长长度。
分析:若所有的a[i]均为偶数,那么答案就是所有a[i]之和,否则又几个奇数就要划分出来几个回文串,然后把看剩余的部分能平分出几对字符出来。
题意:给你n个字符,第i个字符有a[i]个,现在让你把这些字符构造成一些回文串,问你所有划分方案中最短的回文串的最长长度。
分析:若所有的a[i]均为偶数,那么答案就是所有a[i]之和,否则又几个奇数就要划分出来几个回文串,然后把看剩余的部分能平分出几对字符出来。
#include <cstdio>
#include <queue>
#include <vector>
#include <cstdio>
#include <utility>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
int T,n,tot,a[100007];
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int flag = 0;
for(int i = 1;i <= n;i++)
{
scanf("%d",&a[i]);
if(a[i] & 1)
{
flag++;
a[i]--;
}
}
if(!flag)
{
int tot = 0;
for(int i = 1;i <= n;i++) tot+=a[i];
cout<<tot<<endl;
continue;
}
int now = 0;
for(int i = 1;i <= n;i++) now += a[i]/2;
cout<<1+2*(now/flag)<<endl;
}
}