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

bzoj1572 [Usaco2009 Open]工作安排Job

2018-02-20 17:15 459 查看
Description

Farmer John 有太多的工作要做啊!!!!!!!!为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单位时间。 他的工作日从0时刻开始,有1000000000个单位时间(!)。在任一时刻,他都可以选择编号1~N的N(1 <= N <= 100000)项工作中的任意一项工作来完成。 因为他在每个单位时间里只能做一个工作,而每项工作又有一个截止日期,所以他很难有时间完成所有N个工作,虽然还是有可能。 对于第i个工作,有一个截止时间D_i(1 <= D_i <= 1000000000),如果他可以完成这个工作,那么他可以获利P_i( 1<=P_i<=1000000000 ). 在给定的工作利润和截止时间下,FJ能够获得的利润最大为多少呢?答案可能会超过32位整型。

Input

第1行:一个整数N. 第2~N+1行:第i+1行有两个用空格分开的整数:D_i和P_i.

Output

输出一行,里面有一个整数,表示最大获利值。

Sample Input

3

2 10

1 5

1 7

Sample Output

17

HINT

第1个单位时间完成第3个工作(1,7),然后在第2个单位时间完成第1个工作(2,10)以达到最大利润

题意:每个工作花费1的时间 然后将会获得一些报酬 但是每个工作还有一个截止期限 现在求我最大获利是多少 那么我是刚了很久 才搞定 我的做法是首先假设所有工作我都做 按照截至日期排序 那么可以知道我现在等待做的队列里都有哪些东西 并且把他们按照费用的大小排列 如果我现在要做的时间超过了我等待队列最小的那个 那么我就替换一下 并且把他们差值加入 注意答案需要long long 即可

#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#define N 1100000
#define ll long long
using namespace std;
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<='9'&&ch>='0') x=x*10+ch-'0',ch=gc();
return x*f;
}
struct node{
int d,p;
}data
;
inline bool cmp1(const node &a,const node &b){
return a.d<b.d;
}
struct cmp{
inline bool operator()(const node &a,const node &b){
return a.p>b.p;
}
};
ll ans;int n;
priority_queue<node,vector<node>,cmp>q;
int main(){
freopen("bzoj1572.in","r",stdin);
n=read();for (int i=1;i<=n;++i) data[i].d=read(),data[i].p=read();
//for (int i=1;i<=n;++i) printf("%d\n",q.top().p),q.pop();
sort(data+1,data+n+1,cmp1);
for (int i=1;i<=n;++i){
if (data[i].d<=q.size()){
if(data[i].p>q.top().p){
ans+=data[i].p-q.top().p;q.pop();q.push(data[i]);
}
}else q.push(data[i]),ans+=data[i].p;
}
printf("%lld",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: