• POJ2431 优先队列+贪心


    题目大意:

    见《挑战程序设计竞赛》P74。

    我的理解:

    优先队列+贪心

    注意把输入的距离(加油站到终点)改为起点到加油站。

    因为求得是最优解,需要尽可能少的加油站,所以我们每次希望去加油的时候 加最大的那个,因而将加油站push进priority_queue(堆结构,默认每次弹出最大值)

    在到达加油站 i 时,就获得了一次在之后的任何时候都可以在加 stop[i].second 单位汽油的权利。

    #include<iostream>
    #include<queue>
    using namespace std;
    const int maxn = 10000 + 10;
    
    typedef pair<int, int> P;
    int L, p, N;
    P stop[maxn];
    
    int cmp(P p1, P p2) {
        return p1.first < p2.first;
    }
    
    void solve() {
        //技巧
        stop[N].first = L;
        stop[N].second = 0;
        N++;
        priority_queue<int> que;
        int ans = 0, pos = 0, tank = p;
        for (int i = 0; i < N; i++) {
            int d = stop[i].first - pos;
            while (tank - d < 0) {
                if (que.empty()) {
                    puts("-1");
                    return;
                }
                tank += que.top();
                que.pop();
                ans++;
            }
            tank -= d;
            pos = stop[i].first;
            que.push(stop[i].second);
        }
        cout << ans << endl;
    }
    
    
    int main()
    {
        //freopen("in.txt", "r", stdin);
        while (scanf("%d", &N) != EOF) {
            for (int i = N - 1; i >= 0; i--) {
                cin >> stop[i].first >> stop[i].second;
            }
            cin >> L >> p;
            for (int i = 0; i < N; i++) {
                stop[i].first = L - stop[i].first;
                //cout<<A[i]<<" "<<B[i]<<" ";
            }
            sort(stop, stop + N, cmp);
            solve();
        }
        return 0;
    }
    
  • 相关阅读:
    codeforces round #433 div2
    bzoj1951
    bzoj3620
    bzoj2286
    bzoj1513
    bzoj4390
    codeforces round 430 div 2
    bzoj3339
    准备实现体积蒙皮
    看牛顿法的改进与验证局部收敛
  • 原文地址:https://www.cnblogs.com/RioTian/p/13073476.html
Copyright © 2020-2023  润新知