hdu 1081 To The Max(二维压缩的最大连续序列)(最大矩阵和)
As an example, the maximal sub-rectangle of the array:
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
is in the lower left corner:
9 2
-4 1
-1 8
and has a sum of 15.
8 0 -2
题意:求n*n的矩阵里和最大的子矩阵。
最大连续序列和找最大和矩阵有着共通点,这里需要把一维的转化为二维的是关键
我们可以假定把每一行看作单个的元素,知道每个元素的值,那我们就能够将n列,看作有n个这样的压缩元素,那我们就是在这n个元素中找最大值
(当然压缩列也是可以的,这里我的代码写的是压缩行)
那么我们转化成用二维的sum[i][j]来维护前缀和,表示第i行前j个数的和
#include <cstdio>
#include <map>
#include <iostream>
#include<cstring>
#include<bits/stdc++.h>
#define ll long long int
#define M 6
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int n;
int sum[][];
int dp[];
int main(){
ios::sync_with_stdio(false);
while(cin>>n){
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
int t; cin>>t;
sum[i][j]=sum[i][j-]+t; //前缀和
}
int ans=-inf;
for(int i=;i<=n;i++)
for(int j=;j<=i;j++){ //二重循环枚举 把j~i压缩成一个点
int s=-inf;
memset(dp,,sizeof(dp));
for(int k=;k<=n;k++){ //找最大连续序列
dp[k]=max(dp[k-]+sum[k][i]-sum[k][j-],sum[k][i]-sum[k][j-]);
s=max(s,dp[k]);
}
ans=max(s,ans);
}
cout<<ans<<endl;
}
}
hdu 1081 To The Max(二维压缩的最大连续序列)(最大矩阵和)的更多相关文章
- lintcode 最长上升连续子序列 II(二维最长上升连续序列)
题目链接:http://www.lintcode.com/zh-cn/problem/longest-increasing-continuous-subsequence-ii/ 最长上升连续子序列 I ...
- HDU 1081 To The Max【dp,思维】
HDU 1081 题意:给定二维矩阵,求数组的子矩阵的元素和最大是多少. 题解:这个相当于求最大连续子序列和的加强版,把一维变成了二维. 先看看一维怎么办的: int getsum() { ; int ...
- hdu1081 DP类最大子段和(二维压缩+前缀和数组/树状数组计数)
题意:给出一个 n * n 的数字矩阵,问最大子矩阵和是多少. 由于和最长子段和问题类似,一开始想到的就是 DP ,一开始我准备用两个循环进行 DP ,对于每一个 (i,j) ,考察(i - 1,j) ...
- 【C语言】二维数组中的查找,杨氏矩阵
//二维数组中的查找,杨氏矩阵 //在一个二维数组中,每行都依照从左到右的递增的顺序排序.每列都依照从上到下递增的顺序排序. //请完毕一个函数.输入这种一个数组和一个数,推断数组中是否包括这个数. ...
- 【c语言】二维数组中的查找,杨氏矩阵在一个二维数组中,每行都依照从左到右的递增的顺序排序,输入这种一个数组和一个数,推断数组中是否包括这个数
// 二维数组中的查找,杨氏矩阵在一个二维数组中.每行都依照从左到右的递增的顺序排序. // 每列都依照从上到下递增的顺序排序.请完毕一个函数,输入这种一个数组和一个数.推断数组中是否包括这个数 #i ...
- hdu 1081 To The Max(dp+化二维为一维)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1081 To The Max Time Limit: 2000/1000 MS (Java/Others ...
- Hdu 1081 To The Max
To The Max Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Poj1050_To the Max(二维数组最大字段和)
一.Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is an ...
- HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
随机推荐
- [转帖]pfSense软路由系统的使用
图解pfSense软路由系统的使用(NAT功能) http://seanlook.com/2015/04/23/pfsense-usage/ 发表于 2015-04-23 | 更新于: 2015- ...
- Flutter的scope_model使用mixin语法报错
在pubspec.yaml同级目录下创建analysis_options.yaml文件,内容: # https://www.dartlang.org/guides/language/analysis- ...
- 爬虫 之Requests库的详细使用
1.什么是Requests? Requests是用Python语言编写的,基于urllib3来改写的,采用Apache2 Licensed 来源协议的HTTP库. 它比urllib更加方便,可以节约我 ...
- centos7之vm11添加网卡
需求 根据实际需求原来有一块网卡,现在需要新加一块网卡做集群. 1.在虚拟机添加一块网卡,开机后ip a查看是不是新加了一块网卡,下图是为了讲解,其实已经是做完的状态. 2.上满我们看到新加了一块网卡 ...
- 【转】MySQL sql_mode 说明(及处理一起 sql_mode 引发的问题)
1. MySQL 莫名变成了 Strict SQL Mode 最近测试组那边反应数据库部分写入失败,app层提示是插入成功,但表里面里面没有产生数据,而两个写入操作的另外一个表有数据.因为 inser ...
- RuntimeError: cryptography requires setuptools 18.5 or newer, please upgrade to a newer version of setuptool
setuptool 太老了,更新下: pip install --upgrade setuptools
- nginx 卸载后重新安装/etc/nginx配置文件没了,cannot open /etc/nginx/nginx.conf (No such file or directory)
sudo apt-get --purge remove nginx-common sudo apt-get --purge remove nginx* sudo apt-get autoremove ...
- linux 查看路由表
随便转载,保留出处:http://www.cnblogs.com/aaron-agu/ route –n
- ERROR org.hibernate.internal.SessionImpl - HHH000346: Error during managed flush [object references an unsaved transient instance - save the transient instance before flushing: cn.itcast.domain.Custom
本片博文整理关于Hibernate中级联策略cascade和它导致的异常: Exception in thread "main" org.hibernate.TransientOb ...
- oracle 触发器详情
Oracle PL/SQL编程之八: 把触发器说透 本篇主要内容如下: 8.1 触发器类型 8.1.1 DML触发器 8.1.2 替代触发器 8.1.3 系统触发器 8.2 创建触发器 8.2.1 触 ...