简介易懂题解
2025-12-27 10:03:24
发布于:上海
1阅读
0回复
0点赞
可用结构体存储苹果的各个性质,进行排序找出最先摘的苹果
#include<iostream>
#include<algorithm>
using namespace std;
int n, s;
int a, b, cnt;
struct apple{
int x;
int y;
}w[5001];
bool cmp(apple a, apple b){
if(a.y == b.y){
return a.x < b.x;
}
return a.y < b.y;
}
int main(){
cin >> n >> s;
cin >> a >> b;
for(int i = 0; i < n; i++){
cin >> w[i].x >> w[i].y;
}
sort(w, w + n, cmp);
for(int i = 0; i < n; i++){
if(w[i].x <= a + b && s - w[i].y >= 0){
cnt++;
s -= w[i].y;
}
}
cout << cnt;
return 0;
}
全部评论 1
制作不易,留赞后去
2025-12-27 来自 上海
0






有帮助,赞一个