To the Max
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 39913   Accepted: 21099

Description

Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*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 * 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

这是hdu 1003的拓展,知道了在一维数组中如何求最大连续子段和,那么这题就是扩展到二维数组中,让我们求出子矩阵最大的和,我们可以这样考虑,我们把同行不同列(或者同列不同行)的加起来,比如i行,j行,i,j两行之间的数字组成了一个矩阵,我们把i行到j行之间同列的数组元素加起来按照列号组成一个一维数组,这样我们只需要利用最大子段和的知识找出这个数组的最大连续和,这个和就是我们要求的那个子矩阵最大和! 可以说, i和j行定义了子矩阵高度,一维数组最大子段和连续的长度定义了子矩阵的宽度,OK,代码。
 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; int temp[],n;
int cal() //
{
int Max = temp[];
int sum = temp[];
for(int i = ;i<n;i++)
{
if(sum+temp[i]<temp[i])
sum = temp[i];
else
sum+=temp[i];
if(Max<sum)
Max = sum;
}
return Max;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
int a[][];
for(int i = ;i<n;i++)
for(int j = ;j<n;j++)
scanf("%d",&a[i][j]);
int Max = ;
for(int i = ;i< n;i++) //i是起始行
{
for(int j =i ;j<n;j++) //j是终止行
{
memset(temp,,sizeof(temp));
for(int m = ;m<n;m++) //固定列,注意是行在变
{
for(int k =i ;k<=j;k++) //累加i起始行,j终止行中间的同列的数据
temp[m]+=a[k][m];
}
int MaxTemp = cal();
if(MaxTemp>Max)
Max = MaxTemp;
} }
printf("%d\n",Max);
}
return ;
}

事实证明我蠢了,后来看到nyoj这题的最优程序解答,在处理第i行到j行同列相加上面处理的很好,利用输入时候进行累加,然后做减法,直接减掉了一层循环

nyoj 的版本


 #include<iostream>
#include<cstring>
using namespace std;
#define N 110
int a[N][N];
int b[N];
int main()
{
int n,r,c;
cin>>n;
while(n--)
{
cin>>r>>c;
for(int i=;i<=r;++i)
for(int j=;j<=c;++j)
{
cin>>a[i][j];
a[i][j]+=a[i-][j];
}
int max=a[][];
for(int i=;i<=r-;++i)
for(int j=i+;j<=r;++j)
{
memset(b,,sizeof(b));
for(int k=;k<=c;++k)
{
if(b[k-]>=)
b[k]=b[k-]+a[j][k]-a[i][k];
else
b[k]=a[j][k]-a[i][k];
if(max<b[k])
max=b[k];
}
}
cout<<max<<endl;
}
}

还有一种没有用这种求和的方法,但是是先求第一行最大子段和,再求第一行跟第二行合起来的最大子段和,,再求第一到第三合起来的最大子段和,以此类推,直到求出整个矩阵的合起来的最大子段和,最后就是我们需要的解 。

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; int temp[],n;
int cal() //最大子段和
{
int Max = temp[];
int sum = temp[];
for(int i = ;i<n;i++)
{
if(sum+temp[i]<temp[i])
sum = temp[i];
else
sum+=temp[i];
if(Max<sum)
Max = sum;
}
return Max;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
int a[][];
for(int i = ;i<n;i++)
for(int j = ;j<n;j++)
scanf("%d",&a[i][j]);
int Max = ;
for(int i = ;i< n;i++)
{
memset(temp,,sizeof(temp));
for(int j =i ;j<n;j++)
{
for(int k = ;k<n;k++)
temp[k]+=a[j][k];
int t = cal();
if(t>Max)
Max =t;
} }
printf("%d\n",Max);
}
return ;
}

