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.

 
 
思路:每经过一个加油站,先不加油,而是认为可以加油,当油箱内的油为0时,则判断当前能加的油量最大值,如此可解。
#include<iostream>
#include<stdio.h>
#include<queue>
#include<algorithm>
#define maxn 1000005
using namespace std;
struct Node
{
int dis;
int fuel; }node[maxn];
priority_queue <int> fu;
bool cmp(Node a,Node b)
{ if(a.dis!=b.dis)
return a.dis>b.dis;
}
int main()
{
int n;
cin>>n;
for(int i=;i<n;i++)
{
scanf("%d%d",&node[i].dis,&node[i].fuel);
}
sort(node,node+n,cmp);
int p,l;
cin>>l>>p;
int t=;
bool flag=false;
int res=;
for(int i=l;i>;i--)
{
//cout<<i<<endl;
//cout<<fu.top()<<endl<<endl;
if(p>)
{
p--;
}
else if(p==)
{
for(t;t<n;t++)
{
if(node[t].dis<i) break;
fu.push(node[t].fuel);
}
if(fu.empty())
{
flag=true;
break;
} p=fu.top();
fu.pop();
res++;
p--;
}
}
if(!flag)
{
cout<<res<<endl;
}
else cout<<"-1"<<endl;
return ;
}

poj2431优先队列的更多相关文章

  1. POJ2431 优先队列+贪心 - biaobiao88

    以下代码可对结构体数组中的元素进行排序,也差不多算是一个小小的模板了吧 #include<iostream> #include<algorithm> using namespa ...

  2. POJ2431 Expedition(排序+优先队列)

    思路:先把加油站按升序排列. 在经过加油站时.往优先队列里增加B[i].(每经过一个加油站时,预存储一下油量) 当油箱空时:1.假设队列为空(能够理解成预存储的油量),则无法到达下一个加油站,更无法到 ...

  3. poj2431(优先队列+贪心)

    题目链接:http://poj.org/problem?id=2431 题目大意:一辆卡车,初始时,距离终点L,油量为P,在起点到终点途中有n个加油站,每个加油站油量有限,而卡车的油箱容量无限,卡车在 ...

  4. poj2431 Expedition优先队列

    Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Bein ...

  5. 【poj2431】驾驶问题-贪心,优先队列

    Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29360   Accepted: 8135 Descr ...

  6. H - Expedition 优先队列 贪心

    来源poj2431 A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being ...

  7. POJ 2431 (优先队列)

    题目链接:https://vjudge.net/problem/POJ-2431 思路: “ 在卡车行驶途中, 只有经过加油站才能加油.” 我们不妨转变思路, 理解成“当卡车驶过加油站时就获得了加油的 ...

  8. 【POJ - 2431】Expedition(优先队列)

    Expedition 直接中文 Descriptions 一群奶牛抓起一辆卡车,冒险进入丛林深处的探险队.作为相当差的司机,不幸的是,奶牛设法跑过一块岩石并刺破卡车的油箱.卡车现在每运行一个单位的距离 ...

  9. 堆排序与优先队列——算法导论(7)

    1. 预备知识 (1) 基本概念     如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...

随机推荐

  1. synchronized-异常

    对于web应用程序,异常释放锁的情况,很可能对你的应用程序业务逻辑产生必要严重的错误,比如:执行某个队列任务,很多对象都会去等待第一个对象正常执行的结果返回再次去释放锁,那么其中摸个对象发生执行异常了 ...

  2. python学习:常见问题

    问题1:SyntaxError: Non-ASCII character '\xe5' in file E:\PythonDev\testmodule.py on line 21, but no en ...

  3. go语言基础之常量

    1.常量 示例: package main //必须有一个main包 import "fmt" func main() { //变量:程序运行期间,可以改变的量, 变量声明需要va ...

  4. JS排序:localeCompare() 方法实现中文排序、sort方法实现数字英文混合排序

    定义:用本地特定的顺序来比较两个字符串. 语法:stringObject.localeCompare(target) 参数:target——要以本地特定的顺序与 stringObject 进行比较的字 ...

  5. 从外部重置一个运行中consumer group的消费进度

    对于0.10.1以上版本的kafka, 如何从外部重置一个运行中的consumer group的进度呢?比如有一个控制台,可以主动重置任意消费组的消费进度重置到12小时之前, 而用户的程序可以保持运行 ...

  6. Short与Integer互转

    int 是4字节, short 是2字节的, 如果将int(Integer)转成short(Short), 那么必须强制转换,否则会报编译异常. 但是, 当int(Integer)是一个final时, ...

  7. 在笛卡尔坐标系上描绘函数 y=4x^2-2/4x-3

    代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...

  8. iOS 使用 AVCaptureVideoDataOutputSampleBufferDelegate获取实时拍照的视频流

    iOS 使用 AVCaptureVideoDataOutputSampleBufferDelegate获取实时拍照的视频流 可用于实时视频聊天 实时视频远程监控 #import <AVFound ...

  9. win7远程凭据无法工作

    由于上周过度用脑之后没有清空内存,导致脑容量不够用,办了件傻事,但是傻人有傻福,从中收获了很多, 这个错误可以这样形容,就是从哪里开始,就从哪里结束,好了,开始正文(以win8系统为例) 想要远程某一 ...

  10. Unity3d网络游戏Socket通讯

    http://blog.csdn.net/wu5101608/article/details/37999409