40分求助,调了半个钟头都没调出来啊,wzbl

P1478 陶陶摘苹果(升级版)

__KrNalty__ @ 2021-03-30 20:37:08

$40pts\ Code
#include <bits/stdc++.h>
using namespace std;
int n, s, a, b, addba;
struct Apples {
    int x, y;
} apple[5005];
bool cmp(Apples r, Apples w) {
    return r.y < w.y;
}
int main() {
    cin >> n >> s;
    cin >> a >> b;
    for (int i = 1; i <= n; i++) {
        cin >> apple[i].x >> apple[i].y;
    }
    addba = a + b;
    int j = 1, cnt = 0;
    sort(apple + 1, apple + n + 1, cmp);
    while (s >= 0) {
        if (j > n) {
            break;
        }
        if (apple[j].x <= addba) {
            s -= apple[j].y;
            cnt++;
        }
        j++;
    }
    cout << cnt;
    return 0;
}

by whhsteven @ 2021-03-30 21:16:48

判断每个果子是否可取时,不仅要判断是否能够到,还应判断剩余力气是否足够。

if(apple[j].x <= addba && apple[j].y <= s)


by レムです @ 2021-03-30 21:17:04

@whhsteven %%%!


by whhsteven @ 2021-03-30 21:18:47

@レムです 假死了


by __KrNalty__ @ 2021-03-31 20:43:44

谢@whhstevendalao解答


|