zoukankan      html  css  js  c++  java
  • TZOJ 4475 The Coolest Sub-matrix(对角线前缀和)

    描述

    Given an N*N matrix, find the coolest square sub-matrix.
    We define the cool value of the square matrix as X-Y where X indicating the sum of all integers of the main diagonal and Y indicating the sum of the other diagonal.

    输入

    The first line has a positive integer N (2 ≤ N ≤ 400), the size of the matrix.
    The following N lines each contain N integers in the range [-1000, 1000], the elements of the matrix.

    输出

    Output the coolest value of a square sub-matrix.

    样例输入

    2
    1 -2
    4 5

    样例输出

    4

    题意:

    在n*n的矩阵中找到一个子矩阵,使得其 (正对角线整数之和-反对角线整数之和) 最大

    思路:

    记录矩阵中各对角线的前缀和,将所取对角线两端的前缀和相减,所得即为该对角线整数之和。

    #include<bits/stdc++.h>
    #define MAX 405
    using namespace std;
    int A1[MAX][MAX],A2[MAX][MAX];//A1正对角线前缀和 与 A2反对角线前缀和 
    int main()
    {
        int i,j,k,n,x,maxx=-1;
        cin>>n;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                scanf("%d",&x);
                A1[i][j]=A1[i-1][j-1]+x;//左上加到右下 
                A2[i][j]=A2[i-1][j+1]+x;//右上加到左下 
            }
        }
        for(i=1;i<=n;i++)    //起始行位置i 
            for(j=1;j<=n;j++)//起始列位置j 
                for(k=1;k<=min(n+1-i,n+1-j);k++)//小矩阵边长k 
                    maxx=max(maxx,A1[i+k][j+k]-A1[i-1][j-1]-(A2[i+k][j]-A2[i-1][j+k+1]));
        cout<<maxx<<endl;
        return 0;
    } 
  • 相关阅读:
    [LuoguP2161] 会场预约
    [LuoguP1198] 最大数
    [LuoguP1484] 种树
    [LuoguP1801] 黑匣子
    [LuoguP1196]银河英雄传说
    [LuoguP1345] 奶牛的电信Telecowmunication
    [LuoguP1119]灾后重建
    【笔记】一元函数微分学
    【复习】Listening and Reading Comprehension
    【笔记】一元函数的不定积分
  • 原文地址:https://www.cnblogs.com/kannyi/p/9610277.html
Copyright © 2011-2022 走看看