【HDU5744 2016 Multi-University Training Contest 2K】【水题 贪心】Keep On Movin 给定字符使得最短回文串最长

99 篇文章 0 订阅
84 篇文章 5 订阅

Keep On Movin

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


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
 

Author
zimpha
 

Source

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e5+10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int casenum, casei;
int n;
int a[N];
int main()
{
	scanf("%d", &casenum);
	for (casei = 1; casei <= casenum; ++casei)
	{
		scanf("%d", &n);
		int odd = 0;
		int even = 0;
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d", &a[i]);
			if (a[i] & 1)++odd;
			even += a[i] / 2;
		}
		if (!odd)
		{
			printf("%lld\n", even * 2);
		}
		else
		{
			even /= odd;//最少能分几对
			printf("%lld\n", 1 + even * 2);
		}
	}
	return 0;
}
/*
【trick&&吐槽】
比赛要找水题做——
而不是抢一血卡难题>.<

【题意】
我们希望用给定字符集,组成回文串,数量无所谓,但使得最短回文串的长度尽可能长。

【类型】
贪心

【分析】
回文串的长度有奇数、偶数两种类型。

1,如果没有长度为奇数的回文串 ,显然答案就是|len|
2,否则,我们至少也要有奇数个回文串。
	对于偶数字符pair,我们要凑在奇数回文串的两侧。
	显然均分最优。

【时间复杂度&&优化】
O(n)

*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值