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 贪心的更多相关文章

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

    题目:传送门. 如果每个字符出现次数都是偶数, 那么答案显然就是所有数的和. 对于奇数部分, 显然需要把其他字符均匀分配给这写奇数字符. 随便计算下就好了. #include <iostream ...

  2. HDU 5744 Keep On Movin (贪心)

    Keep On Movin 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5744 Description Professor Zhang has k ...

  3. HDU 5744 Keep On Movin

    Keep On Movin Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  4. hdu 5744 Keep On Movin (2016多校第二场)

    Keep On Movin Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  5. hdu 4825 Xor Sum(trie+贪心)

    hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...

  6. 【HDU 5744】Keep On Movin

    找出奇数个的数有几个,就分几组. #include<cstdio> #include<cstring> #include<algorithm> #include&l ...

  7. HDU 5813 Elegant Construction (贪心)

    Elegant Construction 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5813 Description Being an ACMer ...

  8. HDU 5802 Windows 10 (贪心+dfs)

    Windows 10 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5802 Description Long long ago, there was ...

  9. HDU 5500 Reorder the Books 贪心

    Reorder the Books Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

随机推荐

  1. [转] Android 性能分析案例

    Android 系统的一个工程师(Romain Guy)针对Falcon Pro  应用,撰写了一个Android性能分析的文章.该文章介绍了如何分析一个应用哪里出现了性能瓶颈,导致该应用使用起来不流 ...

  2. CSS 编码中超级有用的工具集合

    当你开发网站和 Web 应用时,使用合适的工具可以节省大量的时间.本文我将收集一些非常有用的 CSS 编码工具,希望对你有帮助. Pure CSS Pure 是来自雅虎的 CSS 框架,使用 Norm ...

  3. .net中的一般处理程序实例

    最近在学习一般处理程序,也学习了一些jQuery的异步操作,于是就想着亲手做一个小的登陆,锻炼一下自己. 1.首先新建了一个项目LoginDemo,在此基础上又添加了一个一般处理程序BackLogin ...

  4. 20155303 2016-2017-2 《Java程序设计》第五周学习总结

    20155303 2016-2017-2 <Java程序设计>第五周学习总结 教材学习中的问题和解决过程 『问题一』:受检异常与非受检异常 『问题一解决』: 受检异常:这种在编译时被强制检 ...

  5. 【转】在Mac OS X 10.8中配置Apache + PHP + MySQL

    CHENYILONG Blog 在Mac OS X 10.8中配置Apache + PHP + MySQL 在Mac OS X 10.8中配置Apache+PHP+MySQL的内容包括: 配置Apac ...

  6. iOS 远程推送注册的小问题

    iOS8有了新方法,用新方法后,用7.0版本运行会奔溃.只要加一句判断就ok: #ifdef __IPHONE_8_0 // 在 iOS 8 下注册苹果推送,申请推送权限. UIUserNotific ...

  7. 关闭ios弹出框:“would like to use your current location”

    图一: 图二: 使用cordova生成ios项目,首次打开获取用户定位时会弹出两次对话框,关闭图二中对话框方法: document.addEventListener("deviceready ...

  8. weblogica

  9. House Robber I & II & III

    House Robber You are a professional robber planning to rob houses along a street. Each house has a c ...

  10. 诡异的Linux磁盘空间被占用问题,根目录满了,df和du占用不一样【转】

    新公司的测试机磁盘空间空余很小,日志很多,也很大,做个日志压缩脚本,在夜里4:30自动运行,第二天后发现磁盘空间又满了,只好删除没用的日志,清空空间,可诡异的是怎么删除没用的文件,空间还是占用很大.如 ...