poj 1050 To the Max && 51nod dp 最大子矩阵和

来源:互联网 发布:apache 配置php7 编辑:程序博客网 时间:2024/05/29 03:54

思路:

我们把每一列第i行到第j行之间的和求出来,形成一个数组c,于是一个第i行到第j行之间的最大子矩阵和对应于这个和数组c的最大子段和。然后每次求数组c的最大子串和,

ACcode:

#include <map>#include <queue>#include <cmath>#include <cstdio>#include <cstring>#include <stdlib.h>#include <iostream>#include <algorithm>#define maxn 700#define ll long longusing namespace std;int n,m;ll dp[maxn][maxn];ll c[maxn];ll ans=0,tmp;int main(){    while(~scanf("%d",&n)){        m=n;        for(int i=1;i<=m;++i)for(int j=1;j<=n;++j)scanf("%I64d",&dp[i][j]);        memset(c,0,sizeof(c));        for(int i=1;i<=m;++i)            for(int j=i;j<=m;++j)                for(int k=1,tmp=0;k<=n;++k){                    c[k]=(j==i)?dp[i][k]:c[k]+dp[j][k];                    if(tmp>0)tmp+=c[k];                    else tmp=c[k];                    ans=ans>tmp?ans:tmp;                }        printf("%I64d\n",ans);    }    return 0;}/*13 6 71 7 5 4 8 3 91 4 3 5 6 2 8 9*/


0 0