zoukankan      html  css  js  c++  java
  • hdu-5744 Keep On Movin(思维)

    题目链接:

    Keep On Movin

    Time Limit: 4000/2000 MS (Java/Others)   

     Memory Limit: 65536/65536 K (Java/Others)


    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
     
    题意:
     
    给出每种字符有多少个,现在要你全部用完,拼成回文串,问回文串最短的那串的最长长度是多少;
     
    思路:
     
    每俩个相同的字符就可以加在任意的串上,要使最短的最长,所以就要平均加;
    具体的看代码吧;
     

     AC代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <bits/stdc++.h>
    #include <stack>
    
    using namespace std;
    
    #define For(i,j,n) for(int i=j;i<=n;i++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    
    typedef  long long LL;
    
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
    
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const int inf=1e9;
    const int N=1e5+10;
    const int maxn=500+10;
    const double eps=1e-9;
    
    int a[N];
    
    int main()
    {
            int t;
            read(t);
            while(t--)
    		{
    			int n,num=0,sum=0;
    			read(n);
    			For(i,1,n)
    			{
    				read(a[i]);
    				if(a[i]&1)num++;
    				sum+=a[i]/2;
    			}
    			if(num)cout<<1+sum/num*2<<"
    ";
    			else cout<<sum*2<<"
    ";
    			//if(sum%num==0)cout<<1+sum/num*2<<endl;
    			//else cout<<1+sum
    
    		}
            return 0;
    }
    

      

  • 相关阅读:
    LeetCode 258. Add Digits
    LeetCode 257. Binary Tree Paths
    LeetCode 周赛 184
    js算法初窥05(算法模式02-动态规划与贪心算法)
    js算法初窥04(算法模式01-递归)
    js算法初窥03(搜索及去重算法)
    js算法初窥02(排序算法02-归并、快速以及堆排序)
    js算法初窥01(排序算法01-冒泡、选择、插入)
    用js来实现那些数据结构16(图02-图的遍历)
    用js来实现那些数据结构15(图01)
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5692963.html
Copyright © 2011-2022 走看看