Expedition

题目:你需要驾驶一辆卡车行驶L单位距离。最开始时,卡车上有P单位汽油。卡车每开1单位距离需要消耗1单位的汽油。如果在途中汽车上的汽油耗尽,卡车就无法继续前行,因而无法到达终点。在途中一共有N个加油站。第I个加油站在距离终点Ai单位距离的地方,最多可以给卡车加Bi单位汽油。假设卡车的燃料箱的容量是无限大的,无论加多少油都没问题。那么请问卡车能否到达终点?如果可以,最少需要加多少次油?如果可以到达终点输出最少的加油次数,否则输出-1。
思路:将距离终点站的距离按从大到小排序,利用优先队列存储经过的加油站,并选择加油站加油量最多的加油。

#include <iostream>  
#include <cstdio>  
#include <algorithm>  
#include <queue>  
using namespace std;  

const int maxn = 10005;
int n,L,P;
struct node  
{  
    int ai, bi;  
};  
node arr[maxn];
bool cmp(const node &a, const node &b)  
{  
    return a.ai > b.ai;  
}  
int main()  
{  

    while(scanf("%d", &n) != EOF)  
    {  
        priority_queue<int> q;  
        for(int i = 0; i < n; i++)  
            scanf("%d %d", &arr[i].ai, &arr[i].bi);  
        sort(arr, arr + n, cmp);  
        scanf("%d %d", &L, &P);  
        int t = 0;  
        q.push(P);  
        int index = 0;  
        while(L > 0 && !q.empty())  
        {  
            t++;  
            int tmp = q.top();  
            q.pop();  
            L -= tmp;  
            while(index < n && L <= arr[index].ai)  
                q.push(arr[index++].bi);  
        }  
        if(L<=0){
            printf("%d\n",t-1);
        }else{
            printf("%d\n",-1);          
        }
    }  

    return 0;  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值