zoukankan      html  css  js  c++  java
  • HDU 5744 Keep On Movin 贪心

    Keep On Movin

    题目连接:

    http://acm.hdu.edu.cn/showproblem.php?pid=5744

    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

    Hint

    题意

    有n个字符,每个字符ai个,你需要构造一些字符串,使得这些字符串都是回文串,而且这些回文串恰好用完所有字符

    而且最短的回文串最长,输出这个长度。

    题解:

    贪心去构造,考虑奇数的字符,我们可以拆成偶数+1,那么显然我们知道,奇数的就一定是单独的一行,然后偶数一定要成对的扔进去。

    然后考虑到这些公式就很简单了……

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e5+7;
    int a[maxn];
    void solve(){
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        int num = 0;
        long long ans = 0;
        long long sum = 0;
        long long Ans = 0;
        for(int i=1;i<=n;i++)
        {
            if(a[i]%2==1)num++;
            ans+=a[i];
            Ans+=a[i]/2;
        }
        if(num==0){
            cout<<ans<<endl;
        }else{
            cout<<Ans/num*2+1<<endl;
        }
    }
    int main(){
        int t;
        scanf("%d",&t);
        while(t--)solve();
    }
  • 相关阅读:
    中文词频统计
    复合数据类型,英文词频统计
    字符串、文件操作和英文词频统计预处理
    大数据应用期末总评Hadoop综合大作业
    hadoop平台上HDFS和MAPREDUCE的功能、工作原理和工作过程
    hadoop平台上HDFS和MAPREDUCE的功能、工作原理和工作过程
    分布式文件系统HDFS练习
    安装关系型数据库MySQL和大数据处理框架Hadoop
    爬虫综合大作业
    爬取全部的校园新闻
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5692841.html
Copyright © 2011-2022 走看看