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

    题目链接

    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

    题目分析:

    一个N*N的矩阵中的,寻找最大的子矩阵。
    
    主要应用到矩阵压缩的思想,如果某个子矩阵的和最大,我们可以把他们的和压缩为一行,则此时的连续子序列和胃最大的。检查所有的压缩组合,从第一行开始,检查所有的包含此行的往下的行的子矩阵,找出包含此行的子矩阵的最大值,接着检查包含下一行的往下的所有的子矩阵。
    

    代码:

        #include<iostream >
        #include<stdio.h>
        #include<string.h>
        using namespace std;
        int main()
        {
            int jz[141][141],dp[141],sum[141],Max;
            int N,i,j;
            while(~scanf("%d",&N))
            {
                Max=-1333;      ///Max的值每次都要刷新
                for( i=0; i<N; i++)
                    for( j=0; j<N; j++)
                        scanf("%d",&jz[i][j]);///给矩阵赋值
                for(int k=0; k<N; k++)///循环每行都要作为一个起始行
                {
                    memset(dp,0,sizeof(dp));///dp数组刷新
                    memset(sum,0,sizeof(sum));///sum数组刷新
                    for(int i=k; i<N; i++)///从当前行开始循环
                    {
                        for(int j=0; j<N; j++)///每一列都要考虑
                            sum[j]+=jz[i][j];///以前到该列的子矩阵加上该列的
                        dp[0]=sum[0];
                        for(int h=1; h<N; h++)
                        {
                            dp[h]=max(dp[h-1]+sum[h],sum[h]);///以前的和现在的取大值
                            if(dp[h]>Max)
                                Max=dp[h];
                        }
                    }
                }
                printf("%d
    ",Max);
            }
        }
    
  • 相关阅读:
    Controlling behavior of existing services in system
    获取站点路径方法
    如何处理DataTable.Select();执行后重新排序的问题!
    ASP.NET 2.0 编程珠玑之五调试已创建好的代码
    Meal Scheduler in C#
    DHTML+Ajax? MXML+ActionScript? XAML+C#? 是巧合,还是必然?
    C#验证输入的是否数字
    Monitoring System Usage using Windows Service
    ADO.NET 2.0中的DataSet和DataTable
    使用asp.net 2.0和SQL SERVER 2005构建多层应用
  • 原文地址:https://www.cnblogs.com/cmmdc/p/6729588.html
Copyright © 2011-2022 走看看