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. Oracle问题处理

    一,如果在Oracle中执行for update操作时,出现一致卡顿现象,此时可能是有其它的进程阻塞了. 处理的方法就是查找出阻塞的进程,然后强制杀死 先查询出阻塞进程: select object_ ...

  2. SQL FOR JSON PATH 返回 json

    --直接返回 age FOR JSON PATH --返回值 [{"name":"张学友","age":60}] select c1, c2 ...

  3. WRF安装过程

    WRF安装过程 1.  在虚拟机VMware上安装Fedora 12 x64操作系统. 2. 安装PGI9.01 a)         电驴上可下载[[顶级编译器].PGI.Workstation.C ...

  4. [STM32F103]串口UART配置

    l 串口时钟使能,GPIO时钟使能: RCC_APB2PeriphClockCmd(); l 串口复位: USART_DeInit(); 这一步不是必须的 l GPIO端口模式设置: GPIO_Ini ...

  5. redis哨兵架构的基础知识及部署和管理

    一.前言 1.哨兵的介绍 sentinal,中文名是哨兵 哨兵是redis集群架构中非常重要的一个组件,主要功能如下 ()集群监控,负责监控redis master和slave进程是否正常工作 ()消 ...

  6. Access Token 与 Refresh Token【转载哒科普啊】

    Access Token 与 Refresh Token   access token 是客户端访问资源服务器的令牌.拥有这个令牌代表着得到用户的授权.然而,这个授权应该是临时的,有一定有效期.这是因 ...

  7. GetBuffer 与ToArray区别,解决问题场景

    GetBuffer 是把 stream 中的 buffer 的引用传递出来, buffer 的大小是由 stream的 Capacity来决定的. 因为只是地址的引用传递,所以 GetBuffer() ...

  8. orcal - 多表查询

    SQL1999语法标准 CROSS JOIN 产生笛卡尔积 SELECT * from EMP CROSS JOIN dept; NATURAL JOIN 自然连接 相同列 SELECT * from ...

  9. Spring整合quart初识

    Spring集成quart有两种方式,一种是实现Job接口,一种是继承QuartzJobBean 刚开始报错:持久化时未序列化异常 <bean id="simpleJobDetail& ...

  10. 使用ThreadPoolExecutor进行多线程编程

    ThreadPoolExecutor有四个构造函数,分别是: 1,ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keep ...