HDU 4489 The King’s Ups and Downs (DP+数学计数)
题意:给你n个身高高低不同的士兵。问你把他们按照波浪状排列(高低高或低高低)有多少方法数。
析:这是一个DP题是很明显的,因为你暴力的话,一定会超时,应该在第15个时,就过不去了,所以这是一个DP计数问题。
那么我们应该怎么想呢,我们先假设前 i-1 个已经放好了,然后第 i 个一定是最高的,所以,他一定要在前面找一个低后面放上他,肯定不能放在高的后面,
那么状态就有的表示了,d[i][0]表示是以低结尾,d[i][1]是以高结尾,我们假设放第 i 个士兵时,前面有 j 个,那么后面就有 i - j - 1个,前面的乘以后面的,
再乘以 C[i-1][j],就是数量,那么怎么转移呢,就是这样,因为以低结尾和以高结尾,数量肯定是一样,所以,每人都是一半。
那么答案就出来了。再节约一点空间,就可以开成一维的。
代码如下:
- #pragma comment(linker, "/STACK:1024000000,1024000000")
- #include <cstdio>
- #include <string>
- #include <cstdlib>
- #include <cmath>
- #include <iostream>
- #include <cstring>
- #include <set>
- #include <queue>
- #include <algorithm>
- #include <vector>
- #include <map>
- #include <cctype>
- #include <stack>
- using namespace std;
- typedef long long LL;
- typedef pair<int, int> P;
- const int INF = 0x3f3f3f3f;
- const double inf = 0x3f3f3f3f3f3f;
- const LL LNF = 100000000000000000;
- const double PI = acos(-1.0);
- const double eps = 1e-8;
- const int maxn = 1e2 + 5;
- const int mod = 1e9 + 7;
- const char *mark = "+-*";
- const int dr[] = {-1, 0, 1, 0};
- const int dc[] = {0, 1, 0, -1};
- int n, m;
- inline bool is_in(int r, int c){
- return r >= 0 && r < n && c >= 0 && c < m;
- }
- inline LL Max(LL a, LL b){ return a < b ? b : a; }
- inline LL Min(LL a, LL b){ return a > b ? b : a; }
- inline int Max(int a, int b){ return a < b ? b : a; }
- inline int Min(int a, int b){ return a > b ? b : a; }
- LL C[25][25];
- LL d[25];
- void init(){
- for(int i = 0; i < 25; ++i) C[i][0] = 1;
- for(int i = 1; i < 22; ++i)
- for(int j = 1; j <= i; ++j)
- C[i][j] = C[i-1][j] + C[i-1][j-1];
- d[0] = d[1] = 1;
- for(int i = 2; i < 21; ++i){
- LL ans = 0;
- for(int j = 0; j <= i; ++j)
- ans += d[j] * d[i-j-1] * C[i-1][j];
- d[i] = ans/2;
- }
- }
- int main(){
- init();
- int T; cin >> T;
- while(T--){
- scanf("%d %d", &m, &n);
- printf("%d ", m);
- if(1 == n) printf("1\n");
- else printf("%I64d\n", d[n]<<1);
- }
- return 0;
- }
HDU 4489 The King’s Ups and Downs (DP+数学计数)的更多相关文章
- 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 ...
- 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). ...
- 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 ...
- HDU 4489 The King’s Ups and Downs
http://acm.hdu.edu.cn/showproblem.php?pid=4489 题意:有n个身高不同的人,计算高低或低高交错排列的方法数. 思路:可以按照身高顺序依次插进去. d[i][ ...
- HDU 4055 The King’s Ups and Downs(DP计数)
题意: 国王的士兵有n个,每个人的身高都不同,国王要将他们排列,必须一高一矮间隔进行,即其中的一个人必须同时高于(或低于)左边和右边.问可能的排列数.例子有1千个,但是最多只算到20个士兵,并且20个 ...
- 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 ...
- The King’s Ups and Downs(HDU 4489,动态规划递推,组合数,国王的游戏)
题意: 给一个数字n,让1到n的所有数都以波浪形排序,即任意两个相邻的数都是一高一低或者一低一高 比如:1324 4231,再比如4213就是错的,因为4高,2低,接下来1就应该比2高,但是它没有 ...
- The King’s Ups and Downs
有n个高矮不同的士兵,现在要将他们按高,矮依次排列,问有多少种情况. 化简为 n个人,求出可以形成波浪形状的方法数 #include <iostream> #include <cma ...
- hdu 4055 && hdu 4489 动态规划
hdu 4055: 一开始我想的递推方向想得很复杂,看了别人的博客后才醍醐灌顶: 参照他的思路和代码: #include<cstdio> #include<cstring> # ...
随机推荐
- tomcat web.xml配置
关于Tomcat 中 web.xml 文件的配置问题: 1.下面的配置是合法的 <servlet> <servlet-name>test</serv ...
- CodeForces Round #278 (Div.2) (待续)
A 这么简单的题直接贴代码好了. #include <cstdio> #include <cmath> using namespace std; bool islucky(in ...
- “main cannot be resolved or is not a field”解决方案
.layout.main总是在layout上有错误提示波浪线. 解决方法: (1) 删除"import android.R;". (2) 勾选上Eclipse中的"Pro ...
- JQuery Ajax 在asp.net中使用小结
自从有了JQuery,Ajax的使用变的越来越方便了,但是使用中还是会或多或少的出现一些让人短时间内痛苦的问题.本文暂时总结一些在使用JQuery Ajax中应该注意的问题,如有不恰当或者不完善的地方 ...
- Unit testing Cmockery 简单使用
/********************************************************************** * Unit testing Cmockery 简单使用 ...
- UVA 11374 Airport Express 机场快线(单源最短路,dijkstra,变形)
题意: 给一幅图,要从s点要到e点,图中有两种无向边分别在两个集合中,第一个集合是可以无限次使用的,第二个集合中的边只能挑1条.问如何使距离最短?输出路径,用了第二个集合中的哪条边,最短距离. 思路: ...
- 【转】出現 "PowerCam player support IE browser only!" 的錯誤訊息
原文网址:http://www.camdemy.com/faq/1138 A. 這是由於新版 IE11 針對文件模式設定的改變,衍生 PowerCam5 及6 的教材閱讀問題 ( EverCam 已 ...
- 锋利的jQuery读书笔记---选择器
前段时间入手了锋利的jQuery(第二版),想着加强下自己的js能力,可前段时间一只在熟悉Spring和Hibernate.最近抽时间开始读这本书了,随便也做了些记录. 读书的过程是边看边代码测试,所 ...
- 在web.config里使用configSource分隔各类配置
转:http://www.yongfa365.com/Item/using-configSource-Split-Configs.html 大型项目中,可能有多个Service,也就是会有一堆配置,而 ...
- WebForm+Web.config: 超时时间已到。在操作完成之前超时时间已过或服务器未响应。
ylbtech-Error-WebForm+Web.config: 超时时间已到.在操作完成之前超时时间已过或服务器未响应. 超时时间已到.在操作完成之前超时时间已过或服务器未响应. 1.A,错误代码 ...