CF1422B.Nice Matrix

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A matrix of size n×mn \times m is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers (a1,a2,…,ak)(a_1, a_2, \dots , a_k) is a palindrome, if for any integer ii (1≤i≤k1 \le i \le k) the equality ai=ak−i+1a_i = a_{k - i + 1} holds.

Sasha owns a matrix aa of size n×mn \times m. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.

Help him!

一个大小为 n×mn \times m 的矩阵被称为“优美的”,当且仅当该矩阵的所有行和所有列均为回文序列。整数序列 (a1,a2,…,ak)(a_1, a_2, \dots , a_k) 是回文序列,当且仅当对任意整数 ii(1≤i≤k1 \le i \le k),均满足 ai=ak−i+1a_i = a_{k - i + 1}。

萨沙拥有一张大小为 n×mn \times m 的矩阵 aa。在一次操作中,他可以将矩阵中的任意一个数加一或减一。萨沙希望将该矩阵变为优美的矩阵。他关心的是:最少需要多少次操作?

请帮助他!

输入格式

The first line contains a single integer tt — the number of test cases (1≤t≤101 \le t \le 10). The tt tests follow.

The first line of each test contains two integers nn and mm (1≤n,m≤1001 \le n, m \le 100) — the size of the matrix.

Each of the next nn lines contains mm integers ai,ja_{i, j} (0≤ai,j≤1090 \le a_{i, j} \le 10^9) — the elements of the matrix.

第一行包含一个整数 tt —— 测试用例的数量(1≤t≤101 \le t \le 10)。接下来是 tt 个测试用例。

每个测试用例的第一行包含两个整数 nn 和 mm(1≤n,m≤1001 \le n, m \le 100)—— 矩阵的大小。

接下来的 nn 行中,每行包含 mm 个整数 ai,ja_{i, j}(0≤ai,j≤1090 \le a_{i, j} \le 10^9)—— 矩阵的元素。

输出格式

For each test output the smallest number of operations required to make the matrix nice.

对于每个测试用例,输出使矩阵“优美”所需的最少操作次数。

输入输出样例

  • 输入#1

    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

    输出#1

    8
    42

说明/提示

In the first test case we can, for example, obtain the following nice matrix in 88 operations:

2 2
4 4
4 4
2 2

In the second test case we can, for example, obtain the following nice matrix in 4242 operations:

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

在第一个测试用例中,我们可以通过例如 8 次操作得到如下“优美”矩阵:

2 2
4 4
4 4
2 2

在第二个测试用例中,我们可以通过例如 42 次操作得到如下“优美”矩阵:

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

输入解题思路,AI测评打分。不知道怎么写?

首页