【 HDU1081 】 To The Max (最大子矩阵和)
题意
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 (最大子矩阵和)的更多相关文章
- poj 1050 To the Max(最大子矩阵之和,基础DP题)
To the Max Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 38573Accepted: 20350 Descriptio ...
- 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 ...
- POJ 1050 To the Max 最大子矩阵和(二维的最大字段和)
传送门: http://poj.org/problem?id=1050 To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- POJ 1050 To the Max (最大子矩阵和)
题目链接 题意:给定N*N的矩阵,求该矩阵中和最大的子矩阵的和. 题解:把二维转化成一维,算下就好了. #include <cstdio> #include <cstring> ...
- POJ1050 To the Max 最大子矩阵
POJ1050 给定一个矩阵,求和最大的子矩阵. 将每一列的值进行累加,枚举起始行和结束行,然后就可以线性优化了 复杂度O(n^3) #include<cstdio> #include&l ...
- HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- hdu1081 To the Max
直接暴力枚举所有子矩形至少需要O(n^4)的复杂度,显然这不是一个合理的解决方法. 上述方案忽略了矩形之间的联系,进行了过多不必要的计算. 实际上如果固定矩形的左右边界,则底边在i行的矩形内数值之和与 ...
- poj 1050 To the Max 最大子矩阵和 经典dp
To the Max Description Given a two-dimensional array of positive and negative integers, a sub-rect ...
- poj 1050 To the Max(最大子矩阵之和)
http://poj.org/problem?id=1050 我们已经知道求最大子段和的dp算法 参考here 也可参考编程之美有关最大子矩阵和部分. 然后将这个扩大到二维就是这道题.顺便说一下,有 ...
- DP:0
小故事: A * "1+1+1+1+1+1+1+1 =?" * A : "上面等式的值是多少" B : *计算* "8!" A *在上面等式 ...
随机推荐
- Vue与Element走过的坑。。。。带上Axios
1.Axios中post传参数组(java后端接收数组) 虽然源数据本身就是数组,但是传参时会自动变成key:数值或者服务器无法接收的对象,如下 如果不仔细看,很容易认为这两种情况没毛病..(后端不背 ...
- Ionic Android项目Splash设置
ionic项目中,在splashscreen消失后会出现零点几秒的白屏,再出现app页面. 1. 安装Cordova splash screen插件 ionic plugin add org.apac ...
- Springboot 2.0.4 整合Mybatis出现异常Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
在使用Springboot 2.0.4 整合Mybatis的时候出现异常Property 'sqlSessionFactory' or 'sqlSessionTemplate' are require ...
- assert_param函数的用法
在STM32的固件库和提供的例程中,到处都可以见到assert_param()的使用.如果打开任何一个例程中的stm32f10x_conf.h文件,就可以看到实际上assert_param是一个宏定义 ...
- Nginx Windows版的服务安装和管理工具
以前研究过负载均衡,最近正在项目上实施(从来没做过小项目以上级别的东西,哈),nginx挺好,不过Windows有点为难,小流量和本地不追求性能,简单易用是目标. Nginx Windows上并没有提 ...
- .net core实践系列之短信服务-为什么选择.net core(开篇)
前言 从今天我将会写.net core实战系列,以我最近完成的短信服务作为例子.该系列将会尽量以最短的时间全部发布出来.源码也将优先开源出来给大家. 源码地址:https://github.com/S ...
- Js基础---红宝书读书日记(1)-------基本类型和引用类型
JS的变量可能包含两种不同数据类型的值,基本类型和引用类型; 基本类型是指简单的数据段,引用类型是指可能由多个值构成的对象; JS高级程序设计第三章介绍了变量分为 5种简单数据类型(string/nu ...
- CEPH Object Gateway
参考文档: CEPH OBJECT GATEWAY:http://docs.ceph.com/docs/master/radosgw/ 一.环境准备 1. Ceph Object Gateway框架 ...
- Linux运维笔记-日常操作命令总结(2)
回想起来,从事linux运维工作已近5年之久了,日常工作中会用到很多常规命令,之前简单罗列了一些命令:http://www.cnblogs.com/kevingrace/p/5985486.html今 ...
- 12.10 Daily Scrum
各种大作业,进度会放缓一些. Today's Task Tomorrow's Task 丁辛 完善餐厅列表,显示距离. 实现和菜谱相关的餐厅列表. 邓亚梅 ...