HDU4489-The King’s Ups and Downs

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个身高高低不同的士兵,问你把他们按照波浪状排列(高低高或低高低)有多少种排列情况。

解析:
有n个身高不同的人,他们按身高的升序排列。假设我们已经排好了i-1个人,现在要排第i个人,因为这n个人是按升序排列的,所以已经排好的i-1个人的身高都没有第i个人的身高高,它要插入到由前面i-1个人组成的队列中,i-1个人组成的队列中有i个空可以提供给第i个人插入,由于第i个人比前面i-1个人身高都高,所以第i个人插入后必定是波峰,为了满足题意,所以第i个人插入的j位置的前面j-1个人的末尾必然是高低的排列,第j位置的后面的i-j个人的开始必然是低高的排列。我们以dp[j-1][0]表示前面j-1个人的末尾是以高低方式排列的不同排列方法数,以dp[i-j][1]表示第j个位置的后面i-j个人的开始是以低高方式排列的不同排列方法数,所以第i个人插入到第j个位置的不同排列方法数为dp[j-1][0]*dp[i-j][1]*C[i-1][j-1],C[i-1][j-1]表示的是前面j-1个人的不同组合方法:因为前面j-1个人是不确定的,所以就是从i-1个人中选j-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];
		}
	}

代码:

#include<iostream>
#include<cstdio>
#define N 25

using namespace std;

long long dp[N][2];
long long C[N][N];

int main()
{
	int n,T,id;
	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];
		}
	}
	dp[0][0]=dp[1][0]=dp[0][1]=dp[1][1]=1;
	for(int i=2;i<=20;i++)
	{
		long long sum=0;
		for(int j=1;j<=i;j++)
		{
			sum+=dp[j-1][0]*dp[i-j][1]*C[i-1][j-1];
		}
		dp[i][0]=dp[i][1]=sum/2;
	}
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&id,&n);
		if(n==1)
			printf("%d 1\n",id);
		else
			printf("%d %lld\n",id,dp[n][0]*2);
	}
	return 0;
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值