The King’s Ups and Downs_组合dp

原创 2018年04月16日 21:05:17
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.

Input 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.
Output 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.
Sample Input
4
1 1
2 3
3 4
4 20
Sample Output
1 1
2 4
3 10
4 740742376475050
#include<bits/stdc++.h>
#define ll long long
using namespace std;

ll c[25][25],d[25][2];

void init(){
    for(int i=1;i<=20;i++){
        c[i][0]=c[i][i]=1;
        for(int j=1;j<i;j++)
        c[i][j]=c[i-1][j]+c[i-1][j-1];
    }
    d[0][1]=d[0][0]=d[1][0]=d[1][1]=1;
    for(int i=2;i<=20;i++){
        ll cur=0;
        for(int j=0;j<i;j++){
            cur+=d[j][0]*d[i-j-1][1]*c[i-1][j];
        }
        d[i][0]=d[i][1]=cur/2;
    }
}


int main()
{
    int n,cnt;
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    init();
    int t;
    cin>>t;
    while(t--){
        int id;
        scanf("%d%d",&id,&n);
        if(n==1)printf("%d 1\n",id);
        else printf("%d %lld\n",id,d[n][0]*2);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ujn20161222/article/details/79966531

【HDU】【The King’s Ups and Downs】

The King’s Ups and Downs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav...
  • u013200703
  • u013200703
  • 2015-07-14 20:14:23
  • 407

hdu 4489 The King’s Ups and Downs 组合 递推

题意:给定n个个数1~n。要求他们排成一列满足“波浪型”。第一个大于第二个,第二大于第三个,(或者第一个小于第二个,第二个小于第三个。。)也就是相对的高矮高矮。。。或矮高矮高。。依此类推。求排列总方案...
  • chen_minghui
  • chen_minghui
  • 2017-06-07 21:05:15
  • 274

HDOJ 题目4489 The King’s Ups and Downs(组合DP打表)

The King’s Ups and Downs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav...
  • yu_ch_sh
  • yu_ch_sh
  • 2015-09-16 00:06:14
  • 303

Uva 6177 The King's Ups and Downs

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&...
  • niuox
  • niuox
  • 2013-04-29 14:55:28
  • 1596

hdu4489 The King’s Ups and Downs

Problem Description The king has guards of all different heights. Rather than line them up in incre...
  • wu_tongtong
  • wu_tongtong
  • 2017-08-31 08:43:27
  • 319

UVALive 6177|HDU 4489|The King's Ups and Downs|动态规划

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&probl...
  • huanghongxun
  • huanghongxun
  • 2017-11-05 10:27:55
  • 237

HDU 4489 The King’s Ups and Downs(DP + 组合数)

题目链接:点击打开链接 思路:我们不妨把n个人的身高设为1~n, 然后从低到高插入队列。 那么将第i个人插入队列的时候就出现了问题, 插入的这个位置需要满足前面两个是高低, 后面两个是低高。 所以...
  • weizhuwyzc000
  • weizhuwyzc000
  • 2016-04-11 16:44:28
  • 811

组合dp hdu-4489-The King’s Ups and Downs

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4489 题目意思: 给一个n,求n个高矮不同的人排成一排使得高、矮依次排列的种数。 解题思路: 组合...
  • cc_again
  • cc_again
  • 2013-08-12 10:54:25
  • 2905

(POJ) 3682 - King Arthur's Birthday Celebration

亚瑟王掷一枚硬币,概率p正面向上,概率1-p反面朝上,现在亚瑟王要掷k次正面朝上,第i次掷硬币时花费2∗i−1。问:期望要掷多少枚硬币才能达到k次正面朝上,以及达到k次正面朝上时的花费。...
  • qq_35504607
  • qq_35504607
  • 2017-03-03 15:57:08
  • 254

hdu 4489 The King’s Ups and Downs

看到n
  • zhjcezh
  • zhjcezh
  • 2017-08-21 16:22:34
  • 101
收藏助手
不良信息举报
您举报文章:The King’s Ups and Downs_组合dp
举报原因:
原因补充:

(最多只允许输入30个字)