Maximum Sum

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.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cstring>
#include <functional>
#include <cmath>
using namespace std;

int s[110][110];
int main()
{
//freopen("in.txt", "r", stdin);
int n;
while (~scanf("%d", &n))
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
scanf("%d", &s[i][j]);
if(j>=1)
s[i][j] += s[i][j - 1];
}
int ans = -10000000;

for (int i = 0; i < n; i++)
for (int j = i; j < n; j++)
for (int k = 0, temp = 0; k < n;k++)
{
temp += s[k][j] - s[k][i - 1];
ans = (ans > temp) ? ans : temp;
if (temp < 0)temp = 0;
}
printf("%d\n", ans);
}
}

• 本文已收录于以下专栏：

Maximum sum-动态规划

A - Maximum sum Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub...
• qq_18661257
• 2015年07月25日 17:14
• 953

poj2479Maximum sum

• su20145104009
• 2015年03月26日 20:42
• 1453

1007. Maximum Subsequence Sum (25) -- 动态规划

1007. Maximum Subsequence Sum (25)题目地址Given a sequence of K integers { N1, N2, …, NK }. A continuous...
• qq_26437925
• 2015年08月13日 21:11
• 697

算法笔记-1-最大子列和-Maximum Subsequence Sum

• plank_root
• 2016年09月18日 22:58
• 1539

POJ2.6 1481 Maximum sum

POJ2.6 1481 Maximum sum
• leizhehan
• 2016年11月07日 18:05
• 388

LeetCode689. Maximum Sum of 3 Non-Overlapping Subarrays

LeetCode689. Maximum Sum of 3 Non-Overlapping Subarrays
• Yaokai_AssultMaster
• 2017年10月16日 13:27
• 1159

【LeetCode】Binary Tree Maximum Path Sum 解题报告

【题目】 Given a binary tree, find the maximum path sum. The path may start and end at any node ...
• ljiabin
• 2015年01月22日 16:15
• 3255

LWC 52：689. Maximum Sum of 3 Non-Overlapping Subarrays

LWC 52：689. Maximum Sum of 3 Non-Overlapping Subarrays传送门：689. Maximum Sum of 3 Non-Overlapping Suba...
• u014688145
• 2017年10月03日 09:46
• 531

poj2479 - Maximum sum

• wangjian8006
• 2012年05月21日 18:48
• 4352

POJ2479&OpenJudge1481 Maximum sum

• ccutyear
• 2017年04月18日 21:14
• 219

举报原因： 您举报文章：Maximum Sum 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)