HDU-5744 Keep On Movin(回文串 贪心)
来源:互联网 发布:sql server 导出数据库 编辑:程序博客网 时间:2024/06/08 14:11
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1030 Accepted Submission(s): 725
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
题意:
每组数据先给你一个n,然后给你n种字符的个数,求各种组合方案中最短字符串长度的最大值
思路:
最短字符串长度的最大值,所以让最短字符串的长度越趋向平均长度,其越长
由于int类型取整,所以用偶数的个数除以奇数的个数,即某个奇数平均获得的偶数对的数量,再加1便是答案
代码:
/************************************************************************* > File Name: HDU5744.cpp > Author: SIU > Created Time: 2016年11月28日 星期一 16时57分08秒 ************************************************************************/#include<cstdio>#include<iostream>#include<cstring>using namespace std;int main(){ int t; scanf("%d",&t); int n; int num[100050]; int ji; int ou; while(t--) { ji=0; ou=0; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&num[i]); if(num[i]==0) continue; if(num[i]==1) ji++; else if(num[i]%2==1) { ji++; ou+=(num[i]-1)/2; } else if(num[i]%2==0) ou+=num[i]/2; } int ans=0; if(ji==0) ans=ou*2; else if(ou==0) ans=1; else { ans=(ou/ji)*2+1; } printf("%d\n",ans); } return 0;}
- HDU-5744 Keep On Movin(回文串 贪心)
- HDU 5744 Keep On Movin(贪心)
- Hdu-5744 Keep On Movin(贪心)
- HDU 5744 Keep On Movin(思维+贪心)
- HDOJ 5744 Keep On Movin(回文串)
- HDU 5744 Keep On Movin(水题)
- hdu 5744 Keep On Movin (字符串)
- hdu 5744 Keep On Movin
- hdu 5744 Keep On Movin
- hdu 5744 Keep On Movin
- hdu 5744 Keep On Movin
- HDU 5744 ( Keep On Movin )
- HDU 5744 Keep On Movin
- HDU 5744 Keep On Movin
- 【HDU】5744 Keep On Movin
- hdu 5744 Keep On Movin
- HDU 5744 Keep On Movin 贪心,找规律
- hdu 5744 Keep On Movin(2016 Multi-University Training Contest 2——回文串,思维题)
- -Dmaven.multiModuleProjectDirectory system propery is not set.
- Workspace default 的JRE System Library与Project的JRE System Library
- UIWindow基础知识详解
- 配置SharePoint 2016开发环境的时候遇到的两个问题
- Hive分区、分桶操作及其比较
- HDU-5744 Keep On Movin(回文串 贪心)
- Cent OS7.2 服务器安装搭架
- ViewFlipper实现滚动布局
- iOS 去掉html标签
- iOS10本地通知UserNotifications快速入门
- 如何严格设置php中session过期时间
- 一个Win32 C++ 动态连接库的模板 — 调用方可管理DLL分配的内存
- DB2存储过程杂谈
- null is not object evaluating ('_this3_selectRow')