PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#FT0000210. Các đoạn con đẹp (Beautiful Subsequences)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Các đoạn con đẹp (Beautiful Subsequences)
Beautiful Subsequences
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a permutation of the integers and a nonnegative integer .
A contiguous subarray , , is called beautiful if
$$\max(P_L,\ldots,P_R)-\min(P_L,\ldots,P_R)\le R-L+K.$$Count the index pairs satisfying this condition. Different index pairs are counted separately.
Input
- The first line contains two integers , the permutation length and allowed slack.
- The second line contains exactly integers , forming a permutation of .
Output
Print one integer: the number of beautiful index pairs .
Subtasks
- Subtask 1 (20 points): ; ; is a permutation of .
- Subtask 2 (30 points): ; ; is a permutation of .
- Subtask 3 (50 points): ; ; is a permutation of .
Examples
Input
4 1
1 4 2 3
Output
9
Explanation
There are nonempty subarrays. The only invalid one is : its maximum is , its minimum is , so the difference is , exceeding . All other subarrays satisfy the inequality; for example, has difference , and has difference . Hence the answer is .