题目链接 Problem - 1081

题意

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.

求最大的子矩阵和。

题解

这题注意要多组输入输出。

方法1

我自己想的\(O(n^3)\)的算法比较烦:(为什么每次我想的都那么不正常)

前缀和s[i][j],表示(0,0)-(i,j)子矩阵的和。

子矩阵(i,j)-(k,l)的和就是s[k][l]-s[i][l]-s[k][j]+s[i][j]

枚举右下角(K,L),和左上角的行号i,那么s[k][l]-s[i][l]是固定的,要让s[k][j]-s[i][j]最小。

于是g[k][i]保存和最小的s[k][j]-s[i][j]且小于l的j。

方法2

看了别人的,突然觉得自己的真麻烦。

s[i][j]表示第i行的前j列的和。

枚举左右边界的列编号i,j,sum保存第i列到第j列从第k行往上连续的最大和。这个过程只需枚举k从1到n,只要之前的sum是正的就继续累加,否则sum=0再加:sum+=s[k][j]-s[k][i-1]。用sum更新ans即可。

代码

方法1

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define N 105
using namespace std;
int n,a[N][N],g[N][N],s[N][N],ans;
int main() {
while(~scanf("%d",&n)){
ans=-127;
memset(g,0,sizeof g);
memset(s,0,sizeof s); for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&a[i][j]);
for(int k=1;k<=n;k++)
for(int l=1;l<=n;l++){
s[k][l]=s[k-1][l]+s[k][l-1]-s[k-1][l-1]+a[k][l];
for(int i=0;i<k;i++){
int &j=g[k][i];
ans=max(ans,s[k][l]-s[i][l]-s[k][j]+s[i][j]);
if(s[k][j]-s[i][j]>s[k][l]-s[i][l])j=l;
}
}
printf("%d\n",ans);
}
return 0;
}

方法2

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define N 105
using namespace std;
int n,s[N][N],ans,sum;
int main() {
while(~scanf("%d",&n)){
memset(s,0,sizeof s);
ans=-127;sum=0;
for(int i=1,a;i<=n;i++)
for(int j=1;j<=n;j++){
scanf("%d",&a);
s[i][j]=s[i][j-1]+a;
}
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
for(int k=1;k<=n;k++){
if(k==1||sum<0)sum=0;
sum+=s[k][j]-s[k][i-1];
ans=max(ans,sum);
}
printf("%d\n",ans);
}
return 0;
}

【 HDU1081 】 To The Max (最大子矩阵和)的更多相关文章

  1. poj 1050 To the Max(最大子矩阵之和,基础DP题)

    To the Max Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 38573Accepted: 20350 Descriptio ...

  2. hdu1081 To The Max 2016-09-11 10:06 29人阅读 评论(0) 收藏

    To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  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. POJ1050 To the Max 最大子矩阵

    POJ1050 给定一个矩阵,求和最大的子矩阵. 将每一列的值进行累加,枚举起始行和结束行,然后就可以线性优化了 复杂度O(n^3) #include<cstdio> #include&l ...

  6. HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)

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

  7. hdu1081 To the Max

    直接暴力枚举所有子矩形至少需要O(n^4)的复杂度,显然这不是一个合理的解决方法. 上述方案忽略了矩形之间的联系,进行了过多不必要的计算. 实际上如果固定矩形的左右边界,则底边在i行的矩形内数值之和与 ...

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

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

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

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

  10. DP:0

    小故事: A * "1+1+1+1+1+1+1+1 =?" * A : "上面等式的值是多少" B : *计算* "8!" A *在上面等式 ...

随机推荐

  1. php的foreach中使用取地址符,注意释放

    先来举个例子: <?php $array = array(1, 2, 3); foreach ($array as &$value) {} // unset($value); forea ...

  2. Windows下jupyter notebook 修改打开的浏览器

    1. 打开cmd,输入jupyter notebook --generate-config 2. 根据返回的路径打开 C:\Users\Administrator\.jupyter\jupyter_n ...

  3. Spring Boot Admin 日志查看功能

    按照官方配置POM和配置文件后,能够结合Eureka查看各微服务状态,但是日志始终查看不了,出现406等错误. 最后偶然发现,是在在从官方网站拷贝配置的时候,出现的问题. logging.file=* ...

  4. fastjson tojson部分规则

    fastjson 作为java 目前最快速,最轻便  json对象,与json 字符串转换 第三方包,阿里巴巴提供. 对象转json规则 转json字符串 列 JSONObject.toJSON(ne ...

  5. koa2入门(2) koa-router 路由处理

    项目地址:https://github.com/caochangkui/demo/tree/koa-test 1. 创建项目 创建目录 koa-test npm init 创建 package.jso ...

  6. 止不住的裁员潮:看京东前员工吐槽——绩效打C还希望我好好干

    昨天,京东裁员消息被证实,京东将在2019年末位淘汰10%的副总裁级别以上的高管. 在互联网职场交流社区,一名自称京东的员工如此吐槽:办完离职了心情大好,自由放飞,明天入职新公司,你给新员工打C,还希 ...

  7. Haproxy基础知识 -运维小结

    开源软件负载均衡器 现在常用的三大开源软件负载均衡器分别是Nginx.LVS.Haproxy. 在之前的文章中已经对比了这三个负载均衡软件, 下面根据自己的理解和使用经验, 再简单说下这三个负载均衡软 ...

  8. cordova打包webapp

    cordova打包webapp 在项目开发中,需要将h5页面打包成app,这个时候我们可以使用cordova来打包.在官方文档中,我们可以了解到创建一个app十分简单,你的电脑上有nodejs就行,我 ...

  9. 【2016.3.16】作业 VS2015安装&单元测试(1)

    首先说下本机配置. CPU:Intel Atom x5-z8300 @1.44GHz 内存:2GB 操作系统:Windows10 家庭版 32位 硬盘:32GB 然后开始怒装visual studio ...

  10. Indidual Homework Assignment

    一.Pair work的得与失 合作编程在以前的学习过程中也进行过,基本也就是各人负责一部分最后再将之拼凑起来,而这次作业要求的双人合作,要求的并不是这样,而是两人应该在一起进行工作,这样的要求理想情 ...