HDU 5744 Keep On Movin(胡搞)

来源:互联网 发布:打印机服务器软件 编辑:程序博客网 时间:2024/06/16 14:21

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的直接算下和处理下分配就行

代码如下:

#include<bits/stdc++.h>using namespace std;int a[100005];int main(){    int n, t;    scanf("%d",&t);    while(t--){        int sum,num;        num = sum = 0;        scanf("%d", &n);        for(int i = 1; i <= n; i++){            scanf("%d",&a[i]);            if(a[i]%2){                num++;                sum += (a[i]-1)/2;            }            else            sum += a[i]/2;        }        int ans = max(1,num);        printf("%d\n",(sum/ans)*2 + (num!=0));    }    return 0;}
0 0
原创粉丝点击