ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

反悔贪心

2022-03-30 11:01:13  阅读:181  来源: 互联网

标签:int long 反悔 read isdigit 贪心 cs getchar


参考好文

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


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有