The King’s Ups and Downs

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

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
 
Recommend
liuyiding   |   We have carefully selected several similar problems for you:  6018 6017 6016 6015 6014 

一道较为基础的dp
我们可以对每个总人数n设4个状态,分别代表:
a[1][n]:1比2高且n-1比n高,即 \……\;
a[2][n]:1比2高且n比n-1高,即 \……/;
a[3][n]:2比1高且n-1比n高,即 /……\;
a[4][n]:2比1高且n比n-1高,即 /……/。
对于奇数n 不存在1、4这两种状态,
对于偶数n不存在2、3这两种状态。
(所以可以压缩为2个状态,懒得压了。。自己看着压吧)
对于每个状态a[j][i]都可以可以从a[k][i-1]得到。
我们要求解一个n人的高低序列,我们可以把n高度这个人左边插入一个合法的(i-1)长度的末端下降序列和右边插入一个合法的(n-i)长度的左端上升序列(1<=i<=n)。(同样的我们也可以在1的左右插入,结果相同)
所以我们得出一个dp式子:
(c为组合数 $c_{n-1}^{i-1}$)
  对于奇数n
  for i=1 to n 
    i为奇数
        a[2][n]+=a[1][i-1]*a[4][n-i]*c[n-1][i-1];(或 a[3][n]=a[4][i-1]*a[1][n-i]*c[n-1][i-1];插入1的做法,两者等价)
    i为偶数
                a[3][n]+=a[3][i-1]*a[3][n-i]*c[n-1][i-1];(或 a[2][n]+=a[2][i-1]*a[2][n-i]*c[n-1][i-1];)
  对于偶数 n
  for i=1 to n 
    i为奇数
        a[1][n]+=a[1][i-1]*a[3][n-i]*c[n-1][i-1];(或 a[4][n]=a[4][i-1]*a[2][n-i]*c[n-1][i-1];插入1的做法,两者等价)
    i为偶数
                 a[4][n]+=a[3][i-1]*a[4][n-i]*c[n-1][i-1];(或 a[1][n]+=a[2][i-1]*a[1][n-i]*c[n-1][i-1];)
  然后对1~4中状态求和即为答案 sum[i]。
 #include<cstdio>
#include<iostream>
#include<cstring>
#define clr(x) memset(x,0,sizeof(x))
#define LL long long
using namespace std;
LL c[][];
LL a[][];
LL sum[];
void combine(int n);
void dp(int num);
int main()
{
clr(c);
clr(a);
combine();
dp();
int T,n,m;
scanf("%d",&T);
for(int tt=;tt<=T;tt++)
{
scanf("%d%d",&m,&n);
printf("%d %lld\n",m,sum[n]);
}
return ; }
void combine(int n)
{
for(int i=;i<=n;i++)
{
c[i][]=c[i][i]=;
for(int j=;j<i;j++)
{
c[i][j]=c[i][j-]*(i-j+)/j;
}
}
return ;
}
void dp(int num)
{
a[][]=a[][]=a[][]=a[][]=;
sum[]=;
sum[]=;
for(int n=;n<=num;n++)
{
if(n&)
{
for(int i=;i<=n;i++)
if(i&)
a[][n]+=a[][i-]*a[][n-i]*c[n-][i-];
else
a[][n]+=a[][i-]*a[][n-i]*c[n-][i-];
sum[n]=a[][n]+a[][n];
}
else
{
for(int i=;i<=n;i++)
if(i&)
a[][n]+=a[][i-]*a[][n-i]*c[n-][i-];
else
a[][n]+=a[][i-]*a[][n-i]*c[n-][i-];
sum[n]=a[][n]+a[][n];
}
}
return ;
}

hdu 4489 The King’s Ups and Downs(基础dp)的更多相关文章

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

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

  2. 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). ...

  3. 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/1000 ...

  4. HDU 4489 The King’s Ups and Downs

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

  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之递归(例题:猴子吃桃)

    例题1:公园里有200个桃子,猴子每天吃掉一半以后扔掉一个,问6天以后还剩余多少桃子? var sum = 200; for(var i= 0;i<6;i++) { sum = parseInt ...

  2. javascript工厂模式、单例模式

    //工厂模式 function createObject(name,age){ var obj = new Object(); obj.name = name; obj.age = age; obj. ...

  3. Eclipse svn 忽略文件夹/ svn 设置不同步

    Eclipse 开发中我们经常用到SVN插件, 但是对于某些文件的缓存来说, 我们只要有操作缓存便会保存一次, 每次提交很是麻烦, 小编这里教你一种不同步某个文件夹的方法 工具/原料 MyEclips ...

  4. linux编程之信号量编程

    信号量当我们在多用户系统,多进程系统,或是两者混合的系统中使用线程操作编写程序时,我们经常会发现我们有段临界代码,在此处我们需要保证一个进程(或是一个线程的执行)需要排他的访问一个资源.信号量有一个复 ...

  5. 从LFS官方文档构建完整Linux系统

    从LFS官方文档构建完整Linux系统 http://www.cnblogs.com/sonofdark/p/4962609.html 这不是新手教程!!! Parallels Desktop (为防 ...

  6. linux信号Linux下Signal信号太详细了,终于找到了

    linux信号Linux下Signal信号太详细了,终于找到了 http://www.cppblog.com/sleepwom/archive/2010/12/27/137564.html

  7. 1002: 当不成勇者的Water只好去下棋了---课程作业---图的填色

    1002: 当不成勇者的Water只好去下棋了 Time Limit: 1 Sec  Memory Limit: 128 MB Description 由于魔王BOSS躲起来了,说好要当勇者的Wate ...

  8. 【bzoj3223】文艺平衡树

    #include<bits/stdc++.h> #define N 300005 #define rat 4 using namespace std; struct Node{ int s ...

  9. C中级 消息队列设计

    引言  - 补充好开始 消息队列在游戏服务器层应用非常广泛. 应用于各种耗时的IO操作业务上.消息队列可以简单理解为 [消息队列 = 队列 + 线程安全]本文参照思路如下, 最后献上一个大神们斗法的场 ...

  10. keil问题:Error: failed to execute 'C:\Keil\ARM\BIN\ArmCC'

    1.打开cmd,进入相应的路径下 cd C:\Keil\ARM\BIN\ 输入 armcc  若显示如下界面则说明keil已经注册 2.若注册成功还没解决问题,则说明软件是在网上下载的破解版的,建议卸 ...