题干:

Professor Zhang has kinds of characters and the quantity of the ii-th character is aiai. 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}{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 nn (1≤n≤105)(1≤n≤105) -- the number of kinds of characters. The second line contains nn integers a1,a2,...,ana1,a2,...,an (0≤ai≤104)(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

题目大意:

现在这个数组a里有一些字符,第i个字符的数量是a[i]。巨巨想用这些字符来构造一些回文串好让他的程序通过编译。

他想知道各种组合方案中最短字符串长度的最大值。

举个栗子:

现在有 ‘a’, ‘b’, ‘c’, ‘d’ 四种字符并且他们的数量是 {2,3,2,2} 巨巨可以构造出{ “acdbbbdca”}, { “abbba”, “cddc”}, { “aca”, “bbb”, “dcd”},或{“acdbdca”, “bb”} 四种方案.

在以上方案中,第一个方案的最短字符串长度比其他三种方案中的最短字符串长度都长,为9。

解题报告:

奇数字符串单独处理一下。(可以证明回文串中,如果有的话,奇数字符,只能有一个。)

AC代码:

#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
int main()
{ll t,n,x;cin>>t;while(t--) {scanf("%lld",&n);ll ji = 0,ou=0,sum=0;for(int i = 1; i<=n; i++) {scanf("%lld",&x);if(x & 1) ji++;sum+=x/2;}if(ji == 0) printf("%lld\n",sum*2);else printf("%lld\n",2*(sum/ji)+ 1);//不加括号不对}return 0;
}
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
int ji,ou,all,n;
int main()
{int t;cin>>t;while(t--) {ji=ou=all=0;cin>>n;for(int tmp,i = 1; i<=n; i++) {scanf("%d",&tmp);if(tmp&1) ji++,all+=(tmp-1)>>1;else all+=tmp>>1;}if(ji==0) printf("%d\n",all<<1);else printf("%d\n",(all/ji)*2+ 1);} return 0 ;}

【HDU - 5744 】Keep On Movin (回文串性质,贪心思维,不是水题)相关推荐

  1. 最长回文串_第78天——第78题(最长回文串 )

    今天又是阴天,不过阴天凉快,我喜欢. 第78天--第78题(最长回文串) 看题目! 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 ...

  2. 力扣2131——连接两字母单词得到的最长回文串(贪心+哈希表)

    题目(中等) 给你一个字符串数组 words .words 中每个元素都是一个包含 两个 小写英文字母的单词. 请你从 words 中选择一些元素并按 任意顺序 连接它们,并得到一个 尽可能长的回文串 ...

  3. leetcode 1328. Break a Palindrome | 1328. 破坏回文串(贪心)

    题目 https://leetcode.com/problems/break-a-palindrome/ 题解 分析所有可能情况,然后贪心. class Solution {public String ...

  4. hdu 3068 最长回文 (Manacher算法求最长回文串)

    参考博客:Manacher算法--O(n)回文子串算法 - xuanflyer - 博客频道 - CSDN.NET 从队友那里听来的一个算法,O(N)求得每个中心延伸的回文长度.这个算法好像比较偏门, ...

  5. hdu 3613 扩展kmp+回文串

    题目大意: 给个字符串S,要把S分成两段T1,T2,每个字母都有一个对应的价值,如果T1,T2是回文串(从左往右或者从右往左读,都一样),那么他们就会有一个价值,这个价值是这个串的所有字母价值之和,如 ...

  6. 最长回文 HDU - 3068(求最长回文串的长度【马拉车算法Manacher】)

    马拉车算法 Manacher's Algorithm 是用来查找一个字符串的最长回文子串的线性方法,由一个叫 Manacher 的人在 1975 年发明的,这个方法的最大贡献是在于将时间复杂度提升到了 ...

  7. HDU 3613 Best Reward 扩展kmp算法(将一个字符串分成两个回文串)

    题目链接:https://vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. Now ...

  8. Manacher算法 - 求最长回文串的利器

    求最长回文串的利器 - Manacher算法 Manacher主要是用来求某个字符串的最长回文子串. 不要被manacher这个名字吓倒了,其实manacher算法很简单,也很容易理解,程序短,时间复 ...

  9. AK F.*ing leetcode 流浪计划之回文串

    欢迎关注更多精彩 关注我,学习常用算法与数据结构,一题多解,降维打击. 文章目录 一.简介 二.解题步骤 三.作用 四.经典算法介绍 判断一个串是否为回文串(单次查询) 普通情况 判断指定字符 多次子 ...

最新文章

  1. 2、安装ICS(Internet Component Suite)控件
  2. 中班游戏电子计算机,计算器中班歌唱活动教案
  3. 数据结构与算法笔记(七)—— 选择排序
  4. 在tomcat上部署项目,实现类似添加这样的功能之后,tomcat要运行很久,解决办法
  5. list 泛型_带你深挖Java泛型类型擦除以及类型擦除带来的问题
  6. 基于JAVA+SpringMVC+MYSQL的高校教师档案管理系统
  7. sql找出2000-3000年中的闰年。_跟飞哥学编程:SQL入门-4-查询和条件
  8. android 中如何分析内存泄漏
  9. windows10 + centos7 双系统
  10. 8款流行前沿的HTML5文本编辑器
  11. MATLAB基础教程(xlsread和xlswrit函数+数据拟合+数值计算)
  12. java毕业设计物流跟踪系统mybatis+源码+调试部署+系统+数据库+lw
  13. 卡贴机变无锁教程_iphone卡贴机ICCID激活去掉卡贴变成无锁机的教程及原理
  14. 读书笔记2014第6本:《The Hunger Games》
  15. zabbix_sender安装和使用
  16. 访问不到webapp
  17. java se运行环境_Java运行环境Java SE Runtime Environment (JRE) 下载
  18. 呼叫系统的技术实现原理和运作流程
  19. 20层的试炼html5,Vue.js-02:新手村的试炼 - 新世界的武器(指令)
  20. 学习大数据-flink实时数据流处理

热门文章

  1. [安全模型][Cambria Math][A][]敌手A-> 怎么打出来?
  2. [剑指offer][JAVA]面试题第[17]题[打印从1到最大的n位整数][大整数][递归回溯]
  3. [剑指offer]面试题第[2]题[JAVA][替换空格][函数][字符串]
  4. r语言error in match.fun(fun) :_Go语言200行写区块链源代码分析
  5. acctype mysql assoc_DedeCMS V5.3二次开发经验分享
  6. vue项目前端服务器,【前端技术】vue自动部署项目到服务器
  7. mysql 集群 qps_MySQL Cluster:如何通过扩展为MySQL带来2亿QPS
  8. js后退页面不重新加载_快应用:支持加载单独JS文件的规范思考
  9. c语言输入坐标判断位置,〓求助〓谁能帮忙编个程序:输入N个点坐标,判断能否构成多边形....
  10. java io 文件路径_如何从Java项目中的相对路径读取文件? java.io.File找不到指定的路径...