题意:

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

思路:是此题HDU 4055 Number String(DP计数) 的简单版,所以看此题解就行了。数量较小,可以预先算出来。要同时考虑 <><>和><><这样的两种情况。

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int N=23;
long long dp1[N][N];
long long dp2[N][N];
long long ans[N];
int j, n;
int cal()
{
ans[1]=1;
dp1[0][1]=dp2[0][1]=1;
for(int i=1; i<N; i++)
{
if(i&1) //大于
{
for(int j=1; j<=i+1; j++)
{
dp1[i][j]=dp1[i][j-1];
dp1[i][j]+=dp1[i-1][j-1];
ans[i+1]+=dp1[i][j];
}
}
else
{
for(int j=i+1; j>0; j--)
{
dp1[i][j]=dp1[i][j+1];
dp1[i][j]+=dp1[i-1][j];
ans[i+1]+=dp1[i][j];
}
}
} for(int i=1; i<N; i++)
{
if(i&1) //大于
{
for(int j=i+1; j>0; j--)
{
dp2[i][j]=dp2[i][j+1];
dp2[i][j]+=dp2[i-1][j];
ans[i+1]+=dp2[i][j];
}
}
else
{
for(int j=1; j<=i+1; j++)
{
dp2[i][j]=dp2[i][j-1];
dp2[i][j]+=dp2[i-1][j-1];
ans[i+1]+=dp2[i][j];
}
}
}
return 0;
}
int main()
{
//freopen("input.txt","r",stdin);
cal();
int p;
cin>>p;
while(p--)
{
scanf("%d%d",&j,&n);
cout<<j<<" "<<ans[n]<<endl;
}
return 0;
}

易理解版本

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int N=;
long long dp[N][N],ans[N];
int j, n;
void cal()
{
ans[]=;
for(int k=; k<; k++)
{
dp[][]=;
for(int i=; i<N; i++)
{
if((i+k)&) //大于
{
for(int j=; j<=i+; j++)
{
dp[i][j]=dp[i][j-];
dp[i][j]+=dp[i-][j-];
ans[i+]+=dp[i][j];
}
}
else
{
for(int j=i+; j>; j--)
{
dp[i][j]=dp[i][j+];
dp[i][j]+=dp[i-][j];
ans[i+]+=dp[i][j];
}
}
}
memset(dp,,sizeof(dp) );
}
}
int main()
{
//freopen("input.txt","r",stdin);
cal();
int p;cin>>p;
while(p--)
{
scanf("%d%d",&j,&n);
cout<<j<<" "<<ans[n]<<endl;
}
return ;
}

节省一半空间和代码量的版本

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int N=;
long long dp[][N],ans[N];
int j, n;
void cal()
{
ans[]=;
for(int k=; k<; k++)
{
dp[][]=;
for(int i=; i<N; i++)
{
if((i+k)&) //大于
{
for(int j=; j<=i+; j++)
{
dp[i&][j]=dp[i&][j-];
dp[i&][j]+=dp[~i&][j-];
ans[i+]+=dp[i&][j];
}
}
else
{
for(int j=i+; j>; j--)
{
dp[i&][j]=dp[i&][j+];
dp[i&][j]+=dp[~i&][j];
ans[i+]+=dp[i&][j];
}
}
}
memset(dp,,sizeof(dp) );
}
}
int main()
{
//freopen("input.txt","r",stdin);
cal();
int p;cin>>p;
while(p--)
{
scanf("%d%d",&j,&n);
cout<<j<<" "<<ans[n]<<endl;
}
return ;
}

滚动数组版本(更少空间)

HDU 4055 The King’s Ups and Downs(DP计数)的更多相关文章

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

  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)

    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

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

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

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

  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 Number String:前缀和优化dp【增长趋势——处理重复选数】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4055 题意: 给你一个由'I', 'D', '?'组成的字符串,长度为n,代表了一个1~n+1的排列中 ...

随机推荐

  1. Aspose.words写表格时多出空格的问题

    通过aspose.words创建表格时,每一个表格总是有一个制表符,和空格差不多,经过查找原因如下: 我是先通过书签找到需要插入表格的位置,在这个位置开始写表格的操作.问题出在书签上,这个书签在创建的 ...

  2. 阿里云服务器CentOS7中Tomcat8.x启动慢问题解决记录

    公司服务器刚换CentOS7,在内部的刀片机上一直跑的很溜,迁移到阿里云上后Tomcat启动竟然要6.7分钟!这还了得. 且日志上无任何错误,在日志中查看到如下信息: Log4j:[2015-10-2 ...

  3. minihttp安装配置ssl和c语言实现cgi

    概述:参考了大牛们的方法,结合自己的环境做了修改,主要是讲:minihttp安装配置ssl和c语言实现cgi接收字符串并且保存系统环境:centos6.5 开发版 依赖软件包: mini_httpd- ...

  4. ACM-ICPC2018焦作网络赛 Transport Ship(二进制背包+方案数)

    Transport Ship 25.78% 1000ms 65536K   There are NN different kinds of transport ships on the port. T ...

  5. jquery获取元素对应高度

    https://www.cnblogs.com/sntetwt/p/3823592.html 获取点击元素距离页面的高度 $(select).offset().top - $(document).sc ...

  6. Unity 中的坐标系

    说明: 注意几点: 0 行向量右乘矩阵与列向量左乘矩阵,两个矩阵互为逆矩阵 1 法线转换与mul,mul函数左乘矩阵当列矩阵计算,右乘当行矩阵计算 2 叉乘与左右手系,左手系用左手,右手系用右手,ax ...

  7. Codevs 2765 隐形的翅膀

    2765 隐形的翅膀   题目描述 Description 天使告诉小杉,每只翅膀都有长度,两只翅膀的长度之比越接近黄金分割比例(黄金分割比= 0.6180339887498949),就越完美. 现在 ...

  8. AcDbDictionary of AcDbDatabase

    GroupDictionary MLStyleDictionary LayoutDictionary PlotStyleNameDictionary MaterialDictionary Visual ...

  9. 4.Python的不堪一击到初学乍练(列表,元组)

    Python(列表,元组) 一.列表 列表初识 列表是python的基础数据类型之一,其他编程语言也有类似的数据类型. 比如JS中的数组, java中的数组等等,它是以[ ]括起来, 每个元素用&qu ...

  10. JS高级学习历程-17

    [正则案例] 1 匹配手机号码