POJ 1661 Help Jimmy LIS DP
http://poj.org/problem?id=1661
对板按高度排序后。
dp[i][0]表示现在站在第i块板上,向左跑了,的状态,记录下时间和其他信息。
O(n^2)LIS;
唯一的麻烦就是,如果由第i块板---->第j块板,除了高度差会摔死之后,还可能会中间隔着一些板,使得它是去不了第j块的
所以用个vis标记下,如果i--->j中,vis[i]已经是true,表示第i块板已经被其他板截住了。判断一下就好。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e3 + ;
struct node {
int x, y, h;
bool operator < (const struct node & rhs) const {
if (h != rhs.h) return h > rhs.h;
else if (x != rhs.x) return x < rhs.x;
else return y < rhs.y;
}
} a[maxn];
struct DP {
int x, h, tim;
int cat;
} dp[maxn][];
void work() {
// init();
int n, x, y, limit;
scanf("%d%d%d%d", &n, &x, &y, &limit);
for (int i = ; i <= n; ++i) {
scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].h);
}
n++;
a[n].x = -inf;
a[n].y = inf;
a[n].h = ;
memset(dp, 0x3f, sizeof dp);
dp[][].x = dp[][].x = x;
dp[][].h = dp[][].h = y;
dp[][].tim = dp[][].tim = ;
dp[][].cat = dp[][].cat = ;
for (int i = ; i <= n; ++i) {
dp[i][].cat = dp[i][].cat = ;
}
// cout << dp[3][1].cat << en
sort(a + , a + + n);
int ans = inf;
for (int i = ; i <= n; ++i) {
for (int j = ; j < i; ++j) {
if (dp[j][].h - a[i].h > limit) continue;
if (!dp[j][].cat) {
bool flag = true;
if (i == n) {
int cost = dp[j][].h + dp[j][].tim;
ans = min(ans, cost);
flag = false;
}
if (flag) {
if (dp[j][].x >= a[i].x && dp[j][].x <= a[i].y) {
dp[j][].cat = true;
int cost = dp[j][].x - a[i].x + dp[j][].h - a[i].h + dp[j][].tim;
if (dp[i][].tim > cost) {
dp[i][].tim = cost;
dp[i][].h = a[i].h;
dp[i][].x = a[i].x;
}
cost = a[i].y - dp[j][].x + dp[j][].h - a[i].h + dp[j][].tim;
if (dp[i][].tim > cost) {
dp[i][].tim = cost;
dp[i][].x = a[i].y;
dp[i][].h = a[i].h;
}
}
}
}
if (!dp[j][].cat) {
if (i == n) {
int cost = dp[j][].tim + dp[j][].h;
ans = min(ans, cost);
continue;
}
if (dp[j][].x >= a[i].x && dp[j][].x <= a[i].y) {
dp[j][].cat = true;
int cost = dp[j][].x - a[i].x + dp[j][].h - a[i].h + dp[j][].tim;
if (dp[i][].tim > cost) {
dp[i][].tim = cost;
dp[i][].x = a[i].x;
dp[i][].h = a[i].h;
}
cost = a[i].y - dp[j][].x + dp[j][].h - a[i].h + dp[j][].tim;
if (dp[i][].tim > cost) {
dp[i][].tim = cost;
dp[i][].h = a[i].h;
dp[i][].x = a[i].y;
}
}
}
}
}
// cout << dp[3][1].cat << endl;
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int g;
cin >> g;
while (g--) work();
return ;
}
POJ 1661 Help Jimmy LIS DP的更多相关文章
- POJ - 1661 - Help Jimmy - 简单dp
http://poj.org/problem?id=1661 一般化处理,把一开始的落地和大地都视作平台,设计平台类的属性.dp的时候显然是从上往下dp的,而且要小心Jimmy不能够穿过平台,也就是从 ...
- POJ 1661 Help Jimmy【DP】
基础DP,过程想明白了其实也不复杂,从上面的推下面的比倒着推要简单很多.调试了半个多小时..简单dp依然不能快速AC..SAD.. 题目链接: http://poj.org/problem?id=16 ...
- POJ 1661 Help Jimmy(DP/最短路)
Help Jimmy Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14980 Accepted: 4993 Descripti ...
- POJ 1661 Help Jimmy(C)动态规划
没刷过 POJ,这题是论坛有人问的,我才看看. 我发现 POJ 注册很奇怪,账号总是登不上去,弄的我还注册两个.Emmm 首次体验很差,还好我不在 POJ 刷题. 题目链接:POJ 1661 Help ...
- POJ 1661 Help Jimmy(递推DP)
思路: 1. 每个板子有左右两端, dp[i][0], dp[i][1] 分别记录左右端到地面的时间 2. 从下到上递推计算, 上一层的板子必然会落到下面的某一层板子上, 或者地面上 总结: 1. 计 ...
- POJ 1661 Help Jimmy(二维DP)
题目链接:http://poj.org/problem?id=1661 题目大意: 如图包括多个长度和高度各不相同的平台.地面是最低的平台,高度为零,长度无限. Jimmy老鼠在时刻0从高于所有平台的 ...
- POJ 1661 Help Jimmy(DP,注意边界)
Help Jimmy Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9399 Accepted: 3025 Descri ...
- POJ 1661 Help Jimmy DP
思路:Jimmy 跳到一块板上后,可以有两种选择,向左走或向右走.走到左端和走到右端所需的时间,容易算出. n如果我们能知道,以左端为起点到达地面的最短时间,和以右端为起点到达地面的最短时间,那么向左 ...
- POJ 1661 Help Jimmy -- 动态规划
题目地址:http://poj.org/problem?id=1661 Description "Help Jimmy" 是在下图所示的场景上完成的游戏. 场景中包括多个长度和高度 ...
随机推荐
- SQL数据库的十条命令
--(1)查询每个总学时数 select GradeId,SUM(classHour) from subject group by GradeId order by(SUM(classHour)) - ...
- graphql 新API 开发方式
我们知道 GraphQL 使用 Schema 来描述数据,并通过制定和实现 GraphQL 规范 定义了支持 Schema 查询的 DSQL (Domain Specific Query Langua ...
- 解决Hibernate向MySQL数据库插入中文乱码问题
有时候我们在用hibernate插入中文的字符会出现乱码情况,如下图所示. 看到这种情况,第一反应便是应用程序用的字符集合数据库用的字符集不统一了.我的数据库用个是mysql的,看一下建表语句.用的是 ...
- 在Linux下安装PHP过程中,编译时出现错误的解决办法
在Linux下安装PHP过程中,编译时出现configure: error: libjpeg.(a|so) not found 错误的解决办法 configure: error: libjpeg.(a ...
- C++常见gcc编译链接错误解决方法
除非明确说明,本文内容仅针对x86/x86_64的Linux开发环境,有朋友说baidu不到,开个贴记录一下(加粗字体是关键词): 用“-Wl,-Bstatic”指定链接静态库,使用“-Wl,-Bdy ...
- coderforces719b
题目大意:给定一个字符串,这个字符串中只有“r”和"b"组成,每次操作只能交换两个字符的位置或者将一个字符由"r"变"b"(或由" ...
- TCP/UDP的接收包方式
UDP udp不是流式的,每次接收一个包,长度不超过(65535-28,总包长65535字节,包头28字节).所以UDP方式下不需要填写任何参数直接调用 $client->recv() 即可.注 ...
- C++模板元编程 - 挖新坑的时候探索到了模板元编程的新玩法
C++真是一门自由的语言,虽然糖没有C#那么多,但是你想要怎么写,想要实现什么,想要用某种编程范式或者语言特性,它都会提供. 开大数运算类的新坑的时候(又是坑),无意中需要解决一个需求:大数类需要分别 ...
- 利用WCF的双工通讯实现一个简单的心跳监控系统 z
利用WCF的双工通讯实现一个简单的心跳监控系统 http://www.cnblogs.com/zuowj/p/5761011.html 何为心跳监控系统? 故名思义,就是监控某个或某些个程序的运行状态 ...
- 透视校正插值(Perspective-Correct Interpolation)
在渲染器光栅化每个三角形的过程中,需要对根据顶点属性对三角形进行扫描线插值.此时由于投影面上顶点的2D坐标与顶点属性不成线性关系,因此是不能简单地使用线性插值来计算顶点属性的. 此时应当利用透视校正插 ...