To the Max
来源:互联网 发布:知豆汽车有限公司待遇 编辑:程序博客网 时间:2024/05/16 07:59
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.
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
40 -2 -7 0 9 2 -6 2-4 1 -4 1 -18 0 -2
Sample Output
15
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define INF 0x3f3f3f3fusing namespace std;int a[105][105],sum[105],mm,n;int main(){ while(~scanf("%d",&n)) { mm=-INF; for(int i=0;i<n;i++) for(int j=0;j<n;j++) scanf("%d",&a[i][j]); for(int i=0;i<n;i++) { memset(sum,0,sizeof(sum)); for(int k=i;k<=n;k++) { int t=0; for(int j=0;j<n;j++) { sum[j]+=a[k][j]; if(t<=0) t=sum[j]; else t+=sum[j]; if(t>mm) mm=t; } } } printf("%d\n",mm); } return 0;}
0 0
- zoj1074 To the Max
- 1074 To the Max
- 1050 To the Max
- POJ1050 To the Max
- POJ1050 To the Max
- POJ to The Max
- 1081 To The Max
- To the Max
- To the max(hdu1081)
- POJ To the Max
- 1050--To the Max
- POJ1050 To the Max
- poj1050 to the max
- To The Max
- HDU1081--To The Max
- poj1050 To the Max
- poj1050 To the Max
- POJ1050--To the Max
- 纯css3超酷tab选项卡效果
- 使用Spring Annotation注解导致jar包更新的文件扫描不到
- 将在Bundle中的plist存到沙盒Document中读取
- [LeetCode]34.Search for a Range
- mac下配置环境变量
- To the Max
- 生活中的数学问题(四)
- PHP中函数变量传递
- 找数字
- ZOJ 3602 Count the Trees
- 生活中的数学问题(五)
- uva548 - Tree 入门经典II 第六章数据结构基础 例题6-8
- 操作格子(线段树)
- 生活中的数学问题(六)