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
  • 下载

HDU1231最大连续子序列&Uva108Maximum Sum最大子矩阵

HDU1231:给定K个整数的序列{ N1, N2, …, NK },其任意连续子序列可表示为{ Ni, Ni+1, …, Nj },其中 1 ...

poj2479 Maximum sum(最大子段和变形)

dp 最大子段和的变形 求不相交的最大两个最大子段和,之前对最大子段和都喜欢这样写 for(int i=0;i0) res=max(res,sum); else sum=0; }也没有...

LeetCode: Binary Tree Maximum Path Sum [124]

【题目】 Given a binary tree, find the maximum path sum. The path may start and end at any node in the...

pat PAT (Advanced Level) Practise 1007. Maximum Subsequence Sum (25)

1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 ...

[题解]codeforces 280d k-Maximum Subsequence Sum

Discription题目大意: 给出一个长度为 n 的序列 An{A_n}, m 次操作, 操作有如下两种: 1. 给出 i,val, 把 AiA_i 变成 val。 2. 给出 l,r,k,...

【PAT】1007. Maximum Subsequence Sum (25)

题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1007 分析: (1)求最大连续子序列,最终输出最大子序列值以及子序列起始和终止元素。如果数...

BZOJ3638 Cf172 k-Maximum Subsequence Sum

[Solution]
  • laekov
  • laekov
  • 2014年07月14日 17:43
  • 757

Maximum Sum

1146. Maximum Sum Time limit: 1.0 second Memory limit: 64 MB Given a 2-dimensional array ...

#94 Binary Tree Maximum Path Sum

题目描述: Given a binary tree, find the maximum path sum. The path may start and end at any node...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Maximum Sum
举报原因:
原因补充:

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