AcWing 1238. 日志统计
原题链接
中等
作者:
Bug-Free
,
2020-10-10 16:49:21
,
所有人可见
,
阅读 994
暴力
//暴力
#include<iostream>
#include <algorithm>
#include<vector>
#define x first
#define y second
using namespace std;
const int N = 1e5 + 10;
typedef pair<int, int> pii;
int n, d, k;
bool st[N];
pii logs[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> d >> k;
for (int i = 0; i < n; i++) {
cin >> logs[i].x >> logs[i].y;
}
sort(logs, logs + n);
for (int i = 0; i < n; i++) {
int cnt = 1;
int t = logs[i].x;
for (int j = i + 1; j < n; j++) {
if (logs[j].x - t < d && logs[j].y == logs[i].y) {
cnt++;
}
}
if (cnt >= k) {
st[logs[i].y] = true;
}
}
for (int i = 0; i <= 1e5 + 10; i++) {
if (st[i]) {
cout << i << endl;
}
}
return 0;
}
双指针
//
// Created by Genes on 2020/10/10.
//
// 双指针
#include<iostream>
#include<algorithm>
#define x first
#define y second
using namespace std;
const int N = 1e5 + 10;
int n, d, k;
typedef pair<int, int> pii;
pii logs[N];
bool st[N];
int cnt[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> d >> k;
for (int i = 0; i < n; i++) {
cin >> logs[i].x >> logs[i].y;
}
sort(logs, logs + n);
for (int i = 0, j = 0; i < n; i++) {
int id = logs[i].y;
cnt[id]++;
while (logs[i].x - logs[j].x >= d) {
cnt[logs[j].y]--;
j++;
}
if (cnt[id] >= k) {
st[id] = true;
}
}
for (int i = 0; i <= 1e5; i++) {
if (st[i]) {
cout << i << endl;
}
}
return 0;
}
大佬,能解释一下cnt[logs[j].y]–的意思吗
超过长度d的限制了,就把范围外的cnt剪掉,并且使j++,让整体的长度再次符合d
谢谢大佬