To The Max

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7620    Accepted Submission(s): 3692

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
 
 
  这道题目是求二维数组的最大子矩阵的和,最大子矩阵一定是在1~n行之间,所以要任选连续的几行压缩成一位数组求最大连续子段和。
代码:
#include <iostream>
#include <cstdio>
using namespace std;
#define N 105
int arr[N][N],b[N];
int dp(int *a,int m) //求一维数组的最大子段和
{
int i,sum,max;
sum = 0;
max = 0;
for(i=0; i<N; i++)
{
sum += a[i];
if(sum<0)
sum = 0;
if(sum>max)
max = sum;
}
return max;
}
int main()
{
int i,j,k,n,sum,max;
while(scanf("%d",&n)!=EOF)
{
for(i=0; i<n; i++)
for(j=0; j<n; j++)
scanf("%d",&arr[i][j]);
max = 0;
for(i=0; i<n; i++)
{
memset(b,0,sizeof(b));
for(j=i; j<n; j++)
{
for(k=0; k<n; k++)
b[k] += arr[j][k];
sum = dp(b,n);
if(sum>max)
max = sum;
}
}
printf("%d\n",max);
}
return 0;
}

  

 
 

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

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

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

  5. HDU 1081 To The Max(动态规划)

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

  6. ACM HDU 1081 To The Max

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

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

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

  8. HDU 1081 To The Max (dp)

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

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

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

随机推荐

  1. HDU 2243 考研路茫茫——单词情结(AC自动机+DP+快速幂)

    题目链接 错的上头了... 这题是DNA的加强版,26^1 +26^2... - A^1-A^2... 先去学了矩阵的等比数列求和,学的是第二种方法,扩大矩阵的方法.剩下就是各种模板,各种套. #in ...

  2. 最简单的html轮播图制作适合新手

    html代码 --------------------------------------------------------------------------------------------- ...

  3. MySQL函数操作数据库

    1.select语句查询信息(实现模糊查询) <form name="form1" method="post" action=""&g ...

  4. Memcache及telnent命令详解

    1.启动Memcache 常用参数 memcached 1.4.3 -p <num>      设置端口号(默认不设置为: 11211) -U <num>      UDP监听 ...

  5. iOS CoreAnimation 核心动画

    一 介绍 一组非常强大的动画处理API 直接作用在CALAyer上,并非UIView(UIView动画) CoreAnimation是所有动画的父类,但是不能直接使用,应该使用其子类 属性: dura ...

  6. Ubuntu下安装R语言和开发环境

    [简介]R是用于统计分析.绘图的语言和操作环境.R是属于GNU系统的一个自由.免费.源代码开放的软件,它是一个用于统计计算和统计制图的优秀工具. [R语言的安装]官网:https://www.r-pr ...

  7. javascript循环和数组的基础练习

    九九乘法表 <script> //外层循环行数 for(var i=0; i<=9; i++){ //内曾循环控制每一行的列数 for(var j=0;j<=i; j++){ ...

  8. iOS Crash日志

    Understanding Crash Reports on iPhone OS https://developer.apple.com/videos/wwdc/2010/?id=317 http:/ ...

  9. html css 样式继承的问题

    body 设置css中可以继承的属性:letter-spacing.word-spacing.white-space.line-height.color.font等 但有时,body的样式,不能在有的 ...

  10. Tomcat catalina.out日志使用log4j按天分割

    由于tomcat catalina.out日志不会自动分割, 一.日志分割所需包在附近中 1. 压缩包中有三个jar包: log4j-1.2.16.jar tomcat-juli-adapters.j ...