poj 2431

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.

题意:一辆卡车从起点行驶到终点路程为L,每行驶单位距离花费单位油量。起始时有油量P。在路途中有N个加油站,距离终点距离为Ai,有油量Bi。车如果没油就不能到达终点,假设车油箱无限大。问最少加几次油才能到达终点,不能达到输出-1。
题解:把加油站距离转化为到起点的距离便于操作。可以把问题看成在经过第i个加油站时,获得了加Bi油量的权利,等以后需要加油了就直接加。显然为了使加油次数最少,需要每次选尽可能大的Bi加油,所以需要优先队列。为方便操作,把终点也看成一个加油站。
 
 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct No{
int A;
int B;
};
No C[];
int N,P,L; bool cmp(No a,No b)
{
return a.A>b.A;
} int main()
{
int ans=;
while(cin>>N)
{
for(int i=;i<N;i++) cin>>C[i].A>>C[i].B;
cin>>L>>P;
sort(C,C+N,cmp);N++;
C[N].A=L,C[N].B=;
int pos=,tank=P;
priority_queue<int> que;
for(int i=;i<N;i++)
{
C[i].A=L-C[i].A;
int d=C[i].A-pos;
while(tank-d<)
{
if(que.empty()){
cout<<"-1"<<endl;
return ;
}
tank+=que.top();
que.pop();
ans++;
}
tank-=d;
pos=C[i].A;
que.push(C[i].B);
}
cout<<ans<<endl;
}
return ;
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

poj 2431 【优先队列】的更多相关文章

  1. POJ 2431 优先队列

    汽车每过一单位消耗一单位油,其中有给定加油站可加油,问到达终点加油的最小次数. 做法很多的题,其中优先对列解这题是很经典的想法,枚举每个加油站,判断下当前油量是否小于0,小于0就在前面挑最大几个直至油 ...

  2. POJ 2431 Expedition (贪心+优先队列)

    题目地址:POJ 2431 将路过的加油站的加油量放到一个优先队列里,每次当油量不够时,就一直加队列里油量最大的直到能够到达下一站为止. 代码例如以下: #include <iostream&g ...

  3. POJ 2431 Expedition(探险)

    POJ 2431 Expedition(探险) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] A group of co ...

  4. poj - 2431 Expedition (优先队列)

    http://poj.org/problem?id=2431 你需要驾驶一辆卡车做一次长途旅行,但是卡车每走一单位就会消耗掉一单位的油,如果没有油就走不了,为了修复卡车,卡车需要被开到距离最近的城镇, ...

  5. POJ 2431 Expedition (贪心 + 优先队列)

    题目链接:http://poj.org/problem?id=2431 题意:一辆卡车要行驶L单位距离,卡车上有P单位的汽油.一共有N个加油站,分别给出加油站距终点距离,及加油站可以加的油量.问卡车能 ...

  6. POJ 2431——Expedition(贪心,优先队列)

    链接:http://poj.org/problem?id=2431 题解 #include<iostream> #include<algorithm> #include< ...

  7. 优先队列(挑程)poj 2431

    每次写poj的题都很崩溃,貌似从来没有一次一发就ac的,每次都有特别多的细节需要考虑.还有就是自己写的太粗糙了,应该把每种情况都想到的,总是急着交,然后刷一页wa. 优先队列直接用stl就可以,简单实 ...

  8. poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...

  9. poj 2431 Expedition 贪心+优先队列 很好很好的一道题!!!

    Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10025   Accepted: 2918 Descr ...

随机推荐

  1. Ajax和json一道基本的练习题

    关于ajax是javaEE中最基本的操作: 下面是这道题: 基本功能: jsp+servlet+ajax实现用户信息查询,实现无刷新删除 用户信息包括 学号 姓名 出生日期 性别 操作 2017010 ...

  2. [Day4] Nginx Http模块一

    之前介绍了Nginx作为静态资源服务器的用法,​除此之外,Nginx更多的场景是作为反向代理服务器,提高网站的并发和可用性.下面几节着重说一下作为反向代理的http模块,并且了解一些Nginx的架构. ...

  3. Spring Cloud Consul综合整理

    该项目通过自动配置和Spring环境以及其他Spring编程模型习惯用法提供了Spring Boot应用程序的Consul集成. 通过一些简单的注释,您可以快速启用和配置应用程序内的通用模式,并使用基 ...

  4. 代理模式(Proxy、Subject、RealSubject)(代购火车票)

    .(为其他对象提供一种代理以控制对这个对象的访问.) 在实际的软件开发中,我们经常面临着对一个对象进行访问控制的问题,由于跨越网络或安全方面等原因不能直接或不需要直接被访问,直接访问的代价会给系统带来 ...

  5. mapreduce join操作

    上次和朋友讨论到mapreduce,join应该发生在map端,理由太想当然到sql里面的执行过程了 wheremap端 join在map之前(笛卡尔积),但实际上网上看了,mapreduce的笛卡尔 ...

  6. springcloud:Eureka的使用

    1.认识Eureka 服务的管理. 问题分析 在刚才的案例中,itcast-service-provider对外提供服务,需要对外暴露自己的地址.而consumer(调用者)需要记录服务提供者的地址. ...

  7. SpringMVC注解开发方式

    环境准备 springmvc版本:spring3.2 需要spring3.2所有jar(一定包括spring-webmvc-3.2.0.RELEASE.jar 工程结构 配置前端控制器(web.xml ...

  8. TZ_13_负载均衡-Robbin

    1.但是实际环境中,我们往往会开启很多个user-service的集群.此时我们获取的服务列表中就会有多个,到底该访问哪一个呢? 一般这种情况下我们就需要编写负载均衡算法,在多个实例列表中进行选择. ...

  9. vw单位相关

    1.相对于视口的宽度.视口被均分为100单位的vw h1 { font-size: 8vw; } 如果视口的宽度是200mm,那么上述代码中h1元素的字号将为16mm,即(8x200)/100 2.相 ...

  10. JavaScript 实现回文解码

    题目也是源自今日头条前端工程师笔试题.题目描述: 现在有一个字符串,你要对这个字符串进行 n 次操作,每次操作给出两个数字:(p, l) 表示当前字符串中从下标为 p 的字符开始的长度为 l 的一个子 ...