【小白爬POJ2431】3.6 探险车加油问题 Expedition

  • 题目
    • Description
    • Input
    • Output
    • Sample Input
    • Sample Output
    • Hint
  • 思路

POJ2431 hard\color{#FF0000}{hard}hard

点击进入原题链接:POJ2431 Expedition

题目

Description

A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck’s fuel tank. The truck now leaks one unit of fuel every unit of distance it travels.

To repair the truck, the cows need to drive to the nearest town (no more than 1,000,000 units distant) down a long, winding road. On this road, between the town and the current location of the truck, there are N (1 <= N <= 10,000) fuel stops where the cows can stop to acquire additional fuel (1…100 units at each stop).

The jungle is a dangerous place for humans and is especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the way to the town. Fortunately, the capacity of the fuel tank on their truck is so large that there is effectively no limit to the amount of fuel it can hold. The truck is currently L units away from the town and has P units of fuel (1 <= P <= 1,000,000).

Determine the minimum number of stops needed to reach the town, or if the cows cannot reach the town at all.

Input

  • Line 1: A single integer, N

  • Lines 2…N+1: Each line contains two space-separated integers describing a fuel stop: The first integer is the distance from the town to the stop; the second is the amount of fuel available at that stop.

  • Line N+2: Two space-separated integers, L and P

Output

  • Line 1: A single integer giving the minimum number of fuel stops necessary to reach the town. If it is not possible to reach the town, output -1.

Sample Input

4
4 4
5 2
11 5
15 10
25 10

Sample Output

2

Hint

INPUT DETAILS:

The truck is 25 units away from the town; the truck has 10 units of fuel. Along the road, there are 4 fuel stops at distances 4, 5, 11, and 15 from the town (so these are initially at distances 21, 20, 14, and 10 from the truck). These fuel stops can supply up to 4, 2, 5, and 10 units of fuel, respectively.

OUTPUT DETAILS:

Drive 10 units, stop to acquire 10 more units of fuel, drive 4 more units, stop to acquire 5 more units of fuel, then drive to the town.

思路

图片来源:点击进入bilibili原视频

**贪心思想:**我们在油量耗尽的时候加油,每次加油选择最大的加油站(用最大堆记录所有没有加过油的站),这样就可以保证加油次数最少。
难点在于:

  1. 当油量耗尽的时候,往前回溯可能需要加不止一次油才能满足往后走,因此需要一个while循环;
  2. 加油的时候,搜索的范围是所有没有加过油的加油站,包括之前选过的加油站之前的加油站

    代码如下:
bool cmp(const std::pair<int,int>&a,const std::pair<int,int>&b){ //由于每个加油站给的是一个对pair,第一个数字是到终点的距离,第二个数字是可以加的油,所以这里指定排序规则,按照到终点的距离对站点进行降序排列(从远到近)return  a.first>b.first;
}
int get_minimum_stop(int L, int P, std::vector<std::pair<int,int>>&stop){std::priority queue<int,std::vector<int>,std::less<int>> Q;int result = 0; //加油次数stop.push_back(std::make_pair(0,0)); //把终点也当作一个站点,到终点距离为0,可以加的油量也为0std::sort(stop.begin(),stop.end(),cmp); //对pair进行排序P = 0; //油箱里的油,初始值为0;for(int i=0;i<stop.size();i++){ //一个一个站点过int dis = L-stop[i].first; //计算站点之间的距离while (P < dis && !Q.empty()) {P += Q.top(); //选择最大加油量的站点Q.pop();result++; //加油数加一}if (P<dis && Q.empty()) {return -1; //所有可以加油的站点都加了,还是到不了终点}P -= dis; //消耗汽油L = stop[i].first; //前进到当前站点Q.push(stop[i].second); //向堆里加入当前站点可加入的油量}return result; //返回加油次数
}

【小白爬POJ2431】3.6 探险车加油问 Expedition相关推荐

  1. Java 小白 设计加油站类和汽车类,加油站提供一个给车加油的方法,参数为剩余汽油数量。每次执行加油方法,汽车的剩余数量都会加2

    //设计加油站类和汽车类,加油站提供一个给车加油的方法,参数为剩余汽油数量.每次执行加油方法,汽车的剩余数量都会加2 public class fourteen {public static void ...

  2. MM引擎新应用——爱车加油记

    基于MM应用引擎开发的EXTJS应用,车主每次加完汽油后,记录加油时的里程数以及加油金额和汽油价格,就可计算出上次加油后行驶的里程数.上次加油的平均油耗.点击刷新按钮,即可计算有记录以来的行驶公里数和 ...

  3. 小白爬坑记:C语言学习点滴——我对单、双引号的理解

    小白爬坑记:C语言学习点滴--我对单.双引号的理解 一.单引号的作用: 二.双引号的作用: 三.字符或字符串容易犯的错误: 三.做个小题: 一.单引号的作用: 将单引号中间的所有符号直接转换为ASCI ...

  4. python爬虫--小白爬取csdn页面题目与链接

    爬取csdn页面题目与链接 前言 随着人工智能的不断发展,爬虫这门技术越来越重要-哈哈哈,太过官方.新手小白,过程较曲折,代码较不专业,欢迎批评与指教! 进入正题:本文主要爬取csdn博客某专栏下的题 ...

  5. python爬虫--小白爬取哔哩哔哩动画排行榜

    爬取哔哩哔哩网站动画排行榜 前言 本次跟博主上一边爬取CSDN的文章内容差不多,主要是爬取哔哩哔哩网站动画排行榜中的题目与链接以及综合得分,最后保存到excel文件中,此次在代码中添加了注释,通俗易懂 ...

  6. python爬虫--小白爬取哔哩哔哩每周更新栏目动画

    爬取哔哩哔哩每周必看栏目动画 前言 本次内容为爬取哔哩哔哩每周必看栏目动画,灵感来自于一位博主的评论,问能否爬取B站历史排行榜信息,便决定一试,不过B站上的排行耪都是动态更新的,因此没有头绪,自我感觉 ...

  7. Scrapy小白爬取智联校园招聘

    前言 掌握了一部分爬虫基础后开始学习Scrapy分布式爬虫,最初觉得会很难,以为分布式就是像hadoop分布式部署一样去配置很多文件,后来发现Scrapy相比普通python爬虫逻辑更简单,速度更快, ...

  8. P2548 [AHOI2004]智能探险车

    原题链接:https://www.luogu.org/problemnew/show/2548 讲道理这题水过了... 题意简述:给出n*m个字符串,要求检查是否在所有的n行字符串中,第m个字符串都相 ...

  9. jdbc 自增id 原理_给“小白”漫画+图示讲解MyBatis原理,就问香不香!

    MyBatis一款后起之秀的持久层框架ORM,支持自定义SQL.存储过程和高级映射,相对于Hibernate算是半自动化的框架,在国内行业内非常流行. 常规的JDBC操作,配置相应的数据库连接的信息, ...

最新文章

  1. 我收藏的技术知识图(每张都是大图)
  2. NC45实现二叉树先序、中序和后序遍历
  3. 前端日报-20160527-underscore 源码解读
  4. ctrl+f5 强刷新
  5. 【转】不用软件,解压Win8/Win8.1的install.wim文件
  6. 字符输入流读取字符数据
  7. Codeforce 1255 Round #601 (Div. 2) A. Changing Volume (贪心)
  8. jenkins 手动执行_Jenkins环境配置篇-增加节点
  9. 2018推荐的android手机,外媒推荐:2018年下半年最值得期待的5款安卓手机
  10. 写了个数码照片的自动分类整理工具
  11. 撞击测试软件,碰撞检测用什么软件?你会用Navisworks做碰撞检测吗?
  12. maven执行package命令解析配置文件中的占位符进行替换
  13. MySQL Innodb data_free 清理
  14. css3中2D变形tranform总结(附实例)
  15. IDEA怎么设置背景图片
  16. Win10 安装NASM
  17. [转]Linux面试题(2020最新版)
  18. 【UE4】给制作的小地图加上方向指针
  19. C51单片机实验——LCD 1602液晶显示器
  20. VSTO Office二次开发键盘鼠标钩子使用整理

热门文章

  1. javaScript模拟实现call
  2. C语言函数大全-- n 开头的函数
  3. 动物派对怎么修改服务器,动物派对登录失败怎么办 Party Animals玩法进入游戏解决方法...
  4. c语言学生学籍管理程序,C语言实现简单学籍管理系统
  5. RDKit中的分子3D构象生成
  6. 爬取微信朋友圈信息-可视化
  7. MySQL_创建表,添加表注释,复制表结构
  8. 英雄联盟手游正式上线啦
  9. 与另一台计算机建立ipc,利用IPC$开启他人电脑远程桌面
  10. 使用Java实现建造者模式