题目:

                                      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. mysql-创建用户报错ERROR 1396 (HY000): Operation CREATE USER failed for 'XXXX'@'XXXX'(转载)

    创建用户: create user ‘test’@’%’ identified by ‘test’; 显示ERROR 1396 (HY000): Operation CREATE USER faile ...

  2. java 常用API 包装 练习

    package com.oracel.demo01; import java.util.Random; public class Swzy { public static void main(Stri ...

  3. HDU1846 - Brave Game【巴什博弈】

    十年前读大学的时候,中国每年都要从国外引进一些电影大片,其中有一部电影就叫<勇敢者的游戏>(英文名称:Zathura),一直到现在,我依然对于电影中的部分电脑特技印象深刻.  今天,大家选 ...

  4. UVA10340 - All in All(紫书习题3.9)

    输入两个字符串s和t,判断是否可以从t中删除0个或者多个字符(其他字符顺序不变),得到字符串s.例如,abcde可以得到bce,但无法得到cb. Input 输入多组数据 每组一行包含两个字符串s和t ...

  5. 在windows环境中关于 pycharm配置 anaconda 虚拟环境

    因为要在windows系统系统中练习tensorflow,所以需要配置一下环境(来回的开关机切换环境太麻烦了......) 首先安装anaconda3,我选择的版本是Anaconda3 5.1.0,对 ...

  6. Python IO编程-读写文件

    1.1给出规格化得地址字符串,这些字符串是经过转义的能直接在代码里使用的字符串 需要导入os模块 import os >>>os.path.join('user','bin','sp ...

  7. FactoryBean简介

    网上看了很多关于FactoryBean和BeanFactory的介绍,总感觉说的不够简单.直白,今天用自己的语言来描述下,如果有不对的地方,还请大家指正. 1. FactoryBean和BeanFac ...

  8. 洛谷—— P3119 [USACO15JAN]草鉴定Grass Cownoisseur || BZOJ——T 3887: [Usaco2015 Jan]Grass Cownoisseur

    http://www.lydsy.com/JudgeOnline/problem.php?id=3887|| https://www.luogu.org/problem/show?pid=3119 D ...

  9. spring boot系列--spring security (基于数据库)登录和权限控制

    先说一下AuthConfig.java Spring Security的主要配置文件之一 AuthConfig 1 @Configuration 2 @EnableWebSecurity 3 publ ...

  10. exFAT格式

    买了一个64GB的T卡,发如今Windows XP上格式化."文件系统"仅仅有exFAT选项. 用这个exFAT格式化还失败了. 给XP打上KB955704补丁,能够用exFAT格 ...