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. vi及缩进设置

    set autoindent,把当前行的对起格式应用到下一行: set smartindent,智能的选择对起方式: set tabstop=4,设置tab键为4个空格: set shiftwidth ...

  2. 如何在iOS 7.0中隐藏状态栏

    使用Cordova做了一个小项目,在原来iOS6的时候显示挺好,升级为iOS7后,每次App启动后都会显示状态栏,而且状态栏和App的标题栏重叠在一起,非常难看,因此需要将状态栏隐藏起来.   首先, ...

  3. h5移动开发css

    最近刚开始做移动端的开发,接触到很多新的东西,很荣幸(*^__^*) , 下面我们开始正式介绍最近新接触到的属性啦,一起进步: 1.点击按钮等会产生阴影,可设置这个属性:-webkit-tap-hig ...

  4. Codeforces Round #199 (Div. 2)

    A.Xenia and Divisors 题意:给定N个数,每个数的取值范围为1-7,N是3的倍数,判定是否能够恰好将N个数分成若干三元组,使得一个组中的元素a,b,c满足 a < b < ...

  5. Android spinner控件

    spinner控件是Android中下拉控件,现在介绍它两种用法.第一种,从资源文件中获取下拉值:第二种,从代码中获取下拉值. 第一种,首先要在资源文件中把值写好: <?xml version= ...

  6. Java可视化AWT

    AWT 总体上Swing组件替代了绝大部分AWT组件,对AWT图形用户界面编程有极好的补充和加强. package ch11; import java.awt.*; /** * Created by ...

  7. spring data jpa 创建方法名进行简单查询

    版权声明:本文为博主原创文章,未经博主允许不得转载. spring data jpa 可以通过在接口中按照规定语法创建一个方法进行查询,spring data jpa 基础接口中,如CrudRepos ...

  8. 转!!常用的4种动态网页技术—CGI、ASP、JSP、PHP

    1.CGI   CGI(Common Gateway Interface,公用网关接口)是较早用来建立动态网页的技术.当客户端向Web服务器上指定的CGI程序发出请求时,Web服务器会启动一个新的进程 ...

  9. 实体类实现Parcelable(包含boolean类型)

    实体类实现Parcelable接口需要实现方法: public ExtSignClockEntity(Parcel in) { timeMess = in.readString(); repeatMe ...

  10. icp算法的一些参考资料

    1.综述:迭代最近点算法综述,介绍了svd分解和四元数法,其中 svd法:http://blog.csdn.net/kfqcome/article/details/9358853 四元数法:http: ...