日志统计(双指针)

题目描述

小明维护着一个程序员论坛。现在他收集了一份"点赞"日志,日志共有 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 一行。

输入输出样例

示例

输入

复制代码
7 10 2
0 1
0 10
10 10
10 1
9 1
100 3
100 3

输出

复制代码
1
3
java 复制代码
import java.util.*;

public class LogStatistics {
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        int d=scanner.nextInt();
        int k=scanner.nextInt();
        List<Integer> ans=new ArrayList<>();
        Map<Integer,List<Integer>> m=new HashMap<>();
        for(int i=0;i<n;i++){
            int ts=scanner.nextInt();
            int id=scanner.nextInt();
            m.computeIfAbsent(id,K->new ArrayList<>()).add(ts);
        }
        for(Map.Entry<Integer,List<Integer>> entry : m.entrySet()){
            List<Integer> times=entry.getValue();
            Collections.sort(times);
            if(fun(times,d,k)){
                ans.add(entry.getKey());
            }
        }
        Collections.sort(ans);
        for(int id:ans){
            System.out.println(id);
        }
    }
    public static boolean fun(List<Integer> times,int d,int k){
        int left=0;
        for(int right=0;right<times.size();right++){
            while(times.get(right)-times.get(left)>=d){
                left++;
            }
            if(right-left+1>=k){
                return true;
            }
        }
        return false;
    }
}
相关推荐
show4331 天前
2026小程序端AI智能优化技术实践:转文字后自动纠错+润色+分段算法分析
人工智能·算法·小程序
互联网中的一颗神经元1 天前
01. Go 内存管理全景架构
java·jvm·golang
萧瑟余晖1 天前
Java深入解析篇三十四之分布式事务
java·开发语言·分布式
CAE虚拟与现实1 天前
源码注解 /class 注解 / 运行时注解三者生命周期
java·开发语言
互联网中的一颗神经元1 天前
02. 核心概念与术语表
java·jvm·spring
January12071 天前
Cassandra CQL 基本命令详解
java
平生不晚1 天前
在 SVG 体系里画一条任意的线
前端·算法
Escalating_xu1 天前
【Linux】线程概念与控制:从地址空间、LWP 到 pthread_create、join、detach 与 NPTL
java·linux·redis
Lam Tang1 天前
APS 系列文章 09
java·代理模式
evans在进步1 天前
LeetCode 198:打家劫舍——Java 动态规划详解
java·leetcode·动态规划