题意理解
这题就是说,你要能按照摘苹果需要力气的大小排序,需要小的在前面,这样才能贪一波。
代码
#include <cstring>
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct {
int x; // height
int y; // strength
} Apple;
const int maxn = 5050;
Apple apple[maxn];
int n, s;
int a, b;
void read(int &x) {
x = 0;
int f = 1;
char ch = getchar();
while(ch > '9'||ch < '0') {
if(ch == '-') {
f = -1;
}
ch = getchar();
}
while(ch >= '0' && ch <= '9') {
x = x * 10 + (int)(ch - 48);
ch = getchar();
}
x = x * f;
}
int cmp(Apple a1, Apple a2) {
return a1.y < a2.y;
}
int main() {
read(n);
read(s);
read(a);
read(b);
for(int i = 0; i < n; i++) {
read(apple[i].x);
read(apple[i].y);
}
sort(apple, apple + n, cmp);
int cnt = 0;
for(int i = 0; i < n; i++) {
if(s >= apple[i].y) {
if(a + b >= apple[i].x) {
s -= apple[i].y;
cnt++;
}
} else {
break;
}
}
cout << cnt << endl;
return 0;
}
欢迎加入“不会算法一群菜鸟”,群号是⑥⑥①⑨②2025,这是我设置的一道很低的门槛用来阻止广告的。入群的验证暗号是:我爱编译原理