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