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

题意:求最大的子矩阵的和

解题思路:通过循环,用b[k]数组储存每列的和,例如当循环到第2行时,b[0]储存的就是5。 5是怎么来的呢?它是0+9+(-4)=5。
                                 b[1]储存的就是1。 -2+2+1=1 这样求出每列的和然后同时找b[k]序列的最大子段和,不断更新最大值,循环完之后,就可以找出最大值了..... 也许这样比较抽象,举个栗子:(这里就不用N行N列的做例子了.....) 2维数组:     
         1 2 3
        -5 6 7
     我们先求
          
          第0行   b数组 1 2 3                最大子段和:1+2+3=6  这里可以理解为  这是 1 2 3  这个子矩阵
          
          第1行
                b数组 -4 8 10                最大子段和:8+10=18  这里就是 2 3
                                       6 7  这个子矩阵                 所以答案就是18 怎么说,思想应该是通过求每列的和,使得它变成一个求最大字段和的问题......... 代码如下:(去掉注释,也许会对理解思路有帮助....)
 #include<stdio.h>
#include <limits>
#include<string.h>
using namespace std;
int a[][],b[];
int n,cursum=-,max=numeric_limits<int>::min(); int curmaxsum()
{
int sum=0,cursum=-130;
for(int i=; i<n; i++)
{
sum+=b[i];
if(sum<)
sum=b[i];
if(sum>cursum)
cursum=sum;
}
return cursum;
} int maxsub()
{
for(int i=; i<n; i++)
{
memset(b,,sizeof(b));
for(int j=i; j<n; j++)
{
//printf("\nj=%d\n",j);
for(int k=; k<n; k++)
{
b[k]+=a[j][k];
}
/*for(int k=0; k<n-1; k++)
printf("%d ",b[k]);
printf("%d\n",b[n-1]);*/ curmaxsum();
//printf("cursum=%d\n",cursum);
if(cursum>max)
max=cursum;
// printf("max=%d\n",max);
}
}
return max;
} int main()
{
while(scanf("%d",&n)==)
{
for(int i=; i<n; i++)
for(int j=; j<n; j++)
scanf("%d",&a[i][j]);
// printf("******************************\n\n");
maxsub();
printf("%d\n",max);
}
return ;
}


ZOJ 1074 最大子矩阵和的更多相关文章

  1. ZOJ 1074 To the Max(DP 最大子矩阵和)

    To the Max Time Limit: 2 Seconds      Memory Limit: 65536 KB Problem Given a two-dimensional array o ...

  2. ZOJ 1074 To the Max

    原题链接 题目大意:这是一道好题.在<算法导论>这本书里面,有一节是介绍如何求最大子序列的.这道题有点类似,区别是从数组变成了矩阵,求最大子矩阵. 解法:完全没有算法功底的人当然不知道最大 ...

  3. HDOJ 1081(ZOJ 1074) To The Max(动态规划)

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

  4. [LeetCode] 1074. 元素和为目标值的子矩阵数量

    矩阵前缀和.因为矩阵中可能包含负值,所以这题肯定不会存在什么剪枝,动态规划的可能性.所以这个题也就没什么弯弯绕绕.个人感觉算不上个Hard题目. 最直观的思路就是枚举子矩阵,既枚举矩阵的左上角节点和右 ...

  5. ZOJ 3367 Counterfeit Money(最大相同子矩阵)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3909 题意:给出两个矩阵A和B,找出最大的相同子矩阵S.输出S的高和 ...

  6. ZOJ题目分类

    ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 13 ...

  7. ZOJ 刷题记录 (。・ω・)ノ゙(Progress:31/50)

    [热烈庆祝ZOJ回归] P1002:简单的DFS #include <cstdio> #include <cstring> #include <algorithm> ...

  8. ZOJ 1859 Matrix Searching(二维线段树)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1859 Matrix Searching Time Limit: 10 Seco ...

  9. ACM 中 矩阵数据的预处理 && 求子矩阵元素和问题

            我们考虑一个$N\times M$的矩阵数据,若要对矩阵中的部分数据进行读取,比如求某个$a\times b$的子矩阵的元素和,通常我们可以想到$O(ab)$的遍历那个子矩阵,对它的各 ...

随机推荐

  1. Android(java)学习笔记94:关于广播接收者的注册和使用心得

    下面我们先看一部分代码,由代码进行进一步的深入: registerReceiver( new BroadcastReceiver() {//onReceive中代码的执行时间不要超过5s,androi ...

  2. 安装openshift客户端工具 rhc

    安装ruby: $ sudo apt-get install ruby-full 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完成 将会安装下列额外的软件包: ...

  3. Content-Type一览

    文件扩展名 Content-Type(Mime-Type) 文件扩展名 Content-Type(Mime-Type) .*( 二进制流,不知道下载文件类型) application/octet-st ...

  4. vb.net Linq 筛选(像 select distinct) DateTable 日期数据中的年份

    Private Sub initDDLByYear(ByVal dt As DataTable) ddlByYear.Items.Clear() ddlByYear.Items.Add(") ...

  5. [设计模式]<<设计模式之禅>>关于依赖倒置原则

    依赖倒置原则(Dependence Inversion Principle,DIP)这个名字看着有点别扭,“依赖”还“倒置”,这到底是什么意思?依赖倒置原则的原始定义是 High level modu ...

  6. Apache ab 测试工具使用(一)

    简述: 试用apache ab测试工具 下载点 http://httpd.apache.org/download.cgi 参考: http://jingyan.baidu.com/article/e3 ...

  7. 从零单排Linux – 1 – 简单命令

    从零单排Linux – 1 – 简单命令 Posted in: Linux 从零单排Linux – 1 一.Linux的简单命令: 1.忘记root密码: 读秒时按任意键进入 – e – ↓选择第二个 ...

  8. 把mvc4彻底搞定(一)

    1 .net web开发方式 webform开发 asp.net mvc开发 2 请求模式 webform方式:客户端向服务端请求一个aspx页面,服务端生成一个页面对象类,调用这个页面对象类的PR方 ...

  9. hiho 1182 : 欧拉路·三

    1182 : 欧拉路·三 这时题目中给的提示: 小Ho:是这样的,每次转动一个区域不是相当于原来数字去掉最左边一位,并在最后加上1或者0么. 于是我考虑对于"XYYY",它转动之后 ...

  10. 如何找出MySQL数据库中的低效SQL语句

    面对业务的迅猛发展,DBA的一项重要工作就是及时发现数据库中的低效SQL语句,有的可以立刻着手解决(比如缺少合适的索引),有的需要尽快反馈给开发人员进行修改. MySQL数据库有几个配置选项可以帮助我 ...