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
题意: 给你每个字符的个数,让你组成几堆回文串,要求输出最小堆的个数,最佳分配使得该个数最大。
思路: 每个奇数都是一个回文串,所以先统计奇数个数,回文串可表示为 ←|→,字符总个数减去奇数字符的个数为↔可分配的个数,除以二为一边可分配的个数,因为要最小的尽量大所以平均分,所以除以奇数个数, 乘回二为↔总个数,+1为加上对称轴。
代码抄标程...
#include<bits/stdc++.h>
using namespace std;
int a[102400];
int main() {
int T, n;
for(scanf("%d", &T); T--; ) {
scanf("%d", &n);
int s = 0, c = 0;
for(int i = 0, t; i < n; ++i) {
scanf("%d", &t);
if(t&1) ++c;
s += t;
}
printf("%d\n", c ? (s-c)/2/c * 2 + 1 : s);
}
}
刚开始T没看好,以为同种字符不能拆成好几堆...还是要像復锦巨巨说的要分析分析分析分析题意阿,没事就分析分析分析分析分析分析...