传送门:

http://poj.org/problem?id=1050

To the Max
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 52306   Accepted: 27646

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

Source

 
分析:
给你一个N*N的数字矩阵
问你子矩阵的最大和是多少
非常的类似最大子段和问题
dp[i][j]:表示从第i列到j列的子矩阵的最大和
那么在第i列到第j列中
第一行的和就看成一个一个数
第二行的和也是看成一个数
第n行的和也是看成一个数
在这些一维线性的数里面找最大的子段和
比如样例:
第列到第四列
(-2-7+0)=-9
(2-6+2)=-4
(1-4+1)=-2
(8+0-2)=6
在-9,-4,-2,6里面找最大的子段和,看出来是6
那么6就是2列到4列的最大子矩阵和
i列:从1到n
j列:从i到n
code:
#include <iostream>
#include <cstdio>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<memory>
#include<queue>
#include<vector>
using namespace std;
#define max_v 105
#define INF 99999999
int dp1[max_v][max_v];//起始i列 终止j列的max
int dp2[max_v];//最大子段和的dp,代表以第i个数结尾的最大子合和值
int a[max_v][max_v];
int f(int j1,int j2,int i)//j1列到j2列,i行上数字的和
{
int sum=;
for(int j=j1;j<=j2;j++)
{
sum+=a[i][j];
}
return sum;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&a[i][j]);
memset(dp1,,sizeof(dp1));
dp1[][]=a[][];
int result=-INF;
for(int i=;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
memset(dp2,,sizeof(dp2));
dp2[]=f(i,j,);
int maxv=dp2[];
for(int k=;k<=n;k++)
{
int x=;
if(dp2[k-]>)
x=dp2[k-];
dp2[k]=x+f(i,j,k);
maxv=max(maxv,dp2[k]);
}
dp1[i][j]=maxv;
result=max(result,dp1[i][j]);
}
}
printf("%d\n",result);
}
return ;
}

POJ 1050 To the Max 最大子矩阵和(二维的最大字段和)的更多相关文章

  1. poj 1050 To the Max(最大子矩阵之和)

    http://poj.org/problem?id=1050 我们已经知道求最大子段和的dp算法 参考here  也可参考编程之美有关最大子矩阵和部分. 然后将这个扩大到二维就是这道题.顺便说一下,有 ...

  2. POJ 1050 To the Max (最大子矩阵和)

    题目链接 题意:给定N*N的矩阵,求该矩阵中和最大的子矩阵的和. 题解:把二维转化成一维,算下就好了. #include <cstdio> #include <cstring> ...

  3. poj 1050 To the Max(最大子矩阵之和,基础DP题)

    To the Max Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 38573Accepted: 20350 Descriptio ...

  4. poj 1050 To the Max 最大子矩阵和 经典dp

    To the Max   Description Given a two-dimensional array of positive and negative integers, a sub-rect ...

  5. poj 1050 To the Max(线性dp)

    题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...

  6. [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

  7. poj - 1050 - To the Max(dp)

    题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 ...

  8. POJ 1050 To the Max 二维最大子段和

    To the MaxTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 52281 Accepted: 27633Description ...

  9. [poj]1050 To the Max dp

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

随机推荐

  1. Oracle数据库基本操作(三) —— DQL相关内容说明及应用

    本文所使用的查询表来源于oracle数据中scott用户中的emp员工表和dept部门表. 一.基本语法 SQL语句的编写顺序: select 输出的列 from 表名 where 条件 group ...

  2. Oracle数据库采用数据泵方式导入导出数据

    特别说明:Oralce的数据泵导入导出技术只能用在数据库服务器上,在只有客户端的机器上是无法使用数据泵技术的. 1.创建备份文件目录  mkdir d:\dmp 2.在Oralce中注册该目录,将目录 ...

  3. SQLHappy微软数据库连接查询操作,对数据的处理和查询

    (软件已更新,部分介绍与新版软件有出处) 1.服务连接界面介绍 2.主界面介绍 3.表搜索介绍 4.命令菜单部分介绍 5.插件介绍 6.帮助菜单介绍 7.数据库列表右键菜单 8.数据库结构和数据操作( ...

  4. 浅谈ul布局以及table布局

    我个人对于某些言论说要注重html语义化在布局中的应用,我反而不怎么感冒,试试兼容IE7&&项目期相对较赶的情况下,我还是推荐快速开发为主,兼容性强为主. 如果布局中需要用户边框,推荐 ...

  5. bootstrap学习笔记细化(表格)

    主要属性: 全屏宽度带水平线的表格 .table 带边框的表格 .table-bordered 条纹状表格 .table-striped 悬停变色表格 .table-hover 紧凑风格表格 .tab ...

  6. php中http_build_query函数

    http_build_query ( array $formdata [, string $numeric_prefix ] ) 使用给出的关联(或下标)数组生成一个经过 URL-encode 的请求 ...

  7. 养兔子Fibo函数优化

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. maven(16)-灵活的环境构建

     多个环境 一个项目,在家的时候可能会在本机上运行,在公司可能在内网测试环境运行,上线后会在生产环境运行,在不同的环境中会有一些配置是不一样的,至少数据库就不一样.如果每换一个环境就去改所有配置太 ...

  9. js时间戳转换日期

    //js时间戳转换日期function formatDate(now) { var year=now.getFullYear(); var month=now.getMonth()+1; var da ...

  10. 了解 Azure VM 的系统重启

    有时 Azure 虚拟机 (VM) 可能重启,即使没有明显原因,也没有证据表明用户发起重启操作. 本文列出了可导致 VM 重启的操作和事件,并针对如何避免意外重启问题或减少该问题影响提供见解. 配置 ...