题解:
根据最长上升子序列的nlog(n)做法,求出以每个点结尾的最长上升子序列。
然后反过来再求一次
然后取最小值 * 2 - 1 即可
代码:
#include<bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define se second #define fs first #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define pii pair<int,int> const int INF = 1000000000; const int maxn = 10050; const int M = 4000; int n; int a[ maxn ], b[ maxn ]; int num[ maxn ],num2[ maxn ]; int bs( int s, int e, int x ) { int l = s, r = e, goal = e + 1; while( l <= r ) { int m =( l + r ) >> 1; if( b[ m ] >= x ) { goal = m; r = m -1; } else l = m + 1; } return goal; } int main() { int n; while( ~scanf( "%d", &n ) ) { for( int i = 1; i <= n; i ++ ) scanf( "%d", &a[ i ] ); int tmp = 1; b[ tmp ] = a[ tmp ]; num[ tmp ] = 1; for( int i = 2; i <= n; i ++ ) { int pos =bs( 1, tmp, a[ i ] ); b[ pos ] = a[ i ]; num[ i ] = pos; if( pos == tmp + 1 ) tmp ++; } reverse( a + 1, a + n + 1 ); tmp = 1; b[ tmp ] = a[ tmp ]; num2[ tmp ] = 1; for( int i = 2; i <= n; i ++ ) { int pos =bs( 1, tmp, a[ i ] ); b[ pos ] = a[ i ]; num2[ i ] = pos; if( pos == tmp + 1 ) tmp ++; } int G = 0; for( int i = 1; i <= n; i ++ ) G = max( G, min( num[ i ], num2[ n + 1 - i ] ) * 2 - 1 ); printf( "%d\n", G ); } return 0; }
时间: 2024-10-17 02:46:17