POJ 1050  To the Max

To the Max
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 27628 Accepted: 14304
Description

Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle. 
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. 
Input

The input consists of an N * N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N^2 integers separated by whitespace (spaces and newlines). These are the N^2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].
Output

Output the sum of the maximal sub-rectangle.
Sample Input

4 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 -2
Sample Output

15
Source

Greater New York 2001
POJ <wbr>1050 <wbr> <wbr>To <wbr>the <wbr>Max原来暴力也是可以解决问题的啊,吓得我提交的时候一身冷汗,没想到16MS AC了,
这题就是从里面任意截取一个矩阵,使得矩阵所包含的数字的和最大.
先开辟一个数组,记下从(1,1)到(i,j)的和,num[i][j]=a[i][j]+num[i-1][j]+num[i][j-1]-num[i-1][j-1];然后求从(i,j)到(k,l)的时候就好算了,sum=num[k][l]-num[i-1][l]-num[k][j-1]+num[i-1][j-1];
求的时候还是剪枝一下吧,怕超时
代码和数据:
#include<stdio.h>
#include<string.h>
int main()
{
int num[105][105],a[105][105],i,j,k,l,sum,max,n;
while(scanf("%d",&n)!=EOF)
{
memset(num,0,sizeof(num));
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
num[i][j]=a[i][j]+num[i-1][j]+num[i][j-1]-num[i-1][j-1];
}
max=a[1][1];
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
for(k=i;k<=n;k++)
for(l=j;l<=n;l++)
{
sum=num[k][l]-num[i-1][l]-num[k][j-1]+num[i-1][j-1];
if(sum<=0)break;
if(max<sum)max=sum;
}
}
printf("%d\n",max);
}
return 0;
}
测试数据:
10
-84 92 -41 -125 44 99 103 -17 87 89
80 20 -94 -48 86 116 120 67 -48 61
16 -21 27 28 -83 7 46 91 93 20
72 101 -104 -87 -6 37 42 -76 -90 19
78 -64 -53 -42 48 39 16 86 -122 -7
-78 19 37 -118 -88 -16 98 116 -96 -97
-59 -90 65 -33 -87 -19 -85 67 -111 23
71 -70 15 104 -5 -25 -119 -24 18 -77
4 -2 -41 79 -119 -35 91 1 56 -67
69 64 1 -52 -88 -87 -7 -43 54 -84
输出:981


20
-86 90 -43 -127 42 97 101 -19 85 87 78 18 -96 -50 84 114 118 65 -50 59
14 -23 25 26 -85 5 44 89 91 18 70 99 -106 -89 -8 35 40 -78 -92 17
76 -66 -55 -44 46 37 14 84 -124 -9 -80 17 35 -120 -90 -18 96 114 -98 -99
-61 -92 63 -35 -89 -21 -87 65 -113 21 69 -72 13 102 -7 -27 -121 -26 16 -79
2 -4 -43 77 -121 -37 89 -1 54 -69 67 62 -1 -54 -90 -89 -9 -45 52 -86
-44 -12 12 31 77 53 100 -71 46 9 -106 118 47 -55 -107 -48 -100 -54 -30 -115
109 -87 34 9 -22 -110 28 -53 -96 -75 -27 23 64 97 89 53 -20 64 -120 -40
80 -90 -124 6 68 32 82 -119 94 -39 45 69 -121 -97 36 41 23 -36 -115 28
33 -18 -3 -90 -79 106 -32 -86 -25 -27 -86 -41 -3 -107 -31 -106 -29 72 41 107
-96 107 -74 122 41 61 23 -89 0 90 101 16 21 106 -70 44 -67 -10 -64 -113
-68 -11 58 74 -27 122 -108 -71 -79 -74 97 -119 -33 -18 112 75 68 108 -34 -34
-104 -40 -63 -124 71 73 81 -9 -47 -81 -79 -96 -38 -79 -118 30 95 -5 -89 -85
-89 52 63 30 81 64 -62 11 29 -116 75 7 -105 -72 -49 19 -15 9 -2 56
-8 15 -33 39 4 121 -55 24 -106 72 -70 99 15 12 -52 85 -27 -117 -124 -8
-16 61 24 -88 -122 46 -125 -42 55 -92 -39 49 -10 -120 -45 55 42 27 94 62
99 -48 -9 65 48 -72 57 -78 64 -115 18 -67 91 -124 12 46 -98 119 60 -98
-78 60 -11 72 66 68 -80 39 -91 -22 111 -95 78 107 -13 74 -61 44 -91 -114
-64 -22 58 -74 35 -69 -45 68 -64 -121 -56 -69 19 105 104 17 69 95 2 34
-92 73 46 89 -83 32 -85 62 -124 -103 27 -117 118 22 59 -64 97 -105 41 -109
-90 28 81 14 75 -2 100 37 -63 -53 -43 -3 -5 32 -44 -57 110 -80 -109 50
输出结果:1267
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值