To The Max

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

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
 
Source
这道题个人觉得是到灰常好的题目,适合不同层次的人做,求的是最大连续子矩阵和....
方法一:
简单的模拟即可。首先求出每个阶段只和,然后得到这个状态,然后进行矩阵规划,求最大值即可
代码浅显易懂:
 #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
using namespace std;
int arr[][];
int sum[][];
int main()
{
int nn,i,j,ans,temp;
// freopen("test.in","r",stdin);
while(scanf("%d",&nn)!=EOF)
{
ans=;
memset(sum,,sizeof(sum));
for(i=;i<=nn;i++)
{
for(j=;j<=nn;j++)
{
scanf("%d",&arr[i][j]);
sum[i][j]+=arr[i][j]+sum[i][j-]; //求出每一层逐步之和
}
}
for(i=;i<=nn;i++)
{
for(j=;j<=nn;j++)
{
sum[i][j]+=sum[i-][j]; //在和的基础上,逐步求出最大和
}
}
ans=;
for(int rr=;rr<=nn;rr++)
{
for(int cc=;cc<=nn;cc++)
{
for(i=rr;i<=nn;i++)
{
for(j=cc;j<=nn;j++)
{
temp=sum[i][j]-sum[i][cc-]-sum[rr-][j]+sum[rr-][cc-];
if(ans<temp) ans=temp;
}
}
}
}
printf("%d\n",ans);
}
return ;
}

方法二:

采用状态压缩的方式进行DP求解最大值......!

代码:

     /*基于一串连续数字进行求解的扩展*/
/*coder Gxjun 1081*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int nn=;
int arr[nn][nn],sum[nn][nn];
int main()
{
int n,i,j,maxc,res,ans;
// freopen("test.in","r",stdin);
while(scanf("%d",&n)!=EOF)
{
memset(sum,,sizeof(sum));
for(i=;i<=n;i++)
for(j=;j<=n;j++)
{
scanf("%d",&arr[i][j]);
sum[j][i]=sum[j][i-]+arr[i][j];
}
ans=;
for(i=;i<=n;i++){ for(j=i;j<=n;j++)
{
res=maxc=;
for(int k=;k<=n;k++)
{
maxc+=sum[k][j]-sum[k][i-];
if(maxc<=) maxc=;
else
if(maxc>res) res=maxc;
}
if(ans<res) ans=res;
}
}
printf("%d\n",ans);
}
return ;
}

hdu-------1081To The Max的更多相关文章

  1. HDU 1024:Max Sum Plus Plus(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Problem Description Now I think you ...

  2. HDU 1024 A - Max Sum Plus Plus DP + 滚动数组

    http://acm.hdu.edu.cn/showproblem.php?pid=1024 刚开始的时候没看懂题目,以为一定要把那n个数字分成m对,然后求m对中和值最大的那对 但是不是,题目说的只是 ...

  3. 【HDU 1003】 Max Sum

    题 题意 需要在o(n)时间内,求最大连续的子序列的和,及其起点和终点. 分析 一种方法是一边读,一边维护最小的前缀和 s[i] ,然后不断更新 ans = max(ans,s[j] - s[i]), ...

  4. HDU 1024 DP Max Sum Plus Plus

    题意:本题的大致意思为给定一个数组,求其分成m个不相交子段和最大值的问题. kuangbin专题. dp[i][j]=Max(dp[i][j-1]+a[j] , max( dp[i-1][k] ) + ...

  5. HDU 1003:Max Sum(DP,连续子段和)

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

  6. HDU 1024:Max Sum Plus Plus(DP,最大m子段和)

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

  7. HDU 1024:Max Sum Plus Plus 经典动态规划之最大M子段和

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

  8. HDU 1003:Max Sum

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

  9. HDU 1024 max sum plus

    A - Max Sum Plus Plus Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  10. hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行

    测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...

随机推荐

  1. SQL 汉字转换成拼音首字母 首字母查

    -- ============================================= -- 功能:汉字转换成拼音首字母 首字母查 -- ========================== ...

  2. ubuntu Linux 测试PHP却提示下载文件的解决办法

    ubuntu Linux 测试PHP却提示下载文件的解决办法   一般这种情况都是在刚刚开始配置环境时出现的, 输入 sudo a2enmod php5  看提示如果出现“$ This module ...

  3. 遇到问题-----JS中设置window.location.href跳转无效(在a标签里或这form表单里)

    问题情况 JS中设置window.location.href跳转无效 代码如下: ? 1 2 3 4 5 6 7 8 <script type="text/javascript&quo ...

  4. VBA中操作XML

    OFFICE2007之后使用了OpenXml标准(伟大的改变),定制文本级的Ribbon可以通过修改压缩包内的xml文件来实现. 先学习一下VBA中操作XML的方法 先引用Microsoft XML ...

  5. Hibernate之N+1问题

    什么是hibernate的N+1问题?先了解这样一个描述: 多个学生可以对应一个老师,所以student(n)---teacher(1).Stu类中有一个属性teacher.在hibernate配置文 ...

  6. HTTP协议(转自:小坦克博客)

    原文地址:http://www.cnblogs.com/TankXiao/archive/2012/02/13/2342672.html HTTP协议详解 当今web程序的开发技术真是百家争鸣,ASP ...

  7. jQuery的domReady

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. ubuntu 16.04 安装 QQ

    需要在Ubuntu 16.04下使用QQ,查找了一下,知乎的办法可行. 参考了:http://www.zhihu.com/question/20176925 与 http://www.zhihu.co ...

  9. 将图片转成base64 小工具

    工作需要使用,所以就做了一个小工具,方便使用 推荐使用 chrome,ff . 毕竟是个小工具方便自己使用而已,所以没有做浏览器兼容测试了! 代码如下,直接保存为 .html 打开即可 <!DO ...

  10. js数组知识

    js数组   shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift(); //a:[2,3, ...