AcWing1238.日志统计(java版)

AcWing1238.日志统计(java版)_第1张图片

AcWing1238.日志统计(java版)_第2张图片

import java.io.*;
import java.util.*;

public class Main
{
    static int n,d,k;
    static final int N = 100010;
    static int[] cnt = new int[N];  //判断每个在d区间内每个id出现多少次
    static boolean[] st = new boolean[N];   //判断该id是否满足题意
    
    static PII[] p = new PII[N];    //自定义类pii,按时间排序
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    
    public static void main(String[] args) throws IOException
    {
        String init[] = in.readLine().split(" ");
        n = Integer.parseInt(init[0]);
        d = Integer.parseInt(init[1]);
        k = Integer.parseInt(init[2]);
        for(int i = 0;i < n;i ++)
        {
            String[] data = in.readLine().split(" ");
            p[i] = new PII(Integer.parseInt(data[0]),Integer.parseInt(data[1]));
        }
        Arrays.sort(p,0,n);
        
        for(int i = 0,j = 0; i < n;i ++)
        {
            int id = p[i].id;
            cnt[id]++;
            //时间跨度不一定每次跨1
            while(p[i].ts - p[j].ts >= d)
            {
                cnt[p[j].id] --;
                j ++;
            }
            if(cnt[id] >= k) st[id] = true;
        }
        //这里是最大id为N,不能写i < n
        //n只是有n个数据
        for(int i = 0;i < N;i++)
        {
            if(st[i])
            System.out.println(i);
        }
        in.close();
    }
}
class PII implements Comparable
    {
        int ts;
        int id;
        
        public PII(int ts,int id)
        {
            this.ts = ts;
            this.id = id;
        }
        public int compareTo(PII p)
        {
            return Integer.compare(ts,p.ts);
        }
        
    }

你可能感兴趣的:(算法,java,算法,数据结构,蓝桥杯)