A problem that is simple to solve in one dimension is often much more difficult to solve in more than one dimension. Consider satisfying a boolean expression in conjunctive normal form in which each conjunct consists of exactly 3 disjuncts. This problem (3-SAT) is NP-complete. The problem 2-SAT is solved quite efficiently, however. In contrast, some problems belong to the same complexity class regardless of the dimensionality of the problem. Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the subrectangle with the largest sum is referred to as the maximal sub-rectangle. A sub-rectangle is any contiguous sub-array of size 1 × 1 or greater located within the whole array. 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-hand corner:     9    2

                 −4    1

                −1     8         and has the 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 N2 integers separated by white-space (newlines and spaces). These N2 integers make up the array in row-major order (i.e., all numbers on the first row, left-to-right, then all numbers on 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 The output is the sum of the maximal sub-rectangle.

Sample Input

0      −2     −7     0

9       2      −6     2

−4     1      −4      1

−1     8       0     −2

Sample Output

15

题意:从所给的 N × N 的矩阵中选出任意矩阵,使其中的元素和最大

思路:

  枚举i、j,然后对j进行降维压缩,再对降维后的一维数组求最大子序列(利用动态规划)即可。

  (还看到一种写法,但是不是特别理解:枚举行区间,求出每列的和,再用d[i]=max(d[i-1]+sum[i],sum[i])动态规划公式,即可求出最大子矩阵和。)

降维压缩代码:

 #include<iostream>
#include<string.h>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std; int a[][];
int c[];
int dp[];
int main()
{
std::ios::sync_with_stdio(false);
int n;
cin>>n;
for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
{
cin>>a[i][j];
}
}
int ans=-inf;
for (int i=; i<n; i++) //n行
{
memset(c,,sizeof(c));
for (int j=i; j<n; j++) //n行
{
//对i到j行矩阵进行降维操作
for (int k=; k<n; k++) //n列
{
c[k]+=a[j][k];
}
dp[]=c[];//对降维后的数组c[k]进行最大子序列和的动态规划
if (ans<dp[])
{
ans = dp[];
}
for(int k=;k<n;k++)// n列
{
dp[k]=max(dp[k-]+c[k],c[k]);
if (ans<dp[k])
{
ans = dp[k];
}
}
}
}
cout<<ans<<endl;
return ;
}

不是特别理解的代码:

枚举行区间,求出每列的和,再用d[i]=max(d[i-1]+sum[i],sum[i])动态规划公式,即可求出最大子矩阵和。

 #include<iostream>
#define inf 0x3f3f3f3f
using namespace std; int a[][];
int main()
{
std::ios::sync_with_stdio(false);
int n;
cin>>n;
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
cin>>a[i][j];
a[i][j]+=a[i-][j];
}
}//每一行等于加上上面一行的和
int maxx=a[][];
for(int i=; i<=n; i++)
{
for(int j=i+; j<=n; j++)
{
int sum=;
for(int k=;k<=n;k++)
{
sum+=a[j][k]-a[i][k];
if(sum<)
sum=;
if(sum>maxx)
maxx=sum;
}
}
}
cout<<maxx<<endl; return ;
}

UVA-108-Maximum Sum-子矩阵最大和(最大连续子序列的变形)+降维处理+dp的更多相关文章

  1. UVa 108 - Maximum Sum(最大连续子序列)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  2. UVa 108: Maximum Sum

    这道题用暴力解法+动态规划.分析如下: 对于某个1*m的矩阵,即一个数列,求其maximal sub-rectangle,可以通过求最大长连续字串和来求得(这个用到了动态规划). 那么对于n*m的矩阵 ...

  3. UVa 10827 - Maximum sum on a torus

    题目大意:UVa 108 - Maximum Sum的加强版,求最大子矩阵和,不过矩阵是可以循环的,矩阵到结尾时可以循环到开头.开始听纠结的,想着难道要分情况讨论吗?!就去网上搜,看到可以通过补全进行 ...

  4. UVA 10827 Maximum sum on a torus (LA)

    算法入门经典训练指南88页练习 ::这道题只要把原矩阵扩大4倍,那么其跟最大子矩阵的题目就很类似,把二维转化成一维,求最大的序列和,不过这个序列的长度不能超过n. 长度不能超过n? 那这道题又跟hdu ...

  5. UVA 10827 Maximum sum on a torus 最大矩阵和

    题目链接:UVA - 10827 题意描述:给出一个n*n矩阵,把第一行和最后一行粘一起,把第一列和最后一列粘一起,形成一个环面,求出这个环面中最大的矩阵和. 算法分析:首先复制n*n这个矩阵,形成由 ...

  6. 题目1102:最小面积子矩阵(暴力求解&最大连续子序列)

    题目链接:http://ac.jobdu.com/problem.php?pid=1102 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  7. 最大子矩阵和 URAL 1146 Maximum Sum

    题目传送门 /* 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 好像之前做过,没印象了,看来做过的题目要经常看看:) */ #include <cstdio> ...

  8. URAL 1146 Maximum Sum(最大子矩阵的和 DP)

    Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...

  9. [Swift]LeetCode689. 三个无重叠子数组的最大和 | Maximum Sum of 3 Non-Overlapping Subarrays

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

随机推荐

  1. mysql 学习之1 mysql的基本语法

    转载一位csdn中 乍得12138前辈的 转载:https://blog.csdn.net/qq_26200347/article/details/79781882

  2. CTF学习路线指南(附刷题练习网址)

    PWN,Reverse:偏重对汇编,逆向的理解: Gypto:偏重对数学,算法的深入学习: Web:偏重对技巧沉淀,快速搜索能力的挑战: Mic:则更为复杂,所有与计算机安全挑战有关的都算在其中 常规 ...

  3. Thymeleaf语法总结

    Thymeleaf是Spring boot推荐使用的模板引擎. 一.th属性 html有的属性,Thymeleaf基本都有,而常用的属性大概有七八个.其中th属性执行的优先级从1~8,数字越低优先级越 ...

  4. thinkphp环境要求

    框架本身没有什么特别模块要求,具体的应用系统运行环境要求视开发所涉及的模块.ThinkPHP底层运行的内存消耗极低,而本身的文件大小也是轻量级的,因此不会出现空间和内存占用的瓶颈. PHP版本要求 P ...

  5. docker快速安装elasticsearch

    一.选择版本,拉取镜像 docker pull elasticsearch:5.6.9 #不选择版本就是最新的 二.运行设置容器 # -d 表示在后台运行 docker run -d -p 9200: ...

  6. NX11.0和VS2013 创建NXOpen 开发模版失败解决方案【转载】

    转载自PLM之家论坛 NX11.0和VS2013 创建NXOpen 开发模版失败解决方案 首先我觉得这个可能是西门子疏忽,基本上每个大版本没有补丁前都有类似问题,下面来说说怎么解决吧.注意这里版本,N ...

  7. AdaBoost笔记之通俗易懂原理介绍

    转自:https://blog.csdn.net/px_528/article/details/72963977 写在前面 说到Adaboost,公式与代码网上到处都有,<统计学习方法>里 ...

  8. day5:函数练习题

    1.写函数,检查获取传入列表或者元祖的对象的所有奇数位索引的元素,并将作为新的列表返回给调用者 #解1: def lis(x): lis_1 = [] for i in range(len(x)): ...

  9. CSS3新属性之---flex box布局实例

    flex box布局实例 flex的强大之处在于不管什么布局,几行命令即可实现 /*本节模板div元素(代表骰子的一个面)是Flex容器,span元素(代表一个点)是Flex项目.如果有多个项目,就要 ...

  10. ps-手捧城堡滴水云雾图

    1打开背景图 置入第二张图片 栅格化-加入蒙版-渐变 置入第三张图片 栅格化-用快速选择工具选取-加入蒙版 置入第四张图片 栅格化-调整图层-点击城堡建立蒙版-点击手的蒙版 ctrl-点击城堡的蒙版- ...