首页 > 代码库 > UVA 10534 Wavio Sequence
UVA 10534 Wavio Sequence
题解:
根据最长上升子序列的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; }
UVA 10534 Wavio Sequence
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。