To the Max

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 49351   Accepted: 26142

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

题意:

求矩阵中和最大的子矩阵。

思路:

把题目转化成一维的最大连续子段和。

枚举起始行i和终止行j,将i~j行的数对应相加,求解最大连续子段和。

代码:

#include<iostream>
using namespace std;
const int maxn = 105;
int grap[maxn][maxn], sum[maxn][maxn];
int main()
{
int n, tmp, ans=-0x3f3f3f3f;
cin>>n;
for(int i=1; i<=n; ++i)
{
for(int j=1; j<=n; ++j)
{
cin>>grap[i][j];
sum[i][j]=sum[i-1][j]+grap[i][j];
}
}
for(int i=1; i<=n; ++i)
{
for(int j=i; j<=n; ++j)
{
tmp=0;
for(int k=1; k<=n; ++k)
{
if(tmp<=0)
tmp=sum[j][k]-sum[i-1][k];
else
tmp+=sum[j][k]-sum[i-1][k];
ans=max(ans, tmp);
}
}
}
cout<<ans<<endl;
return 0;
}

poj1050 To the Max(降维dp)的更多相关文章

  1. 【noip模拟赛5】任务分配 降维dp

    描述 现有n个任务,要交给A和B完成.每个任务给A或给B完成,所需的时间分别为ai和bi.问他们完成所有的任务至少要多少时间. 输入 第一行一个正整数n,表示有n个任务.接下来有n行,每行两个正整数a ...

  2. [POJ1050]To the Max

    [POJ1050]To the Max 试题描述 Given a two-dimensional array of positive and negative integers, a sub-rect ...

  3. HDOJ(HDU).1003 Max Sum (DP)

    HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...

  4. poj - 1050 - To the Max(dp)

    题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 ...

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

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

  6. [POJ1050]To the Max(最大子矩阵,DP)

    题目链接:http://poj.org/problem?id=1050 发现这个题没有写过题解,现在补上吧,思路挺经典的. 思路就是枚举所有的连续的连续的行,比如1 2 3 4 12 23 34 45 ...

  7. (线性dp 最大子段和 最大子矩阵和)POJ1050 To the Max

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54338   Accepted: 28752 Desc ...

  8. [POJ1050] To the Max 及最大子段和与最大矩阵和的求解方法

    最大子段和 Ο(n) 的时间求出价值最大的子段 #include<cstdio> #include<iostream> using namespace std; int n,m ...

  9. poj 1050 To the Max (简单dp)

    题目链接:http://poj.org/problem?id=1050 #include<cstdio> #include<cstring> #include<iostr ...

随机推荐

  1. springboot之启动端口指定

    https://www.cnblogs.com/yaomajor/p/8616929.html

  2. spring boot之Thymeleaf

    没怎么仔细研究,就是spring boot的服务器需要一个登录界面,用这个实现了一个白板式的页面. https://blog.csdn.net/yelllowcong/article/details/ ...

  3. php反序列化浅谈

    0x01 serialize()和unserialize() 先介绍下几个函数 serialize()是用于将类转换为一个字符串 unserialize()用于将字符串转换回一个类 serialize ...

  4. websocket直接绕过JS加密的方式

    目录 websocket--hook 服务端--WebSocketServer.js 客户端注入JS代码 python开端口 get_data.py 文件方式 get_user_id.py 文件方式 ...

  5. 刷题[CISCN2019 华北赛区 Day2 Web1]Hack World

    解题思路 打开发现是很简单的页面,告诉了表名和列名,只需知道字段即可 尝试一下,输入1,2都有内容,后面无内容.输入1'让他报错,发现返回bool(false) 大概思路就是布尔型注入了,通过不断返回 ...

  6. java中对 闰年的计算 以及月份天数

    import java.io.*;//局部变量的使用import java.util.Scanner; public class HelloJava {     public static void ...

  7. 关于button和submit的form提交以及 页面跳转问题

    最近在做官网的注册登录form提交时遇到了这个问题,1.0时因为使用普通的模板并没有出现页面跳转失败问题 由于2.0时更换了注册模板,此时按钮样式是以下样式 而在css样式的模板里使用的是button ...

  8. 谈谈数据库的事务ACID

    在数据库中新建一个字段并且设置为索引列,还有删除整张表的数据,类似这些操作都是一系列操作的组合,执行后不能出现中间状态,也就是不会出现新建了字段却不是索引的情况,也不会出现只有一部分数据被删除的情况. ...

  9. Python实现的数据结构与算法之双端队列详解

    一.概述 双端队列(deque,全名double-ended queue)是一种具有队列和栈性质的线性数据结构.双端队列也拥有两端:队首(front).队尾(rear),但与队列不同的是,插入操作在两 ...

  10. 八、多线程爬虫(先占个位置,等整理好线程,进程,协程,异步IO在来写)

    计算机的核心是CPU,CPU承担了所有的计算任务. 一个CPU核心,一次只能执行一个任务: 多个CPU核心同时可以执行多个任务. 一个CPU一次只能执行一个进程,其他进程处于非运行状态. 进程里包含的 ...