Sequence Count
Problem Description
English
A sequence is defined as: the absolute value of the difference between two adjacent elements after ordering is exactly equal to 1.
Now given an array of length n, how many subintervals of length k satisfy the requirement that the elements of a subinterval perfectly form a sequence?
For example, for the array [3, 7, 6, 4, 5], the subarray [4, 5] is a sequence.
Note:
A subinterval can be thought of as selecting some elements from the head or tail of the original array to delete (or not delete) and leaving the relative positions of the remaining elements unchanged.
Description of the parameters:
- $1 < k < n < 300000$
- $a_1, a_2,…, a_n (1 \le a_i \le 10^6)$
Chinese
顺子的定义为:排序后相邻两元素的差的绝对值恰好等于 1。
现在提供一个长度为 n 的数组,有多少长度为 k 的子区间满足:子区间中元素怡好构成一个顺子?
例如,对于数组 [3, 7, 6, 4, 5],子数组 [4, 5] 是一个顺子。
备注:
子区间可以理解为从原数组中从头部或尾部选择一些元素删掉 (或者不删)并保持剩余元素的相对位置不变。
输入描述:
- $1 < k < n < 300000$
- $a_1, a_2,…, a_n (1 \le a_i \le 10^6)$
Solution
1 |
|