HDU5744 Keep On Movin (思维题,水题)
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.
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).
Sample Input Sample Output
题意:
开始输入T,有T组测试样例,每组测试样例给定n个数,代表n个字符的个数,每个数代表的字符是唯一确定的,将这些字符组成若干个回文串(可以一个,也可以多个),找出所有组合方式中最短 回文串 的最长长度。
比如第一组样例 一共有4个字符,假定为ABCD,其中A有1个,B有1个,C有2个,D有4个。组成的回文有{(CAC)(DDBDD)}或者{(A)(B)(CC)(DDDD)}等多种方式,其中第一种情况中,最短的长度为3,所以答案为3。再看题意中给出的样例,一共有4个字符,假定为ABCD,其中A有2个,B有3个,C有2个,D有2个。组成的回文有{"acdbbbdca"}, {"abbba", "cddc"}, {"aca", "bbb", "dcd"}, {"acdbdca", "bb"}。在第一种情况中,最短的长度为9,所以输出为9。
思路:
如果是单个的,想构成较长的回文,至少有一对字符来和他组成,组成为ABA。
所以,统计所有的字符中奇数的1的个数和偶数2的对数,比如一个字符有5个,则表示为2对偶数,1个奇数。
最后判断奇数和偶数的数量关系,如果奇数为0,偶数不为0,输出偶数*2(因为偶数是对数,不是个数),如果奇数偶数都不为0,将偶数平均分配到奇数中,表示一个奇数对应这n对偶数,输出(偶数对数/奇数个数)*2+1。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int a[];
int main()
{
int n;
int m;
cin>>m;
for(int j=; j<m; j++)
{
cin>>n;
for(int i=; i<n; i++)
cin>>a[i];
int ou=,ji=;
for(int i=; i<n; i++)
{ if(a[i]%==)
ji++;
ou+=a[i]/;
}
if(ji==&&ou!=)
cout<<ou*<<endl;
else if(ji==&&ou==)
cout<<<<endl;
else
{
int ans=ou/ji;
cout<<ans*+<<endl;
}
}
return ;
}
HDU5744 Keep On Movin (思维题,水题)的更多相关文章
- hihocoder 1322 - 树结构判定 - [hiho一下161周][模板题/水题]
题目链接:http://hihocoder.com/problemset/problem/1322 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个包含 N 个顶 ...
- ZOJ 2679 Old Bill ||ZOJ 2952 Find All M^N Please 两题水题
2679:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1679 2952:http://acm.zju.edu.cn/onli ...
- bzoj2296: 【POJ Challenge】随机种子(思维题/水题)
有点类似CF某场div2T1... 前面接上1234567890000000,后面加上x+(1234567890000000%x)就可以保证是x的倍数了 #include<iostream> ...
- codeforces 828 D. High Load(思维,水题)
题目链接:http://codeforces.com/contest/828/problem/D 题解:任意去一个点为根然后有几个k就是几个子叶也就是根结点有几个分支然后最好的解法就是贪心,将剩下的点 ...
- hdu-5744 Keep On Movin(思维)
题目链接: Keep On Movin Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- ZOJ 2819 Average Score 牡丹江现场赛A题 水题/签到题
ZOJ 2819 Average Score Time Limit: 2 Sec Memory Limit: 60 MB 题目连接 http://acm.zju.edu.cn/onlinejudge ...
- hdu - 6276,2018CCPC湖南全国邀请赛A题,水题,二分
题意: 求H的最大值, H是指存在H篇论文,这H篇被引用的次数都大于等于H次. 思路:题意得, 最多只有N遍论文,所以H的最大值为N, 常识得知H的最小值为0. 所以H的答案在[0,N]之间,二分 ...
- HDU 2674 N!Again(数学思维水题)
题目 //行开始看被吓一跳,那么大,没有头绪, //看了解题报告,发现这是一道大大大的水题,,,,,//2009 = 7 * 7 * 41//对2009分解,看它有哪些质因子,它最大的质因子是41,那 ...
- HDOJ/HDU 1256 画8(绞下思维~水题)
Problem Description 谁画8画的好,画的快,今后就发的快,学业发达,事业发达,祝大家发,发,发. Input 输入的第一行为一个整数N,表示后面有N组数据. 每组数据中有一个字符和一 ...
随机推荐
- ecshop循环计数
循环依次递增+1 <!-- {foreach from=$comments item=comment name=comment} --> {$smarty.foreach.comment. ...
- php几行代码实现CSV格式文件输出
//适用于不需要设置格式简单将数据导出的程序,多多指教...... $str .= 'pro_code'.','.'words'.'\n';//首先写入表格标题栏 foreach($is_error ...
- [leetcode-583-Delete Operation for Two Strings]
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...
- 【LeetCode】66. Plus One
题目: Given a non-negative number represented as an array of digits, plus one to the number. The digit ...
- Linux编程之epoll
现在有这么一个场景:我是一个很忙的大老板,我有100个手机,手机来信息了,我的秘书就会告诉我"老板,你的手机来信息了."我很生气,我的秘书就是这样子,每次手机来信息就只告诉我来信息 ...
- 解决发http get请求的时候不成功,出现android.os.NetworkOnMainThreadException的异常
问题描述:在接游戏sdk的时候,由于游戏要求购买的时候是在主线程里面进行的,但是发http请求是不能在主线程里面发,否则就会出现android.os.NetworkOnMainThreadExcept ...
- xUtils使用详细介绍
xUtils3使用详解 一.xUtils简介: xUtils是基于Afinal开发的目前功能比较完善的一个Android开源框架,官网:https://github.com/wyouflf/xUtil ...
- AngularJS4.0环境搭建详解--windows系统环境
第一步:安装NodeJS 下载最新版的NodeJS并安装,安装完成后打开CMD命令行,输入以下命令: node -v 若返回类似版本号则代表NodeJS安装成功,如下: 第二部 安装npm 新版的No ...
- Your password does not satisfy the current policy requirements
创建用户,做测试想设置一个简单的密码.报错: 大概是MySQL5.7搞事情,默认安装了validate_password插件. mysql> SHOW VARIABLES LIKE 'valid ...
- C# 中关于接口实现、显示实现接口以及继承
先列出我写的代码: 接口以及抽象类.实现类 public interface IA { void H(); } public interface IB { void H(); } public abs ...