1007 Maximum Subsequence Sum (25)(25 分)



1 题目


Given a sequence of K integers { N~1~, N~2~, …, N~K~ }. A continuous subsequence is defined to be { N~i~, N~i+1~, …, N~j~ } 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.

where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID’s of its children. For the sake of simplicity, let us fix the root ID to be 01.

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.

The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output “0 1” in a line.

Sample Input

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

Sample Output

10 1 4

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805514284679168

2 解题思路

  这是一道动态规划dynamic programming,DP)的题目。考虑,在输入的时候就开始进行累加,设置一个总和sum,上下标low_index和high_index,以及用于每一次输入新元素时,进行试探的temp计算临时总和,以及临时上下标,temp_low_index和循环控制变量i,在输入的同时,进行累加,逻辑如下:
  如果总和一直在增加,则认为这个序列符合要求的,记录临时总和和临时上下标,并更新到总和和上下标;
  如果新增元素使得总和减小,那么不更新上下标,只记录临时总和,期待下一个更大的值;
  如果新增数据使得总和为负,显然,那么这个新的元素明显会拉低前面所有总和,需要重新探测,那么使临时总和赋值零,临时上下标设置为当前值,以重新记录。总和和上下标保持上一次记录。
  最后循环遍历所有的输入元素之后,就能得到最大总和序列,但是要注意特殊情况,如果总和是负的,即整个序列都没有正值,需要输出零。
  对每个输入元素进行探测,是一个子过程,每一个子过程都判断是否能得到符合要求的序列,得到最优解,这是一种动态规划的思想。

3 AC代码

/*
**@Brief: No.1007 of PAT advanced level.
**@Author:Jason.Lee
**@Date: 2018-7-3 
*/

#include<iostream>
#include<string>
#include<vector>

using namespace std;

int main(){
    int N;
    while(cin>>N){
        vector<int> v(N);
        int low_index = 0;
        int high_index = N-1;
        int temp_low_index = 0;
        int sum = -1;
        int temp = 0;
        for(int i=0;i<N;i++){
            cin>>v[i];
            temp+=v[i];
            if(temp<0){
                temp=0;
                temp_low_index = i+1; // 从下一个位置重新开始记录 
            }else if(temp>sum){ // 找到更大值,更新 
                sum = temp;
                low_index = temp_low_index;
                high_index = i;
            }
        }
        if(sum < 0){
            sum = 0;
        }
        cout<<sum<<" "<<v[low_index]<<" "<<v[high_index]<<endl;
    }
    return 0; 
} 

猜你喜欢

转载自blog.csdn.net/CV_Jason/article/details/80910763