20分求dalao帮帮蒟蒻

P1478 陶陶摘苹果(升级版)

felixxiaoniu @ 2024-07-09 14:32:01

#include <bits/stdc++.h>
using namespace std;
int n, s, a, b, x, y, ans = 0, str[101];

int main() {
    cin >> n >> s >> a >> b;
    int tot = a + b;
    for (int i = 0; i < n; i++) {
        cin >> x >> y;
        if (x <= tot)
            str[b]++;
    }
    for (int i = 0; i <= 100 && s >= 0; i++) {
        while (str[i]) {
            s = s - i;
            ans++;
            str[i]--;
            if (s < 0) {
                ans--;
                break;
            }
        }
    }
    cout << ans;
    return 0;
}

by a18981826590 @ 2024-07-09 14:40:11

@felixxiaoniu

str[b]++;

改为

str[y]++;
if (s < 0) {
                ans--;
                break;
            }

改为

if(s<0){
    cout<<ans-1;
   return 0;
}

by felixxiaoniu @ 2024-07-09 14:49:48

@a18981826590 谢谢


|