您的位置:首页 > Web前端 > JavaScript

【bzoj1029】[JSOI2007]建筑抢修

2016-03-25 21:04 387 查看
按照t2从小到大排列之后贪心。
若当前任务可以插入,则插入。
若当前任务不可以插入,分两种情况:
①当前任务的耗时大于等于之前插入的任务的最大耗时:跳过当前任务
②当前任务的耗时小于之前插入的任务的耗时:将最前插入的耗时最大的那个任务删除,插入当前任务
用堆维护

#include<algorithm>

#include<iostream>

#include<cstdlib>

#include<cstring>

#include<cstdio>

#include<cmath>

#include<queue>

using
namespace
std;


#define MAXN 150010


priority_queue<
int
>q;


int
n;

int
i;

int
res,ans,tmp;


struct
Node

{

int
t,d;

}a[MAXN];


int
cmp(Node x,Node y)

{

return
x.d<y.d;

}


int
main()

{

scanf
(
"%d"
,&n);

for
(i=1;i<=n;i++)

scanf
(
"%d%d"
,&a[i].t,&a[i].d);

sort(a+1,a+n+1,cmp);

for
(i=1;i<=n;i++)

if
(res+a[i].t<=a[i].d)

{

ans++;

res+=a[i].t;

q.push(a[i].t);

}

else

{

tmp=q.top();

if
(a[i].t<tmp)

{

q.pop();

q.push(a[i].t);

res+=a[i].t-tmp;

}

}

printf
(
"%d"
,ans);

return
0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: