zoukankan      html  css  js  c++  java
  • POJ 2431 Expedition (贪心+优先队列)

    题目地址:POJ 2431

    将路过的加油站的加油量放到一个优先队列里,每次当油量不够时,就一直加队列里油量最大的直到能够到达下一站为止。

    代码例如以下:

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <stdlib.h>
    #include <math.h>
    #include <ctype.h>
    #include <queue>
    #include <map>
    #include <set>
    #include <algorithm>
    
    using namespace std;
    struct node
    {
        int d, p;
    }stop[11000];
    int cmp(node x, node y)
    {
        return x.d<y.d;
    }
    int main()
    {
        int n, i, j, l, p, flag=0, s, cnt;
        scanf("%d",&n);
        priority_queue<int>q;
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&stop[i].d,&stop[i].p);
        }
        scanf("%d%d",&l,&p);
        for(i=0;i<n;i++)
        {
            stop[i].d=l-stop[i].d;
        }
        stop[n].d=l;
        stop[n].p=0;
        sort(stop,stop+n+1,cmp);
        cnt=0;
        for(i=0;i<=n;i++)
        {
            if(stop[i].d<0) continue ;
            if(p<stop[i].d)
            {
                while(!q.empty()&&p<stop[i].d)
                {
                    p+=q.top();
                    q.pop();
                    cnt++;
                }
                if(p<stop[i].d)
                {
                    flag=1;
                    break;
                }
            }
            q.push(stop[i].p);
        }
        if(flag) puts("-1");
        else
            printf("%d
    ",cnt);
        return 0;
    }
    


  • 相关阅读:
    使用Xtrabackup 备份mysql数据库
    Myeclipse总结
    intellij idea问题及技巧
    Tomcat相关配置
    Spark常用算子总结
    前端开发经验
    最近用到的SQL语句
    subline text使用心得
    天龙八部谁是主角?(MR词频统计)
    elasticsearch CURL命令
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4481206.html
Copyright © 2011-2022 走看看