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. vue 项目上传到码云,push时error: failed to push some refs to 'https://gitee.com/mawenrou/vue_ht.git'

    vue 项目上传到码云,push时error: failed to push some refs to 'https://gitee.com/mawenrou/vue_ht.git' 因为之前已经创建 ...

  2. 1-7SpringBoot之表单验证@Valid

    SpringBoot提供了强大的表单验证功能实现,给我们省去了写验证的麻烦: 这里我们给下实例,提交一个有姓名和年龄的表单添加功能, 要求姓名不能为空,年龄必须是不小于18 : 我们先新建一个Stud ...

  3. php 打印格式化显示利器 <pre>

    当我们PHP调试的时候,用var_dump 或 print_r打印json数据或array数组时,html页面没有换行显示,看到的内容一大堆,不好定位. 输出前添加 <pre>,便可以自动 ...

  4. 白手起家Django项目发布中篇_Centos下Python2和3并存环境部署

    python环境部署 我们今天学习的内容是如何将Django项目部署到linux服务器上,我们部署的linux系统是centos7首先,我们先在linux上搭建我们的Python3环境: 在这里首先强 ...

  5. arm linux 移植 python3.6

    背景: 人生苦短,我用Python. 说明: 编译Python的嵌入式版需要解释器解析setup.py从而编译Python的模块,因此需要先编译出host的解释器.(有点像Go语言) Python : ...

  6. Java数组去重的方法

    //第一种方式:最开始想到的是利用Set集合的不可重复性进行元素过滤 public static Object[] oneClear(Object[] arr){  Set set = new Has ...

  7. vs2010编译C++ 静态成员函数的引用

    // CTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using names ...

  8. 我的Grunt之旅-初识gruntfile文件

    时间:2018-03-06 18:23  事件:配置 gruntfile.js文件 首先,回忆一下之前的点,grunt项目下面必须有两个文件  ,第一个  package.json ,第二个  Gru ...

  9. jsp获取web的跟路径

    我实现了一个listener,此listener在tomcat启动的时候读取项目的配置文件,配置文件是xml.但是读取解析的功能是在另一个工具类实现的. 这个普通的工具类B,如何获取到当前web的跟路 ...

  10. java随记 2月16

    1.a=a+b 等于 a+=b ,且a+=b隐含强制类型转换 2.^ 表示异或   两个二进制同号为假,异号为真    即 0^0=0,1^1=0,0^1=1 3.三元运算    布尔表达式 ?表达式 ...