题解 P2949 【[USACO09OPEN]工作调度Work Scheduling】
生活随笔
收集整理的这篇文章主要介绍了
题解 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
总结
以上是生活随笔为你收集整理的题解 P2949 【[USACO09OPEN]工作调度Work Scheduling】的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 深入理解 Node.js 中 Event
- 下一篇: 指令——free