PAT 1007 Maximum Subsequence Sum (25 分) -甲级

1007 Maximum Subsequence Sum (25 分)

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:

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

Sample Output:

10 1 4
#include <stdio.h>      //两个都AC了
#include <algorithm>

using namespace std;

int main() {
    int n, flag = 0;
    int num[10010], dp[10010] = {0};
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d", num + i);
        if (num[i] >= 0) {
            flag = 1;
        }
    }
    if (flag == 0) {
        printf("0 %d %d", num[0], num[n - 1]);
        return 0;
    }
    dp[0] = num[0];
    for (int j = 1; j < n; ++j) {
        dp[j] = max(dp[j - 1], 0) + num[j];
    }
    int max = 0;
    for (int k = 0; k < n; ++k) {
        if (dp[max] < dp[k]) {
            max = k;
        }
    }
    int first = max;
    for (int l = max - 1; l >= 0; --l) {
        if (dp[l] <= 0) {
            first = l + 1;
            break;
        }
        if (l == 0) {
            first = 0;
        }
    }
    printf("%d %d %d", dp[max], num[first], num[max]);
    return 0;
}

寻找序列首尾元素的另一种方法:

#include <stdio.h>

using namespace std;

int main() {
    int K;
    int num[10010] = {0}, dp[10010] = {0}, s[10010] = {0};
    int flag = 0;
    scanf("%d", &K);
    for (int i = 0; i < K; ++i) {
        scanf("%d", num + i);
        if (num[i] >= 0) {
            flag = 1;
        }
    }
    if (flag == 0) {
        printf("0 %d %d", num[0], num[K - 1]);
        return 0;
    }
    dp[0] = num[0];
    for (int j = 1; j < K; ++j) {
        if (dp[j - 1] <= 0) {
            dp[j] = num[j];
            s[j] = j;
        } else {
            dp[j] = dp[j - 1] + num[j];
            s[j] = s[j - 1];
        }
    }
    int max = 0;
    for (int k = 1; k < K; ++k) {
        if (dp[k] > dp[max]) {
            max = k;
        }
    }
    printf("%d %d %d", dp[max], num[s[max]], num[max]);
    return 0;
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页