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 ...
随机推荐
- PHP 数组使用之道
本文首发于 PHP 数组使用之道,转载请注明出处. 这个教程我将通过一些实用的实例和最佳实践的方式列举出 PHP 中常用的数组函数.每个 PHP 工程师都应该掌握它们的使用方法,以及如何通过组合使用来 ...
- ubuntu 安装python 编程环境
1. 安装python sudo add-apt-repository ppa:fkrull/deadsnakessudo apt-get updatesudo apt-get install pyt ...
- python的学习之路(三)
一.set集合#!/usr/bin/env python# *_*coding:utf-8 *_*# Author: harson old_dict = { "#1": {'hos ...
- BNUOJ 26229 Red/Blue Spanning Tree
Red/Blue Spanning Tree Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on HDU. ...
- [luoguP1328] 生活大爆炸版石头剪刀布(模拟)
传送门 虽然是模拟,但是我们可以用矩阵保存结果,来是其更加简便. ——代码 #include <cstdio> #include <iostream> ][] = {{, , ...
- [BZOJ1179] [Apio2009]Atm(tarjan缩点 + spfa)
传送门 题意 N个点M条边的有向图 每个点有点权 从某一个结点出发 问能获得的最大点权和 一个点的点权最多被计算一次 N<=500000 M<=500000 思路 先tarjan缩点,然后 ...
- 【BZOJ2440】完全平方数(莫比乌斯函数,容斥原理)
题意:求第k个无平方因子数 k<=10^9 思路: 感觉这东西和欧拉筛差不多……活到老学到老,退役前学点新知识也是好的 为什么二分答案的上界是2*n?连LYY都证不出来 话说约大爷一年之前就已经 ...
- codevs3728 联合权值
题目描述 Description 输入描述 Input Description 输出描述 Output Description 样例输入 Sample Input 样例输出 Sample Output ...
- springMVC @Value的使用
@Value 功能:将一个SpEL(SpEL:spring表达式类似于ognl)表达式结果映射到功能处理方法的参数上 例子:获取系统参数'java.vm.version'的值给到变量jvmVersio ...
- 洛谷——P1007 独木桥
P1007 独木桥 题目背景 战争已经进入到紧要时间.你是运输小队长,正在率领运输部队向前线运送物资.运输任务像做题一样的无聊.你希望找些刺激,于是命令你的士兵们到前方的一座独木桥上欣赏风景,而你留在 ...