Maximum Sum

原创 2016年08月28日 15:56:56
链接:http://acm.timus.ru/problem.aspx?space=1&num=1146
题目:
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);
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

poj2479 Maximum sum

  • 2014-11-30 12:42
  • 653B
  • 下载

PAT 1007 Maximum Subsequence Sum(最长子段和)

PAT 1007 Maximum Subsequence Sum(最长子段和)

SPOJ KGSS Maximum Sum (线段树)

SPOJ KGSS Maximum Sum 题意:求区间最大值与第二大值之和 思路:线段树维护两个最大值 /*******************************************...

poj 2479 Maximum sum poj 2593 Max Sequence

题目链接http://poj.org/problem?id=2479 题目大意:给定数组a求数组a中两个大子序列之和的最大值 思路:对a数组进行扫描求出以左端点开始到右端点的最大子序列之和 再次扫描求...

1007. Maximum Subsequence Sum (25)

1007. Maximum Subsequence Sum (25)目录Maximum Subsequence Sum 25 目录 原题 思路 代码原题 Given a sequence of K...

1007. Maximum Subsequence Sum (25)

Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

Binary Tree Maximum Path Sum

LeetCode :Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The p...

1007. Maximum Subsequence Sum (25)

Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni...

Binary Tree Maximum Path Sum

Binary Tree Maximum Path Sum

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

注意:dp 题目:1007. Maximum Subsequence Sum (25) Given a sequence of K integers { N1, N2, ..., NK }. A...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)