POJ 1050 To the Max
来源:互联网 发布:pc端前端数据渲染 编辑:程序博客网 时间:2024/06/03 16:50
To the Max
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 39059 Accepted: 20630
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
题目大意: 在一个矩阵里找一个子矩阵,使子矩阵的和最大。
基本思路是将二维数组转化为一维数组,然后就与一维数组求最大值的方法一样。
转化为一维的思路是:输入数组的每一列从i行到j行化为tmp[k][i][j],k为当前列。
#include<stdio.h>#include<string.h>int n,a[105][105],tmp[105][105][105]; //a[][]为输入,tmp[k][i][j]中保存第k列,从i行到j行的综合。void Set() //初始化tmp{ memset(tmp,0,sizeof(tmp)); int i,j,k; for(i=1; i<=n; i++) for(j=1; j<=n; j++) scanf("%d",&a[i][j]); for(k=1; k<=n; k++) for(i=1; i<=n; i++) for(j=i; j<=n; j++) for(int p=i; p<=j; p++) tmp[k][i][j]+=a[p][k];}int main(){ while(~scanf("%d",&n)) { Set(); int sum,_max=0,i,j,k;//此题不存在每个数都为负的情况。 for(i=1; i<=n; i++) for(j=i; j<=n; j++) { sum=0; //每计算一组从i行到j行的矩形都要给sum先清零。 for(k=1; k<=n; k++) //同一维数组的寻找连续字串的和。 { sum+=tmp[k][i][j]; if(sum<0) sum=0; if(sum>_max) _max=sum; } } printf("%d\n",_max); } return 0;}
0 0
- POJ 1050 To the Max
- poj 1050 To the Max
- POJ 1050 To the Max
- poj 1050 To the Max
- Poj 1050 To the Max
- POJ 1050 To the Max
- POJ 1050 To the Max
- POJ 1050 To the Max
- poj 1050 To the Max
- poj 1050 To the Max
- Poj 1050 To the Max
- POJ 1050 To the Max
- poj 1050 to the max
- POJ 1050 To the Max
- poj 1050 to the max
- poj-1050- To the Max
- POJ-1050-To the Max
- POJ 1050 To the Max
- html5 (EME)Encrypted Media Extensions
- easyui和jqueryui冲突解决
- zvasdf
- Ubuntu操作系统学习笔记之NFS基础
- UI-控件-TextField
- POJ 1050 To the Max
- C语言关键字分析系列
- ios app开发教程新的分享
- wince -- 用SYSGEN_CAPTURE实现WINCE PUBLIC文件夹中新增驱动DM9000的本地化(BSP化)
- 实习第四天
- php生成二维码的几种方式
- 且行且珍惜
- ngnix:slab机制
- 平台构建需要加入的功能想法