[蓝桥杯]最大子阵

http://oj.ecustacm.cn/problem.php?id=1451 

#include<bits/stdc++.h>
#define ls o<<1
#define rs o<<1|1
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
typedef long long LL; 
template<class T> inline void gmax(T &a,T b){if(b>a)a=b;}
template<class T> inline void gmin(T &a,T b){if(b<a)a=b;}
const int N=505,M=0,Z=1e9+7,W=13,L=2e6;
const double eps=1e-10;
const int dy[4]={-1,0,0,1},dx[4]={0,-1,1,0};
using namespace std;
int casenum,casei;
int id;
int n,m;
int a[N][N];

int main(){
//	freopen("in.txt","r",stdin);
	while(~scanf("%d%d",&n,&m)) {
		MS(a,0);
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				scanf("%d",&a[i][j]);
				a[i][j]+=a[i-1][j];//top down sum
			}
		}
		int ans=a[1][1];
		for(int i=1;i<=n;i++){//down line
			for(int j=1;j<=i;j++){//top line
				int tans=0;
				for(int k=1;k<=m;k++){
					tans+=a[i][k]-a[j-1][k];
					gmax(ans,tans);
					gmax(tans,0);
				}
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

1.做列的前缀和

2.枚举上下边界

3.如果>0就利用前面的矩阵

发布了810 篇原创文章 · 获赞 19 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/ujn20161222/article/details/104002916