您的位置:首页 > 运维架构

bzoj1572 [Usaco2009 Open]工作安排Job(贪心)

2018-02-21 09:38 513 查看
一开始还naive了一发。。。菜不禁声

我们把工作按截止日期从小到大排序,维护一个做了的工作的收益的小根堆,以及当前时间now,如果这个工作已经超时了,贪心地去已经做了的工作里找一个收益最少的,如果比它收益多,就替换掉他。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define inf 0x3f3f3f3f
#define N 1000010
#define ll long long
inline char gc(){
static char now[1<<16],*S,*T;
if(T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
return x*f;
}
int n;ll ans=0;
struct data{
int d,w;
}a
;
inline bool cmp(data a,data b){return a.d==b.d?a.w>b.w:a.d<b.d;}
priority_queue<int,vector<int>,greater<int> >q;
int main(){
//  freopen("a.in","r",stdin);
n=read();
for(int i=1;i<=n;++i) a[i].d=read(),a[i].w=read();
sort(a+1,a+n+1,cmp);int now=0;
for(int i=1;i<=n;++i){
if(a[i].d<=now){
int x=q.top();if(a[i].w<=x) continue;
q.pop();ans+=a[i].w-x;q.push(a[i].w);continue;
}++now;ans+=a[i].w;q.push(a[i].w);
}printf("%lld\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: