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. zepto-touch事件

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. 自行制作yum源仓库

    背景 客户服务器为内网机器,centos7系统,且无法与外网连接.需要部署对应的LANMP环境及其它软件 解决思路 1.在阿里云服务器,利用阿里云的yum源仓库,下载对应软件及关联软件. 2.在客户机 ...

  3. 关于canvas绘制图像模糊问题

    前段时间在做项目的裁剪并上传图像功能的时候,发现裁剪后展示的图像比较模糊,之后去百度上搜索了一下,看到有一个解决方案是设置canvas的宽高为css宽高的3倍,使用后感觉效果很好,当时就没管原理接着做 ...

  4. Java——面向对象的特征二:继承性

    2.1面向对象的特征二:继承性 ①引入类继承最基本的作用是:代码重用. ②语法 [修饰符列表] class 子类名 extends 父类名{ 类体; } ③子类继承父类以后,父类中声明的属性.方法,子 ...

  5. Ruby 中文编码

    Ruby 中文编码 前面章节中我们已经学会了如何用 Ruby 输出 "Hello, World!",英文没有问题,但是如果你输出中文字符"你好,世界"就有可能会 ...

  6. 在vue中使用handsontable

    1.使用npm安装 npm install handsontable @handsontable/vue 2.定义结构 <hot-table :settings="hotSetting ...

  7. NX二次开发-创建圆弧(圆心-半径)UF_CURVE_create_arc

    NX9+VS2012 #include <uf.h> #include <uf_curve.h> #include <uf_ui.h> #include <u ...

  8. Vue-cli中使用vConsole,以及设置JS连续点击控制vConsole按钮显隐功能实现

    最近发现了一个鹅厂的仓库,实现起来比我这个方便[捂脸].https://github.com/AlloyTeam/AlloyLever 一.vue-cli脚手架中搭建的项目引入vConsole调试 1 ...

  9. 关于CoreData的一个工具Mogenerator的使用

    最近看到用CoreData时使用的工具Mogenerator,发现网上介绍其具体使用的不多,特此简单整理一下, 关于CoreData这里就不具体说了,使用就用MagicalRecord,用起来真是太方 ...

  10. Go kit 概览

    该篇为翻译文:原文地址 https://github.com/go-kit/kit Go kit 是一个语言工具包,用于在GO 语言中构建微服务.我们可以解决分布式系统和应用程序架构中的常见问题,因此 ...