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

和最大连续子串和类似,可以把二维的转化为一维,将i行到j行的各列数分别相加,求最大连续子串和即可。
同时可利用前缀和,sum[i][j]表示第j列前i行相加的和,i从1开始,则i-j行相加的和为sum[j][k]-sum[i-1][k]
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std; #define INF 0x3f3f3f3f
int a[][];
int t[], dp[];
int sum[][]; int n; int sovle()
{
int ans = -INF;
for (int i = ; i <= n; i++) {
for (int j = i; j <= n; j++) { int Max;
dp[] = sum[j][] - sum[i-][];
for (int k = ; k <= n; k++) {
int temp = sum[j][k]-sum[i-][k];
dp[k] = max(dp[k-]+temp, temp);
}
Max = *max_element(dp+, dp+n+);
if (Max > ans)
ans = Max;
}
}
return ans;
} int main()
{
//freopen("1.txt", "r", stdin);
scanf("%d", &n);
memset(sum, , sizeof(sum));
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++) {
scanf("%d", &a[i][j]);
sum[i][j] = sum[i-][j] + a[i][j];
}
printf("%d\n", sovle()); return ;
}
 

[poj]1050 To the Max dp的更多相关文章

  1. poj 1050 To the Max (简单dp)

    题目链接:http://poj.org/problem?id=1050 #include<cstdio> #include<cstring> #include<iostr ...

  2. POJ 1050 To the Max 最大子矩阵和(二维的最大字段和)

    传送门: http://poj.org/problem?id=1050 To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submi ...

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

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

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

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

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

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

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

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

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

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

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

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

  9. POJ 1050 To the Max 枚举+dp

    大致题意: 求最大子矩阵和 分析: 一开始想复杂了,推出了一个状态方程:d[i][j]=max(d[i][j-1]+-,d[i-1][j]+-).写着写着发现上式省略的部分记录起来很麻烦. 后来发现n ...

随机推荐

  1. 洛谷 2261 [CQOI2007]余数求和

    题目戳这里 一句话题意 求 \(\sum_{i=1}^{n} (k ~~\texttt{mod} ~~i)\) Solution 30分做法: 说实话并不知道怎么办. 60分做法: 很明显直接一遍o( ...

  2. BZOJ2328: [HNOI2011]赛车游戏

    BZOJ2328: [HNOI2011]赛车游戏 Description 题解Here! 一开始被题面那一长串的描述吓到了,一直没敢做... 然后尝试着硬着头皮读懂题面. 然后...这不是贪心么??? ...

  3. gradle 跳过junitTest的方法

    Web项目中不长会写JunitTest,但也会写.gradle build的时候回执行test 这项task.如果想跳过,通常有几种方法: 1.在build.gradle 文件中禁用task test ...

  4. Java基础之I/O流

    一.数据流的基本概念 数据流是一串连续不断的数据的集合,就象水管里的水流,在水管的一端一点一点地供水,而在水管的另一端看到的是一股连续不断的水流.数据写入程序可以是一段.一段地向数据流管道中写入数据, ...

  5. vi中如何替换某字符成“回车”?

    vi中如何替换某字符成“回车”? 在 vi 中::s/,/^M/g (you need to type CTRL-V <CR> to get a ^M here)VIM - Vi IMpr ...

  6. python获取本机IP地址

    方法一 通常使用socket.gethostname()方法即可获取本机IP地址,但有时候获取不到(比如没有正确设置主机名称) import socket #获取计算机名称hostname=socke ...

  7. AnkhSVN

    安装和配置 签入签出问题 1.安装和配置 ①安装.(貌似默认的安装到C:\Program Files\AnkhSVN 2下,开始菜单也没快捷?) ②源代码管理器设置:打开vs2012,工具→选项→源代 ...

  8. CSS那个背景图片的坐标怎么设置?怎么计算的?

    background:url(images/hh.gif) no-repeat -10px 0;},作用是移动背景的位置. 背影图片的左上角相对当前元素左上角的坐标. 右为X轴正半轴, 下为Y轴正半轴 ...

  9. Java_正则_00_资源贴

    二.参考资料 1.揭开正则表达式的神秘面纱

  10. Eclipse_插件_03_反编译插件_Eclipse Class Decompiler

    一.插件优势 此插件比jd-eclipse更加强大,反编译之后不会像jd-eclipse一样出现注释符号. 二.插件下载地址 1.github https://github.com/cnfree/Ec ...