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

解题思路:
最大子矩阵,首先一行数列很简单求最大的子和,我们要把矩阵转化成一行数列,
就是从上向下在输入的时候取和,a[i][j]表示在J列从上向下的数和,这样就把一列转化成了一个点,
再用双重,循环,任意i行j列开始的一排数的最大和,就是最终的最大和
注意:如果m<0,则就不需要继续加了
程序代码:
 #include <cstdio>
#include <cstring>
using namespace std;
int a[][];
int main()
{
int n,c;
while( scanf("%d",&n)==)
{
memset(a,,sizeof(a));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
scanf("%d",&c);
a[i][j]=a[i-][j]+c;
}
int sum=;
for(int i=;i<=n;i++)
for(int j=i;j<=n;j++)
{
int m=;
for(int k=;k<=n;k++)
{
int t=a[j][k]-a[i-][k];
m+=t;
if(m<) m=;
if(sum<m) sum=m;
}
} printf("%d\n",sum);
}
return ;
}

动态规划——F 最大矩阵和的更多相关文章

  1. 斐波那契数列第N项f(N)[矩阵快速幂]

    矩阵快速幂 定义矩阵A(m*n),B(p*q),A*B有意义当且仅当n=p.即A的列数等于B的行数. 且C=A*B,C(m*q). 例如: 进入正题,由于现在全国卷高考不考矩阵,也没多大了解.因为遇到 ...

  2. 2D-2D:对极几何 基础矩阵F 本质矩阵E 单应矩阵H

    对极约束 \[ \boldsymbol{x}_{2}^{T} \boldsymbol{F} \boldsymbol{x}_{1}=\boldsymbol{0} \quad \hat{\boldsymb ...

  3. 动态规划(DP),最大矩阵和

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=74 http://poj.org/problem?id=1050 解题 ...

  4. 集训第五周动态规划 F题 最大子矩阵和

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

  5. SCNU 2015ACM新生赛决赛【F. Oyk闯机关】解题报告

            题目大意:一个$N$$\times$$N$的阵列,每个格子有$X_{ij}$个调和之音,若每次只能选择走右边或下边,从左上角出发走到右下角,问最多能收集到多少个调和之音?       ...

  6. 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式

    矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b     *     A B   =   a*A+b*C  a*c+b*D c d     ...

  7. Tyvj P3119 核电站问题 动态规划

    题目:http://tyvj.cn/p/3119 P3119 核电站问题 时间: 1000ms / 空间: 65536KiB / Java类名: Main 描述 一个核电站有N个放核物质的坑,坑排列在 ...

  8. Bzoj 1042: [HAOI2008]硬币购物 容斥原理,动态规划,背包dp

    1042: [HAOI2008]硬币购物 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1747  Solved: 1015[Submit][Stat ...

  9. HDU 1505 Largest Rectangle in a Histogram &amp;&amp; HDU 1506 City Game(动态规划)

    1506意甲冠军:给你一个连续的直方图(拼贴底部长度1).求连续基质区. 对每一个直方图,分别向左向右进行扩展. #include<cstdio> #include<stdlib.h ...

随机推荐

  1. CSS定位:相对定位、绝对定位和固定定位(relative absolute fixed)

    相对定位:position:relative; 不脱离文档流,参考自身静态位置通过top,bottom,left,right定位,并且可通过z-index进行层次分级. 绝对定位:position:a ...

  2. The windows PowerShell snap-in 'Microsoft.Crm.PowerShell' is not installed on this computer

    加载PowerShell插件时出现以下错误: The windows PowerShell snap-in 'Microsoft.Crm.PowerShell' is not installed on ...

  3. CentOS下的svn强制用户提交时写日志

    问题:在项目提交时候不写日志,在后期查看修改历史时需要对比版本才知道提交原因.解决方案:在svn服务端通过hooks在提交时强制要求写日志.#!/bin/shREPOS="$1"T ...

  4. Vijos1523 NOI2002 贪吃的九头龙 树形dp

    思路不算很难,但细节处理很麻烦 前面建图.多叉转二叉,以及确定dp处理序列的过程都是套路,dp的状态转移过程以注释的形式阐述 #include <cstdio> #include < ...

  5. Centos7搭建php+mysql环境(整理篇)

    终于将mysql+php环境搭建成功,将之前的整理一下,环境:centos7,本机IP:192.168.1.24,数据库用户名及密码都设为root,测试文件路径:/var/www/html 1.取消c ...

  6. 关于alarm函数

    #include<unistd.h> #include<signal.h> void handler() { printf("Hello\n"); sign ...

  7. PAT - 基础 - 最大公约数和最小公倍数

    题目: 本题要求两个给定正整数的最大公约数和最小公倍数. 输入格式: 输入在一行中给出2个正整数M和N(<=1000). 输出格式: 在一行中顺序输出M和N的最大公约数和最小公倍数,两数字间以1 ...

  8. php基础知识【函数】(7)url和ob函数

    一.URl函数 1.urlencode -- 编码 URL 字符串 2.urldecode -- 解码已编码的 URL 字符串 3.rawurlencode -- 按照 RFC 1738 对 URL ...

  9. you need to be root to perform this command linux

    获得root权限如何获得:打开终端,输入su回车 然后输入密码回车就行了

  10. Linux 使用yum install安装mysql登陆不上解决办法

    CentOS yum安装mysql后 Can’t connect to local MySQL server through socket ‘/var/lib/ CentOS Can’t connec ...