HDU 1081 To The Max
To The Max
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6688 Accepted Submission(s): 3198
As an example, the maximal sub-rectangle of the array:
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
is in the lower left corner:
9 2
-4 1
-1 8
and has a sum of 15.
memset(temp,0,sizeof(temp));
for(int k = 1;k <= n;k ++)
{
for(int l = i;l <= j;l ++)
temp[k] += map[l][k];
}
dp[1] = temp[1];
for(int k = 1;k <= n;k ++){
if(dp[k - 1] > 0)
dp[k] = dp[k - 1] + temp[k];
else
dp[k] = temp[k];
}
for(int k = 1;k <= n;k ++)
if(dp[k] > max)
max = dp[k];
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
int map[110][110];
int dp[110];
int temp[110];
int n;
int main()
{
while(~scanf("%d",&n))
{
for(int i = 1;i <= n;i ++)
for(int j = 1;j <= n;j ++)
scanf("%d",&map[i][j]);
int max = 0;
for(int i = 1;i <= n;i ++)
for(int j = i;j <= n;j ++)
{
memset(dp,0,sizeof(dp));
memset(temp,0,sizeof(temp));
for(int k = 1;k <= n;k ++)
{
for(int l = i;l <= j;l ++)
temp[k] += map[l][k];
}
dp[1] = temp[1];
for(int k = 1;k <= n;k ++){
if(dp[k - 1] > 0)
dp[k] = dp[k - 1] + temp[k];
else
dp[k] = temp[k];
}
for(int k = 1;k <= n;k ++)
if(dp[k] > max)
max = dp[k];
}
printf("%d\n",max);
}
return 0;
}