HDU 4489 The King’s Ups and Downs(组合DP)
作者:Accagain
链接:点击打开链接
原题
The King’s Ups and Downs
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-1个人已经放好了,则有n个位置是可以放人的,第n个人的身高大于前n-1个人的任何人的身高。
设状态dp[i][0]表示有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)相关推荐
- 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 ...
- hdu 4489 The King’s Ups and Downs 组合 递推
题意:给定n个个数1~n.要求他们排成一列满足"波浪型".第一个大于第二个,第二大于第三个,(或者第一个小于第二个,第二个小于第三个..)也就是相对的高矮高矮...或矮高矮高..依 ...
- HDU - 4489 The King’s Ups and Downs(dp)
题目链接 题目大意: 给一个n,求身高为1-n的n个人排成 高低高低高低 或 低高低高低高 这种波浪式的形状有多少种: 思路: 把第n个人插到前n-1个人的序列中,序列中共有n个空位,第n个人一定是 ...
- 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/ ...
- hdu 4489 The King’s Ups and Downs【递推】
题目 http://acm.hdu.edu.cn/showproblem.php?pid=4489 题意 给一个n,求n个高矮不同的人排成一排使得高.矮依次排列的种数. 分析 我们不妨把n个人的身高设 ...
- hdu 4489 The King’s Ups and Downs
看到n<=20我就想暴力打表找出所有情况了:但死在第15组,要花的时间太久了: 然后就想着推嘛:每次加入一个当前情况最大的数:合理的位置就是高低高低高:所以要找前面高低的情况n,后面低高的情况m ...
- HDU 4489 The King's Ups and Downs
题目链接 问的是n个不一样的数,大小交替,或者小大交替的种类数量. n个数,想象成[1,n]的自然数即可. 我们假设大小交替得到的长度为i的排列数为dp1[i],小大交替得到的长度为i的排列数为dp2 ...
- The King’s Ups and Downs (线性DP)
The King's Ups and Downs (线性DP) [link](Problem - 4489 (dingbacode.com)) 题意 给你n个身高不同的人,问你这些人按照波浪形排列有多 ...
- 6177 The King’s Ups and Downs(组合dp)
欢迎访问https://blog.csdn.net/lxt_Lucia-- 宇宙第一小仙女\(^o^)/--萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗-- ---------- ...
最新文章
- 【每日DP】day4 P1417 烹调方案(奇怪的01背包增加了)难度⭐⭐⭐
- table control 光标定位控制
- L1正则化与数据分布的关系
- Linux运维 第三阶段 (二) DHCP
- PandasSQL语法归纳总结,真的太全了
- Linux Qt打包应用程序--利用linuxdeployqt
- Blend设计VSM
- NLP基础|中英文词向量评测理论与实践
- L2-008 最长对称字串 以下标i展开
- xshell连接成功但无法输入命令_如何解决cisco设备无法进入系统问题?
- 斐波那契数列n项的值。(递归和非递归算法Golang实现)
- hive join 数据倾斜 真实案例
- 极域课堂管理系统软件V6.0 2016 豪华版
- Python手写强化学习Q-learning算法玩井字棋
- 七人成团即拼即赚七人拼团模式解析
- 麦当劳肯德基供应商使用变质过期肉:吃不死人
- 9种小程序赚钱方法!看懂的人已经在行动了
- 【PS】图片背景透明化
- Qt 设置Excel单元格文本属性
- 图解Transformer