题目链接

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. Spark Wordcount

    1.Wordcount.scala(本地模式) package com.Mars.spark import org.apache.spark.{SparkConf, SparkContext} /** ...

  2. Ubuntu14.04安装PHP5

    因为任务需要在Ubuntu14.04的server版本下安装PHP5,所以总结一下   使用root进行安装  要么在前面加上sudo进行安装. apt-get install php5-cgi ap ...

  3. 删除 CentOS7 更新后产生的多余的内核

    今天更新完系统的内核,重启电脑时发现突然多了一个启动项,想删除多余的启动项,在上网查找后,找到了下面的方法,经过测试,是完全可行的.自己写下来,以便以后用到.1.# uname -a 列出系统中正在使 ...

  4. xml动态修改 dom4j修改

    xml的动态修改需要传入的参数 xml的位置(tomcat中的发布位置).修改后的xml需要保存的位置(因为动态修改,所以建议和xml未修改前的位置相同).添加的节点的信息.或者修改的节点的信息 SA ...

  5. Unity3DGUI:Window

    #pragma strictvar winRect:Rect=Rect(30,100,150,200);var windowShow:boolean=true;function OnGUI () { ...

  6. hdu1031

    #include <cstdio> #include <cstdlib> struct element{ int id; double sa; }e[10000]; int c ...

  7. 介绍shell脚本

    简单的说shell就是一个包含若干行Shell或者Linux命令的文件.对于一次编写,多次使用的大量命令,就可以使用单独的文件保存下来, 以便日后使用.通常shell脚本以.sh为后缀.在编写shel ...

  8. gvim窗口根据gnome-terminal位置定位

    gvim启动位置固定的话容易挡到东西,所以写了一段vimscript根据gnome-terminal的位置启动gvim,这样被遮住的概率就一些了. fun! g:get_xterm_pos ()&qu ...

  9. ionic中$ionicPopover和$ionicModal

    Popover可点多个按钮弹出同一个浮动框但内容不一样.那想要在同一页面弹出不同的浮动框怎么办呢? 这事就用到了$ionicModal,他和$ionicPopover一样的用法. 请看图: html: ...

  10. for clean

    static(修饰变量方,法:静态块:静态内部类:静态导包) final transient 作用foreach 原理 volatile底层原理实现 集合LIST MAP SET 实现类的底层原理优, ...