To the Max
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 43241   Accepted: 22934

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

题意是给定一个矩阵,求其子矩阵的最大和。

这题也是弄得相当郁闷,一开始暴力,结果预料之中的TLE。然后试了一下dp,结果还MLE。。。郁闷得不行。

然后看了别人的思路,发现可以二维变一维,想了想忽然恍然大悟。

将每一列的加起来,就是一维了。枚举不同行即可。之前怎么做的这次怎么求。

代码:

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#pragma warning(disable:4996)
using namespace std; int value[250][250];
int value2[250];
int dp[250]; int main()
{
//freopen("input.txt","r",stdin);
//freopen("out.txt","w",stdout); int N,i,j,h,k,g,f;
int ans=-100;
scanf("%d",&N); memset(dp,0,sizeof(dp));
memset(value2,0,sizeof(value2)); for(i=1;i<=N;i++)
{
for(j=1;j<=N;j++)
{
scanf("%d",&value[i][j]);
ans=max(ans,value[i][j]);
}
} for(i=1;i<=N;i++)
{
for(h=i;h<=N;h++)
{
for(k=1;k<=N;k++)
{
value2[k] += value[h][k];
dp[k] = max(dp[k-1]+value2[k],value2[k]);
ans = max(ans,dp[k]);
}
memset(dp,0,sizeof(dp));
}
memset(value2,0,sizeof(value2));
} cout<<ans<<endl;
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1050:To the Max的更多相关文章

  1. (POJ - 1050)To the Max 最大连续子矩阵和

    Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...

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

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

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

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

  4. POJ 1050 To the Max 最详细的解题报告

    题目来源:To the Max 题目大意:给定一个N*N的矩阵,求该矩阵中的某一个矩形,该矩形内各元素之和最大,即最大子矩阵问题. 解题方法:最大子序列之和的扩展 解题步骤: 1.定义一个N*N的矩阵 ...

  5. 九度oj 题目1050:完数

    题目1050:完数 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8778 解决:3612 题目描述: 求1-n内的完数,所谓的完数是这样的数,它的所有因子相加等于它自身,比如6有3个因子 ...

  6. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  7. 页面上有3个输入框:分别为max,min,num;三个按钮:分别为生成,排序,去重;在输入框输入三个数字后,先点击生成按钮,生成一个数组长度为num,值为max到min之间的随机整数点击排序,对当前数组进行排序,点击去重,对当前数组进行去重。 每次点击之后使结果显示在控制台

    <!DOCTYPE html> <html> <head> <!-- 页面上有3个输入框:分别为max,min,num:三个按钮:分别为生成,排序,去重: 在 ...

  8. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

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

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

随机推荐

  1. jQuery加减

    var num1 = 123; var num2=123; var sum = num1+num2; 结果是246; <input type="text" id=" ...

  2. Linux centosVMware 磁盘格式化、磁盘挂载、手动增加swap空间

    一.磁盘格式化 磁盘分区后不能直接使用,需要对每一个分区格式化,格式化其实就是安装系统文件. 命令mke2fs:不支持格式化成xfs系统文件  mkfs.ext4 == mke2fs -t ext4 ...

  3. UOJ Contest #50: Goodbye Jihai

    比赛传送门:Goodbye Jihai. \(\Huge{\mathbf{再见,己亥.\\你好,庚子!\\祝大家新春快乐!}}\) A. 新年的促销 这题如果直接做的话可能方向会想歪,方向对了其实就是 ...

  4. Java面向对象之类、接口、多态

    Java面向对象之类.接口.多态 类 class Person { // 实例属性 int age; String name; // 类属性 static int v = 1; // 构造器 publ ...

  5. Opencv中常见的滤波方法

    滤波(模糊)的概念和作用: 图像滤波增强处理实质上就是运用滤波技术来增强图像的某些空间频率特征,以改善地物目标与领域或背景之间的灰度反差. 遥感系统成像过程中可能产生的”模糊”作用,常使遥感图像上某些 ...

  6. 整合Spring时Service层为什么不做全局包扫描详解

    合Spring时Service层为什么不做全局包扫描详解 一.Spring和SpringMVC的父子容器关系 1.讲问题之前要先明白一个关系 一般来说,我们在整合Spring和SpringMVC这两个 ...

  7. Ubuntu下使用mail命令发送邮件

    Ubuntu下使用mail命令发送邮件 mail命令在Ubuntu下是需要安装的,使用下条命令进行安装: sudo apt-get install heirloom-mailx 接下来输入用户密码,等 ...

  8. MySQL学习之SQL基础(一)DDL

    Sql基础 DDL (data defination language) 创建表 CREATE TABLE emp( ename varchar(10), hiredate date, sal dec ...

  9. CentOS configuration uses the SFTP server

    SFTP,即 SSH 文件传输协议( SSH File Transfer Protocol ),或者说是安全文件传输协议( Secure File Transfer Protocol ).SFTP 是 ...

  10. C# 绘制矩形方框读写内存类 cs1.6人物透视例子

     封装的有问题 其中方框可能在别的方向可能 会显示不出来建议不要下载了 抽时间我会用纯c#写一个例子的  其中绘制方框文字和直线调用的外部dll采用DX11(不吃CUP)绘制我封装成了DLL命名为 S ...