hdu4489 The King’s Ups and Downs (dp)

The King’s Ups and Downs

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


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
 

dp功力太差了,这题推了好久都没推出来,而且方向居然还是错的c[i][j]表示的就是高中学的排列组合i里面挑出j个总共有多少种方法,说一下c[i][j]的一个公式,c[i][j]=c[i-1][j]+c[i-1][j-1],高中学的。也可以直接暴力算,用__int64存也不会爆

说说这个题的思路:求i解的时候,我们把i拿出来,在i-1里面随机取出j个排在前面使其组成末尾为低的符合条件的状态,这样的组合总共有f[i][0]种,另外i-j-1个则组成f[i-j-1][1]种,而在i-1里面随机抽取j个有c[i-1][j]种,所以对于一个确定的i,它的解为sum{f[i][0]*f[i][1]*c[i-1][j]}‘这里的f[i][1]和f[i][0]均为i状态所求解的一半,因为对于每个i,它的所有状态里面总有两个是对称的,所有以高低结尾的数目和以低高结尾的数目都是i的解的一半,所有以高低开头的数目和以低高开头的数目也是i的解的一半。所以这里不需要担心有的状态以高低结尾也以高低开头这样f[i][0]和f[i][1]包含的状态会不会重复的问题,这里是不矛盾的

#include<stdio.h>

int main()
{
	__int64 f[21][2],sum;
	int c[21][21];
	int i,j,k,n,t;
	for(i=1;i<21;i++)
	{
		c[i][0]=c[i][i]=1;
		for(j=1;j<i;j++)
			c[i][j]=c[i-1][j-1]+c[i-1][j];
	}
	f[1][0]=f[1][1]=f[0][0]=f[0][1]=1;//0表示低结尾的
	for(i=2;i<21;i++)
	{
		sum=0;
		for(j=0;j<i;j++)//i-1里面选j个放前面,i-j-1个放后面,再把i插在两个之间
		{
			sum+=f[j][0]*f[i-1-j][1]*c[i-1][j];//c[]表示的是i-1里面抽取j个的排列组合数f[][]是我们要求的值的一半
		}
		f[i][0]=f[i][1]=sum/2;
	}
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&i);
		printf("%d %I64d\n",n,i>1?f[i][0]*2:1);//因为是一半所以这里乘以2
	}
	return 0;
}



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值