POJ1050 To the Max 最大子矩阵
给定一个矩阵,求和最大的子矩阵。
将每一列的值进行累加,枚举起始行和结束行,然后就可以线性优化了 复杂度O(n^3)
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int N=301,M=301;
const int INF=0x3fffffff;
int sum[N][M],arr[M];
int find_max(int a[N][M],int n,int m)
{
if(n==0||m==0)return 0;
int i,j,up,down, ret=-INF;
memset(sum,0,sizeof(sum));
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
sum[i][j]=sum[i-1][j]+a[i][j];
arr[0]=0;
for(up=1;up<=n;up++)
for(down=up;down<=n;down++)
{
for(i=1;i<=m;i++)
arr[i]=arr[i-1]+(sum[down][i]-sum[up-1][i]);
int mini=0;
for(i=1;i<=m;i++)
{
ret=max(ret,arr[i]-mini);
mini=min(mini,arr[i]);
}
}
return max(0,ret);
} int main()
{freopen("t.txt","r",stdin);
int n;
while(scanf("%d",&n)!=EOF)
{
int num[N][M];
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&num[i][j]);
printf("%d\n",find_max(num,n,n));
}
return 0;
}
POJ1050 To the Max 最大子矩阵的更多相关文章
- [POJ1050]To the Max
[POJ1050]To the Max 试题描述 Given a two-dimensional array of positive and negative integers, a sub-rect ...
- (线性dp 最大子段和 最大子矩阵和)POJ1050 To the Max
To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 54338 Accepted: 28752 Desc ...
- [POJ1050]To the Max(最大子矩阵,DP)
题目链接:http://poj.org/problem?id=1050 发现这个题没有写过题解,现在补上吧,思路挺经典的. 思路就是枚举所有的连续的连续的行,比如1 2 3 4 12 23 34 45 ...
- [POJ1050]To the Max (矩阵,最大连续子序列和)
数据弱,暴力过 题意 N^N的矩阵,求最大子矩阵和 思路 悬线?不需要.暴力+前缀和过 代码 //poj1050 //n^4暴力 #include<algorithm> #include& ...
- poj 1050 To the Max(最大子矩阵之和,基础DP题)
To the Max Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 38573Accepted: 20350 Descriptio ...
- [POJ1050] To the Max 及最大子段和与最大矩阵和的求解方法
最大子段和 Ο(n) 的时间求出价值最大的子段 #include<cstdio> #include<iostream> using namespace std; int n,m ...
- 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> ...
- HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
随机推荐
- linux下硬盘分区、格式化以及文件管理系统
1.添加虚拟硬盘 (1)点击编辑虚拟机位置,然后点击添加 (2)点击添加硬盘 (3)点击下一步 (4)创建新虚拟磁盘并点击下一步 (5)指定磁盘容量并且点击下一步 (6)点击完成 2.系统分区 当 ...
- i2c精简总结
基本的i2c的编程包括:读数据,写命令,写数据 有关i2c的时序需要的话查看这里http://blog.csdn.net/qqliyunpeng/article/details/41511347 1. ...
- Qt 安装与配置记录
一 安装的时候得选一个Qt安装啊!!不要忘了展开这一项,而只安装Qt creator 展开之后会发现有很多版本,为了方便,选自带编译器mingw,就不需要麻烦的配置了 二 打开Qt creator 后 ...
- Uva - 11181 Probability|Given (条件概率)
设事件B为一共有r个人买了东西,设事件Ai为第i个人买了东西. 那么这个题目实际上就是求P(Ai|B),而P(Ai|B)=P(AiB)/P(B),其中P(AiB)表示事件Ai与事件B同时发生的概率,同 ...
- JDBC的存储过程
以下内容引用自http://wiki.jikexueyuan.com/project/jdbc/stored-procedure.html: 正如一个Connection对象创建了Statement和 ...
- 英特尔固态盘 说明书PDF
http://www.intel.cn/content/www/cn/zh/solid-state-drives/solid-state-drives-ssd.html
- qiniu
@RestControllerpublic class QiNiuController { private static final Logger logger = LoggerFactory.get ...
- Cts框架解析(2)-cts调试环境的搭建
上一篇文章中说了怎样在windows搭建cts以及执行cts进行測试.这篇文章来讲讲怎样在eclipse中配置源代码,进行debug调试. 下载 cts源代码地址:https://android.go ...
- 关于 TCP 并发连接的几个思考题与试验
http://blog.csdn.net/solstice/article/details/6579232
- 从头认识Spring-2.3 注解装配-@autowired(4)-required(2)
这一章节我们来继续具体讨论一下@autowired里面的參数required.在多构造器注入的情况. 1.domain(重点) 蛋糕类: package com.raylee.my_new_sprin ...