• poj 1050(矩阵求和问题dp)


    To the Max
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 44765   Accepted: 23700

    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

     
    思路:将其化简成子段求和问题:将第i行到第j行的数累加成一维数列,在用子段求和求解。
       
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cmath>
     4 #include <cstdio>
     5 #include <string>
     6 #include <cstring>
     7 #include <vector>
     8 #include <queue>
     9 #include <stack>
    10 #include <set>
    11 #define INF 0x3f3f3f3f
    12 #define INFL 0x3f3f3f3f3f3f3f3f
    13 #define zero_(x,y) memset(x , y , sizeof(x))
    14 #define zero(x) memset(x , 0 , sizeof(x))
    15 #define MAX(x) memset(x , 0x3f ,sizeof(x))
    16 using namespace std ;
    17 #define N 505
    18 typedef long long LL ;
    19 LL dp[N],a[N][N],s[N][N];
    20 int main(){
    21     //freopen("in.txt","r",stdin);
    22     int n;
    23     scanf("%d",&n);
    24     zero(dp);zero(a);zero(s);
    25     for(int i=1;i<=n;i++){
    26         for(int j=1;j<=n;j++){
    27             scanf("%I64d",&a[i][j]);
    28         }
    29     }
    30     for(int i=1;i<=n;i++){
    31         for(int j=1;j<=n;j++){
    32             s[j][i]=s[j-1][i]+a[j][i];
    33            
    34         }
    35     }
    36    
    37     LL sum=0,b=0;
    38     for(int i=0;i<=n;i++){
    39         for(int j=i+1;j<=n;j++){
    40                 b=0;
    41             for(int k=1;k<=n;k++){
    42                 
    43                 if(b>0)
    44                     b+=(s[j][k]-s[i][k]);
    45                 else
    46                     b=s[j][k]-s[i][k];
    47                 if(b>sum)
    48                     sum=b;
    49                 
    50             }
    52         }
    53     }
    54     printf("%I64d
    ",sum);
    55     return 0;
    56 }
  • 相关阅读:
    python 垃圾回收机制
    @property 取代getter setter方法
    ==值相等 is同一性
    循环获取文件名,拼接路径打印
    python 闭包
    python生成器
    hadoop集群环境搭建之zookeeper集群的安装部署
    hadoop集群环境搭建之安装配置hadoop集群
    hadoop集群环境搭建准备工作
    linux下安装jdk
  • 原文地址:https://www.cnblogs.com/yoyo-sincerely/p/5093805.html
Copyright © 2020-2023  润新知