题目链接

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. C# typeof() 和 GetType()区别

    1.typeof(x)中的x,必须是具体的类名.类型名称等,不可以是变量名称. 2.GetType()方法继承自Object,所以C#中任何对象都具有GetType()方法,它的作用和typeof() ...

  2. [转]numpy线性代数基础 - Python和MATLAB矩阵处理的不同

    转自:http://blog.csdn.net/pipisorry/article/details/45563695 http://blog.csdn.net/pipisorry/article/de ...

  3. cuda编程(一)

    环境安装和例程运行 显卡主要有两家,ATI.NVIDIA,简称A卡和N卡.随着GPU计算能力的上升,采用GPU并行计算来加速的应用越来越多. Nvidia创立人之一,黄仁勋(Jen-Hsun Huan ...

  4. 我喜欢的快捷键 webstorm

    1.打开设置  ctrl+alt+s 2.重命名 rename  ctrl+r

  5. 何为PostgreSQL?

    PostgreSQL 是以加州大学伯克利分校计算机系开发的 POSTGRES, Version 4.2 为基础的对象关系型数据库管理系统(ORDBMS).POSTGRES 领先的许多概念只是在非常迟的 ...

  6. CodeForces 707A Brain's Photos

    简单题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #inclu ...

  7. ps遇到的问题及笔记

    1. ps软件图层面板中不显示缩略图 打开软件后,任意打开一个文件,再打开图层面板(直接按F7也可),点图层面板右上角的小三角后有一个新的面板出现,点下面的"面板选项"弹出一个面板 ...

  8. bzDemo

    <Public> <property name="Types"> <get/> </property> <method nam ...

  9. mac 搭建node 开发环境记录

    安装homebrew: enter 键 后 输入电脑密码 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/i ...

  10. node + express 资料

    https://www.youtube.com/watch?v=65a5QQ3ZR2g&list=PL55RiY5tL51oGJorjEgl6NVeDbx_fO5jR https://www. ...