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. 虚虚实实,亦假亦真的 ValueTuple,绝对能眩晕你

    一:背景 1. 讲故事 前几天在写一个api接口,需要对衣物表进行分页查询,查询的output需要返回两个信息,一个是 totalCount,一个是 clothesList,在以前我可能需要封装一个 ...

  2. 云计算openstack核心组件——keystone身份认证服务(5)

    一.Keystone介绍:       keystone 是OpenStack的组件之一,用于为OpenStack家族中的其它组件成员提供统一的认证服务,包括身份验证.令牌的发放和校验.服务列表.用户 ...

  3. (数据科学学习手札95)elyra——jupyter lab平台最强插件集

    本文示例文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 jupyter lab是我最喜欢的编辑器,在过往 ...

  4. iOS多线程之超实用理论+demo演示(可下载)

    目录 背景简介 GCD.OperationQueue 对比 核心理念 区别 GCD 队列 串行队列(Serial Queues) 并发队列(Concurrent Queues) 串行.并发队列对比图 ...

  5. 1.9Hadoop插件

  6. Redis中有序列表(ZSet)相关命令

    redis语序集合和集合set是一样内部value为string类型的集合,有序不允许重复元素 但是,zset的每个元素有一个double类型的分数(score).redis正是靠这个分数对元素从小到 ...

  7. zabbix安装要求

    zabbix server 和 zabbix Agent都在一个压缩包中,可以从官网上下载源码包,www.zabbix.com/download.php zabbix是C语言编写,压缩包的文件夹介绍: ...

  8. 【数量技术宅 | Python爬虫系列分享】实时监控股市重大公告的Python爬虫

    实时监控股市重大公告的Python爬虫小技巧 精力有限的我们,如何更加有效率地监控信息? 很多时候特别是交易时,我们需要想办法监控一些信息,比如股市的公告.如果现有的软件没有办法实现我们的需求,那么就 ...

  9. 指针数组学习中的小插曲真是醉了-----Strcmp用法

    参考: 1.C++ 从入门到精通第三版: 2.https://blog.csdn.net/liaoshengshi/article/details/45099923      如是多次被别人转载的地址 ...

  10. ATMEGA的SPI总线 - 第2部分

    参考: 1.https://www.yiboard.com/thread-783-1-1.html 2.https://mansfield-devine.com/speculatrix/2018/01 ...