HDU 5744 Keep On Movin

来源:互联网 发布:七天网络网页版 编辑:程序博客网 时间:2024/06/05 12:42

2016暑期集训3-F

HDU 5744 Keep On Movin

想法题

传送门:HustOJ

传送门:HDU


题意

N种字母,每种a[i]个。用这些字母组成多个(可以是1个)回文串,但每个字母都要用。每种组合方案中最小长度的回文串的最大值是多少。
比如:

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.


思路

每种回文串想最长,一定是中间一个字母,然后依次往外对称分布字母。所以回文串的个数就是字母个数中奇数的个数,(如果没有奇数,那么偶数个的字幕一定能全拼一起组成一个回文串)。每读一种字母的个数,如果他是奇数,拆出一个1,剩下的拆成2,如果是偶数,那么全拆成2。最后看如果拆出1的个数是0,那么输出偶数个数乘2,这时所有字幕都是偶数个,可以组成一个长的回文串。否则最小长度就是(2*(ou/ji))+1,因为/是向下取整的。


代码

#include<cstdio>#include<algorithm>#include<cstdlib>#include<cstring>#include<cmath>using namespace std;int main ( ){    int n;    scanf ( "%d" , &n );    while ( n-- )    {        int m;        scanf ( "%d" , &m );        int ji = 0 , ou = 0;        for ( int i = 0; i < m; i++ )        {            int temp;            scanf ( "%d" , &temp );            if ( temp % 2 )            {                ji++;                ou += ( ( temp - 1 ) ) / 2;            }            else            {                ou += ( temp / 2 );            }        }        int ans = 1;        if ( ji == 0 )        {            printf ( "%d\n" , 2 * ou );        }        else        {            ans += ( 2 * ( ou / ji ) );            printf ( "%d\n" , ans );        }    }    return 0;}
0 0
原创粉丝点击