P2058 海港题解

题目传送门

c++ 代码

#include <bits/stdc++.h>

using namespace std;

int n, t, k, x;
//国籍的桶
unordered_map<int, int> _map;
int res;

struct person {
    int nation; //国籍
    int t; //到岸时间
};
queue<person> q; //人员队列
person p;        //人员实例

int main() {
    cin >> n;
    //n艘船
    for (int i = 1; i <= n; i++) {
        //t:每艘船的到岸时间
        //k:每艘船的人数
        cin >> t >> k;

        //k个乘客
        for (int j = 1; j <= k; j++) {
            cin >> x;
            p.nation = x;   //国籍
            p.t = t;        //到岸时间
            q.push(p);
            //新增加的国籍,结果才加1
            if (_map[x] == 0) res++;
            //此国籍人数+1
            _map[x]++;
        }
        //维护一个整体,加加减减解决问题,而不是每次都全新计算
        while (!q.empty()) {
            p = q.front();
            //已经超过24小时
            if (p.t + 86400 <= t) {
                //相应国籍人数--
                _map[p.nation]--;
                //如果减了就没有了,需要结果-1
                if (_map[p.nation] == 0) res--;
                //弹出
                q.pop();
            } else break;//因为数据保证是ti是由小到大的,所以可以减枝
        }
        //输出每艘船结果
        cout << res << endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/littlehb/p/15075180.html