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. 【题解】Painting Fence

    [题解]Painting Fence 分治模板.贪心加分治.直接\(O(n^2logn)\)分治过去.考虑一块联通的柱形是子问题的,是递归的,贪心分治就可.记得对\(r-l+1\)取\(min\). ...

  2. php微信支付测试开发(流程已通)

    必要条件: appid //公众号后台开发者中心获得(和邮件内的一样)   mchid//邮件内获得  key//商户后台自己设置  appsecret //公众号开发者中心获得 两个证书文件,邮件内 ...

  3. 蓝牙 CTS 测试

    安装蓝牙测试安装包 之后  . 安卓包名字 android-cts-6.0_r19-linux_x86-x86.zip 解压之后 /cts/android-cts/tools/  目录下 运行  ./ ...

  4. Django—工程创建以及models数据库易错点

    Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...

  5. IOS 长姿势---双击Home键

    这不值得大惊小怪,因为按两次Home键后,苹果只是简单第提供了一个历史任务列表,而不是人们以为的当前任务列表——这在苹果网站上已经说得很清楚了.至于为什么苹果没有能力为用户提供一个“任务管理器”,我们 ...

  6. ios点击链接直接跳转到 App Store 指定应用下载页面

    //跳转到应用页面 NSString  *str = [NSString stringWithFormat:@"http://itunes.apple.com/us/app/id%d&quo ...

  7. git功能速查

    http://gitbook.liuhui998.com/index.html git rebase:在本地变基.将本地所有的修改应用到另一个分支上 git merge:在本地合并分支 git bra ...

  8. CSS3实现自定义Checkbox动画

    CSS3实现自定义Checkbox动画是一款CSS3自定义checkbox,而且这款checkbox还带有动画效果,当你选中checkbox的时候,会以动画的方式打上一个大大的勾. 源码下载:http ...

  9. kvm初体验之五:vm连接网络的两种方式:bridge和nat

    1. 在安装vm时指定网络连接方式 1)bridge virt-install --name vm1 --ram=1024 --vcpus=1 --disk path=/vm-images/vm1,s ...

  10. 分享知识-快乐自己:Mybatis缓存机制

    论缓存机制: 1):mybatis 提供了缓存机制减轻数据库压力,提高数据库性能. 2):mybatis 的缓存分为两级:一级缓存.二级缓存 3):一级缓存是SqlSession级别的缓存,缓存的数据 ...