HDOJ 5744 Keep On Movin(回文串)
来源:互联网 发布:淘宝男士护肤 编辑:程序博客网 时间:2024/06/08 05:22
Keep On Movin
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 791 Accepted Submission(s): 572
Problem Description
Professor Zhang has kinds of characters and the quantity of thei -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.
For example, there are 4 kinds of characters denoted as 'a', 'b', 'c', 'd' and the quantity of each character is
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 integern (1≤n≤105) -- the number of kinds of characters. The second line contains n integers a1,a2,...,an (0≤ai≤104) .
The first line contains an integer
Output
For each test case, output an integer denoting the answer.
Sample Input
441 1 2 432 2 251 1 1 1 151 1 2 2 3
Sample Output
3613
Author
zimpha
Source
2016 Multi-University Training Contest 2
思路:
题意就是说,给定你几个字母,让你看看他们能不能构成n个回文串,并且找每个回文串组中的长度最小值,然后再在这些最小值中找最大值。这就是对字母个数分组,如果字母都是偶数个,那很好,能平分成两个长度等长的。如果有字母的个数为奇数,那么先把这个1拿出来,剩下的还是能均等分成两份,再把这个1加到其中一个的中间位置。如果有n个奇数,那么先把n个1先拿出来,再剩下的均分成n份,再加进去1。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int N=100010;int a[N],b[N];int main(){ int T,n; scanf("%d",&T); while (T--) { scanf("%d",&n); int x,odd=0,sum=0; for (int i=1;i<=n;i++) { scanf("%d",&x); if (x&1) odd++,sum+=x-1; else sum+=x; } if (odd==0) printf("%d\n",sum); else printf("%d\n",sum/odd/2*2+1); } return 0;}
0 0
- HDOJ 5744 Keep On Movin(回文串)
- HDOJ 5744 Keep On Movin(最大化最短回文串长度)
- HDU-5744 Keep On Movin(回文串 贪心)
- HDOJ 5744 Keep On Movin
- hdu5744——Keep On Movin(构造回文)
- HDU 5744 Keep On Movin(水题)
- hdu 5744 Keep On Movin (字符串)
- HDU 5744 Keep On Movin(贪心)
- hdu 5744 Keep On Movin(2016 Multi-University Training Contest 2——回文串,思维题)
- hdu 5744 Keep On Movin
- hdu 5744 Keep On Movin
- hdu 5744 Keep On Movin
- hdu 5744 Keep On Movin
- HDU 5744 ( Keep On Movin )
- HDU 5744 Keep On Movin
- HDU 5744 Keep On Movin
- 【HDU】5744 Keep On Movin
- hdu 5744 Keep On Movin
- HDOJ 5742 It's All In The Mind(数学变形)
- max sub sum II
- Java 过滤器的作用
- ListView上拉加载
- 重学 Statistics, Cha16 General Linear Model
- HDOJ 5744 Keep On Movin(回文串)
- django学习笔记二
- HDOJ 5753 Permutation Bo(排列组合)
- python高级编程
- 格雷码
- django学习笔记三
- HDOJ 5774 Where Amazing Happens(人肉搜索)
- Popular Products_太阁竞赛B
- JComboBox默认不选中任何选项的方法