To the Max

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 44723 Accepted: 23679

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

这道题目是hdu1003 的升级版,HDU 1003,是一维数组最长子段和的问题,这个题目扩展到二维,思路就是把二维转换成一维,

先求第一行最大子段和,再求第一行跟第二行合起来的最大子段和,再求第一行到第三行合起来的最大值,实际上就是把二维数组转换成一维的了,

#include <iostream>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h> using namespace std;
int a[105][105];
int n;
int dp[105];
int b[105];
int sum;
int main()
{
while(scanf("%d",&n)!=EOF)
{
sum=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(int i=1;i<=n;i++)
{
memset(b,0,sizeof(b));
memset(dp,0,sizeof(dp));
for(int k=i;k<=n;k++)
{
for(int j=1;j<=n;j++)
{
b[j]+=a[k][j];
if(dp[j-1]>=0)
dp[j]=dp[j-1]+b[j];
else
dp[j]=b[j];
if(sum<dp[j])
sum=dp[j];
}
} }
printf("%d\n",sum); }
return 0;
}

POJ--1050--To the Max(线性动规,最大子矩阵和)的更多相关文章

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

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

  2. HOJ 2156 &POJ 2978 Colored stones(线性动规)

    Colored stones Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1759 Accepted: 829 Descrip ...

  3. POJ-1958 Strange Towers of Hanoi(线性动规)

    Strange Towers of Hanoi Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 2677 Accepted: 17 ...

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

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

  5. POJ-1953 World Cup Noise(线性动规)

    World Cup Noise Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16374 Accepted: 8097 Desc ...

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

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

  7. POJ 1050 To the Max 暴力,基础知识 难度:0

    http://poj.org/problem?id=1050 设sum[i][j]为从(1,1)到(i,j)的矩形中所有数字之和 首先处理出sum[i][j],此时左上角为(x1,y1),右下角为(x ...

  8. POJ 1050 To the Max -- 动态规划

    题目地址:http://poj.org/problem?id=1050 Description Given a two-dimensional array of positive and negati ...

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

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

随机推荐

  1. python登录网易163邮箱,爬取邮件

    from common import MyRequests,LoggerUntil,handle_exception myRequests.update_headers({ 'Accept':'tex ...

  2. SpringMVC由浅入深day01_1springmvc框架介绍

    springmvc 第一天 springmvc的基础知识 课程安排: 第一天:springmvc的基础知识 什么是springmvc? springmvc框架原理(掌握) 前端控制器.处理器映射器.处 ...

  3. 8 -- 深入使用Spring -- 3...1 Resource实现类ClassPathResource

    8.3.1 Resource实现类------ClassPathResource : 访问类加载路径下的资源的实现类 2.访问类加载路径下的资源 ClassPathResource 用来访问类加载路径 ...

  4. UrlUtils工具类,Java URL工具类,Java URL链接工具类

    UrlUtils工具类,Java URL工具类,Java URL链接工具类 >>>>>>>>>>>>>>>&g ...

  5. 【Leaflet】鼠标提取坐标

    map.on('mousemove', function (e) { document.getElementById('info').innerHTML = /* innerHTML 属性设置或返回表 ...

  6. 【转】使程序在Linux下后台运行 (关掉终端继续让程序运行的方法)

    一.为什么要使程序在后台执行 我们计算的程序都是周期很长的,通常要几个小时甚至一个星期.我们用的环境是用putty远程连接到日本Linux服务器.所以使程序在后台跑有以下三个好处: 1:我们这边是否关 ...

  7. CallByValue和CallByName区别

    /** * Created by root * Description :CallByValue:进入函数就得先计算实参的值:CallByName:函数体重使用到的时候才计算 */ object Ca ...

  8. MSF 内网渗透笔记

    进入meterpreter模式 在meterpreter中输入shell即可进入CMD窗口接着即可执行CMD命令,例如打开RDP服务REG ADD HKLM\SYSTEM\CurrentControl ...

  9. [Maven]Maven中的一些基本概念

    Pom文件中的groupId.artifactId.version和name,这三个元素定义了一个项目的基本的坐标,在Maven世界中,任何的jar.pom或者war都是基于这些基本的坐标惊醒区分. ...

  10. MongoDB 备份恢复

    备份: mongodump --host -u admin -p -o /tmp/alldb/ // 备份所有的库 mongodump --host -u admin -p -d mydb -o /t ...