标签:空格 max return sed += 任务 key i++ 之间
4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1
8 0 -2
15
#define cin2(a,n,m) for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j];
#define rep_(n,m) for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
#define rep(n) for(int i=1;i<=n;i++)
#define test(xxx) cout<<" Test " <<" "<<xxx<<endl;
const int mxn = 1e2+5;
int key[mxn],a[mxn][mxn];
int main()
{
int n,ans=-1e9;
cin>>n;
cin2(a,n,n);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
a[i][j] += a[i-1][j];
}
}
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
mem(key,0);
int col = -2147483647;
for(int k=1;k<=n;k++)
{
int cnt = a[j][k] - a[i-1][k];
key[k] = max(key[k-1]+cnt,cnt);
col = max(col,key[k]);
}
ans = max(ans,col);
//cout<<ans<<endl;
}
}
cout<<ans<<endl;
return 0;
}
int a[mxn][mxn],b[mxn][mxn];
int main()
{
int n,mx = -1e9;
cin>>n;
cin2(a,n,n);
b[1][0] = 0;b[0][1] = 0;
rep(n) b[1][i] = max( a[1][i],a[1][i]+b[1][i-1] );
rep(n) b[i][1] = max( a[i][1],a[i][1]+b[i-1][1] );
for(int i=2;i<=n;i++)
{
for(int j=2;j<=n;j++)
{
b[i][j] = max( max( a[i][j],a[i][j]+b[i-1][j] ) , max( a[i][j]+b[i][j-1] , a[i][j]+b[i-1][j]+ b[i][j-1]-b[i-1][j-1] ));
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
mx = max(mx,b[i][j]);
cout<<b[i][j]<<" ";
}
cout<<endl;
}
cout<<mx<<endl;
}
最大子矩阵 (经典贪心/DP )( Greater New York 2001 )
标签:空格 max return sed += 任务 key i++ 之间
原文地址:https://www.cnblogs.com/Shallow-dream/p/11729749.html