1007 Maximum Subsequence Sum (25point (s)) require two brush * dynamic programming and maximum continuous sequence is extremely important

The basic idea of ​​the key points described in "a data structure of a typical problem"

 

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<vector> 
#include<string>
#include<math.h>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<set>
#include<stack>
using namespace std;

int n;
vector<int>num;
vector<int>dp;
vector<int>root;
int main() {
    cin >> n;
    int st, ed;
    bool flag = true;
    //num.resize(n);
    dp.resize(n);
    root.resize(n);
    int a;
    for (int i = 0; i < n; i++) {
        cin >> a;
        num.push_back(a);
        if (a >= 0)
            flag = false;
    }
    if (flag) {
        printf("% D D D%% \ n- " , 0 , NUM [ 0 ], NUM [num.size () - . 1 ]);
         return  0 ; 
    } 
    DP [ 0 ] = NUM [ 0 ];
     for ( int I = . 1 ; I <n-; I ++ ) {
         IF (DP [I - . 1 ] + NUM [I] <= NUM [I]) {
             // If we add [i] num but smaller; 
            DP [I] = NUM [I ]; 
            the root [I] = I; // start over the opening path 
        }
         the else {
             //If coupled with a larger 
            DP [I] = NUM [I] + DP [I - . 1 ]; 
            the root [I] = the root [I - . 1 ]; 
        } 
    } 
    int K = 0 ;
     for ( int I = . 1 ; I <n-; I ++ ) {
         IF (DP [I]> DP [K]) 
            K = I; 
    } 
    the printf ( " % D% D% D \ n- " , DP [K], NUM [the root [K]], NUM [K]);
     return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/songlinxuan/p/12355382.html