贪心 Codeforces Round #675 Div. 2 B. Nice Matrix

原题链接: 传送门

B. Nice Matrix

题面

B. Nice Matrix
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
A matrix of size n×m is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers (a1,a2,…,ak) is a palindrome, if for any integer i (1≤i≤k) the equality ai=ak−i+1 holds.

Sasha owns a matrix a of size n×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!

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

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

Each of the next n lines contains m integers ai,j (0≤ai,j≤109) — the elements of the matrix.

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

Example
inputCopy
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
outputCopy
8
42
Note
In the first test case we can, for example, obtain the following nice matrix in 8 operations:

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

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

题解

题目意思是给你一个n*m的矩阵,你每次操作可以让矩阵内任意一个元素加1或者减1,求经过多次操作把矩阵变为回文矩阵的最小操作数。(回文矩阵即矩阵的每一行每一列都是一个回文序列)

拿样例来说:
1 2 3 4
5 6 7 8
9 10 11 18
我们会发现,变为回文串之后,a[i][j],a[i][m-j+1],a[n-i+1][j],a[n-i+1][m-j+1](我这里的下标从1开始)。
这四个数一定会相等(根据回文的定义得知),而且与其他数是多少貌似并无关系。
也就是说: 如果我们能让每一组这样位置的元素变为一个相等的数的操作数最小,最后的答案肯定就是最小的(贪心)
比如 上面的 1 4 9 18 为一组,我们会发现,其实把所有数变为这一组数的中位数即是最小的情况(具体就不证明了,应该好理解,多写几个数就理解了)
思路有了,如何写代码呢
我们按照矩阵的顺序遍历所有元素,定义is二维数组判断该元素是否被访问过(被访问过即直接跳过)。将每一组数据加入到vector数组中排序,找出中位数。然后计算操作数,并且把这一组元素全部定义为已遍历。
注意:
m-j+1与j有可能相等,n-i+1与i又可能相等,要注意判断这种情况。

AC代码

#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
typedef long long int ll;
using namespace std;
ll a[110][110];
int is[110][110];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll mn=0,ans=0;
        memset(a,0,sizeof(a));
        memset(is,0,sizeof(is));
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            cin>>a[i][j];
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(is[i][j]==1) continue;
                vector<ll> h;
                h.push_back(a[i][j]);
                is[i][j]=1;
                h.push_back(a[i][m-j+1]);
                is[i][m-j+1]=1;
                h.push_back(a[n-i+1][j]);
                is[n-i+1][j]=1;
                h.push_back(a[n-i+1][m-j+1]);
                is[n-i+1][m-j+1]=1;
                sort(h.begin(),h.end());
                ll x=h[(h.size()+1)/2-1];
                ans+=abs(a[i][j]-x);
                if((m-j+1)!=j&&(n-i+1)!=i) ans+=abs(a[i][m-j+1]-x)+abs(a[n-i+1][j]-x)+abs(a[n-i+1][m-j+1]-x);
                else if((m-j+1)!=j&&(n-i+1)==i) ans+=abs(a[i][m-j+1]-x);
                else if((m-j+1)==j&&(n-i+1)!=i) ans+=+abs(a[n-i+1][j]-x);
                a[i][j]=a[i][m-j+1]=a[n-i+1][j]=a[n-i+1][m-j+1]=x;
            }
        }
        cout<<ans<<endl;

    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值