HDU 5744 - Keep On Movin(思维)

针对一组字符数量,构造若干回文串,使最短回文串长度最大化。解析输入输出格式,提供解题思路及AC代码。

Keep On Movin

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1019 Accepted Submission(s): 719

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

题意:
现在这个数组a里有一些字符,第i个字符的数量是a[i]。
构造一些回文串使得各种组合方案中最短字符串长度的最大值。

解题思路:
对于偶数来说,自身就可构成回文串。
对于奇数来说,奇数n可以提取成1和n-1,并将n-1归入偶数。
最后将这些偶数均摊到剩下的若干个1上,使得最小长度最大。

AC代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        int sum = 0;
        int odd = 0;
        while(n--)
        {
            int tmp;
            scanf("%d",&tmp);
            if(tmp & 1)
            {
                odd++;
                sum += tmp>>1;
            }
            else    sum += tmp>>1;
        }
        if(odd > sum)   printf("1\n");
        else
        {
            if(!odd)    printf("%d\n",sum<<1);
            else        printf("%d\n",(sum/odd)<<1|1);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值