浅谈反悔贪心
forever516 · · 算法·理论
引入
有一个比较经典的问题:不限交易次数的股票交易。
给定一个数组prices,其中prices[i]表示某支股票在第i天的价格。要求设计一个算法来计算在满足一定条件下,通过多次买卖该股票所能获取的最大利润。
对于一般情况下,我们都可以用动态规划来解决;但是题目说了:不限交易次数!
设想一下,假如我们不用DP,而是考虑使用贪心,应该怎么做?
贪心的局限性是只能用当前最优解贪出全局最优,但有时候已经存在某种情况使当前不是最优,我们可以考虑反悔!
即去掉不优的,反悔选其他的来使当前一直更优。但是注意:它的本质不是真的 “撤销” 已完成的交易,而是通过数学等价转换,将 “错过的更高卖点” 或 “过早的买点” 转化为更优的交易组合,最终效果与动态规划一致。
正文
T1
这题也算是比较经典的一类反悔贪心题目,思路就是先按截止日期排序,然后如果一个工作有时间去做,就先做了它,然后把它的价值塞入优先队列。
当我们找到一个没法做却价值比当前堆顶高的工作时,我们就放弃那个最小的工作,用做它的时间去做这个价值更高的工作。
代码非常简单啊: ::::info[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
那么这个应该怎么反贪?
发现只要当前优先队列里面还有元素(股票),且它没有当前的更优,那我们就将它反悔掉。
其实还有个比较直观的说法:
天买的股票在第
天卖,再在第
天买回来,在第
天卖掉。$
天买的股票在第
天卖。$
这样看起来就合理多了,代码如下: ::::info[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
用堆维护“性价比”最低的任务,也就是价值最低的任务用于反悔。
由于每个任务的价值是一(对于答案都只能贡献一)的,所以性价比最低的任务就是耗时最长的任务。
如果不做耗时比较长的任务去做耗时比较短的任务,就能留下更多的时间给后面的任务,又由于每个任务的价值都是一的,所以这样做的正确性也是显然的。 ::::info[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! ::::info[暴力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;
}
:::: 当我们不知所措时,反贪此时就可以发挥出它的真正实力了!
显然,贪心选是肯定错的,考虑反悔。
那么怎么设置反悔策略呢?
可以发现,假设
是取一个时最优的,而
总是不能与
同时选的,这两个只能同时选或同时不选(因为在取两个时,要么
和
同时被选要么同时放弃才能满足贪心,因为如果只选其一的话,它们中的任意一个都比
小。)
此时,我们选了
被选时,我们就可以删去
和
改成