Daily Question·Summer[Acwing 3565] Perfect Matrix

Link to the original title: Acwing 3565 Perfect Matrix

topic

A matrix is ​​said to be a perfect matrix if all its rows and columns are palindrome sequences.

An integer sequence a 1 , a 2 , … , ak a_1,a_2,…,a_ka1,a2,,ak, if for any integer i ( 1 ≤ i ≤ k 1≤i≤k1ik ), the equationai = ak − i + 1 a_i=a_k−i+1ai=aki+1 , the sequence is a palindrome sequence.

Given an n×m matrix a, each operation can add one or subtract one to an element in the matrix, how many operations can the matrix a become a perfect matrix at least?

input format

The first line contains an integer T, indicating that there are T sets of test data.

The first line of each set of data contains integers n and m, indicating the size of the matrix.

Next n lines, each line contains m integers aij a_{ij}aij, representing elements in the matrix.

output format

Each set of data outputs one line, one answer, indicating the minimum number of operations.

data range

1 ≤ T ≤ 10 1≤T≤10 1T10,
1 ≤ n , m ≤ 100 1≤n,m≤100 1n,m100,
0 ≤ a i j ≤ 1 0 9 0≤a_{ij}≤10^9 0aij109

Input sample:

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

Sample output:

8
42

sample explanation

The first set of data can get the following matrix through 8 steps:

2 2
4 4
4 4
2 2

The second set of data can obtain the following matrix through 42 steps of operation:

5 6 6 5
6 6 6 6
5 6 6 5

Ideas:

For a perfect matrix, if it is a palindrome sequence, and it is required to be the same horizontally and vertically, in essence, it is required that every four-tuple obtained by the following symmetry is the same.
insert image description here
So you only need to enumerate all the points in the upper left block, get all the quadruples, and then make each quadruple equal, and the least number of steps can make the overall number of steps the least.
For a , b , c , da,b,c,da,b,c,d four numbers, let them become the same and the number of steps is the least.
Find ∣a − x ∣ + ∣ b − x ∣ + ∣ c − x ∣ + ∣ d − x ∣ |ax|+|bx|+|cx|+|dx|ax+bx+cx+dx is the smallest, according to the absolute value inequality, we can know thatxxx a , b , c , d a,b,c,d a,b,c,The median of d is the smallest. If it is an even number, any value between the middle two numbers is fine.
 
Considering that the obtained quadruple may only have 2 points or 1 point, it can be deduplicated through set storage, because we obtain the quadruple through symmetry.

Even numbers are enumerated to the front of the middle line, and odd numbers are enumerated to the row (or column) of the middle line

the code

#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <vector>

#define x first
#define y second

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int N = 110;

int n, m;
int w[N][N];

LL calc(set<PII> S)
{
    
    
    vector<int> q;
    for (auto& p: S) q.push_back(w[p.x][p.y]);
    sort(q.begin(), q.end());
    LL res = 0;
    for (int i = 0; i < q.size(); i ++ )
        res += abs(q[i] - q[q.size() / 2]);
    return res;
}

int main()
{
    
    
    int T;
    scanf("%d", &T);
    while (T -- )
    {
    
    
        scanf("%d%d", &n, &m);
        for (int i = 0; i < n; i ++ )
            for (int j = 0; j < m; j ++ )
                scanf("%d", &w[i][j]);

        LL res = 0;
        for (int i = 0; i <= n - 1 - i; i ++ )
            for (int j = 0; j <= m - 1 - j; j ++ )
                res += calc({
    
    {
    
    i, j},{
    
    i, m - 1 - j},{
    
    n - 1 - i, j},{
    
    n - 1 - i, m - 1 - j}});
        printf("%lld\n", res);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/xxmy7/article/details/117282279