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   (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
 


题目大意:给定n个数,代表n个字符的个数,每个数代表的字符是唯一确定的,将这些字符组成若干个回文串,找出所有组合方式中最短回文串的最长长度。


显然,既然是回文串,如果所有数都是偶数的话,答案就是这些数的和。如果出现奇数,显然要让它们各自单独成串,要使最短的回文串最长,那么就要使得这些串的长度尽可能均等,那么我们就把较长串的偶数部分均匀地分给短串,实际上就是我们求出一个累加值sum,sum等于所有偶数和加所有奇数-1的和,然后平均分配到odd个奇数串中,如果sum / odd = x结果为偶数,那么加上奇数串长度1,答案就是x + 1,否则如果x是奇数的话,由于本题不能向一个奇数串中加入奇数个字符构成回文串,所以我们可以加x + 1或x - 1,显然x - 1符合题目最短最长的要求,再加上1,答案就是x,代码如下:


#include <bits/stdc++.h>

using namespace std;

int main()
{
	int t,n,x;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		int odd = 0,cnt = 0;
		for(int i = 1 ; i <= n ; i++)
		{
			scanf("%d",&x);
			if(x & 1) odd++;
			cnt += x & (~1);
		}
		printf("%d\n",odd ? (cnt / odd) | 1 : cnt);
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值