#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
priority_queue<int,vector<int>,greater<int>>q;
const int N=1e5+10;
struct node {
int t,p;
}a[N];
int n;
bool cmp(node x,node y) {
return x.t<y.t;
}
int main(){
cin >> n;
for(int i=1;i<=n;i++) {
cin >> a[i].t >> a[i].p;
}
sort(a+1,a+n+1,cmp);
LL ans=0;
for(int i=1;i<=n;i++) {
if(a[i].t<=q.size()) {
if(a[i].p>q.top()) {
ans-=q.top();
q.pop();
q.push(a[i].p);
ans+=a[i].p;
}
}
else {
q.push(a[i].p);
ans+=a[i].p;
}
}
cout << ans << endl;
return 0;
}
这是一道贪心题,说到贪心我们想到的就是“骗分”,贪心就是一个寻找局部最优解的过程,从而达到整体最优解。接下来,我说一下我对于这道题的解法。首先,我们假设每个工作都做,我们按截至时间排序,如果有个时间能去做,我们不妨把当前的时间的价值压入一个小根堆中,我们比较接下来,如果不能做却价值比堆顶价值更高,我们就把该堆顶弹出,把这个价值高的压入当中。本道题我们用优先队列来维护。
代码: