他想请你帮他计算数组
例如数组
第一行输入两个正整数和
.
第二行输入n个正整数.
输出一个整数表示答案.
5 2 1 2 1 2 3
5
满足条件的区间为[1,3],[1,4],[1,5],[2,4],[2,5].
from collections import defaultdict n, m = list(map(int, input().split())) arr = list(map(int, input().split())) mem = defaultdict(int) ans = 0 i,j = 0,0 max_num = 0 while j < n: mem[arr[j]] += 1 max_num = max(max_num, mem[arr[j]]) if max_num == m: while max_num == m: ans += n-j if mem[arr[i]] == max_num: max_num -= 1 mem[arr[i]] -= 1 i += 1 j+=1 print(ans)