hdu4489 The King’s Ups and Downs

108 篇文章 0 订阅

The King’s Ups and Downs

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 163 Accepted Submission(s): 101


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
唉,不想,说我的dp了,当时花了好长时间,也没推出递推公式来,光是纠结在大小小大上是有几个错误上,其实,几个排错,并没有定论,因为,大小本来就是相对的,……。 我们用answer[i][0]表示是以低结尾,answer[i][1]表示是以高结尾,我们怎么求出最后的结果呢?我们想,我们最后加入的一定是最高的,那么它周围一定要是
以低结尾,右边一定是以低开头的,那么i个人总方法数是,sum{dp[j][0]*dp[n-j-1]*c[i,j]};这个式子是怎么得来的呢?你想,我们在i-1个人中选出j个,这些人一定是分高低的,那么就一定,可以组成以低结尾和以高开头的数目,这其实在前面已经求出,两者想乘不就是总的个数了么;因为,以低结尾和以高结尾的就是须序变了,一定,是相等的,这样就可以更新了,这题就要以a了!
answer[i][0],和answer[i][1];
#include<stdio.h>
#include<iostream>
using namespace std;
__int64 answer[30][2];
__int64 pow(int n)
{
    if(n==0)
    return 1;
    __int64 sum=1,i;
    while(n)
    {
        sum*=n;
        n--;
    }
    return sum;
}
__int64 fc(int n,int m)
{
    return pow(n)/pow(m)/pow(n-m);
}
int main()
{
    int n,i,j,tcase,t;
    scanf("%d",&tcase);
    while(tcase--)
    {
        scanf("%d%d",&t,&n);
        if(n==1)
        {
            printf("%d 1\n",t);
            continue;
        }
        answer[0][0]=1;answer[0][1]=1;
        answer[1][0]=1;answer[1][1]=1;
        answer[2][1]=1;answer[2][0]=1;
        for(i=3;i<=n;i++)
        {
            __int64 temp=0;
            for(j=0;j<i;j++)
            {
                temp+=answer[j][0]*answer[i-j-1][1]*fc(i-1,j);
            }
            answer[i][0]=answer[i][1]=temp/2;
            //printf("%d %I64d %I64d\n",i,answer[i][0],answer[i][1]);
        }
        printf("%d %I64d\n",t,answer[n][0]*2);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值