博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【优先队列+贪心】Expedition
阅读量:5272 次
发布时间:2019-06-14

本文共 3106 字,大约阅读时间需要 10 分钟。

Expedition
Time Limit: 1000MS   Memory Limit: 65536K
     

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

44 45 211 515 1025 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.
 
题意:
   第一行,输入N,表示有N个加油站。
   第2~N+1行,每行输入两个数值L[i]和P[i],分别表示每个加油站的位置L[i]和最多能加P[i]升的汽油。
   第N+2行输入L和P,分别辆卡车要行驶L单位距离和卡车最开始时有P单位汽油。
   每向前行驶1单位距离消耗1单位汽油,起点,终点,以及加油站是在同一条直线上的,卡车的油箱无限大,无论加多少油都没问题。
        问小车从起点到终点最少要加几次油?若,在小车无法到达终点,则输出-1。
分析:
   起点和终点是一条直线,这条直线上有若干个加油站能够提供加油,最少需要加多少次油才能到达终点。
   要使得加油的次数最少的话,则是尽可能的让小车的没油的时候,才去加油站加汽油。
      对于小车来说,每次当经过第i个加油站的时候,能够选择加油或者不加,也就是在之后的路程中可以使用或者不使用这些汽油。
      所以,我们每次把经过的加油站时,所能够提供的每份汽油保存起来,在下次汽油不足的时候,才去取出来,每次先前保存下来的那份最多的汽油,再继续行驶,在判断。从而可以得出需要最少加油的次数。
  
代码:(优先队列+贪心)
1 #include 
2 #include
3 #include
4 #include
5 #include
6 using namespace std; 7 struct Node{
int L,P;}; 8 struct CMP_L{
bool operator()(Node a,Node b){
return a.L
,CMP_L>ID_L;17 priority_queue
,CMP_P>ID_P;18 for(i=0;i
View Code

 

转载于:https://www.cnblogs.com/Wurq/articles/4687971.html

你可能感兴趣的文章
线程池的概念
查看>>
Oracle_Statspack性能诊断工具
查看>>
转获取sql维护的表关系
查看>>
Java 序列化
查看>>
Java 时间处理实例
查看>>
Java 多线程编程
查看>>
Java 数组实例
查看>>
mysql启动过程
查看>>
2017前端面试题总结
查看>>
Http GetPost网络请求
查看>>
SWIFT国际资金清算系统
查看>>
Sping注解:注解和含义
查看>>
站立会议第四天
查看>>
如何快速掌握一门技术
查看>>
利用AMPScript获取Uber用户数据的访问权限
查看>>
vagrant 同时设置多个同步目录
查看>>
python接口自动化28-requests-html爬虫框架
查看>>
生成随机数的模板
查看>>
Mysql 数据库操作
查看>>
转:linux终端常用快捷键
查看>>