题目来源: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 than one dimension. Consider satisfying a boolean expression in conjunctive normal form in which each conjunct consists of exactly 3 disjuncts. This problem (3-SAT) is NP-complete. The problem 2-SAT is solved quite efficiently, however. In contrast, some problems belong to the same complexity class regardless of the dimensionality of the problem.

The Problem

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  or greater located within the whole array. As an example, the maximal sub-rectangle of the array:

is in the lower-left-hand corner:

and has the sum of 15.

Input and Output

The input consists of an  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  integers separated by white-space (newlines and spaces). These 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].

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

Sample Input

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

Sample Output

15

解题思路:

题意:给出n*n的矩阵,求出里面子矩阵的和的最大值。

最大连续子序列的应用,序列是一维的,矩阵是二维的,所以我们可以把矩阵转换为一维的来算。

也就是枚举矩阵的连续几行的合并,这样就转换为一维的了,再用最大子序列的算法去求,更新最大值就可以了。

代码:

 #include <bits/stdc++.h>

 using namespace std;

 int table[][];
int sum[];
int N; int max_continuous_sum()
{
int maxs=,s=;
for(int i=; i<N; i++)
{
if(s>=) s+=sum[i];
else s=sum[i];
maxs = maxs>s ? maxs : s;
}
return maxs;
}
int main()
{
cin >> N;
int maxsum=;
int tmp;
for(int i=; i<N; i++)
{
for(int j=; j<N; j++)
{
cin >> table[i][j];
sum[j]=table[i][j];
}
tmp = max_continuous_sum();
maxsum = maxsum>tmp ? maxsum : tmp;
for(int j=i-; j>=; j--)
{
for(int k=; k<N; k++)
sum[k]+=table[j][k];
tmp = max_continuous_sum();
maxsum = maxsum>tmp ? maxsum : tmp;
}
}
cout << maxsum << endl;
return ;
}

UVa 108 - Maximum Sum(最大连续子序列)的更多相关文章

  1. UVa 108: Maximum Sum

    这道题用暴力解法+动态规划.分析如下: 对于某个1*m的矩阵,即一个数列,求其maximal sub-rectangle,可以通过求最大长连续字串和来求得(这个用到了动态规划). 那么对于n*m的矩阵 ...

  2. UVa 10827 - Maximum sum on a torus

    题目大意:UVa 108 - Maximum Sum的加强版,求最大子矩阵和,不过矩阵是可以循环的,矩阵到结尾时可以循环到开头.开始听纠结的,想着难道要分情况讨论吗?!就去网上搜,看到可以通过补全进行 ...

  3. PAT 1007 Maximum Subsequence Sum 最大连续子序列和

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

  4. PAT 1007 Maximum Subsequence Sum (最大连续子序列之和)

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

  5. 紫书 例题 10-29 UVa 1642(最优连续子序列)

    这类求最优连续子序列的题一般是枚举右端点,然后根据题目要求更新左端点, 一般是nlogn,右端点枚举是n,左端点是logn 难点在于如何更新左端点 用一些例子试一下可以发现 每次加进一个新元素的时候 ...

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

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目意思: 即给出一串数据,求连续的子序列的最大和 解题思路: 因为我们很容易想到用一个max ...

  7. UVA 10827 Maximum sum on a torus (LA)

    算法入门经典训练指南88页练习 ::这道题只要把原矩阵扩大4倍,那么其跟最大子矩阵的题目就很类似,把二维转化成一维,求最大的序列和,不过这个序列的长度不能超过n. 长度不能超过n? 那这道题又跟hdu ...

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

    http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  9. UVA 10827 Maximum sum on a torus 最大矩阵和

    题目链接:UVA - 10827 题意描述:给出一个n*n矩阵,把第一行和最后一行粘一起,把第一列和最后一列粘一起,形成一个环面,求出这个环面中最大的矩阵和. 算法分析:首先复制n*n这个矩阵,形成由 ...

随机推荐

  1. Python开源框架Scrapy安装及使用

    一.安装问题 环境: CentOS  + Python 2.7 + Pip 1) 安装时遇到 ”UnicodeDecodeError: 'ascii' codec can't decode byte  ...

  2. Feathers JS – 基于 Express 构建数据驱动的服务

    Feathers 是一个轻量的 Web 应用程序框架,基于 NodeJS 最流行​​的 Web 框架——Express.这使得它很容易使用 socket.io 来创建 RESTful Web 服务和实 ...

  3. android基础---->JSON数据的解析

    上篇博客,我们谈到了XML两种常用的解析技术,详细可以参见我的博客(android基础---->XMl数据的解析).网络传输另外一种数据格式JSON就是我们今天要讲的,它是比XML体积更小的数据 ...

  4. MySQL中的锁(表锁、行锁)

    锁是计算机协调多个进程或纯线程并发访问某一资源的机制.在数据库中,除传统的计算资源(CPU.RAM.I/O)的争用以外,数据也是一种供许多用户共享的资源.如何保证数据并发访问的一致性.有效性是所在有数 ...

  5. stl的优先级队列

    #include <iostream> #include <vector> #include <queue> using namespace std; class ...

  6. CART(分类回归树)原理和实现

    前面我们了解了决策树和adaboost的决策树墩的原理和实现,在adaboost我们看到,用简单的决策树墩的效果也很不错,但是对于更多特征的样本来说,可能需要很多数量的决策树墩 或许我们可以考虑使用更 ...

  7. mysql线上一些隐患查询sql

    开发写了几个语句,觉得查询结果跟逻辑有点不相符,就拿到这里一起分析了下. 语句如下: select tp.title, tp.amount, ifnull( ) as aInvestAmount, i ...

  8. 6/13 sprint2 看板和燃尽图的更新

  9. HNU 13308 Help cupid

    Help cupid Problem's Link: http://acm.hnu.cn/online/?action=problem&type=show&id=13308&c ...

  10. Opserver监控工具的使用

    Opserver监控工具的使用 Opserver是Stack Overflow的开源监控解决方案,由Stack Exchange发布,基于.NET框架构建.开源地址:https://github.co ...