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. jq-demo-拖拽

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

  2. wpf tabcontrol内的datagrid的selectionChanged事件向往传递问题

    tabcontrol 内的一个tabitem里是datagrid 当程序相应datagrid的selectionchanged事件后会向上传递到tabcontrol的selectionchanged事 ...

  3. vue+elementui 中 @keyup 键盘上下左右移动聚焦

    <template> <el-table :data="CreditUnclearOutlineList" border style="width: 1 ...

  4. 解析Spring MVC上传文件

    新建一个普通的maven工程 在pom.xml文件中引入相应的坐标 <?xml version="1.0" encoding="UTF-8"?> & ...

  5. 区别 |mysql |Timestamp、time、datetime

    Timestamp 时间格式为 类似 2012-11-11 12:23:00 ,默认值为当前时间 time 时间格式类似12:23:00 默认值为null datetime 时间格式类似2012-11 ...

  6. thinkphp 储存驱动

    存储驱动完成了不同环境下面的文件存取操作,也是ThinkPHP支持分布式和云平台的基础. 默认的存储驱命名空间位于Think\Storage\Driver,每个存储驱动必须继承Think\Storag ...

  7. NX二次开发-UFUN工程图初始化视图信息UF_DRAW_initialize_view_info

    NX9+VS2012 #include <uf.h> #include <uf_draw.h> #include <uf_obj.h> #include <u ...

  8. favicon.ico请求处理

    favicon.ico 图标用于收藏夹图标和浏览器标签上的显示,如果不设置,浏览器会请求网站根目录的这个图标,如果网站根目录也没有这图标会产生 404. 出于优化的考虑,要么就有这个图标,要么就禁止产 ...

  9. 基于Netty的RPC架构学习笔记(七):netty学习之心跳

    文章目录 idleStateHandler netty3

  10. 4、postman的常见断言

    推荐我的另一篇文章  浅谈JSONObject解析JSON数据,这篇文章原理类似,使用java或者beanshell进行断言解析json数据 介绍断言之前,我们先测试1个接口: 接口地址:https: ...