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. Atitit. Atiposter 发帖机 新特性 poster new feature   v7 q39

    Atitit. Atiposter 发帖机 新特性 poster new feature   v7 q39 V1  初步实现sina csdn cnblogs V2  实现qzone sohu 的发帖 ...

  2. xCode删除storyboard,新建window并启动

    application:didFinishLaunchingWithOptions该函数是应用程序启动之后首次加载页面的函数,删除storyboard之后,需要在这里new出新的window,初始化, ...

  3. C语言中的指针和内存泄漏

    引言 对于任何使用C语言的人,如果问他们C语言的最大烦恼是什么,其中许多人可能会回答说是指针和内存泄漏.这些的确是消耗了开发人员大多数调试时间的事项.指针和内存泄漏对某些开发人员来说似乎令人畏惧,但是 ...

  4. 求当前时间100天后的时间日期,格式化为xxxx年xx月xx日

    package com.demo1; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Da ...

  5. ubuntu15.10 给解压版的eclipse安装桌面快捷方式

    在桌面用vi 建立eclipse.desktop文件,并赋予权限 sudo chmod  u+x  /home/liujl/Desktop/eclipse.desktop [Desktop Entry ...

  6. Erlang 的新数据结构 map 浅析

    更新:文中示例代码直接从Joe的新版 Erlang 书中摘抄而来,其中模式匹配的代码有错误,现已纠正.应该用 := 匹配字段,而不是 => . 即将发布的 Erlang 17 最大变化之一包括新 ...

  7. 编写一个Java程序,计算半径为3.0的圆周长和面积并输出结果。把圆周率π定义为常量,半径定义为变量,然后进行计算并输出结果。

  8. html标题_段落_换行_水平线_特殊字符

    标题 <h1>一级标题</h1> <h2 align="对齐方式">二级标题</h2> 对齐方式有left,center,right ...

  9. ELK 信息统计分析-1

    Aggregations 格式如下: "aggregations"{ //可以简写为aggs "<aggregation_name>":{ //名称 ...

  10. Web Storage API : LocalStroage

    这是一篇详细介绍详细介绍详细介绍_(:з」∠)_ 背景: 当你访问一个页面,并不是丢到服务器,等待用户访问就可以了的.从输入网址到显示网页的全过程,可以参考这里 简单来说,在输入url按下回车键后,首 ...