hdu-5744 Keep On Movin(思维)

题目链接:

Keep On Movin

Time Limit: 4000/2000 MS (Java/Others)   

 Memory Limit: 65536/65536 K (Java/Others)


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 (1n105) -- the number of kinds of characters. The second line contains n integers a1,a2,...,an (0ai104).
 

 

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
 
题意:
 
给出每种字符有多少个,现在要你全部用完,拼成回文串,问回文串最短的那串的最长长度是多少;
 
思路:
 
每俩个相同的字符就可以加在任意的串上,要使最短的最长,所以就要平均加;
具体的看代码吧;
 

 AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));

typedef  long long LL;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('\n');
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e5+10;
const int maxn=500+10;
const double eps=1e-9;

int a[N];

int main()
{
        int t;
        read(t);
        while(t--)
		{
			int n,num=0,sum=0;
			read(n);
			For(i,1,n)
			{
				read(a[i]);
				if(a[i]&1)num++;
				sum+=a[i]/2;
			}
			if(num)cout<<1+sum/num*2<<"\n";
			else cout<<sum*2<<"\n";
			//if(sum%num==0)cout<<1+sum/num*2<<endl;
			//else cout<<1+sum

		}
        return 0;
}

  

posted @ 2016-07-21 19:50  LittlePointer  阅读(171)  评论(0编辑  收藏  举报