POJ 1050 To the Max 二维最大子段和
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 二维最大子段和的更多相关文章
- poj 1195:Mobile phones(二维树状数组,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14489 Accepted: 6735 De ...
- poj 1195:Mobile phones(二维线段树,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14391 Accepted: 6685 De ...
- POJ 1050 To the Max 最大子矩阵和(二维的最大字段和)
传送门: http://poj.org/problem?id=1050 To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- poj - 1050 - To the Max(dp)
题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 ...
- poj 1050 To the Max(线性dp)
题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...
- poj 1050 To the Max(最大子矩阵之和)
http://poj.org/problem?id=1050 我们已经知道求最大子段和的dp算法 参考here 也可参考编程之美有关最大子矩阵和部分. 然后将这个扩大到二维就是这道题.顺便说一下,有 ...
- HDU 3404&POJ 3533 Nim积(二维&三维)
(Nim积相关资料来自论文曹钦翔<从"k倍动态减法游戏"出发探究一类组合游戏问题>) 关于Nim积计算的两个函数流程: 代码实现如下: ][]={,,,}; int N ...
- hdu 1081 To The Max(二维压缩的最大连续序列)(最大矩阵和)
Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle ...
随机推荐
- tail命令使用
1.tail命令 命令的主要用途是将指定的文件的最后部分输出到终端,如果该文件有更新,tail会自己主动刷新. 2.tail语法 tail [ -f ] [ -c Number | -n Number ...
- python, pycharm, virtualenv 的使用
创建虚拟环境,一次安装多个库 pip freeze > requirements.txt (库的名字都在里面) 产生requirements.txt文件 在另一个环境下使用 pip instal ...
- 9.Mongodb与python交互
1.与python交互 点击查看官方文档 安装python包 进入虚拟环境 sudo pip install pymongo 或源码安装 python setup.py 引入包pymongo impo ...
- POJ 1568 Find the Winning Move
Find the Winning Move 链接 题意: 4*4的棋盘,给出一个初始局面,问先手有没有必胜策略? 有的话输出第一步下在哪里,如果有多个,按(0, 0), (0, 1), (0, 2), ...
- c++ class as sort function
// constructing sets #include <iostream> #include <set> #include <string.h> bool f ...
- Anytime项目开发记录1
关于Android APP 应用设计,我并没有接受过系统的学习. 下面,是按照我一直以来的方法来进行编辑. 由于在程序开始之前并没有画类图,这里简单的讲述一下程序是如何设计的. 自己实现了一个Appl ...
- Linux下创建pycharm的快捷方式
第一步:创建桌面快捷方式文件Pycharm.desktop,并打开 sudo gedit /usr/share/applications/Pycharm.desktop 第二步:在打开的文件Pycha ...
- volatility的使用
volatility取证的使用----windows内存 简介 kali下默认安装 可以对windows,linux,mac,android的内存进行分析 内存文件的准备 Win2003SP2x86下 ...
- 自动化测试---mybatis的使用
mybatis如何实现了对数据库的操作: 1.通过Resources.getResourceAsReader()或者 Resources.getResourceAsStream()加载mybatis. ...
- packstack安装ironic
KVM Centos7.3虚机 安装openstack Pike版本, 其它版本安装方法类似. packstack目前对NetworkManager 还不支持,我们修改下配置: systemctl d ...