zhangwch • 1个月前
桶排序计数,队列维护顺序。
每名乘客最多进队一次、出队一次,所以时间复杂度就是3 x 1e5 x 2。
#include <iostream>
#include <queue>
using namespace std;
// 111026海港
const int N = 100010;
// x[]存储该国籍人数,x[0]存储现在有多少个国家
int n, t[N], k[N], x[N];
// 队列里存放现在属于合法区间内的国家
queue<int> q;
// 桶排序计数,队列维护先进先出的顺序
int main() {
cin >> n;
int last = 1;
for (int i = 1; i <= n; i++) {
cin >> t[i] >> k[i];
for (int j = 0; j < k[i]; j++) {
int country;
cin >> country;
if (x[country] == 0) x[0]++;
x[country]++;
q.push(country);
}
// 当上一个船超时的时候,就把船上乘客弹出
while (t[i] - t[last] >= 86400) {
for (int j = 0; j < k[last]; j++) {
int country = q.front();
q.pop();
x[country]--;
if (x[country] == 0) x[0]--;
}
last++;
}
cout << x[0] << endl;
}
return 0;
}
评论: