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 ...
随机推荐
- python爬虫入门02:教你通过 Fiddler 进行手机抓包
哟~哟~哟~ hi起来 everybody 今天要说说怎么在我们的手机抓包 通过 python爬虫入门01:教你在Chrome浏览器轻松抓包 我们知道了 HTTP 的请求方式 以及在 Chrome 中 ...
- 三菱PLC FB库函数调用方法 (Gx Work2版本)
本文以 GxWorks2 软件为例 1.新建使用标签项目的工程文件 2.从其它库所在工程项目中导入库 3.选择库文件及FB功能块 4.插入FB功能块调用
- GitHub总结
1) 工作原理 2) 工作流程 clone资源到本地 更新本地资源 新增或修改clone的资源 查看状态 资源推送回github
- 九度oj 题目1056:最大公约数
题目1056:最大公约数 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8068 解决:5317 题目描述: 输入两个正整数,求其最大公约数. 输入: 测试数据有多组,每组输入两个正整数. ...
- MVC系统学习6—Filter
Mvc的过滤器是特性类,可以使我们在执行Action之前,执行Action之后,执行Action发生异常时,编写相关的处理代码实现某些逻辑.下面是四个基本的Filter接口. 上面这四个基本的Filt ...
- [luoguP1086] 花生采摘(模拟)
传送门 模拟... 代码 #include <cstdio> #include <iostream> #include <algorithm> #define ab ...
- noip模拟赛 斐波那契
分析:暴力分有90,真良心啊. a,b这么大,连图都建不出来,肯定是有一个规律.把每个点的父节点写出来:0 1 1 12 123 12345 12345678,可以发现每一个循环的长度刚好是斐波那契数 ...
- noip模拟赛 单词
分析:这道题真心难想.最主要的是怎么样不重复. 为了不重复统计,把所有符合条件的单词分成两类,一类是某些单词的前缀,一类是 不是任何单词的前缀.涉及到前缀后缀,维护两个trie树,处理3个数组a,b, ...
- 【BZOJ2818】Gcd(莫比乌斯反演,欧拉函数)
题意:给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对 1<=N<=10^7 思路:莫比乌斯反演,同BZOJ2820…… ; ..max]of ...
- Codeforces913E. Logical Expression
现有串x=11110000,y=11001100,z=10101010,通过这三个串只用与或非三种操作到达给定的串,优先级非>或>与,可以加括号,问表达式最短的里面字典序最小的是谁,有&l ...