zoukankan      html  css  js  c++  java
  • poj 1050 To the Max(最大子矩阵之和,基础DP题)

    To the Max

    Time Limit: 1000MS
    Memory Limit: 10000K

    Total Submissions: 38573
    Accepted: 20350

    Description

    Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.
    As an example, the maximal sub-rectangle of the array:
    0 -2 -7 0
    9 2 -6 2
    -4 1 -4 1
    -1 8 0 -2
    is in the lower left corner:
    9 2
    -4 1
    -1 8
    and has a sum of 15.

    Input

    The input consists of an N * N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N^2 integers separated by whitespace (spaces and newlines). These are the N^2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].

    Output

    Output the sum of the maximal sub-rectangle.

    Sample Input

    4
    0 -2 -7 0
    9 2 -6 2
    -4 1 -4 1
    -1 8 0 -2

    Sample Output

    15
     
    题解:假设已经知道矩形的上下边界,比如知道矩形的区域的上下边界分别是第a行和第c行,现在要确定左右边界;

    代码:

    #include <iostream>
    #define INF 2147483647
    using namespace std;
    int a[1010][1010];
    int sum[1010][1010];//数组我开的比较大,这无所谓
    
    int Maxsum(int n,int m)
    {//求最大子矩阵之和
        int i,j,k;
        int Max=-INF;
        for(i=0;i<=n;i++)
            sum[i][0]=0;
        for(i=1;i<=m;i++)
            sum[0][i]=0;
        for(i=1;i<=n;i++)
        {
            for(j=i;j<=n;j++)
            {
                sum[j][m]=sum[j-1][m]+a[j][m];//sum[a][b]储存 第b列中 第1行到第a行之间所有元素的和
                int tmp=sum[j][m]-sum[i-1][m];//此时tmp值为 第m列中 第i行到第j行之间所有元素之和
                int big=tmp;
                for(k=m-1;k>=1;k--)
                {
                    if(tmp<0)
                        tmp=0;
                    sum[j][k]=sum[j-1][k]+a[j][k];
                    tmp+=sum[j][k]-sum[i-1][k];
                    big=max(big,tmp);
                    Max=max(big,Max);
                }
            }
        }
        return Max;
    }
    
    int main()
    {
        int n,m,i,j;
        cin>>n;
        m=n;//这里可变为cin>>m,则矩阵是n*m
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
                cin>>a[i][j];
        cout<<Maxsum(n,m)<<endl;
        return 0;
    }
  • 相关阅读:
    C# checklistbox控件用法总结(怎样得到多选的值,以及动态加载数据)
    Tomcat service.xml详解
    修改windows服务器上面tomcat启动窗口的名称
    表空间
    安装selenium遇到的问题
    oracel 锁表查询
    Can not perform this action after onSaveInstanceState
    Caused by: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.HEADSET_PLUG
    Java.lang.IllegalStateException Activity has been destroyed
    StrictMode 使用
  • 原文地址:https://www.cnblogs.com/zyx1314/p/3561519.html
Copyright © 2011-2022 走看看