版本比较
浅谈反悔贪心
正文
增加 7 行删除 7 行
74 行未改动7576777879798081828347 行未改动131132133134135135136137138138139140141141142143144145146147148149150151149150151152153154155154 行未改动全部展开74 行未改动```cpp#include<bits/stdc++.h>using namespace std;#define int long longint n,ans=0; int n,ans=0;priority_queue<int,vector<int>,greater<int>>q;signed main(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>n;47 行未改动#include<bits/stdc++.h>using namespace std;#define int long longconst int N=2e5+10;int n,lst;//贪心,在尽量短的时间内修更多的建筑 int n,lst;//贪心,在尽量短的时间内修更多的建筑struct node{ int d,u; bool operator<(const node& x)const{return x.u>u;}//堆一直取耗时最大的,方便后续反悔 bool operator<(const node& x)const{return x.u>u;}//堆一直取耗时最大的,方便后续反悔}a[N];bool cmp(const node& x,const node& y){ return x.d<y.d;//a按截止时间排序,我们按它的截止顺序来处理 return x.d<y.d;//a按截止时间排序,我们按它的截止顺序来处理}priority_queue<node>q;signed main(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>n; for(int i=1;i<=n;i++)cin>>a[i].u>>a[i].d; sort(a+1,a+n+1,cmp); for(int i=1;i<=n;i++){//lst用来维护一定时间内,我们可以修几个 if(lst+a[i].u<=a[i].d)lst+=a[i].u,q.push(a[i]);//若当前建筑可以修就修,并将这个建筑的用时加入 else if(!q.empty()&&a[i].u<q.top().u)lst+=a[i].u-q.top().u,q.pop(),q.push(a[i]);//如果超时,但是当前建筑比堆中的更优,就优化掉 for(int i=1;i<=n;i++){//lst用来维护一定时间内,我们可以修几个 if(lst+a[i].u<=a[i].d)lst+=a[i].u,q.push(a[i]);//若当前建筑可以修就修,并将这个建筑的用时加入 else if(!q.empty()&&a[i].u<q.top().u)lst+=a[i].u-q.top().u,q.pop(),q.push(a[i]);//如果超时,但是当前建筑比堆中的更优,就优化掉 } cout<<q.size()<<"\n"; return 0;}154 行未改动全部展开