您的位置:首页 > 其它

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

2016-05-14 16:28 363 查看
思路:

我们把每一列第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 long
using 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;
}
/*
1
3 6 7
1 7 5 4 8 3 9
1 4 3 5 6 2 8 9
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: