UVa 108 - Maximum Sum(最大连续子序列)
| 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(最大连续子序列)的更多相关文章
- UVa 108: Maximum Sum
这道题用暴力解法+动态规划.分析如下: 对于某个1*m的矩阵,即一个数列,求其maximal sub-rectangle,可以通过求最大长连续字串和来求得(这个用到了动态规划). 那么对于n*m的矩阵 ...
- UVa 10827 - Maximum sum on a torus
题目大意:UVa 108 - Maximum Sum的加强版,求最大子矩阵和,不过矩阵是可以循环的,矩阵到结尾时可以循环到开头.开始听纠结的,想着难道要分情况讨论吗?!就去网上搜,看到可以通过补全进行 ...
- PAT 1007 Maximum Subsequence Sum 最大连续子序列和
Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni ...
- PAT 1007 Maximum Subsequence Sum (最大连续子序列之和)
Given a sequence of K integers { N1, N2, ..., *N**K* }. A continuous subsequence is defined to be { ...
- 紫书 例题 10-29 UVa 1642(最优连续子序列)
这类求最优连续子序列的题一般是枚举右端点,然后根据题目要求更新左端点, 一般是nlogn,右端点枚举是n,左端点是logn 难点在于如何更新左端点 用一些例子试一下可以发现 每次加进一个新元素的时候 ...
- 杭电1003 Max Sum 【连续子序列求最大和】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目意思: 即给出一串数据,求连续的子序列的最大和 解题思路: 因为我们很容易想到用一个max ...
- UVA 10827 Maximum sum on a torus (LA)
算法入门经典训练指南88页练习 ::这道题只要把原矩阵扩大4倍,那么其跟最大子矩阵的题目就很类似,把二维转化成一维,求最大的序列和,不过这个序列的长度不能超过n. 长度不能超过n? 那这道题又跟hdu ...
- zoj1003-Max Sum (最大连续子序列之和)
http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others) Mem ...
- UVA 10827 Maximum sum on a torus 最大矩阵和
题目链接:UVA - 10827 题意描述:给出一个n*n矩阵,把第一行和最后一行粘一起,把第一列和最后一列粘一起,形成一个环面,求出这个环面中最大的矩阵和. 算法分析:首先复制n*n这个矩阵,形成由 ...
随机推荐
- 二项堆(一)之 图文解析 和 C语言的实现
概要 本章介绍二项堆,它和之前所讲的堆(二叉堆.左倾堆.斜堆)一样,也是用于实现优先队列的.和以往一样,本文会先对二项堆的理论知识进行简单介绍,然后给出C语言的实现.后续再分别给出C++和Java版本 ...
- BackgroundCheck – 根据图片亮度智能切换元素样式
BackgroundCheck 是一个轻量的 JavaScript 库,能够根据元素后面的图片的亮度自动切换元素样式.例如在图片幻灯片功能中,根据图片亮度调整导航箭头的颜色,这样让图片和导航的颜色形成 ...
- Javascript起源...
Javascript的设计思路是这样的: (1)借鉴C语言的基本语法: (2)借鉴Java语言的数据类型和内存管理: (3)借鉴Scheme语言,将函数提升到"第一等公民"(fir ...
- PHP如何将进程作为守护进程
看了这篇:http://blog.codinglabs.org/articles/write-daemon-with-php.html 对里面的posix_setsid()不解 文档解释是" ...
- Entity FrameWork 增删查改的本质
之前的文章里面已经说了,EF的增删查改.那时候的修改,删除,只能是先查询出来要修改的数据,再修改,删除...现在来一个改进版的,增删查改. 1.Add static void Add() { //1. ...
- 2015 Multi-University Training Contest 1 - 1002 Assignment
Assignment Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Mean: 给你一个数列和一个k,求连续区间的极值之 ...
- 2015年百度之星初赛(1) --- F 矩形面积
矩形面积 Problem Description 小度熊有一个桌面,小度熊剪了很多矩形放在桌面上,小度熊想知道能把这些矩形包围起来的面积最小的矩形的面积是多少. Input 第一行一个正整数 T, ...
- 微软开源的30个基础设施项目-C#
.NET Compiler Platform ("Roslyn") .NET Core 5 .NET Micro Framework .NET SDK For Hadoop ASP ...
- 怎样在C#中从数据库中读取数据(数据读取器)
实现在C#中通过语句,查询数据库中的数据 SqlConnection con = null; //创建SqlConnection 的对象 try //try里面放可能出现错误的代码 ...
- 重新想象 Windows 8 Store Apps (69) - 其它: 自定义启动屏幕, 程序的运行位置, 保持屏幕的点亮状态, MessageDialog, PopupMenu
[源码下载] 重新想象 Windows 8 Store Apps (69) - 其它: 自定义启动屏幕, 程序的运行位置, 保持屏幕的点亮状态, MessageDialog, PopupMenu 作者 ...