To the Max

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 47985   Accepted: 25387

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

枚举:单纯枚举必然超时,n^6完全不可取

前缀和优化枚举:分别搜一个左上角的点和一个右上角的点,可以将复杂度降到n^4,但仍然超时

于是我们考虑,可不可以换一种思路:

可以枚举上下边界,然后问题就转变成了一位字段和求最大

#include<iostream>
using namespace std;
int n,map[][],sum[][],ans;
int main(){
cin>>n;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
cin>>map[i][j];
sum[i][j]=sum[i][j-]+map[i][j];
}
for(int i=;i<n;i++){
for(int j=i;j<=n;j++){
int Sum=;
for(int k=;k<=n;k++){
Sum+=sum[k][j]-sum[k][i-];
if(Sum<)Sum=;
else if(Sum>ans)ans=Sum;
}
}
}
cout<<ans;
}
												

To the max(求最大子矩阵和)的更多相关文章

  1. POJ1050To the Max(求最大子矩阵)

    题目链接 题意:给出N*N的矩阵,求一个子矩阵使得子矩阵中元素和最大 分析: 必备知识:求一组数的最大连续和 int a[N]; ,maxn = -INF; ; i <= n; i++) { i ...

  2. Task 4.4二维环形数组求最大子矩阵之和

    任务: (1)输入一个二维整形数组,数组里有正数也有负数. (2)二维数组首尾相接,象个一条首尾相接带子一样. (3)数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和. (4)求所有子数 ...

  3. City Game UVALive - 3029(悬线法求最大子矩阵)

    题意:多组数据(国外题好像都这样),每次n*m矩形,F表示空地,R表示障碍 求最大子矩阵(悬线法模板) 把每个格子向上延伸的空格看做一条悬线 以le[i][j],re[i][j],up[i][j]分别 ...

  4. BZOJ 1057: [ZJOI2007]棋盘制作 悬线法求最大子矩阵+dp

    1057: [ZJOI2007]棋盘制作 Description 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑 ...

  5. poj 1050 To the Max_dp求最大子矩阵和

    题意:求最大子矩阵和 利用dp[i]每次向下更新,构成竖起的单条矩阵,再按不小于零就加起来来更新,构成更大的矩阵 #include <iostream> #include<cstdi ...

  6. hdu 2870(dp求最大子矩阵)

    题意:让你求的是由同一字母组成的最大子矩阵,w可以变成a或者b,x可以变成b或者c,y可以变成a或者c,z可以变成a或者b或者c. 分析:这是hdu 1506.hdu 1505的加强版,具体的分析看我 ...

  7. hdu 1505(dp求最大子矩阵)

    题意:就是让你求出全由F组成的最大子矩阵. 分析:这是hdu 1506的加强版,只不过这道题变成了2维的,那我们就一行一行的来.具体的分析见1506的博客:http://www.cnblogs.com ...

  8. 【 HDU1081 】 To The Max (最大子矩阵和)

    题目链接 Problem - 1081 题意 Given a two-dimensional array of positive and negative integers, a sub-rectan ...

  9. 51nod 1051 求最大子矩阵和

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1051 1051 最大子矩阵和 基准时间限制:2 秒 空间限制: ...

随机推荐

  1. (转)js中__proto__和prototype的区别和关系

    作者:doris链接:https://www.zhihu.com/question/34183746/answer/58155878来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...

  2. CUDA:零拷贝主机内存

    The easy way to achieve copy/compute overlap!1.Enable Host Mapping* Runtime: cudaSetDeviceFlags() wi ...

  3. PYTHON调用C接口(基于Ctypes)实现stein算法最大公约数的计算

    相关环境配置 mingw,选择相应的32位.64位的版本,主要用于编译动态链接库dll文件,可用vs替代,这里我选择轻量级的mingw windows64位地址:https://sourceforge ...

  4. Java for LeetCode 102 Binary Tree Level Order Traversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  5. nodejs搭建简单的websocket服务端

    创建websocket服务端使用了nodejs-websocket ,首先要安装nodejs-websocket,在项目的目录下: npm install nodejs-websocket 1.搭建w ...

  6. CSS3文字阴影实现乳白文字效果

    CSS3文字阴影实现乳白文字效果是一款有效利用css3的text-shadow属性,可以实现很多漂亮的效果,CSS3 文字阴影 文字特效,字体效果. 源码下载:http://www.huiyi8.co ...

  7. laravel基础课程---13、数据库基本操作2(lavarel数据库操作和tp对比)

    laravel基础课程---13.数据库基本操作2(lavarel数据库操作和tp对比) 一.总结 一句话总结: 非常非常接近:也是分为两大类,原生SQL 和 数据库链式操作 学习方法:使用时 多看手 ...

  8. CentOS7 网络管理工具nmcli

    今天帮别人调试虚拟机的网络问题(CentOS 7系统),习惯性直接改/etc/sysconfig/network-scripts/ifcfg-xxx配置文件,但是不知道为什么重启network后静态i ...

  9. python raw string

    path = r'C:\a\b\c.txt' r'字符串' 是raw 字符串的意思, 其中的字符串不会转义,即不解释 \ . 作用之一:可以用来保存Windows的路径,直接从资源管理器复制来粘贴,不 ...

  10. 001-Bootstrap栅格系统

    1 安装和基本使用 外文官网 中文官网 可以正常下载使用 有三个文件夹, 分别是css, fonts, js bootstrap/ ├── css/ │ ├── bootstrap.css │ ├── ...