CF1294E.Obtain a Permutation
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a rectangular matrix of size n×m consisting of integers from 1 to 2⋅105.
In one move, you can:
- choose any element of the matrix and change its value to any integer between 1 and n⋅m, inclusive;
- take any column and shift it one cell up cyclically (see the example of such cyclic shift below).
A cyclic shift is an operation such that you choose some j (1≤j≤m) and set a1,j:=a2,j,a2,j:=a3,j,…,an,j:=a1,j simultaneously.
Example of cyclic shift of the first column
You want to perform the minimum number of moves to make this matrix look like this:

In other words, the goal is to obtain the matrix, where a1,1=1,a1,2=2,…,a1,m=m,a2,1=m+1,a2,2=m+2,…,an,m=n⋅m (i.e. ai,j=(i−1)⋅m+j) with the minimum number of moves performed.
你被给定一个大小为 n×m 的矩形矩阵,其中元素为 1 到 2⋅105 之间的整数。
在一次操作中,你可以执行以下任一动作:
- 选择矩阵中的任意一个元素,并将其值更改为 1 到 n⋅m(含)之间的任意整数;
- 选择任意一列,并将其循环上移一格(如下图所示为该循环移位操作的示例)。
循环移位是指:选定某个 j(1≤j≤m),同时执行如下赋值:a1,j:=a2,j,a2,j:=a3,j,…,an,j:=a1,j。
第一列循环上移的示例
你的目标是通过最少的操作次数,将该矩阵变为如下形式:

换言之,最终矩阵需满足:a1,1=1,a1,2=2,…,a1,m=m,a2,1=m+1,a2,2=m+2,…,an,m=n⋅m(即对任意 i,j,有 ai,j=(i−1)⋅m+j),且操作总次数应最小。
输入格式
The first line of the input contains two integers n and m (1≤n,m≤2⋅105,n⋅m≤2⋅105) — the size of the matrix.
The next n lines contain m integers each. The number at the line i and position j is ai,j (1≤ai,j≤2⋅105).
输入的第一行包含两个整数 n 和 m(1≤n,m≤2⋅105,且 n⋅m≤2⋅105)——表示矩阵的大小。
接下来的 n 行,每行包含 m 个整数。第 i 行第 j 列的数字为 ai,j(1≤ai,j≤2⋅105)。
输出格式
Print one integer — the minimum number of moves required to obtain the matrix, where a1,1=1,a1,2=2,…,a1,m=m,a2,1=m+1,a2,2=m+2,…,an,m=n⋅m (ai,j=(i−1)m+j).
输出一个整数——得到如下矩阵所需的最少移动次数:其中 a1,1=1,a1,2=2,…,a1,m=m,a2,1=m+1,a2,2=m+2,…,an,m=n⋅m(即 ai,j=(i−1)m+j)。
输入输出样例
输入#1
3 3 3 2 1 1 2 3 4 5 6
输出#1
6
输入#2
4 3 1 2 3 4 5 6 7 8 9 10 11 12
输出#2
0
输入#3
3 4 1 6 3 4 5 10 7 8 9 2 11 12
输出#3
2
说明/提示
In the first example, you can set a1,1:=7,a1,2:=8 and a1,3:=9 then shift the first, the second and the third columns cyclically, so the answer is 6. It can be shown that you cannot achieve a better answer.
In the second example, the matrix is already good so the answer is 0.
In the third example, it is enough to shift the second column cyclically twice to obtain a good matrix, so the answer is 2.
在第一个例子中,你可以设置 a1,1:=7,a1,2:=8 和 a1,3:=9,然后对第一、第二和第三列分别进行循环移位,因此答案为 6。可以证明无法得到更优的答案。
在第二个例子中,矩阵本身已是“好”矩阵,因此答案为 0。
在第三个例子中,只需将第二列循环移位两次即可得到一个“好”矩阵,因此答案为 2。
输入解题思路,AI测评打分。不知道怎么写?