跳到正文

剪贴板

剪贴板 a4w35shk

原文
剪贴板信息

剪贴板信息

收录
更新

【14】贪心选择策略01(贪心的概念与使用)总结

介绍

贪心是一种常见的算法思想,它在每一步选择中都采取当前状态下最优的选择,以期望获得全局最优解。贪心法通常适用于问题具有最优子结构和贪心选择性质的情况。

贪心的使用条件

如果想要使用贪心,需要满足

1.最优子结构:问题的最优解可以通过子问题的最优解来构建。

2.贪心选择性质:在每一步选择中,都采取当前状态下的最优选择。

使用贪心解决问题的基本思路

step1:将问题抽象转化为数学问题,方便后面的理解和处理。

step2:设计(或猜测)贪心策略,也就是怎么贪。

step3:证明你的贪心策略,一般可以使用反证法或举反例。

step4:实现你的算法,解决问题。

贪心的实现一般化框架

代码
从问题的某一初始解出发;
while(需要或能够继续贪心){
    贪心求出当前的最优解;
}
由所有解元素组成成问题的一个可行解;

贪心的时间复杂度

因为贪心是“贪”,所以这个算法能够快速的求出答案,时间复杂度大约为O(ab)O(ab),a为需要贪心的次数,b为每一次贪心需要的时间复杂度。

大多数时候,贪心需要进行排序,所以时间复杂度是不好进行一般化的计算的。

贪心的优劣

我们可以对比一下贪心和暴力(搜索)的优劣:

从时间复杂度的角度来说,贪心是明显更高的,它能够解决较大数据的问题。

但是从思考深度来说,贪心需要设计、证明,而搜索只需按照要求枚举即可,显然贪心更难想。

从正确性来说也是如此,贪心可能会存在错误,而搜索是枚举思想,几乎不会存在错误。

不过,贪心还有一点好处,那就是代码量少,调试更方便。

经过多个方面的对比,我们可以思考一种较为周全的解决方案:

我们可以使用搜索一个std,再用贪心写一个测试代码去进行对拍,这样就可以保证贪心的正确性了。

练习与详解

1.例14.1-1 最优装载

思路:我们拿走性价比最高的东西。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
int n;
double t;
struct node{
	int m,v;
	bool operator<(const node &p)const{
		return m*1.0/v<p.m*1.0/p.v;
	}
}s[100005];
int main(){
	cin>>n>>t;
	for(int i=1;i<=n;i++){
		cin>>s[i].m>>s[i].v;
	}
	double ans=0;
	sort(s+1,s+n+1);
	for(int i=1;i<=n;i++){
		if(t>=s[i].m){
			t-=s[i].m;
			ans+=s[i].v;
		}
		else{
			ans+=t*1.0/s[i].m*s[i].v;
			break;
		}
	}
	cout<<fixed<<setprecision(2)<<ans;
	return 0;
}

2.例14.1-2 删数问题

思路:我们把数位从左到右的大小看成一座山峰,删掉在第一座峰谷上的数字,可以证明这样是正确的。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
string s;
int k;
int main(){
	cin>>s>>k;
	int len=s.size();
	s[len]='0';
	for(int i=1;i<=k;i++){
		for(int j=0;j<len;j++){
			if(s[j]>s[j+1]){
		    	s.erase(j,1);
		    	break;
			}
		}
	}
	len=s.size();
	while(s[0]=='0' and len>1){
		--len;
		s.erase(0,1);
	}
	cout<<s;
	return 0;
}

3.例14.1-3 均分纸牌

思路:我们求出这些纸牌的平均值,然后从左往右枚举,每一次向右边的元素要纸牌,使得自己能够成为均值,因为均值是这些数的平均数,所以最后一定能被刚好要完,在要纸牌的过程中统计答案即可。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,s[100001],ans;
signed main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>s[i];
		ans+=s[i];
	}
	ans/=n;
	int cnt=0;
	for(int i=1;i<=n;i++){
		if(s[i]!=ans){
			cnt++;
			s[i+1]+=(s[i]-ans);
		}
	}
	cout<<cnt;
	return 0;
}

