作者:Accagain

链接:点击打开链接

原题

The King’s Ups and Downs

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

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,求n个高矮不同的人排成一排使得高、矮波浪形依次排列的种数。

涉及知识及算法

对于n个人,设其高度分别为1,2,3,,,,,n.
对于第n个人,假设前面的n-1个人已经放好了,则有n个位置是可以放人的,第n个人的身高大于前n-1个人的任何人的身高。
对于任一位置j,左边的j-1个人的排列中,必须满足最后一个人一定是通过身高下降得到的,右边的n-j个人中,最开始的那个人一定通过升高得到后面一个人的。
因为任意两人的高度是不等的,所以人选一定能组成波浪形,所以前面j-1个人的组合情况是C(n-1,j-1)。
设状态dp[i][0]表示有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 <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
ll dp[30][2];
ll sum[30];
//求组合数
ll C(int a,int b)
{if(b==0) return 1;ll res=1;for(int i=0;i<b;i++)res*=(a-i);for(int i=1;i<=b;i++)res/=i;return res;
}int main()
{//freopen("in.txt","r",stdin);dp[0][0]=dp[0][1]=1;dp[1][0]=dp[1][1]=1;sum[1]=1;for(int i=2;i<=20;i++){for(int j=1;j<=i;j++)sum[i]+=(dp[j-1][0]*dp[i-j][1]*C(i-1,j-1));//由对称性dp[i][0]=dp[i][1]=sum[i]/2;}int t,d,k;scanf("%d",&t);while(t--){scanf("%d%d",&d,&k);printf("%d %lld\n",d,sum[k]);}return 0;
}

HDU 4489 The King’s Ups and Downs(组合DP)相关推荐

  1. HDU 4489 The King’s Ups and Downs 组合DP

    The King's Ups and Downs HDU - 4489 给一个整数 nnn,求 {1,⋯,n}\{1,\cdots,n\}{1,⋯,n} 符合以下条件的的排列数: 设 a1,⋯,ana ...

  2. hdu 4489 The King’s Ups and Downs 组合 递推

    题意:给定n个个数1~n.要求他们排成一列满足"波浪型".第一个大于第二个,第二大于第三个,(或者第一个小于第二个,第二个小于第三个..)也就是相对的高矮高矮...或矮高矮高..依 ...

  3. HDU - 4489 The King’s Ups and Downs(dp)

    题目链接 题目大意: 给一个n,求身高为1-n的n个人排成 高低高低高低  或 低高低高低高 这种波浪式的形状有多少种: 思路: 把第n个人插到前n-1个人的序列中,序列中共有n个空位,第n个人一定是 ...

  4. HDU - 4489 The King’s Ups and Downs (排列组合+dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4489点击打开链接 The King's Ups and Downs Time Limit: 2000/ ...

  5. hdu 4489 The King’s Ups and Downs【递推】

    题目 http://acm.hdu.edu.cn/showproblem.php?pid=4489 题意 给一个n,求n个高矮不同的人排成一排使得高.矮依次排列的种数. 分析 我们不妨把n个人的身高设 ...

  6. hdu 4489 The King’s Ups and Downs

    看到n<=20我就想暴力打表找出所有情况了:但死在第15组,要花的时间太久了: 然后就想着推嘛:每次加入一个当前情况最大的数:合理的位置就是高低高低高:所以要找前面高低的情况n,后面低高的情况m ...

  7. HDU 4489 The King's Ups and Downs

    题目链接 问的是n个不一样的数,大小交替,或者小大交替的种类数量. n个数,想象成[1,n]的自然数即可. 我们假设大小交替得到的长度为i的排列数为dp1[i],小大交替得到的长度为i的排列数为dp2 ...

  8. The King’s Ups and Downs (线性DP)

    The King's Ups and Downs (线性DP) [link](Problem - 4489 (dingbacode.com)) 题意 给你n个身高不同的人,问你这些人按照波浪形排列有多 ...

  9. 6177 The King’s Ups and Downs(组合dp)

    欢迎访问https://blog.csdn.net/lxt_Lucia-- 宇宙第一小仙女\(^o^)/--萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗-- ---------- ...

最新文章

  1. 【每日DP】day4 P1417 烹调方案(奇怪的01背包增加了)难度⭐⭐⭐
  2. table control 光标定位控制
  3. L1正则化与数据分布的关系
  4. Linux运维 第三阶段 (二) DHCP
  5. PandasSQL语法归纳总结,真的太全了
  6. Linux Qt打包应用程序--利用linuxdeployqt
  7. Blend设计VSM
  8. NLP基础|中英文词向量评测理论与实践
  9. L2-008 最长对称字串 以下标i展开
  10. xshell连接成功但无法输入命令_如何解决cisco设备无法进入系统问题?
  11. 斐波那契数列n项的值。(递归和非递归算法Golang实现)
  12. hive join 数据倾斜 真实案例
  13. 极域课堂管理系统软件V6.0 2016 豪华版
  14. Python手写强化学习Q-learning算法玩井字棋
  15. 七人成团即拼即赚七人拼团模式解析
  16. 麦当劳肯德基供应商使用变质过期肉:吃不死人
  17. 9种小程序赚钱方法!看懂的人已经在行动了
  18. 【PS】图片背景透明化
  19. Qt 设置Excel单元格文本属性
  20. 图解Transformer

热门文章

  1. 黑马程序员---java基础-Java之IO
  2. matlab 线性最小二乘法,matlab_最小二乘法线性和非线性拟合.ppt
  3. 2017,那些引发关注的新建展馆
  4. python骰子游戏分析_两个骰子的Python概率骰子游戏
  5. php 获取一年有多少天,【后端开发】php获取一年有多少天
  6. 台湾省应广科技PMS150C纯IO PWM单片机MCU
  7. latex和word文档互相转换
  8. 如何解决读写txt文件中文乱码问题
  9. Oracle数据库(二) 表空间的管理
  10. 开源建站系统的开源组件风险