To the Max
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 52281 Accepted: 27633
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

题意:给你一个n*n 的矩形,要你求和最大的一个子矩形
题解:由一维的最大子段和变成了二维的最大子矩阵和,思想还是一样的,那就是保存每一段的最大和,然后更新最大值就行
将二维的看做一维,即控制第二维的深度去求最大子段和
代码如下:

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define fuck(x) cout<<"["<<x<<"]";
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int maxn = 1e3+;
const int INF = 0x3f3f3f3f;
int dp[maxn];
int mp[maxn][maxn];
int maxx; int main(){
#ifndef ONLINE_JUDGE
FIN
#endif
int n;
scanf("%d",&n);
maxx=-;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&mp[i][j]);
if(mp[i][j]>maxx){
maxx=mp[i][j];
}
}
}
if(maxx<=){
printf("%d\n",maxx);
}else{
maxx=-;
int l,r;
for(int i=;i<=n;i++){
for(int j=;j<=n-i+;j++){ //控制所求子段的深度
l=i,r=j+i-;
dp[]=;
for(int k=;k<=n;k++){ //控制所求子段的长度
int tmp=;
for(int s=l;s<=r;s++){
tmp+=mp[k][s];
}
dp[k]=max(dp[k-]+tmp,tmp);
maxx=max(dp[k],maxx);
}
}
}
printf("%d\n",maxx);
}
return ;
}

POJ 1050 To the Max 二维最大子段和的更多相关文章

  1. poj 1195:Mobile phones(二维树状数组,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14489   Accepted: 6735 De ...

  2. poj 1195:Mobile phones(二维线段树,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14391   Accepted: 6685 De ...

  3. POJ 1050 To the Max 最大子矩阵和(二维的最大字段和)

    传送门: http://poj.org/problem?id=1050 To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submi ...

  4. [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

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

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

  6. poj 1050 To the Max(线性dp)

    题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...

  7. poj 1050 To the Max(最大子矩阵之和)

    http://poj.org/problem?id=1050 我们已经知道求最大子段和的dp算法 参考here  也可参考编程之美有关最大子矩阵和部分. 然后将这个扩大到二维就是这道题.顺便说一下,有 ...

  8. HDU 3404&POJ 3533 Nim积(二维&三维)

    (Nim积相关资料来自论文曹钦翔<从"k倍动态减法游戏"出发探究一类组合游戏问题>) 关于Nim积计算的两个函数流程: 代码实现如下: ][]={,,,}; int N ...

  9. hdu 1081 To The Max(二维压缩的最大连续序列)(最大矩阵和)

    Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle ...

随机推荐

  1. 静态栈抽象数据类型stack实现

    #include<stdio.h> #include<stdbool.h> #include<stdlib.h> #define MAX_STACK_SIZE 10 ...

  2. Kuernetes-设计架构(二)

    Kubernetes设计架构 Kubernetes集群包含有节点代理kubelet和Master组件(APIs,scheduler.etc),一切都基于分布式的存储系统.Kubernetes架构图: ...

  3. ABAP CDS ON HANA-(10)項目結合して一つ項目として表示

    Numeric Functions ABS(arg)  CEIL(arg) DIV(arg1, arg2) DIVISION(arg1, arg2, dec) FLOOR(arg) MOD(arg1, ...

  4. Hibernate-ORM:12.Hibernate中的多对多关联关系

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客将讲述Hibernate中的多对多关联关系的操作,准备的篇幅较少,望海涵 一,讲述多对多 多对多的关联 ...

  5. javascript 自定义发布与订阅

    //声明一个类,与普通的类的声明不一样, function Girl() { //将类的事件声明成一个私有的属性,里面是一个对象 this._events = {} } /* { "失恋&q ...

  6. NoSQL简单学习(一)

    只是简单的知道有这个东西,却从来没有去接触,今天看了几篇文章,记录一下,开始慢慢接触这一领域 简介: 8种Nosql数据库系统对比 http://blog.jobbole.com/1344/ 一网打尽 ...

  7. 在Linux下通过rpm打包发布Java程序

    这个东西涉及的内容较多,根据下面这些文章慢慢学习 一个简单的例子 http://blog.csdn.net/king_on/article/details/7169384 按照文章中的步骤来,打包之后 ...

  8. Linux初步——常用简单命令

    散乱的记录,目前是边学边用,以后有机会再整理 curl命令 发起一个HTTP请求,如:curl "http://www.baidu.com" 加上-I选项查看HTTP协议头的信息, ...

  9. react children技巧总结

    在使用该技巧时,建议先看一下相关的知识,点我查看 假如使用该属性时,想把父组件的所有属性及部分方法传递给子组件,该怎么办呢?看代码 const Child = ({ doSomething, valu ...

  10. Java中的while(true)

    while(true)是一个无限循环 在内部用break或return退出循环,否则一直循环