1 条题解

  • 1
    @ 2024-11-22 16:18:47
    #include<iostream>
    #include<vector>
    #include<algorithm>
    #define x first
    #define y second
    using namespace std;
    typedef long long LL;
    typedef pair<int, int> PII;
    const int N = 100010;
    PII p[N];
    LL n, len;
    vector<PII> v;
    bool check(int mid)
    {
        int l = 0, r = 0;
        for (int i = 0; i < n; ++ i)
        {
            if (p[i].y <= mid)
            {
                l = p[i].x - (mid - p[i].y);
                r = p[i].x + (mid - p[i].y);
                v.push_back({ l,r });
            }
        }
        sort(v.begin(), v.end());
        if (v.size())
        {
            int st = v[0].x, ed = v[0].y;
            for (int i = 0; i < v.size(); ++ i)
            {
                if (v[i].x <= ed + 1) ed = max(ed, v[i].y);
                else return false;
            }
            if (st <= 1 && ed >= len) return true;
            else return false;
        }
        else return false;
    }
    int main()
    {
        cin >> n >> len;
        for (int i = 0; i < n; ++ i)
        {
            int a, b;
            scanf("%d%d", &a, &b);
            p[i] = { a,b };
        }
        int l = 0, r = 2e9;
        while (l < r)
        {
            if (v.size()) v.clear();
            int mid = l + r >> 1;
            if (check(mid)) r = mid;
            else l = mid + 1;
        }
        cout << r << endl;
        return 0;
    }
    
    • 1

    信息

    ID
    5501
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    5
    已通过
    4
    上传者