http://www.lydsy.com/JudgeOnline/problem.php?id=1029
把任务按截止时间从小到大排序
如果当前时间+当前任务耗时<=当前任务截止时间,把这个任务耗时放到大根堆里,ans++
否则 如果堆顶>当前任务耗时,删除堆顶,把这个任务耗时放到大根堆里
#include<queue>#include<cstdio>#include<iostream>#include<algorithm> #define N 150001using namespace std;struct node{ ???int len,endd;}e[N];priority_queue<int>q;void read(int &x){ ???x=0; char c=getchar(); ???while(!isdigit(c)) c=getchar(); ???while(isdigit(c)) { x=x*10+c-‘0‘; c=getchar(); ?} }bool cmp(node p,node q){ ???return p.endd<q.endd; }int main(){ ???freopen("repair.in","r",stdin); ???freopen("repair.out","w",stdout); ????int n; ???read(n); ???for(int i=1;i<=n;++i) read(e[i].len),read(e[i].endd); ???sort(e+1,e+n+1,cmp); ???long long now=0; ???int ans=0; ???for(int i=1;i<=n;++i) ???{ ???????if(now+e[i].len<=e[i].endd) ans++,q.push(e[i].len),now+=e[i].len; ???????else if(q.top()>e[i].len) now-=q.top()-e[i].len,q.pop(),q.push(e[i].len); ???} ???cout<<ans;}
1029: [JSOI2007]建筑抢修
Time Limit: 4 Sec Memory Limit: 162 MBSubmit: 5493 Solved: 2438
[Submit][Status][Discuss]
Description
小刚在玩JSOI提供的一个称之为“建筑抢修”的电脑游戏:经过了一场激烈的战斗,T部落消灭了所有z部落的
入侵者。但是T部落的基地里已经有N个建筑设施受到了严重的损伤,如果不尽快修复的话,这些建筑设施将会完全
毁坏。现在的情况是:T部落基地里只有一个修理工人,虽然他能瞬间到达任何一个建筑,但是修复每个建筑都需
要一定的时间。同时,修理工人修理完一个建筑才能修理下一个建筑,不能同时修理多个建筑。如果某个建筑在一
段时间之内没有完全修理完毕,这个建筑就报废了。你的任务是帮小刚合理的制订一个修理顺序,以抢修尽可能多
的建筑。
Input
第一行是一个整数N接下来N行每行两个整数T1,T2描述一个建筑:修理这个建筑需要T1秒,如果在T2秒之内还
没有修理完成,这个建筑就报废了。
Output
输出一个整数S,表示最多可以抢修S个建筑.N < 150,000; T1 < T2 < maxlongint
Sample Input
4
100 200
200 1300
1000 1250
2000 3200
100 200
200 1300
1000 1250
2000 3200
Sample Output
3
bzoj千题计划119:bzoj1029: [JSOI2007]建筑抢修
原文地址:http://www.cnblogs.com/TheRoadToTheGold/p/7898600.html