日志统计
题目描述
小明维护着一个程序员论坛。现在他收集了一份"点赞"日志,日志共有 NN 行。其中每一行的格式是:
ts idts id
表示在 tsts 时刻编号 idid 的帖子收到一个"赞"。
现在小明想统计有哪些帖子曾经是"热帖"。如果一个帖子曾在任意一个长度为 DD 的时间段内收到不少于 KK 个赞,小明就认为这个帖子曾是"热帖"。
具体来说,如果存在某个时刻 T 满足该帖在 [T,T+D)[T,T+D) 这段时间内(注意是左闭右开区间)收到不少于 KK 个赞,该帖就曾是"热帖"。
给定日志,请你帮助小明统计出所有曾是"热帖"的帖子编号。
输入描述
输入格式:
第一行包含三个整数 N,D,KN,D,K。
以下 N 行每行一条日志,包含两个整数 ts 和 id。
其中,1≤K≤N≤105,0≤ts≤105,0≤id≤1051≤K≤N≤105,0≤ts≤105,0≤id≤105。
输出描述
按从小到大的顺序输出热帖 idid。每个 idid 一行。
输入输出样例
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, D, K;
cin >> N >> D >> K;
vector<pair<int, int>> blog(N); // 存储时间和bo值
for (int i = 0; i < N; ++i) {
cin >> blog[i].first >> blog[i].second;
}
sort(blog.begin(), blog.end()); // 按时间排序
unordered_map<int, int> count; // 记录当前窗口内各bo的出现次数
set<int> result; // 存储符合条件的bo(自动排序)
int left = 0; // 滑动窗口的左指针
for (int right = 0; right < N; ++right) {
int current_time = blog[right].first;
int current_bo = blog[right].second;
count[current_bo]++; // 当前bo次数+1
// 维护窗口:确保当前时间与左端时间的差不超过D
while (current_time - blog[left].first >= D) {
int left_bo = blog[left].second;
count[left_bo]--; // 左指针右移前减少计数
if (count[left_bo] == 0) {
count.erase(left_bo); // 清理计数为0的项
}
left++;
}
// 检查当前bo是否满足条件
if (count[current_bo] >= K) {
result.insert(current_bo);
}
}
// 输出结果(按升序排列)
for (int bo : result) {
cout << bo << endl;
}
return 0;
}