题解 P2949 【[USACO09OPEN]工作调度Work Scheduling】

P2949 [USACO09OPEN]工作调度Work Scheduling

题目标签是单调队列+dp,萌新太弱不会

明显的一道贪心题,考虑排序先做截止时间早的,但我们发现后面可能会出现价值更高却没有时间做的情况

我们需要反悔的操作

于是我们想到用堆,如果当前放不下且当前价值高于已做工作中的最小价值,则删去它加入当前值

类似用堆实现反悔的贪心的经典题目:P1484 种树

#include<queue>
#include<cstdio>
#include<algorithm>
using namespace std;
struct thing {
    int t,v;
    bool operator <(const thing &b)const {
        return v>b.v;//小根堆
    }
} a[100005];
inline bool cmp(thing a,thing b) {
    return a.t<b.t;
}
int n;
long long ans;
priority_queue<thing> q;
int main() {
    scanf("%d",&n);
    for (int i=1; i<=n; i++) scanf("%d%d",&a[i].t,&a[i].v);
    sort(a+1,a+n+1,cmp);
    for (int i=1; i<=n; i++)
        if (a[i].t<=q.size()) {
            if (q.top().v<a[i].v) ans+=a[i].v-q.top().v,q.pop(),q.push(a[i]);
        } else q.push(a[i]),ans+=a[i].v;
    printf("%lld",ans);
}
原文地址:https://www.cnblogs.com/Randolph68706/p/11197967.html