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

    题意:已知某车距离城镇为L,油量为P,油箱无穷大,已知有n个加油量为x的加油站,问,车到城镇最少加几次油。若不能到达城镇,则输出-1。

    分析:

    1、贪心,先将加油站按照离城镇由远及近排序。

    2、卡车只要油够,就不断往前走,若当前油量不足以到达终点(或下一个加油站),则在之前经过的加油站里选择提供油量最大的加油站,加够了再继续走。

    #pragma comment(linker, "/STACK:102400000, 102400000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define Min(a, b) ((a < b) ? a : b)
    #define Max(a, b) ((a < b) ? b : a)
    const double eps = 1e-8;
    inline int dcmp(double a, double b){
        if(fabs(a - b) < eps) return 0;
        return a > b ? 1 : -1;
    }
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const int MAXN = 10000 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    int N, L, P;
    struct Node{
        int d, fuel;
        Node(){}
        void read(){
            scanf("%d%d", &d, &fuel);
        }
        void Set(int dd, int ff){
            d = dd;
            fuel = ff;
        }
        bool operator < (const Node& rhs)const{
            return d > rhs.d;
        }
    }num[MAXN];
    priority_queue<int> q;
    int solve(){
        int ans = 0;
        for(int i = 1; i <= N + 1; ++i){
            int tmp = num[i - 1].d - num[i].d;
            if(P - tmp >= 0) P -= tmp;
            else{
                while(!q.empty() && P - tmp < 0){
                    P += q.top();
                    q.pop();
                    ++ans;
                }
                if(P - tmp < 0) return -1;
                P -= tmp;
            }
            q.push(num[i].fuel);
        }
        return ans;
    }
    int main(){
        scanf("%d", &N);
        for(int i = 1; i <= N; ++i){
            num[i].read();
        }
        scanf("%d%d", &L, &P);
        sort(num + 1, num + 1 + N);
        num[0].Set(L, 0);
        num[N + 1].Set(0, 0);
        printf("%d\n", solve());
        return 0;
    }
    

      

  • 相关阅读:
    [导入]自由的生活
    [导入]宁静
    [导入]书店
    [导入]娶老婆的15条金科玉律
    [导入]静静的日子
    [导入]生活无聊的日子
    [导入]新的任务
    [导入]问题:我是一个内向的男生。请问怎么追求自己喜欢的女孩
    [导入]奋斗
    java 多种方式文件读取
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6547784.html
Copyright © 2011-2022 走看看