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

题解:

看起来很难做的题目,但他让我们想起 最大字段和,但那个是一维的。

那怎么转化二维为1维呢?

当然是枚举了。

分别枚举第 i 行到第 j 行 ,这里的复杂度为O(n^2),然后在用字段和遍历一遍,所以总的时间复杂度为O(n^3) 考虑到n不大,可行。

有一个问题是怎么最快求第k列中,第 i 行道第 j 行的和,我们采用前缀和的方式。

由于网上多是行前缀和,这里贴一个列前缀和的代码

#include<stdio.h>
#include<cmath>
#include<iostream>
#define INF 0x3f3f3f3f
#define me(a,b) memset(a,b,sizeof(a))
#define N 102
typedef long long ll;
using namespace std; int n,a[N][N],dp[N][N],t,sum,ans; int main()
{
//freopen("input.txt","r",stdin);
cin>>n;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
cin>>t;
a[i][j]=a[i][j-]+t;//记录前缀和
}
}
for(int i=;i<=n;i++)
{
for(int j=i;j<=n;j++) //枚举,第i列道第j列
{
sum=;
for(int k=;k<=n;k++)//最大子序列遍历一遍
{
int t=a[k][j]-a[k][i-];//t代表: 第k行中,第 i 列到第 j 列的和
sum+=t;
sum=sum<?:sum; //<0则置为0
if(sum>ans)
ans=sum;
}
}
}
cout<<ans; }

To the Max 二维dp(一维的变形)的更多相关文章

  1. 洛谷P1048 采药 二维dp化一维

    题目描述 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个难题.医师把他带到一个到处都是草药的山洞里对他说:“孩子,这个 ...

  2. HDU - 2159 FATE(二维dp之01背包问题)

    题目: ​ 思路: 二维dp,完全背包,状态转移方程dp[i][z] = max(dp[i][z], dp[i-1][z-a[j]]+b[j]),dp[i][z]表示在杀i个怪,消耗z个容忍度的情况下 ...

  3. 洛谷p1732 活蹦乱跳的香穗子 二维DP

    今天不BB了,直接帖原题吧  地址>>https://www.luogu.org/problem/show?pid=1732<< 题目描述 香穗子在田野上调蘑菇!她跳啊跳,发现 ...

  4. 传纸条 NOIP2008 洛谷1006 二维dp

    二维dp 扯淡 一道比较基本的入门难度的二维dp,类似于那道方格取数,不过走过一次的点下次不能再走(看提交记录里面好像走过一次的加一次a[i][j]的也AC了,,),我记得当年那道方格取数死活听不懂, ...

  5. 关于二维DP————站上巨人的肩膀

    意匠惨淡经营中ing, 语不惊人死不休........ 前几天学了DP,做了个简单的整理,记录了关于DP的一些概念之类的,今天记录一下刚学的一个类型 ----关于二维DP 那建立二维数组主要是干嘛用的 ...

  6. php 二维转一维

    Array(    [0] => Array        (            [salesorderid] => 10001            [createdtime] =& ...

  7. Win10 UWP开发:摄像头扫描二维码/一维码功能

    这个示例演示整合了Aran和微软的示例,无需修改即可运行. 支持识别,二维码/一维码,需要在包清单管理器勾选摄像头权限. 首先右键项目引用,打开Nuget包管理器搜索安装:ZXing.Net.Mobi ...

  8. 三维码 & 二维码 & 一维码

    三维码 & 二维码 & 一维码 3D, 2D, 1D 防伪国家标准 -<结构三维码防伪技术条件> http://www.xinhuanet.com/tech/2019-12 ...

  9. 经典DP 二维换一维

    HDU 1024  Max Sum Plus Plus // dp[i][j] = max(dp[i][j-1], dp[i-1][t]) + num[j] // pre[j-1] 存放dp[i-1] ...

随机推荐

  1. MySQL binlog2sql-闪回数据

    binlog2sql 从MySQL binlog解析出你要的SQL.根据不同选项,你可以得到原始SQL.回滚SQL.去除主键的INSERT SQL等. 用途 数据快速回滚(闪回) 主从切换后新mast ...

  2. 移植QT库的问题:QT_INSTALL/include/QtCore/qatomic_arm.h:131: Error: no such instruction: `swpb %al,

    解决办法:错误信息说明编译器未识别swpb汇编操作,指令集有问题.把配置命令改成: ./configure -embedded armv7 -prefix /home/thwijeth/Softwar ...

  3. Specified key was too long; max key length is 767 bytes解决方案

    问题描述: 1.  使用spark sql处理数据逻辑,逻辑处理后使用 df.write.mode(saveMode).jdbc(url, tableName, connectionPropertie ...

  4. Vue 路由的模块化

    其实就是对路由配置和实例化的过程进行js封装,挂载路由的时候依然在main.js中: 步骤: 1.在src文件夹下新建一个router文件夹,在router文件夹下新建文件router.js; 2.引 ...

  5. vue分页效果

    js部分 <script src="....js/vue.js"></script> <script src="..../js/vue-re ...

  6. linux下导入、导出mysql数据库命令的实现方法

    首先建空数据库 mysql>create database abc; 导入数据库 mysql>use abc; 设置数据库编码 mysql>set names utf8; 导入数据( ...

  7. Android 开发 启动activity并且将前面activity全部清空

    方法一: Intent intent = new Intent(A.this,B.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | I ...

  8. 编译APR包报错 rm: cannot remove `libtoolT': No such file or directory

    centos 6  编译APR包报错 在当前apr 目录 : #Vi configure +31880  ,注释掉此行 再次编译即可.

  9. Unable to compile class for JSP 的可能原因

    浏览器访问web jsp文件,导致500-Unable to compile class for JSP 的原因. 原因一:查看网上的方法,说很可能的原因是tomcat版本比JDK的版本要低导致的.所 ...

  10. 基础Gan代码解析

    initializer总结: #f.constant_initializer(value) 将变量初始化为给定的常量,初始化一切所提供的值. #tf.random_normal_initializer ...