Maximum Subsequence Sum

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 ≤ 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

 

解题思路

  这题其实与最大子列和问题相同,只不过输出最大子列和的同时,还要求输出该序列的下标位置范围。关于最大子列和问题,可以查看我的这篇文章:https://www.cnblogs.com/onlyblues/p/14811372.html

  这里还是给出三种方法的代码,需要修改的部分并不多。

  AC代码:

复制代码
 1 #include <cstdio>
 2 
 3 void maxSubSeqSum(int *a, int n);
 4 
 5 int main() {
 6     int n;
 7     scanf("%d", &n);
 8     int a[n];
 9     for (int i = 0; i < n; i++) {
10         scanf("%d", a + i);
11     }
12     maxSubSeqSum(a, n);
13     
14     return 0;
15 }
16 
17 void maxSubSeqSum(int *a, int n) {
18     int beg = 0, end = 0, maxSum = -1;
19     for (int i = 0; i < n; i++) {
20         int ret = 0;
21         for (int j = i; j < n; j++) {
22             ret += a[j];
23             if (ret > maxSum) {
24                 maxSum = ret;
25                 beg = i;
26                 end = j;
27             }
28         }
29     }
30     
31     if (maxSum < 0) printf("%d %d %d", 0, a[0], a[n - 1]);
32     else printf("%d %d %d", maxSum, a[beg], a[end]);
33 }
复制代码

复制代码
 1 #include <cstdio>
 2 
 3 struct Node {
 4     int maxSum, beg, end;
 5 };
 6 
 7 Node maxSubSeqSum(int *a, int low, int high);
 8 Node max(Node &a, Node &b, Node &c);
 9 
10 int main() {
11     int n;
12     scanf("%d", &n);
13     int a[n];
14     for (int i = 0; i < n; i++) {
15         scanf("%d", a + i);
16     }
17     Node ret = maxSubSeqSum(a, 0, n - 1);
18     if (ret.maxSum < 0) printf("%d %d %d", 0, a[0], a[n - 1]);
19     else printf("%d %d %d", ret.maxSum, a[ret.beg], a[ret.end]);
20     
21     return 0;
22 }
23 
24 Node maxSubSeqSum(int *a, int low, int high) {
25     Node node;
26     if (low == high) {
27         if (a[low] >= 0) return node = {a[low], low, high};
28         else return node = {-1, low, high};
29     }
30     
31     int mid = low + high >> 1;
32     Node leftMaxSum = maxSubSeqSum(a, low, mid);
33     Node rightMaxSum = maxSubSeqSum(a, mid + 1, high);
34     
35     int leftMax = -1, rightMax = -1, sum = 0, beg = mid, end = mid;
36     for (int i = mid; i >= low; i--) {
37         sum += a[i];
38         if (sum >= leftMax) {
39             leftMax = sum;
40             beg = i;
41         }
42     }
43     sum = 0;
44     for (int i = mid + 1; i <= high; i++) {
45         sum += a[i];
46         if (sum > rightMax) {
47             rightMax = sum;
48             end = i;
49         }
50     }
51     int maxSum = leftMax >= 0 ? rightMax >= 0 ? leftMax + rightMax : leftMax : rightMax >= 0 ? rightMax : -1;
52     node = {maxSum, beg, end};
53     
54     return max(leftMaxSum, rightMaxSum, node);
55 }
56 
57 Node max(Node &a, Node &b, Node &c) {
58     if (a.maxSum > b.maxSum) {
59         if (a.maxSum > c.maxSum) return a;
60         else return c;
61     }
62     else {
63         if (b.maxSum > c.maxSum) return b;
64         else return c;
65     }
66 }
复制代码

复制代码
 1 #include <cstdio>
 2 
 3 int main() {
 4     int n;
 5     scanf("%d", &n);
 6     int a[n];
 7     for (int i = 0 ; i < n; i++) {
 8         scanf("%d", a + i);
 9     }
10     
11     int sum = 0, maxSum = -1, beg = 0, end = 0, last = 0;
12     for (int i = 0; i < n; i++) {
13         sum += a[i];
14         if (sum > maxSum) {
15             maxSum = sum;
16             beg = last;
17             end = i;
18         }
19         else if (sum < 0) {
20             sum = 0;
21             last = i + 1;
22         }
23     }
24     
25     if (maxSum < 0) printf("%d %d %d", 0, a[0], a[n - 1]);
26     else printf("%d %d %d", maxSum, a[beg], a[end]);
27     
28     return 0;
29 }
复制代码

posted @   onlyblues  阅读(177)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
Web Analytics
点击右上角即可分享
微信分享提示