http://poj.org/problem?id=2431
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.
题目大意: 一个人要开卡车从起点到终点, 卡车每走n距离要消耗n的油, 路上有一些加油站,每个加油站都有固定的油量, 你可以认为卡车的油箱是无限大的,如果这个人可以从起点开到终点的话, 输出最少的加油次数; 否则输出-1。
思路: 首先题目给出的是加油站到终点的距离, 以及起点到终点的距离, 我们可以认为卡车从0开始走, 并把各加油站的距离修改成从0到该加油站的距离,同时把终点也看成加油站, 只不过油量为0。 (方便我们进行操作)这道题我们要用到贪心的思想, 不要认为只有在加油站i才能加j升油, 我们换一种思路:走过了当前加油站i, 就认为可以在任意时候加j升油。 这样我们用优先队列存储走过的加油站的油量, 当油不够时,就可以贪最大的来做。
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
struct node
{
int dis;
int v;
};
node a[10005];
priority_queue<int> q;
bool cmp(node a,node b)
{
return a.dis<b.dis;
}
int main()
{
int n,l,p;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d %d",&a[i].dis,&a[i].v);
int d,sum;
scanf("%d %d",&d,&sum);
for(int i=0;i<n;i++)
a[i].dis=d-a[i].dis; //转换
a[n].dis=d,a[n].v=0; //把终点当作加油站
sort(a,a+n+1,cmp);
int cur=0,cnt=0;
for(int i=0;i<=n;i++)
{
while(a[i].dis-cur>sum) //油不够
{
if(q.empty())
{
printf("-1\n");
return 0;
}
sum+=q.top();
q.pop();
++cnt;
}
sum-=a[i].dis-cur;
cur=a[i].dis;
q.push(a[i].v);
}
printf("%d\n",cnt);
return 0;
}