URAL1146 & POJ1050 Maximum Sum (最大连续子序列和)

Description

Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. 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. A sub-rectangle is any contiguous sub-array of size 1 × 1 or greater located within the whole array.

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-hand corner and has the 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 white-space (newlines and spaces). These N 2integers make up the array in row-major order (i.e., all numbers on the first row, left-to-right, then all numbers on 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

The output is the sum of the maximal sub-rectangle.

题意解析:

题目比较简单,给定一个N*N的矩阵,求这个矩阵中子矩阵的最大和。譬如给定的示例中,最大的子矩阵为

9 2    
−4 1    
−1 8    

即为15.

解题思路:

这个是一个二维的数组,求最大子矩阵和,应该要想到使用一维的最大子序列和,然后就可以将子矩阵压缩为子序列,再求解。

1)将矩阵缩为一列列,然后对这四个列进行 求最大子序列的和。

2)求一个一维的最大子序列和。

代码如下:

#include <stdio.h>
#define MAX_N 100
int arr[MAX_N][MAX_N];
int rowsSum[MAX_N]; //每一行的和
int N;
int calc(int x, int y);
int main()
{
      //freopen("input.txt","r",stdin);
      scanf("%d",&N);
      int i = 0;
      int j = 0;
      int max = -1270000;
      int sum = 0;
      for(i=0;i<N;i++)
      {
             for(j=0;j<N;j++)
             {
                   scanf(" %d",&arr[i][j]);
             }
      }

for(i=0;i<N;i++)
      {
           for(j=i;j<N;j++)
          {
                 sum = calc(i,j); //计算第i列到第j列的最大和
                 if(sum>max)
                {
                      max = sum;
                }
          }
      }
      printf("%d\n",max);
      return 0;
}

int calc(int x, int y)
{
      int i = 0;
      int j = 0;
      int resultValue = -1270000;
      int thisSum = 0;
      for(i=0;i<N;i++)
      {
            rowsSum[i] = 0;
            for(j=x;j<=y;j++)
            {
                   rowsSum[i] = rowsSum[i] + arr[i][j];
            }
      }

//rowsSum表示,对于指定的列i到j,每一行的和。

//求rowsSum[]这个一维数组的最大子序列和
      for(i=0;i<N;i++)
      {
            thisSum = thisSum + rowsSum[i];
            if(thisSum>resultValue)
            {
                    resultValue = thisSum;
            }
            if(thisSum<0)
            {
                    thisSum = 0;
            }
      }

return resultValue;

}

时间: 2024-10-13 11:35:22

URAL1146 & POJ1050 Maximum Sum (最大连续子序列和)的相关文章

HDU 1003 Max Sum 最大连续子序列的和

Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input contains

leetcode | Maximum Subarray 最大连续子序列的和

Maximum Subarray: https://leetcode.com/problems/maximum-subarray/ Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [

ural 1146 Maximum Sum 最大连续和

1146. Maximum Sum Time limit: 0.5 second Memory limit: 64 MB Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. The sum of a rectangle is the sum of all the elements in that rectangle. In this

杭电1003 Max Sum 【连续子序列求最大和】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目意思: 即给出一串数据,求连续的子序列的最大和 解题思路: 因为我们很容易想到用一个max来存放找到的子序列的和中的最大值,通过不断比较,对max的值进行更新,最后我们就能够得到最大子序列的和,于是很容易想到用暴力搜索,见上一篇博客,这样的时间复杂度为O(n^3),是超时的. 又因为想到只要一个数不是负数,不管它再小,加上去也是会使和变大的,所以我们需要用另一个变量来判断即将要加上的一个

zoj1003-Max Sum (最大连续子序列之和)

http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 161361    Accepted Submission(s): 37794 Problem Description Given a sequence a[1],a[2],a[3]

URAL1146——DP——Maximum Sum

Description Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. 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

UVa 108 - Maximum Sum(最大连续子序列)

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=44  Maximum Sum  Background A problem that is simple to solve in one dimension is often much more difficult to solve in more th

最大连续子序列和问题(Maximum Consecutive Subsequence Sum)

该算法的定义是:给出一个int序列,元素有正有负,找出其中的最大连续子序列的和. 例如:-2,11,-4,13,-5-2,:最大和为20(11,-4, 13). 怎么考虑这个问题呢? 要充分利用,连续,这个条件. 连续子序列的和可能为正,也可能为负.如果为正,那么我们要继续加下去,因为如果后面一个数是正数, 最大子序列和必定包括前序列的和(毕竟前者和为正),如果后面一个是负数,我们也加,直到子序列的和不为正为止. 因为它不为正,所以如果后面有更大的连续子序列,那么也不会包括它,因为加上一个非正数

Poj 2479 Maximum sum【双向DP/最大连续和】

Maximum sum 题意:给定一个长度为N的数组,求两个连续的子序列,使得两个连续子序列的和最大. 分析:乍一看,跟最大连续和有点类似,但是,又有区别,因为对于这个题,考虑第i项两个连续子序列的最大和,不能仅仅由前i-1项递推得出,第i项两个连续子序列的最大和,与前i项和i以后的之间是存在关系的,因此这个题目是一个双向dp. 假如给定的序列为a0, a1, a2, a3, a4, ...... ,an,那么,对于任意第i项,我以第i个元素为分界点,用一个数组项pMax [i]来保存  区间