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;
    }
  • 相关阅读:
    rsync 配置
    DNS主从服务,子域授权,view视图,日志系统,压力测试
    ubuntu VNC server 黑屏 yum源更新(ubuntu16.04)
    HTTPD服务 openssl的https服务机制
    vmware Esxi 更换管理网卡IP
    httpd 虚拟主机建立之访问机制及其日志定义
    pxe kickstart 配置+TFTP+NFS多版本系统部署
    Linux nohup不输出日志文件的方法
    Tomcat部署时war和war exploded区别
    vim编辑超大文件
  • 原文地址:https://www.cnblogs.com/albert7xie/p/4729788.html
Copyright © 2011-2022 走看看