zoukankan      html  css  js  c++  java
  • poj 1081 To The Max

                                                                   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代码:
    #define _CRT_SECURE_NO_DEPRECATE
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<set>
    #include<map>
    #include<vector>
    #include<queue>
    #include<functional>
    using namespace std;
    const int N_MAX= 100+2;
    int a[N_MAX][N_MAX];
    int sum[N_MAX][N_MAX];
    int main() {
        int n;
        while (scanf("%d", &n) != EOF) {
            memset(sum,0,sizeof(sum));
            memset(a, 0,sizeof(a));
            for (int i = 0; i < n; i++) {
                for (int j = 1; j <= n; j++) {
                    scanf("%d", &a[i][j]);
                }
            }
            for (int i = 0; i < n; i++) {
                for (int j = 1; j <= n; j++) {
                     sum[i][j] =sum[i][j-1]+ a[i][j];
                }
            }
            
            int res = -INT_MAX;
            for (int i = 0; i < n; i++) {//固定i,j
                for (int j = i+1; j <= n; j++) {
                    int S = 0;
                    for (int k = 0; k < n; k++) {
                        S += sum[k][j]-sum[k][i-1];//累加上闭区间[i,j]值的和
                        if (S > res)
                            res = S;
                        if (S < 0)S = 0;
                        
                    }
                }
            }
            printf("%d
    ",res);
    
        }
    return 0;
    }
     
    思路
     
  • 相关阅读:
    【开卷故意】JAVA正則表達式模版
    CSS控制显示超出部分,用省略号显示
    1星《微信软文营销实战技巧》:标题党,作者没有实战经验
    3星|《强势谈判》:有趣的绑匪谈判故事
    3星|《赢者的诅咒》:新晋诺奖得主92年作品,博弈论在拍卖、薪酬、股市、彩票、赛马、汇市等领域的应用,偏专业
    4星|《OKR实践指南》:老司机经验谈
    3星|《华为管理变革》:华为有史以来为了变革而开展的项目的概述。
    3星|《信号》:全球经济的坏消息
    2星|《麦肯锡图表工作法》:用图表做商业分析的入门演示
    2星|《麦肯锡与贝恩的团队管理智慧》:从投入、产出两个维度把下属分到4个象限
  • 原文地址:https://www.cnblogs.com/ZefengYao/p/6686980.html
Copyright © 2011-2022 走看看