AtCoder Regular Contest 126
D
Pure Straight
AtCoder
⌀ 2332
原题
← CE →
翻译中…

Problem Statement

翻译中…

Given is a sequence of $N$ positive integers $A = (A_1, A_2, \ldots, A_N)$, where each $A_i$ is $1$, $2$, $\ldots$, or $K$.

翻译中…

You can do the following operation on this sequence any number of times.

翻译中…
  • Swap two adjacent elements, that is, choose $i$ and $j$ such that $|i-j|=1$ and swap $A_i$ and $A_j$.
翻译中…

Find the minimum number of operations needed to make $A$ satisfy the following condition.

翻译中…
  • $A$ contains $(1, 2, \ldots, K)$ as a contiguous subsequence, that is, there is a positive integer $n$ at most $N-K+1$ such that $A_n = 1$, $A_{n+1} = 2$, $\ldots$, and $A_{n+K-1} = K$.
翻译中…

Constraints

翻译中…
  • $2\leq K\leq 16$
  • $K \leq N\leq 200$
  • $A_i$ is $1$, $2$, $\ldots$, or $K$.
  • $A$ contains at least one occurrence of each of $1, 2, \ldots, K$.
翻译中…

Input

翻译中…

Input is given from Standard Input in the following format:

翻译中…

N K
A_1 A_2 \ldots A_N

翻译中…

Output

翻译中…

Print the minimum number of operations needed to make $A$ satisfy the condition.

翻译中…

样例

Input #1
4 3
3 1 2 1
Output #1
2
One optimal sequence of operations is as follows. - Swap $A_1$ and $A_2$, changing $A$ to $(1,3,2,1)$. - Swap $A_2$ and $A_3$, changing $A$ to $(1,2,3,1)$. - Now we have $A_1 = 1$, $A_2 = 2$, $A_3 = 3$, satisfying the condition.
Input #2
5 5
4 1 5 2 3
Output #2
5
Input #3
8 4
4 2 3 2 4 2 1 4
Output #3
5
题目信息
题号
arc126_d
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。