题目:

                                      To the Max
 
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 48507   Accepted: 25662

Description

Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*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. 

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 N^2 integers separated by whitespace (spaces and newlines). These are the N^2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in 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

Output the sum of the maximal sub-rectangle.

Sample Input

4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1 8 0 -2

Sample Output

  15

题意:给你一个N*N的矩阵,求该矩阵的最大子矩阵和。

子矩阵和:该子矩阵中所以元素的和。

解决方法:

  用第一个三维数组dp[k][i][j]存第k行 第i~j 列的和。 比如dp[3][1][5]表示从 "第3行第1列" 到 "第3行第5列" 的和。

  用第二个三维数组sum[k][i][j]表示"dp[1][i][j]"到"dp[k][i][j]"的和。比如sun[4][2][5]表示前4行 所有第2~5列的和。  注意:所有的数据输出从下标1开始。

  然后四层循环统计sum[k2][i][j]-sum[k1][i][j]的最大值。(1<=k1<=k2<=n)

代码:

#include <iostream>
#include <cstring> using namespace std; int mmap[101][101]; //存矩阵
int dp[101][101][101]; //dp[k][i][j]存第k行 第i~j列的和。 比如dp[3][1][5]表示从 "第3行第1列" 到 "第3行第5列" 的和。
int sum[101][101][101]; //sum[k][i][j]表示"dp[1][i][j]"到"dp[k][i][j]"的和。比如sun[4][2][5]表示前4行 所有第2~5列的和。 注意:所有的数据输出从下标1开始。 int n;
int main()
{
while(cin>>n)
{
memset(dp,0,sizeof(dp)); //数组初始化为0
memset(sum,0,sizeof(sum)); //矩阵数据输入
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>mmap[i][j];
}
} //计算dp数组
for(int k=1;k<=n;k++) //k表示第k行
{
for(int j=1;j<=n;j++) //因为从i~j列所以j放在i的外层
{
for(int i=1;i<=j;i++)
{
//统计从i~j列的和
int sum=0;
for(int p=i;p<=j;p++)
sum+=mmap[k][p];
dp[k][i][j]=sum;
} }
} //计算sum数组
for(int j=1;j<=n;j++)
{
for(int i=1;i<=j;i++)
{
for(int k=1;k<=n;k++)
{
//对应每组 i~j 列,前k行 所有的i~j列的元素的和
sum[k][i][j]+=(dp[k][i][j]+sum[k-1][i][j]);
}
}
} int mmax=-1000000; for(int j=1;j<=n;j++)
{
for(int i=1;i<=j;i++)
{
//对应每组i~j列,统计每组 a~b 行的最大的和。
for(int b=1;b<=n;b++)
{
for(int a=1;a<=b;a++)
{
mmax=max(mmax,sum[b][i][j]-sum[a-1][i][j]);
}
}
}
} cout<<mmax<<endl; }
return 0;
}

  

