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. IO复用——poll系统调用

    1.poll函数 #include<poll.h> int poll(struct pollfd* fds, nfds_t ndfs, int timeout) poll函数在一定的时间内 ...

  2. 017---Django的中间件解决跨域

    跨域 跨域是什么? 浏览器从一个域名的网页去请求另一个域名的资源的时候,如果不同源.请求的响应结果就会被浏览器的同源策略所拦截 同源策略是什么? 同源:协议 + 域名 + 端口 特点:阻止ajax请求 ...

  3. Spring + MySQL + Mybatis + Redis【二级缓存】

    一.Redis环境 Redis 官网 :http://redis.io/ windows下载:https://github.com/dmajkic/redis/downloads 1.文件解压缩 2. ...

  4. Hibernate-ORM:03.Hibernate主键生成策略

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 此篇博客简单记录五种常用的主键生成策咯: 不同的主键生成策略,生成的sql语句,以及hibernate的操作都 ...

  5. L008之前课程实战模拟。

    L008之前课程实战模拟. . 安装CentOS 6.5 X86_64 . 配置网络 . 用CRT连接服务器 . 更换源http://mirrors.163.com/.help/CentOS6-Bas ...

  6. 【Spring实战】----开篇(包含系列目录链接)

    [Spring实战]----开篇(包含系列目录链接) 置顶2016年11月10日 11:12:56 阅读数:3617 终于还是要对Spring进行解剖,接下来Spring实战篇系列会以应用了Sprin ...

  7. Java byte 位移操作 注意事项

    转自:http://blog.163.com/pilgrim_yang/blog/static/55631481201111542151582/ Java对byte 的 + - * / >> ...

  8. SQL 注入教程

    SQL 注入测评教程 1     准备 安装包:Burpsuit.Python27.sqlmap 2     安装配置 2.1    Burpsuit 1)       解压Burpsuit 2)   ...

  9. 【Selenium-Python】Selenium-Firefox 环境配置 win64

    Python 已安装完毕 Selenium 安装: Windows > cmd pip install selenium 注:未加selenium版本号时默认安装最新版本. 查询当前Seleni ...

  10. 树莓派3_win10下使用"远程桌面连接"与树莓派通信(使用VNC实现连接后)

    -----------------------------------------------------------学无止境------------------------------------- ...