题意:

  国王的士兵有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. vue 之 折线图挤压

    当tab标签栏变动时,echarts图会发生挤,这种情况,发现是html容器的width=100%这个设置,变成了width=100px. 解决方式: 1.设置一个最小宽度,获取width的值,当这个 ...

  2. 32bit / 64bit co-exist Linux, ld-linux.so, linux-gate.so.1 etc

    before this, confirm that you don't have 32bit libs notably 32bit libc, e.g. you have /lib64/ld-linu ...

  3. Programming With Objective-C---- Introduction ---- Objective-C 学习(一)

    About Objective-C Objective-C is the primary programming language you use when writing software for ...

  4. Spring入门第八课

    看如下代码 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...

  5. 使用go实现基于命令行的计算器程序

    项目目录结构 calcs.go源文件 package main import ( "fmt" "os" "strconv" "my ...

  6. js基础(创建标签)

    创建标签 var divBox1 = document.getElementById('box1'); var p = document.createElement('p'); p.innerHTML ...

  7. springboot 之 controller

    添加一个testController的java 类,部分代码 注解标记这是一个controller,配置路径,自动加载配置. 注入的方式有@Autowired 和@Resource 二者的区别是 @A ...

  8. 洛谷 P1875 佳佳的魔法药水

    P1875 佳佳的魔法药水 题目描述 发完了 k 张照片,佳佳却得到了一个坏消息:他的 MM 得病了!佳佳和大家一样焦急 万分!治好 MM 的病只有一种办法,那就是传说中的 0 号药水 --怎么样才能 ...

  9. zabbix 接口 | zabbix api 实践

    原文地址:https://www.jianshu.com/p/d5faa110e78e zabbix 接口地址:https://www.zabbix.com/documentation/3.2/man ...

  10. php输出变量加{}的作用

    之前在输出字符串中有变量如 echo “中间有”; echo $i; echo "变量"; 现在发现一个好方法,把变量用{}括起来 echo "中间有{$i}变量&quo ...