解题思路
我们将所有工作按截至日期升序排列,依次枚举,用一个变量 t o t tot tot记录现在花费的时间(每个工作花费时间为1),然后把所得利润放进堆中, t o t + + tot++ tot++,答案加上所得利润,如果tot比当前工作截止日期大,就把堆顶踢出来(堆顶的利润最小),答案减去所得利润,。
代码
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
int n,len,tot,tree[100010];
long long ans;
struct c{
int d,p;
}a[100010];
bool cmp(c l,c r){
return l.d<r.d;
}
void put(int x){
tree[++len]=x;
int son=len;
while(son>0)
{
int fa=son/2;
if(tree[fa]<=tree[son])
break;
swap(tree[fa],tree[son]);
son=fa;
}
}
int get(){
int fa=1,kl=tree[1];
tree[1]=tree[len++];
while(fa*2<=len)
{
int son=fa*2;
if(son+1<=len&&tree[son+1]<tree[son])
son++;
if(tree[fa]<=tree[son])
break;
swap(tree[fa],tree[son]);
fa=son;
}
return kl;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d%d",&a[i].d,&a[i].p);
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++)
{
put(a[i].p);
tot++;
ans+=a[i].p;
if(tot>a[i].d)
ans-=get();
}
printf("%lld",ans);
}