Permutation Subsequence

AtCoder
IDabc352_d
Time2000ms
Memory256MB
Difficulty
You are given a permutation $P = (P_1, P_2, \dots, P_N)$ of $(1, 2, \dots, N)$. A length-$K$ 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 exists 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$ [samples]
Samples
Input #1
4 2
2 3 1 4
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$.
Input #2
4 1
2 3 1 4
Output #2
0

$i_K - i_1 = i_1 - i_1 = 0$ in all good index sequences.
Input #3
10 5
10 1 6 8 7 2 5 9 3 4
Output #3
5
API Response (JSON)
{
  "problem": {
    "name": "Permutation Subsequence",
    "description": {
      "content": "You are given a permutation $P = (P_1, P_2, \\dots, P_N)$ of $(1, 2, \\dots, N)$. A length-$K$ sequence of indices $(i_1, i_2, \\dots, i_K)$ is called a **good index sequence** if it satisfies both of th",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc352_d"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a permutation $P = (P_1, P_2, \\dots, P_N)$ of $(1, 2, \\dots, N)$.\nA length-$K$ sequence of indices $(i_1, i_2, \\dots, i_K)$ is called a **good index sequence** if it satisfies both of th...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments