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列的和,这样不用每次列举的时候都求和。
然后两个for循环列举列,两个for循环列举行,具体还是看代码吧。
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <map>
#define PI acos(-1.0)
#define ms(a) memset(a,0,sizeof(a))
#define msp memset(mp,0,sizeof(mp))
#define msv memset(vis,0,sizeof(vis))
using namespace std;
//#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
ios::sync_with_stdio(false);
int mp[][];
int n;
while(cin>>n)
{msp;
for(int i=; i<n; i++)
for(int j=; j<n; j++)
{
cin>>mp[i][j];
if(j!=)mp[i][j]=mp[i][j]+mp[i][j-];
}
int cnt=,maxx=-1e9;
for(int x1=; x1<n; x1++)
for(int x2=x1; x2<n; x2++)
{
for(int y1=; y1<n; y1++)
{cnt=;
for(int y2=y1; y2<n; y2++)
{
if(x1!=x2)cnt+=mp[y2][x2]-mp[y2][x1];
else cnt+=mp[y2][x2];
maxx=max(maxx,cnt);
}}
}
printf("%d\n",maxx);}
return ;
}
HDU 1081 To The Max(动态规划)的更多相关文章
- 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【dp,思维】
HDU 1081 题意:给定二维矩阵,求数组的子矩阵的元素和最大是多少. 题解:这个相当于求最大连续子序列和的加强版,把一维变成了二维. 先看看一维怎么办的: int getsum() { ; int ...
- HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- dp - 最大子矩阵和 - HDU 1081 To The Max
To The Max Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=1081 Mean: 求N*N数字矩阵的最大子矩阵和. ana ...
- Hdu 1081 To The Max
To The Max Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- URAL 1146 Maximum Sum & HDU 1081 To The Max (DP)
点我看题目 题意 : 给你一个n*n的矩阵,让你找一个子矩阵要求和最大. 思路 : 这个题都看了好多天了,一直不会做,今天娅楠美女给讲了,要转化成一维的,也就是说每一列存的是前几列的和,也就是说 0 ...
- ACM HDU 1081 To The Max
To The Max Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- hdu 1081 To The Max(二维压缩的最大连续序列)(最大矩阵和)
Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle ...
- HDU 1081 To The Max (dp)
题目链接 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rect ...
随机推荐
- nginx(2)
上一篇: nginx(1) 负载均衡: linux集群的一种常见方式,即由多台服务器组成一个服务器集合实现某个特定需求,其中每台服务器都是等价的,从而实现负载均摊的目的. 反向代理: 是指以代理服务器 ...
- Arch安装KDE5
plasma desktop Install the plasma-meta meta-package or the plasma group. Alternatively, for a more m ...
- iOS 发布项目到CocoaPods其实没那么复杂😆
首先大家必须要了解一下CocoaPods (如果你连CocoaPods是啥都不知道可以不用往下看了
- Ubuntu14.04安装PHP5
因为任务需要在Ubuntu14.04的server版本下安装PHP5,所以总结一下 使用root进行安装 要么在前面加上sudo进行安装. apt-get install php5-cgi ap ...
- <poj - 2139> Six Degrees of Cowvin Bacon 最短路径问题 the cow have been making movies
本题链接:http://poj.org/problem?id=2139 Description: The cows have been making movies lately, so the ...
- 分布式缓存Memcached/memcached/memcache详解及区别
先来解释下标题中的三种写法:首字母大写的Memcached,指的是Memcached服务器,就是独立运行Memcached的后台服务器,用于存储缓存数据的“容器”.memcached和memcache ...
- C# IDisposable的理解
C#里可以嵌入非托管代码,这就涉及到了这些代码资源的释放.以前总是看到别人的代码里那么写,也没有好好想想为什么,今天看了书,总结一下. 资源释放分为两种: 托管的 非托管的 两者的释放方式不一致: 没 ...
- 【IE6的疯狂之五】div遮盖select的解决方案
IE6以及一下版本下,选择框Select会覆盖Div中的内容一般情况下,可以将显示的内容放到Iframe中,然后再显示框架内的内容.由于Iframe的可以显示在Select上层,就可以解决这个问题.不 ...
- 前端必知的ajax
简介 异步交互 此篇只介绍部分方法,想了解更多就猛戳这里 1. load( url, [data], [callback] ) :载入远程 HTML 文件代码并插入至 DOM 中. url (Stri ...
- 第八章 监控 Zabbix
1.1 YUM部署 Zabbix 2.4 1. 前期准备(省略) l Yum源的配置 cd /etc/yum.repos.d/; mkdir old;mv ./* old/ rpm -Uvh ...