题目描述
又是一年秋季时,陶陶家的苹果树结了 n 个果子。陶陶又跑去摘苹果,这次他有一个 a 公分的椅子。当他手够不着时,他会站到椅子上再试试。
这次与 NOIp2005 普及组第一题不同的是:陶陶之前搬凳子,力气只剩下 s了。当然,每次摘苹果时都要用一定的力气。陶陶想知道在 s<0 之前最多能摘到多少个苹果。
现在已知 n 个苹果到达地上的高度 xi,椅子的高度 a,陶陶手伸直的最大长度 b,陶陶所剩的力气 s,陶陶摘一个苹果需要的力气 yi,求陶陶最多能摘到多少个苹果。
输入格式
第 1行:两个数 苹果数 n,力气 s。
第 2 行:两个数 椅子的高度 a,陶陶手伸直的最大长度 b。
第 3行~第 3+n−1 行:每行两个数 苹果高度 xi,摘这个苹果需要的力气 yi。
输出格式
只有一个整数,表示陶陶最多能摘到的苹果数。
分析:
先摘需要力气小的苹果,创建一个结构体,对力气升序排序,再判断高度。
代码:
#include<bits/stdc++.h>
using namespace std;
struct pingguo
{
int x;
int y;
}app[5001];
bool paixu(pingguo a, pingguo b) {
if (a.y < b.y) return true;
if (a.y == b.y) {
if (a.x < b.x) {
return true;
}
}
return false;
}
int main() {
int n, s, a, b,sum=0;
int i;
cin >> n >> s;
cin >> a >> b;
for ( i = 1; i <= n; i++) {
cin >> app[i].x >> app[i].y;
}
sort(app + 1, app + 1 + n, paixu);
i = 1;
while (s > 0) {
if (app[i].x <= (a + b)) {
sum++;
s = s - app[i].y;
if (s < 0)sum--;
}
i++;
}
cout << sum;
return 0;
}