引入
有一个比较经典的问题:不限交易次数的股票交易。
给定一个数组prices,其中prices[i]表示某支股票在第i天的价格。要求设计一个算法来计算在满足一定条件下,通过多次买卖该股票所能获取的最大利润。对于一般情况下,我们都可以用动态规划来解决;但是题目说了:不限交易次数!
设想一下,假如我们不用DP,而是考虑使用贪心,应该怎么做?
贪心的局限性是只能用当前最优解贪出全局最优,但有时候已经存在某种情况使当前不是最优,我们可以考虑反悔!
即去掉不优的,反悔选其他的来使当前一直更优。但是注意:它的本质不是真的 “撤销” 已完成的交易,而是通过数学等价转换,将 “错过的更高卖点” 或 “过早的买点” 转化为更优的交易组合,最终效果与动态规划一致。
正文
T1
这题也算是比较经典的一类反悔贪心题目,思路就是先按截止日期排序,然后如果一个工作有时间去做,就先做了它,然后把它的价值塞入优先队列。
当我们找到一个没法做却价值比当前堆顶高的工作时,我们就放弃那个最小的工作,用做它的时间去做这个价值更高的工作。
代码非常简单啊:
code
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+10;
int n,ans=0;
struct node{
int d,v;
bool operator <(const node& x)const{return x.d>d;}
}a[N];
priority_queue<int,vector<int>,greater<int>>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].d>>a[i].v;
sort(a+1,a+n+1);
for(int i=1;i<=n;i++){
if(a[i].d<=q.size()){
if(a[i].v>q.top())ans-=q.top(),q.pop(),q.push(a[i].v),ans+=a[i].v;
}else q.push(a[i].v),ans+=a[i].v;
}
cout<<ans<<"\n";
return 0;
}
T2
那么这个应该怎么反贪?
发现只要当前优先队列里面还有元素(股票),且它没有当前的更优,那我们就将它反悔掉。
其实还有个比较直观的说法: ijjk
ik
这样看起来就合理多了,代码如下:
code
#include<bits/stdc++.h>
using namespace std;
#define int long long
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;
for(int i=1,x;i<=n;i++){
cin>>x;
if(!q.empty()&&q.top()<x)ans+=x-q.top(),q.pop(),q.push(x);
q.push(x);
}
cout<<ans<<"\n";
return 0;
}
T3
用堆维护“性价比”最低的任务,也就是价值最低的任务用于反悔。
由于每个任务的价值是一(对于答案都只能贡献一)的,所以性价比最低的任务就是耗时最长的任务。
如果不做耗时比较长的任务去做耗时比较短的任务,就能留下更多的时间给后面的任务,又由于每个任务的价值都是一的,所以这样做的正确性也是显然的。
code
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+10;
int n,lst;
struct node{
int use,d;
bool operator <(const node &x)const{return x.use>use;}
}a[N];
priority_queue<node>q;
bool cmp(const node& x,const node& y){
return x.d<y.d;
}
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].use>>a[i].d;
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++){
if(a[i].d>=lst+a[i].use)lst+=a[i].use,q.push(a[i]);
else if(a[i].use<q.top().use)lst-=q.top().use,q.pop(),q.push(a[i]),lst+=a[i].use;
}
cout<<q.size()<<"\n";
return 0;
}
//注释版如下:
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+10;
int n,lst;//贪心,在尽量短的时间内修更多的建筑
struct node{
int d,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按截止时间排序,我们按它的截止顺序来处理
}
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]);//如果超时,但是当前建筑比堆中的更优,就优化掉
}
cout<<q.size()<<"\n";
return 0;
}
扩展:双向链表反悔贪心
前置知识:链表例题
这个一般用于维护不能连续取(比如要求至少隔一个才能继续取)的问题,详见以下例题:
T1
这题一看其实想先打50分的暴力DP,但是很显然会MLE!
暴力code
#include<bits/stdc++.h>
using namespace std;
const int M=6e3+10,inf=-1e9;
int n,k,a[M],f[M][M/2][2],ans=0;
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++)cin>>a[i],f[i][1][1]=a[i];
for(int i=1;i<=n;i++){
for(int j=1;j<=k;j++){
if(j>=i)continue;
f[i][j][0]+=max(f[i-1][j][1],f[i-1][j][0]);
f[i][j][1]=a[i]+f[i-1][j-1][0];
}
}
for(int j=1;j<=k;j++)ans=max(ans,max(f[n][j][0],f[n][j][1]));
cout<<ans<<"\n";
return 0;
}当我们不知所措时,反贪此时就可以发挥出它的真正实力了!
显然,贪心选是肯定错的,考虑反悔。
那么怎么设置反悔策略呢?
可以发现,假设 是取一个时最优的,而 总是不能与 同时选的,这两个只能同时选或同时不选(因为在取两个时,要么 和 同时被选要么同时放弃才能满足贪心,因为如果只选其一的话,它们中的任意一个都比 小。)
此时,我们选了 ,就将它相邻两位合并。
所以当 被选时,我们就可以删去 和 ,并把 改成 ,重新找最大的。
根据以上策略,不难得出代码:
code
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=3e5+10;
struct Node{int val,l,r;}p[N];
struct node{
int val,id;
bool operator<(const node&x)const{return val<x.val;}
};
int n,k;bool vis[N];
priority_queue<node>q;
void del(int x){
int l=p[x].l,r=p[x].r;
vis[l]=vis[r]=1;
p[x].l=p[l].l;p[x].r=p[r].r;
p[p[l].l].r=x;p[p[r].r].l=x;
p[x].val=p[l].val+p[r].val-p[x].val;
q.push({p[x].val,x});
}
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
for(int i=1;i<=n;++i){cin>>p[i].val;p[i].l=i-1;p[i].r=i+1;q.push({p[i].val,i});}
p[0].val=p[n+1].val=-1e18;p[0].l=0;p[0].r=1;p[n+1].l=n;p[n+1].r=n+1;
int ans=0,cnt=0;
while(cnt<k&&!q.empty()){
auto tmp=q.top();q.pop();
int id=tmp.id;
if(vis[id]||p[id].val<0)continue;
ans+=p[id].val;cnt++;del(id);
}
cout<<ans<<"\n";
return 0;
}T2
可以发现,题目几乎和上一题一模一样,双向链表也是同样的作用,那么不难得出代码。
code
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e6+10,inf=1e18;
struct pl{
int val,l,r;
}p[N];
struct node{
int val,id;
bool operator <(const node& x)const{return x.val<val;}
};
int n,m,ans,ls;
bool vis[N];
priority_queue<node>q;
void del(int x){
p[x].l=p[p[x].l].l;
p[x].r=p[p[x].r].r;
p[p[x].l].r=x;
p[p[x].r].l=x;
}
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>m>>ls;
for(int i=1,x;i<n;i++)cin>>x,p[i].val=x-ls,ls=x,p[i].l=i-1,p[i].r=i+1,q.push({p[i].val,i});
p[0].val=p[n].val=inf;
for(int i=1;i<=m;i++){
while(vis[q.top().id])q.pop();
node tmp=q.top();
q.pop();
ans+=tmp.val;
vis[p[tmp.id].l]=vis[p[tmp.id].r]=1;
p[tmp.id].val=p[p[tmp.id].l].val+p[p[tmp.id].r].val-p[tmp.id].val;
q.push({p[tmp.id].val,tmp.id});
del(tmp.id);
}
cout<<ans<<"\n";
return 0;
}总结
只要题目满足以下共性,我们都可以考虑使用反悔贪心:
- 决策存在 “冲突 / 依赖”,局部最优≠全局最优(即不能普通贪心);
- 可通过 “替换已选元素”“合并冲突元素”“标记无效选择” 提供反悔路径;
- 需高效维护 “候选决策集”(如优先队列)和 “决策状态”(如链表)。