其他分享
首页 > 其他分享> > BZOJ 1572: [Usaco2009 Open]工作安排Job 贪心_堆_反悔

BZOJ 1572: [Usaco2009 Open]工作安排Job 贪心_堆_反悔

作者:互联网

Code:

#include<bits/stdc++.h>
#define setIO(s) freopen(s".in","r",stdin) 
#define maxn 2000000 
#define ll long long 
using namespace std;
priority_queue< ll , vector<ll>, greater<ll> >Q; 
struct Node
{
    ll val,tim; 
}key[maxn]; 
bool cmp(Node a,Node b)
{
    return a.tim < b.tim; 
}
int main()
{
  //   setIO("input"); 
    ll ans=0; 
    int n; 
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
    {
        scanf("%lld%lld",&key[i].tim,&key[i].val); 
    }
    sort(key+1,key+1+n,cmp); 
    for(int i=1;i<=n;++i)
    {
        if(key[i].tim <= (ll)Q.size())  
        {
            if(key[i].val > Q.top()) 
            {
                ans -= Q.top(); 
                ans += key[i].val; 
                Q.pop(); 
                Q.push(key[i].val); 
            }
        }
        else 
        {
            ans += key[i].val; 
            Q.push(key[i].val); 
        }
    }
    printf("%lld\n",ans); 
    return 0; 
}

  

标签:val,tim,int,ll,Usaco2009,Job,key,ans,1572
来源: https://www.cnblogs.com/guangheli/p/10977924.html