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. A20地址线科普【转载】

    1981 年8 月,IBM 公司最初推出的个人计算机IBM PC 使用的CPU 是Intel 8088.在该微机中地址线只有20 根(A0 – A19).在当时内存RAM 只有几百KB 或不到1MB ...

  2. LINUX运行级别的原理

    在目录 /etc/rc.d/init.d 下有许多服务器脚本程序,一般称为服务(service), 在 /etc/rc.d 下有 7 个名为 rcN.d 的目录,其中 N=0-6,对应于系统的 7 个 ...

  3. 网站被攻击扫描SQL注入的日常记录

    我发了个博客,泄露了域名之后,便有人疯狂的尝试攻击我的站点,奈何我防守做得比较好,直接把网段封了,看到403还锲而不舍,我真是想给他颁奖了 查看ua,发现很多sqlmap的ua,肯定会是被刷了,只是运 ...

  4. springmvc java程序文件保存地址的路径问题

    会保存为这种斜杠 不论之前填写的是什么样

  5. go语言:获取字符串长度

    go语言字符串底层由字节数组实现,使用UTF-8编码,初始化以后不能被修改 获取字符串长度 一.当字符串中所有字符都是单字节字符时,使用 len 函数获取字符串的长度 package main imp ...

  6. Python实例 遍历文件夹和文件

    import  os import  os.path #  os,os.path里包含大多数文件访问的函数,所以要先引入它们. #  请按照你的实际情况修改这个路径 rootdir  =   &quo ...

  7. Python科学计算生态圈--Scipy

  8. 洛谷P1003 [NOIP2011提高组Day1T1]铺地毯

    P1003 铺地毯 题目描述 为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有 n 张地毯,编号从 1 到n .现在将这些地毯按照编号 ...

  9. 当移动数据分析需求遇到Quick BI

    我叫洞幺,是一名大型婚恋网站“我在这等你”的资深老员工,虽然在公司五六年,还在一线搬砖.“我在这等你”成立15年,目前积累注册用户高达2亿多,在我们网站成功牵手的用户达2千多万.目前我们的公司在CEO ...

  10. web服务发展历程

    PhP发展历史1.php: 开始名字含义:personal home page 个人网页 现在名字含义:HyperText Perprocessor 超文本预处理语言 预处理: 说明PHP是在服务器预 ...