poj1050查找最大子矩阵和的更多相关文章

  1. POJ1050 To the Max 最大子矩阵

    POJ1050 给定一个矩阵,求和最大的子矩阵. 将每一列的值进行累加,枚举起始行和结束行,然后就可以线性优化了 复杂度O(n^3) #include<cstdio> #include&l ...

  2. (线性dp 最大子段和 最大子矩阵和)POJ1050 To the Max

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54338   Accepted: 28752 Desc ...

  3. poj1050最大子矩阵和

    这篇是看了别人的报告写的,就当是屡屡思路好了. 题目大意.给定一个n阶矩阵(方阵),每一个元素中存在一个数字.任务就是求出一个最大的子矩阵使得矩阵元素之间的和是最大的. n=100; 1.矩阵A[m] ...

  4. [POJ1050]To the Max(最大子矩阵,DP)

    题目链接:http://poj.org/problem?id=1050 发现这个题没有写过题解,现在补上吧,思路挺经典的. 思路就是枚举所有的连续的连续的行,比如1 2 3 4 12 23 34 45 ...

  5. POJ1050最大子矩阵面积

    题目:http://poj.org/problem?id=1050 自己用了n^4的像暴搜一样的方法,感到有点奇怪——真的是这样? #include<iostream> #include& ...

  6. 【poj1050】 To the Max

    http://poj.org/problem?id=1050 (题目链接) 题意 求二维最大子矩阵 Solution 数据好像很水,N最大才100,N^4大暴力都可以随便水过. 其实有N^3的做法.枚 ...

  7. POJ1050:To the max

    poj1050:http://poj.org/problem?id=1050 * maximum-subarray 问题的升级版本~ 本题同样是采用DP思想来做,同时有个小技巧处理:就是把二维数组看做 ...

  8. 《剑指Offer》面试题-二维数组中的查找

    题目1384:二维数组中的查找 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7318 解决:1418 题目描述: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到 ...

  9. [POJ1050]To the Max (矩阵,最大连续子序列和)

    数据弱,暴力过 题意 N^N的矩阵,求最大子矩阵和 思路 悬线?不需要.暴力+前缀和过 代码 //poj1050 //n^4暴力 #include<algorithm> #include& ...

随机推荐

  1. centos7常见的操作01 UTC CST

      CentOS系统将UTC时间修改为CST时间方法 2018-10-31 07:51 世界协调时间(Universal Time Coordinated,UTC): GPS 系统中有两种时间区分,一 ...

  2. node——express框架

    express基于Node.js是一个web开发框架,web框架是为了我们开发更方便,更简洁,更高效. 英文网址 中文网址 安装: npm install express --save express ...

  3. JS中增加日期格式化原型函数之prototype

    /** * javascript Date format(js日期格式化) * 对Date的扩展,将 Date 转化为指定格式的String 月(M).日(d).小时(h).分(m).秒(s).季度( ...

  4. 多行文本省略号样式失效丢失,以及控制台显示autoprefixer警告'Autoprefixer applies control comment to whole block, not to next rules.'

    问题现象   使用webpack压缩打包vue项目,遇到一个问题,文本多行显示省略号的关键css语句-webkit-box-orient: vertical;莫名其妙丢失失效了.查阅资料,有不少人提出 ...

  5. BZOJ 3774 最优选择 (最小割+二分图)

    题面传送门 题目大意:给你一个网格图,每个格子都有$a_{ij}$的代价和$b_{ij}$的回报,对于格子$ij$,想获得$b_{ij}$的回报,要么付出$a_{ij}$的代价,要么$ij$周围四联通 ...

  6. 分析Ajax来爬取今日头条街拍美图并保存到MongDB

    前提:.需要安装MongDB 注:因今日投票网页发生变更,如下代码不保证能正常使用 #!/usr/bin/env python #-*- coding: utf-8 -*- import json i ...

  7. yii2-dingtalk 钉钉群机器人

    说明 群机器人是钉钉群的高级扩展功能.群机器人可以将第三方服务的信息聚合到群聊中,实现自动化的信息同步.目前,大部分机器人在添加后,还需要进行Webhook配置,才可正常使用(配置说明详见操作流程中的 ...

  8. Python - def 函数

    1.def 函数 如果我们用代码实现了一个小功能,但想要在程序代码中重复使用,不能在代码中到处粘贴这些代码,因为这样做违反了软件工程中 DRY原则. Python 提供了 函数功能,可以将我们这部分功 ...

  9. 洛谷 P1272 重建道路(树形DP)

    P1272 重建道路 题目描述 一场可怕的地震后,人们用N个牲口棚(1≤N≤150,编号1..N)重建了农夫John的牧场.由于人们没有时间建设多余的道路,所以现在从一个牲口棚到另一个牲口棚的道路是惟 ...

  10. [Beginning SharePoint Designer 2010]Chapter 3 分析SharePoint页面

    本章概要: 1.SharePoint中主要页面类型 2.SharePoint如何组织页面 3.如何编辑母板页 4.SharePoint母板页中的主要内容占位符