The King’s Ups and Downs

本文介绍了一个有趣的问题:国王希望他的卫兵在换岗时形成独特的身高起伏序列。文章详细解析了如何通过动态规划的方法来计算特定数量的卫兵能够形成的起伏序列总数,包括核心算法的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
组合dp.

对于n个人,设其高度分别为1,2,3,,,,,n.  
对于第n个人,假设前面的n-1个人已经放好了。第n个人有n个位置可放,对于任一位置j,
显然第n个人的身高n大于前n-1个人的任何人的身高。所以第n个人左边的j-1个人的排列
中,必须满足最后一个人一定是通过身高下降得到的,右边的n-j个人中,最开始的那个
人一定通过升高得到后面一个人的,当然前面j-1个人可选C(n-1,j-1)。
所以设状态dp[i][0]表示有i个人,并且第一个人通过上升得到第二个人的总的排列种数
,i个人身高肯定不一样,故只考虑个数。
dp[i][1]表示有i个人,并且最后一个人是通过下降得到的。
很显然在人数相同的情况下,由对称性得 dp[i][0]=dp[i][1]=sum[i]/2 sum[i]为i
个人总的满足要求的排列数。
对于第n个人放到位置j ,有C(n-1,j-1)*dp[j-1][0]*dp[n-j][1]种情况。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll ans[25],dp[25][2],c[25][25];
void zuhe()
{
	int i,j;
	for (i=0;i<=20;i++)
		c[i][0] = 1;
	for (i=1;i<=20;i++)
		for (j=1;j<=20;j++)
			c[i][j] = c[i-1][j] + c[i-1][j-1];
}
void listt()
{
	int k,i,j;
	ans[1] = 1;
	dp[0][0] = dp[0][1] = 1;
	dp[1][0] = dp[1][1] = 1;
	for (k=2;k<=20;k++)
	{
		ll cnt = 0;
		for (i=0;i<k;i++)
		{
			j = k - i - 1;
			cnt += dp[i][0]*dp[j][1]*c[k-1][i];
		}
		ans[k] = cnt;
		dp[k][0] = dp[k][1] = cnt / 2;
	}
}
int main()
{
	zuhe();
	listt();
	int t;
	scanf("%d",&t);
	while (t--)
	{
		ll cas,n;
		scanf("%lld %lld",&cas,&n);
		printf("%lld %lld\n",cas,ans[n]);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值