To the Max
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. 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.
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 corner:
9 2
-4 1
-1 8
and has a sum of 15.
Input
The input consists of an N * N 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 N^2 integers separated by whitespace (spaces and newlines). These are the N^2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in 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].
Output
Output 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的整数矩阵,包含正负数,从该矩阵中寻找一个子矩阵使其数之和最大,子矩阵最小可为1*1。
二维空间寻找子矩阵较复杂,可以先降到一维先考虑如何求得一维数组子段和最大。
求最大子段和:
int max_array(int a[],int n)
{
int sum=0,max=-10000;
for(int i=0;i<n;i++)
{
if(sum>0) //如果到此为止和为正的话继续加入下一元素比较
sum+=a[i];
else //如果和为零或负值则进行剪枝,直接以下一元素作为字段头再开始
sum=a[i];
if(sum>max) //记录每次的最大值并不断更新
max=sum;
}
return max;
}
接下来从最大子段为基础考虑最大子阵:
int max_rectangle(int n,int b[][101])
{
int temp[101];
int max=-10000;
for(int i=0;i<n;i++)
{
memset(temp,0,sizeof(temp)); //注意清空重置!
for(int j=i;j<n;j++)
{
for(int k=0;k<n;k++)
temp[k]+=b[j][k]; //将二维的数组相加压缩成一维的,找最大子段和
if(max_array(k,temp)>max)
max=max_array(k,temp);
}
}
return max;
}
最大连续子序列
Problem Description
给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ...,
Nj },其中 1 <= i <= j <= K。最大连续子序列是所有连续子序列中元素和最大的一个,
例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和
为20。
在今年的数据结构考卷中,要求编写程序得到最大和,现在增加一个要求,即还需要输出该
子序列的第一个和最后一个元素。
Input
测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( < 10000 ),第2行给出K个整数,中间用空格分隔。当K为0时,输入结束,该用例不被处理。
Output
对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元
素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。
Sample Input
6 -2 11 -4 13 -5 -2 10 -10 1 2 3 4 -5 -23 3 7 -21 6 5 -8 3 2 5 0 1 10 3 -1 -5 -2 3 -1 0 -2 0
Sample Output
20 11 13 10 1 4 10 3 5 10 10 10 0 -1 -2 0 0 0
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int n;
int a[10001];
while (scanf("%d",&n)&&n)
{
int sum = 0, box = 0;
int first, last, max = -10000, ans1, ans2;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
if (a[i] < 0)
box++;
}
if (box != n) //特判是否全为负数
{
for (int i = 1; i <= n; i++)
{
if (sum > 0) //目前和大于零则继续,更新尾元素
{
sum += a[i];
last = a[i];
}
else //如果到此和小于等于零的话将前面的剪枝,重新开始
{
sum = a[i];
first = last = a[i];
}
if (sum > max) //与前面储存的最大记录进行比较,大于的话则进行更新
{
max = sum;
ans1 = first;
ans2 = last;
}
}
cout << max << ' ' << ans1 << ' ' << ans2 << endl;
}
else
cout << '0' << ' ' << a[1] << ' ' << a[n]<<endl;
}
return 0;
}