1 solutions

  • 2
    @ 2025-9-30 20:14:51

    精灵联谊

    考虑将精灵的可用时间看成一个线段,问题实际上是计算某个点上最多的线段覆盖。对于一个时间段在[a,b][a, b]中的精灵,可以看作在点aa处增加了一个覆盖,在点b+1b+1及之后后减少了一个线段覆盖。排序之后,即可解决每个点上的线段覆盖数量(也就是当天能参加的最多精灵数量)的问题。

    接下来考虑怎么具体求解某一天,不难发现水之精灵和火之精灵在同一天上的值应该取较小值。只需要在所有日期内得到当天能够参加的精灵数量,并取最大值即可。

    复杂度瓶颈在于排序,为O(nlog(n))O(nlog(n))。

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 2e5 + 10;
    struct node {
        int type, pos, val;
        bool operator < (const node tmp) const {
            return pos < tmp.pos;
        }
    };
    
    node t[maxn]; 
    int n, ans[2], num[2];
    
    int main()
    {
        cin >> n;
        for(int i = 1;i <= n; ++i) {
            int a, b, c;
            cin >> a >> b >> c;
            t[i] = {a, b, 1};
            t[n + i] = {a, c + 1, -1};
        }
        sort(t + 1, t + 2 * n + 1);
    
        for(int i = 1;i <= 2 * n; ++i) {
            num[t[i].type] += t[i].val;
            if(t[i].pos != t[i + 1].pos) {
                int tmp = min(num[0], num[1]);
                if(2 * tmp > ans[1]) {
                    ans[0] = t[i].pos;
                    ans[1] = 2 * tmp;
                }
            }
        }
        cout << ans[0] << ' ' << ans[1];
        return 0;
    }
    
    • 1

    Information

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