有没有大佬解释一下,这个题开二维数组会不会爆内存?我第一次的思路是开二维数组,害怕爆内存(因为原来有题跟他很相似,所以这次很小心),然后我就用集合来做了,发现内存也没那么高,不知道是不是我多想了~~~~
import java.io.*;
import java.util.*;
public class Main {
static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws Exception{
String[] sp=br.readLine().split(" ");
int n=Integer.parseInt(sp[0]);
int d=Integer.parseInt(sp[1]);
int k=Integer.parseInt(sp[2]);
int[] arr=new int[100001];
List<Integer>[] lists=new List[100001];
Set<Integer> index=new HashSet<>();
List<Integer> ind=new ArrayList<>();
for(int i=0;i<lists.length;i++) lists[i]=new ArrayList<>();
while(n-->0) {
sp=br.readLine().split(" ");
int ts=Integer.parseInt(sp[0]);
int id=Integer.parseInt(sp[1]);
lists[ts].add(id);
}
//初始化
for(int i=0;i<d;i++) {
for(int id:lists[i]) {
arr[id]++;
if (arr[id]>=k) {
index.add(id);
}
}
}
for(int i=2,j=i+d-1;j<lists.length;i++,j++) {
for(int id:lists[i-1]) {
arr[id]--;
}
for(int id:lists[j]) {
arr[id]++;
if (arr[id]>=k) {
index.add(id);
}
}
}
for(int i:index) ind.add(i);
ind.sort((a,b)->(a-b));
for(int i:ind) {
bw.write(i+"\n");
}
bw.flush();
br.close();
}
}