Allen_yang @ 2022-08-16 10:35:36
#include<iostream>
#include<algorithm>
using namespace std;
int x1[5005],r1[5005];
int main(){
int n,s,la,lb,cnt=0;
cin >> n >> s >> la >> lb;
for(int i=1;i<=n;i++){
cin >> x1[i] >> r1[i];
}
sort(x1+1,x1+1+n);
sort(r1+1,r1+1+n);
for(int i=1;i<=n;i++){
if(x1[i]<=lb || x1[i]<=lb+la){
if(s<0){
break;
}
cnt++;
s-=r1[i];
}
}
cout << cnt;
return 0;
}
by Allen_yang @ 2022-08-16 10:40:21
好像只蒙对了一个点。。。
by Lvyuze @ 2022-08-16 10:51:36
@Allen_yang 因为你排序时把苹果高度顺序和对应的力气顺序打乱了,就是不对应了,建议结构体做
by Lvyuze @ 2022-08-16 10:52:21
或者手写排序
by e4fsrc2e4fsrc2 @ 2022-08-16 10:55:50
#include<iostream>
#include<algorithm>
using namespace std;
struct apple{
int x,y;//x为苹果到地上的高度,y为摘这个苹果所花费的力气
}m[50005];
bool cmp(apple x,apple y){//排序让陶陶取苹果取得更多
if(x.y!=y.y)return x.y<y.y;//力气不等让力气小的在前
return x.x<y.x;//否则让矮的在前
}
int main(){
int a,b,s,n,sum=0;//sum记录他最多能摘几个苹果
cin>>n>>s>>a>>b;
for(int i=1; i<=n; i++)cin>>m[i].x>>m[i].y;
sort(m,m+n+1,cmp);
for(int i=1; i<=n; i++)
if(s>=m[i].y&&a+b>=m[i].x){s-=m[i].y;sum++;}//取苹果
cout<<sum;
return 0;
}
by Allen_yang @ 2022-08-16 14:06:05
@Lvyuze || @e4fsrc2e4fsrc2 谢诸位大佬,题已经AC