zoukankan      html  css  js  c++  java
  • hdu 1081To 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): 4842    Accepted Submission(s): 2289


    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
    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3 #define Max(x,y) x>y?x:y
     4 #define INF -0x7fffffff/2
     5 int max_sub(int x[],int n)
     6 {
     7     int max=x[0];
     8     int sum=0;
     9     int i;
    10     for(i=0;i<n;i++)
    11     {
    12         sum+=x[i];
    13         if(sum<0)
    14         {
    15             sum=0;
    16         }
    17         if(max<sum)
    18         {
    19             max=sum;
    20         }
    21     }
    22     
    23     return max;
    24 }
    25 int  main()
    26 {
    27     int t;
    28     int i,j,k;
    29     int aa[105][105];
    30     int bb[105];
    31     int maxx;
    32 
    33     while(~scanf("%d",&t))
    34     {
    35         maxx=INF;
    36         for(i=0;i<t;i++)
    37         {
    38             for(j=0;j<t;j++)
    39             {
    40                 scanf("%d",&aa[i][j]);
    41             }
    42         }
    43         
    44         for(i=0;i<t;i++)
    45         {
    46             memset(bb,0,sizeof(bb));
    47             for(j=i;j<t;j++)
    48             {
    49                 for(k=0;k<t;k++)
    50                 {
    51                     bb[k]+=aa[j][k];
    52                 }
    53                 
    54                 maxx=maxx>max_sub(bb,t)?maxx:max_sub(bb,t);
    55             }
    56         }
    57         printf("%d\n",maxx);
    58     }
    59 }
  • 相关阅读:
    迷宫最短路问题
    回溯算法
    解题报告:poj1321 棋盘问题
    矩阵、分数、点、线类
    判断图像中有多少行文本(开发中)
    图形-回行扫描函数
    贝叶斯分类器
    js解析数学运算公式
    用postcss给less加上webkit前缀
    node创建文件夹
  • 原文地址:https://www.cnblogs.com/1114250779boke/p/2624312.html
Copyright © 2011-2022 走看看