1 solutions

  • 0
    @ 2025-5-16 14:44:30

    仔细观察本题 titjwi+wj,ij|t_i-t_j|\ge w_i+w_j,i\ne j

    这个条件 我们可以把 事件 i 看成一个区间 [tiwi,ti+wi][t_i - w_i, t_i + w_i] , 两个事件有联系,本质是两个事件对应区间不相交。

    故本题是求给出 n 条线段,求最多可以取出多少不相交的线段条数,经典贪心问题。

    #include <bits/stdc++.h>
    using namespace std;
    
    const int NR = 1e6;
    
    int n;
    
    struct Node {
        long long t, w;
    } p[NR + 5];
    
    bool cmp(Node x, Node y) { return x.t + x.w < y.t + y.w; }
    
    int main() {
        ios::sync_with_stdio(0);
        cin >> n;
        for (int i = 1; i <= n; i++) cin >> p[i].t;
        for (int i = 1; i <= n; i++) cin >> p[i].w;
        sort(p + 1, p + n + 1, cmp);
        int ans = 1;
        long long now = p[1].t + p[1].w;
        for (int i = 2; i <= n; i++)
            if (p[i].t - p[i].w >= now) {
                ans++, now = p[i].t + p[i].w;
            }
        cout << ans << '\n';
    
        return 0;
    }
    
    • 1

    Information

    ID
    663
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    9
    Tags
    (None)
    # Submissions
    55
    Accepted
    4
    Uploaded By