日志统计-双指针

小明维护着一个程序员论坛。现在他收集了一份”点赞”日志,日志共有 N 行。

其中每一行的格式是:

ts id
表示在 ts 时刻编号 id 的帖子收到一个”赞”。

现在小明想统计有哪些帖子曾经是”热帖”。

如果一个帖子曾在任意一个长度为 D 的时间段内收到不少于 K 个赞,小明就认为这个帖子曾是”热帖”。

具体来说,如果存在某个时刻 T 满足该帖在 [T,T+D) 这段时间内(注意是左闭右开区间)收到不少于 K 个赞,该帖就曾是”热帖”。

给定日志,请你帮助小明统计出所有曾是”热帖”的帖子编号。

输入格式
第一行包含三个整数 N,D,K。

以下 N 行每行一条日志,包含两个整数 ts 和 id。

输出格式
按从小到大的顺序输出热帖 id。

每个 id 占一行。

数据范围
1≤K≤N≤105,
0≤ts,id≤105,
1≤D≤10000
输入样例:
7 10 2
0 1
0 10
10 10
10 1
9 1
100 3
100 3
输出样例:
1
3

做法:指针i为后指针,j为前,维护区间完成模拟。

#include <iostream>
#include <cstdio>
#include <algorithm> 
using namespace std;
#define x first
#define y second
typedef pair <int, int> PII;

const int maxn = 100010;

int K,N,D;
int a[maxn];
PII logs[maxn];
bool st[maxn];
int cnt[maxn];


int main(){
	scanf("%d%d%d", &N, &D, &K);
	for(int i = 0; i < N; i++){
		scanf("%d%d", &logs[i].x, &logs[i].y);
	}
	sort(logs, logs + N);
	/*
	for(int i = 0; i < N; i++){
		cout << logs[i].x << " " << logs[i].y << endl;
	}*/
	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 < maxn; i++){
		if(st[i]){
			printf("%d\n", i);
		}
	}
	return 0;
} 
/*
7 10 2
0 1
0 10
10 10
10 1
9 1
100 3
100 3
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值