The largest sub-matrix (greedy)

Although I wrote greedy

But the question of how greedy feeling

Feel unable to be greedy

After I was tortured for a long time

I am from an on someone else's blog

Inspired by

Similar cut and complement geometry

And counted out the rows of the matrix

And then for poor comparison

Draw a matrix

Before comparing the calculated weight matrix

To see if greater than zero, greater than zero would add less than zero to rounding

Then enumerate

Then the heart of this question on the same subject

#include<iostream>
#include<cstdio>
using namespace std;
int N[105][105];
int maxn=-1e3;
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            {
                cin>>N[i][j];
                N[i][j]+=N[i-1][j];
            }
    for(int i=1;i<=n;i++)
        for(int j=i;j<=n;j++)
            {
                    int ans=0;
                    for(int l=1;l<=n;l++)
                        {
                            ans=N[j][l]-N[i-1][l]+max(0,ans);
                            maxn=max(ans,maxn);
                        }
            }
    cout<<maxn;
}

Always be sure to be sure to remember a detail -

maxn initial value can not be zero, can not be zero, it can not be zero

(Because of this, several times without too)

 

Guess you like

Origin www.cnblogs.com/-Iris-/p/12319588.html