JakeLin-and maximum subsequence-problem-DP

Title description

For a given integer sequence A of length N, its "subsequence" is defined as: a non-empty continuous element (integer) in A. Your task is to find a subsequence among all possible subsequences. The sum of all elements in the subsequence is the largest (compared to all other subsequences). The program requires you to output this maximum value.

Input

The first line of the input file contains an integer N, and the second line contains N integers, meaning A. 
Where 
1 <= N <= 100000 
-10000 <= A [i] <= 10000 

Output

The output contains only an integer, indicating the answer you calculated. 

Sample input

5
3 -2 3 -5 4

Sample output

4

Original title link: and maximum subsequence 

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int num[100010];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&num[i]);
    }
    //2种可能:  1.把新来的数加进当前序列   2.以新来的数重开一个序列   取二者大的情况 
    int res=num[0];  //最终结果 
    int sum=num[0]; //sum:当前序列和 
    for(int i=1;i<n;i++){
        sum += num[i];   //当前的sum加上新来的数 
        sum = max(sum,num[i]);   //看看是这种情况大,还是新来的数重开序列大 ,取大的情况 
        res = max(sum,res);   //看看这一次的决策是否能够更新结果呢 
    }
    cout<<res;
    return 0;
}

 

Published 20 original articles · won 15 · views 217

Guess you like

Origin blog.csdn.net/qq_37414463/article/details/105375995