题意:

  国王的士兵有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. Hibernate的auto-import属性详解

    auto-import是什么意思呢? 我们经常会写这样一个HQL语句: from User u where u.name='罗灿锋'; 绝大多数时候,这样写是不会发生问题的. hibernate在处理 ...

  2. C# 中的迭代器 yield关键字 提高性能和可读性

    展示一个例子 IList<string> FindBobs(IEnumerable<string> names) { var bobs = new List<string ...

  3. JDBC的初步了解及使用

    一.概念 1.什么是JDBC? JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由 ...

  4. 有线电视网(树形dp)

    有线电视网 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端,其他中转站为该树的内部节点.从转播站到转播站以及从 ...

  5. uoj#349. 【WC2018】即时战略(动态点分治)

    传送门 头一次看着题解有一种咱不会\(c++\)的感觉-- 看题解吧-- //minamoto #include<bits/stdc++.h> #include "rts.h&q ...

  6. [Xcode 实际操作]六、媒体与动画-(1)使用图形上下文按一定比例缩放图片

    目录:[Swift]Xcode实际操作 本文将演示如何通过图形上下文,来实现图片缩放的功能. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKi ...

  7. CSS揭秘—打字动效(四)

    前言:     所有实例均来自<CSS揭秘>,该书以平时遇到的疑难杂症为引,提供解决方法,只能说秒极了,再一次刷新了我对CSS的认知 该书只提供了关键CSS代码,虽然有在线示例代码链接,但 ...

  8. C 语言实例 - 判断数字为几位数

    C 语言实例 - 判断数字为几位数 用户输入数字,判断该数字是几位数. 实例 #include <stdio.h> int main() { long long n; ; printf(& ...

  9. blur和focus的运用

    这两个事件不仅仅只能运用与input.span之类的元素.还可以运用于window. 可以切换title. 当切换当前页面时,改变title的文字为‘离开了’. <!DOCTYPE html&g ...

  10. Python随笔---return与print,全局变量与局部变量

    Return是指返回一个数值,一般在函数中应用较多 Print则是指把结果打印出来,显示在屏幕上 def sum(a,b): total=a+b print('函数内:',total) return ...