P1728 陶陶摘苹果题解

题目传递门

总结 :
1、贪心+排序
2、结构体

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N = 5010;
int n, s; //苹果数 n,力气 s
int a, b; //椅子的高度a,陶陶手伸直的最大长度b。

struct apple {
    int x, y;
} apples[N];

bool cmp(const apple &a, const apple &b) {
    return a.y < b.y;
}

int cnt;    //结果数

//每行两个数 苹果高度 xi,摘这个苹果需要的力气yi
int main() {
    cin >> n >> s >> a >> b;
    for (int i = 1; i <= n; i++) cin >> apples[i].x >> apples[i].y;
    //排序
    sort(apples + 1, apples + 1 + n, cmp);

    for (int i = 1; i <= n; i++)
        if (a + b >= apples[i].x && s >= apples[i].y) {//能够的到,而且力气够用
            cnt++;
            s -= apples[i].y;
        }
    //输出大吉
    cout << cnt << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/littlehb/p/15035243.html