疑问悬关
查看原帖
疑问悬关
513937
zazic楼主2025/7/25 20:45
#include <bits/stdc++.h>
using namespace std;
const int N = 150010;
int n;
int f[N];
struct node
{
    int l, r;
} Le[N];

bool cmp(node x, node y)
{
    return x.r < y.r;
}

int lower_bound(int l, int r, int key)
{
    int ans = 0;
    while (l <= r)
    {
        // cout << " ? " << endl;
        int mid = (l + r) >> 1;
        if (Le[mid].r < key)
            ans = mid, l = mid + 1;
        else
            r = mid - 1;
    }
    return ans;
}

signed main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        cin >> Le[i].l >> Le[i].r;
    }
    sort(Le + 1, Le + 1 + n, cmp);
    for (int i = 1; i <= n; ++i)
    {
        f[i] = max(f[i - 1], f[lower_bound(1, i - 1, Le[i].l)] + Le[i].r - Le[i].l + 1);
    }
    cout << f[n] << endl;
    return 0;
}

为什么这里的cmp函数里只能填小于号,而不能填小于等于号(第四个点为什么会RE?)

2025/7/25 20:45
加载中...