码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-07-16 22:37:40      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:scan   algorithm   但我   出现   ++   std   ret   lld   org   

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);
}

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

标签:scan   algorithm   但我   出现   ++   std   ret   lld   org   

原文地址:https://www.cnblogs.com/Randolph68706/p/11197967.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!