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. leetcode560题解【前缀和+哈希】

    leetcode560.和为K的子数组 题目链接 算法 前缀和+哈希 时间复杂度O(n). 在解决这道题前需要先清楚,一个和为k的子数组即为一对前缀和的差值. 1.我们假设有这么一个子数组[i,j]满 ...

  2. Java 中 static 的作用

    static 关键字的作用 在 Java 中 static 关键字有4种使用场景,下面分别进行介绍: 1.static 成员变量 public class Student { // 静态成员变量 pr ...

  3. javaweb修改表单参数---使用过滤器

    需求: 所有的字段要将空字符串转成null: 问题: 我们知道表单如果不写值的时候,传递到后台的不是null,而且是空字符串.那么怎么改成null呢? 解决: 使用过滤器,将请求的参数修改过后继续,再 ...

  4. 记一次select2赋值动态数组的坑

    var roles = $td.eq(3).text().split(","); var arr = []; //循环去除每个值前后的空格,否则下拉框赋值回显出错for(var i ...

  5. 微信小程序-实现文字跑马灯-wepy

    百度蛮多例子的,但是代码太长懒得看了 前言 要实现跑马灯主要就是获得判断开始定界和结束定界, 1.9.3新增的wxml操作接口 就可以拿到节点长宽等属性,当然你也可以直接用 文字数量 * 文字大小(注 ...

  6. 快速删除XMind指定层级的方法

    在使用xmind梳理知识点的时候,因为长期积累,单个文件的节点数可能超过1000个,层级可能超过6层.但在我们做文件分享时,可能只需要提供3层的思维导图,这时候就需要对子节点进行删除.原始的方法,就是 ...

  7. 【CF1425B】 Blue and Red of Our Faculty! 题解

    原题链接 简要翻译: 有一个连通图,A和B同时从点1出发,沿不同的路径前进.原本,图上的每一条边都是灰色的.A将经过的边涂成红色,B将经过的边涂成蓝色的.每个回合每个人只能走灰色的边.当某个回合中不存 ...

  8. Book of Shaders 00 - 使用 VS Code 编写 GLSL

    0x00 写在前面 最近在学习由 Patricio 编写的 The Book of Shaders,这是一本关于 Fragment Shaders(片段着色器)的入门指南.为了在一个相对熟悉的平台运行 ...

  9. 使用free掉的内存的危害

    1 源码 #include <stdio.h> #include <stdlib.h> // 编译环境 gcc int main(void) { printf("** ...

  10. 实验四  用PS制作横幅广告

    实验四  用PS制作横幅广告 [实验目的] ⑴.了解广告设计应用 ⑵.学会利用PS制作简单的横幅广告 [实验条件] ⑴.个人计算机一台 ⑵.个人计算机中预装Windows7操作系统和浏览器及Photo ...