一道思维提,注重双指针的应用
AcWing 1238 日志统计
#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int, int>PII;
const int N = 1e5 + 10;
PII logs[N];
int n, k, d;
int cnt[N];
bool st[N];
int main()
{
cin>>n>>d>>k;
for(int i = 0; i < n; i ++ ){
cin>>logs[i].x>>logs[i].y;
}
sort(logs, logs + n);
//双指针,i表示区间末尾,j表示区间开头
for(int i = 0, j = 0; i < n; i ++ ){
int id = logs[i].y;
cnt[id] ++ ;
while(logs[i].x - logs[j].x >= d){ //区间长度超出d的话都要进行长度更改
cnt[logs[j].y] -- ;
j ++ ;//开头向后移一位
}
if(cnt[id] >= k) st[id] = true;
}
for(int i = 0; i <= 100000; i ++ ){
if(st[i]) cout<<i<<endl;
}
return 0;
}