【 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 *在上面等式 ...
随机推荐
- BZOJ4860 BJOI2017 树的难题 点分治、线段树合并
传送门 只会线段树……关于单调队列的解法可以去看“重建计划”一题. 看到路径长度$\in [L,R]$考虑点分治.可以知道,在当前分治中心向其他点的路径中,始边(也就是分治中心到对应子树的根的那一条边 ...
- [Spark][Python]Mapping Single Rows to Multiple Pairs
Mapping Single Rows to Multiple Pairs目的: 把如下的这种数据, Input Data 00001 sku010:sku933:sku02200002 sku912 ...
- 使用 cron 定时任务实现 war 自动化发布
autoRelease.sh #!/bin/sh /home/tomcat/bin/shutdown.sh echo "tomcat stoped" cd /home/tomcat ...
- .Net core使用EF Core Migration做数据库升级
---恢复内容开始--- (1)VS Code下创建含有授权功能的并且使用localdb作为数据库的命令 dotnet new -au individual -uld --name identityS ...
- [译]通往 Java 函数式编程的捷径
原文地址:An easier path to functional programming in Java 原文作者:Venkat Subramaniam 译文出自:掘金翻译计划 以声明式的思想在你的 ...
- TDD、BDD、ATDD、DDD 软件开发模式
TDD.BDD.ATDD.DDD 软件开发模式 四个开发模式意思: TDD:测试驱动开发(Test-Driven Development) BDD:行为驱动开发(Behavior Driven Dev ...
- Terraform:简介
在 DevOps 实践中,基础设施即代码如何落地是一个绕不开的话题.像 Chef,Puppet 等成熟的配置管理工具,都能够满足一定程度的需求,但有没有更友好的工具能够满足我们绝大多数的需求?笔者认为 ...
- 记一次拿webshell踩过的坑(如何用PHP编写一个不包含数字和字母的后门)
0x01 前言 最近在做代码审计的工作中遇到了一个难题,题目描述如下: <?php include 'flag.php'; if(isset($_GET['code'])){ $code = $ ...
- NEWBE CRALWER 产品需求文档
1.产品概述 本产品是学霸软件系统的爬虫部分,由NEWBE团队负责.主要任务是从网上爬取出相关数据后提供给C705组使用. 2.产品的发展经历 2.1 产品的发展经历 本产品从2014.10.29开始 ...
- 《Linux内核设计与实现》第四章读书笔记
4.1 多任务 多任务操作系统就是能同时并发地交互执行多个进程的操作系统. 多任务系统可以划分为两类: 非抢占式多任务进程会一直执行直到自己主动停止运行 抢占式多任务Linux/Unix使用的是抢占式 ...