题解:P1478 陶陶摘苹果(升级版)
大意
有
请求出陶陶最多可以摘下的苹果数。
解析
已知陶陶手伸直的最大长度为
然后考虑贪心。我们可以把剩下的苹果按需要的力气值升序排序,从花费力气最小的开始摘,直到
代码
#include <cstdio>
#include <algorithm>
using namespace std;
struct apple{
int x, y;
} apples[5005];
bool cmp(apple a, apple b) {
return a.y < b.y;
}
int main() {
int n, a, b, s, ans = 0, m = 0; //ans为答案,m为筛选后的苹果个数
apple tmp; //当前输入的苹果数据
scanf ("%d %d\n%d %d", &n, &s, &a, &b);
for (int i = 1; i <= n; i++) {
scanf("%d %d", &tmp.x, &tmp.y);
if (tmp.x <= (a + b)) //筛选高度小于a+b的苹果
apples[++m] = tmp;
}
sort(apples + 1, apples + m + 1, cmp);
for (int i = 1; i <= m; i++) {
s -= apples[i].y;
if (s < 0) //力气耗尽
break;
else
ans++;
}
printf("%d", ans);
return 0;
}
过了。