日志统计(双指针)

题目描述

小明维护着一个程序员论坛。现在他收集了一份"点赞"日志,日志共有 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;
    }
}
相关推荐
寻星探路8 小时前
【深度长文】万字攻克网络原理:从 HTTP 报文解构到 HTTPS 终极加密逻辑
java·开发语言·网络·python·http·ai·https
你撅嘴真丑10 小时前
第九章-数字三角形
算法
曹牧10 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
uesowys10 小时前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
ValhallaCoder11 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
董董灿是个攻城狮11 小时前
AI 视觉连载1:像素
算法
爬山算法11 小时前
Hibernate(90)如何在故障注入测试中使用Hibernate?
java·后端·hibernate
智驱力人工智能11 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
kfyty72511 小时前
集成 spring-ai 2.x 实践中遇到的一些问题及解决方案
java·人工智能·spring-ai
猫头虎11 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven