問題一覧 > 通常問題

No.1838 Modulo Straight

レベル : / AI推定難易度 : 1900 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 33
作問者 : Sumitacchan / テスター : 👑 hitonanode ygussany
お気に入りにしたユーザー ProblemId : 7183 / 自分の提出
問題文最終更新日: 2022-01-15 16:09:29
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 331 (順位表) の他の問題:

Problem Statement

You are given a sequence $A=(A_1,A_2,\dots,A_{MK})$ of length $M\times K$ that contains each non-negative integer less than $M$ exactly $K$ times.

You may perform the following operation on the sequence $A$ any number of times (possibly $0$ times).

  • Swap $2$ adjacent terms. That is, choose an integer $i$ such that $1\le i\le MK-1$, and swap the values of $A_i$ and $A_{i+1}$.


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

  • For every integer $i\ (1\le i\le MK-1)$, $A_{i+1} \equiv A_i+1\ (\bmod M)$ holds.

Input

$M\ \ K$
$A_1\ \ A_2\ \ \cdots\ \ A_{MK}$

  • $M\ge 2$
  • $2 \le M\times K \le 4\times 10^5$
  • The sequence $A=(A_1,A_2,\dots,A_{MK})$ contains each non-negative integer less than $M$ exactly $K$ times.
  • All input values are integers.

Output

Output the minimum number of operations required to make the sequence $A$ satisfy the condition.

Sample

Sample 1
Input
3 2
1 2 0 2 1 0
Output
1

With $1$ operation, you can make $A=(1,2,0,1,2,0)$.

Sample 2
Input
2 3
0 0 0 1 1 1
Output
3

With $3$ operations, you can make $A=(0,1,0,1,0,1)$.

Sample 3
Input
7 1
3 4 5 6 0 1 2
Output
0

The condition is already satisfied from the start.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。