题目
Given a sequence of
K
K
K integers
{
N
1
,
N
2
,
.
.
.
,
N
K
}
\{N_1,N_2,..., N_K\}
{N1,N2,...,NK}. A continuous subsequence is defined to be
{
N
i
,
N
i
+
1
,
.
.
.
,
N
j
}
\{N_i, N_{i+1}, ..., N_j\}
{Ni,Ni+1,...,Nj} where
1
≤
i
≤
j
≤
K
1\leq i\leq j\leq K
1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence which has the largest sum of its elements. For example, given sequence
{
−
2
,
11
,
−
4
,
13
,
−
5
,
−
2
}
\{-2, 11, -4, 13, -5, -2\}
{−2,11,−4,13,−5,−2}, its maximum subsequence is
{
11
,
−
4
,
13
}
\{11, -4, 13\}
{11,−4,13} with the largest sum being 20.
Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.
Input Specification:
Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer
K
(
≤
10000
)
K(\leq 10000)
K(≤10000). The second line contains
K
K
K numbers, separated by a space.
Output Specification:
For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices
i
i
i and
j
j
j (as shown by the sample case). If all the
K
K
K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.
Sample Input:
10
-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
10 1 4
代码长度限制
16 KB
时间限制
200 ms
内存限制
64 MB
利用在线处理的方法
#include<stdio.h>
int main()
{
int K;
int a[10000];
int ThisSum = 0;
int MaxSum = 0;
int LeftPosition;
int RightPosition;
int Left;
scanf("%d",&K);
for(int i=0;i<K;i++)
{
scanf("%d",a+i);
}
LeftPosition = 0;
Left = LeftPosition;
RightPosition = K-1;
for(int i=0;i<K;i++)
{
ThisSum += a[i];
if(ThisSum > MaxSum){
MaxSum = ThisSum;
Left = LeftPosition;
RightPosition = i;
}
else if (ThisSum<0)
{
ThisSum = 0;
LeftPosition = i+1;
}
}
printf("%d %d %d",MaxSum, a[Left], a[RightPosition]);
}
提交后显示如下图:
经过分析,原因是当序列中仅存在0和负数时,上述算法的最大序列和输出仍然是0,但子列的输出却是整个序列的第一个和最后一个数,而题目中希望输出的是数字应该为0。
但又不能将ThisSum
>
>
> MaxSum更改为ThisSum
≥
\geq
≥ MaxSum,否则会不满足In case that the maximum subsequence is not unique, output the one with the smallest indices i and j
的条件。
方法是: 将初始最大值由0更改为-0.1,并将数据类型更改为浮点数。这样,当索引到达数字0所在的位置时,满足
0
≥
−
0.1
0 \geq -0.1
0≥−0.1的判断条件,会更新子序列的左右位置。
但仅仅改变这些是不够的,对于全是负数的测试用例,此程序的输出是-0.1,而不是0。因此需要在最后额外加入一个判断最大值是否小于0的语句。
最终代码如下:
#include<stdio.h>
int main()
{
int K;
int a[10000];
int ThisSum = 0;
double MaxSum = -1e-1;
int LeftPosition;
int RightPosition;
int Left;
scanf("%d",&K);
for(int i=0;i<K;i++)
{
scanf("%d",a+i);
}
LeftPosition = 0;
Left = LeftPosition;
RightPosition = K-1;
for(int i=0;i<K;i++)
{
ThisSum += a[i];
if(ThisSum > MaxSum){
MaxSum = ThisSum;
Left = LeftPosition;
RightPosition = i;
}
else if (ThisSum<0)
{
ThisSum = 0;
LeftPosition = i+1;
}
}
if(MaxSum<0)MaxSum=0;
printf("%d %d %d",(int)MaxSum, a[Left], a[RightPosition]);
}