HDU 5744 Keep On Movin(水题)
2016-07-22 10:38
344 查看
题目链接:HDU 5744
题面:
Total Submission(s): 296 Accepted Submission(s): 219
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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).
[align=left]Output[/align]
For each test case, output an integer denoting the answer.
[align=left]Sample Input[/align]
4
4
1 1 2 4
3
2 2 2
5
1 1 1 1 1
5
1 1 2 2 3
[align=left]Sample Output[/align]
3
6
1
3
[align=left]Author[/align]
zimpha
[align=left]Source[/align]
2016 Multi-University Training Contest 2
题意:
给定多种字符的数量,要求用上全部的字符构建回文串,其中最短的回文串,在所有的方案中最长,求最短回文串长度。
解题:
因为希望最短回文串最长,故肯定希望构建尽量少的回文串,因为奇数个数量的字符肯定得开辟新串,故最少的串的数量为(even_cnt,1)。如果没有奇数数量的字符,则全部加到一个串上。在串数定下来之后,便是如何使得最短串最长,那么尽可能地均分剩余元素,使得最短串最长即可。
代码:
题面:
Keep On Movin
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 296 Accepted Submission(s): 219
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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).
[align=left]Output[/align]
For each test case, output an integer denoting the answer.
[align=left]Sample Input[/align]
4
4
1 1 2 4
3
2 2 2
5
1 1 1 1 1
5
1 1 2 2 3
[align=left]Sample Output[/align]
3
6
1
3
[align=left]Author[/align]
zimpha
[align=left]Source[/align]
2016 Multi-University Training Contest 2
题意:
给定多种字符的数量,要求用上全部的字符构建回文串,其中最短的回文串,在所有的方案中最长,求最短回文串长度。
解题:
因为希望最短回文串最长,故肯定希望构建尽量少的回文串,因为奇数个数量的字符肯定得开辟新串,故最少的串的数量为(even_cnt,1)。如果没有奇数数量的字符,则全部加到一个串上。在串数定下来之后,便是如何使得最短串最长,那么尽可能地均分剩余元素,使得最短串最长即可。
代码:
<pre name="code" class="cpp">#include <iostream> #include <cstdio> using namespace std; int main() { int t,n,cnt,even,tmp; scanf("%d",&t); while(t--) { cnt=0; even=0; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&tmp); if(tmp%2) { even++; } cnt+=(tmp/2); } if(even==0) printf("%d\n",cnt*2); else { tmp=cnt/even; printf("%d\n",tmp*2+1); } } return 0; }
相关文章推荐
- PostgreSQL新手入门教程
- Ajax PHP简单入门教程代码
- MySQL入门教程(七)之视图
- Nodejs学习笔记之入门篇
- javascript每日必学之基础入门
- React.js入门学习第一篇
- Bootstrap入门书籍之(五)导航条、分页导航
- Bootstrap入门书籍之(三)栅格系统
- Bootstrap入门书籍之(一)排版
- Javascript 基础---Ajax入门必看
- jQuery入门 构造函数
- jQuery EasyUI 入门必看
- jQuery入门介绍之基础知识
- jQuery基础_入门必看知识点
- 针对初学者的jQuery入门指南
- jquery 指南/入门基础
- JDBCTM 指南:入门
- struts2入门Demo示例
- JSP入门教程(4)
- JSP入门教程(3)