最大子矩阵和(二维矩阵转一维DP)
题目描述
蒜头君拿到了一个矩阵,他想知道其中的最大非空子矩阵和是多少。
输入格式
第一行输入两个整数 n,m代表这个矩阵的行数和列数。接下来n行,每行m个整数 ai1,ai2,ai3⋯aim。(1≤m,n≤400,−109≤aij≤109)
输出格式
输出一个整数,代表最大非空子矩阵和,占一行。
样例输入
-
- -
-
样例输出
与最大子段和类似,不过上升到了二维,那么我们可以通过枚举上下边界将题目转化为一维的,通过前缀和又可以快速算出固定一列从一行到某一行所有数的和。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
const double PI = acos(-);
const double eps =1e-;
#define Bug cout<<"---------------------"<<endl
const int maxn=1e5+;
using namespace std; LL sum[][];//sum[j][i]表示第j列前i行的前缀和
LL MAX=-INF;//别忘了初始化 int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
LL x;
scanf("%lld",&x);
MAX=max(MAX,x);
sum[j][i]=sum[j][i-]+x;
}
}
if(MAX>=)//如果MAX小于零则直接输出即可
{
for(int i=;i<=n;i++)//i为矩阵上边界
{
for(int k=i;k<=n;k++)//k为矩阵下边界
{
long long t=;//和
for(int j=;j<=m;j++)//一维最大子段和(利用列的前缀和快速计算)
{
if(t+sum[j][k]-sum[j][i-]<) t=;
else t+=sum[j][k]-sum[j][i-];
MAX=max(MAX,t);
}
}
}
}
printf("%lld\n",MAX);
return ;
}
-
最大子矩阵和(二维矩阵转一维DP)的更多相关文章
- 51nod-1503 猪和回文 - 二维矩阵上的dp
题目链接 一只猪走进了一个森林.很凑巧的是,这个森林的形状是长方形的,有n行,m列组成.我们把这个长方形的行从上到下标记为1到n,列从左到右标记为1到m.处于第r行第c列的格子用(r,c)表示. 刚开 ...
- 多维矩阵转一维数组(c++)【转载】
在由二维矩阵转为一维数组时,我们有两种方式:以列为主和以行为主. 以列为主的二维矩阵转为一维数组时,转换公式为: index=column+row×行数 以行为主的二维矩阵转为一维数组时,转换公式为: ...
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- NYOJ 104 最大子矩阵(二维DP)
最大和 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描写叙述 给定一个由整数组成二维矩阵(r*c),如今须要找出它的一个子矩阵,使得这个子矩阵内的全部元素之和最大,并把这个 ...
- C语言经典算法 - 多维矩阵转一维矩阵的代码
下边内容内容是关于C语言经典算法 - 多维矩阵转一维矩阵的内容,应该能对码农也有好处. #include <stdio.h>#include <stdlib.h>int mai ...
- LeetCode(74):搜索二维矩阵
Medium! 题目描述: 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 ...
- [Leetcode] search a 2d matrix 搜索二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] 74. Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- CUDA学习之一:二维矩阵加法
今天忙活了3个小时,竟然被一个苦恼的CUDA小例程给困住了,本来是参照Rachal zhang大神的CUDA学习笔记来一个模仿,结果却自己给自己糊里糊涂,最后还是弄明白了一些. RZ大神对CUDA关于 ...
随机推荐
- ROS常用库(一) fake_localization
wiki是最好的学习资料,以下直接参考了wiki官网.另外po出官网网址,建议英语较好的朋友之接看原版 http://wiki.ros.org/fake_localization 概述 fake_lo ...
- R函数
1. sd() 求一组数据的标准差 > x = rep(1,15) > x [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 > sd(x) [1] 0 2.var ...
- servlet中urlpatterns注意事项
在servlet中, @WebServlet(urlPatterns="/newsAdd")接收 resp.sendRedirect("/wedding/houtai/N ...
- plsql和navicat连接远程oracle(易错点)
plsql和navicat连接远程oracle,只需要安装oracle客户端即可.注意此处是oracle客户端(Instant Client),并不是oracle数据库. oracle客户端下载地址: ...
- python-模块安装
首先到这个网址https://www.lfd.uci.edu/~gohlke/pythonlibs/ 找到自己想要用的模块,然后下载下来, 回到桌面找到文件所在位置进入cmd中, pip instal ...
- C++ #if 1
当注释掉大块代码时,使用"#if 0"比使用"/**/"要好,因为用"/**/"做大段的注释要防止被注释掉的代码中有嵌套的"/** ...
- 02-NVIDIA Jetson TX2 通过JetPack 3.1刷机完整版(踩坑版)
未经允许,不得擅自改动和转载 文 | 阿小庆 2018-1-20 本文继第一篇文章:01-NVIDIA Jetson TX2开箱上电显示界面 TX2 出厂时,已经自带了 Ubuntu 16.04 系统 ...
- 【pwnable.kr】 shellshock
pwnable从入门到放弃,第五题. ssh shellshock@pwnable.kr -p2222 (pw:guest) 这题主要涉及了一个关于bash的CVE漏洞. 首先还是下载源代码审计一下, ...
- web嵌入到原生的app里需要注意的事项
1.https://www.cnblogs.com/shimily/articles/7943370.html 2.https://www.cnblogs.com/stoneniqiu/p/60771 ...
- PAT Advanced 1151 LCA in a Binary Tree (30) [树的遍历,LCA算法]
题目 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both ...