HDU 5744 Keep On Movin 贪心
Keep On Movin
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5744
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
Hint
题意
有n个字符,每个字符ai个,你需要构造一些字符串,使得这些字符串都是回文串,而且这些回文串恰好用完所有字符
而且最短的回文串最长,输出这个长度。
题解:
贪心去构造,考虑奇数的字符,我们可以拆成偶数+1,那么显然我们知道,奇数的就一定是单独的一行,然后偶数一定要成对的扔进去。
然后考虑到这些公式就很简单了……
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int a[maxn];
void solve(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
int num = 0;
long long ans = 0;
long long sum = 0;
long long Ans = 0;
for(int i=1;i<=n;i++)
{
if(a[i]%2==1)num++;
ans+=a[i];
Ans+=a[i]/2;
}
if(num==0){
cout<<ans<<endl;
}else{
cout<<Ans/num*2+1<<endl;
}
}
int main(){
int t;
scanf("%d",&t);
while(t--)solve();
}
HDU 5744 Keep On Movin 贪心的更多相关文章
- HDU 5744 Keep On Movin (贪心) 2016杭电多校联合第二场
题目:传送门. 如果每个字符出现次数都是偶数, 那么答案显然就是所有数的和. 对于奇数部分, 显然需要把其他字符均匀分配给这写奇数字符. 随便计算下就好了. #include <iostream ...
- HDU 5744 Keep On Movin (贪心)
Keep On Movin 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5744 Description Professor Zhang has k ...
- HDU 5744 Keep On Movin
Keep On Movin Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- hdu 5744 Keep On Movin (2016多校第二场)
Keep On Movin Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- hdu 4825 Xor Sum(trie+贪心)
hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...
- 【HDU 5744】Keep On Movin
找出奇数个的数有几个,就分几组. #include<cstdio> #include<cstring> #include<algorithm> #include&l ...
- HDU 5813 Elegant Construction (贪心)
Elegant Construction 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5813 Description Being an ACMer ...
- HDU 5802 Windows 10 (贪心+dfs)
Windows 10 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5802 Description Long long ago, there was ...
- HDU 5500 Reorder the Books 贪心
Reorder the Books Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...
随机推荐
- html5 canvas 多个填充渐变形状
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 20155339 2016-2017-2 《Java程序设计》第5周学习总结
20155339 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 使用try.catch 使用try.catch语法,JVM会先尝试执行try区块中的代码,如 ...
- UVALive 6467
题目链接 : http://acm.sdibt.edu.cn/vjudge/contest/view.action?cid=2186#problem/C 题意:对于斐波那契数列,每个数都mod m , ...
- redhat7配置本地yum源
1.首先是要有一个iso文件,并将这个文件挂载到某个目录 挂载: 配置: 检验: yum list 现在你就可以在没有网的情况下,安装软件了~~~
- perl6: Proc::Async (new)
# command with arguments my $proc = Proc::Async.new('whoami'); # subscribe to new output from out an ...
- Replication监控及自动故障切换
首先在相应的机器上增加授权 GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.108' IDENTIFIED BY 'repl';GRANT RE ...
- pymongo创建索引
from database import db db_list = ["table1", "table2", "table3", " ...
- JavaScript中对象与函数的某些事[JavaScript语言精粹-N1]
今天在读<JavaScript语言精粹>的时候,关于函数的一个部分,始终觉得有点难以理解,代码如下: 1: var obj = (function(){ 2: var value = 0; ...
- 探秘Java类加载
Java是一门面向对象的编程语言. 面向对象以抽象为基础,有封装.继承.多态三大特性. 宇宙万物,经过抽象,均可归入相应的种类.不同种类之间,有着相对井然的分别. Java中的类,便是基于现实世界中的 ...
- Java编程的逻辑 (20) - 为什么要有抽象类?
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...