题目:
解析:
N代表有几个数据;D代表时间段;K代表不少于几个赞;ts即在ts时刻;id代表某一行的代码
即:在D时间段内某id代码行获得不少于K个赞就为热帖,即计数加一;
代码:
public class Main {
static class R {
int ts, id;
}
static void main (String[] args) {
Scanner sc = new Scanenr(System.in);
int N = sc.nextInt(), D = sc.nextInt(), K = sc.nextInt();
R[] rs = new R[N];
for (int i = 0; i < n; i++) {
R r = new R();
r.ts = sc.nextInt();
r.td = sc.nextInt();
rs[i] = r;
}
//匿名内部类定义排序器
Arrays.sort(rs, new Comparator<R>() {
public int compare(R r1, R r2) {
return r1.ts - r2.ts;
}
});
//用于给id计数
Map<Integer, Integer> cnt = new HashMap<Intger, Integer>();
//用于储存id
SortedSet<Integer> answers = new TreeSet<Integer>();
//尺取法
int j = 0;
for (int i = 0; i < N; ++i) {
while (j < N && rs[j].ts - rs[i].ts < D) {
int td = rs[j].td;
Integer exist = cnt.get(td);
if(exist != null) {
cnt.put(td, exist + 1);
} else {
cnr.put(td, 1);
}
if (cnt.get(td) >= K)
answers.add(td);
j++;
}
Integer cntOfI = cnt.get(rs[i].td);
if (cntOfI != null) {
cnt.put(rs[i].td, cntOfI - 1);
}
for(Integer i : answers) {
System.out.println(i);
}
}
}