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. Ceph源码解析:Scrub故障检测

    转载请注明出处 陈小跑 http://www.cnblogs.com/chenxianpao/p/5878159.html 本文只梳理了大致流程,细节部分还没搞的太懂,有时间再看,再补充,有错误请指正 ...

  2. python对于0x01的处理

    对于python脚本,可以使用: .replace('\x01', '') 对于vim工具,可以使用: :%s/\%x01/ /g

  3. http://blog.csdn.net/xingfuzhijianxia/article/details/6433918

    http://blog.csdn.net/xingfuzhijianxia/article/details/6433918

  4. Linux 服务器环境启动

    1.PHP 关闭php killall php-fpm php重启 /usr/local/php/sbin/php-fpm & 或者 /usr/local/php/sbin/php-fpm { ...

  5. Mapreduce 测试自带实例 wordcount

    2.7.3版本的hadoop: jar程序所在目录:$HADOOP_HOME/shar/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.3.jar 1.本 ...

  6. php输出语句echo、print、print_r、printf、sprintf、var_dump比较

    一.echo    echo() 实际上不是一个函数,是php语句,因此您无需对其使用括号.不过,如果您希望向 echo() 传递一个以上的参数,那么使用括号会发生解析错误.而且echo是返回void ...

  7. Moving Tables-贪心

    id=19566" target="_blank" style="color:blue; text-decoration:none">Movin ...

  8. ResourceBundle的使用

    ResourceBundle用来读取properties配置文件,配置文件的位置只能放到src根目录下,当然这个功能的目的是为了实现国际化. 代码如下: package com.comp.common ...

  9. java开发中的一些概念名词

    1. JavaBeans JavaBean是符合某种规范的Java组件,也就是Java类.它必须满足如下规范: 1)必须有一个零参数的默认构造函数 2)必须有get和set方法,类的字段必须通过get ...

  10. 求字符串A与字符串B的最长公共字符串(JAVA)

    思路:引入一个矩阵的思想,把字符串A(长度为m)当成矩阵的行,把字符串B(长度为n)当矩阵的列.这样就构成一个m*n的矩阵.若该矩阵的节点相应的字符同样,即m[i]=n[j]时.该节点值为1:当前字符 ...