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. 652. Find Duplicate Subtrees

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  2. json模块、os模块

    一.eval模拟序列化操作 1.序列化 内存中的数据-------->转成一种中间格式(字符串)---------->存到文件中 dic={'name':'egon','age':18} ...

  3. Go语言中的HTTP

    Go中的http使用 package main import ( "fmt" "net/http" "io/ioutil" "st ...

  4. .Net 面试题 汇总(一)

    1.@page指令只能在_aspx___文件(填写扩展名)中使用,而@Control指令只能用在_ascx___文件(填写扩展名)中使用. 2.说明控件DataGrid,DataTable,DataV ...

  5. Python标准库--inspect

    inspect模块是针对模块,类,方法,功能等对象提供些有用的方法.例如可以帮助我们检查类的内容,检查方法的代码,提取和格式化方法的参数等. import inspect import os clas ...

  6. Jexus支持HTTPS协议

    众所周知,在HTTPS页面请求HTTP资料的时候,现代浏览器会拦截,提示用户是否继续,或者直接拦截,提示都不出来. 最近给自己做了个快速书签工具,点击书签就直接把书签发送到服务器地址,然后保存到我的网 ...

  7. Oracle 完全理解connect by-详细脚本-可实战

    狒狒Q971751392 未来星开发团队--狒狒(QQ:9715234) oracle树查询的最重要的就是select…start with…connect by…prior语法了.依托于该语法,我们 ...

  8. c++返回引用

    #include <iostream> #include <ctime> using namespace std; double vals[] = {10.1, 12.6, 3 ...

  9. vs调试代码的时候断点无法命中

    https://blog.csdn.net/xxdddail/article/details/18696399 该链接提供的解决方案主要是如下图片:禁用 图片标记的这个选项即可:

  10. jquery框架一点小心得

    下面的小事例 主要实现了 一和按ID查找,并获取元素的 value 或 标签内容和一个去字符串空格的小功能能 假设元素id=“myid”: 获取标签内容$("myid").html ...