简介
本文主要讲解题单贪心(基础)
P2240
部分背包:依据性价比排序,先装性价比高的.
AC code
#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
第个人接水对后面人的影响为,对于排列中的每个人的为定值,且不断变小,显然越靠前的人权值越大,所以对从大到小排序即可.
AC code
#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
假设选取了第条线段,则所有(且)小于的线段都不可取,那么显然当尽可能小的时候对答案影响最小,所以对从小到大排序.
AC code
#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
将第堆和第堆果子被合并,那么在接下来的合并中它们的权值还会被加入答案.那么一堆果子越早被合并,它就会被计算更多次.所以每次先挑最小的两个合并.用堆或集合实现即可.
AC code
#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
假设当前正在处理第个和第个盒子,如果显然不用删除.如果需要,那删可以使下一次处理需要删除的更少,所以一定更优.
警告:注意开.
AC code
#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
因为是一个高精度整数,所以把它转化为一个字符串.因为要使答案尽量小,所以每次使保留下来的高位尽量小,即在的范围内选最小的保留.(表示上次留下的数的位置,表示还需删除的位数)
AC code
#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
首先,因为要求最多摘几个苹果,不妨假设陶陶每次都站在凳子上,即.那么显然超过这个高度的苹果不用考虑.剩下的苹果价值都是,所以肯定先挑省力的摘.
AC code
#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
考虑当前需要铺平第个区域,所需天数与和的关系为:
即能让前面帮我干就不自己干.
AC code
#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;
}