AtCoder Regular Contest 204 (Div. 1)
B
Sort Permutation
AtCoder
⌀ 2210
原题
← AC →
翻译中…

Problem Statement

翻译中…

You are given positive integers $N, K$ and a permutation $P = (P_{1}, P_{2}, \dots, P_{NK})$ of $(1, 2, \dots, NK)$.

翻译中…

Alice repeatedly performs the following operation to sort $P$ in ascending order.

翻译中…
  • Choose integers $i$ and $j$ $(i\neq j)$ where $1 \leq i, j \leq NK$, and swap $P_{i}$ and $P_{j}$. If $|i - j|$ is a multiple of $N$, Alice gets $1$ point.
翻译中…

Alice sorts $P$ in ascending order with the minimum number of operations, and under that condition, she maximizes the sum of points she gets.

翻译中…

Output the sum of points Alice gets.

翻译中…

Constraints

翻译中…
  • $1\leq N\leq 500$
  • $1\leq K\leq 10$
  • $(P_{1}, P_{2}, \dots , P_{NK})$ is a permutation of $(1, 2, \dots, NK)$
  • 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_{NK}

翻译中…

Output

翻译中…

Output the answer.

翻译中…

样例

Input #1
3 2
1 6 5 3 2 4
Output #1
2
For example, $P$ can be sorted in ascending order with four operations as follows. - Perform the operation with $(i, j) = (2, 5)$. After the operation, $P = (1, 2, 5, 3, 6, 4)$. - Perform the operation with $(i, j) = (4, 6)$. After the operation, $P = (1, 2, 5, 4, 6, 3)$. - Perform the operation with $(i, j) = (3, 5)$. After the operation, $P = (1, 2, 6, 4, 5, 3)$. - Perform the operation with $(i, j) = (3, 6)$. After the operation, $P = (1, 2, 3, 4, 5, 6)$. Among the above four operations, Alice gets $1$ point from each of the $1$st and $4$th operations, so Alice gets a total of $2$ points. This sequence of operations is one of the ways to sort $P$ in ascending order with the minimum number of operations and maximize the sum of points under that condition, so output $2$.
Input #2
1 1
1
Output #2
0
Input #3
4 6
10 24 3 4 8 14 5 2 22 9 21 1 15 6 13 23 18 12 7 17 19 16 20 11
Output #3
7
题目信息
题号
arc204_b
时间限制
2 s
内存限制
1024 MB

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