题解 | 谐距下标对
谐距下标对
https://www.nowcoder.com/practice/12fd032361704c978bcb9c2c2b3bb93d
import sys
from collections import defaultdict
n = int(input().strip())
a = list(map(int,input().strip().split()))
b = [a[i]-(i+1) for i in range(len(a))]
count = defaultdict(int)
for val in b:
count[val] += 1 # 初值0
res = 0
for cnt in count.values():
if cnt > 2:
res += cnt * (cnt-1) // 2 # 组合数
elif cnt == 2:
res += 1
print(res)
C(cnt,2) 组合数
查看9道真题和解析