甲级1007 Maximum Subsequence Sum

Given a sequence of K integers { N1​​, N2​​, ..., NK​​ }. A continuous subsequence is defined to be { Ni​​, Ni+1​​, ..., Nj​​ } where 1. 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 (≤). 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


分析:最大子序列和的问题

这道题最好的解法是动态规划,但我并没有用,反而用了更麻烦的方法(分治法),打算在另一篇博文里再回忆回忆动态规划吧。

代码如下:
 1 #include <iostream>
 2 #include <limits>
 3 using namespace std;
 4 
 5 int K;
 6 int res[10005];
 7 struct max_subsequece
 8 {
 9     int low;
10     int high;
11     int sum;
12     max_subsequece(int a, int b, int c)
13     {
14         low = a;
15         high = b;
16         sum = c;
17     }
18 };
19 
20 max_subsequece find_max_crossing_subsequece(int *res, int low, int mid, int high)
21 {
22     int left_max_sum = numeric_limits<int>::lowest();
23     int right_max_sum = numeric_limits<int>::lowest();
24     int left, right;
25     int sum = 0;
26     for (int i = mid; i >= low; i--)
27     {
28         sum += res[i];
29         if (sum > left_max_sum)
30         {
31             left_max_sum = sum;
32             left = i;
33         }
34     }
35     sum = 0;
36     for (int i = mid + 1; i <= high; i++)
37     {
38         sum += res[i];
39         if (sum > right_max_sum)
40         {
41             right_max_sum = sum;
42             right = i;
43         }
44     }
45     return max_subsequece(left, right, left_max_sum + right_max_sum);
46 }
47 
48 max_subsequece find_max_subsequece(int *res, int low, int high)
49 {
50     if (low == high) return max_subsequece(low, high, res[low]);
51     
52     int mid = (low + high) / 2;
53     max_subsequece left_max = find_max_subsequece(res, low, mid);
54     max_subsequece right_max = find_max_subsequece(res, mid + 1, high);
55     max_subsequece crossing_max = find_max_crossing_subsequece(res, low, mid, high);
56     if (left_max.sum >= crossing_max.sum && left_max.sum >= right_max.sum)
57         return left_max;
58     if (crossing_max.sum >= left_max.sum && crossing_max.sum >= right_max.sum)
59         return crossing_max;
60     if (right_max.sum >= left_max.sum && right_max.sum >= crossing_max.sum)
61         return right_max;
62 }
63 
64 int main()
65 {
66     scanf("%d", &K);
67     for (int i = 0; i < K; i++)
68         scanf("%d", &res[i]);
69     int i;
70     for (i = 0; i < K; i++)
71     {
72         if (res[i] >= 0)    break;
73     }
74     if (i == K)
75     {
76         printf("0 %d %d", res[0], res[K - 1]);
77         return 0;
78     }
79     max_subsequece result = find_max_subsequece(res, 0, K - 1);
80     printf("%d %d %d", result.sum, res[result.low], res[result.high]);
81     return 0;
82 }

 

思路可以见算法导论上第40页开始,用分治法解决最大子序列和

 

下面贴上其他人用动态规划解决这道题的代码:

#include <iostream>
#include <vector>
using namespace std;
int main() {
    int n;
    scanf("%d", &n);
    vector<int> v(n);
    int leftindex = 0, rightindex = n - 1, sum = -1, temp = 0, tempindex = 0;
    for (int i = 0; i < n; i++) {
        scanf("%d", &v[i]);
        temp = temp + v[i];
        if (temp < 0) {
            temp = 0;
            tempindex = i + 1;
        } else if (temp > sum) {
            sum = temp;
            leftindex = tempindex;
            rightindex = i;
        }
    }
    if (sum < 0) sum = 0;
    printf("%d %d %d", sum, v[leftindex], v[rightindex]);
    return 0;
}

转载自:柳婼
链接地址:https://blog.csdn.net/liuchuo/article/details/52144554


posted @ 2019-01-22 22:27  囧YY  阅读(149)  评论(0编辑  收藏  举报