Blue Bridge Cup log statistics Java double pointer sliding window

Title description

Xiao Ming maintains a programmers forum. Now he has collected a "like" log with N lines in total.

The format of each line is: ts id
means that the post with id numbered at time ts received a "like".

Now Xiao Ming wants to count which posts were once "hot posts".

If a post has received no less than K likes in any time period of length D, Xiao Ming thinks that the post was once a "hot post".

Specifically, if there is a certain moment T that
satisfies that the post receives no less than K likes during the period of [T,T+D) (note that the left-closed and right-open interval), the post was once "hot Post".

Given a log, please help Xiaoming count all the post numbers that were once "hot posts".

Input format

The first line contains three integers N, D, K.

The following N lines have one log per line, containing two integers ts and id.

Output format

The hot post id will be output in ascending order.

Each id occupies one line.

data range

1≤K≤N≤105,
0≤ts,id≤105,
1≤D≤10000

Input sample:

7 10 2
0 1
0 10
10 10
10 1
9 1
100 3
100 3

Sample output:

1
3

Similar topics

String arrangement

Longest substring without repetition

Find all letter dysphoric words in the string

Minimum covering substring

Ideas :

1.我们要判断一个帖子是不是热帖,就要知道它在某个
区间内是否点赞数大于等于K,而且这个区间还要有序才
能保证答答案的正确性,这就要把时间与对应的id,一
起排序,需要定义一个类,然后用ArrayList.sort去
排序;
class Ps implements Comparable<Ps>{
    
    
    public int t;
    public int id;

    public Ps(int t,int id){
    
    
        this.t=t;
        this.id=id;

    }
    public int compareTo(Ps o){
    
    
        return Integer.compare(t,o.t);
    }
}

2.排好序后我们就在双指针加滑动窗口在规定的区间
里找,不能两重循环暴力找会超时,只能过百分之五
十的数据;

3.用j,i分别表示窗口的左右边界,每次遍历的时候
hot[ps[i].id]++;
当ps[i].t-ps[j].t>=D的时候窗口就要缩小,还要
把左边界所指的那个时间的对应的店铺热度减掉,即
hot[ps[j].id]--;

4.经过上述操作后如果当前时间点对应的店铺的热度
大于等于K,则表示这个店铺是热帖;

Code

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner input=new Scanner(System.in);
        int N=input.nextInt();
        int D=input.nextInt();
        int K=input.nextInt();
        Ps[] logs = new Ps[N+1];
        int ans[]=new int[1000000];
        int hot[]=new int[1000000];
        for(int i=0;i<N;i++){
    
    
            int t=input.nextInt();
            int id=input.nextInt();
            logs[i] = new Ps(t,id);
        }
        Arrays.sort(logs,0,N);
        for(int i=0,j=0;i<N;i++){
    
    
            hot[logs[i].id]++;
            while(logs[i].t-logs[j].t>=D){
    
    
                hot[logs[j].id]--;
                j++;
            }
            if(hot[logs[i].id]>=K){
    
    
                ans[logs[i].id]=1;
            }
        }
        for(int i=0;i<1000000;i++){
    
    
            if(ans[i]==1)
                System.out.println(i);
        }

    }

}

class Ps implements Comparable<Ps>{
    
    
    public int t;
    public int id;

    public Ps(int t,int id){
    
    
        this.t=t;
        this.id=id;

    }
    public int compareTo(Ps o){
    
    
        return Integer.compare(t,o.t);
    }
}

Guess you like

Origin blog.csdn.net/qq_44844588/article/details/108164968