zoukankan      html  css  js  c++  java
  • hdu-1081 To The Max (最大子矩阵和)

    http://acm.hdu.edu.cn/showproblem.php?pid=1081

    To The Max

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


    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

    动态规划问题

    可以将二维数组压缩成一维数组,求压缩后的数字最大子段和,即二维数组的最大子矩阵和。

    将二维数组压缩:将第1行到第n行的每一列都加起来,即a[i][j]表示第j列前1-i行数的和,再求第i行最大子段和就可以了

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 using namespace std;
     6 int main(){
     7     int n, a[110][110], i, j, k;
     8     while(cin>>n){
     9         memset(a, 0, sizeof(a));
    10         for(i=1; i<=n; i++){
    11             for(j=1; j<=n; j++){
    12                 cin>>a[i][j];
    13                 a[i][j] += a[i-1][j];//a[i][j]表示第j列前i行数的和
    14             }
    15         }
    16         int mmax = -1000000, sum;
    17         for(i=1; i<=n; i++){
    18             for(k=1; k<=i; k++){
    19                 sum = 0;
    20                 for(j=1; j<=n; j++){
    21                     sum += (a[i][j]-a[k-1][j]);
    22                     if(sum > mmax)
    23                         mmax = sum;
    24                     if(sum < 0)
    25                         sum = 0;
    26                 }
    27             }
    28         }
    29         cout<<mmax<<endl;
    30     }
    31     return 0;
    32 }
  • 相关阅读:
    关联接口,依赖接口的用法
    通过requests发送不同请求格式的数据
    requests请求数据参数化配置
    requests模块中通用的请求方法,即requests.request
    预期方向需要动态调整
    红枣大幅低开高开
    大涨大跌判断风向
    开盘大幅拉升回落
    红枣变盘之前的预测
    开盘情况要稳定才能进,不看看一根线
  • 原文地址:https://www.cnblogs.com/wudi-accept/p/5334247.html
Copyright © 2011-2022 走看看