可以反悔的贪心

第一次遇到这种题,但是我觉得还是不严谨,希望能御剑有同感的朋友交流一下。

https://www.luogu.org/problem/P4053

我写的代码是这样的:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
inline long long read() {
    char ch = getchar(); long long x = 0, f = 1;
    while(ch < '0' || ch > '9') {
        if(ch == '-') f = -1;
        ch = getchar();
    } while('0' <= ch && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    } return x * f;
}
struct build {
    ll a;
    ll t;

} datas [150001];
inline bool cmp (const build &a,const build &b) {
    return a.t < b.t;
}
priority_queue <int> q;
signed main(){
#ifndef ONLINE_JUDGE
    freopen ("shit.txt","r",stdin);
#endif 
#ifdef ONLINE_JUDGE
#endif
    int n = read ();
    for (int i = 1;i <= n;++ i) {
        datas [i].a = read ();
        datas [i].t = read ();
    }
    sort (datas + 1,datas + 1 + n,cmp);
    //开始贪心
    int nowt = 0;
    ll ans = 0,nowa = 0;
    for (int i = 1;i <= n;++ i) {
        //其实就是bfs 
        if (nowt + datas [i].a > datas [i].t) {
            //不满足 弄出来一个修复时间最长的然后替换 
            if (!q.empty () && q.top () > datas [i].a && nowt - q.top () + datas [i].a <= datas [i].t) {
                nowt -= q.top ();
                q.pop ();
                q.push (datas [i].a);
                nowt += datas [i].a;
            }
        }else {    //那就直接建造
            ++ ans;
            nowt += datas [i].a;
            q.push (datas [i].a); 
        }
    }
    cout << ans << endl;
    return 0;
}

但是我觉得还是很玄学。也说不出来为啥2333

猜你喜欢

转载自www.cnblogs.com/dorbmon/p/11666918.html