zoukankan      html  css  js  c++  java
  • hdu 5744 Keep On Movin (2016多校第二场)

    Keep On Movin

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


    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 (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
    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
     
    Author
    zimpha
     

    题意:给出每种字符的个数,组成回文串,找出所有回文串中最小的,要尽量是它最大。

    水题,注意理解题意。没有奇数的字母或者一个奇数的字母则直接连成一个回文串,直接输出所有字母的个数。若出现多(a)个奇数的字母,则分成a个回文串,平分剩下的偶数字母,输出此时最短的回文串数目。

    附上代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 int main()
     5 {
     6     int T,i,j,n,m;
     7     scanf("%d",&T);
     8     while(T--)
     9     {
    10         int a=0,b=0,s=0;
    11         scanf("%d",&n);
    12         for(i=0;i<n;i++)
    13         {
    14             scanf("%d",&m);
    15             if(m%2) a++;  ///记录奇数出现的个数
    16             s+=m;        ///字母的总个数
    17         }
    18         if(!a||a==1)  ///如果只有一组奇数字母组或者没有奇数的字母组,则可以拼成一个回文串
    19         {
    20             printf("%d
    ",s);
    21             continue;
    22         }
    23         b=(s-a)/a/2*2+1; ///出现多组奇数字母组的情况
    24         printf("%d
    ",b);
    25     }
    26     return 0;
    27 }
  • 相关阅读:
    设计模式:备忘录模式(Memento)
    设计模式:中介者模式(Mediator)
    设计模式:迭代器模式(Iterator)
    设计模式:解释器模式(Interpreter)
    设计模式:命令模式(Command)
    设计模式:职责链模式(Chain of Responsibility)
    设计模式:单例模式(单例模式)
    win7硬盘安装方法
    sqlite 附加和分离数据库
    Sqlite 复制表结构和数据
  • 原文地址:https://www.cnblogs.com/pshw/p/5694220.html
Copyright © 2011-2022 走看看