#abc352d. D - Permutation Subsequence

D - Permutation Subsequence

Score: 425425 points

问题陈述

给定一个排列 P=(P1,P2,,PN)P = (P_1, P_2, \dots, P_N),它是 (1,2,,N)(1, 2, \dots, N) 的一个排列。

如果一个长度为 KK 的索引序列 (i1,i2,,iK)(i_1, i_2, \dots, i_K) 满足以下两个条件,则称其为一个好索引序列

  • 1i1<i2<<iKN1 \leq i_1 < i_2 < \dots < i_K \leq N
  • 子序列 (Pi1,Pi2,,PiK)(P_{i_1}, P_{i_2}, \dots, P_{i_K}) 可以通过重新排列某些连续的 KK 个整数获得。 形式上,存在一个整数 aa 使得 $\lbrace P_{i_1},P_{i_2},\dots,P_{i_K} \rbrace = \lbrace a,a+1,\dots,a+K-1 \rbrace$。

在所有好索引序列中,找到 iKi1i_K - i_1 的最小值。可以证明,在这个问题的约束条件下,至少存在一个好索引序列。

以上为大语言模型 kimi 翻译,仅供参考。

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:

  • 1i1<i2<<iKN1 \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 iKi1i_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

  • 1KN2×1051 \leq K \leq N \leq 2 \times 10^5
  • 1PiN1 \leq P_i \leq N
  • PiPjP_i \neq P_j if iji \neq j.
  • All input values are integers.

Input

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

NN KK

P1P_1 P2P_2 \dots PNP_N

Output

Print the minimum value of iKi1i_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 1i1<i2N1 \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 iKi1i_K - i_1 is for (1,2)(1,2), which is 21=12-1=1.

Sample Input 2

4 1
2 3 1 4

Sample Output 2

0

iKi1=i1i1=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
}