POJ 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 (Java/Others)
Total Submission(s): 237    Accepted Submission(s): 150



Problem Description
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
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   4769  4768  4767  4766  4765 
 

整理的有些晚


/*************************************************************************
	> File Name: F.cpp
	> Author: BSlin
	> Mail:  
	> Created Time: 2013年10月07日 星期一 20时21分28秒
 ************************************************************************/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iterator>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#define MP make_pair
#define INF (1<<30)
#define PI acos(-1.0)
#define esp 1e-8
const int dx[4]={0,0,0,0};
using namespace std;
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#if defined (_WIN32) || defined (__WIN32) || defined (WIN32) || defined (__WIN32__)
#define LL __int64
#define LLS "%" "I" "6" "4" "d"
#else
#define LL long long
#define LLS "%" "l" "l" "d"
#endif



LL C[25][25];
LL dp[25];

void yanghui() {
    C[0][0] = 1;
    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-1] + C[i-1][j];
        }
    }
}

void init() {
    yanghui();
    LL sum;
    dp[0] = 1;
    dp[1] = 1;
    for(int i=2; i<=20; i++) {
        sum = 0;
        for(int j=0; j<i; j++) {
            sum += dp[j] * dp[i-1-j] * C[i-1][j];
        }
        dp[i] = sum / 2;
        //printf(LLS"\n",dp[i]);
    }
}

int main(int argc, char** argv) {
    int t,num,n;
    init();
    dp[1] = 1;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%d",&num,&n);
        if(n == 1) printf("%d 1\n",num);
        else 
            printf("%d "LLS"\n",num,dp[n] * 2);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值