ZROI#592

ZROI#592

题目链接
简化题意:
给定一个序列,求出以每个元素为端点的使得和最大的子段的另一个端点.
就按照最大子段和的常规(DP),记录一下转移,正反分别跑一遍就行了.
输出的时候正着反着哪一边大就输出哪一边的端点.
(Code:)

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#define MEM(x,y) memset ( x , y , sizeof ( x ) )
#define rep(i,a,b) for (int i = a ; i <= b ; ++ i)
#define per(i,a,b) for (int i = a ; i >= b ; -- i)
#define pii pair < int , int >
#define X first
#define Y second
#define rint read<int>
#define int long long
#define pb push_back

using std::set ;
using std::pair ;
using std::max ;
using std::min ;
using std::priority_queue ;
using std::vector ;
using std::swap ;
using std::sort ;
using std::unique ;
using std::greater ;

template < class T >
    inline T read () {
        T x = 0 , f = 1 ; char ch = getchar () ;
        while ( ch < '0' || ch > '9' ) {
            if ( ch == '-' ) f = - 1 ;
            ch = getchar () ;
        }
        while ( ch >= '0' && ch <= '9' ) {
            x = ( x << 3 ) + ( x << 1 ) + ( ch - 48 ) ;
            ch = getchar () ;
       }
   return f * x ;
}

const int N = 1e5 + 100 ;

int f[N] , n , v[N] , g[N] ;
int pre[N] , suf[N] ;

signed main (int argc , char * argv[] ) {
    n = rint () ; rep ( i , 1 , n ) v[i] = rint () ;
    f[1] = v[1] ; g[n] = v[n] ;
    rep ( i , 1 , n ) suf[i] = pre[i] = i ;
    rep ( i , 2 , n ) {
        f[i] = max ( v[i] , f[i-1] + v[i] ) ;
        if ( v[i] > f[i-1] + v[i] ) pre[i] = pre[i] ;
        else pre[i] = pre[i-1] ;
    }
    per ( i , n - 1 , 1 ) {
        g[i] = max ( v[i] , g[i+1] + v[i] ) ;
        if ( v[i] >= g[i+1] + v[i] ) suf[i] = suf[i] ;
        else suf[i] = suf[i+1] ;
    }
    rep ( i , 1 , n ) printf ("%lld " , f[i] >= g[i] ? pre[i] : suf[i] ) ;
    return 0 ;
}
May you return with a young heart after years of fighting.
原文地址:https://www.cnblogs.com/Equinox-Flower/p/11459475.html