传送门
考虑可以反悔的贪心。
按照 t 排序后,我们依次修复建筑物。
如果出现一个时间不足无法被修复的建筑物 i ,那么比较它与目前选中最大的 a j ,如果 a i < a j ,那么我们就不修复 j 了,转而修复 i ,这样可以为以后的修复争取时间。

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<queue> 
using namespace std;
const int N=150001;
int n,ti,ans=1;
priority_queue<int> pq;
struct data{
    int v,t;
}a[N];

void read(int &x){
    char ch=getchar();x=0;
    for(;ch<'0'||ch>'9';ch=getchar());
    for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
}

int cmp(const data q,const data w){
    return q.t<w.t;
}

int main(){
    read(n);
    for(int i=1;i<=n;i++) read(a[i].v),read(a[i].t);
    sort(a+1,a+1+n,cmp);
    pq.push(a[1].v);ti=a[1].v;
    for(int i=2;i<=n;i++)
     if (a[i].t-ti>=a[i].v) pq.push(a[i].v),ti+=a[i].v,ans++;
      else if (pq.top()>a[i].v){
        ti-=pq.top()-a[i].v;
        pq.pop();pq.push(a[i].v);
      }
    cout<<ans;
    return 0;
}