题意
给出n个需要抢修的建筑,每个建筑抢修需要t1时间,并且要在t2之前完成抢修,问在时间S内能抢修的建筑最多有多少个。
解析
贪心,首先按t2排序,能修则修。然后不能的话每当有建筑时判断,如果这个建筑需要用时比之前的短,就替换。
#include <queue>
#include <cstdio>
#include <algorithm>
#define Rep( i , _begin , _end ) for(int i=(_begin);i<=(_end);i++)
#define For( i , _begin , _end ) for(int i=(_begin);i!=(_end);i++)
#define x first
#define y second
using std :: max;
using std :: min;
using std :: sort;
using std :: pair;
using std :: priority_queue;
typedef pair<int,int> aii;
const int maxx = 200000 + 25;
priority_queue < aii > quq;
aii a[maxx],b[maxx];
int n,ans,tmp;
int main(){
scanf("%d",&n);
Rep( i , 1 , n )
scanf("%d%d",&a[i].y,&a[i].x);
sort(a+1,a+n+1);
Rep( i , 1 , n )
b[i].y = a[i].x,b[i].x = a[i].y;
Rep( i , 1 , n ){
if(tmp + b[i].x <= b[i].y){
quq.push(b[i]);
ans ++;
tmp += b[i].x;
}
else{
if(quq.empty()) continue;
if(b[i].x < quq.top().x){
tmp -= (quq.top().x - b[i].x);
quq.pop();
quq.push(b[i]);
}
}
}
printf("%d",ans);
return 0;
}