链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1081

 

这道题使用到的算法是:预处理+最大连续子串和

如果会做最大连续子串和,那么理解这题就相对简单一些,若不知道最大连续子串和,建议先看一下这两题:

http://acm.hdu.edu.cn/showproblem.php?pid=1003

http://www.cnblogs.com/YY56/p/4855766.html

 

 

To The Max

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10107    Accepted Submission(s): 4864


Problem Description
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 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 x 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
 
之前一直不理解虽知道是dp,却不知这是从何而来的,如何计算,

 

代码1:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define N 200
#define oo 0x3f3f3f3f

int a[N][N], dp[N][N];

int main()
{
    int  n;

    while(scanf("%d", &n)!=EOF)
    {
        int i, j, j1, j2;

        memset(dp, 0, sizeof(dp));
        for(i=1; i<=n; i++)
        for(j=1; j<=n; j++)
        {
            scanf("%d", &a[i][j]);
            dp[i][j] = dp[i][j-1] + a[i][j];  /// dp[i][j] i 代表的是第 i 行,j 代表的是这行前 j 个数的和 
        }

        int S = 0;
        for(j1=1; j1<=n; j1++)    
        for(j2=j1; j2<=n; j2++)
        {

      /** 
      
      * i   很明显代表的是行数
      * j1  从第几列开始
      * j2  从第几列结束
      
      **/
      
            int mx=0, my=0;

            for(i=1; i<=n; i++)  
            {
                mx += dp[i][j2] - dp[i][j1-1];  /// mx 代表的是前 i 行里,从第j1-1列到j2列的和(相当于矩阵了)

                if(mx>=0)
                {
                    if(mx>my) my = mx;  /// my 记录的是前 i 行里,从第j1-1列到第j2列矩阵的最大和
                }
                else mx = 0;
            }
            if(my>=S) S = my;  /// S 里面存的肯定是在所有矩阵中取最大值
        }

        printf("%d\n", S);
    }

    return 0;
}

 

 

 

 

代码2:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max2(a,b) (a>b?a:b)

#define N 110
#define INF 0xfffffff

int a[N][N], b[N][N][N];

int main()
{
    int n;
    while(scanf("%d", &n)!=EOF)
    {
        int i, j, k, x, max1;

        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));

        for(i=1; i<=n; i++)
        for(j=1; j<=n; j++)
            scanf("%d", &a[i][j]);

        max1=-INF;
        for(i=1; i<=n; i++)
        for(j=1; j<=n; j++)
        for(x=0, k=j; k>0; k--)
        {
            x += a[i][k];

            b[i][j][k] = max2(b[i][j][k], b[i-1][j][k]) + x;

            if(b[i][j][k]>max1)
                max1 = b[i][j][k];
        }

        printf("%d\n", max1);
    }
    return 0;
}

 

 

题目比较水暴力也可以过

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

#define met(a,b) (memset(a,b,sizeof(a)))
#define N 110
#define INF 0xffffff

int a[N][N], sum[N][N];

int main()
{
    int n;

    while(scanf("%d", &n)!=EOF)
    {
        int i, j, i1, j1, Max=-INF;

        met(a, 0);
        met(sum, 0);

        for(i=1; i<=n; i++)
        for(j=1; j<=n; j++)
        {
            scanf("%d", &a[i][j]);
        }

        for(i=1; i<=n; i++)
        for(j=1; j<=n; j++)
            sum[i][j] = sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1] + a[i][j];

        for(i=0; i<=n; i++)
        for(j=0; j<=n; j++)
        for(i1=i+1; i1<=n; i1++)
        for(j1=j+1; j1<=n; j1++)
        {
            Max = max(Max, sum[i1][j1]-sum[i1][j]-sum[i][j1]+sum[i][j]);
        }

        printf("%d\n", Max);
    }
    return 0;
}

 

posted on 2015-08-21 12:45  栀蓝  阅读(237)  评论(0编辑  收藏  举报

levels of contents