bzoj 1572: [Usaco2009 Open] Job Arrangement [Greedy + Heap]

First add in chronological order, put the value into the small root pile, and subtract it from the pile when it is illegal.

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int N=100005;
int n,now;
priority_queue<long long>q;
struct use
{
    int t;
    long long v;
}a[N];
bool cmp(use a,use b)
{
    return a.t<b.t;
}
long long ans;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) 
        scanf("%d%lld",&a[i].t,&a[i].v);
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++)
    {
        ans+=a[i].v;
        now++;
        q.push(-a[i].v);
        if(now>a[i].t)
        {
            ans+=q.top();
            q.pop();
            now--;
        }
    }
    printf("%lld\n",ans);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324996160&siteId=291194637