388 传统题

[ABC352D] 连续值域下标跨度 (Permutation Subsequence)

时间限制
2000ms
内存限制
1024MiB
登顶 / 尝试
0/ 0
难度
4
    上传者 标签AtCoderABC352普及+滑动窗口单调队列

[ABC352D] 连续值域下标跨度 (Permutation Subsequence)

Score: 425425 points

Problem Statement

You are given a permutation P=(P1,P2,…,PN)P = (P_1, P_2, \dots, P_N) of (1,2,…,N)(1, 2, \dots, N).

A length-KK sequence of indices (i1,i2,…,iK)(i_1, i_2, \dots, i_K) is called a good index sequence if it satisfies both of the following conditions:

  • 1≤i1<i2<⋯<iK≤N1 \leq i_1 < i_2 < \dots < i_K \leq N.

  • The subsequence (Pi1,Pi2,…,PiK)(P_{i_1}, P_{i_2}, \dots, P_{i_K}) can be obtained by rearranging some consecutive KK integers. Formally, there exists an integer aa such that $\lbrace P_{i_1},P_{i_2},\dots,P_{i_K} \rbrace = \lbrace a,a+1,\dots,a+K-1 \rbrace$.

Find the minimum value of iK−i1i_K - i_1 among all good index sequences. It can be shown that at least one good index sequence exists under the constraints of this problem.

Constraints

  • 1≤K≤N≤2×1051 \leq K \leq N \leq 2 \times 10^5

  • 1≤Pi≤N1 \leq P_i \leq N

  • Pi≠PjP_i \neq P_j if i≠ji \neq j.

  • All input values are integers.

Input

The input is given from Standard Input in the following format:

$N$ $K$
$P_1$ $P_2$ $\dots$ $P_N$

Output

Print the minimum value of iK−i1i_K - i_1 among all good index sequences.

Sample Input 1

4 2
2 3 1 4

Sample Output 1

1

The good index sequences are (1,2),(1,3),(2,4)(1,2),(1,3),(2,4). For example, (i1,i2)=(1,3)(i_1, i_2) = (1,3) is a good index sequence because 1≤i1<i2≤N1 \leq i_1 < i_2 \leq N and (Pi1,Pi2)=(2,1)(P_{i_1}, P_{i_2}) = (2,1) is a rearrangement of two consecutive integers 1,21, 2.

Among these good index sequences, the smallest value of iK−i1i_K - i_1 is for (1,2)(1,2), which is 2−1=12-1=1.

Sample Input 2

4 1
2 3 1 4

Sample Output 2

0

iK−i1=i1−i1=0i_K - i_1 = i_1 - i_1 = 0 in all good index sequences.

Sample Input 3

10 5
10 1 6 8 7 2 5 9 3 4

Sample Output 3

5