08_Queue(队列UVa 10128)
问题描述:n(1<=n<=13)个身高均不相等的人站成一排,从左向右看能看见L个人,从右向左看能看见R个人,问这个队列有多少种排法?
问题分析: 1.n个人的身高可设为1~n,
2.设dp[k][i][j]中,k代表当前有k个人的队列,i代表从左边看能看见的人数,j代表从右边看能看到的人数
3.由于谁先进队列,谁后进队列无所谓,我们从最高的人开始排起,保证每次新加入队列的人都是队列里边最矮的一个。
4.若将新加的人放在最左边,则dp[k][i][j] += dp[k-1][i-1][j];
若将新加的人放在最右边,则dp[k][i][j] += dp[k-1][i][j-1];
若将新加入的人放在中间的任意地方,则dp[k][i][j] += dp[k-1][i][j]*(k-2);
所以状态转移方程为:dp[k][i][j] = dp[k-1][i-1][j] + dp[k-1][i][j-1] + dp[k-1][i][j]*(k-2);
例题链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1069
例题:UVa 10128
10128 - Queue
Time limit: 3.000 seconds
There is a queue with N people. Every person has a different heigth. We can see P people, when we are looking from the beginning, and R people, when we are looking from the end. Its because they are having different height and they are covering each other. How many different permutations of our queue has such a interesting feature?
Input
The input consists of T test cases. The number of them (1<=T <=10000) is given on the rst line of the input file.
Each test case begins with a line containing a single integer number N that indicates the number of people in a queue (1<=N<=13). Then follows line containing two integers.The first integer corresponds to the number of people, that we can see looking from the beginning.The second integer corresponds to the number of people, that we can see looking from the end.
Output
For every test case your program has to determine one integer. Print how many permutations of N people we can see exactly P people from the beginning, and R people, when we are looking from the end.
Sample Input
3
10 4 4
11 3 1
3 1 2
Sample Output
90720
1026576
1
代码:
#include "stdio.h"
#include "string.h"
#define N 15
int dp[N][N][N]; int main()
{
int T;
int n,L,R;
int i,j,k;
scanf("%d",&T);
memset(dp,,sizeof(dp));
dp[][][] = ;
for(k=; k<N; k++)
{
for(j=; j<=k; j++)
{
for(i=; i<=k-j+; i++)
dp[k][i][j] = dp[k-][i][j]*(k-) + dp[k-][i-][j] + dp[k-][i][j-];
}
}
while(T--)
{
scanf("%d %d %d",&n,&L,&R);
printf("%d\n",dp[n][L][R]);
}
}
08_Queue(队列UVa 10128)的更多相关文章
- uva 10128
动归 转移方程 :dp(i, j, k) = dp(i – 1, j, k) * (i – 2) + dp(i – 1, j – 1, k) + dp(i – 1, j, k – 1) i表示此时排第 ...
- UVa 12100打印队列(队列)
原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA.540 Team Queue (队列)
UVA.540 Team Queue (队列) 题意分析 有t个团队正在排队,每次来一个新人的时候,他可以插入到他最后一个队友的身后,如果没有他的队友,那么他只能插入到队伍的最后.题目中包含以下操作: ...
- uva 540 - Team Queue(插队队列)
首发:https://mp.csdn.net/mdeditor/80294426 例题5-6 团体队列(Team Queue,UVa540) 有t个团队的人正在排一个长队.每次新来一个人时,如果他有队 ...
- UVA 540 Team Queue(模拟+队列)
题目代号:UVA 540 题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page ...
- UVa 10935 Throwing cards away I【队列】
题意:给出 n张牌,从上往下编号依次为1到n,当牌的数目至少还剩下2张时,把第一张牌扔掉,然后把新的一张牌放在牌堆的最底部,问最后剩下的那一张牌是哪一张牌. 模拟队列的操作------- #inclu ...
- uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列
题目很简单,给一个队列以及文件的位置,然后一个一个检查,如果第一个是优先级最高的就打印,否则放到队列后面,求所要打印的文件打印需要花费多长时间. 这里我用数组模拟队列实现,考虑到最糟糕的情况,必须把数 ...
- Uva 10305 - Ordering Tasks 拓扑排序基础水题 队列和dfs实现
今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题. 于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了... 于是检查了一遍又交了一发,还是WA. ...
- UVa 540 (团体队列) Team Queue
题意: 每个人都属于一个团体,在排队的时候,如果他所在的团体有人在队伍中,则他会站到这个团体的最后.否则站到整个队伍的队尾. 输出每次出队的人的编号. 分析: 容易看出,长队中,在同一个团体的人是排在 ...
随机推荐
- 11条javascript知识
1.局部变量和全局变量 var操作符定义的变量将成为定义该变量作用域中的局部变量.这个局部变量会在函数退出后销毁.不同于其他语言,javaScript不存在块级作用域. 全局变量就是window对象的 ...
- 剑指offer面试题30:最小的k个数
一.题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 二.解题思路 1.思路1 首先对数组进行排序,然后取出前k个数 ...
- 操作AppConfig.xml中AppSettings对应值字符串
//查询AppSettings的key public static List sql() { List list = new List(); ...
- windows远程控制
windows+r 输入 mstsc 打开远程控制 首先,确定目标机器支持远程控制 问题解决: Win7远程桌面提示您的凭据不工作: http://jingyan.baidu.com/article ...
- Servlet-中文乱码
背景 从Tomcat5.x开始,GET,POST方法提交信息,Tomcat采用不同的方式来处理编码. 对于GET请求,Tomcat不会考虑使用request.setCharacterEncoding( ...
- Hibernate关联映射及高级查询
一.Hibernate中的关联关系 1.1.单向一对多关联关系 按照以下步骤配置hibernate中持久化类的一对多对象关联: (1).持久化类添加关联类的相关属性及getter/setter方法. ...
- http get post
使用java代码模拟http请求 package ftp; import java.io.BufferedReader; import java.io.IOException; import java ...
- 关于js中两种定时器的设置及清除
1.JS中的定时器有两种: window.setTimeout([function],[interval]) 设置一个定时器,并且设定了一个等待的时间[interval],当到达时间后,执行对应的方法 ...
- jQuery淡入淡出效果轮播图
用JavaScript做了平滑切换的焦点轮播图之后,用jQuery写了个简单的淡入淡出的轮播图,代码没有做优化,html结构稍微有一些调整,图片部分用ul替换了之前用的div. html结构如下: & ...
- 404 & 401 Errors with the App Management Service
from:http://blogs.technet.com/b/sharepoint_-_inside_the_lines/archive/2013/06/23/404-amp-401-errors- ...