using namespace std;
#define MAX(a, b) (a>b ? a : b)
#define MIN(a, b) (a>b ? b : a)
int const maxn = 102;
int map[maxn][maxn], sum[maxn][maxn];
int n;
int read()
{
int i, j;
if(cin >> n)
{
for(i=1;i<=n;++i)
{
sum[i][0] = 0;
for(j=1;j<=n;++j)
{
cin >> map[i][j];
sum[i][j] = map[i][j] + sum[i][j-1];
}
}
return 1;
}
return 0;
}
void solve()
{
int i, j, k, sub_row, max[maxn], min[maxn], best;
best = 0;
for(i=1;i<=n;++i)
{
for(j=i;j<=n;++j)
{
sub_row = 0;
max[0] = 0;
min[0] = 100000;
for(k=1;k<=n;++k)
{
sub_row = sum[k][j] - sum[k][i];
max[k] = MAX(max[k-1], sub_row);
min[k] = MIN(min[k-1], sub_row);
}
best = MAX(max[n] - min[n], best);
}
}
cout << best << endl;
/*for(i=1;i<=n;++i)
for(j=1;j<=n;++j)
cout << map[i][j] << " " << endl;*/
}
int main()
{
while(read()) solve();
return 0;
}
2 comments :
#include
#include
using namespace std;
#define MAX(a, b) (a>b ? a : b)
#define MIN(a, b) (a>b ? b : a)
#define INFINITY 100000
int const maxn = 102;
int map[maxn][maxn], sum[maxn][maxn];
int n;
int read()
{
int i, j;
if(cin >> n)
{
for(i=1;i<=n;++i)
{
sum[i][0] = 0;
for(j=1;j<=n;++j)
{
cin >> map[i][j];
sum[i][j] = map[i][j] + sum[i][j-1];
}
}
return 1;
}
return 0;
}
void solve()
{
int i, j, k, sub_row[n+1], best;
best = -INFINITY;
for(i=1;i<=n;++i)
{
for(j=i;j<=n;++j)
{
//memset(sub_row, 0, sizeof(sub_row));
for(k=0;k<n+1;++k)sub_row[k]=0;
for(k=1;k<=n;++k)
{
sub_row[k] = sum[k][j] - sum[k][i-1];
if(sub_row[k-1] < 0) sub_row[k-1] = 0;
sub_row[k] += sub_row[k-1];
best = MAX(sub_row[k], best);
}
}
}
cout << best << endl;
/*for(i=1;i<=n;++i)
for(j=1;j<=n;++j)
cout << map[i][j] << " " << endl;*/
}
int main()
{
while(read()) solve();
return 0;
}
#include
using namespace std;
#define MAX 100
long mx(long a , long b)
{
if(a>b) return a;
return b;
}
int main()
{
unsigned int N=4,a,b,i,j;
long ret=-9999999;
short A[4][4]={{0,-2,-7,0},{9,2,-6,2},{-4,1-4,1},{-1,8,0,-2}};
for( a=0;a<N;a++)
for( b=a;b<N;b++)
{
long sum=-9999999;
for( i=0;i<N;i++)
{
long temp = 0;
for(j=a;j<=b;j++) temp += A[i][j];
printf("temp=%ld \t sum+temp=%ld ",temp,(sum+temp));
sum = mx(sum+temp,temp);
ret = mx(ret,sum);
printf(" sum=%ld \t ret=%ld \n ",sum,ret);
}
}
cout<<ret<<endl;
return 0;
}
output Analysis
temp=0 sum+temp=-9999999 sum=0 ret=0
temp=9 sum+temp=9 sum=9 ret=9
temp=-4 sum+temp=5 sum=5 ret=9
temp=-1 sum+temp=4 sum=4 ret=9
temp=-2 sum+temp=-10000001 sum=-2 ret=9
temp=11 sum+temp=9 sum=11 ret=11
temp=-7 sum+temp=4 sum=4 ret=11
temp=7 sum+temp=11 sum=11 ret=11
temp=-9 sum+temp=-10000008 sum=-9 ret=11
temp=5 sum+temp=-4 sum=5 ret=11
temp=-6 sum+temp=-1 sum=-1 ret=11
temp=7 sum+temp=6 sum=7 ret=11
temp=-9 sum+temp=-10000008 sum=-9 ret=11
temp=7 sum+temp=-2 sum=7 ret=11
temp=-6 sum+temp=1 sum=1 ret=11
temp=5 sum+temp=6 sum=6 ret=11
temp=-2 sum+temp=-10000001 sum=-2 ret=11
temp=2 sum+temp=0 sum=2 ret=11
temp=-3 sum+temp=-1 sum=-1 ret=11
temp=8 sum+temp=7 sum=8 ret=11
temp=-9 sum+temp=-10000008 sum=-9 ret=11
temp=-4 sum+temp=-13 sum=-4 ret=11
temp=-2 sum+temp=-6 sum=-2 ret=11
temp=8 sum+temp=6 sum=8 ret=11
temp=-9 sum+temp=-10000008 sum=-9 ret=11
temp=-2 sum+temp=-11 sum=-2 ret=11
temp=-2 sum+temp=-4 sum=-2 ret=11
temp=6 sum+temp=4 sum=6 ret=11
temp=-7 sum+temp=-10000006 sum=-7 ret=11
temp=-6 sum+temp=-13 sum=-6 ret=11
temp=1 sum+temp=-5 sum=1 ret=11
temp=0 sum+temp=1 sum=1 ret=11
temp=-7 sum+temp=-10000006 sum=-7 ret=11
temp=-4 sum+temp=-11 sum=-4 ret=11
temp=1 sum+temp=-3 sum=1 ret=11
temp=-2 sum+temp=-1 sum=-1 ret=11
temp=0 sum+temp=-9999999 sum=0 ret=11
temp=2 sum+temp=2 sum=2 ret=11
temp=0 sum+temp=2 sum=2 ret=11
temp=-2 sum+temp=0 sum=0 ret=11
11
Post a Comment