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.

 
这到题的大意是是一个卡车路经许多加油站,每个加油站可加的油有上限,问到达终点最少需加几次油。
做这道题思维要变化一下,我们假设没经过一个加油站就获得了一张随时可变成油的魔法卡片。
当我们发现没油时,就去使用一张卡片,此时当然需要用那张油量最多的卡片。
 
使用priority_queue需#include <queue>
using namespace std
方法有push(), top(), pop(), empty()
它会每回pop()一个最大值出来
 
代码如下
 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue> using namespace std;
int n;
int l, p;
struct Fuel{
int disD;
int disS;
int c;
};
Fuel fuel[]; int cmp(const void *a, const void *b) {
Fuel at = *(Fuel *)a;
Fuel bt = *(Fuel *)b;
return at.disS - bt.disS;
} priority_queue <int> fque; int main(int argc, char const *argv[])
{
while(scanf("%d",&n) != EOF) {
for(int i = ; i < n; i++) {
int tmp;
scanf("%d %d",&fuel[i].disD,&fuel[i].c);
}
scanf("%d %d",&l, &p);
for(int i = ; i < n; i++) {
fuel[i].disS = l - fuel[i].disD;
}
fuel[n].disS = l;
fuel[n].c = ;
qsort(fuel,n, sizeof(Fuel), cmp); while(!fque.empty()) {
fque.pop();
}
int ans = ;
int now = ;
int flag = true;
for(int i = ; i <= n && flag; i++) {
int remain = p - fuel[i].disS + now;
while(remain < ) {
if(fque.empty()) {
flag = false;
break;
}
int tmp = fque.top();
fque.pop();
p = p + tmp;
remain = remain + tmp;
ans++;
}
if(remain >= ) {
fque.push(fuel[i].c);
now = fuel[i].disS;
p = remain;
} }
if(now == l) {
printf("%d\n",ans);
}
else {
puts("-1");
} }
return ;
}

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

  1. poj 3431 Expedition 优先队列

    poj 3431 Expedition 优先队列 题目链接: http://poj.org/problem?id=2431 思路: 优先队列.对于一段能够达到的距离,优先选择其中能够加油最多的站点,这 ...

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

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

  3. H - Expedition 优先队列 贪心

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

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

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

  5. EXPEDI - Expedition 优先队列

    题目描述 A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rathe ...

  6. poj2431 Expedition

    直接代码... #include<string.h> #include<stdio.h> #include<queue> #include<iostream& ...

  7. POJ2431 Expedition 贪心

    正解:模拟费用流 解题报告: 先放个传送门鸭,题目大意可以点Descriptions的第二个切换成中文翻译 然后为了方便表述,这里强行改一下题意(问题是一样的只是表述不一样辣,,, 就是说现在在高速公 ...

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

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

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

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

随机推荐

  1. Java GUI设置图标

    ImageIcon是Icon接口的一个实现类. ImageIcon类的构造函数: ImageIcon() ImageIcon(String filename)   //本地图片文件 ImageIcon ...

  2. 微信小程序---图片上传+服务端接受

    原文地址:http://blog.csdn.net/sk719887916/article/details/54312573 微信小程序,图片上传,应用地方-修改用户信息的头像. 详细代码: 小程序的 ...

  3. NSTimer 实现时钟回调方法

    在开发过程中,发现时钟调用的地方比较多.所以对时钟进行了一个简单的统一封装.具体代码如下: 1.时钟回调函数的声明: #pragma mark 时钟回调处理 //时钟回调 +(NSTimer*) ls ...

  4. iOS 通知、本地通知和推送通知有什么区别? APNS机制。

    本地/推送通知为不同的需要而设计.本地通知对于iPhone,iPad或iPod来说是本地的.而推送通知——来自于设备外部.它们来自远程服务器——也叫做远程通知——推送给设备上的应用程序(使用APNs) ...

  5. spark-wordcount-sample算子测试

    import org.apache.spark.{SparkConf, SparkContext} object radomSampleU { def main(args: Array[String] ...

  6. selenium-Python之定位下拉框选择

    1.通过select 进行定位下拉框 下拉框如图所示 通过代码定位 #通过index进行选择Select(driver.find_element_by_id("cardType") ...

  7. SQL Server中变量的声明和使用方法

    声明局部变量语法:DECLARE @variable_name DataType其中 variable_name为局部变量的名称,DataType为数据类型.给局部变量赋值有两种方法:1.SET @v ...

  8. HDU 4291 A Short problem 短问题 (递推,找规律)

    题意: 给出递推式 g(n) = 3g(n - 1) + g(n - 2),且g(1) = 1,g(0) = 0.求g( g( g(n))) mod 109 + 7. 思路: 要求的g( g( g(n ...

  9. react中的setState的使用和深入理解

    前端框架从MVC过渡到MVVM.从DOM操作到数据驱动,一直在不断的进步着,提升着, angular中用的是watcher对象,vue是观察者模式,react就是state了,他们各有各的特点,没有好 ...

  10. CPP-基础:单目运算符重载

    关于++运算符前置和后置重载的实现实例: #include <iostream> using namespace std; //创建时钟类 class Clock { public: Cl ...