• POJ2431-Expedition【优先队列+贪心】


    题目大意:卡车每走一公里就消耗一单位的汽油,初始时给你p单位油,你要到达l距离的终点。其中路上有n个补给点可以加油,并且油箱容量无限大,问你最少可以停车几次。

    思路:因为油箱无限大,所以我们可以这么认为,我们路过一个加油站之后,我们在之后的路上随时可以选择加那个加油站的油,而且肯定是一次加完B_i,所以我们从汽车初始状态开始开,到没油了,看看路上路过有加油站没,选路过过油最多的,加上,继续这样。最后加油次数一定是最少的

    #include<cstdio>
    #include<queue>
    #include <iostream>
    #include<algorithm>
    using namespace std;
    const int maxn=10005;
    struct node
    {
        int d,f;
    };
    node a[maxn];
    
    bool cmp(node x,node y)
    {
        return x.d>y.d;
    }
    int main()
    {
        int n,l,p;
        while(scanf("%d",&n)!=EOF)
        {
            priority_queue<int> q;
            for(int i=0;i<n;++i)
                scanf("%d%d",&a[i].d,&a[i].f);
            sort(a,a+n,cmp);
            scanf("%d%d",&l,&p);
            int ans=0;
            q.push(p);
            int index=0;
            while(l>0 && !q.empty())
            {
                ++ans;
                int temp=q.top();
                q.pop();
                l-=temp;
                while(index<n && l<=a[index].d)
                {
                    q.push(a[index].f);
                    ++index;
                }
            }
            if(l<=0)
                printf("%d
    ",ans-1);
            else
                printf("-1
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    Helpers Overview
    Validation
    Support Facades
    Session Store
    位运算(参考百科)
    开源项目_可能使用到的开源项目集合
    秒杀系统架构分析与实战(转)
    shell命令之根据字符串查询文件对应行记录
    MySQL做为手动开启事务用法
    spring 加载bean过程源码简易解剖(转载)
  • 原文地址:https://www.cnblogs.com/aerer/p/9930929.html
Copyright © 2020-2023  润新知