題解 P2949 【[USACO09OPEN]工做調度Work Scheduling】

P2949 [USACO09OPEN]工做調度Work Schedulingspa

題目標籤是單調隊列+dp,萌新太弱不會code

明顯的一道貪心題,考慮排序先作截止時間早的,但咱們發現後面可能會出現價值更高卻沒有時間作的狀況排序

咱們須要反悔的操做隊列

因而咱們想到用堆,若是當前放不下且當前價值高於已作工做中的最小价值,則刪去它加入當前值get

相似用堆實現反悔的貪心的經典題目:P1484 種樹it

#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);
}
相關文章
相關標籤/搜索