HDU 4489 The King’s Ups and Downs(递推)

37 篇文章 0 订阅

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,2,3…,n,那么我们假设前n-1个人已经排好,把这个身高最高为n的人加入队里,如果我们把高看成1,低看成0,构成的排列看成一个01串,那么在他前面的子序列一定以10结尾,后面的子序列一定以01开头。所以设dp(n)为n个人时的解,f[i][0]为长度为i且结尾是10的情况数,f[i][1]是长度为i且开头是01的情况数。则dp[n]=sum{f[i][0]*f[n-1-i]*c[n-1][i]|0<=i<=n},c[n-1][i]是从n-1个人中挑出i个的组合数。注意到开头为01的话如果把它颠倒那么就一定以10结尾,所以f[i][0]=f[i][1]=dp[i],这样就可以递推答案了。
数组c打表

数组dp打表

代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=20;
int a,n;
ll c[maxn+5][maxn+5];
ll f[maxn+5][2],dp[maxn+5];
void init()
{
	c[0][0]=1;
	for (int i=1;i<=maxn;++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];
			//cout<<c[i][j]<<" ";
		}
		//cout<<endl;
	}
	dp[1]=1;
	f[0][0]=f[0][1]=1;
	f[1][0]=f[1][1]=1;
	for (int i=2;i<=maxn;++i)
        {
		dp[i]=0;
		for (int j=0;j<=i;++j)
            {
			dp[i]+=f[j][0]*f[i-1-j][1]*c[i-1][j];// 第 i 个人加入之后的可能排列数
			//cout<<dp[i]<<" ";
		}
		f[i][0]=f[i][1]=dp[i]/2;//010和101正好相反所以/2
		//cout<<endl;
	}
}
int main()
{
	init();
	int t;
	scanf("%d",&t);
	while(t--)
    {
		scanf("%d%d",&a,&n);
		printf("%d %lld\n",a,dp[n]);
	}
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值