基础DP,过程想明白了其实也不复杂,从上面的推下面的比倒着推要简单很多。调试了半个多小时。。简单dp依然不能快速AC。。SAD。。


题目链接:

http://poj.org/problem?id=1661

题意:

Jimmy从坐标为x,高度为y的点向下跳,每次只能跳到平台上或者地面上,跳到平台上必须跑到平台边缘才能继续下跳。问最少多少时间跳到地面。

分析:

注意:

  1. 对于每个平台左右两边都可以跳,需要分别记录最短时间。
  2. 只能从平台直接跳到下面最近的平台,不能隔着平台跳到更下面的平台。

想明白了就很简单了。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define sa(a) scanf("%d", &a)
#define sal(a) scanf("%I64d", &a)
const int maxn = 1e3+ 5, INF = 0x3f3f3f3f;
struct DP {int x1; int x2; int h; int t1; int t2;};
DP dp[maxn];
bool cmp(DP a, DP b){return a.h > b.h;}
int main (void)
{
int t;sa(t);
while(t--){
int n, x, y, MAX;
sa(n), sa(x), sa(y), sa(MAX);
for(int i = 1; i <= n; i++){
sa(dp[i].x1), sa(dp[i].x2), sa(dp[i].h);
}
sort(dp + 1, dp + n + 1, cmp);
for(int i = 1; i <= n; i++){
dp[i].t1 = dp[i].t2 = INF;
}
dp[0].t1 = dp[0].t2 = 0;
dp[0].h = y, dp[0].x1 = dp[0].x2 = x;
int res = INF;
for(int i = 0; i <= n; i++){
bool flag = false;
for(int j = i + 1; j <= n; j++){
if(dp[i].x1 >= dp[j].x1 && dp[j].x2 >= dp[i].x1){
int cha = dp[i].h - dp[j].h;
if(cha == 0 || cha > MAX) continue;
dp[j].t1 = min(dp[j].t1, dp[i].t1 + cha + dp[i].x1 - dp[j].x1);
dp[j].t2 = min(dp[j].t2, dp[i].t1 + cha + dp[j].x2 - dp[i].x1);
flag = true;
}
if(flag) break;
}
bool flag2 = false;
for(int j = i + 1; j <= n; j++){
if(dp[i].x2 <= dp[j].x2 && dp[j].x1 <= dp[i].x2){
int cha = dp[i].h - dp[j].h;
if(cha == 0 || cha > MAX) continue;
dp[j].t1 = min(dp[j].t1, dp[i].t2 + cha + dp[i].x2 - dp[j].x1);
dp[j].t2 = min(dp[j].t2, dp[i].t2 + cha + dp[j].x2 - dp[i].x2);
flag2 = true;
}
if(flag2) break;
}
if(dp[i].h > MAX) continue;
if(!flag) res = min(res, dp[i].t1 + dp[i].h);
if(!flag2) res = min(res, dp[i].t2 + dp[i].h);
}
printf("%d\n", res);
}
return 0;
}

最新文章

  1. 20151207Study
  2. java导入excel时遇到的版本问题
  3. iOS进阶_动画的多种实现方式
  4. 转:纠结的Shim
  5. JS获取Cookie值
  6. Subversion中文手册(svnbook) TortoiseSVN中文帮助手册
  7. Stream,Reader/Writer,Buffered的区别(1)
  8. why you write code so slow.
  9. js 实现win7任务栏拖动效果
  10. [转] 用GDB调试程序(五)
  11. Three.js 学习笔记(1)--坐标体系和旋转
  12. Visual Studio 无法记忆标签页、断点等的解决办法
  13. 防cc攻击策略
  14. EF学习笔记(九):异步处理和存储过程
  15. linux下创建用户组与用户 只能访问指定目录的方法 以及FTP用户配置详解
  16. tcp的4次挥手、三次握手
  17. Luogu 1603 - 斯诺登的密码 - [简单字符串操作]
  18. [LeetCode] 69. Sqrt(x)_Easy tag: Binary Search
  19. vue 用v-if 或者 v-show 渲染dom时,初次加载闪烁的问题
  20. HTML中常见的其它标签

热门文章

  1. spring Existing transaction found for transaction marked with propagation &#39;never&#39; 解决
  2. 【HEVC帧间预测论文】P1.1 基于运动特征的HEVC快速帧间预测算法
  3. SQL Server2012 T-SQL对分页的增强尝试
  4. COGS 2098. Asm.Def的病毒
  5. Using URL Schemes to Communicate with Apps
  6. HashMap Hashtable TreeMap LinkedHashMap 分析
  7. jeecms
  8. python制作二维码
  9. PHP08 数组和数据结构
  10. Spring框架针对dao层的jdbcTemplate操作之jdbc数据库连接原始操作方法 所需安装包下载