BZOJ-1264: [AHOI2006]基因匹配Match(LIS)

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1264

转成LIS来搞,做法很神啊。。。:http://hi.baidu.com/cenyk1230/item/2e5216ca19b1af140ad93ae1

代码:

#include 
#include 
#include 
#include 
 
using namespace std ;
 
const int maxn = 501000 ;
 
int a[ maxn ] , b[ maxn ] , c[ maxn ] , n , m = 0 ;
 
struct BIT {
    int I[ maxn ] , N ;
    BIT(  ) {
        memset( I , 0 , sizeof( I ) ) ;
    }
    void INIT( int _N ) {
        N = _N ;
    }
    int lowbit( int x ) {
        return x & ( - x ) ;
    }
    void Add( int pos , int val ) {
        for ( ; pos <= N ; pos += lowbit( pos ) ) {
            I[ pos ] = max( I[ pos ] , val ) ;
        }
    }
    int Max( int pos ) {
        int temp = 0 ;
        for ( ; pos ; pos -= lowbit( pos ) ) {
            temp = max( temp , I[ pos ] ) ;
        }
        return temp ;
    }
} bit ;
 
vector < int > V[ maxn ] ;
int f[ maxn ] , ans = 0 ;
 
int main(  ) {
    scanf( "%d" , &n ) ;
    for ( int i = 1 , rec = n * 5 ; i <= rec ; ++ i ) {
        scanf( "%d" , a + i ) ;
    }
    for ( int i = 1 , rec = n * 5 ; i <= rec ; ++ i ) {
        scanf( "%d" , b + i ) ;
        V[ b[ i ] ].push_back( i ) ;
    }
    for ( int i = 1 , rec = n * 5 ; i <= rec ; ++ i ) {
        for ( int j = 4 ; j >= 0 ; -- j ) {
            c[ ++ m ] = V[ a[ i ] ][ j ] ;
        }
    }
    bit.INIT( n * 5 ) ;
    for ( int i = 0 ; i ++ < m ; ) {
        f[ i ] = bit.Max( c[ i ] - 1 ) + 1 ; 
        bit.Add( c[ i ] , f[ i ] ) ;
        ans = max( ans , f[ i ] ) ;
    }
    printf( "%d\n" , ans ) ;
    return 0 ; 
}

你可能感兴趣的:(BZOJ-1264: [AHOI2006]基因匹配Match(LIS))