洛谷 P4053 [JSOI2007]建筑抢修(反悔贪心)

传送门


解题思路

反悔贪心。按照结束时间排序,然后枚举结束时间,能修就修,不能修就比较以前修过的耗时最长的建筑和当前这个建筑比较是否更优。

注意这里用大根堆。

AC代码

 1 #include<iostream>
 2 #include<cmath>
 3 #include<cstring>
 4 #include<queue>
 5 #include<algorithm>
 6 #include<cstdio>
 7 using namespace std;
 8 const int maxn=150005;
 9 int n,ans;
10 long long last,now;
11 struct node{
12     long long t1,t2;
13 }a[maxn];
14 bool cmp(node a,node b){
15     return a.t1<b.t1;
16 }
17 priority_queue<long long> q;
18 int main(){
19     cin>>n;
20     for(int i=1;i<=n;i++) scanf("%lld%lld",&a[i].t2,&a[i].t1);
21     sort(a+1,a+n+1,cmp);
22     for(int i=1;i<=n;i++){
23         now=a[i].t1;
24         last+=now-a[i-1].t1;
25         if(last>=a[i].t2){
26             last-=a[i].t2;
27             q.push(a[i].t2);
28             ans++;
29         }else{
30             if(!q.empty()&&q.top()>a[i].t2){
31                 last+=q.top()-a[i].t2;
32                 q.pop();
33                 q.push(a[i].t2);
34             }
35         }
36     }
37     cout<<ans;
38     return 0;
39 } 

//JSOI 2007

posted @ 2020-11-04 20:49  尹昱钦  阅读(113)  评论(0编辑  收藏  举报