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

Source

2016 Multi-University Training Contest 2

##题意:

给出n种字符的各自的数量,用它们构成多个回文串,求最短回文串的最大长度.


##题解:

首先,如果某个字符有奇数个,那么单出来的那个肯定要放在某个回文串的中心位置.
那么可以先找出最少有多少个这样的中心点.
对于剩下的字符(肯定是偶数个), 要把它们平均分配到这些中心点两边.
这里要注意:每个中心点被额外分配的字符个数一定是偶数.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define double LL
#define eps 1e-8
#define maxn 101000
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

int n;

int num[maxn];

int main(void)

{

//IN;

int t; cin >> t;
while(t--)
{
cin >> n;
int center = 0;
int extra = 0;
for(int i=1; i<=n; i++) {
scanf("%d", &num[i]);
extra += num[i];
if(num[i]&1) center++,extra--;
} int ans;
if (!center || (extra%center==0 && (extra/center)%2==0)) {
if(!center) ans = extra;
else ans = extra/center + 1;
}else {
int tmp = extra/center;
if(tmp&1) ans = tmp-1;
else ans = tmp;
ans += 1;
} printf("%d\n", ans);
} return 0;

}

最新文章

  1. 转载:第六弹!全球首个微信小程序(应用号)开发教程!通宵吐血赶稿!
  2. sql: table,view,function, procedure created MS_Description in sql server
  3. linq 动态组合条件
  4. think完全还原原形的 SQL
  5. Rank of Tetris HDU--1881
  6. [Angular 2] NgNonBindable
  7. Digit Stack
  8. 【线段树求最靠前】【HDU2795】【Billboard】
  9. 解读Java内部类
  10. python 自定义模块的发布和安装
  11. 如何解决Java警告信息:&quot;objc[31336]: Class JavaLaunchHelper is implemented in both places ...&quot;
  12. alfs学习笔记-自动化构建lfs系统
  13. opencart 3添加pdf文档下载功能
  14. [Spark][Python][DataFrame][RDD]从DataFrame得到RDD的例子
  15. Linux 文件查看,文件夹切换,权限查看
  16. Nginx解析PHP的原理 | CGI、FastCGI及php-fpm的关系
  17. 给sublime设置格式化代码的快捷键
  18. Kubernetes 之上的架构应用
  19. ubuntu apache linux
  20. ps叠加模式笔记

热门文章

  1. 【C#设计模式——创建型模式】抽象工厂模式
  2. HDU 4549 M斐波那契数列(矩阵幂)
  3. R语言日期时间函数
  4. 最受Web前端开发者欢迎的五大开发工具
  5. 用JAVA代码构造一个日历
  6. UVALive 4043 Ants(二分图完美匹配)
  7. GBDT(Gradient Boosting Decision Tree)算法&amp;协同过滤算法
  8. operator.itemgetter的用法【转】
  9. Cocoa Touch(一)开发基础:Xcode概念、目录结构、设计模式、代码风格
  10. windows配置jdk