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 ...
随机推荐
- python-xlrd 实现excel 导入数据
首先安装 xlrd 两种方式: 1.wheel 方式 安装: 首先要下载 wheel :
- c:if标签数据回显判断是否选中
<form action="/brand/list.do" method="post" style="padding-top:5px;" ...
- ffmpeg添加水印的方法举例 (砖)
网上大部分关于ffmpeg加视频水印的方法还是使用vhook,在现在的ffmpeg中已经不推荐使用,但是也能编译,也能使用,至于效果,一会再说.现在的ffmpeg推荐使用的是libavfilter,但 ...
- C# WinForm 关闭登陆窗体后进程还再内存怎么办?
问题:我们通常再制作WinForm应用程序的时候,运行程序的第一个窗口一般是登陆窗口.代码如下: 那么这种方式有一个弊端,这种启动方式,其实就是把登陆窗口设置为主窗体.因此,再登陆后,我们通常是调用H ...
- css垂直居中方法(一)
第一种方法:首先用margin:0 auto实现水平居中,然后设置position:relative,设置top为50%(父元素高度的50%),然后设置margin-top:-150px(设置负值是因 ...
- curl的特殊使用
1.php可以通过shell_exec()和其他系统函数使用curl,也可用PHP带的libcurl库. $curl = curl_init("wwww.baidu.com"); ...
- Codeforces Good Bye 2018 D (1091D) New Year and the Permutation Concatenation
题意:给n!个n的排列,按字典序从小到大连成一条序列,例如3的情况为:[1,2,3, 1,3,2, 2,1,3 ,2,3,1 ,3,1,2 ,3,2,1],问其中长度为n,且和为sum=n*(n+1) ...
- SQL 连贯操作 [1]
一. 连贯入门 查找到 id 为 1,2,3,4 中按照创建时间的倒序的前两位. 在 Home/controller/UserController.class.php 下插入 1.连贯操作入门 $us ...
- 业务逻辑:shiro框架的功能实现
思路:分别在web.xml配置过滤器以及在applicationContext.xml去配置 实现步骤:1.在pom.xml里引入shiro的坐标 2.在web.xml里配置shiro过滤器 3.在a ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-002如何改进算法
1. package algorithms.analysis14; import algorithms.util.In; import algorithms.util.StdOut; /******* ...