UVA-108-Maximum Sum-子矩阵最大和(最大连续子序列的变形)+降维处理+dp
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的更多相关文章
- UVa 108 - Maximum Sum(最大连续子序列)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- UVa 108: Maximum Sum
这道题用暴力解法+动态规划.分析如下: 对于某个1*m的矩阵,即一个数列,求其maximal sub-rectangle,可以通过求最大长连续字串和来求得(这个用到了动态规划). 那么对于n*m的矩阵 ...
- UVa 10827 - Maximum sum on a torus
题目大意:UVa 108 - Maximum Sum的加强版,求最大子矩阵和,不过矩阵是可以循环的,矩阵到结尾时可以循环到开头.开始听纠结的,想着难道要分情况讨论吗?!就去网上搜,看到可以通过补全进行 ...
- UVA 10827 Maximum sum on a torus (LA)
算法入门经典训练指南88页练习 ::这道题只要把原矩阵扩大4倍,那么其跟最大子矩阵的题目就很类似,把二维转化成一维,求最大的序列和,不过这个序列的长度不能超过n. 长度不能超过n? 那这道题又跟hdu ...
- UVA 10827 Maximum sum on a torus 最大矩阵和
题目链接:UVA - 10827 题意描述:给出一个n*n矩阵,把第一行和最后一行粘一起,把第一列和最后一列粘一起,形成一个环面,求出这个环面中最大的矩阵和. 算法分析:首先复制n*n这个矩阵,形成由 ...
- 题目1102:最小面积子矩阵(暴力求解&最大连续子序列)
题目链接:http://ac.jobdu.com/problem.php?pid=1102 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- 最大子矩阵和 URAL 1146 Maximum Sum
题目传送门 /* 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 好像之前做过,没印象了,看来做过的题目要经常看看:) */ #include <cstdio> ...
- URAL 1146 Maximum Sum(最大子矩阵的和 DP)
Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...
- [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 ...
随机推荐
- python_django_urls模块与views模块请求访问过程
diango接收到web请求后的在urls模块与views模块进行的过程操作: 匹配过程: urls拿到网址,在项目级urls中匹配,若在urlpatterns中存在,则跳转到应用级urls中匹配,若 ...
- leetcode-第11场双周赛-5089-安排会议日程
题目描述: 自己的提交: class Solution: def minAvailableDuration(self, slots1: List[List[int]], slots2: List[Li ...
- vue中使用router全局守卫实现页面拦截
一.背景 在vue项目中使用vue-router做页面跳转时,路由的方式有两种,一种是静态路由,另一种是动态路由.而要实现对路由的控制需要使用vuex和router全局守卫进行判断拦截(安全问题文章最 ...
- faster-rcnn算法总结
faster-rcnn的整体流程比较复杂,尤其是数据的预处理部分,流程比较繁琐.我写faster-rcnn系列文章的目的是对该算法的原始版本有个整体的把握,如果需要使用该算法做一些具体的任务,推荐使用 ...
- Python3 From Zero——{最初的意识:003~数字、日期、时间}
一.对数值进行取整:round(value,ndigits) >>> round(15.5,-1) #可以取负数 20.0 >>> round(15.5,0) #当 ...
- 使用nginx访问本地电脑的目录文件
cat /usr/local/opt/nginx/ //nginx路径 cd /usr/local/opt/nginx/html //localhost的指向 ln -s ~/Documents do ...
- SparkStreaming整合Flume的pull方式之启动报错解决方案
Flume配置文件: simple-agent.sources = netcat-source simple-agent.sinks = spark-sink simple-agent.channel ...
- 《转》python对象
http://www.cnblogs.com/BeginMan/p/3160044.html 一.学习目录 1.pyhton对象 2.python类型 3.类型操作符与内建函数 4.类型工厂函数 5. ...
- extern const 不能一起用
转载至:https://www.cnblogs.com/herenzhiming/articles/5442893.html 常变量在定义的时候必须初始化,所以当你在a.cpp中定义extern co ...
- 2018-8-10-使用-IncrediBuild-提升-VisualStudio-编译速度
title author date CreateTime categories 使用 IncrediBuild 提升 VisualStudio 编译速度 lindexi 2018-08-10 19:1 ...