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

这道题其实就是求最大子段和,需要把原题数据变化一下,例如
0 -2 -7 0 
9 2 -6 2 
-4 1 -4 1 
-1 8 0 -2 
这个矩阵我选择的是
9 2
-4 1
-1 8
那么我还可以把这个选择过程看待为求数组 4 11 -10 1 的最大子段和,很显然是选择4 11,答案为15
那么4 11 -10 1是怎么来的呢,是我把2 3 4行数组组合成一个数组得来的
那么这道题的解法就出来了,不断枚举行区间,得到一个新数组,然后求最大子段和
#include"iostream"
#include"cstring"
using namespace std;
const int maxn=;
int b[maxn],a[maxn][maxn];
int n; void Init()
{
int t;
memset(a,,sizeof(a));
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
cin>>t;
a[i][j]=a[i-][j]+t;
}
}
} int main()
{
while(cin>>n)
{
Init();
int sum,ans,temp;
sum=;
ans=-;
int c=;
for(int i=;i<=n;i++)
for(int j=i;j<=n;j++)
{
sum=;
for(int k=;k<=n;k++)
{
temp=a[j][k]-a[i-][k];
sum+=temp;
if(sum>ans)
{
ans=sum;
}
if(sum<)
{
sum=;
}
}
}
cout<<ans<<endl;
}
return ;
}

集训第五周动态规划 F题 最大子矩阵和的更多相关文章

  1. 集训第五周 动态规划 B题LIS

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Des ...

  2. 集训第五周动态规划 H题 回文串统计

    Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A ...

  3. 集训第五周动态规划 D题 LCS

    Description In a few months the European Currency Union will become a reality. However, to join the ...

  4. 集训第五周动态规划 I题 记忆化搜索

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  5. 集训第五周动态规划 G题 回文串

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  6. 集训第五周动态规划 C题 编辑距离

    Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...

  7. 集训第五周 动态规划 K题 背包

    K - 背包 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  8. 集训第五周动态规划 J题 括号匹配

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  9. 集训第五周动态规划 E题 LIS

    Description The world financial crisis is quite a subject. Some people are more relaxed while others ...

随机推荐

  1. Unexpected EOF 远程主机强迫关闭了一个现有的连接 如何处理

    由于数据量的增大,调用接口的次数会增加. 当连续向目标网站发送多次request后,目标网站可能会认为是,恶意攻击. 于是会抛出requests异常. 测试代码: for i in range(200 ...

  2. php实现rpc简单的方法

    rpc是啥这不多解释,php扩展实现rpc yar是鸟哥的写的扩展,实现简单的rpc.比较很好理解 windows安装yar http://pecl.php.net/package/yar/2.0.4 ...

  3. 进击的Python【第九章】:paramiko模块、线程与进程、各种线程锁、queue队列、生产者消费者模型

    一.paramiko模块 他是什么东西? paramiko模块是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接. 先来个实例: import param ...

  4. IEEE Trans 2006 使用K-SVD构造超完备字典以进行稀疏表示(稀疏分解)

    K-SVD可以看做K-means的一种泛化形式,K-means算法总每个信号量只能用一个原子来近似表示,而K-SVD中每个信号是用多个原子的线性组合来表示的.    K-SVD算法总体来说可以分成两步 ...

  5. C# DataTable的详细用法[转]

    原文链接 1.新建表   private DataTable vsDt =new DataTable(); 2.清空表中内容(表中的列还在)  vsDt.Clear(); 3.清空表中的列   vsD ...

  6. E. The Values You Can Make 背包,同时DP

    http://codeforces.com/problemset/problem/688/E 题目需要在n个数中找出一个集合,使得这个集合的和为val,然后问这些所有集合,能产生多少个不同的和值. 题 ...

  7. (六)Mybatis总结之延迟加载

    应用场景: i.假如一个用户他有N个订单(N>=1000),那么如果一次性加载的话,一个用户对象的订单集合OrderList里面就会有1000多个Order的对象.计算:一个订单对象里面数据有多 ...

  8. for循环的两种写法哪个快

    结果如下: 其实工作中,也没有这么多数据需要遍历,基本上用foreach

  9. 锁 Lock、重入锁、写入锁

    ReentrantLock 重入锁 类似于synchronize 区别与写法上,在需要进行同步的代码部分加上锁定,但不要忘记最后一定要释放锁定, 不然会造成锁永远无法释放,其他线程永远进不来的结果.e ...

  10. UnixTime的时间戳的转换

    public static string XConvertDateTime(double unixTime) { System.DateTime time = System.DateTime.MinV ...