zoukankan      html  css  js  c++  java
  • HDU 4489 The King’s Ups and Downs dp

    题目链接:

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

    The King’s Ups and Downs

    Time Limit: 2000/1000 MS (Java/Others)
    Memory Limit: 32768/32768 K (Java/Others)
    #### 问题描述 > The king has guards of all different heights. Rather than line them up in increasing or decreasing height order, he wants to line them up so each guard is either shorter than the guards next to him or taller than the guards next to him (so the heights go up and down along the line). For example, seven guards of heights 160, 162, 164, 166, 168, 170 and 172 cm. could be arranged as: > or perhaps: > The king wants to know how many guards he needs so he can have a different up and down order at each changing of the guard for rest of his reign. To be able to do this, he needs to know for a given number of guards, n, how many different up and down orders there are: > > For example, if there are four guards: 1, 2, 3,4 can be arrange as: > > 1324, 2143, 3142, 2314, 3412, 4231, 4132, 2413, 3241, 1423 > > For this problem, you will write a program that takes as input a positive integer n, the number of guards and returns the number of up and down orders for n guards of differing heights. #### 输入 > The first line of input contains a single integer P, (1 <= P <= 1000), which is the number of data sets that follow. Each data set consists of single line of input containing two integers. The first integer, D is the data set number. The second integer, n (1 <= n <= 20), is the number of guards of differing heights. #### 输出 > For each data set there is one line of output. It contains the data set number (D) followed by a single space, followed by the number of up and down orders for the n guards.

    样例输入

    4
    1 1
    2 3
    3 4
    4 20

    样例输出

    1 1
    2 4
    3 10
    4 740742376475050

    题意

    给你n个数(1到n),问排出高低高,,,或低高低,,,的所有情况数。

    题解

    考虑最后一个数n摆放的位置,枚举在它左边放x个数,右边放n-1-x个数,然后转移。
    需要用到一个结论:对于任意的n>=2,高低高和低高低的情况数都是相等的!

    代码

    #include<map>
    #include<set>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<ctime>
    #include<vector>
    #include<cstdio>
    #include<string>
    #include<bitset>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<functional>
    using namespace std;
    #define X first
    #define Y second
    #define mkp make_pair
    #define lson (o<<1)
    #define rson ((o<<1)|1)
    #define mid (l+(r-l)/2)
    #define sz() size()
    #define pb(v) push_back(v)
    #define all(o) (o).begin(),(o).end()
    #define clr(a,v) memset(a,v,sizeof(a))
    #define bug(a) cout<<#a<<" = "<<a<<endl
    #define rep(i,a,b) for(int i=a;i<(b);i++)
    #define scf scanf
    #define prf printf
    
    typedef long long LL;
    typedef vector<int> VI;
    typedef pair<int,int> PII;
    typedef vector<pair<int,int> > VPII;
    
    const int INF=0x3f3f3f3f;
    const LL INFL=0x3f3f3f3f3f3f3f3fLL;
    const double eps=1e-8;
    const double PI = acos(-1.0);
    
    //start----------------------------------------------------------------------
    
    const int maxn=22;
    
    LL dp[maxn],C[maxn][maxn];
    
    void get_C(){
        C[0][0]=1;
        for(int i=1;i<maxn;i++){
            C[i][0]=1;
            for(int j=1;j<=i;j++){
                C[i][j]=C[i-1][j-1]+C[i-1][j];
            }
        }
    }
    
    void pre(){
        get_C();
        dp[0]=dp[1]=1;
        dp[2]=2;
        for(int i=3;i<=20;i++){
            for(int x=0;x<i;x++){
                LL lef=x<=1?dp[x]:dp[x]/2;
                int y=i-x-1;
                LL rig=y<=1?dp[y]:dp[y]/2;
                dp[i]+=C[i-1][x]*lef*rig;
            }
        }
    }
    
    int main() {
        pre();
        int tc;
        scanf("%d",&tc);
        while(tc--){
            int kase,n;
            scf("%d%d",&kase,&n);
            prf("%d %lld
    ",kase,dp[n]);
        }
        return 0;
    }
    
    //end-----------------------------------------------------------------------
  • 相关阅读:
    26个新鲜有魅力的自适应网站设计实例
    计划——Redis
    Go语言工具go get的一点问题
    JNDI数据源的配置及使用 (2010-11-21 21:16:43)转载▼
    策略模式
    在Sping的配置文件中,关于dataSource的配置,就我们常用的方法大致可以有三种:
    Spring 中 用 ${xxx} 读取properties文件的说明
    Spring中属性文件properties的读取与使用
    通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
    设置xml以让通知spring 扫描 注解
  • 原文地址:https://www.cnblogs.com/fenice/p/5857642.html
Copyright © 2011-2022 走看看