1007. Maximum Subsequence Sum (25)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 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). The second line contains 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 and j (as shown by the sample case). If all the 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:
小结:很简单的求最大子列问题,用的也是最经典的方法,稍微不同的是队列全为负数要分开讨论和要列出第一个得到的最大子列的起始值和结束值,注意,不是下标!!我也不知道为什么不是下标!!都是套路!!
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 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). The second line contains 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 and j (as shown by the sample case). If all the 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
以上是题目
以下是代码
#include
<iostream>
#include
<vector>
using
namespace
std;
class
array_class{
public:
int max_id;
int min_id;
int sum;
};
int
main(){
int number;
cin >> number;
vector<
int> a;
//创建容纳数组的容器
int arg;
int pre_count=
0;
for(
int i=
0;i<number;i++){
cin >> arg;
if(arg <
0)
pre_count++;
//记录负数数量用来判断队列是否全为负数
a.
push_back(arg);
}
if(pre_count == number){
cout <<
0 <<
" " << a[
0] <<
" " << a[number-
1] << endl;
return
0;
}
vector<array_class> b;
array_class test_case;
test_case.
min_id =
0;
test_case.
max_id =
0;
test_case.
sum = a[
0];
b.
push_back(test_case);
for(
int i=
1; i<number; i++){
if(b[i-
1].
sum >=
0){
test_case.
sum = b[i-
1].
sum + a[i];
test_case.
min_id = b[i-
1].
min_id;
}
else{
test_case.
sum = a[i];
test_case.
min_id = i;
}
test_case.
max_id = i;
b.
push_back(test_case);
}
//一定要重置一下test_case! 否则如果最后一组最大子列正好是最大值,而前面有子列也取到最大值那么就错了!!坑!!!
test_case.
min_id = b[
0].
min_id;
test_case.
max_id = b[
0].
max_id;
test_case.
sum = b[
0].
sum;
for(vector<array_class>::iterator i = b.
begin();i!=b.
end();i++){
if(i->
sum > test_case.
sum){
test_case.
min_id = i->
min_id;
test_case.
max_id = i->
max_id;
test_case.
sum = i->
sum;
}
}
cout << test_case.
sum <<
" " << a[test_case.
min_id] <<
" " << a[test_case.
max_id] << endl;
return
0;
}