Hdu 5744 Keep On Movin【思维】

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

Author

zimpha

Source

2016 Multi-University Training Contest 2

给出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);
	}
} 
时间: 2024-10-05 10:08:20

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 palindro

HDU 5744 - Keep On Movin

题意: 给你不同的字符每个 ai 个,让你构造一些回文串,问你能达到这些回文串长度的最小值的最大值是多少 分析: 要么直接组成单链. 要么按落单的字符的数目将成对字符分摊取最短. 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int main() 5 { 6 int t, n; 7 scanf("%d", &t); 8 while(t--) 9 { 10 long l

HDU 5744 Keep On Movin (贪心) 2016杭电多校联合第二场

题目:传送门. 如果每个字符出现次数都是偶数, 那么答案显然就是所有数的和. 对于奇数部分, 显然需要把其他字符均匀分配给这写奇数字符. 随便计算下就好了. #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; int main() { int T,n,a; scanf("%d",&T);

HDU 6154 CaoHaha&#39;s staff 思维 找规律

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6154 题目描述: 围成一个面积不小于S的多边形, 最少需要多少根儿线段, 线段可以为单元格边或者对角线 解题思路: 最大的面积肯定是由根号2为边长的正方形围成了, 那么我们把所有正方形都遍历一遍, 找出S介于N, N+1的那个上界N+1设为max, 因为MAX所围成的多边形面积和MAX-1, MAX-2, MAX-3围成的多边形面积, 找出满足条件的最小的一个即可 代码: #include <io

2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6154 CaoHaha&#39;s staff 思维

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6154 题意:在笛卡尔坐标系下,画一个面积至少为  n 的简单多边形,每次只能画一条边或者一个格子的对角线,问至少要画几条. 解法:如果一个斜着的矩形长宽分别是 a,b,那么它的面积是 2ab.最优解肯定是离 sqrt(n/2)很近的位置.想想 n=5 时答案为什么是7 然后在那个小范围内枚举一下就好了.我给一张做题时画的图 #include <bits/stdc++.h> using namesp

hdu 4710 Balls Rearrangement (数学思维)

题意:就是  把编号从0-n的小球对应放进i%a编号的盒子里,然后又买了新盒子, 现在总共有b个盒子,Bob想把球装进i%b编号的盒子里.求重置的最小花费. 每次移动的花费为y - x ,即移动前后盒子编号的差值的绝对值. 算法: 题目就是要求                  先判断  n与  lcm(a,b)的大小,每一个周期存在循环,这样把区间缩短避免重复计算. 如果n>lcm(a,b)则   ans = (n/lcm)*solve(lcm)+solve(n%lcm) 否则   ans =

HDU 3972 1 M possible(思维)

1 M possible Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 2048/1024 K (Java/Others) Total Submission(s): 1031    Accepted Submission(s): 343 Problem Description There are 3*N+2 nonnegative integers. Except two special integers, the rest 3

【HDU 5744】Keep On Movin

找出奇数个的数有几个,就分几组. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define N 100005 using namespace std; int n,a[N],k,t,sum; int main(){ scanf("%d",&t); while(t--){ scanf("%d",&n); s

HDU 4869 Turn The Pokers 思维+组合

HDU 4869题意:m张牌,朝上状态为1,朝下状态为0,现在有n个操作 第i次操作可以反转任意xi张牌初始牌全部朝下,n,m<=1e5,问n次操作后能得到多少种不同的状态? 关心的是最后的状态 假如1有x个 则贡献C(m,x)种状态因为每翻转一次,1的个数和0的个数都相差2. 当每轮最少得到x个1,最多得到y个1 则1的个数范围[x,x+2...y-2,y]中都能取到,维护1的可取个数 组合数累加即可. #include <bits/stdc++.h> using namespace