题目链接

Problem Description
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.

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.

 
Input
The input consists of an N x N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N 2 integers separated by whitespace (spaces and newlines). These are the N 2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].
 
Output
Output the sum of the maximal sub-rectangle.
 
Sample Input
4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1
-1
8 0 -2
 
Sample Output
15

题解:属于动态规划,不过可以暴力水过,列举所有可能找出最大和即可。

先求出前缀和,把数组变成第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(动态规划)的更多相关文章

  1. 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 ...

  2. HDU 1081 To The Max【dp,思维】

    HDU 1081 题意:给定二维矩阵,求数组的子矩阵的元素和最大是多少. 题解:这个相当于求最大连续子序列和的加强版,把一维变成了二维. 先看看一维怎么办的: int getsum() { ; int ...

  3. HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

  4. 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 ...

  5. Hdu 1081 To The Max

    To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. URAL 1146 Maximum Sum & HDU 1081 To The Max (DP)

    点我看题目 题意 : 给你一个n*n的矩阵,让你找一个子矩阵要求和最大. 思路 : 这个题都看了好多天了,一直不会做,今天娅楠美女给讲了,要转化成一维的,也就是说每一列存的是前几列的和,也就是说 0 ...

  7. ACM HDU 1081 To The Max

     To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  8. hdu 1081 To The Max(二维压缩的最大连续序列)(最大矩阵和)

    Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle ...

  9. HDU 1081 To The Max (dp)

    题目链接 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rect ...

随机推荐

  1. php的redis函数

    phpredis中文手册--<redis中文手册> php版   redis中文手册:http://readthedocs.org/docs/redis/en/latest/ 本文是参考& ...

  2. java list<string>集合 传递值给js的数组

    转载地址:http://blog.sina.com.cn/s/blog_611f65fd0100msc6.html. 1.Action 中代码              List result = n ...

  3. 使用pycharm+pyqt5 调取界面程序

    一.使用QtDesigner制作界面 1)打开的界面设计工具QtDesigner,如图: 2)新建窗体,选择Main Window: 3)分别在窗口添加如下控件,Calendar.3个pushButt ...

  4. android 开发心得杂记

    1.Android周刊关注. 2.4季Android性能优化典范 胡凯 http://mp.weixin.qq.com/s?__biz=MzA4NTQwNDcyMA==&mid=4021354 ...

  5. html5精品教程

    链接:http://pan.baidu.com/s/1ntr7yJ3 密码:7qvz链接:http://pan.baidu.com/s/1c0haxZM 密码:paok

  6. 在ubuntu14.04上安装openstack mitaka

    最近在工作环境安装部署了juno版本,在GE口测试网络性能不太满意,发现mitaka版本支持ovs-dpdk,于是抽时间安装实验一番. 参考官网的安装文档,先准备将mitaka版本安装好再配置ovs. ...

  7. Facebook 在page添加自己开发的app

    最初接到的需求是,在facebook主页中嵌入一个类似这样领取游戏礼包的页面. 一开始连facebook开发者中心在哪里都不知道,在万能的搜索框里面找到static html之类的第三方应用,但是这样 ...

  8. Python的加入!

    今天有幸领略了Python的风采. 真是好清新>_< 赶紧尝试一下. 好酷. 以后会在项目中使用

  9. Excel的 OleDb 连接串的格式(Provider=Microsoft.ACE.OLEDB)

    string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;data source=" + filePath + ";Exten ...

  10. yahoo给出的关于网站优化的建议

    1.使用CDN 内容分发服务器会根据用户的位置选择最近的服务器响应用户的请求,静态资源放在CDN的性能将提升20%左右. 2.设置Expires和Cache-Contral头 将静态资源的过期时间设置 ...