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

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.

Source

挑战上的74页

<span style="font-size:24px;"><a target=_blank href="http://poj.org/searchproblem?field=source&key=USACO+2005+U+S+Open+Gold" style="text-decoration: none;">#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
struct Node{
int d;
int v;
bool operator <(Node b) const{
return v<b.v;
};
}node[10005];
bool cmp(Node a,Node b)
{
return a.d<b.d;
}
int main()
{
int n,s,p;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d %d",&node[i].d,&node[i].v);
scanf("%d %d",&s,&p);
for(int i=1;i<=n;i++)
node[i].d=s-node[i].d;
sort(node+1,node+n+1,cmp);
priority_queue<Node> q;
n++;
node[n].v=0;node[n].d=s;//为了方便,将终点也设成一个加油站
int t=p,cnt=0;
for(int i=1;i<=n;i++)
{
int x=node[i].d-node[i-1].d;
while(x>t) /*刚开始是写成if,,wrong 了无数次,后来才发现到达某个点不一定是取一个加油站,可以取多个然后过该点。*/</a></span>
<span style="font-size:24px;"><a target=_blank href="http://poj.org/searchproblem?field=source&key=USACO+2005+U+S+Open+Gold" style="text-decoration: none;">            {
if(q.empty())
{
printf("-1\n");
return 0;
}
t+=q.top().v;
q.pop();
cnt++;
}
q.push(node[i]);/*能到达该点,就能在它这里加油,因此入维护油量最大的优先队列*/
t-=x;
}
printf("%d\n",cnt);
return 0;
}</a><a target=_blank href="http://poj.org/searchproblem?field=source&key=USACO+2005+U+S+Open+Gold">
</a></span>

poj 2431 Expedition 贪心+优先队列 很好很好的一道题!!!的更多相关文章

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

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

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

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

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

    题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...

  4. POJ 2431 Expedition(优先队列、贪心)

    题目链接: 传送门 Expedition Time Limit: 1000MS     Memory Limit: 65536K 题目描述 驾驶一辆卡车行驶L单位距离.最开始有P单位的汽油.卡车每开1 ...

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

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

  6. poj 2431 Expedition 贪心

    简单的说说思路,如果一开始能够去到目的地那么当然不需要加油,否则肯定选择能够够着的油量最大的加油站加油,,不断重复这个贪心的策略即可. #include <iostream> #inclu ...

  7. POJ 2431 Expedition 贪心 优先级队列

    Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30702   Accepted: 8457 Descr ...

  8. POJ 2431 Expedition(探险)

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

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

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

随机推荐

  1. linux下安装anconda3

    Liunx下安装anconda3其实特变简单,只需这几步骤 1.  首先要到网上下载一个ancoda3的安装包.然后在上传到linux上面即可 3. 是不是特别简单呢?哈哈

  2. P3748 [六省联考2017]摧毁“树状图”

    传送门 显然是可以树形 $dp$ 的 对每个节点维护以下 $5$ 个东西 $1.$ 从当前节点出发往下的链的最大贡献 $2.$ 节点子树内不经过本身的路径最大贡献 $3.$ 节点子树内经过本身的路径的 ...

  3. http请求204

    项目中发现一个奇怪的问题,请求的时候同一个接口有两个请求,而且有一个状态为204,有一个为200 在网上查看资料后得知,是因为跨域而引起的,OPTIONS是一种“预检请求”,浏览器在处理跨域访问的请求 ...

  4. Express bodyParser中间件使用方式

    bodyParser中间件用来解析http请求体,是express默认使用的中间件之一. 1.这个模块提供以下解析器 (1) JSON body parser (2) Raw body parser ...

  5. Jquery 学习-菜鸟教程

    jquery效果和元素选择 //元素选择 $(this).hide(); $("p.test") //隐藏所有class="test"的<p>元素 ...

  6. https://bbs.ichunqiu.com/thread-48915-1-1.html

    使用BurpSuite进行双文件上传拿Webshell 首先进入网站后台:(后台界面应该是良精CMS) <ignore_js_op> 在 添加产品 这一栏有个上传文件: <ignor ...

  7. KVM命令记录

    创建qcow2镜像qemu-img create -f qcow2 /vm/kvm/img/vm41.img 500G 创建虚拟机virt-install --name=vm41 --disk pat ...

  8. Delphi 画笔

    樊伟胜

  9. js常用骚操作总结

    打开网址 window.open("http://www.runoob.com"); 判断是否为url var url = $("#url").val(); i ...

  10. 简单了解Linux文件目录

    /bin :获得最小的系统可操作性所需要的命令 /boot :内核和加载内核所需的文件 /dev :终端.磁盘.调制解调器等的设备项 /etc :关键的启动文件和配置文件 /home :用户的主目录 ...