题目描述
又是一年秋季时,陶陶家的苹果树结了 n 个果子。陶陶又跑去摘苹果,这次他有一个 a 公分的椅子。当他手够不着时,他会站到椅子上再试试。
这次与 NOIp2005 普及组第一题不同的是:陶陶之前搬凳子,力气只剩下 s 了。当然,每次摘苹果时都要用一定的力气。陶陶想知道在 s<0 之前最多能摘到多少个苹果。
现在已知 n 个苹果到达地上的高度 xi,椅子的高度 a,陶陶手伸直的最大长度 b,陶陶所剩的力气 s,陶陶摘一个苹果需要的力气 yi,求陶陶最多能摘到多少个苹果。
输入格式
第 11 行:两个数 苹果数 n,力气 s。
第 22 行:两个数 椅子的高度 a,陶陶手伸直的最大长度 b。
第 33 行~第 3+n−1 行:每行两个数 苹果高度 xi,摘这个苹果需要的力气 yi。
输出格式
只有一个整数,表示陶陶最多能摘到的苹果数。
#include <cstdio>
#include <algorithm>
using namespace std;
struct Apple {
int x, y;
};
int n, s, a, b, ans;
Apple app[5005];
bool cmp(const Apple &a, const Apple &b) {
return a.y < b.y;
}
int main() {
scanf("%d%d", &n, &s);
scanf("%d%d", &a, &b);
for (int i = 0; i < n; i++)
scanf("%d%d", &app[i].x, &app[i].y);
sort(app, app + n, cmp);
for (int i = 0; i < n; i++)
if (app[i].x <= a + b)
if (app[i].y <= s) {
ans++;
s -= app[i].y;
}
printf("%d\n", ans);
return 0;
}