贪心
__Deity_Ling__ · · 个人记录
贪心算法(Greedy Algorithm)是一种在每一步选择中都采取在当前状态下最好或最优(即最有利)的选择,从而希望导致结果是全局最好或最优的算法策略。贪心算法在每一步都做出一个局部最优的选择,希望这样的局部最优能累积成全局最优。
在C++中,贪心算法通常用于解决一些最优化问题,例如最小生成树(Prim和Kruskal算法)、单源最短路径(Dijkstra算法)、活动选择问题、霍夫曼编码等。贪心算法并不总是能得到全局最优解,但对某些问题特别有效,尤其是当问题具有贪心选择性质和最优子结构时。
例题
一 P1803线段覆盖
将比赛抽象成一条条在数轴上的线段,显然,从左往右放,右端点越靠左的越先放。因为这样的话,右端点越靠左所影响的线段越少,能放的就越多
代码
#include<bits/stdc++.h>
#define r read()
using namespace std;
inline __int128 read(){
char ch=getchar();__int128 x=0,f=1;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch&15);ch=getchar();}
return x*f;
}
const int N=1e6+10;
int n=r,ans,fre;
struct Node{
int start,End;
}bs[N];
bool cmp(Node bs1,Node bs2){return bs1.End<bs2.End;}
int main(){
for(int i=1;i<=n;i++) bs[i].start=r,bs[i].End=r;
sort(bs+1,bs+n+1,cmp);
for(int i=1;i<=n;i++){
if(i==1){
++ans;
fre=bs[i].End;
}
else{
if(fre<=bs[i].start){
++ans;
fre=bs[i].End;
}
}
}
cout<<ans<<endl;
return 0;
}
二P1094 纪念品分组
贪心策略:
将数组从小到大排序,定义两个指针i++,j++
证明
若贪心算法错误,即
-
a[i]+a[j]>w$但$a[j]$不是单独一组。$\because \forall 1\le i<j\le n,a[i]<a[j]
因此贪心正确
Code
#include<bits/stdc++.h>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#define r read()
using namespace std;
inline long long read(){
char ch=getchar();long long x=0,f=1;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
return x*f;
}
inline void write(long long x){
if(x<0) x=-x,putchar('-');
if(x>9) write(x/10);
putchar(x%10+'0');
}
int w=r,n=r,a[30005],i=1,j=n,ans;
int main(){
for(int i=1;i<=n;i++)a[i]=r;
sort(a+1,a+n+1);
while(i<=j){
if(a[i]+a[j]>w){
ans++;
j--;
}
else {
ans++;
i++;
j--;
}
}
printf("%d\n",ans);
return 0;
}
三P1106 删数问题
贪心策略 在左边m+1个数中,找到其中最小的数(如果有多个则选最左边的),记它的位置为t,显然,t左边的l个数都可以删掉,此时还需要删m-l个数,m重新赋值为m-l,而t右边的数是一个新的数列,进行同样的操作,直到选出了n-k个数为止。 注意本代码需要格外注意边界条件,必要时可以用特殊值法检验 Code
#include<bits/stdc++.h>
#define r read()
using namespace std;
inline __int128 read(){
char ch=getchar();__int128 x=0,f=1;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch&15);ch=getchar();}
return x*f;
}
const int N=255;
int n,m;
int tot; //第一个没有被删或者留下的地址
string s;
bool f;
int Search(int len){ //返回要留下的字符的地址
int loc,num=10;
for(int i=tot;i<=tot+len;i++)if(s[i]-'0'<num)num=s[i]-'0',loc=i;
if(num)printf("%d",num),f=true;
m-=loc-tot;
return loc;
}
int main(){
cin>>s;
m=r;
n=s.size();
while(m){
if(tot+m==n)break;//再删就删干净了
tot=Search(m)+1;
if(m==0&&tot<n){
f=true;
for(int i=tot;i<n;i++)cout<<s[i];//删除的机会用完了
break;
}
}
if(!f)cout<<"0";//全删了或留下来的全是0
return 0;
}
五P1090合并果子
每次取最小的果子合并即可
代码
#include<bits/stdc++.h>
using namespace std;
int n,ans=0;
priority_queue<int,vector<int>,greater<int> >q;
int main(){
scanf("%d",&n);
for(int i=1,x;i<=n;i++){
scanf("%d",&x);
q.push(x);
}
while(q.size()>1){
int x=q.top();q.pop();
int y=q.top();q.pop();
ans+=x+y;
q.push(x+y);
}
printf("%d\n",ans);
return 0;
}
六 哈夫曼树P2168荷马史诗
及构造哈夫曼编码,将单词出现的次数作为子树的权值,则第一问最终答案为树的所有叶子节点的带权路径长度之和,第二问答案即为最深叶子节点的深度。
Code
#include<bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<string>
#include<map>
#include<stack>
#include<vector>
#include<set>
#include<ctime>
#include<queue>
#define r read()
using namespace std;
inline __int128 read(){
char ch=getchar();__int128 x=0,f=1;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch&15);ch=getchar();}
return x*f;
}
inline void write(__int128 x){
if(x<0) x=-x,putchar('-');
if(x>9) write(x/10);
putchar(x%10+'0');
}
const int N=1e6+10;
long long n=r,k=r,ans,max_lenth;
struct Node{
long long val,h;
bool operator <(const Node &x)const{
if(val!=x.val)return x.val<val;
return x.h<h;
}
};
priority_queue<Node>q;
int main(){
for(int i=1;i<=n;i++){long long u=r;q.push((Node){u,0});}
while((n-1)%(k-1)){ //最后选的时后可能会剩几个,这里要补上几个0节点
++n;
q.push((Node){0,0});
}
while(q.size()>1){
long long sum=0,len=0;
for(int i=1;i<=k;i++){
Node x=q.top();q.pop();
sum+=x.val;
len=max(len,x.h);
}
q.push((Node){sum,len+1});
ans+=sum;
max_lenth=max(max_lenth,len+1);
}
printf("%lld\n%lld",ans,max_lenth);
return 0;
}
四P4447分组
练习
贪心策略,统计每一个数出现的次数,再从小到大遍历,若当前的数出现的次数不小于上一个数,则将他俩分到一组,一次类推。
感性证明:我们要尽量避免出现“一枝独秀”的情况,而上述过程中,峰的高度与其右边的数的高度差越来越小,因此避免了一枝独秀的情况。
Code
#include<bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<string>
#include<cstring>
#include<map>
#include<cstdio>
#include<stack>
#include<vector>
#include<set>
#include<algorithm>
#include<ctime>
#include<queue>
#include<bitset>
#define r read()
using namespace std;
inline __int128 read(){
char ch=getchar();__int128 x=0,f=1;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch&15);ch=getchar();}
return x*f;
}
inline void write(__int128 x){
if(x<0) x=-x,putchar('-');
if(x>9) write(x/10);
putchar(x%10+'0');
}
const int N=1e5+10;
int n=r,a[N],tot,len,ans=2e9;
struct Node{
int val,sum;
}b[N];
int main(){
for(int i=1;i<=n;i++)a[i]=r;
sort(a+1,a+n+1);
b[++tot]={a[1],1};
for(int i=2;i<=n;i++){
if(b[tot].val==a[i])++b[tot].sum;
else b[++tot]={a[i],1};
}
for(int i=1;i<=tot;i++){
while(b[i].sum){
--b[i].sum;
len=1;
while(b[i+len].val-b[i+len-1].val==1&&b[i+len].sum>=b[i+len-1].sum+1){
--b[i+len].sum;
++len;
}
ans=min(ans,len);
}
}
printf("%d\n",ans);
return 0;
}