Keep On Movin

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

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
 
Author
zimpha
 
Source
 
 
 
解析:如果每个字符出现的次数都是偶数,结果显然为各字符出现的次数之和。如果含有奇数,则奇数1+2k(k>=0)可分为1和2k,2k和其他偶数相加得到总和,再把得到的和平均分给奇数字符即可。
 
 
 
#include <cstdio>

int main()
{
int T, n;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
int odd = 0; //记录奇数字符有多少个
int sum = 0; //记录总和
int num;
while(n--){
scanf("%d", &num);
if(num&1){
++odd;
sum += num-1;
}
else{
sum += num;
}
}
if(odd == 0){ //只有偶数字符
printf("%d\n", sum);
}
else{
sum >>= 1; //得到能够分配的对数
printf("%d\n", sum/odd*2+1);
}
}
return 0;
}

  

HDU 5744 Keep On Movin的更多相关文章

  1. HDU 5744 Keep On Movin (贪心)

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

  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 (2016多校第二场)

    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杭电多校联合第二场

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

  5. 【HDU 5744】Keep On Movin

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

  6. 2016 Multi-University Training Contest 2题解报告

    A - Acperience HDU - 5734 题意: 给你一个加权向量,需要我们找到一个二进制向量和一个比例因子α,使得|W-αB|的平方最小,而B的取值为+1,-1,我们首先可以想到α为输入数 ...

  7. 类似区间计数的种类并查集两题--HDU 3038 & POJ 1733

    1.POJ 1733 Parity game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5744   Accepted: ...

  8. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

随机推荐

  1. Synchronized Methods

    Synchronized Methods The Java programming language provides two basic synchronization idioms: synchr ...

  2. gulp-css-spriter 雪碧图合成

    一.配置 gulp的雪碧图功能没有grunt那么强大,但是类似功能也支持,功能稍微弱一些,但是也很棒 npm地址:https://www.npmjs.com/package/gulp-css-spri ...

  3. Android开发--解决AndroidADT开发工具不能代码提示的问题

    google android的新的开发工具,打开以后没有代码自动提示功能,下面对ADT工具的一些配置: 1.设置代码的字体 设置JAVA文件代码的字体:我这里设置的14 常规.

  4. hdu 1426 Sudoku Killer

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1426 #include<stdio.h> #include<math.h> #in ...

  5. asp.net 权限问题

    asp.net项目中通过Web.config配置文件及文件夹的访问权限! http://blog.csdn.net/qingyun1029/article/details/6184723

  6. JavaWeb项目开发案例精粹-第2章投票系统-004action层

    1. package com.sanqing.action; import java.util.UUID; import com.opensymphony.xwork2.ActionSupport; ...

  7. tomcat version

    Tomcat version 6.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 Web modules   转自:http://jingwang0 ...

  8. windows系统下Python环境的搭建

    1.下载最新的Python版本3.5.0.

  9. Shuffle和排序

    MapReduce确保每个reducer的输入都按键排序.系统执行排序的过程——将map输出作为输入传给reducer——称为shuffle.shuffle属于不断被优化和改进的代码库的一部分,从许多 ...

  10. redis sentinel 配置

    在最小配置:master.slave各一个节点的情况下,不管是master还是slave down掉一个,“完整的”读/写功能都将受影响,这在生产环境中显然不能接受.幸好redis提供了sentine ...