Expedition
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 9952 |
|
Accepted: 2899 |
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.
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
* 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.
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个补给站,现在给出每个补给站到终点的距离,再给出每个补给站能补 充的能量。最后给出起点到终点的距离,和牛从起点出发所携带的能量。 已知牛每前进一单位距离就消耗一单位能量。 能量消耗完牛就死去。 现在求牛到达终点最少需要补给的次数,若牛不能到达终点输出-1.
题解: 可以把问题看成牛每经过一个站点就储存该站点的能量,等牛能量不够用时,就拿出能量使用。则要求拿出的能量次数最少,那么每次都拿出最大的能量包。 当储存的能量不足以支撑牛到达下一个站点时,则代表牛死亡。这里用优先队列来储存牛的能量,需要使用时,则将最大值出队列。
注意:此题给出的站点并不是有序的。
代码如下:
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
int dis,pow;//dis表示加油站到终点的距离
}a[10010];
int cmp(node a,node b)
{
return a.dis>b.dis;
}
int main()
{
int n,L,P,ans,i;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;++i)
scanf("%d%d",&a[i].dis,&a[i].pow);
sort(a,a+n,cmp);
a[n].dis=0; a[n].pow=0;//将终点也视为一个加油站,否则以下做法实际结果并没有到达终点
n++;
scanf("%d%d",&L,&P);
priority_queue<int>q;
int d,tank=P,pos=0;
ans=0;
int sign=0;
for(i=0;i<n;++i)
{
d=L-a[i].dis-pos;
while(tank-d<0)
{
if(q.empty())
{
sign=1;
break;
}
tank+=q.top();
q.pop();
ans++;
}
if(sign)
break;
tank-=d;
pos=L-a[i].dis;
q.push(a[i].pow);
}
if(sign)
printf("-1\n");
else
printf("%d\n",ans);
}
return 0;
}