poj 1050 To the Max(最大子矩阵之和)
http://poj.org/problem?id=1050
我们已经知道求最大子段和的dp算法 参考here 也可参考编程之美有关最大子矩阵和部分。
然后将这个扩大到二维就是这道题。顺便说一下,有时候不要把问题想复杂了,有些问题只能靠暴力求解,而这道题是暴力加算法。
在这个题中,我们可以把二维压缩到一维然后求解最大子段。我们先枚举所求矩阵的起点行和结束行,然后把每一列的数据之和求出,用这些数据和就构造出一个一维的数组(代码中我没有明确表示出这个数组),然后用最大子段和的dp算法求解。
关于二维压缩到一维的过程,适当处理可以大大减小时间复杂度。
最终时间复杂度是O(n^3);
我的代码,思路是正确的,但是提交上去之后wa了,求大神赐教。
//2013-06-26-08.45
//poj 1050
#include <iostream>
#include <string.h>
#include <algorithm>
int map[103][103];
int sum[103][103]; using namespace std; int main()
{
int n;
while (cin >> n)
{
memset(sum, 0, sizeof(sum));
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> map[i][j];
sum[i][j] = map[i][j] + sum[i-1][j];
}
}
int ans = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < i; j++)
{
int tol = 0;
for (int k = 1; k <= n; k++)
{
if (sum[i][k]-sum[j][k] < 0) //这个地方就是压缩后的数组
tol = 0;
else
tol += (sum[i][k]-sum[j][k]);
if (tol > ans)
ans = tol;
}
}
}
cout << ans << endl;
}
return 0;
}
poj 1050 To the Max(最大子矩阵之和)的更多相关文章
- poj 1050 To the Max(最大子矩阵之和,基础DP题)
To the Max Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 38573Accepted: 20350 Descriptio ...
- POJ 1050 To the Max 最大子矩阵和(二维的最大字段和)
传送门: http://poj.org/problem?id=1050 To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- POJ 1050 To the Max (最大子矩阵和)
题目链接 题意:给定N*N的矩阵,求该矩阵中和最大的子矩阵的和. 题解:把二维转化成一维,算下就好了. #include <cstdio> #include <cstring> ...
- poj 1050 To the Max 最大子矩阵和 经典dp
To the Max Description Given a two-dimensional array of positive and negative integers, a sub-rect ...
- hdu 1081 & poj 1050 To The Max(最大和的子矩阵)
转载请注明出处:http://blog.csdn.net/u012860063 Description Given a two-dimensional array of positive and ne ...
- [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 暴力,基础知识 难度:0
http://poj.org/problem?id=1050 设sum[i][j]为从(1,1)到(i,j)的矩形中所有数字之和 首先处理出sum[i][j],此时左上角为(x1,y1),右下角为(x ...
- POJ 1050 To the Max -- 动态规划
题目地址:http://poj.org/problem?id=1050 Description Given a two-dimensional array of positive and negati ...
- poj - 1050 - To the Max(dp)
题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 ...
随机推荐
- Frameset下的frame动态隐藏
技术涉及:html+Jquery 不多说直接上图:由于是 .netcore MVC Web应用对于大家来说不一致的话可供参考哦
- Java基本数据类型之间转换
一.自动类型转换 转换的过程自动发生规则:小——>大byte->short->int->long->float->double char类型识别为int,可以转成i ...
- BZOJ 1026:windy数(数位DP)
http://www.lydsy.com/JudgeOnline/problem.php?id=1026 1026: [SCOI2009]windy数 Time Limit: 1 Sec Memor ...
- redis源码笔记-内存管理zmalloc.c
redis的内存分配主要就是对malloc和free进行了一层简单的封装.具体的实现在zmalloc.h和zmalloc.c中.本文将对redis的内存管理相关几个比较重要的函数做逐一的介绍 参考: ...
- Ajax探讨
Ajax是一种技术方案,并不是什么新技术,Ajax请求使用XmlHttpRequest对象发送, XmlHttpRequest是一个浏览器接口,使得Javascript可以进行HTTP(S)通信. 最 ...
- Java学习笔记之---static
Java学习笔记之---static static不能修饰类,局部变量 (一)静态成员的特征 static+属性 静态属性 无论实例化出来多少个对象,都会共用同一块静态空间,类对象共享 静态成员从第 ...
- 1. UML软件设计模型图整理
UML建模 程序设计ER图 UML建模(一)---UserCase用例图 UML建模(二)--流程图 (程序框图) UML建模(三)--部署图 UML建模(四)--类图 UML用例图.流程图 (五)
- SpringCloud解析之Zuul(一)
本文基于Spring Cloud Edgware.SR6,Zuul版本1.3.1,解析Zuul的请求拦截机制,让大家对Zuul的原理有个大概的认识和了解.如有不对的地方,欢迎指正. spring bo ...
- xtrabackup 全量备份、恢复数据
1.全量备份 [root@localhost lib]##innobackupex --defaults-file=$defaults_file --user=$mysql_username --pa ...
- 好用的在线画图工具processon
ProcessOn是一款基于SaaS的前沿.高效线上作图工具,它将Visio.Xmind等专业作图工具搬到了"云端" 注册链接:https://www.processon.com/ ...