题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=4489

The King’s Ups and Downs

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
#### 问题描述
> 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.
#### 输入
> The first line of input contains a single integer P, (1 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.

样例输入

4

1 1

2 3

3 4

4 20

样例输出

1 1

2 4

3 10

4 740742376475050

题意

给你n个数(1到n),问排出高低高,,,或低高低,,,的所有情况数。

题解

考虑最后一个数n摆放的位置,枚举在它左边放x个数,右边放n-1-x个数,然后转移。

需要用到一个结论:对于任意的n>=2,高低高和低高低的情况数都是相等的!

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=22; LL dp[maxn],C[maxn][maxn]; void get_C(){
C[0][0]=1;
for(int i=1;i<maxn;i++){
C[i][0]=1;
for(int j=1;j<=i;j++){
C[i][j]=C[i-1][j-1]+C[i-1][j];
}
}
} void pre(){
get_C();
dp[0]=dp[1]=1;
dp[2]=2;
for(int i=3;i<=20;i++){
for(int x=0;x<i;x++){
LL lef=x<=1?dp[x]:dp[x]/2;
int y=i-x-1;
LL rig=y<=1?dp[y]:dp[y]/2;
dp[i]+=C[i-1][x]*lef*rig;
}
}
} int main() {
pre();
int tc;
scanf("%d",&tc);
while(tc--){
int kase,n;
scf("%d%d",&kase,&n);
prf("%d %lld\n",kase,dp[n]);
}
return 0;
} //end-----------------------------------------------------------------------

HDU 4489 The King’s Ups and Downs dp的更多相关文章

  1. HDU 4489 The King's Ups and Downs

    HDU 4489 The King's Ups and Downs 思路: 状态:dp[i]表示i个数的方案数. 转移方程:dp[n]=∑dp[j-1]/2*dp[n-j]/2*C(n-1,j-1). ...

  2. hdu 4489 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 ...

  3. HDU 4489 The King’s Ups and Downs

    http://acm.hdu.edu.cn/showproblem.php?pid=4489 题意:有n个身高不同的人,计算高低或低高交错排列的方法数. 思路:可以按照身高顺序依次插进去. d[i][ ...

  4. HDU 4489 The King’s Ups and Downs (DP+数学计数)

    题意:给你n个身高高低不同的士兵.问你把他们按照波浪状排列(高低高或低高低)有多少方法数. 析:这是一个DP题是很明显的,因为你暴力的话,一定会超时,应该在第15个时,就过不去了,所以这是一个DP计数 ...

  5. HDU 4055 The King’s Ups and Downs(DP计数)

    题意: 国王的士兵有n个,每个人的身高都不同,国王要将他们排列,必须一高一矮间隔进行,即其中的一个人必须同时高于(或低于)左边和右边.问可能的排列数.例子有1千个,但是最多只算到20个士兵,并且20个 ...

  6. UVALive 6177 The King's Ups and Downs

    The King's Ups and Downs Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UV ...

  7. The King’s Ups and Downs(HDU 4489,动态规划递推,组合数,国王的游戏)

    题意: 给一个数字n,让1到n的所有数都以波浪形排序,即任意两个相邻的数都是一高一低或者一低一高 比如:1324   4231,再比如4213就是错的,因为4高,2低,接下来1就应该比2高,但是它没有 ...

  8. The King’s Ups and Downs

    有n个高矮不同的士兵,现在要将他们按高,矮依次排列,问有多少种情况. 化简为 n个人,求出可以形成波浪形状的方法数 #include <iostream> #include <cma ...

  9. hdu 4055 && hdu 4489 动态规划

    hdu 4055: 一开始我想的递推方向想得很复杂,看了别人的博客后才醍醐灌顶: 参照他的思路和代码: #include<cstdio> #include<cstring> # ...

随机推荐

  1. 创建Podfile,添加类库,中途添加库指令

    前提是你电脑已经安装了CocoaPods 1.打开终端 2.进入你的工程目录  cd /Users/...../CocoaPodsDemo  3. 创建Pods文件  touch Podfile 新建 ...

  2. flask中请求勾子

    请求勾子 在客户端和服务器交互的过程中,有些准备工作或扫尾工作需要处理,比如: *在请求开始时,建立数据库连接; *在请求开始时,根据需求进行权限校验; *在请求结束时,指定数据的交互格式; 为了让每 ...

  3. 帝国CMS给会员注册加入问答验证

    修改文件有e/enews/index.php //注册 elseif($enews=="register") { if($_POST['ask']=='帝国软件') { $user ...

  4. Unity判断鼠标是否在UI(UGUI)上

    "EventSystem.current.IsPointerOverGameObject()" UI和3D场景同时都需要响应触摸事件,如果同时响应可能就会出现触摸UI的时候影响到了 ...

  5. Hbase(1)-MySQL海量数据存储的启发

    宽表拆分 有一张user表,记录了用户的信息,,如果表中的列有很多,就称之为宽表,为了提升效率,会进行垂直拆分 拆分后 将用户的信息分为基本信息和其他信息,页面一开打就需要展示的信息为基本信息,其他信 ...

  6. 【笔记学习】Linux系统与虚拟机学习

    Part 1 : 基于VirtualBox虚拟机安装Ubuntu 问题剪辑 --给一开始未知的我的科普指南 1. VirtualBox不能创建64位虚拟机 解决办法: 开启虚拟化技术 详细:重启电脑, ...

  7. 2017-2018-1 20155321《信息安全技术》实验二——Windows口令破解

    2017-2018-1 20155321<信息安全技术>实验二--Windows口令破解 实验原理 口令破解方法 口令破解主要有两种方法:字典破解和暴力破解. 字典破解是指通过破解者对管理 ...

  8. 【BZOJ4553】[HAOI2016&TJOI2016]序列

    [BZOJ4553][HAOI2016&TJOI2016]序列 题面 bzoj 洛谷 题解 一定要仔细看题啊qwq... 我们设$mn[i],mx[i]$表示第$i$个位置上最小出现.最大出现 ...

  9. eclipse中编译出现错误undefined reference to `_sbrk'

    1. 在eclipse中使用gcc-arm-none-eabi-7-2017-q4-major-win32编译代码的时候出现了undefined reference to `_sbrk' e:/pro ...

  10. Windows下python环境的安装

    1.下载安装包 https://www.python.org/downloads/ 2.安装 默认安装路径:C:\python27 3.配置环境变量 [右键计算机]-->[属性]-->[高 ...