题解:[JSOI2007]建筑抢修

贪心加优先队列

先对每个建筑物的t值从小到大进行排序

当某个建筑物无法按时完成时

在已经选择的建筑物里选择比这个建筑物用时长的替换下来,并且放弃这个建筑物

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<queue>
 5 using namespace std;
 6 int n, ans, now;//now代表现在已经用了的时间
 7 struct s{
 8     int a, t;
 9 }qwq[150005];
10 bool cmp(s a, s b){
11     return a.t<b.t;
12 }
13 priority_queue <int> q;//大根堆
14 int main(){
15     scanf("%d",&n);
16     for(int i=1; i<=n; i++){
17         scanf("%d%d",&qwq[i].a,&qwq[i].t);
18     }
19     sort(qwq+1, qwq+n+1, cmp);
20     for(int i=1; i<=n; i++){
21         if(now+qwq[i].a>qwq[i].t){//无法修复
22             if(qwq[i].a<q.top()){
23                 now-=q.top();
24                 q.pop();
25                 q.push(qwq[i].a);
26                 now+=qwq[i].a;
27             }
28         }
29         else{//可以修复
30             q.push(qwq[i].a);
31             ans++;
32             now+=qwq[i].a;
33         }
34     }
35     cout<<ans<<endl;
36     return 0;
37 }
原文地址:https://www.cnblogs.com/Aze-qwq/p/9869820.html