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

题意:
有n个身高不同的人,计算高低或低高交错排列的方法数。

思路:
可以按照身高顺序依次插进去。

d【i】【0】表示i个人以高低结尾的方法数,d【i】【1】表示i个人以低高开头的方法数。

将第i个人插入时,当它左边为j个人的时候,右边就是i-1-j,并且左边必须要以高低结尾,右边必须以低高开头。也就是d【i-1】【0】*d【i-1】【1】。当然了,后面还得再乘c(i-1,j),表示选j个人的方法数。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + ; int n; int c[][];
ll d[maxn][];
ll sum[maxn]; void dp()
{
memset(c,,sizeof(c));
memset(d,,sizeof(d)); for(int i=;i<=;i++)
{
c[i][]=;
for(int j=;j<=i;j++)
c[i][j]=c[i-][j-]+c[i-][j];
} sum[]=;
sum[]=;
d[][]=d[][]=;
d[][]=d[][]=;
d[][]=d[][]=; for(int i=;i<=;i++)
{
sum[i]=;
for(int j=;j<=i;j++)
{
sum[i]+=d[j][]*d[i-j-][]*c[i-][j];
}
d[i][]=d[i][]=sum[i]/;
}
} int main()
{
//freopen("in.txt","r",stdin);
int T;
int kase;
scanf("%d",&T);
dp();
while(T--)
{
scanf("%d%d",&kase, &n);
printf("%d %lld\n",kase,sum[n]);
}
return ;
}

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

  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

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

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

  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. ios unrecognized selector sent to instance出现的原因和解决方案

    概述:造成unrecognized selector sent to instance iphone,大部分情况下是因为对象被提前release了,在你心里不希望他release的情况下,指针还在,对 ...

  2. C++中的.和::和:和->的区别

    在学习C++的过程中我们经常会用到.和::和:和->,在此整理一下这些常用符号的区别. 1.A.B则A为对象或者结构体: 2.A->B则A为指针,->是成员提取,A->B是提取 ...

  3. Oracle管理监控之监控表空间使用率脚本

    SELECT D.TABLESPACE_NAME,       SPACE "SUM_SPACE(M)",       BLOCKS SUM_BLOCKS,       SPACE ...

  4. Java spring mvc多数据源配置

    1.首先配置两个数据库<bean id="dataSourceA" class="org.apache.commons.dbcp.BasicDataSource&q ...

  5. Apache 2.4 编码GB2312中文乱码的问题

    今天部署了一个项目,代码和数据库都是gb2312的,本地和服务器都是apache2.4的版本,本地编码没问题,response的content-type是空的.按html的mete解析的,查看源码也是 ...

  6. CMDB经验分享之 – 剖析CMDB的设计过程

    作为IT管理的核心,CMDB逐渐成为系统管理项目实施的热点.在很多的案例中,由于忽视了CMDB的因素,ITIL的深入应用受到了极大的挑战.同时,由于CMDB是IT管理信息的集中,CMDB也是一个重要的 ...

  7. PAT 1128 N Queens Puzzle[对角线判断]

    1128 N Queens Puzzle(20 分) The "eight queens puzzle" is the problem of placing eight chess ...

  8. java map典型排序

    List<Map.Entry<TbDiseases, Double>> list = new ArrayList<Map.Entry<TbDiseases,Doub ...

  9. 通过CFX发布WebService(一)

    发布WebService的方法很多.如XFire,CFX等.现在首先介绍下怎样通过CFX来发部一个WebService. (1) 首先,是从Apache官方网站获取CFX的Java包.其地址是:htt ...

  10. Kylo 入坑记

    一.概述 Kylo,作为一个基于 Spark 和 NiFi 的开源数据湖编排框架,解决对数据湖获取.治理.感知和技术支持等诸多问题.Kylo 将数据湖的很多功能自动化,包括数据接入.准备.分析发现.P ...