跳到正文

算法·理论

贪心题单解析

zyx110824约 11 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月20日
篇幅
约 11 分钟
获赞
1

版本

  1. v1当前

简介

本文主要讲解题单贪心(基础)

P2240

部分背包:依据性价比排序,先装性价比高的.

AC code

C++
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int n,m;
double ans;
struct HLC{
    int m,v;
    bool operator <(const HLC b)const{return v*b.m>b.v*m;}
}a[105];
int read(){
    int ret=0,f=1;
    char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
    while(isdigit(ch))ret=ret*10+(ch&15),ch=getchar();
    return ret*f;
}
int main(){
    n=read();m=read();
    for(int i=1;i<=n;i++)a[i].m=read(),a[i].v=read();
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++){
        if(m>=a[i].m){ans+=a[i].v;m-=a[i].m;continue;}
        ans+=(double)m*a[i].v/a[i].m;break;
    }
    printf("%.2lf",ans);
    return 0;
}

P1223

第ii个人接水对后面人的影响为Ti∗(n−i)T_i*(n-i),对于排列中的每个人的n−in-i为定值,且不断变小,显然越靠前的人TiT_i权值越大,所以对TiT_i从大到小排序即可.

AC code

C++
#include<bits/stdc++.h>
using namespace std;
int n;
struct HLC{
	int x,id;
	bool operator <(const HLC b)const{return x<b.x;}
}a[1005];
long long ans;
int read(){
	int ret=0,f=1;
	char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
	while(isdigit(ch))ret=ret*10+(ch&15),ch=getchar();
	return ret*f;
}
int main(){
	n=read();
	for(int i=1;i<=n;i++)a[i].x=read(),a[i].id=i;
	sort(a+1,a+1+n);
	for(int i=1;i<=n;i++){
		printf("%d ",a[i].id);
		ans+=(n-i)*a[i].x;
	}putchar('\n');
	printf("%.2lf",(double)ans/n);
	return 0;
}

P1803

假设选取了第ii条线段,则所有aka_k(k∈[1,n]k\in[1,n]且k≠ik \ne i)小于bib_i的线段都不可取,那么显然当bib_i尽可能小的时候对答案影响最小,所以对bib_i从小到大排序.

AC code

C++
#include<bits/stdc++.h>
using namespace std;
int n,lst=-1,ans;
struct HLC{
    int l,r;
    bool operator <(const HLC b)const{return r<b.r;}
}a[1000005];
int read(){
    int ret=0,f=1;
    char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
    while(isdigit(ch))ret=ret*10+(ch&15),ch=getchar();
    return ret*f;
}
int main(){
    n=read();
    for(int i=1;i<=n;i++)a[i].l=read(),a[i].r=read();
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++){
        if(a[i].l<lst)continue;
        lst=a[i].r;ans++;
    }
    printf("%d",ans);
    return 0;
}

P1090

将第ii堆和第jj堆果子被合并,那么在接下来的合并中它们的权值还会被加入答案.那么一堆果子越早被合并,它就会被计算更多次.所以每次先挑最小的两个合并.用堆或集合实现即可.

AC code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,ans;
multiset<int> s;
int read(){
	int ret=0,f=1;
	char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
	while(isdigit(ch))ret=ret*10+(ch&15),ch=getchar();
	return ret*f;
}
signed main(){
	n=read();
	for(int i=1;i<=n;i++)s.insert(read());
	while(s.size()>1){
		int x=*s.begin();
		s.erase(s.begin());
		int y=*s.begin();
		s.erase(s.begin());
		ans+=x+y;
		s.insert(x+y);
	}
	printf("%lld\n",ans);
	return 0;
}

P3817

假设当前正在处理第ii个和第i+1i+1个盒子,如果ai+ai+1≤xa_i+a_{i+1}\le x显然不用删除.如果需要,那删i+1i+1可以使下一次处理需要删除的更少,所以一定更优.

警告:注意开longlonglonglong.

AC code

C++
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int n,x,a[100005];
ll ans;
int read(){
    int ret=0,f=1;
    char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
    while(isdigit(ch))ret=ret*10+(ch&15),ch=getchar();
    return ret*f;
}
int main(){
    n=read(),x=read();
    for(int i=1;i<=n;i++)a[i]=read();
    for(int i=1;i<n;i++){
        ll s=a[i]+a[i+1]-x;
        if(s<=0)continue;
        ans+=s;
        if(s<=a[i+1]){a[i+1]-=s;continue;}
        a[i+1]=0;s-=a[i+1];a[i]-=s;
    }
    printf("%lld",ans);
    return 0;
}

P1106

因为nn是一个高精度整数,所以把它转化为一个字符串.因为要使答案尽量小,所以每次使保留下来的高位尽量小,即在[lst+1,lst+p][lst+1,lst+p]的范围内选最小的保留.(lstlst表示上次留下的数的位置,pp表示还需删除的位数)

AC code

C++
#include<bits/stdc++.h>
using namespace std;
string s;
int a[255],k,n,len,cnt,st;
bool flg;
int read(){
    int ret=0,f=1;
    char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
    while(isdigit(ch))ret=ret*10+(ch&15),ch=getchar();
    return ret*f;
}
int main(){
    cin>>s;
    n=s.size();
    k=read();
    for(int i=0;i<n;i++)a[i+1]=s[i]-'0';
    st=1;len=n-k;
    while(cnt<len){
        cnt++;
        int op=0,Min=1<<20;
        for(int i=st;i<=st+k;i++)
        if(a[i]<Min)op=i,Min=a[i];
        if(a[op])flg=1;
        if(flg)printf("%d",a[op]);
        k-=op-st;
        st=op+1;
    }
    if(!flg)printf("0");
    return 0;
}

P1478

首先,因为要求最多摘几个苹果,不妨假设陶陶每次都站在凳子上,即b+ab+a.那么显然超过这个高度的苹果不用考虑.剩下的苹果价值都是11,所以肯定先挑省力的摘.

AC code

C++
#include<bits/stdc++.h>
using namespace std;
int n,m,a,b,tot,ans,c[5005];
int read(){
    int ret=0,f=1;
    char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
    while(isdigit(ch))ret=ret*10+(ch&15),ch=getchar();
    return ret*f;
}
int main(){
    n=read(),m=read();
    a=read(),b=read();
    for(int i=1;i<=n;i++){
        int x=read(),y=read();
        if(x>a+b)continue;
        c[++tot]=y;
    }
    sort(c+1,c+1+tot);
    for(int i=1;i<=tot&&m;i++){
        if(m<c[i])break;
        m-=c[i];ans++;
    }
    printf("%d",ans);
    return 0;
}

P5019

考虑当前需要铺平第ii个区域,所需天数dayiday_i与di−1d_{i-1}和did_i的关系为:

dayi={di−di−1di>di−10di≤di−1day_i = \begin{cases} d_i-d_{i-1} & d_i > d_{i-1}\\ 0 & d_i \le d_{i-1} \end{cases}

即能让前面帮我干就不自己干.

AC code

C++
#include<bits/stdc++.h>
using namespace std;
int n,lst,ans;
int read(){
    int ret=0,f=1;
    char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
    while(isdigit(ch))ret=ret*10+(ch&15),ch=getchar();
    return ret*f;
}
int main(){
    n=read();ans=lst=read();
    for(int i=2;i<=n;i++){
        int x=read();
        ans+=max(0,x-lst);lst=x;
    }
    printf("%d",ans);
    return 0;
}
原文