Expedition POJ

来源:互联网 发布:js array删除指定元素 编辑:程序博客网 时间:2024/05/17 23:16


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个加油站,第i个加油站距离终点Ai单位的距离,最多可以给卡车加Bi单位汽油。求卡车是否能到达终点,若可以,输出最少需要加多少次油,否则输出-1.


题解:假设{在到达加油站Ai时,就获得了一次在之后的社和时候到可以加Bi单位汽油的权利},则在每遇到一个加油站Ai,就将Bi单位汽油加入优先队列ms。当油箱空了时:如果优先队列也是空的,则无法到达终点;否则取出优先队列中的最大元素,给卡车加ms.top()单位油。

注意:因为题目给出的是加油站到达终点的距离,所以要先求出从起点到加油站的距离,然后从小到大排序。



#include<queue>#include<stdio.h>#include<algorithm>using namespace std;struct node{    int x,y;} que[10010];int cmp(node a,node b){    return a.x<b.x;}int main(){    int n,p,l,f;    while(~scanf("%d",&n))    {        priority_queue<int> ms;        for(int i=0; i<n; i++)            scanf("%d%d",&que[i].x,&que[i].y);        scanf("%d%d",&l,&p);        for(int i=0; i<n; i++)            que[i].x=l-que[i].x;        sort(que,que+n,cmp);        que[n].x=l;        que[n].y=0;        n++;        int pos=0,tmp=p,res=0;        f=1;        for(int i=0; i<n && f; i++)        {            int d=que[i].x-pos;            while(tmp-d<0)            {                if(ms.empty())                {                    printf("-1\n");                    f=0;                    break;                }                tmp+=ms.top();                ms.pop();                res++;            }            tmp-=d;            pos=que[i].x;            ms.push(que[i].y);        }        if(f)            printf("%d\n",res);    }    return 0;}


原创粉丝点击