题目链接:

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. js 里常用的字符串操作方法

    /*var str='啦啦啦'; var str1='哈哈哈' //charAt() 返回指定索引处的字符串 console.log(str.charAt(3)) //charCodeAt() 返回指 ...

  2. 显示youtube视频缩略图和Vimeo视频缩略图代码分享

    这篇文章主要介绍了显示youtube视频缩略图和Vimeo视频缩略图的代码,需要的朋友可以参考下 function video_image($url){ $image_url = parse_url( ...

  3. 海思平台交叉编译curl支持SSL功能

    1.准备工具 1).交叉编译工具 2).下载libcurl和openssl源代码,我使用的是(openssl-1.0.2o.tar,curl-7.59.0.tar) 3).查看cpu详细 ~ # ca ...

  4. 获取文件属性“详细信息” - StringFileInfo

    0.什么是StringFileInfo1.获取方法2.示例代码 参考链接: 1.MS docs - GetFileVersionInfoA:https://docs.microsoft.com/zh- ...

  5. (杭电 2045)不容易系列之(3)—— LELE的RPG难题

    不容易系列之(3)-- LELE的RPG难题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...

  6. 20155203 实验四《 Android程序设计》实验报告

    一.实验内容 实验项目一:Android Stuidio的安装测试: 参考<Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)>第二十 ...

  7. 《Java程序设计》实验三(敏捷开发与XP实践)20155214 实验报告

    <JAVA程序设计> 实验三(敏捷开发与XP实践) 实验内容 XP基础 XP核心实践 相关工具 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)>< ...

  8. 20155322 《Java程序设计》课堂实践项目 数据库-3-4

    20155322 <Java程序设计>课堂实践项目 数据库-3-4 数据库-3 实践要求 参考教材代码完成下面的要求,提交能连接到world的截图(有学号水印),并提交代码的码云链接.查询 ...

  9. Week6课下作业

    课本练习p2.96 p2.97 浮点数 float 单精度浮点数(32位) double 双精度浮点数(64位) 练习对应书上内容P78--P81页 知识点是IEEE浮点表示 符号(sign):s决定 ...

  10. P,NP,NPC的通俗解释

    这或许是众多OIer最大的误区之一.    你会经常看到网上出现“这怎么做,这不是NP问题吗”.“这个只有搜了,这已经被证明是NP问题 了”之类的话.你要知道,大多数人此时所说的NP问题其实都是指的N ...