Uva 1638 Pole Arrangement DP

倒过来从插入最短的木棒考虑

1638 Pole ArrangementThere are poles of height 1, 2, . . . , n in a row. If you look at these poles from the left side or the rightside, smaller poles are hidden by taller poles. For example, consider the two arrangements of 4 poles
inthe next figure:For each arrangement, only one pole can be seen from the left, and two poles can be seen from theright.You are to write a program to calculate the number of arrangements of n poles such that seen fromthe left you see l poles and seen from
the right you see r poles.InputYour program is to read from standard input. The input consists of T test cases. The number of testcases T is given in the first line of the input. Each test case consists of a line containing three integers,n, l, and r (1 ≤
l, r ≤ n ≤ 20), where n is the number of poles and l (resp. r) is the number of polesthat can be seen from the left (resp. right).OutputYour program is to write to standard output. Print exactly one line for each test case. The line shouldcontain the number
of arrangements of poles for the test case.The following shows sample input and output for four test cases.Sample Input44 1 24 1 15 2 420 2 1Sample Output2046402373705728000

/* ***********************************************
Author        :CKboss
Created Time  :2015年01月29日 星期四 14时51分47秒
File Name     :UVA1638.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long int LL;

int n,l,r;
LL dp[30][30][30];

void init()
{
	dp[1][1][1]=1;
	for(int i=2;i<=20;i++)
		for(int j=1;j<=20;j++)
			for(int k=1;k<=20;k++)
				dp[i][j][k]=dp[i-1][j-1][k]+dp[i-1][j][k-1]+(i-2)*dp[i-1][j][k];
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

	int T_T;
	scanf("%d",&T_T);
	init();
	while(T_T--)
	{
		scanf("%d%d%d",&n,&l,&r);
		cout<<dp[n][l][r]<<endl;
	}

    return 0;
}
时间: 2024-08-01 20:20:23

Uva 1638 Pole Arrangement DP的相关文章

UVa 1638 Pole Arrangement (递推或DP)

题意:有高为1,2,3...n的杆子各一根排成一行,从左边能看到L根,从右边能看到R根,求杆子的排列有多少种可能. 析:设d(i, j, k)表示高度为1-i的杆子排成一行,从左边看到j根,从右边看到k根的数目.当i>1时,我们按照从大到小的顺序按排杆子, 假设已经安排完i-1根了,那么还剩下一根就是高度为1的了,那么它放在哪都不会挡住任何一根杆子.情况有3种: 1.放在左边,那么在左边一定能够看到它,在右边看不到(因为i>1): 2.放在右边,那么在右边一定能够看到它,在左边看不到(因为i&

UVa 1638 Pole Arrangement

递推. 设f[柱子数量][从左边能看到的数量][从右边能看到的数量]=方案数 假设2~i高度的柱子都已经安排好,现在要插入高度为1的柱子.若它放在最左边,左边可见数量+1,放在最右边,右边可见数量+1,放在中间(有i-2个可能位置),左右可见数量不变. 递推完一个高度i时,假设把所有在场的柱子都拔高1单位高度,此时方案数不变,再加一个高度为1的柱子进去…… 1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #

UVa 1638 Pole Arrangement【递推】

题意:给出n根高度为1,2,3,---n的杆子,从左边能看到l根,右边能够看到r根,问有多少种可能 看的紫书的思路 先假设已经安排好了高度为2---i的杆子, 那么高度为1的杆子的放置方法有三种情况 放在最左边:从左边看得见,右边看不见 放在最右边:从右边看得见,左边看不见 放在中间,有i-2个空位可以插,左右都看不见 所以可以写出递推关系: d[i][j][k]=d[i-1][j-1][k]+d[i-1][j][k-1]+d[i-1][j][k]*(i-2); 1 #include<iostr

[UVA 12589]Learning Vector[DP]

题目链接:[UVA 12589]Learning Vector[DP] 题意分析:给出n个矢量,从中选择k个,以坐标原点为起点,收尾相连,问:这样的k个周围相连矢量与x轴围成图形的最大面积的两倍是多少? 解题思路:考虑状态:dp[id][pick][h]代表到第id个矢量为止,选择pick个矢量离最大面积还差多少,h为当前图形最右端高度.具体转移看代码. 这里着重说一下为什么要对这些矢量按斜率进行排序: 首先,整个求解过程其实就是在暴力枚举这些向量的组合,只不过采用了记忆化搜索优化. 其次,对于

Uva 12018 Juice Extractor dp

题目链接:点击打开链接 题意: 切水果游戏 给出n个水果 水果出现在屏幕上的时间的区间 1.每次切会把屏幕上所有水果切完 2.当同时切3个或以上时计分,分数为切的水果个数 3.不能遗漏水果 问最高得分 dp[i] 表示 最后一次切第i个的得分. #include<stdio.h> #include<algorithm> #include<string.h> #include<iostream> using namespace std; #define N 1

uva 11578 - Situp Benches(dp+输出路径)

题目连接:uva 11578 - Situp Benches 题目大意:健身房有两个仪器,初始角度为10度,每次有人使用需要交15元,每调10度需要花费10元,现在有n个人,给出每个人使用仪器的顺序和角度,保证不会同时有大于2个人序号一样,求最小花费,并且输出每个人分别使用哪一个仪器,并且所有人使用结束后,要将仪器调回10度. 解题思路:dp[i][x][y]表示第i个人,一个仪器为x,另一个仪器为y的情况,path[i][x][y]记录的是它的前一个状态,[0]为前一个仪器的夹角,[1]为另一

uva 10269 最短路+dp

题意:有a个村庄.b个城镇, 编号分别为:1-a , a+1--a+b . 有双神奇的鞋,可以瞬时移动,可以使用k次,每次可以移动L , 但穿这双鞋的时候,不能经过城镇 , 问:从a+b 到 1 最短距离是多少? 刚开始看这个题时 , 一点思路都没有 , dp类型的题目做得太少了. 解法:进行状态压缩, 用点+使用鞋子的次数 , 来表示一个状态 , d[i][k] , 表示到 点 i 使用 k 次鞋子的最短距离是多少. 但要先进行初始化 , 求任意点之间不经过城镇的最短距离 , 用floyd算法

uva 10671 - Grid Speed(dp)

题目链接:uva 10671 - Grid Speed 题目大意:给出N,表示在一个N*N的网格中,每段路长L,现在给出h,v的限制速度,以及起始位置sx,sy,终止位置ex,ey,时间范围st,et,车只走最短路,问说在范围内最快到达和耗油量最小的情况下时间和耗油量. 解题思路:dp[x][y][t]表示在x,y这一点,时间为t的耗油量最小为dp[x][y][t],vis[x][y][t]表示该情况是否可达.dx,dy表示由起点向终点移动的方向.因为时间为L?60v, 因为v是5的倍数,并且小

UVA 1559 - Nim(博弈dp)

UVA 1559 - Nim 题目链接 题意:一开始有s个石子,2n个人轮流取石子,每个人有个最大能取数目,2n个人奇数一队,偶数一队,取到最后一个石子的队输,问谁赢 思路:记忆化搜索,每个人取的时候对应的后继状态如果有一个必败态,则该状态为必胜态,如果都是必胜态,则该状态为必败态 代码: #include <stdio.h> #include <string.h> int n, s, m[25], dp[25][10005]; int dfs(int now, int state