蓝桥杯python 日志统计

问题遇到的现象和发生背景

新手蒟蒻超时了,该怎么节约时间呢

问题相关代码,请勿粘贴截图
import math
N,D,K=map(int,input().split())
list1=[]
hot=[]
for i in range(N):
    list1.append(list(map(int,input().split())))
# print(list1)
for i in range(len(list1)):
    count=0
    for j in range(i,len(list1)):
        if list1[i][1]==list1[j][1] and abs(list1[j][0]-list1[i][0] )<D:
            count+=1
        if count>=K and list1[i][1] not in hot:

            hot.append(list1[i][1])
            break
hot.sort()
flag=[]
for i in hot:
    # if i not in flag:
    #
    #     print(i)
    #     flag.append(i)
    print(i)

运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
import math

N, D, K = map(int, input().split())
list1 = []
hot = []
dic = {}
for i in range(N):
    list1.append(list(map(int, input().split())))
# print(list1)
for i in range(len(list1)):
    count = 0
    ts,id = list1[i][0],list1[i][1]
    if id not in dic:
        dic[id] = {}
    s = (ts // 10) * 10
    m = (s, s + D)
    if m not in dic[id]:
        dic[id][m] = 1
    else:
        dic[id][m] += 1
res = []
for i, j in dic.items():
    for s in j.values():
        if s >= K:
            res.append(i)
            break
for i in res:
    print(i)

hhh,你要看看我的吗

N,D,K = map(int,input().split())

dic = {}
output = []  # 输出的id不是有序的,将其放入到output排序后再输出,内存大,任性

for _ in range(N):
    t,i = map(int,input().split())
    if i not in dic:
        dic[i] = [t]
    else:
        dic[i].append(t)

for i in dic:
    l = dic[i]
    length = len(l)
    if length < K:
        continue
    l.sort()
    for j in range(length):
        if j+K-1 < length:
            if l[j+K-1] - l[j] < D:
                output.append(i)  
                break


output.sort()
for i in output:
    print(i)