题解 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
- 下一篇: 原创《如何用vue来轻松的驾驭 html