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. 使用 Nginx 提升网站访问速度(转)

    Nginx 简介 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Ig ...

  2. Java8系列之重新认识HashMap(转)

    转自美团电瓶技术团队:原文地址 简介 Java为数据结构中的映射定义了一个接口java.util.Map,此接口主要有四个常用的实现类,分别是HashMap.Hashtable.LinkedHashM ...

  3. ubuntu nohup命令用法

    让程序在后台运行 该命令的一般形式nohup command & 程序在后台运行并打印日志 nohup ./china_fund.py > china_fund.file 2>&a ...

  4. TopCoder SRM420 Div1 RedIsGood —— 期望

    题目链接:https://vjudge.net/problem/TopCoder-9915 (论文上的题) 题解: 更正:, i>0, j>0 代码如下: #include <ios ...

  5. 通道(Channel)的原理获取

    通道表示打开到 IO 设备(例如:文件.套接字)的连接.若需要使用 NIO 系统,需要获取用于连接 IO 设备的通道以及用于容纳数据的缓冲区.然后操作缓冲区,对数据进行处理.Channel 负责传输, ...

  6. BZOJ 1041 [HAOI2008]圆上的整点:数学【费马平方和定理】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1041 题意: 给定n(n <= 2*10^9),问你在圆x^2 + y^2 = n^ ...

  7. 搭建LoadRunner中的场景(三)场景的执行计划

    所谓场景操作,包括初始化用户组.启动用户组各用户以及停止虚拟用户的全过程.依据设置不同,执行过程中可以最多有5类操作,分别是启动用户组(start group).初始化(Initialize).启动虚 ...

  8. Elasticsearch: Five Things I was Doing Wrong

    Elasticsearch: Five Things I was Doing Wrong Update: Also check out my series on scaling Elasticsear ...

  9. swoole的http服务

    PHP实现基于Swoole简单的HTTP服务器 引用Swoole官方定义: PHP语言的异步.并行.高性能网络通信框架,使用纯C语言编写,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户 ...

  10. 疑难杂症:SQLServerAgent 当前未运行,因此无法将此操作通知它。

    日志信息:SQLServerAgent 当前未运行,因此无法将此操作通知它. (Microsoft SQL Server,错误: 22022) 确认问题之后,远程到服务器.按照以下步骤  1.打开计算 ...