标签:
4 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 -2
15
//二维前缀和 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int N=105; int n,a[N][N],s[N][N],ans=-1e5,f[N]; void init(){ for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) s[i][j]=s[i][j-1]+s[i-1][j]-s[i-1][j-1]+a[i][j]; } inline int get(int x,int y1,int y2){ return s[x][y2]-s[x-1][y2]-s[x][y1]+s[x-1][y1]; } int main(int argc, const char * argv[]) { scanf("%d",&n); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&a[i][j]); init(); for(int y2=1;y2<=n;y2++) for(int y1=0;y1<y2;y1++) for(int x=1;x<=n;x++){ f[x]=max(0,f[x-1])+get(x,y1,y2); ans=max(ans,f[x]); } cout<<ans; return 0; }
//c[x]递推,当前压缩的值 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int N=105; int n,a[N][N],c[N],ans=-1e5,f[N]; int main(int argc, const char * argv[]) { scanf("%d",&n); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&a[i][j]); for(int y1=0;y1<n;y1++){ memset(c,0,sizeof(c)); for(int y2=y1+1;y2<=n;y2++) for(int x=1;x<=n;x++){ c[x]+=a[x][y2]; f[x]=max(0,f[x-1])+c[x]; ans=max(ans,f[x]); } } cout<<ans; return 0; }
openjudge1768 最大子矩阵[二维前缀和or递推|DP]
标签:
原文地址:http://www.cnblogs.com/candy99/p/5811908.html