103523: [Atcoder]ABC352 D - Permutation Subsequence
Description
Score: $425$ points
Problem Statement
You are given a permutation $P = (P_1, P_2, \dots, P_N)$ of $(1, 2, \dots, N)$.
A sequence of indices $(i_1, i_2, \dots, i_K)$ is called a good index sequence if it satisfies both of the following conditions:
- $1 \leq i_1 < i_2 < \dots < i_K \leq N$.
- The subsequence $(P_{i_1}, P_{i_2}, \dots, P_{i_K})$ can be obtained by rearranging some consecutive $K$ integers.
Formally, there exists an integer $a$ 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 $i_K - i_1$ among all good index sequences. It can be shown that at least one good index sequence exist under the constraints of this problem.
Constraints
- $1 \leq K \leq N \leq 2 \times 10^5$
- $1 \leq P_i \leq N$
- $P_i \neq P_j$ if $i \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 $i_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)$. For example, $(i_1, i_2) = (1,3)$ is a good index sequence because $1 \leq i_1 < i_2 \leq N$ and $(P_{i_1}, P_{i_2}) = (2,1)$ is a rearrangement of two consecutive integers $1, 2$.
Among these good index sequences, the smallest value of $i_K - i_1$ is for $(1,2)$, which is $2-1=1$.
Sample Input 2
4 1 2 3 1 4
Sample Output 2
0
$i_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
Input
Output
问题描述
给你一个排列 $P = (P_1, P_2, \dots, P_N)$,它是 $(1, 2, \dots, N)$ 的全排列。
一个下标序列 $(i_1, i_2, \dots, i_K)$ 称为一个 好下标序列,如果满足以下两个条件:
- $1 \leq i_1 < i_2 < \dots < i_K \leq N$。
- 子序列 $(P_{i_1}, P_{i_2}, \dots, P_{i_K})$ 可以通过重新排列连续的 $K$ 个整数得到。
形式上,存在一个整数 $a$ 使得 $\lbrace P_{i_1},P_{i_2},\dots,P_{i_K} \rbrace = \lbrace a,a+1,\dots,a+K-1 \rbrace$。
找出所有好下标序列中 $i_K - i_1$ 的最小值。在本题的约束下,可以证明至少存在一个好下标序列。
约束
- $1 \leq K \leq N \leq 2 \times 10^5$
- $1 \leq P_i \leq N$
- 如果 $i \neq j$,则 $P_i \neq P_j$。
- 所有输入值都是整数。
输入
输入通过标准输入给出以下格式:
$N$ $K$ $P_1$ $P_2$ $\dots$ $P_N$
输出
打印所有好下标序列中 $i_K - i_1$ 的最小值。
样例输入 1
4 2 2 3 1 4
样例输出 1
1
好的下标序列有 $(1,2)$, $(1,3)$, 和 $(2,4)$。例如,$(i_1, i_2) = (1,3)$ 是一个好下标序列,因为 $1 \leq i_1 < i_2 \leq N$ 且 $(P_{i_1}, P_{i_2}) = (2,1)$ 是连续整数 $1, 2$ 的重新排列。
在这些好的下标序列中,$i_K - i_1$ 的最小值是对于 $(1,2)$ 的,即 $2-1=1$。
样例输入 2
4 1 2 3 1 4
样例输出 2
0
所有好的下标序列中 $i_K - i_1 = i_1 - i_1 = 0$。
样例输入 3
10 5 10 1 6 8 7 2 5 9 3 4
样例输出 3
5