#题解没用前几个题讲的bisect函数,我写了一个用bisect函数的,我也写了count那个办法,但不知道为什么超时了 import bisect n, c = map(int, input().split()) A = list(map(int, input().split())) A.sort() pair = 0 for i, x in enumerate(A): idx1 = bisect.bisect_left(A, x+c) idx2 = bisect.bisect_right(A, x+c) if idx1 < n and A[idx1] == x+c and idx1 != idx2: pair += idx2 - idx1 print(pair) #下面这个时间复杂度超标,放弃 #for i, x in enumerate(A): # idx = A.count(x+C) # pair += idx #print(pair)