To The Max

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12697    Accepted Submission(s):
6090

Problem Description
Given a two-dimensional array of positive and negative
integers, a sub-rectangle is any contiguous sub-array of size 1 x 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 x 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
 
题意:二维的矩阵,从中找到一个子矩阵,使得子矩阵的和最大。
思路:可以先考虑一维的情况,一维时即数列,求数列中连续子列的和的最大值,做法就是在线处理,从头到尾一个一个元素考虑并累加过去,记当前累加值为sum,若累加的时候当前sum值小于0了,那么舍弃前面的累加列,sum更新为0,并且从下一个位置
的元素重新开始累加,途中不断的更新sum,找出最大的sum值即可,二维的情况可以看作一维的延伸情况,如果把列固定住(即选取矩阵连续的几列并固定,先算好每一行的这几列的和值),此时纵向的从上到下累加就可以看成是一维情况下的累加,算法类同。
AC代码:
  1. #define _CRT_SECURE_NO_DEPRECATE
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<string>
  5. #include<set>
  6. #include<map>
  7. #include<vector>
  8. #include<queue>
  9. #include<functional>
  10. using namespace std;
  11. const int N_MAX= +;
  12. int a[N_MAX][N_MAX];
  13. int sum[N_MAX][N_MAX];
  14. int main() {
  15. int n;
  16. while (scanf("%d", &n) != EOF) {
  17. memset(sum,,sizeof(sum));
  18. memset(a, ,sizeof(a));
  19. for (int i = ; i < n; i++) {
  20. for (int j = ; j <= n; j++) {
  21. scanf("%d", &a[i][j]);
  22. }
  23. }
  24. for (int i = ; i < n; i++) {
  25. for (int j = ; j <= n; j++) {
  26. sum[i][j] =sum[i][j-]+ a[i][j];
  27. }
  28. }
  29.  
  30. int res = -INT_MAX;
  31. for (int i = ; i < n; i++) {//固定i,j
  32. for (int j = i+; j <= n; j++) {
  33. int S = ;
  34. for (int k = ; k < n; k++) {
  35. S += sum[k][j]-sum[k][i-];//累加上闭区间[i,j]值的和
  36. if (S > res)
  37. res = S;
  38. if (S < )S = ;
  39.  
  40. }
  41. }
  42. }
  43. printf("%d\n",res);
  44.  
  45. }
  46. return ;
  47. }
 
思路
 

poj 1081 To The Max的更多相关文章

  1. hdu 1081 To The Max(dp+化二维为一维)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1081 To The Max Time Limit: 2000/1000 MS (Java/Others ...

  2. hdu 1081 &amp; poj 1050 To The Max(最大和的子矩阵)

    转载请注明出处:http://blog.csdn.net/u012860063 Description Given a two-dimensional array of positive and ne ...

  3. POJ 1050 To the Max 暴力,基础知识 难度:0

    http://poj.org/problem?id=1050 设sum[i][j]为从(1,1)到(i,j)的矩形中所有数字之和 首先处理出sum[i][j],此时左上角为(x1,y1),右下角为(x ...

  4. POJ 1050 To the Max -- 动态规划

    题目地址:http://poj.org/problem?id=1050 Description Given a two-dimensional array of positive and negati ...

  5. poj 1050 To the Max (简单dp)

    题目链接:http://poj.org/problem?id=1050 #include<cstdio> #include<cstring> #include<iostr ...

  6. POJ 1050 To the Max 最大子矩阵和(二维的最大字段和)

    传送门: http://poj.org/problem?id=1050 To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submi ...

  7. poj - 1050 - To the Max(dp)

    题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 ...

  8. dp - 最大子矩阵和 - HDU 1081 To The Max

    To The Max Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=1081 Mean: 求N*N数字矩阵的最大子矩阵和. ana ...

  9. HDU 1081 To The Max【dp,思维】

    HDU 1081 题意:给定二维矩阵,求数组的子矩阵的元素和最大是多少. 题解:这个相当于求最大连续子序列和的加强版,把一维变成了二维. 先看看一维怎么办的: int getsum() { ; int ...

随机推荐

  1. perl -p -i -w -e

    .txt kllk nciuwbufcbew``````//.]];s[[..; klklkl x,dsncdk,;l,ex xw,eocxmcmck .txt .txt kkkkkkkkkkkkkk ...

  2. iOS与JS相互传值与交互

    JavaScriptCore是webkit的一个重要组成部分,主要是对JS进行解析和提供执行环境.iOS7后苹果在iPhone平台推出,极大的方便了我们对js的操作.我们可以脱离webview直接运行 ...

  3. windows系统下的两个批处理命令

    启动应用:***.exe 关闭应用:taskkill /f /im ***.exe 保存为.bat文件

  4. mac 升级EI Capitan后遇到c++转lua时遇到libclang.dylib找不到的错

    升级EI Capitan后,打包lua脚本时,会报这个错: LibclangError: dlopen(libclang.dylib, 6): image not found. To provide ...

  5. iOS应用架构谈part3 网络层设计方案

    前言 网络层在一个App中也是一个不可缺少的部分,工程师们在网络层能够发挥的空间也比较大.另外,苹果对网络请求部分已经做了很好的封装,业界的AFNetworking也被广泛使用.其它的ASIHttpR ...

  6. HDU-4848-Such Conquering

    这题就是深搜加剪枝,有一个很明显的剪枝,因为题目中给出了一个deadline,所以我们一定要用这个deadline来进行剪枝. 题目的意思是求到达每个点的时间总和,当时把题看错了,卡了好久. 剪枝一: ...

  7. JDBC-防止SQL注入问题

      String sql = "select * from user where name = '" + name + "' and password = '" ...

  8. paper:synthesizable finit state machine design techniques using the new systemverilog 3.0 enhancements之全0/1/z/x的SV写法

  9. 掌握这些Python代码技巧,编程至少快一半!

    被人工智能捧红的 Python 已是一种发展完善且非常多样化的语言,其中肯定有一些你尚未发现的功能.本文或许能够让你学到一些新技巧. ​ Python 是世界上最流行.热门的编程语言之一,原因很多,比 ...

  10. SQL_3_表达式、条件语句与运算

    加号的两种用法: 1.在SELECT子句中使用+号以执行对数据的运算并将结果显示出来. SELECT ITEM WHOLESALE WHOLESALE+0.15 FROM PRICE; 还可以重命名新 ...