BZOJ1029: [JSOI2007]建筑抢修(贪心)

时间:2021-07-12 07:17:53

题目链接:BZOJ1029: [JSOI2007]建筑抢修

题解:贪心思想,按结束时间从小到大排序,选花费时间尽量短的建筑维修,用堆维护。

 #include<stdio.h>
#include<queue>
#include<algorithm>
using namespace std;
const int N = ;
struct node{
int t1, t2;
}s[N]; int cmp(node a, node b){
return a.t2 < b.t2;
} int main(){
int n, t1, t2, i, cur;
scanf("%d", &n);
for(i = ; i < n; ++i)
scanf("%d %d", &s[i].t1, &s[i].t2);
sort(s, s+n, cmp);
priority_queue<int>q;
for(cur = i = ; i < n; ++i){
if(cur + s[i].t1 < s[i].t2){
q.push(s[i].t1);
cur += s[i].t1;
}
else if(!q.empty()&& s[i].t1 < q.top()){
cur = cur -q.top() + s[i].t1;
q.pop();
q.push(s[i].t1);
}
}
printf("%d\n", q.size());
return ;
}