poj1050(nyoj104 zoj1074)dp问题的更多相关文章

  1. [POJ1050]To the Max(最大子矩阵,DP)

    题目链接:http://poj.org/problem?id=1050 发现这个题没有写过题解,现在补上吧,思路挺经典的. 思路就是枚举所有的连续的连续的行,比如1 2 3 4 12 23 34 45 ...

  2. ZOJ1074 (最大和子矩阵 DP)

    F - 最大子矩阵和 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u   Descri ...

  3. poj1050 dp动态规划

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

  4. POJ1050【DP】

    题意: 求一个最大子矩阵和. 思路: 枚举行区间,然后求一个最大子序列和. 贴一发挫code- #include <iostream> #include <cstdio> #i ...

  5. POJ1050(dp)

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 46788   Accepted: 24774 Desc ...

  6. (线性dp 最大子段和 最大子矩阵和)POJ1050 To the Max

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54338   Accepted: 28752 Desc ...

  7. poj1050 To the Max(降维dp)

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 49351   Accepted: 26142 Desc ...

  8. DP总结 ——QPH

    常见优化 单调队列 形式 dp[i]=min{f(k)} dp[i]=max{f(k)} 要求 f(k)是关于k的函数 k的范围和i有关 转移方法 维护一个单调递增(减)的队列,可以在两头弹出元素,一 ...

  9. POJ1050:To the max

    poj1050:http://poj.org/problem?id=1050 * maximum-subarray 问题的升级版本~ 本题同样是采用DP思想来做,同时有个小技巧处理:就是把二维数组看做 ...

随机推荐

  1. Python列表及元组

    列表(list)是Python以及其他语言中最常用到的数据结构之一.Python使用使用中括号[ ] 来解析列表.列表是可变的(mutable)——可以改变列表的内容. 如 list1 = ['phy ...

  2. struts2复习(五)拦截器总结

    1. 拦截器(Interceptor): 拦截器是Struts2的核心,Struts2的众多功能都是通过拦截器来实现的. 2. 拦截器的配置 1)编写实现Interceptor 接口的类.  2)在s ...

  3. docker 实战---使用oracle xe作为开发数据库(六)

    oracle作为oltp的大佬,非常多行业应用都会用到它.那么在开发的过程中就不可避免的要使用oracle数据库,oracle数据库的版本号有好多,当中express版本号是免费的开发版.它的主要限制 ...

  4. poj1184 聪明的打字员(BFS剪枝)

    http://poj.org/problem?id=1184 用字符串s存下数字,并把光标位置做一个字符加到s末尾,用map做标记状态是否出现过,然后bfs即可. 不剪枝是过不了的,考虑的两种交换操作 ...

  5. Kali下使用libheap

    Kali下使用libheap 在github上,可以libheap用来帮助调试堆溢出.链接见:https://github.com/cloudburst/libheap 但是最后一次更新在一年前了,我 ...

  6. LINQ 基本子句之三 let

    let子句,可以作为临时变量储存表达式的结果,但是let子句一旦初始化后无法再次进行更改. 1. static void Main(string[] args) { string[] names = ...

  7. 76 bytes for faster jQuery

    转载自http://james.padolsey.com/javascript/76-bytes-for-faster-jquery/ 作者JAMES PADOLSEY 在我们平时使用JQuery,调 ...

  8. MVC不错的学习资料

    MVC不错的学习资料: http://www.cnblogs.com/darrenji/

  9. 深入char、varchar、text和nchar、nvarchar、ntext的区别详解

    很多开发者进行数据库设计的时候往往并没有太多的考虑char, varchar类型,有的是根本就没注意,因为存储价格变得越来越便宜了,忘记了最开始的一些基本设计理论和原则,这点让我想到了现在的年轻人,大 ...

  10. 网易云数据结构- Maximum Subsequence Sum

    题目 题目地址 思路 显然是最大子列和的进化版,那就先思考下经典的最大子列和.这也是道思维题,啥算法也没用到,全是思维技巧,真心不知道考试遇到这种题该怎么办了. 存放答案的一个类,我把它看成一个袋子, ...