求助,为什么cmp函数<可以过,换成<=最后一个点就RE
查看原帖
求助,为什么cmp函数<可以过,换成<=最后一个点就RE
201499
OneMore14楼主2021/2/4 11:59
#include <iostream>
#include <climits>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <stack>
#include <algorithm>
#include <cstring>
#include <set>
#include <unistd.h>
#include <map>
#include <sys/wait.h>
#define ll long long
using namespace std;


int n, s;
int a, b;
struct Node {
    int h;
    int w;
};
int len = 0;
Node list[5005];

bool cmp(Node x, Node y) {
    return x.w < y.w;  // 这里如果是 <=, 最后一个点会RE
}
int main() {

    cin >> n >> s;
    cin >> a >> b;
    for (int i = 0; i < n; i++) {
        int h, w;
        cin >> h >> w;
        if (h <= (a + b)) {
            list[len].w = w;
            list[len].h = h;
            ++len;
        }
    }
    sort(list, list + len, cmp);
    int ans = 0;
    for (int i = 0; i < len; i++) {
        if (s >= list[i].w) {
            ans++;
            s -= list[i].w;
        }
        if (s < 0) {
            break;
        }
    }
    cout << ans << endl;
    return 0;
}

27行cmp函数里如果是<=,最后一个点就会RE,但换成<就不会,有什么说法吗?

2021/2/4 11:59
加载中...