zoukankan      html  css  js  c++  java
  • POJ 1050 To the Max 最大子矩阵和(二维的最大字段和)

    传送门:

    http://poj.org/problem?id=1050

    To the Max
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 52306   Accepted: 27646

    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

    Source

     
    分析:
    给你一个N*N的数字矩阵
    问你子矩阵的最大和是多少
    非常的类似最大子段和问题
    dp[i][j]:表示从第i列到j列的子矩阵的最大和
    那么在第i列到第j列中
    第一行的和就看成一个一个数
    第二行的和也是看成一个数
    第n行的和也是看成一个数
    在这些一维线性的数里面找最大的子段和
    比如样例:
    第列到第四列
    (-2-7+0)=-9
    (2-6+2)=-4
    (1-4+1)=-2
    (8+0-2)=6
    在-9,-4,-2,6里面找最大的子段和,看出来是6
    那么6就是2列到4列的最大子矩阵和
    i列:从1到n
    j列:从i到n
    code:
    #include <iostream>
    #include <cstdio>
    #include<stdio.h>
    #include<algorithm>
    #include<cstring>
    #include<math.h>
    #include<memory>
    #include<queue>
    #include<vector>
    using namespace std;
    #define max_v 105
    #define INF 99999999
    int dp1[max_v][max_v];//起始i列 终止j列的max
    int dp2[max_v];//最大子段和的dp,代表以第i个数结尾的最大子合和值
    int a[max_v][max_v];
    int f(int j1,int j2,int i)//j1列到j2列,i行上数字的和
    {
        int sum=0;
        for(int j=j1;j<=j2;j++)
        {
            sum+=a[i][j];
        }
        return sum;
    }
    int main()
    {
        int n;
        while(~scanf("%d",&n))
        {
            for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++)
                    scanf("%d",&a[i][j]);
            memset(dp1,0,sizeof(dp1));
            dp1[1][1]=a[1][1];
            int result=-INF;
            for(int i=1;i<=n;i++)
            {
                for(int j=i;j<=n;j++)
                {
                    memset(dp2,0,sizeof(dp2));
                    dp2[1]=f(i,j,1);
                    int maxv=dp2[1];
                    for(int k=2;k<=n;k++)
                    {
                        int x=0;
                        if(dp2[k-1]>0)
                            x=dp2[k-1];
                        dp2[k]=x+f(i,j,k);
                        maxv=max(maxv,dp2[k]);
                    }
                    dp1[i][j]=maxv;
                    result=max(result,dp1[i][j]);
                }
            }
            printf("%d
    ",result);
        }
        return 0;
    }
  • 相关阅读:
    Nginx开启Gzip压缩
    VMware克隆虚拟机,克隆机网卡启动不了解决方案
    Linux 几个简单的操作命令
    1. Java环境搭建及demo
    美柚记录
    action找不到
    < >
    document 写法
    develop process
    git stash
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9441184.html
Copyright © 2011-2022 走看看