网站制作找360优化大师最新版
题目描述
小明维护着一个程序员论坛。现在他收集了一份"点赞"日志,日志共有 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
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;}
}