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合并两个select查询

    现有2个查询,需要将每个查询的结果合并起来(注意不是合并结果集,因此不能使用union),可以将每个查询的结果作为临时表,然后再从临时表中select所需的列,示例如下:   SELECT get.d ...

  2. 查看博客模板的css代码

    1.可以去模板列表里选择一个模板 http://www.cnblogs.com/Skins.aspx 目前使用的模板是http://www.cnblogs.com/SkinUser.aspx?Skin ...

  3. 关于MySQL数据库如何按时间查询

    这里做了几个测试 select * from simingpai where TIMESTAMP(createTime) >= '2015-9-6'; select * from simingp ...

  4. Nmap使用指南(1)

    Nmap是一款开源免费的网络发现(Network Discovery)和安全审计(Security Auditing)工具.软件名字Nmap是Network Mapper的简称.Nmap最初是由Fyo ...

  5. javascript权威指南笔记--javascript语言核心(一)

    1.javascript的数据类型分为两类:原始类型和对象类型. 原始类型包括字符串.数字.布尔值.null.undefined. 对象是属性的集合,每个对象都由“名/值”对构成.数组和函数是特殊的对 ...

  6. js操作cookie,实现登录密码保存 [转]

    转自:http://blog.csdn.net/zyujie/article/details/8727828 ( 谢谢博主了) js操作cookie,实现登录密码保存.cookie的存放方式是以键值对 ...

  7. JSP学习——原理

    JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术.   JSP这门技术的最大的特点在于,写jsp就像在写html,但它相比 ...

  8. mysql 求时间段平均值

    考虑下面的需求,在一段时间内,间隔一段时间,取一个平均值,把所有的平均值取出来,怎么办?思路:在存储过程中,拼接sql语句.根据起始时间和结束时间,while循环每次加一段时间.DROP PROCED ...

  9. 微信开发时遇到的UrlConnection乱码的问题

    昨天做一个微信的模板消息推送的功能,功能倒是很快写完了,我本地测试微信收到的推送消息是正常的,但是一部署到服务器后微信收到的推送消息就变成乱码了. 为了找到原因,做了很多测试,查了一下午百度,最后得出 ...

  10. go循环

    Go语言里的For循环语句 更多 0 golang   package main import "fmt" func main() { sum := 0 for i := 0; i ...