flowerdances @ 2024-07-31 21:11:54
#include<bits/stdc++.h>
using namespace std;
int p[5000002]={0};
int h,n,s,a,b,h1,ans=0,num=0;
int main(){
cin>>n>>s;
cin>>a>>b;
h1=a+b;
int i=1;
while(ans<=n){
ans++;
cin>>h>>p[i];
if(h<=h1){
i++;
}
}
n=i-1;
sort(p+1,p+1+n);
i=1;
while(s>0){
s=s-p[i];
if(s>0){
num++;
i++;
}}
cout<<num;
}
by haimingbei @ 2024-07-31 21:18:40
@flowerdances
贪心:用力气最小的苹果树
结构体好做一点
/*
1、P1478 陶陶摘苹果(升级版)
*/
#include<bits/stdc++.h>
using namespace std;
struct apple{
int high,strength;
}m[5010];
bool cmp(apple a,apple b){
return a.strength<b.strength;
}
int main(){
int n,s,a,b,ans=0;
cin>>n>>s>>a>>b;
int x,y;
for(int i=1;i<=n;i++)cin>>m[i].high>>m[i].strength;
sort(m+1,m+n+1,cmp);
for(int i=1;i<=n;i++){
if(s-m[i].strength>=0&&a+b>=m[i].high){
ans++;
s-=m[i].strength;
}
}
cout<<ans;
return 0;
}
by XiaoHongChong @ 2024-07-31 21:24:53
@flowerdances
#include<bits/stdc++.h>
using namespace std;
int p[5000002]={0};
int h,n,s,a,b,h1,ans=0,num=0;
int main(){
cin>>n>>s;
cin>>a>>b;
h1=a+b;
int i=1;
while(ans<=n){
ans++;
cin>>h>>p[i];
if(h<=h1){
i++;
}
}
n=i-1;
sort(p+1,p+1+n);
i=1;
while(s>0){
s=s-p[i];
if(s>=0){ //这里有点问题,如果他摘了这个苹果就没有力气了,那么也可以摘得。
num++;
i++;
}}
cout<<num;
}
我没有试过,但是好像就这个问题
by flowerdances @ 2024-07-31 21:57:57
@XiaoHongChong 不是这个问题,还是40分
by XiaoHongChong @ 2024-07-31 22:19:46
@flowerdances
我每天再看看怎么改
by XiaoHongChong @ 2024-08-01 17:11:51
@flowerdances
已AC
#include<bits/stdc++.h>
using namespace std;
int p[5000002]={0};
int h,n,s,a,b,h1,ans=1,num=0;
//ans的初始化有问题
int main(){
cin>>n>>s;
cin>>a>>b;
h1=a+b;
int i=1;
while(ans<=n){
ans++;
cin>>h>>p[i];
if(h<=h1){
i++;
}
}
n=i-1;
sort(p+1,p+1+n);
i=1;
while(s>0){
s=s-p[i];
if(s>=0){
//这里也有问题,如果他摘了这个苹果就没有力气了,那么也可以摘得。
num++;
i++;
}}
cout<<num;
}
by flowerdances @ 2024-08-02 20:45:22
@XiaoHongChong 谢谢!
by XiaoHongChong @ 2024-08-02 20:49:00
@flowerdances 不用谢