hdu4489 The King’s Ups and Downs

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

分析:
一道题不会做,我们的原则是什么:

打~表~找~规~律~

n=1 : 1
n=2 : 2
n=3 : 4
n=4 : 10
n=5 : 32
n=6 : 122
n=7 : 544
n=8 : 2770
n=9 : 15872
n=10: 101042

但是并没有看出什么,
我们还是需要化繁为简,
如果现在我们有i个高度,已经排好了i-1个高度,
现在需要放第i个高度(也就是最高的)
因为是最高的,所以不管放在哪,ta都是山峰
那ta的前面一定是 高低
后面一定是 低高
只有这样才能符合波浪形的限制

设f[i][0]表示长度为i且结尾是高低的序列数
f[i][1]表示长度为i且开头是低高的序列数

假设我们现在要求长度为i的序列数,
枚举第i个的插入位置j

ans[i]+=f[j][0]*f[i-j-1][1]*C(i-1,j)

C(i-1,j)是组合数(从i-1中取出j个)

那么现在的问题就变成了f[i][0/1]怎么求
我们把一个序列抽象成01串,1表示山峰,0表示山谷

i个高度排好后无非两种情况
开始为低高或开始为高低,那么排列的逆序也满足条件,
也就是说结尾为高低的方法数和开始为低高的方法数相同
而对于人数一定的情况,开始为低高的人数和开始为高低的人数相等

证明:
当n为偶数时:假设波峰开始的序列为1010.那么把它倒置一下就变成了0101了
也就是说每一个1打头的对应着一个0打头的

当n为奇数时:假设波峰开始的序列为10101.
假设第一个数大于最后一个数,那我们把序列最后一个数放到最前序列就变成01010.
如果第一个数小于最后一个数把第一个数放到最后就行了

所以每一个1打头的对应着一个0打头的

所以f[i][0]=f[i][1]=ans[i]/2;

这样就可以完成递推了

tip

进阶版

这里写代码片
#include<cstdio>
#include<cstring>
#include<iostream>
#define ll long long

using namespace std;

int n,nn,p;
ll jc[40];
ll f[40][2],ans[40];

void cl(int n)
{
    int i;
    ll t=1;
    for (i=1;i<=n;i++){
        t=(t*(ll)i);jc[i]=t;
    }
}

ll C(ll n,ll m)
{
    if (n<m) return 0;
    if (n==m) return 1;
    return jc[n]/(jc[m]*jc[n-m]);
}

int main()
{
    int T;
    scanf("%d",&T);
    cl(21); jc[0]=1; n=2;
    f[0][0]=f[0][1]=1;
    ans[1]=1; f[1][0]=f[1][1]=1;
    ans[2]=2; f[2][0]=f[2][1]=1;
    while (T--)
    {
        scanf("%d%d",&p,&nn);
        int i,j,k;
        if (nn>n)
        {
            for (i=n+1;i<=nn;i++)
            {
                for (j=0;j<i;j++)
                {
                    ll r=C(i-1,j);
                    ans[i]=(ans[i]+f[j][0]*f[i-j-1][1]*r);
                }
                f[i][0]=f[i][1]=ans[i]/2;
            }
            n=nn;
        }
        printf("%d %lld\n",p,ans[nn]);
    }
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值