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. java和js互调 webview

    public class JavaAndJSActivity extends Activity implements View.OnClickListener { private EditText e ...

  2. javah生成带有包名的头文件

    无包名情况 多数的demo都是基于这种条件,假设在目录jni/下有一个包含native方法的文件Hello.class.进入jni/目录,直接执行javah Hello,就可以在jni/目录下生成文件 ...

  3. callback机制之内核通知链表【转】

    本文转载自:http://bbs.chinaunix.net/thread-2011776-1-1.html 1.通知链表简介    大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生 ...

  4. Buffer的数据存取

    缓冲区 存放要读取的数据 缓冲区 和 通道 配合使用 一个用于特定基本数据类行的容器.有java.nio包定义的,所有缓冲区都是抽象类Buffer的子类. Java NIO中的Buffer主要用于与N ...

  5. (4)获取servlet常用api

    *五)与ServletAPI解耦 方式1 AddAction public String execute() throws Exception, IOException{ //获取请求对象reques ...

  6. 深入理解JVM - 垃圾收集器与内存分配策略 - 第三章

    引用计数算法——判断对象是否存活的算法 很多教科书判断对象是否存活的算法是这样的:给对象添加一个引用计数器,每当一个地方引用它时,计数器值就加1:当引用失效时,计数器值就减1:任何时刻计数器为0的对象 ...

  7. python学习笔记:第五天( 列表、元组)

    Python3 列表 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型,但最常见 ...

  8. Django:locals()小技巧

    locals()返回一个包含当前作用域里面的所有变量和它们的值的字典. 所以可以把views改写为 def current_datetime(request):     current_date = ...

  9. sangfor-AF 地址转换以及各种模式理解(路由,透明,虚拟网线,混合模式)

    目的地址转换: 1.路由其实很简单的,其实你可以理解为路由器就好了2.透明和虚拟网线的区别:虚拟网线不对数据做任何的处理,你可以理解为不封装不拆包,直接丢给对端.而透明不一样,透明你可以把设备当做是交 ...

  10. poj3709 K-Anonymous Sequence[贪心+斜率优化dp]

    地址 n个数,可进行把一个数减小的操作,代价为减小的值.现求使数列任意一个数都存在至少k-1个数和他相同,问操作的最小代价. 可以先考虑最小的数,由于只能减,所以必须得至少k-1个数减为最小数,贪心策 ...