最大连续子序列
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 32827 Accepted Submission(s): 14770
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
求最大连续子序列 难点在输出子序列的前后两个端点的值:
核心代码为:
for (int i = 2; i <= n; i++)
{
if (dp[i - 1] + a[i]>a[i])
{
dp[i] = dp[i - 1] + a[i];
}
else
{
dp[i] = a[i];
temp = a[i];
}
if (Max < dp[i])
{
l = temp;
r = a[i];
Max = dp[i];
}
}
用temp保存开头的位置, 结尾的位置即为i,然后找到每次找到最大之后再用l 和 r 保存;
AC代码:
#include
#include
#include
#include
using namespace std;
#define Maxn 10005
int dp[Maxn];
int a[Maxn];
int main()
{
int n;
while (~scanf("%d", &n) && n)
{
int Max = 0;
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
}
dp[1] = a[1];
Max = a[1];
int l = a[1];
int r = a[1];
int temp = a[1];
for (int i = 2; i <= n; i++)
{
if (dp[i - 1] + a[i]>a[i])
{
dp[i] = dp[i - 1] + a[i];
}
else
{
dp[i] = a[i];
temp = a[i];
}
if (Max < dp[i])
{
l = temp;
r = a[i];
Max = dp[i];
}
}
if (Max < 0) { printf("0 %d %d\n", a[1], a[n]); }
else cout << Max << " " << l << " " << r << endl;
}
}
#include
#include
#include
using namespace std;
#define Maxn 10005
int dp[Maxn];
int a[Maxn];
int main()
{
int n;
while (~scanf("%d", &n) && n)
{
int Max = 0;
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
}
dp[1] = a[1];
Max = a[1];
int l = a[1];
int r = a[1];
int temp = a[1];
for (int i = 2; i <= n; i++)
{
if (dp[i - 1] + a[i]>a[i])
{
dp[i] = dp[i - 1] + a[i];
}
else
{
dp[i] = a[i];
temp = a[i];
}
if (Max < dp[i])
{
l = temp;
r = a[i];
Max = dp[i];
}
}
if (Max < 0) { printf("0 %d %d\n", a[1], a[n]); }
else cout << Max << " " << l << " " << r << endl;
}
}
HDU-1003也是类似的题目
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 247757 Accepted Submission(s): 58534
Problem Description
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
Sample Input
2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5
Sample Output
Case 1: 14 1 4 Case 2: 7 1 6
AC代码:
#include
#include
#include
#include
using namespace std;
#define Maxn 100005
int dp[Maxn];
int a[Maxn];
int main()
{
int time = 1;
int T;
scanf("%d", &T);
while (T--)
{
int n;
scanf("%d", &n);
int Max = 0;
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
}
dp[1] = a[1];
Max = a[1];
int l = 1;
int r = 1;
int temp = 1;
for (int i = 2; i <= n; i++)
{
if (dp[i - 1] + a[i] >= a[i])
{
dp[i] = dp[i - 1] + a[i];
}
else
{
dp[i] = a[i];
temp = i;
}
if (Max < dp[i])
{
l = temp;
r = i;
Max = dp[i];
}
}
printf("Case %d:\n", time++);
cout << Max << " " << l<< " " << r<< endl;
if (T != 0)
cout << endl;
}
}
#include
#include
#include
using namespace std;
#define Maxn 100005
int dp[Maxn];
int a[Maxn];
int main()
{
int time = 1;
int T;
scanf("%d", &T);
while (T--)
{
int n;
scanf("%d", &n);
int Max = 0;
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
}
dp[1] = a[1];
Max = a[1];
int l = 1;
int r = 1;
int temp = 1;
for (int i = 2; i <= n; i++)
{
if (dp[i - 1] + a[i] >= a[i])
{
dp[i] = dp[i - 1] + a[i];
}
else
{
dp[i] = a[i];
temp = i;
}
if (Max < dp[i])
{
l = temp;
r = i;
Max = dp[i];
}
}
printf("Case %d:\n", time++);
cout << Max << " " << l<< " " << r<< endl;
if (T != 0)
cout << endl;
}
}