PTA甲级——1007

1007 Maximum Subsequence Sum

Given a sequence of K integers { N1, N2, …, N K }. A continuous subsequence is defined to be { N i, N i+1, …, N j } where 1≤ijK. 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:

1
2
10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

1
10 1 4

思路

​ 大致题意:简单来说就是给你一个数列,然后需要你找到和最大的序列,需要你输出最大值、序列最左边的数值和最右边的数值,如果和相同的则输出下标位置小的。如果没有则输出0还有数列的第1个元素和最后1个元素。

​ 思路一:最暴力,把所有的序列的和都列举出来然后寻找一下就ok;

​ 思路二:

代码一(暴力!!!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// 这样会有一个案例过不了,因为这个案例
// 10
// -10 0 -2 -3 -4 -5 -23 -3 -7 -21
#include <iostream>
using namespace std;
const int N = 1e4 + 10;
int n;
long long a[N], b[N];

int main()
{
int k, count;
cin >> k;
for (int i = 1; i <= k; i++) {
long long x;
cin >> x;
b[i] = x;
a[i] = a[i - 1] + x;
}
int ll = 0, rr = 0;
long long res = 0;
for (int l = 1; l <= k; l++) {
for (int r = l; r <= k; r++) {
long long kk = a[r] - a[l - 1];
if (res < kk) {
ll = l;
rr = r;
res = kk;
}
}
}

if (ll == 0 && rr == 0) cout << 0 << " " << b[1] << " " << b[k];
else {
cout << res << " " << b[ll] << " " << b[rr];
}
}

// 下面是正解
#include <iostream>
using namespace std;
const int N = 1e4 + 10;
int n;
long long a[N], b[N];

int main()
{
int k, count;
cin >> k;
for (int i = 1; i <= k; i++) {
long long x;
cin >> x;
b[i] = x;
a[i] = a[i - 1] + x;
}
int ll = 0, rr = 0;
double res = -0.1;
for (int l = 1; l <= k; l++) {
for (int r = l; r <= k; r++) {
long long kk = a[r] - a[l - 1];
// cout << kk << " " << res << '\n';
if (res < kk) {
ll = l;
rr = r;
res = kk;
}
}
}

if (ll == 0 && rr == 0) cout << 0 << " " << b[1] << " " << b[k];
else {
cout << res << " " << b[ll] << " " << b[rr];
}
}

代码二(贪心思路,也是一种经典解法)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
#include<string>
using namespace std;
int a[10010];
int main(){
int n;
cin >> n;
int sum = -1, tmp = 0, tmpx = 1;
int left = 1, right = n;
for(int i = 1; i <= n; i++){
cin >> a[i];
tmp += a[i];
if(tmp < 0){
tmp = 0;
tmpx = i+1;
}else if(tmp > sum){
sum = tmp;
left = tmpx;
right = i;
}
}
if(sum < 0)sum = 0;
cout<< sum << " " << a[left] << " " << a[right];
return 0;
}