标签:nod .com http play cstring 51nod += lap pre
【题解】
二重循环枚举起始列和终止列,竖着往下加,转化为一个最大子段和问题,逐行累加即可。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define LL long long 5 #define N 1000 6 #define rg register 7 using namespace std; 8 int n,m,x; 9 LL ans,sum[N][N]; 10 inline int read(){ 11 int k=0,f=1; char c=getchar(); 12 while(c<‘0‘||c>‘9‘)c==‘-‘&&(f=-1),c=getchar(); 13 while(‘0‘<=c&&c<=‘9‘)k=k*10+c-‘0‘,c=getchar(); 14 return k*f; 15 } 16 int main(){ 17 m=read(); n=read(); 18 for(rg int i=1;i<=n;i++) 19 for(rg int j=1;j<=m;j++) x=read(),sum[i][j]=sum[i][j-1]+x; 20 for(rg int i=1;i<=m;i++) 21 for(rg int j=i;j<=m;j++){ 22 LL tmp=0; 23 for(rg int k=1;k<=n;k++){ 24 tmp+=sum[k][j]-sum[k][i-1]; 25 if(tmp<0) tmp=0; 26 else ans=max(ans,tmp); 27 } 28 } 29 printf("%lld\n",ans); 30 return 0; 31 }
标签:nod .com http play cstring 51nod += lap pre
原文地址:https://www.cnblogs.com/DriverLao/p/9572039.html