• poj1050 To the Max


    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

    这道题最重要的是把二维数组的问题转化为一维数组的求最大字段和的问题。我们选定i行和j行,上下一压缩,二维就变为一维了,而每一项即为从i行到j行的元素的和。之后求最大字段和即可。
    代码如下:
     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #define MAX 102
     5 #define MIN -12702
     6 int Mat[MAX][MAX];
     7 int sum[MAX][MAX][MAX];
     8 //        col  rowi rowk i < j
     9 /*
    10     1 2 3 
    11     4 5 6
    12     7 8 9
    13 
    14  */
    15 int main(int argc, char const *argv[])
    16 {
    17     int N;
    18     scanf("%d",&N);
    19     for(int i = 0; i < N; i++) {
    20         for(int j = 0; j < N; j++) {
    21             scanf("%d",&Mat[i][j]);
    22             sum[j][i][i] = Mat[i][j];
    23         }
    24     }
    25 
    26     for(int j = 0; j < N; j++) {
    27         for(int i = 0; i < N; i++) {
    28             for(int k = i+1; k < N; k++) {
    29                 sum[j][i][k] = sum[j][i][k-1] + Mat[k][j];
    30             }
    31         }
    32     }
    33     int ans = MIN;
    34     for(int i = 0; i < N; i++) {
    35         int maxj = MIN;
    36         for(int j = i; j < N; j++) {
    37             //from i row to j row
    38             int ansij = 0;
    39             int maxij = MIN;
    40             for(int k = 0; k < N; k++) {
    41                 ansij = ansij + sum[k][i][j];
    42                 if(maxij < ansij) {
    43                     maxij = ansij;
    44                 }
    45                 if(ansij < 0) {
    46                     ansij = 0;
    47                 }
    48             }
    49             if(maxij > maxj) {
    50                 maxj = maxij;
    51             }
    52         }
    53         if(maxj > ans) {
    54             ans = maxj;
    55         }
    56     }
    57     printf("%d
    ",ans);
    58     //system("pause");
    59     return 0;
    60 }

    这道题开始有些不知道怎么做,也是参考了别人的思路才做出来,不过一遍就AC了

  • 相关阅读:
    PostgreSQL在Update时使用Substring函数截取字符串并且加上CASE WHEN THEN条件判断
    清理Visual Studio 2017的项目历史记录或手工修改Visual Studio 2017的注册表设置
    基于ABP模块组件与依赖注入组件的项目插件开发
    jenkins git can't work ERROR: Timeout after 10 minutes ERROR: Error fetching remote repo 'origin'
    SV randomize
    SV class
    SV coverage
    uvm设计分析——reg
    shell bash-shell
    scrapy的安装
  • 原文地址:https://www.cnblogs.com/jasonJie/p/5669447.html
Copyright © 2020-2023  润新知