杭电5744之Keep On Movin

来源:互联网 发布:csp软件下载 编辑:程序博客网 时间:2024/06/07 06:50
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.
 

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(1n105) -- the number of kinds of characters. The second line contains n integers a1,a2,...,an(0ai104).
 

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
题意:输入m组测试案例,每组测试案例,输入一个数n,表示n组,每组输入一个数a[i],表示某个字符有a[i]个,各个字符互不相同;要求把所有字符分成若干份对称的字符串,有多种情况,每种情况取最小长度的字符串的长度,每个案例最终结果为所有情况的最大值
案例分析如下:
{2,3,2,2}
{"acdbbbdca"} 9
{"abbba", "cddc"} 4
{"aca", "bbb", "dcd"} 3
{"acdbdca", "bb"} 2
每种情况取最短字符串的长度,最终结果取最大值9


分析:此题为纯思维题,有几个奇数就分几组,若某奇数大于1,该数减去1之后,剩余的数当做偶数处理,然后把所有的偶数对,尽可能地平均分给这些组,结果为所有组数的最小值;若无奇数即全为偶数,结果就是所有数相加!

AC代码如下:

#include "iostream"using namespace std;int a[100000];int main(){    int m,n,i,sum,flag,count;    cin>>m;    while(m--)    {        cin>>n;        sum=0;//所有偶数对之和        count=0;//奇数个数,即  分为count组        flag=1;//标记是否全为偶数        for (i=0;i<n;i++)        {            cin>>a[i];            if (a[i]%2==0)            {                sum+=a[i];            }            else            {                flag=0;                count++;                sum+=(a[i]-1);            }        }        sum/=2;  //偶数对的数量        if (flag)  //判断是否全为偶数        {            cout<<sum*2<<endl;        }        else    //把偶数对尽可能均分给各组        {            if (sum<count)//偶数对少于组数,不够分            {                cout<<1<<endl;            }            else   //够分            {                cout<<1+sum/count*2<<endl;            }        }    }    return 0;}


 
0 0
原创粉丝点击