Maximum Sum

Maximum Sum

Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  URAL 1146

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−70
92−62
−41−41
−180−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  2 integers 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.

Sample Input

inputoutput
4
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
15

Dp:

   因为所求之和为矩阵和,所以必须满足行数相邻,每行起点终点相同,即需要构造dp[i][j][k],表示矩阵行数以终点为i,以j为起点,k为终点的矩阵最大和,sum值记录该i行起点为for(k--),终点为k的矩阵和。当然同样可以枚举以k为起点的矩阵和,同理。

   复杂度O(n3);

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
const int INF = 0x3f3f3f3f;
using namespace std;
int a[105][105];
int dp[105][105][105];
int main(){
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            scanf("%d", &a[i][j]);
        }
    }
    int ans = -INF;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            int sum = 0;
            for(int k = j; k >= 1; k--)//枚举以k为终点;//(for(int k = j; k <= n; j++)//这是枚举k为起点;)
        { sum
+= a[i][k];//sum值记录第i行以k为终点的矩阵和; dp[i][j][k] = max(sum,sum+ dp[i-1][j][k]); ans = max(ans, dp[i][j][k]); } } } printf("%d", ans); return 0; }

 

 

转载于:https://www.cnblogs.com/ACMessi/p/4900602.html

### 回答1: 以下是一个经典的 Maximum Sum 子数组问题的动态规划代码: ```python def max_sum_subarray(nums): if not nums: return 0 n = len(nums) dp = [0]*n dp[0] = nums[0] max_sum = nums[0] for i in range(1, n): dp[i] = max(dp[i-1]+nums[i], nums[i]) max_sum = max(max_sum, dp[i]) return max_sum ``` 该算法的时间复杂度为 $O(n)$,其中 $n$ 是数组的长度。 ### 回答2: Maximum sum问题是一个经典的动态规划问题,其目标是在一个给定的数组中找到一个具有最大和的子数组。 在解决这个问题时,可以定义一个一维动态规划数组dp,其中dp[i]表示以第i个元素结尾的子数组的最大和。那么,可以得出动态规划的转移方程如下: dp[i] = max(dp[i-1] + nums[i], nums[i]) 其中,nums表示给定的整数数组。 接下来,可以使用一个变量maxSum来记录所有子数组的最大和。遍历整个数组,更新dp[i]的同时,不断更新maxSum的值,即可得到最终的结果。 下面是该问题的动态规划代码实现: ```python def maxSum(nums): dp = [0] * len(nums) maxSum = float('-inf') dp[0] = nums[0] maxSum = max(maxSum, dp[0]) for i in range(1, len(nums)): dp[i] = max(dp[i-1] + nums[i], nums[i]) maxSum = max(maxSum, dp[i]) return maxSum ``` 该算法的时间复杂度为O(n),其中n为数组的长度。使用动态规划的思想,可以高效地解决Maximum sum问题。 ### 回答3: 动态规划(Dynamic Programming)是一种常用的算法思想,可以解决一些最优化问题。Maximum Sum问题是一种经典的动态规划问题,目标是找出一个数组中最大的子数组和。 要编写Maximum Sum的动态规划代码,可以按照以下步骤进行: 1. 首先定义一个变量max_sum,用于记录当前最大的子数组和,初始化为数组中的第一个元素(即max_sum = arr[0])。 2. 然后定义一个变量cur_sum,用于记录当前的子数组和,初始化为数组中的第一个元素(即cur_sum = arr[0])。 3. 接着,使用一个循环遍历数组中的每一个元素(从第二个元素开始): (1)如果当前子数组和cur_sum加上当前元素arr[i]大于当前元素arr[i]本身,说明加上当前元素后,子数组和变得更大,因此更新cur_sum为cur_sum + arr[i]。 (2)否则,当前元素arr[i]比当前子数组和cur_sum更大,说明当前元素作为新的起点,重新开始构建子数组,即令cur_sum = arr[i]。 (3)将当前子数组和cur_sum与当前最大的子数组和max_sum进行比较,如果cur_sum大于max_sum,则更新max_sum为cur_sum。 4. 最后,返回最大的子数组和max_sum作为最终结果。 下面给出这个算法的代码实现: ```python def maximum_sum(arr): max_sum = arr[0] cur_sum = arr[0] for i in range(1, len(arr)): if cur_sum + arr[i] > arr[i]: cur_sum += arr[i] else: cur_sum = arr[i] if cur_sum > max_sum: max_sum = cur_sum return max_sum ``` 这段代码的时间复杂度为O(n),其中n为数组的长度,因为需要遍历整个数组。在使用动态规划思想解决Maximum Sum问题时,可以过定义合适的状态和状态转移方程来简化问题,并提高算法的效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值