No.1838 Modulo Straight
タグ : / 解いたユーザー数 33
作問者 :
Sumitacchan
/ テスター :
👑
hitonanode
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もしくは右上の雲マークをクリックしてアカウントを作成してください。