题目:

                                      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. bzoj4282 慎二的随机数列 树状数组求LIS + 构造

    首先,我们不难发现N个位置都选一定不会比少选任意几个差,所以我们就先设定我们将这N个修改机会都用上, 那么如果点 i">ii 前有sumv">sumvsumv个可修改点 ...

  2. 常用css和js内容

    1.让一个200x200的div在不同分辨率屏幕上下左右居中. <div class="box"></div> <style type="t ...

  3. 实验一:JAVA实验环境搭建 ,JDK下载与安装及 Eclipse下载与安装

    一.搭建JAVA实验环境 1.JDK的下载 (1)打开 IE 浏览器,输入网址“http://www.oracle.com/index.html”,浏览 Oracle 官方主页.鼠标双击Downloa ...

  4. iframe嵌入的子页面如何刷新父窗口

    iframe中刷新父页面方法及一些按钮刷新代码集合[原创+转]2009-07-23 11:12a页面里iframe了个b页面,我想实现在b页面里一个按钮,一按就刷新a页面,也就是父页面,不是只刷新if ...

  5. 把SQLAlchemy查询对象转换成字典/json使用(分开)

    注:针对的是查询出来的是单条对象 多个对象的话可以使用for循环遍历查询出来的对象列表,也可以使用下面的方法 1.config.py文件 #!/usr/bin/env python #-*- codi ...

  6. Spring框架的理解

    Spring 是一個开源的IOC和AOP容器框架! 具体描述为: 1.轻量级:Spring是非侵入性-基于Spring开发的应用中的对象可以不依赖API开发 2.依赖注入(DI---------dep ...

  7. python 多线程处理框架

    多线程处理框架 python2.7 python3.5 多线程通用任务处理型驱动框架 probe_type 探测类型rtsp或者http task_queue 任务队列 task_handler 任务 ...

  8. java+selenium自动化遇到confirm弹窗,出现NoAlertPresentException: no alert open

    //操作js的confirm弹窗,bool控制是否点击确定,true为点击确定,false为点击取消 public static void OperaterJSOfConfirm(WebDriver ...

  9. 使用angularjs的$http.post异步提交数据时,服务器接收不了的问题

    一,在正常情况下,使用表单的post方法提交数据,默认请求头的Content-Type:application/x-www-form-urlencoded类型, 提交数据格式如下: 二,使用angul ...

  10. Eclipse设置jdk相关

    2.window->preferences->java->Compiler->设置右侧的Compiler compliance level 3.window->prefe ...