hdu 5744 Keep On Movin
2016-07-21 22:26
281 查看
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
如果每个字符出现次数都是偶数, 那么答案显然就是所有数的和. 对于奇数部分, 显然需要把其他字符均匀分配给这写奇数字符. 随便计算下就好了.
如果都是奇数,那结果直接出来了,就是该字符串长度;
否则,把偶数一次分给个个奇数,分成一次,长度加一次,分不成时输出当前长度就行了;
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
int a[100005];
int main(){
int T;
cin>>T;
while(T--){
memset(a,0,sizeof(a));
int n,ans=0,sum=0;
cin>>n;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]%2!=0)ans++;
sum+=a[i];
}
if(ans==0) ans=1;
if(ans==1)
cout<<sum<<endl;
else{
int t=1;
sum-=ans;
while(sum-ans*2>=0){
sum-=ans*2;
t+=2;
}
cout<<t<<endl;
}
}
}
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
如果每个字符出现次数都是偶数, 那么答案显然就是所有数的和. 对于奇数部分, 显然需要把其他字符均匀分配给这写奇数字符. 随便计算下就好了.
如果都是奇数,那结果直接出来了,就是该字符串长度;
否则,把偶数一次分给个个奇数,分成一次,长度加一次,分不成时输出当前长度就行了;
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
int a[100005];
int main(){
int T;
cin>>T;
while(T--){
memset(a,0,sizeof(a));
int n,ans=0,sum=0;
cin>>n;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]%2!=0)ans++;
sum+=a[i];
}
if(ans==0) ans=1;
if(ans==1)
cout<<sum<<endl;
else{
int t=1;
sum-=ans;
while(sum-ans*2>=0){
sum-=ans*2;
t+=2;
}
cout<<t<<endl;
}
}
}
相关文章推荐
- 创业18
- 设计模式之单例模式
- SpringMvc与Struts2的对比
- 2016 Multi-University Training Contest 2 1009 It's All In The Mind
- Tomcat 启动闪退 找不到类 java/lang/Object
- GUI:图形用户界面 界面设计
- neural-networks-and-deep-learning valley2.py
- HDU 5728 PowMod
- CCNA系列八之STP协议
- 【分治】折半查找法
- 创业17
- 面向对象的三大基石(封装,继承和复合,多态)
- 行为的封装
- Spring 通知
- 如何获取chrome视频缓存位置
- error: call to '__open_missing_mode' declared with attribute error:
- CrossDomain.xml的作用及其简单用法
- httpd 2.2 2.4配置
- neural-networks-and-deep-learning valley.py
- PF_INET 和 AF_INET 的区别