zoukankan      html  css  js  c++  java
  • To the Max POJ

    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


    题意:给你一个n,然后给你一个n*n的矩阵,求出最大的子矩阵的和


    思路:我们知道一种求最大子段和的方法(什么你不知道?),就是O(n)遍历这个一维的数组,把当前遍历的数加入一个变量(tmp),在这个过程中记录最大值,如果这个变量变成负数,
    那么就把这个变量置零,继续往下遍历。
    为什么呢?
    如果我们加入的这个数是一个正数,那正和我们意(我们意是什么鬼),因为正数可以让变量(tmp)更大,我们需要的就是一个最大值,如果加入的数是一个负数的话,分两种情况
    1、tmp >= 0
            这样的话对于后面加入的数来说,我们前面所加的数是有意义的,因为变量还是一个正数(虽然减小了),它仍可以使得后面加入的数变大(哲学的声音?)
    2、tmp < 0
            这样对于后面加入的数来说,我们前面所加的数毫无意义,它使得后面的数反而更小了,所以我们就不要前面的数了(一脸嫌弃),将tmp置零。


    那么给你一个二维数组,求一个最大的子矩阵,和这个有什么关系呢? 一维数组 == n*1*1的二维矩阵
    这么一看我们好像已经完成了对于一个特殊二维矩阵求最大子矩阵和。

    那么对于题目给出的二维矩阵,我们可以转换为我们的特殊矩阵。我们枚举i、j,表示将i~j行看成一维数组,我们将mar【i】【k】 += mar【j】【k】(对应位置相加),对mar【i】这个一维数组求最大字段和


     1 #include<cstdio>
     2 #include<iostream>
     3 using namespace std;
     4 
     5 int n;
     6 int mar[104][104];
     7 int maxx = -20000;
     8 int main()
     9 {
    10     scanf("%d",&n);
    11     int lim = 0;
    12     for(int i=1;i<=n;i++)
    13         for(int j=1;j<=n;j++)
    14             scanf("%d",&mar[i][j]),lim+=mar[i][j];
    15     for(int i=1;i<=n;i++)
    16     {
    17         for(int j=i;j<=n;j++)
    18         {
    19             int tmp = 0;
    20             for(int k=1;k<=n;k++)
    21             {
    22                 if(i != j)mar[i][k] += mar[j][k];
    23                 if(tmp > 0)tmp+=mar[i][k];
    24                 else tmp = mar[i][k];
    25                 if(tmp > maxx && tmp != lim)maxx = tmp;
    26             }
    27         }
    28     }
    29     printf("%d
    ",maxx);
    30 }
    View Code
  • 相关阅读:
    SecureCRT设置linux终端显示颜色
    OpenCV空洞填充算法
    祝贺自己的软件《万能数据库查询分析器》在非凡软件站和太平洋电脑的下载排行榜分别名列第1和第2
    linux磁盘分区 简要
    linux压缩解压命令
    【可视化】Echarts3坐标系倒映
    【可视化】Echarts3 在世界地图中绘制中国各省份的轮廓
    【Maven】解决linux下安装maven update-alternative --display mvn链接层数过多
    【Ubuntu 16】启动Eclipse Indigo报错 error code1 jdk没有配置好
    oracle体系结构
  • 原文地址:https://www.cnblogs.com/iwannabe/p/10158787.html
Copyright © 2011-2022 走看看