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. FastDFS+Nginx轻量级分布式

    一 简介 FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题.特别适合以文件为载体的在线 ...

  2. UITabBarItem的selectedImage

    TabBar使用频率很高的一个组件,TabBar的TabBarItem有两个属性一个是image(未选中图片),另一个是selectedImage(选中时图片) 但是运行时发现,选中时的图片变成了蓝色 ...

  3. 【读书笔记】iOS网络-HTTP-URL结构

    http://user:password@hostname:port/absolute-path?query. http:  协议 user:password@   认证 hostname:  主机名 ...

  4. iOS开发之保存照片到自己创建的相簿

    iOS开发之保存照片到自己创建的相簿 保存照片还可以用ALAssetsLibrary,ALAssetsLibrary提供了我们对iOS设备中的相片.视频的访问,是连接应用程序和相册之间访问的一个桥梁. ...

  5. iOS网络-02-数据解析(JSON与XML)

    数据交互格式 服务器返回给用户的数据,通常是以下两种方式: JSON XML JSON 一种轻量级的数据数据格式,体积比XML小,是服务器返回给移动端通常采用的格式 用使用JSON文件中的数据,需要对 ...

  6. Windows 7 与 Vmware Ubuntu 15.10_64 共享文件夹

    设置共享文件夹     安装/更新最新的vmware-tools   安装             在ubuntu 内部会打开DVD ,里面的压缩包中包含vmware-toole. 先解压      ...

  7. SQL 注入防御方法总结

    SQL 注入是一类危害极大的攻击形式.虽然危害很大,但是防御却远远没有XSS那么困难. SQL 注入可以参见:https://en.wikipedia.org/wiki/SQL_injection S ...

  8. 【mysql】添加对emoji的支持

    1.简介 涉及无线相关的 MySQL 数据库建议都提前采用 utf8mb4 字符集,避免 emoji 表情符号带来的问题 MySQL Server >  5.5.3 2.配置+升级 当前配置 m ...

  9. 基于python的flask的应用实例注意事项

    1.所有的html文件均保存在templates文件夹中 2.运行网页时python manage.py runserver

  10. poj 1664 放苹果,递归(深度优先搜索)

    #include "stdio.h" int DFS(int n,int m); int main() { int T; int n,m; int s; scanf("% ...