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. Redis 部署方式(单点、master/slaver、sentinel、cluster) 概念与区别

    转载自 https://blog.csdn.net/java_zyq/article/details/83818341 在K8S上部署Redis集群时突然遇到一个(sentinel哨兵模式)概念,感觉 ...

  2. 【转】PostgreSQL Index性能调优

    Index(索引)这个概念对于很多熟悉关系型数据库的人来说,不是一个陌生的概念.当表中数据越来越多时,在查询时,为了避免全表查询(sequence scan)可以在查询相关的条件字段上添加索引.举例来 ...

  3. [Abp vNext 源码分析] - 21. 界面与文字的本地化

    一.简介 ABP vNext 提供了全套的本地化字符串支持,具体用法可以参考官方使用文档.vNext 本身是对 Microsoft 提供的本地化组件进行了实现,通过 JSON 文件提供本地化源,这一点 ...

  4. 适用于 deno 的多版本管理工具 dvm 发布

    不知不觉中,deno 已经默默的发布了 3 个版本了: 0.1.0 0.1.1 0.1.2 昨晚通宵做了一个 deno 多版本的管理工具: dvm. github 地址: https://github ...

  5. IIS网站建立好后如何更改绑定IP或端口号

    写在前面的话 我们利用IIS建立网站的时候,一般都是设定好网站名称和物理地址,直接下一步建立完成了.正常访问都没问题,但如果我们这时候想要更改访问的IP或者端口号,打开了很多设置项就是没找到设置的地方 ...

  6. 编程体系结构(05):Java多线程并发

    本文源码:GitHub·点这里 || GitEE·点这里 一.多线程导图 二.多线程基础 1.基础概念 线程是操作系统能够进行运算调度的最小单位,包含在进程之中,是进程中的实际运作单位.一条线程指的是 ...

  7. selenium-自动化测试51job网站(MacOS + Safari)2020年10月6日

    登录 51job ,http://www.51job.com 输入搜索关键词 "python", 地区选择 "杭州"(注意,如果所在地已经选中其他地区,要去掉) ...

  8. 092 01 Android 零基础入门 02 Java面向对象 02 Java封装 01 封装的实现 03 # 088 01 Android 零基础入门 02 Java面向对象 02 Java封装 02 static关键字 02 static关键字(中)

    092 01 Android 零基础入门 02 Java面向对象 02 Java封装 01 封装的实现 03 # 088 01 Android 零基础入门 02 Java面向对象 02 Java封装 ...

  9. The Python Tutorial 和 documentation和安装库lib步骤

    链接: The Python Tutorial : https://docs.python.org/3.6/tutorial/index.html Documentation: https://doc ...

  10. k8s的namespace一直Terminating的完美解决方案

    k8s的namespace一直Terminating的完美解决方案 在k8s集群中进行测试删除namespace是经常的事件,而为了方便操作,一般都是直接对整个名称空间进行删除操作. 相信道友们在进行 ...