HDU5744 Keep On Movin(貪心)

HDU5744 Keep On Movin(貪心)

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


題目

Time Limit:2000MS Memory Limit:65536KB
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


題意

告訴你有n個字符,然後給出你每個字符的數量,你來用他們組成若干字符串,保證每個字符串都是迴文串。問你如何組字符串,可以使他們中最短的那串的長度最長。


分析

對於數量爲1的字符,我們可以用2個相同字符來“嵌套”它。如{a,b和b}可以形成bab。故每2個相同字符可以形成一個“嵌套”。而大於1的奇數則可以轉化爲1個字符和若干對嵌套層。
故問題轉化爲計算嵌套層和單獨字符數量的問題。見代碼


源碼

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<sstream>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<utility>
#include<sstream>
#define mem0(x) memset(x,0,sizeof x)
#define mem1(x) memset(x,-1,sizeof x)
#define dbug cout<<"here"<<endl;
//#define LOCAL

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e6+10;
const int MOD = 1000000007;

int main(){
    #ifdef LOCAL
        freopen("C:\\Users\\asus-z\\Desktop\\input.txt","r",stdin);
        freopen("C:\\Users\\asus-z\\Desktop\\output.txt","w",stdout);
    #endif
    int t;
    int n;
    cin >> t;
    while(t--){
        cin >> n;
        int tmp;
        int cnt1 = 0;
        int sumList = 0;
        for(int i = 1; i <= n; ++i){
            cin >> tmp;
            if(tmp == 1)
                cnt1++;
            else if(!(tmp & 1))
                sumList += tmp/2;
            else{
                sumList += tmp/2;
                cnt1++;
            }
        }
        if(cnt1 == 0)
            cout << sumList*2 << endl;
        else if(cnt1 > sumList)
            cout << 1 << endl;
        else
            cout << 1+2*(sumList/cnt1) << endl;
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章