To the Max
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 45906   Accepted: 24276

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

Source

翻译:

总时间限制: 
1000ms

内存限制: 
65536kB
描述
已知矩阵的大小定义为矩阵中所有元素的和。给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵。

比如,如下4 * 4的矩阵

0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2

的最大子矩阵是

9 2
-4 1
-1 8

这个子矩阵的大小是15。

输入
输入是一个N * N的矩阵。输入的第一行给出N (0 < N <= 100)。再后面的若干行中,依次(首先从左到右给出第一行的N个整数,再从左到右给出第二行的N个整数……)给出矩阵中的N2个整数,整数之间由空白字符分隔(空格或者空行)。已知矩阵中整数的范围都在[-127, 127]。
输出
输出最大子矩阵的大小。
样例输入
4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1 8 0 -2
样例输出
15

第一种
/*准备一个数组F[i,j]来存到第i,j格时的矩阵和(类似于前缀和)
对于一个子矩阵[x1,y1,x2,y2]//x1,x2代表左上角和右下角的横坐标
有 S[x1,y1,x2,y2] = f[x2,y2] - f[x1-1,y2] - f[x2,y1-1] + f[x1,y1];*/
#include<cstdio>
#include<iostream>
using namespace std;
#define N 101
int ans=-0x7f,n,a[N][N],f[N][N];
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
scanf("%d",&a[i][j]);
f[i][j]=f[i-][j]+f[i][j-]-f[i-][j-]+a[i][j];
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
for(int k=;k+i<=n;k++)
for(int l=;l+j<=n;l++){
int xx=i+k,yy=j+l;
ans=max(ans,f[xx][yy]-f[i-][yy]-f[xx][j-]+f[i-][j-]);
}
printf("%d\n",ans);
return ;
}
第二种
#include<cstdio>
#include<iostream>
using namespace std;
#define N 101
int a[N][N],n,ans=;
int main(){
scanf("%d",&n);
for(int i=,x;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&x),a[i][j]=a[i-][j]+x;//列前缀和
for(int i=;i<=n;i++)
for(int j=i;j<=n;j++){
int tmp=;
for(int k=;k<=n;k++){
int num=a[j][k]-a[i-][k];
if(tmp>) tmp+=num;
else tmp=num;
ans=max(ans,tmp);
}
}
printf("%d\n",ans);
return ;
}
												

poj 1050 To the Max的更多相关文章

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

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

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

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

  3. 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 ...

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

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

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

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

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

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

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

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

  8. [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)

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

  9. poj 1050 To the Max(最大子矩阵之和,基础DP题)

    To the Max Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 38573Accepted: 20350 Descriptio ...

随机推荐

  1. android 永不关闭toast

    Toast信息提示框之所以在显示一定时间后会自动关闭,是因为在系统中有一个Toast队列;那么有些时候需要这个Toast信息提示框长时间显示,直到需要关闭它时通过代码来控制,而不是让系统自动来关闭To ...

  2. Android Java 自定义异常

    1.自定义异常 package com; public class ZeroException extends Exception { private static final long serial ...

  3. iOS NSNumber转化NSString之description

    我们经常需要把一个数字转成字符串,当你不需要配合其他字符串的时候可以用description. /** description属于NSObject 值是NSNumber时候,不用stringWithF ...

  4. 关于 xib 的使用

    前两天写百度地图的时候要添加 一个标注的泡泡view  但有些复杂所以想用xib 拖拽出一个View ,拖拽出来之后发现添加不到Controller中 ,郁闷!! 终于找到了方法: //先获取NIb ...

  5. Android底部TabHost API

    今天在项目中遇到了底部TabHost,顺便就写了一个底部TabHost的api继承即可使用非常简单,以下为源代码: 首先是自定义的TabHostActivity,如果要使用该TabHost继承该类即可 ...

  6. Erlang数据类型的表示和实现(4)——boxed 对象

    Boxed 对象 Boxed 对象是比较复杂的对象,在 Erlang 中主标签为 10 的 Eterm 表示一个对 boxed 对象的引用.这个 Eterm 除去标签之后剩下的实际上是一个指针,指向具 ...

  7. 管理故事——和尚挑水的故事

    有时候企业.公司的各种混乱都是源于管理问题,例如人浮于事.资源错配.机构臃肿-----,暂且不说企业管理.项目的管理,光是个人工作的管理.一个处理不好,接踵而来的就是一堆问题,可怕的不是出现问题,而是 ...

  8. 线程本地存储TLS(Thread Local Storage)的原理和实现——分类和原理

    原文链接地址:http://www.cppblog.com/Tim/archive/2012/07/04/181018.html 本文为线程本地存储TLS系列之分类和原理. 一.TLS简述和分类 我们 ...

  9. centos7 拨号之后添加路由

    问题:拨号主机再自动拨号(/sbin/ifdown ppp0;/sbin/ifup ppp0)之后,无法上网(没有添加路由) 思路:在拨号程序中添加路由代码 vim /sbin/ifup { slee ...

  10. linux系统下设置oracle开机自动启动

    在Linux系统中,安装好oracle数据库服务后,并不像在Windows系统下一样,oracle服务在默认情况下会随时系统的启动自动启动.Linux系统中,是需要用户去手动进行设置,才能实现orac ...