传送门:

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. UGUI——重写Image类实现进度条

    目的: 游戏中经常会用到进度条,但是美术给的图片用filled一拉伸就很难看,如下图 第一种模式是九宫格模式,第二种是filled.而我们需要的是两种可结合的. 如何实现: 新建一个类,继承image ...

  2. MIME格式说明,电子邮件格式(转载)

    邮件格式说明 Mutiple Internet Mail Extensions Refer to Internet Official Protocol Standards RFC 822 1 概述 网 ...

  3. springcloud 实战 网关zuul使用中遇到的相关问题

    springcloud 实战  网关zuul使用中遇到的相关问题 1.网关zuul使用时,跨域问题在网关中配置pre过滤器: response.setHeader("Access-Contr ...

  4. python保存字典和读取字典pickle

    import pickle import numpy as np def save_obj(obj, name): with open(name + '.pkl', 'wb') as f: pickl ...

  5. 【python基础】之str类字符串

    str类字符串是不可变对象 1.创建字符串 s1 = str() #创建一个空字符串 s2 = str("hello") #创建字符串"hello" 2.处理字 ...

  6. MySQL数据库(11)----使用子查询实现多表查询

    子查询指的是用括号括起来,并嵌入另一条语句里的那条 SELECT 语句.下面有一个示例,它实现的是找出与考试类别('T')相对应的所有考试事件行的 ID,然后利用它们来查找那些考试的成绩: SELEC ...

  7. Creating dynamic/configurable parameterized queries in Entity Framework

    https://dillieodigital.wordpress.com/2013/05/09/creating-dynamicconfigurable-parameterized-queries-i ...

  8. (转)informatica 面试题大全

    1 What is the difference between a data warehouse and a data mart? Ø Dataware house: It is a collect ...

  9. 如何清除SQL 的登录名

    复制路径 找到SqlStudio.bin 文件删除即可 SQL Server 2016 版本: C:\Users\%username%\AppData\Roaming\Microsoft\SQL Se ...

  10. [翻译] CHAnimation

    CHAnimation https://github.com/cyndibaby905/CHAnimation How it looks CHAnimation is a project used t ...