Codeforces Round #615 (Div. 3) E Obtain a Permutation (思維)

題目鏈接

E. Obtain a Permutation

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a rectangular matrix of size n×mn×m consisting of integers from 11 to 2⋅1052⋅105.

In one move, you can:

  • choose any element of the matrix and change its value to any integer between 11 and n⋅mn⋅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 jj (1≤j≤m1≤j≤m) and set a1,j:=a2,j,a2,j:=a3,j,…,an,j:=a1,ja1,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⋅ma1,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+jai,j=(i−1)⋅m+j) with the minimum number of moves performed.

Input

The first line of the input contains two integers nn and mm (1≤n,m≤2⋅105,n⋅m≤2⋅1051≤n,m≤2⋅105,n⋅m≤2⋅105) — the size of the matrix.

The next nn lines contain mm integers each. The number at the line ii and position jj is ai,jai,j (1≤ai,j≤2⋅1051≤ai,j≤2⋅105).

Output

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⋅ma1,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+jai,j=(i−1)m+j).

Examples

input

Copy

3 3
3 2 1
1 2 3
4 5 6

output

Copy

6

input

Copy

4 3
1 2 3
4 5 6
7 8 9
10 11 12

output

Copy

0

input

Copy

3 4
1 6 3 4
5 10 7 8
9 2 11 12

output

Copy

2

Note

In the first example, you can set a1,1:=7,a1,2:=8a1,1:=7,a1,2:=8 and a1,3:=9a1,3:=9 then shift the first, the second and the third columns cyclically, so the answer is 66. It can be shown that you cannot achieve a better answer.

In the second example, the matrix is already good so the answer is 00.

In the third example, it is enough to shift the second column cyclically twice to obtain a good matrix, so the answer is 22.

 

題意:

給你一個n*m的數字矩陣a[n][m],保證n*m<=2e5,a[i][j]<=2e5

你有兩種操作:

1、在一次操作中,將數組中一個元素變成任意一個數

2、在一次操作中,將某一列向上循環移動一位

問最少多少次操作,將數字矩陣變成如下形式:

思路:

考慮對於某一列每個數,計算它應該在這一列的什麼位置,這樣我們就能計算出它這一列上移幾位這個數才能到正確的位置。

對於不能移到正確位置的數,我們只能通過第一種操作強行改變。

最後,計算一下每一列最少需要進行多少次操作,然後累加一下。總體複雜度O(n*m)。

PS:這道題有個巨坑點,沒說a[i][j]<=nm,所以邊界處理要注意,不然會掛!終!測!(不要問我爲什麼知道,我只是終測的時候wa on test 85),作爲div3的E題來說,並不算難題。

代碼:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define dep(i,a,b) for(int i=(a);i>=(b);i--)
using namespace std;
const int maxn=2e6+5;
//const double pi=acos(-1.0);
//const double eps=1e-9;
//const ll mo=1e9+7;
int n,m,k,x;
int a[maxn],c[maxn];
int ans,tmp,cnt;
int flag;
int set_id(int i,int j)
{
    return (i-1)*m+j;
}
int main()
{
    int T,cas=1;
    scanf("%d%d",&n,&m);
    rep(i,1,n)
    rep(j,1,m)
    {
        int id=set_id(i,j);
        scanf("%d",&a[id]);
    }
    int ans=0;
        rep(j,1,m)
        {
            rep(i,0,n-1) c[i]=0;
            int as=n;
            rep(i,1,n)
            {
                int id=set_id(i,j);
                if(a[id]%m==j%m)
                {
                    int s=(a[id]-j)/m+1;
                    if(s<=0||s>n) continue;
                    int k=(i-s+n)%n;
                    c[k]++;
                }
            }
            rep(i,0,n-1)
            as=min(as,i+n-c[i]);
            //cout<<as<<endl;
            ans+=as;
        }
        printf("%d\n",ans);
    return 0;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章