LuoGuP1970花匠

LuoGuP1970花匠

简化题意 (:)

给定一个序列,求最长的波浪子序列.

单峰的意思是存在一个 (v_i) 使得 (v_i > v_{i-1},v_i > v_{i+1})(1)({i-1}) 单调,(i+1)(n) 也单调(单调性相反).

(f_{i,0/1}) 表示前 (i) 个数字中,形状是 (lor) 或者 (land) 的最长长度.

每次比较当前元素和上一个元素的大小,考虑继承即可.

#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 one first
#define two second
#define rint read<int>
#define int long long
#define pb push_back
#define db double
#define ull unsigned long long
#define lowbit(x) ( x & ( - x ) )

using std::queue ;
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 n , v[N] ;
int f[N][2] ;

signed main (int argc , char * argv[]) {
    n = rint () ; rep ( i , 1 , n ) v[i] = rint () ;
    f[1][0] = f[1][1] = 1 ;
    rep ( i , 2 , n ) {
        if ( v[i] > v[i-1] ) f[i][0] = max ( f[i-1][0] , f[i-1][1] + 1ll ) , f[i][1] = f[i-1][1] ;
        else if ( v[i] < v[i-1] ) f[i][1] = max ( f[i-1][1] , f[i-1][0] + 1ll ) , f[i][0] = f[i-1][0] ;
        else f[i][1] = f[i-1][1] , f[i][0] = f[i-1][0] ;
    }
    printf ("%lld
" , max ( f[n][0] , f[n][1] ) ) ;
    #ifndef ONLINE_JUDGE
    system ("pause") ;
    #endif
    return 0 ;
}
原文地址:https://www.cnblogs.com/Equinox-Flower/p/11756778.html