top1编程
← 返回题目
题解

[NOIP 2016 普及组] 海港

1 条题解

  • 0
    @ 2026-10-21 0:50:45

    1. 解题思路

    按到港时间依次处理乘客,用队列移除距当前时间至少86400秒的乘客,国籍计数从0变1或1变0时更新不同国家数。

    2. 坑点和注意事项

    船≤10^5、乘客总数≤3×10^5、国籍≤10^5。窗口左端严格不包含t-86400,所有同国籍乘客都离开才减少种类。

    3. 复杂度

    • 时间复杂度:O(n+Σk)。
    • 空间复杂度:O(Σk+10^5)。
    /*
    思路:按到港时间依次处理乘客,用队列移除距当前时间至少86400秒的乘客,国籍计数从0变1或1变0时更新不同国家数。
    关键规则与坑点:船≤10^5、乘客总数≤3×10^5、国籍≤10^5。窗口左端严格不包含t-86400,所有同国籍乘客都离开才减少种类。
    时间复杂度:O(n+Σk)。空间复杂度:O(Σk+10^5)。
    */
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n;
        cin >> n;
        queue<pair<int, int>> q;
        static int cnt[100001];
        int kinds = 0;
        while (n--)
        {
            int t, k;
            cin >> t >> k;
            while (!q.empty() && q.front().first <= t - 86400)
            {
                if (--cnt[q.front().second] == 0)
                    kinds--;
                q.pop();
            }
            while (k--)
            {
                int x;
                cin >> x;
                if (cnt[x]++ == 0)
                    kinds++;
                q.push({t, x});
            }
            cout << kinds << "\n";
        }
    }
    
    • 1