Ural_1146/uva_108 maximum child matrix DP Dimension Reduction

Source: Internet
Author: User

The meaning of the question is very simple. Given a large matrix of N * n, calculate the value and the largest submatrix.

At first, I couldn't find the DP, and there was no optimal sub-structure. Later, Cong gave me the idea and turned it into a one-dimensional model, which turned into a maximum continuous sum. To convert the data into one dimension, the width of the sub-matrix must be enumerated. Through the pre-processing suffix, the sum of a certain segment of each column can be quickly calculated, and then one-dimensional DP can be performed .. The total complexity is O (n ^ 3 );

 

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int mat[110][110];int suffix[110][110];int n;int main(){    while (scanf("%d",&n)!=EOF)    {        for (int i=1; i<=n; i++)        {            for (int j=1; j<=n; j++)            {                scanf("%d",&mat[i][j]);            }        }        //memset(suffix,0,sizeof suffix);        for (int i=1; i<=n; i++)        {            for (int j=1; j<=n; j++)            {                suffix[i][j]=suffix[i][j-1]+mat[i][j];            }        }        int d=0;        int ans=0;        for (int i=1; i<=n; i++)        {            for (int j=i; j<=n; j++)            {                d=0;                for (int k=1; k<=n; k++)                {                    int tmp=suffix[k][j]-suffix[k][i-1];                    if (d+tmp>tmp) d+=tmp;                    else d=tmp;                    ans=max(d,ans);                }            }        }        printf("%d\n",ans);    }    return 0;}

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.