4.例14.1-4 小A的糖果

思路:小A从编号为0的地方开始吃糖果,使得从0-i+1满足要求,每一次吃右边的那个,因为这样能够帮助下一次少吃一点。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,x,s[100001];
signed main(){
	cin>>n>>x;
	for(int i=1;i<=n;i++)cin>>s[i];
	int ans=0;
	for(int i=1;i<=n;i++){
		if(s[i]+s[i+1]>x){
			ans+=(s[i]+s[i+1]-x);
			s[i+1]-=(s[i]+s[i+1]-x);
		}
	}
	cout<<ans;
	return 0;
}

5.例14.1-5 修改

思路:我们每一个数向右找到第一个空位,记录一下跨度。然后我们把跨度最大的给到b最小的,把跨度最小的给到b最大的。最后统计答案即可。注意一下,我们使用ne数组记录一下进行快速跳跃,增加效率。还需要注意的一点是数据太大,需要使用unordered_map。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
const int N=1000005;
int n,s[N],b[N],cnt;
unordered_map<int,bool> mp;
unordered_map<int,int> ne;
inline int read(){
	int ans=0,j=1;
	char c=getchar();
	while(c>'9' or c<'0'){
		if(c=='-')j=-1;
		c=getchar();
	}
	while(c>='0' and c<='9'){
		ans=ans*10+c-'0';
		c=getchar();
	}
	return ans*j;
}
inline int ask(int x){
	if(mp[x]==0){
		mp[x]=1;
		return x;
	}
	int nx=ne[x];
	if(nx==0 or nx==x)nx=x+1;
	return ne[x]=ask(nx);

//	if(mp[x]==0){
//		mp[x]=1;
//		return x;
//	}
//	return ask(x+1);
}
int d[N],all=0;
signed main(){
	cin>>n;
	for(int i=1;i<=n;i++)s[i]=read();
	for(int i=1;i<=n;i++)b[i]=read();
    sort(s+1,s+n+1);
	sort(b+1,b+n+1);
	int use=0;
	for(int i=n;i>=1;i--){
		int p=ask(s[i])-s[i];
		if(p)d[++all]=p;
	}
	sort(d+1,d+all+1);
	for(int i=all;i>=1;i--)cnt+=d[i]*b[++use];
	cout<<cnt;
	return 0;
}

6.例14.1-6 分组

思路:我们把实力从小到大排序,每一次加一个人,面临两种情况:1.能够找到小组可以加这个人,选择这种小组中人数最少的那个进去。2.没有小组能够加这个人,那么他就自己开一个组。我们使用priority_queue来解决这个问题。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
struct node{
	int big,cnt;
	bool operator<(const node &p)const{
		if(big!=p.big)return big>p.big;
		return cnt>p.cnt;
	}
};
priority_queue<node> q;
int s[N],n;
inline int read(){
	int ans=0,j=1;
	char c=getchar();
	while(c>'9' or c<'0'){
		if(c=='-')j=-1;
		c=getchar();
	}
	while(c>='0' and c<='9'){
		ans=ans*10+c-'0';
		c=getchar();
	}
	return ans*j;
}
int main(){
	n=read();
	for(int i=1;i<=n;i++)s[i]=read();
	sort(s+1,s+n+1);
	int ans=2e9;
	for(int i=1;i<=n;i++){
		int x=s[i];
		while(!q.empty() and q.top().big+1<x){
			ans=min(ans,q.top().cnt);
			q.pop();
		}
		if(q.empty() or q.top().big==x)q.push({x,1});
		else{
			int w=q.top().cnt;
			q.pop();q.push({x,w+1});
		}
	}
	while(!q.empty()){
		ans=min(ans,q.top().cnt);
		q.pop();
	}
	cout<<ans;
	return 0;
}

练习

【14】贪心选择策略01(贪心的概念与使用)