7.29 bzoj1029[JSOI2007]建筑抢修
2016-08-01 19:00
218 查看
题目描述
小刚在玩JSOI提供的一个称之为“建筑抢修”的电脑游戏:经过了一场激烈的战斗,T部落消灭了所有z部落的入侵者。但是T部落的基地里已经有N个建筑设施受到了严重的损伤,如果不尽快修复的话,这些建筑设施将会完全
毁坏。现在的情况是:T部落基地里只有一个修理工人,虽然他能瞬间到达任何一个建筑,但是修复每个建筑都需
要一定的时间。同时,修理工人修理完一个建筑才能修理下一个建筑,不能同时修理多个建筑。如果某个建筑在一
段时间之内没有完全修理完毕,这个建筑就报废了。你的任务是帮小刚合理的制订一个修理顺序,以抢修尽可能多
的建筑。
输入
第一行是一个整数N接下来N行每行两个整数T1,T2描述一个建筑:修理这个建筑需要T1秒,如果在T2秒之内还没有修理完成,这个建筑就报废了。
输出
输出一个整数S,表示最多可以抢修S个建筑.N < 150,000; T1 < T2 < maxlongint样例输入
4100 200
200 1300
1000 1250
2000 3200
样例输出
3 题解:贪心?贪心!!!我们发现结束时间最早的必须最早被考虑,否则就一定完成不了,由此启发,先把所有的按结束时间由小到大排序,按此顺序扫描,看当前时间能不能满足,如果能,算上并方案+1,如果不能但能通过取消前面的一个计划来达到目的,就取消,这个可以用priority_queue实现 #include <iostream>#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <memory.h>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <float.h>
using namespace std;
struct node{
int l,r;
} t[1500005];
priority_queue <int> p;
int n,i,ans,sum;
int cmp(const void *a,const void *b){
node *x=(node *)a,*y=(node *)b;
return x->r>y->r?1:-1;
}
int main(){
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d %d",&t[i].l,&t[i].r);
qsort(t+1,n,sizeof(t[0]),cmp);
i=1;
while(t[i].l>t[i].r) i++;
p.push(t[i].l);
sum+=t[i].l;
ans=1;
for(i++;i<=n;i++){
if(sum+t[i].l<=t[i].r){
sum+=t[i].l;
p.push(t[i].l);
ans++;
}
else if(t[i].l<p.top()){
sum=sum+t[i].l-p.top();
p.pop();
p.push(t[i].l);
}
}
printf("%d\n",ans);
return 0;
}
相关文章推荐
- bzoj1029 [JSOI2007]建筑抢修
- BZOJ1029 [JSOI2007] 建筑抢修
- [JSOI2007][BZOJ1029] 建筑抢修
- bzoj1029: [JSOI2007]建筑抢修
- BZOJ 1029: [JSOI2007]建筑抢修
- BZOJ_1029_ [JSOI2007]建筑抢修_贪心+堆
- BZOJ1029: [JSOI2007]建筑抢修
- BZOJ1029: [JSOI2007]建筑抢修
- BZOJ_1029_[JSOI2007]_建筑抢修_(贪心+优先队列)
- BZOJ 1029: [JSOI2007]建筑抢修
- 【堆,贪心】BZOJ1029 [JSOI2007]建筑抢修
- 【bzoj1029】[JSOI2007]建筑抢修
- [BZOJ1029] [JSOI2007]建筑抢修(贪心 + 优先队列)
- BZOJ 1029 [JSOI2007] 建筑抢修(贪心)
- 【BZOJ1029】[JSOI2007][贪心][堆]建筑抢修
- [bzoj] 1029 [JSOI2007]建筑抢修
- [BZOJ1029][JSOI2007]建筑抢修(multiset)
- bzoj 1029 [JSOI2007] 建筑抢修 题解
- [BZOJ 1029][JSOI2007]建筑抢修
- BZOJ 1029 [JSOI2007]建筑抢修