You are given a rectangular matrix of size consisting of integers from to .
In one move, you can:
choose any element of the matrix and change its value to any integer between and , 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 and set 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 with the minimum number of moves performed.
Input
The first line of the input contains two integers and — the size of the matrix.
The next n lines contain m integers each. The number at the line and position is .
Output
Print one integer — the minimum number of moves required to obtain the matrix, where .
Examples
inputCopy
3 3
3 2 1
1 2 3
4 5 6
outputCopy
6
inputCopy
4 3
1 2 3
4 5 6
7 8 9
10 11 12
outputCopy
0
input
3 4
1 6 3 4
5 10 7 8
9 2 11 12
output
2
Note
In the first example, you can set and 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.
顯然列與列之間互不影響,因此只需單獨考慮每一列的最小操作數,然後求和即可。
設爲移動步所需的最小花費,初始狀態。
爲數值最終所在的行數。
對於第列的每一行,如果最終狀態中該列存在元素,則更新,最終更新。
#include<bits/stdc++.h>
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;
inline int qr() {
int f = 0, fu = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')fu = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
f = (f << 3) + (f << 1) + c - 48;
c = getchar();
}
return f * fu;
}
const int N = 2e5 + 10;
int n, m, spd[N];
mii loc;
vi a[N];
inline void init(int j) {
repi(i, 0, n - 1)spd[i] = i + n;
loc.clear();
repi(i, 1, n)loc[(i - 1) * m + j] = i;
}
int main() {
n = qr(), m = qr();
repi(i, 1, n)repi(j, 1, m)a[i].pb(qr());
int ans = 0;
repi(i, 0, m - 1) {
init(i + 1);
repi(j, 1, n)
if (loc[a[j][i]])
spd[(j - loc[a[j][i]] + n) % n]--;
int mn = INF;
repi(j, 0, n - 1)mn = min(mn, spd[j]);
ans += mn;
}
pi(ans);
return 0;
}