PKU 1050-To The Max(找矩形内元素最大和)

时间:2023-01-15 14:33:12
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 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 x 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
 
题意:给一个N*N的矩形数组,求其中元素和最大的子矩形,输出最大值。
解析:因为数据比较小,可以枚举每个矩形,但是求矩形和时可以预处理一下。可以设置一个数组比如RecSum[i][j],i,j分别代表行,RecSum[i][j]表示右下角坐标为(i,j)左上角为(1,1)的矩形元素和,那么求某个矩形时,如左上角坐标为(upx,upy),右下角坐标为(lowx,lowy),则体积 V=RecSum[lowx][lowy]-RecSum[upx][lowy]-(RecSum[lowx][upy]-RecSum[upx][upy]);最后找最大值即可。
 
 代码如下:
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iterator>
#include<utility>
#include<sstream>
#include<iostream>
#include<cmath>
#include<stack>
using namespace std;
const int INF=1000000007;
const double eps=0.00000001;
int N,elem[101][101];
int RowSum[101][101],RecSum[101][101];
inline void Get_RowSum() //处理每一行的前m个数的元素和
{
memset(RowSum,0,sizeof(RowSum));
for(int x=1;x<=N;x++)
for(int y=1;y<=N;y++)
{
if(y==1) RowSum[x][y]=elem[x][y];
else RowSum[x][y]=RowSum[x][y-1]+elem[x][y];
}
}
inline void Get_RecSum() //得到RecSum[][]
{
memset(RecSum,0,sizeof(RecSum));
for(int x=1;x<=N;x++)
for(int y=1;y<=N;y++)
{
if(x==1) RecSum[x][y]=RowSum[x][y];
else RecSum[x][y]=RecSum[x-1][y]+RowSum[x][y];
}
}
inline int Get(int upx,int upy,int lowx,int lowy)
{
return RecSum[lowx][lowy]-RecSum[upx][lowy]-(RecSum[lowx][upy]-RecSum[upx][upy]);
}
int Cal(int row,int col)
{
int ret=-INF;
for(int i=0;i+row<=N;i++) //枚举每个矩形
{
for(int j=0;j+col<=N;j++)
{
int upx=i,upy=j,lowx=i+row,lowy=j+col;
ret=max(ret,Get(upx,upy,lowx,lowy));
}
}
return ret;
}
int main()
{
while(cin>>N)
{
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++) scanf("%d",&elem[i][j]);
Get_RowSum();
Get_RecSum();
int ans=-INF;
for(int row=1;row<=N;row++)  // 枚举矩形大小
for(int col=1;col<=N;col++)
ans=max(ans,Cal(row,col));
cout<<ans<<endl;
}
return 0;
}
 
 
 

PKU 1050-To The Max(找矩形内元素最大和)的更多相关文章

  1. lightOJ 1366 Pair of Touching Circles(统计矩形内相切圆对)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1366 题意:给出一个矩形,在内部画两个圆A和B使得AB都完全在矩形内且AB相切且AB的 ...

  2. POJ 1410 Intersection(判断线段交和点在矩形内)

    Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9996   Accepted: 2632 Desc ...

  3. 以log&lpar;n&rpar;的时间求矩形内的点

    设想这么一个简单的问题,在一个平面上有n个点,给定一个矩形,问位于矩形内的点有哪些. 这个问题的简单思路非常简单,每次遍历所有点,看其是否在给定的矩形中.时间复杂度呢?单次查询的时间就是一次遍历的时间 ...

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

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

  5. POJ 1050 To the Max 最详细的解题报告

    题目来源:To the Max 题目大意:给定一个N*N的矩阵,求该矩阵中的某一个矩形,该矩形内各元素之和最大,即最大子矩阵问题. 解题方法:最大子序列之和的扩展 解题步骤: 1.定义一个N*N的矩阵 ...

  6. 洛谷P2241-统计方形-矩形内计算长方形和正方形的数量

    洛谷P2241-统计方形 题目描述: 有一个 \(n \times m\) 方格的棋盘,求其方格包含多少正方形.长方形(不包含正方形). 思路: 所有方形的个数=正方形的个数+长方形的个数.对于任意一 ...

  7. CSS里常见的块级元素和行内元素

    根据CSS规范的规定,每一个网页元素都有一个display属性,用于确定该元素的类型,每一个元素都有默认的display属性值,比如div元素,它的默认display属性值为“block”,成为“块级 ...

  8. CSS块级元素和行内元素

    根据CSS规范的规定,每一个网页元素都有一个display属性,用于确定该元素的类型,每一个元素都有默认的display属性值,比如div元素,它的默认display属性值为“block”,成为“块级 ...

  9. css盒模型和块级、行内元素深入理解

    盒模型是CSS的核心知识点之一,它指定元素如何显示以及如何相互交互.页面上的每个元素都被看成一个矩形框,这个框由元素的内容.内边距.边框和外边距组成,需要了解的朋友可以深入参考下 一.CSS盒模型 盒 ...

随机推荐

  1. Atitit 深入理解耦合Coupling的原理与attilax总结

    Atitit 深入理解耦合Coupling的原理与attilax总结     耦合是指两个或两个以上的电路元件或电网络等的输入与输出之间存在紧密配合与相互影响,并通过相互作用从一侧向另一侧传输能量的现 ...

  2. Python自动化 【第五篇】:Python基础-常用模块

    目录 模块介绍 time和datetime模块 random os sys shutil json和pickle shelve xml处理 yaml处理 configparser hashlib re ...

  3. 慎用MySQL replace语句

    语法: REPLACE [LOW_PRIORITY | DELAYED] [INTO] tbl_name [PARTITION (partition_name,...)] [(col_name,... ...

  4. MongoDB的交互(mongodb&sol;node-mongodb-native)、MongoDB入门

    MongoDB 开源,高性能的NoSQL数据库:支持索引.集群.复制和故障转移.各种语言的驱动程序:高伸缩性: NoSQL毕竟还处于发展阶段,也有说它的各种问题的:http://coolshell.c ...

  5. 【英语】Bingo口语笔记&lpar;32&rpar; - 口语中的弱读

  6. css超出一行添加省略号属性&colon;text-overflow和white-space

    通过使用text-overflow和white-space属性来使文本在一行内显示,超出则加省略号,添加如下html代码: <p>前端开发博客专注前端开发和技术分享,如果描述超过100像素 ...

  7. Sqlserver查询数据库文件大小和剩余空间

    在MS Sql Server中可以能过以下的方法查询出磁盘空间的使用情况及各数据库数据文件及日志文件的大小及使用利用率: 1.查询各个磁盘分区的剩余空间:Exec master.dbo.xp_fixe ...

  8. unity3d 通过添加rigidBody来指明物体是动态的,以避免cache开销

    unity3d 通过添加rigidBody来指明物体是动态的,以避免cache开销. 如果不添加rigidBody,则unity会认为它是静态的,会对物理计算进行cache,但如果此物体实际上tran ...

  9. shell if&comma;case&comma;for&comma;while语法

    #shell if的语法 if [空格 xxx 空格] then echo xxxxx exit 1/2/3/4/.... 0表示正确. elif [空格 xxx 空格] then echo xxxx ...

  10. scrapy框架基础

    一.简介 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,非常出名,非常强悍.所谓的框架就是一个已经被集成了各种功能(高性能异步下载,队列,分布式,解析,持久化等)的具有很强通用性 ...