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

    To The Max

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


    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

     DP

     1 #include<iostream>
     2 //#include<fstream>
     3 #include<memory.h>
     4 using namespace std;
     5 int main()
     6 {
     7     //ifstream in("data.txt");
     8     int n,i,j,k;
     9     int mat[102][102]={0};
    10     int a;
    11     while(cin>>n)
    12     {
    13         memset(mat,0,sizeof(mat));
    14         for(i=1;i<=n;i++)
    15             for(j=1;j<=n;j++)
    16             {
    17                 cin>>a;
    18                 mat[i][j]=mat[i][j-1]+a;
    19             }
    20         int max=-128,sum;
    21         for(i=1;i<=n;i++)
    22             for(j=i;j<=n;j++)
    23             {
    24                 sum=0;
    25                 for(k=1;k<=n;k++)
    26                 {
    27                     sum+=mat[k][j]-mat[k][i-1];
    28                     if(sum>max)
    29                         max=sum;
    30                     if(sum<0)
    31                         sum=0;
    32                 }
    33             }
    34         cout<<max<<endl;
    35     }
    36     return 0;
    37 }
  • 相关阅读:
    列式数据库
    Subway POJ
    操作系统知识汇总
    Linux工具指南
    常用数据结构
    bzoj1257: [CQOI2007]余数之和 整除分块
    HDU
    hdu1693 Eat the Trees 插头dp
    HDU
    poj2411 轮廓线dp裸题
  • 原文地址:https://www.cnblogs.com/zhaopeng938/p/9425337.html
Copyright © 2011-2022 走看看