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. Know How And When To Use System.Message_Level To Control Messages In Oracle Forms

    SYSTEM.MESSAGE_LEVEL is used to control the messages that end users see when they use the Oracle For ...

  2. 读取excel到数据库里面

    //读取excel数据到dataTable里面 public DataTable ReadExcelDataToDataTable(string path) { DataTable dt = new ...

  3. ,2,liunx命令格式

    一.命令基本格式 ~用户的初始登录位置   /root  这个叫root用户的家目录,每个用户都有自己的家 超级用户的家是根目录,普通用户的家是home下的二级目录   :/home/uer1 pwd ...

  4. 用iconv指令解决utf8和gb18030编码间转换

    Linux显示在Windows编辑过的中文就会显示乱码是由于两个操作系统使用的编码不同所致.Linux下使用的编码是utf8,而Windows使用的是gb18030.  解决方案:  在终端中,进入到 ...

  5. 详解 ASP.NET异步

    在前文中,介绍了.NET下的多种异步的形式,在WEB程序中,天生就是多线程的,因此使用异步应该更为谨慎.本文将着重展开ASP.NET中的异步. [注意]本文中提到的异步指的是服务器端异步,而非客户端异 ...

  6. Maven测试

    生命周期阶段需要绑定到某个插件的目标才能完成真正的工作,test阶段正是与maven-surefire-plugin的test目标相绑定了,这是一个 内置的绑定. 在默认情况下,maven-suref ...

  7. STRUTS2 嵌套循环

    <!--begin 类目循环 --> <s:iterator value="categories" id='i' begin="0" step ...

  8. create table xxx as select 与 create table xxx like

    create table xxx )       | NO   | PRI | NULL    | auto_increment | | Name       | varchar() | NO   | ...

  9. html5实现GIF动画!

     代码如下: <!DOCTYPE html><html>    <head>        <meta charset="utf-8"&g ...

  10. Android广播BroadcastReceiver 一

    Android 系统里定义了各种各样的广播,如电池的使用状态,电话的接收和短信的接收,开机启动都会产生一个广播.当然用户也可以自定义自己的广播. 既然说到广播,那么必定有一个广播发送者,以及广播接收器 ...