kuangbin专题十二 POJ1661 Help Jimmy (dp)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 14214 | Accepted: 4729 |
Description

场景中包括多个长度和高度各不相同的平台。地面是最低的平台,高度为零,长度无限。
Jimmy老鼠在时刻0从高于所有平台的某处开始下落,它的下落速度始终为1米/秒。当Jimmy落到某个平台上时,游戏者选择让它向左还是向右跑,它跑动的速度也是1米/秒。当Jimmy跑到平台的边缘时,开始继续下落。Jimmy每次下落的高度不能超过MAX米,不然就会摔死,游戏也会结束。
设计一个程序,计算Jimmy到底地面时可能的最早时间。
Input
Jimmy的大小和平台的厚度均忽略不计。如果Jimmy恰好落在某个平台的边缘,被视为落在平台上。所有的平台均不重叠或相连。测试数据保证问题一定有解。
Output
Sample Input
1
3 8 17 20
0 10 8
0 10 13
4 14 3
Sample Output
23
思路:本来以为是dp二维x, y, 但是不可能开那么大,状态也不好规划。无奈之下看了题解。
dp[i][0]表示从左边到达i的最小花费
dp[i][1]表示从右边到达i的最小花费
然后考虑怎么转移。
例:
-----------------------------------
---------------- --------------------
到达最上面的木板可以从左边或右边,下面的一直递推。
状态转移方程:
dp[i][0] = h[i] - h[j] + min( dp[j][0] + l[i] - l[j], dp[j][1] + r[j] - l[i] )
dp[i][1] = h[i] - h[j] + min( dp[j][0] + r[i] - l[j], dp[j][1] + r[j] - r[i] )
也就是线段左端 + 交叉的距离 或 线段右端 + 交叉的距离
其中还有最大高度的细节处理,分为两类,①找不到并且距离之外,②找不到但距离之内
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define mem(a,b) memset((a),(b),sizeof(a))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF =0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-;
const ll mod = 1e9+;
//head int dp[][];//dp[i][0]表示从左边到达i的最小花费
//dp[i][1]表示从右边到达i的最小花费
struct node{
int r, l, h;
}stu[]; bool cmp(node a, node b) {//按高度排序
if(a.h != b.h)
return a.h < b.h;
return a.l < b.l;
} bool flag;
int main(int argc, char const *argv[])
{
int _, n, x, y, m;
for(scanf("%d", &_);_;_--) {
memset(dp, , sizeof(dp));
scanf("%d%d%d%d", &n, &x, &y, &m);
for(int i = ; i < n; i++) {
scanf("%d%d%d", &stu[i].l, &stu[i].r, &stu[i].h);
}
sort(stu, stu + n, cmp);
stu[n].l = stu[n].r = x;
stu[n].h = y;
for(int i = ; i < n + ; i++) {
flag = false;//标记
for(int j = i - ; j >= ; j--) {//左
if(stu[i].h - stu[j].h <= m && stu[i].h > stu[j].h)//保证不摔的前提
if(stu[j].l <= stu[i].l && stu[j].r >= stu[i].l) {//可以从左边到达的条件
dp[i][] = stu[i].h - stu[j].h + min(dp[j][] + stu[i].l - stu[j].l, dp[j][] + stu[j].r - stu[i].l);
flag = true;//标记找到
break;//递推,找到一个就break
}
}
if(stu[i].h > m && flag == false) {//如果找不到,并且木板i到达地面高度大于m -----无穷大
dp[i][] = inf;
} else if(flag == false){//找不到,但是到达地面高度 <= m
dp[i][] = stu[i].h;
}
flag = false;//同理求出右边的最小花费
for(int j = i - ; j >= ; j--) {//右
if(stu[i].h - stu[j].h <= m && stu[i].h > stu[j].h)
if(stu[j].l <= stu[i].r && stu[j].r >= stu[i].r) {
dp[i][] = stu[i].h - stu[j].h + min(dp[j][] + stu[i].r - stu[j].l, dp[j][] + stu[j].r - stu[i].r);
flag = true;
break;
}
}
if(stu[i].h > m && flag == false) {
dp[i][] = inf;
} else if(flag == false) {
dp[i][] = stu[i].h;
}
}
printf("%d\n", min(dp[n][], dp[n][]));//到达木板n的最小花费
}
return ;
}
kuangbin专题十二 POJ1661 Help Jimmy (dp)的更多相关文章
- kuangbin专题十二 HDU1260 Tickets (dp)
Tickets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- kuangbin专题十二 POJ3186 Treats for the Cows (区间dp)
Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7949 Accepted: 42 ...
- kuangbin专题十二 HDU1176 免费馅饼 (dp)
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)
FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- kuangbin专题十二 HDU1074 Doing Homework (状压dp)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- kuangbin专题十二 HDU1069 Monkey and Banana (dp)
Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- kuangbin专题十二 HDU1114 Piggy-Bank (完全背包)
Piggy-Bank Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- kuangbin专题十二 HDU1087 Super Jumping! Jumping! Jumping! (LIS)
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
随机推荐
- Java 的编译和运行机制
创建一个 名为 test.java 的 Java 源文件 源代码: class Hello{ public static void main(String[] args) { System.out.p ...
- Oracle、SqlServer——临时表
一.oracle 1.概述: oracle数据库的临时表的特点: 临时表默认保存在TEMP中: 表结构一直存在,直到删除:即创建一次,永久使用: 不支持主外键. 可以索引临时表和在临时表基础上建立视图 ...
- MD5 加密算法的使用
最近在看视频时,看到 MD5 的加密算法,感觉其在某些重要信息中,还是很好的解决了一些安全问题的.于是,就在自己理解的情况下,实现了 MD5 算法. 具体的流程大致是: (1)将指定的数据首先通过 M ...
- Codeforces 960F 线段树
题意:https://blog.csdn.net/qq_39809664/article/details/79871282 思路:我们考虑LIS的状态转移,对于这个题,假设现在扫描到的边是(u, v, ...
- ubuntu16.04 安装opencv3.4
1.去官网下载opencv,在本教程中选用的时opencv3.4.1,其他版本的配置方法异曲同工. 下载链接http://opencv.org/releases.html,选择sources版本 2. ...
- 吸收效果,像是在Mac上的垃圾桶的效果一样
#import "AppDelegate.h" #import <QuartzCore/QuartzCore.h> @interface AppDelegate () ...
- multi-mechanize安装实践
关于multi-mechanize的详细介绍参见如下链接,是其官网 http://testutils.org/multi-mechanize/setup.html multi-mechanize是一款 ...
- php学习笔记-while循环
while(condition) { func(); //break the loop } while循环的执行顺序是先判断condition是不是true,如果是true,那么就执行while循环体 ...
- 高性能MySQL笔记-第5章Indexing for High Performance-002Hash indexes
一. 1.什么是hash index A hash index is built on a hash table and is useful only for exact lookups that u ...
- Luogu 1314 [NOIP2011] 聪明的质监员
二分答案 + 前缀和. 题面中式子的意思是每一个区间$[l, r]$的贡献是这个区间内$w_i \geq W$的个数乘以这些$i$的$v_i$和. 很快发现了答案具有单调性,可以做两遍二分,分别看看小 ...