201263: [AtCoder]ARC126 D - Pure Straight

Memory Limit:1024 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $600$ points

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.


Sample Input 1

4 3
3 1 2 1

Sample 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.

Sample Input 2

5 5
4 1 5 2 3

Sample Output 2

5

Sample Input 3

8 4
4 2 3 2 4 2 1 4

Sample Output 3

5

Input

题意翻译

给你一个有 $N$ 个正整数的序列 $A = (A_1,A_2,\dots,A_N)$,且 $A_i$ 是 $1,2,\dots$ 或 $K$。 你可以对这个序列做如下操作若干次。 - 交换两个相邻的元素,也就是选出 $i$ 和 $j$ 满足 $|i - j| = 1$ 并交换 $A_i$ 和 $A_j$。 找到最小的操作数使 $A$ 满足如下条件。 - $A$ 包含 $(1,2,\dots,K)$ 作为一个相接的子序列,也就是对于任意正整数 $n$ 最大是 $N - K + 1$ 满足 $A_n = 1, A_{n + 1} = 2,\dots,A_{N - K + 1} = K$。

加入题单

上一题 下一题 算法标签: