POJ 3934 Queue(DP)
Description
the dinning hall every day. When they are walking in line, if and only if two kids can see each other, they will talk to each other. Two kids can see each other if and only if all kids between them are shorter then both of them, or there are no kids between
them. Kids do not only look forward, they may look back and talk to kids behind them. Linda don’t want them to talk too much (for it’s not safe), but she also don’t want them to be too quiet(for it’s boring), so Linda decides that she must form a line in which
there are exactly m pairs of kids who can see each other. Linda wants to know, in how many different ways can she form such a line. Can you help her?
Note: All kids are different in height.
Input
Input ends by a line containing two zeros.
Output
Sample Input
1 0
2 0
3 2
0 0
Sample Output
1
0
4
题意 linda在一个幼儿园当老师 他要把n个学生排成一列 使仅仅有m对学生可以讲话 当两个学生相邻或者他们之间的全部人都比他们矮时 他们就行讲话
每一个学生的身高都不同
令d[i][j]表示把i个学生排成一列使j对学生可以讲话的方法数
能够把i个学生分成i-1个学生和一个最矮的学生 把这个学生放在i-1个学生中随意两个学生之间都不会影响原来的结果 可是能讲话的学生对数添加了2 有i-2种放法
或者把这个最矮的学生放在两边 这样能讲话的对数仅仅添加了1 有两种放法
所以有转移方程d[i][j]=d[i-1][j-2]*i-2+d[i-1][j-1]*2(j>=2)
j<2时仅仅有d[1][0]=1(学生数大于1就有相邻的就能讲话了) 和d[2][1]=2(学生数大于2肯定不止一对能讲话了)两个有值 能够作为初始条件
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 81, M = 10001;
int d[N][M], n, m;
int main()
{
d[1][0] = 1, d[2][1] = 2;
for (int i = 1; i < N; ++i)
for (int j = 2; j < M; ++j)
d[i][j] = (d[i - 1][j - 2] * (i - 2) + 2 * d[i - 1][j - 1]) % 9937; while (scanf ("%d%d", &n, &m), n)
printf ("%d\n", d[n][m]); return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
POJ 3934 Queue(DP)的更多相关文章
- 周赛 POJ 3934 Queue
Description Linda is a teacher in ACM kindergarten. She is in charge of n kids. Because the dinning ...
- Fire (poj 2152 树形dp)
Fire (poj 2152 树形dp) 给定一棵n个结点的树(1<n<=1000).现在要选择某些点,使得整棵树都被覆盖到.当选择第i个点的时候,可以覆盖和它距离在d[i]之内的结点,同 ...
- poj 3140(树形dp)
题目链接:http://poj.org/problem?id=3140 思路:简单树形dp题,dp[u]表示以u为根的子树的人数和. #include<iostream> #include ...
- poj 3249(bfs+dp或者记忆化搜索)
题目链接:http://poj.org/problem?id=3249 思路:dp[i]表示到点i的最大收益,初始化为-inf,然后从入度为0点开始bfs就可以了,一开始一直TLE,然后优化了好久才4 ...
- poj 1088 滑雪 DP(dfs的记忆化搜索)
题目地址:http://poj.org/problem?id=1088 题目大意:给你一个m*n的矩阵 如果其中一个点高于另一个点 那么就可以从高点向下滑 直到没有可以下滑的时候 就得到一条下滑路径 ...
- poj 3071 可能DP
http://poj.org/problem? id=3071 推方程不难,可是难在怎么算 dp[i][j]表示第i场时第j仅仅队伍存活下来的概率 方程:dp[i][j]=sigma(dp[i-1][ ...
- poj 2342树形dp板子题1
http://poj.org/problem?id=2342 #include<iostream> #include<cstdio> #include<cstring&g ...
- poj上的dp专题
更新中... http://poj.org/problem?id=1037 dp[i][j][0]表示序列长度为i,以j开始并且前两位下降的合法序列数目; dp[i][j][1]表示序列长度为i, 以 ...
- POJ 2096 (概率DP)
题目链接: http://poj.org/problem?id=2096 题目大意:n种bug,s个子系统.每天随机找一个bug,种类随机,来自系统随机.问找齐n种bug,且每个子系统至少有一个bug ...
随机推荐
- text bss data的区别
BSS段 在采用段式内存管理的架构中,BSS段(bss segment)通常是指用来存放程序中未初始化的全局变量的一块内存区域.BSS是英文Block Started by Symbol的简称.BSS ...
- 域名注册查询接口(API)的说明
1.域名查询 接口采用HTTP,POST,GET协议: 调用URL:http://panda.www.net.cn/cgi-bin/check.cgi 参数名称:area_domain 值为标准域名, ...
- 用python输出汉字字库
问题1:如果我们知道汉字编码范围是0x4E00到0x9FA5,怎么从十六进制的编码转成人类可读的字呢? 问题2:怎么把unicode编码的字写入文件呢,假设直接用open()的话,会提示Unicode ...
- NSUserDefaults写作和阅读对象定义自己
需要编写对象必须实现NSCoding protocol Person Class Person.h #import <Foundation/Foundation.h> #import &q ...
- Google 开源项目的风格指南
谷歌C++代码风格指南.农业所需的代码.更难得的是不FQ,决定性的最爱!! . http://zh-google-styleguide.readthedocs.org/en/latest/google ...
- Hbase结构简单、作法
Hbase架构简单介绍.实践 版权声明:本文博主原创文章,博客,未经同意不得转载.
- FileUpload类别FileUpload1.FileName和FileUpload1.PostedFile.FileName差异
FileUpload1.FileName 用来获取client上使用 FileUpload 控件上载的文件的名称.此属性返回的文件名称不包括此文件在client上的路径. FileUpload1.Po ...
- facade pattern
外观模式是一种使用频率非常高的设计模式,它通过引入一个外观角色来简化客户端与子系统之间的交互,为复杂的子系统调用提供一个统一的入口,使子系统与客户端的耦合度降低,且客户端调用非常方便.外观模式并不给系 ...
- 检验身份证的正确性(C语言版本)
/* check id_card * write by sndnvaps<sndnvaps@gmail.com> * ai -> a1 , a2, a3, a4, a5, a6... ...
- sql中 in 、not in 、exists、not exists 使用方法和区别
% 的一类. NOT IN:通过 NOT IN keyword引入的子查询也返回一列零值或很多其它值. 以下查询查找没有出版过商业书籍的出版商的名称. SELECT pub_name FROM pub ...