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. Java spring实现文件下载

    一,实现目的,后台写一个controller,然后前台页面点击文件下载,实现文件下载功能.(文件是存放于服务器的磁盘上的) @RequestMapping("/filesdownloads& ...

  2. spring资源加载结构解析

    1.spring中资源加载使用resources的原因? 在java将不同资源抽象成url,然后通过注册不同的hander来处理不同读取逻辑,一般hander使用协议的前缀来命名,如http,jar, ...

  3. sql server top 10 IO性能查询

    use master go ), ((case qs.statement_end_offset then datalength(qt.text) else qs.statement_end_offse ...

  4. 使用GSON工具实现json和其他数据格式间转换

    1 package gson; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.google.gson. ...

  5. java 基础排序算法

    冒泡: 从左往右依次比较相邻的两个数,将小数放在前面,大数放在后面. public void bobSort(){        for(int i=0;i<length-1;i++){//排序 ...

  6. LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++

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

  7. linux下使用nmon工具对服务器性能进行检测

    1.nmon工具介绍: nmon工具是linux系统下可以对服务器及系统性能进行监测,CPU信息.CPU占用.内存使用.网卡使用等.最大的好处是此工具会将结果以列表的形式或者是模拟图形化的方式展示,不 ...

  8. docx httpheader头设置

    设置contentType内容类型如下: Extension MIME Type .doc application/msword .dot application/msword .docx appli ...

  9. leetcode974

    这道题目描述很清晰,直接两层循环,代码如下: class Solution(object): def subarraysDivByK(self, A: 'List[int]', K: int) -&g ...

  10. C# ConfigurationManager不存在问题解决

    在做串口通信的时候,需要使用"ConfigurationManager"类,但是添加"Using System.Configuration"命名空间后编译器依旧 ...