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

    To the Max
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 43725   Accepted: 23166

    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

     
    题意:求子矩阵的最大和。
     
    极度朴素的算法是O(N6),加上预处理出和能达到O(N4),但是这都不能达到要求。
    我们在O(N4)的算法上加优化,可以少掉一层循环。
    我们先来看一下这道题的线性版本。
    给出一个序列,求其中一段连续子序列的最大和。
    很明显我们可以预处理出前 i 个元素的和。然后 j ~ i 这段元素的和就是 sum[i] - sum[j - 1],现在我们要找这个值的最大值,我们看到在 i 不变的时候,
    如果我们想让这个值最大,那sum[j - 1]必定是 1 ~ i - 1最小的。
    这道题只是把它扩充成二维的版本。
    加上一点东西,把这个矩阵压缩成一条线就行了。
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    const int MAXN = 105;
    const int INF = 0x7fffffff;
    int best;
    int rowsum[MAXN][MAXN];
    int sum[MAXN];
    int n;
    int ans;
    
    int main()
    {
        scanf("%d", &n);
        memset(rowsum, 0, sizeof(rowsum));
        for (int i = 1; i <= n; ++i)
        {
            for (int j = 1; j <= n; ++j)
            {
                scanf("%d", &rowsum[i][j]);
                rowsum[i][j] += rowsum[i][j - 1];
            }
        }
        ans = -INF;
        sum[0] = 0;
        for (int i = 1; i <= n; ++i)
            for (int j = i; j <= n; ++j)
            {
                best = 0;
                for (int k = 1; k <= n; ++k)
                {
                    sum[k] = rowsum[k][j] - rowsum[k][i - 1] + sum[k - 1]; /* 压缩 */
                    ans = max(ans, sum[k] - sum[best]);
                    if (sum[best] > sum[k]) best = k;
                }
            }
        printf("%d
    ", ans);
        return 0;
    }
  • 相关阅读:
    MUTC2013 E-Deque-hdu 4604
    MUTC7 C
    MUTC7 A-As long as Binbin loves Sangsang
    MUTC2013 J-I-number-hdu4608
    MUTC2013 H-Park Visit-hdu4607
    判断点是否在多边形内 扫描法
    蓝桥杯 基础练习 十六进制转八进制
    判断点是否在三角形内
    判断点在线段上
    向量的叉乘
  • 原文地址:https://www.cnblogs.com/albert7xie/p/4729788.html
Copyright © 2011-2022 走看看