cswork / OA打开练习工作台

AmazonOA #315

Number of Well Performing Groups

估计中等可在线判题

考点(AI 标注)双指针滑窗单调栈与堆

Every non-empty contiguous subarray is a group, and a group performs well if its maximum minus its minimum is exactly k. Groups with different start or end positions are different, even if their values are the same. Find the total number of well-performing groups.

输入格式

The first line contains n k and the second line contains the array. On this site 1≤n≤200000, −10^9≤a[i]≤10^9, −2000000000≤k≤2000000000. A negative k naturally has no valid groups and is not excluded.

输出格式

Output a single integer, the answer.

样例 1

输入:

3 2
2 4 6

输出:

2

样例 2

输入:

3 0
2 2 2

输出:

6

样例 3

输入:

1 -1
1

输出:

0

样例说明

The answers are 2, 6 and 0. In the second example all 6 non-empty subarrays count, so you cannot add only 1 per right end.