Problem Description
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的矩阵里和最大的子矩阵。

最大连续序列和找最大和矩阵有着共通点,这里需要把一维的转化为二维的是关键

我们可以假定把每一行看作单个的元素,知道每个元素的值,那我们就能够将n列,看作有n个这样的压缩元素,那我们就是在这n个元素中找最大值

(当然压缩列也是可以的,这里我的代码写的是压缩行)

那么我们转化成用二维的sum[i][j]来维护前缀和,表示第i行前j个数的和

#include <cstdio>
#include <map>
#include <iostream>
#include<cstring>
#include<bits/stdc++.h>
#define ll long long int
#define M 6
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int n;
int sum[][];
int dp[];
int main(){
ios::sync_with_stdio(false);
while(cin>>n){
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
int t; cin>>t;
sum[i][j]=sum[i][j-]+t; //前缀和
}
int ans=-inf;
for(int i=;i<=n;i++)
for(int j=;j<=i;j++){ //二重循环枚举 把j~i压缩成一个点
int s=-inf;
memset(dp,,sizeof(dp));
for(int k=;k<=n;k++){ //找最大连续序列
dp[k]=max(dp[k-]+sum[k][i]-sum[k][j-],sum[k][i]-sum[k][j-]);
s=max(s,dp[k]);
}
ans=max(s,ans);
}
cout<<ans<<endl;
}
}

hdu 1081 To The Max(二维压缩的最大连续序列)(最大矩阵和)的更多相关文章

  1. lintcode 最长上升连续子序列 II(二维最长上升连续序列)

    题目链接:http://www.lintcode.com/zh-cn/problem/longest-increasing-continuous-subsequence-ii/ 最长上升连续子序列 I ...

  2. HDU 1081 To The Max【dp,思维】

    HDU 1081 题意:给定二维矩阵,求数组的子矩阵的元素和最大是多少. 题解:这个相当于求最大连续子序列和的加强版,把一维变成了二维. 先看看一维怎么办的: int getsum() { ; int ...

  3. hdu1081 DP类最大子段和(二维压缩+前缀和数组/树状数组计数)

    题意:给出一个 n * n 的数字矩阵,问最大子矩阵和是多少. 由于和最长子段和问题类似,一开始想到的就是 DP ,一开始我准备用两个循环进行 DP ,对于每一个 (i,j) ,考察(i - 1,j) ...

  4. 【C语言】二维数组中的查找,杨氏矩阵

    //二维数组中的查找,杨氏矩阵 //在一个二维数组中,每行都依照从左到右的递增的顺序排序.每列都依照从上到下递增的顺序排序. //请完毕一个函数.输入这种一个数组和一个数,推断数组中是否包括这个数. ...

  5. 【c语言】二维数组中的查找,杨氏矩阵在一个二维数组中,每行都依照从左到右的递增的顺序排序,输入这种一个数组和一个数,推断数组中是否包括这个数

    // 二维数组中的查找,杨氏矩阵在一个二维数组中.每行都依照从左到右的递增的顺序排序. // 每列都依照从上到下递增的顺序排序.请完毕一个函数,输入这种一个数组和一个数.推断数组中是否包括这个数 #i ...

  6. hdu 1081 To The Max(dp+化二维为一维)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1081 To The Max Time Limit: 2000/1000 MS (Java/Others ...

  7. Hdu 1081 To The Max

    To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  8. Poj1050_To the Max(二维数组最大字段和)

    一.Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is an ...

  9. HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

随机推荐

  1. js-canvas(基本用法)

    ###1. canvas(画布) <canvas>是HTML 5 新增的元素,可用于通过使用JavaScript中的脚本来绘制图形 默认宽高为300px*150px 基本概念和方法入门推荐 ...

  2. [转帖]PAT 计算机程序设计能力考试

    PAT 计算机程序设计能力考试 https://blog.csdn.net/bat67/article/details/52134211 [官方简介] 计算机程序设计能力考试(Programming ...

  3. 工程下CmakeLists.txt

    2.工程下Cmake 本小节的任务是让上一小结的程序更像一个工程: 为工程添加一个子目录 src,用来放置工程源代码 : 添加一个子目录doc,用来放置这个工程的文档 hello.txt: 在工程目录 ...

  4. 20181114教学sql

    --精确查找:查询水表编号为30408的业主记录 ' --模糊查询:查询业主名称包含'刘'的业主记录 SELECT * FROM T_OWNERS WHERE NAME LIKE '%刘%' --AN ...

  5. win10查看无线密码

  6. K3CLOUD常用数据表

    一.数据库查询常用表 --查询数据表select * from ( select convert(varchar(4000),t1.FKERNELXML.query('//TableName')) a ...

  7. SQL字段类型bit 查询时注意

    sql 查询时  字段=1 或 字段=0 c# 里也是

  8. 【README.md】Markdown语言常用语法

    转自:http://blog.csdn.net/zhaokaiqiang1992 这里只介绍最常用和最常见的功能,若想查看全部的语法,请移步http://wowubuntu.com/markdown/ ...

  9. Ubuntu install flash

    Software&Updates - Other Software - Canonical Parners sudo apt install adobe-flashplugin

  10. do not track

    privacy.trackingprotection.enabled