题意:给你n个身高高低不同的士兵。问你把他们按照波浪状排列(高低高或低高低)有多少方法数。

析:这是一个DP题是很明显的,因为你暴力的话,一定会超时,应该在第15个时,就过不去了,所以这是一个DP计数问题。

那么我们应该怎么想呢,我们先假设前 i-1 个已经放好了,然后第 i 个一定是最高的,所以,他一定要在前面找一个低后面放上他,肯定不能放在高的后面,

那么状态就有的表示了,d[i][0]表示是以低结尾,d[i][1]是以高结尾,我们假设放第 i 个士兵时,前面有 j 个,那么后面就有 i - j - 1个,前面的乘以后面的,

再乘以 C[i-1][j],就是数量,那么怎么转移呢,就是这样,因为以低结尾和以高结尾,数量肯定是一样,所以,每人都是一半。

那么答案就出来了。再节约一点空间,就可以开成一维的。

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <stack>
  15. using namespace std;
  16.  
  17. typedef long long LL;
  18. typedef pair<int, int> P;
  19. const int INF = 0x3f3f3f3f;
  20. const double inf = 0x3f3f3f3f3f3f;
  21. const LL LNF = 100000000000000000;
  22. const double PI = acos(-1.0);
  23. const double eps = 1e-8;
  24. const int maxn = 1e2 + 5;
  25. const int mod = 1e9 + 7;
  26. const char *mark = "+-*";
  27. const int dr[] = {-1, 0, 1, 0};
  28. const int dc[] = {0, 1, 0, -1};
  29. int n, m;
  30. inline bool is_in(int r, int c){
  31. return r >= 0 && r < n && c >= 0 && c < m;
  32. }
  33. inline LL Max(LL a, LL b){ return a < b ? b : a; }
  34. inline LL Min(LL a, LL b){ return a > b ? b : a; }
  35. inline int Max(int a, int b){ return a < b ? b : a; }
  36. inline int Min(int a, int b){ return a > b ? b : a; }
  37. LL C[25][25];
  38. LL d[25];
  39.  
  40. void init(){
  41. for(int i = 0; i < 25; ++i) C[i][0] = 1;
  42. for(int i = 1; i < 22; ++i)
  43. for(int j = 1; j <= i; ++j)
  44. C[i][j] = C[i-1][j] + C[i-1][j-1];
  45.  
  46. d[0] = d[1] = 1;
  47. for(int i = 2; i < 21; ++i){
  48. LL ans = 0;
  49. for(int j = 0; j <= i; ++j)
  50. ans += d[j] * d[i-j-1] * C[i-1][j];
  51. d[i] = ans/2;
  52. }
  53. }
  54.  
  55. int main(){
  56. init();
  57. int T; cin >> T;
  58. while(T--){
  59. scanf("%d %d", &m, &n);
  60. printf("%d ", m);
  61. if(1 == n) printf("1\n");
  62. else printf("%I64d\n", d[n]<<1);
  63. }
  64. return 0;
  65. }

HDU 4489 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 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. tomcat web.xml配置

    关于Tomcat 中 web.xml 文件的配置问题: 1.下面的配置是合法的     <servlet>        <servlet-name>test</serv ...

  2. CodeForces Round #278 (Div.2) (待续)

    A 这么简单的题直接贴代码好了. #include <cstdio> #include <cmath> using namespace std; bool islucky(in ...

  3. “main cannot be resolved or is not a field”解决方案

    .layout.main总是在layout上有错误提示波浪线. 解决方法: (1) 删除"import android.R;". (2) 勾选上Eclipse中的"Pro ...

  4. JQuery Ajax 在asp.net中使用小结

    自从有了JQuery,Ajax的使用变的越来越方便了,但是使用中还是会或多或少的出现一些让人短时间内痛苦的问题.本文暂时总结一些在使用JQuery Ajax中应该注意的问题,如有不恰当或者不完善的地方 ...

  5. Unit testing Cmockery 简单使用

    /********************************************************************** * Unit testing Cmockery 简单使用 ...

  6. UVA 11374 Airport Express 机场快线(单源最短路,dijkstra,变形)

    题意: 给一幅图,要从s点要到e点,图中有两种无向边分别在两个集合中,第一个集合是可以无限次使用的,第二个集合中的边只能挑1条.问如何使距离最短?输出路径,用了第二个集合中的哪条边,最短距离. 思路: ...

  7. 【转】出現 "PowerCam player support IE browser only!" 的錯誤訊息

    原文网址:http://www.camdemy.com/faq/1138 A.  這是由於新版 IE11 針對文件模式設定的改變,衍生 PowerCam5 及6 的教材閱讀問題 ( EverCam 已 ...

  8. 锋利的jQuery读书笔记---选择器

    前段时间入手了锋利的jQuery(第二版),想着加强下自己的js能力,可前段时间一只在熟悉Spring和Hibernate.最近抽时间开始读这本书了,随便也做了些记录. 读书的过程是边看边代码测试,所 ...

  9. 在web.config里使用configSource分隔各类配置

    转:http://www.yongfa365.com/Item/using-configSource-Split-Configs.html 大型项目中,可能有多个Service,也就是会有一堆配置,而 ...

  10. WebForm+Web.config: 超时时间已到。在操作完成之前超时时间已过或服务器未响应。

    ylbtech-Error-WebForm+Web.config: 超时时间已到.在操作完成之前超时时间已过或服务器未响应. 超时时间已到.在操作完成之前超时时间已过或服务器未响应. 1.A,错误代码 ...