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

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

Sample Output

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

代码:

  1. #include <iostream>
  2. #define INF 2147483647
  3. using namespace std;
  4. int a[1010][1010];
  5. int sum[1010][1010];//数组我开的比较大,这无所谓
  6.  
  7. int Maxsum(int n,int m)
  8. {//求最大子矩阵之和
  9. int i,j,k;
  10. int Max=-INF;
  11. for(i=0;i<=n;i++)
  12. sum[i][0]=0;
  13. for(i=1;i<=m;i++)
  14. sum[0][i]=0;
  15. for(i=1;i<=n;i++)
  16. {
  17. for(j=i;j<=n;j++)
  18. {
  19. sum[j][m]=sum[j-1][m]+a[j][m];//sum[a][b]储存 第b列中 第1行到第a行之间所有元素的和
  20. int tmp=sum[j][m]-sum[i-1][m];//此时tmp值为 第m列中 第i行到第j行之间所有元素之和
  21. int big=tmp;
  22. for(k=m-1;k>=1;k--)
  23. {
  24. if(tmp<0)
  25. tmp=0;
  26. sum[j][k]=sum[j-1][k]+a[j][k];
  27. tmp+=sum[j][k]-sum[i-1][k];
  28. big=max(big,tmp);
  29. Max=max(big,Max);
  30. }
  31. }
  32. }
  33. return Max;
  34. }
  35.  
  36. int main()
  37. {
  38. int n,m,i,j;
  39. cin>>n;
  40. m=n;//这里可变为cin>>m,则矩阵是n*m
  41. for(i=1;i<=n;i++)
  42. for(j=1;j<=m;j++)
  43. cin>>a[i][j];
  44. cout<<Maxsum(n,m)<<endl;
  45. return 0;
  46. }

poj 1050 To the Max(最大子矩阵之和,基础DP题)的更多相关文章

  1. poj 1050 To the Max(最大子矩阵之和)

    http://poj.org/problem?id=1050 我们已经知道求最大子段和的dp算法 参考here  也可参考编程之美有关最大子矩阵和部分. 然后将这个扩大到二维就是这道题.顺便说一下,有 ...

  2. poj 1050 To the Max 最大子矩阵和 经典dp

    To the Max   Description Given a two-dimensional array of positive and negative integers, a sub-rect ...

  3. POJ 1050 To the Max 最大子矩阵和(二维的最大字段和)

    传送门: http://poj.org/problem?id=1050 To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submi ...

  4. POJ 1050 To the Max (最大子矩阵和)

    题目链接 题意:给定N*N的矩阵,求该矩阵中和最大的子矩阵的和. 题解:把二维转化成一维,算下就好了. #include <cstdio> #include <cstring> ...

  5. hdu 1081 &amp; poj 1050 To The Max(最大和的子矩阵)

    转载请注明出处:http://blog.csdn.net/u012860063 Description Given a two-dimensional array of positive and ne ...

  6. [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

  7. POJ 1050 To the Max 暴力,基础知识 难度:0

    http://poj.org/problem?id=1050 设sum[i][j]为从(1,1)到(i,j)的矩形中所有数字之和 首先处理出sum[i][j],此时左上角为(x1,y1),右下角为(x ...

  8. POJ 1050 To the Max -- 动态规划

    题目地址:http://poj.org/problem?id=1050 Description Given a two-dimensional array of positive and negati ...

  9. poj - 1050 - To the Max(dp)

    题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 ...

随机推荐

  1. IOS 之 PJSIP 笔记(一) 编译多平台支持的静态库

    好久没有写博客了,这也算是我步入新工作后的第一篇技术博文吧.在进入新公司前,早就有了技术层进入下一个迭代的准备,但很多事情是意想不到的,就像我以 C# 程序员的身份面试入职的,而今却是一个全职的 IO ...

  2. MVC+EF+esayui初试(一 布局加菜单显示)

    最近都在做linq+ext.net的开发.这两天想学习下MVC和ef,刚好,在看ext.js的时候也喜欢上了esayui,所以就想用mvc+ef+esayui做一个汽车网后台管理来加强下.在这里也把我 ...

  3. 【jQuery基础学习】12 jQuery学习感想

    学习完<锋利的jQuery>,用时13天. 这期间,私底下又用了一点时间去W3C上把HTML和CSS重新过了一遍. 总的来说,收获还是蛮多的. 其实在本书里面真正重要的也就前几章,后面的都 ...

  4. 咋一看DWoo 比 Smarty要好

    虽然很少用模板引擎,但总是有要用到的时候. 随意翻看了两者代码,发现Smarty发展了这么多年居然还在用Eval实现一些特性.其实这没有什么高不高级之分,只是因为eval这个东东,导致一旦语法出错时, ...

  5. 泛函编程(7)-数据结构-List-折叠算法

    折叠算法是List的典型算法.通过折叠算法可以实现众多函数组合(function composition).所以折叠算法也是泛函编程里的基本组件(function combinator).了解折叠算法 ...

  6. T-SQL的回车和换行符(SQL)

    T-SQL的回车和换行符(SQL) sql server中的回车换行字符是  char(13)+char(10) 回车:char(13) 换行:char(10) 实例1: DECLARE @c NVA ...

  7. Android应用开发基础之六:页面跳转和数据传递

    创建第二个Activity 需要在清单文件中为其配置一个activity标签 标签中如果带有这个子节点,则会在系统中多创建一个快捷图标 <intent-filter> <action ...

  8. [C#]多线程下载

    发现电脑里以前编写的下载程序... 做个记录,那时做的挺匆忙的,没用委托,通过公开出窗体来修改下载进度,做的比较乱... ==!! 程序具体功能(流程): 1.检测系统托盘图标内的进程名是否符合要求 ...

  9. JS中检测数据类型的四种方法

    1.typeof 用来检测数据类型的运算符->typeof value->返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number"."st ...

  10. R语言学习笔记:绘制地图

    在R中画地图先从简单的maps包开始. library("maps") 在这个maps包中有一些数据集,用命令data(package=”maps”),可以看到如下数据: cana ...