码迷,mamicode.com
首页 > 其他好文 > 详细

Hdu 5744 Keep On Movin【思维】

时间:2016-07-22 19:22:11      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

Keep On Movin

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 343 Accepted Submission(s): 248


Problem Description
Professor Zhang has kinds of characters and the quantity of the i技术分享-th character is a技术分享i技术分享技术分享. 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技术分享 (1n10技术分享5技术分享)技术分享 -- the number of kinds of characters. The second line contains n技术分享 integers a技术分享1技术分享,a技术分享2技术分享,...,a技术分享n技术分享技术分享 (0a技术分享i技术分享10技术分享4技术分享)技术分享.

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

Author
zimpha

Source


给出n个数字,代表n种字符的数量,把这些字符全部使用,来组建成回文的字符串,可能会有很多种方式,问这些方式中,其中最短的回文字符串最长有多长

解释的有点绕口....


1,对于偶数个数的字符来说,只有每次组合都在两边同时添加某种字符,肯定还是回文的,但是奇数的不能加到别的字符旁边,所以把奇数单独拿出来讨论

2,奇数可以由偶数+1得到,所以把奇数全部变成1,其他的都是偶数个了,并且都是成对存在的

3,平均分配这些成对的字符串,计算其中最少的


原先自己只想到第1步,还想着用二分去做,后来才发现自己太局限了,思路没有打开,大水题都卡了半天


#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,cnt=0,sum=0;
		scanf("%d",&n);
		for(int i=0;i<n;++i)
		{
			int tp;
			scanf("%d",&tp);
			if(tp&1)
			{
				++cnt;
			}
			sum+=tp/2*2;
		}
		int ans=sum;
		if(cnt)
		{
			ans=sum/2/cnt*2+1;
		}
		printf("%d\n",ans);
	}
} 



Hdu 5744 Keep On Movin【思维】

标签:

原文地址:http://blog.csdn.net/liuke19950717/article/details/51993392

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!