1. 程式人生 > >HDU 5744 Keep On Movin(胡搞)

HDU 5744 Keep On Movin(胡搞)

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; }