AcWing 1238. 日志统计(小根堆做法)有没有大佬帮我看一下为什么总是多算啊
原题链接
中等
作者:
Theshot
,
2025-04-09 20:11:41
· 山西
,
所有人可见
,
阅读 10
C++ 代码
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 1e5 + 10;
int n,d,k;
int cnt[N];
int res[N];
bool st[N];
priority_queue<int,vector<int>,greater<int>>heap[N];
int main()
{
cin>>n>>d>>k;
int t = 0;
for(int i = 0;i < n;i ++)
{
int ts,id;
cin>>ts>>id;
if(st[id]) continue;
while(heap[id].size() && ts >= d && heap[id].top() <= ts - d )
{
cnt[id] --;
heap[id].pop();
}
heap[id].push(ts);
cnt[id] ++;
if(cnt[id] >= k)
{
st[id] = true;
res[t ++] = id;
}
}
sort(res,res + t);
for(int i = 0;i < t;i ++)
cout<<res[i]<<endl;
}