其他分享
首页 > 其他分享> > 反悔贪心

反悔贪心

作者:互联网

参考好文

就是说反悔贪心有两种,一种反悔堆是需要你手动去把之前的劣解换出来,一种反悔自动机是通过类似网络流反向边一样的办法让你的贪心自动实现反悔。


P2949

贪心的选取大的,如果插不进去就把时间在它前面并且价值在它后面的换出来。为了手动换需要按时间顺序选,同时维护一个小根堆。

点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define in read()
inline int read(){
	int p=0,f=1;char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){p=p*10+c-48;c=getchar();}
	return p*f;
}
cs int N=100005;
int n,siz;
struct node{
	int p,d;
	bool operator<(const node &b){
		return d<b.d;
	}
}a[N];
priority_queue<int,vector<int>,greater<int>>q;
long long ans;
signed main(){
	n=in;
	for(int i=1;i<=n;i++)
		a[i].d=in,a[i].p=in;
	sort(a+1,a+1+n);
	for(int i=1;i<=n;i++)
		if(siz+1<=a[i].d)ans+=a[i].p,q.push(a[i].p),siz++;
		else if(q.top()<a[i].p)ans+=a[i].p-q.top(),q.pop(),q.push(a[i].p);
	cout<<ans;
	return 0; 
}

CF865D

贪心的让一个位置卖出时选择前面最小的买入,为了避免 1 2 100 的情况让这个位置卖出后在买入的堆里插入卖出位置的值,通过 \((a[k]-a[j])+(a[j]-a[i])=a[k]-a[i]\) 实现反悔。

点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define in read()
inline int read(){
	int p=0,f=1;char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){p=p*10+c-48;c=getchar();}
	return p*f;
}
cs int N=300005;
int n,siz,a[N];
priority_queue<int,vector<int>,greater<int>>q;
long long ans;
signed main(){
	n=in;
	for(int i=1;i<=n;i++)a[i]=in;
	for(int i=1;i<=n;i++){
		if(!q.empty()&&a[i]>q.top())
			ans+=a[i]-q.top(),q.pop(),q.push(a[i]);
		q.push(a[i]);
	}
	cout<<ans;
	return 0; 
}

BZOJ2151

贪心的选取美观度最大的位置并把前驱后缀删除,同时把原位置换成 \(a[pre]+a[nxt]-a[i]\),再次选到这个位置 ans 加上这个权值就代表去掉这个位置换成两边的。

点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define in read()
inline int read(){
	int p=0,f=1;char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){p=p*10+c-48;c=getchar();}
	return p*f;
}
cs int N=300005;
int n,m,a[N],pre[N],nxt[N];
struct node{
	int d,pos;
	friend bool operator<(const node &a,const node &b){
		return a.d<b.d;
	}
};
priority_queue<node>q;
long long ans;
bool vis[N];
signed main(){
	n=in,m=in;
	for(int i=1;i<=n;i++)a[i]=in,pre[i]=i-1,nxt[i]=i+1,q.push({a[i],i});
	pre[1]=n,nxt[n]=1;
	if(n/2<m)cout<<"Error!\n",exit(0);
	for(int i=1;i<=m;i++){
		int d=q.top().d,p=q.top().pos;q.pop();
		if(vis[p]){i--;continue;}
		ans+=d,q.push({a[p]=a[pre[p]]+a[nxt[p]]-d,p});
		vis[pre[p]]=1,vis[nxt[p]]=1;
		pre[p]=pre[pre[p]],nxt[pre[p]]=p,
		nxt[p]=nxt[nxt[p]],pre[nxt[p]]=p;
	}
	cout<<ans;
	return 0; 
}

标签:int,long,反悔,read,isdigit,贪心,cs,getchar
来源: https://www.cnblogs.com/llmmkk/p/16075914.html