Matrix (matrix)

[Problem Description]
Lulu designed an IQ test. Test for a matrix of n rows and n columns. Each matrix has a grid on
a number. You have to find the most beautiful of a sub-matrix considered IQ pass.
That is the most beautiful and the most beautiful degrees. We define a matrix for the beauty of this matrix main diagonal figures and subtract
to the sum of the numbers on the diagonal and the other. The main diagonal from upper left to lower right of the diagonal.
[Input format
of the first input line of a positive integer n-(2 <= n-<= 400)
.
Next is a n * n matrix, the matrix is a digital number of 1000 -1000.
Output Format] [
an integer representing the maximum value of all of the sub-matrices beautiful.

input
2
1 -2
4 5
output
4

 

input
3
1 2 3
4 5 6
7 8 9
output
0

 

input
3
-3 4 5
7 9 -2
1 0 -6
output
5

 

For 50% of the data, N≤100.
To 100% of the data, 2 ≤ N ≤ 400.

 

Prefix and optimization

n^3

#include <iostream>
#include <cstdio>
#define max(a,b) a>b?a:b
#define R register
#define MAXN 400+5
using namespace std;
#define ll long long
ll n;
ll mp[405][405],s1[405][405],s2[405][405];
int main()
{
    freopen("matrix.in","r",stdin);
    freopen("matrix.out","w",stdout);
    cin>>n;
    for(R int i=1;i<=n;i++)
    for(R int j=1;j<=n;j++) 
    {
        cin>>mp[i][j];
        s1[i][j]=s1[i-1][j-1]+mp[i][j];
        s2[i][j]=s2[i-1][j+1]+mp[i][j];
    }
    int ans=0;
    for(R int i=1;i<=n;i++)
        for(R int j=1;j<=n;j++)
            for(R int k=1;k+i<=n&&j+k<=n;k++)
                ans=max(ans,s1[i+k][j+k]-s1[i-1][j-1]-s2[i+k][j]+s2[i-1][j+k+1]);
    cout<<ans<<endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/000226wrp/p/11352791.html