POJ 2431 Expedition 优先队列+贪心
来源:互联网 发布:生成雪碧图 for mac 编辑:程序博客网 时间:2024/05/22 15:11
Description
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
* 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
Sample Input
44 45 211 515 1025 10
Sample Output
2
Hint
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单位汽油,每向前行驶1单位距离消耗1单位汽油。如果在途中车上的汽油耗尽,
卡车就无法继续前行,即无法到达终点。途中共有N个加油站,加油站提供的油量有限,卡车的油箱无限大,无论加多少油都没问题。
给出每个加油站距离终点的距离和能够提供的油量,问卡车从起点到终点至少要加几次油?如果不能到达终点,输出-1。
思路:题目中让我们求最少加油次数不由得想起了贪心,况且题目中提到油箱无限大,那么我们不禁可以想到,如果该车里的汽油无法跑到下一个
加油站,那么我们就在让他在前面能加油量最大的加油站加油,即"在达到加油站i时,我们就可以认为获得了一次加Bi 油的机会”,在后面我们
需要加油时,就理解是在前面加油就可以了;所以用到了从大到小的优先队列;还有题中给出的是距终点的距离;
ac代码
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std;
struct node
{ int s;
int f;
friend bool operator<(node a,node b)
{return a.f<b.f;//从大到小;
}
}q[10010];
int cmp(node a,node b){
return a.s<b.s;
}
int main(){
int n,l,p,i;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d%d",&q[i].s,&q[i].f);
}
scanf("%d%d",&l,&p);
for(i=0;i<n;i++)
q[i].s=l-q[i].s;
q[n].s=l;
q[n++].f=0;
priority_queue<node>Q;
sort(q,q+n,cmp);
int rest=p;//刚开始有P单位的汽油
int pos=0,count=0;
for(i=0;i<n;i++){
int dis=q[i].s-pos;
while(rest-dis<0){
if(Q.empty())
{ count=-1;
break;
}
else
{ node x=Q.top();
rest+=x.f;
Q.pop();
count++;
}
}
if(count==-1)
break;
rest-=dis;
pos=q[i].s;
Q.push(q[i]);
}
printf("%d\n",count);
return 0;
}
- poj 2431 Expedition 贪心 + 优先队列
- POJ 2431 Expedition (优先队列+贪心)
- POJ 2431 Expedition (贪心 + 优先队列)
- poj 2431 Expedition 贪心 + 优先队列
- 2431Expedition POJ- 优先队列 + 贪心
- poj 2431 Expedition (贪心+优先队列)
- POJ 2431 Expedition (贪心+优先队列)
- POJ 2431 Expedition (贪心、优先队列)
- poj 2431 Expedition 贪心 + 优先队列
- poj 2431Expedition(优先队列+贪心)
- poj,2431Expedition 贪心 + 优先队列
- POJ 2431 Expedition (贪心、优先队列)
- Poj 2431 Expedition【优先队列+贪心】
- POJ 2431 Expedition 贪心 + 优先队列
- POJ 2431 Expedition 优先队列+贪心
- POJ 2431 Expedition 贪心 + 优先队列
- POJ 2431 Expedition (贪心+优先队列)
- POJ 2431 Expedition(贪心+优先队列)
- Android中Fragmeng的示例代码
- 最大公约数和最小公倍数问题
- 文件I/O
- 理解java的CompletionService
- 441. Arranging Coins
- POJ 2431 Expedition 优先队列+贪心
- 父母赴美签证办理
- nsoj 4647 表达式求值
- JavaScript对象数组的处理
- hdu 1016(dfs基础)
- PE 66 Diophantine equation(Pell方程)
- 硬盘分区小知识
- MySQL源码详解一 mysqld启动
- Vijos P1053 Easy sssp(SPFA 判负环)