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. 扩展SharePoint链接字段

    默认SharePoint中的链接字段有很多限制,例如 输入文字的时候只能录入255个字符 链接显示的是文字 点击链接后只能在当前页面打开链接 - - - - - - -- - - - - - -   ...

  2. 用win下的快捷键提高工作效率

    常用的快捷键: WIN+D:显示桌面,再按一次还原桌面: WIN+R:打开运行,输入命令可以执行相应操作,输入路径可以打开对应路径,输入程序名称可以打开对应程序(前提是你打开的是windows下面的程 ...

  3. SSH整合简述一

    1.web.xml中配置 struts2过滤器 <filter> <filter-name>struts2</filter-name> <filter-cla ...

  4. runtime之消息转发

    前言 在上一篇文章中我们初尝了runtime的黑魔法,可以在程序编译阶段就获取到成员变量的名字,特性以及动态的给对象增加属性等等,在接下来中我们进一步了解OC的消息发送机制.如果之前没接触过runti ...

  5. iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载+使用输出流代替文件句柄

    前言:本篇讲解,在前篇iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载的基础上,使用输出流代替文件句柄实现大文件断点续传.    在实际开发中,输入输出流用的比较少,但 ...

  6. iOS开发之网络编程--小文件下载

    文件下载方式: 如果下载的文件比较小,下载方式: 直接用NSData的 +(id)dataWithContentsOfURL:(NSURL*)url; 利用NSURLConnection发送一个HTT ...

  7. Eclipse下快速打开本地文件的插件easy explore

    插件下载地址:http://jianguoyun.com/p/DeNpa8IQx5jkBRjKlAk 放到eclipse的plugin目录下后,eclipse 3.5+可以放到dropins目录下,重 ...

  8. 最近开始研究PMD(一款采用BSD协议发布的Java程序代码检查工具)

    PMD是一款采用BSD协议发布的Java程序代码检查工具.该工具可以做到检查Java代码中是否含有未使用的变量.是否含有空的抓取块.是否含有不必要的对象等.该软件功能强大,扫描效率高,是Java程序员 ...

  9. python基础入门

    Python简介 python是吉多·范罗苏姆发明的一种面向对象的脚本语言,可能有些人不知道面向对象和脚本具体是什么意思,但是对于一个初学者来说,现在并不需要明白.大家都知道,当下全栈工程师的概念很火 ...

  10. MySQL在创建相同表结构时as和like 使用的区别

    1.MySQL的复制相同表结构方法: 1)create table table_name as select * from table1 where 1=2 (或者limit  0): 2) crea ...