【HDU - 5744 】Keep On Movin (回文串性质,贪心思维,不是水题)

题干:

Professor Zhang has kinds of characters and the quantity of the ii-th character is aiai. 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}{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 nn (1≤n≤105)(1≤n≤105) -- the number of kinds of characters. The second line contains nn integers a1,a2,...,ana1,a2,...,an (0≤ai≤104)(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

题目大意:

现在这个数组a里有一些字符,第i个字符的数量是a[i]。巨巨想用这些字符来构造一些回文串好让他的程序通过编译。

他想知道各种组合方案中最短字符串长度的最大值。

举个栗子:

现在有 ‘a’, ‘b’, ‘c’, ‘d’ 四种字符并且他们的数量是 {2,3,2,2} 巨巨可以构造出{ “acdbbbdca”}, { “abbba”, “cddc”}, { “aca”, “bbb”, “dcd”},或{“acdbdca”, “bb”} 四种方案.

在以上方案中,第一个方案的最短字符串长度比其他三种方案中的最短字符串长度都长,为9。

 

解题报告:

        奇数字符串单独处理一下。(可以证明回文串中,如果有的话,奇数字符,只能有一个。)

AC代码:

#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
int main()
{
	ll t,n,x;
	cin>>t;
	while(t--) {
		scanf("%lld",&n);
		ll ji = 0,ou=0,sum=0;
		for(int i = 1; i<=n; i++) {
			scanf("%lld",&x);
			if(x & 1) ji++;
			sum+=x/2;
		}
		if(ji == 0) printf("%lld\n",sum*2);
		else printf("%lld\n",2*(sum/ji)+ 1);//不加括号不对
	}
	return 0;
}
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
int ji,ou,all,n;
int main()
{
	int t;
	cin>>t;
	while(t--) {
		ji=ou=all=0;
		cin>>n;
		for(int tmp,i = 1; i<=n; i++) {
			scanf("%d",&tmp);
			if(tmp&1) ji++,all+=(tmp-1)>>1;
			else all+=tmp>>1;
		}
		if(ji==0) printf("%d\n",all<<1);
		else printf("%d\n",(all/ji)*2+ 1);
	} 


	return 0 ;
 }
全部评论

相关推荐

头像
04-27 15:11
已编辑
华东师范大学 算法工程师
暑期实习从2月开始投,面了两个月,流程该挂的都挂完了,腾讯字节一共号称是1.7w个hc,不知道都发给谁了,估计今年秋招要难顶。Timeline米哈游、美团、蚂蚁、微软等公司直接简历挂穿,没进面。携程:3.3&nbsp;投递、测评3.12&nbsp;笔试3.18&nbsp;一面3.25&nbsp;二面4.13&nbsp;ai面(hr面)4.14&nbsp;英语测评4.23&nbsp;offer(已拒)腾讯:2.6&nbsp;测评2.28&nbsp;wxg一面3.5&nbsp;wxg二面(挂)3.11&nbsp;teg一面3.21&nbsp;teg二面(取消)3.31&nbsp;teg一面4.10&nbsp;teg二面(挂)4.21&nbsp;wxg一面4.24&nbsp;wxg二面(挂)字节:1.28&nbsp;aml约面(取消)3.17&nbsp;火山一面(挂)4.8&nbsp;aml一面(挂)4.20&nbsp;抖音data一面(挂)阿里:3.23&nbsp;投递、测评3.28&nbsp;笔试3.31&nbsp;淘天一面4.8&nbsp;钉钉一面4.9&nbsp;淘天二面4.10&nbsp;阿里控股一面4.12&nbsp;钉钉二面(取消)4.15&nbsp;淘天hr面4.16&nbsp;淘天offer(已接)4.21&nbsp;高德一面(取消)4.22&nbsp;淘宝闪购一面(取消)面试最大的感触是,现在撞上ai转型,一堆老业务急着转向,新业务非常不成熟,研究型的组bar非常高根本进不去,业务侧挂着算法的岗位干的都是工程活,面试却又要问算法,另外agent的落地也远没有那么广,绝大多数还是那套写死的系统调一下llm&nbsp;api或者做做rag,其余少部分真的在搭agent的,基本不能在线上服务用什么很智能的模型,现阶段成本太高,进去大概率就是给垃圾模型从工程方面兜底,除了业务场景的应用和数据经验以外,技术方面很难有什么提升。算法岗做不了基模的还是去搜广推好,之前判断失误了完全没投,秋招不知道还进不进得去。
嵌入式的小白:不错啊,淘天也是挺好的,恭喜
我的求职进度条
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务