BZOJ-1492: [NOI2007]货币兑换Cash(动态规划+动态维护凸壳)

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std ;
#define cal( p0 , p1 ) ( ( p0.y - p1.y ) / ( p0.x - p1.x ) )
#define MAXN 101000
#define X( x ) ( ( r[ x ] * f[ x ] ) / ( a[ x ] * r[ x ] + b[ x ] ) )
#define Y( x ) ( f[ x ] / ( a[ x ] * r[ x ] + b[ x ] ) )
#define fun( i , p ) ( a[ i ] * p.x + b[ i ] * p.y )
#define clear( x ) memset( x , 0 , sizeof( x ) )
#define update( t ) S( t ) = S( L( t ) ) + S( R( t ) ) + 1
#define L( t ) left[ t ]
#define R( t ) right[ t ]
#define K( t ) key[ t ]
#define S( t ) size[ t ]
const double inf = 100000000 ;
const double INF = inf * inf ;
struct itype {
    double x , y ;
    bool operator < ( const itype &a ) const {
        return x < a.x ;
    }
    bool operator == ( const itype &a ) const {
        return x == a.x ;
    }
    bool operator > ( const itype &a ) const {
        return x > a.x ;
    }
} key[ MAXN ] ;
int left[ MAXN ] , right[ MAXN ] , size[ MAXN ] , V = 0 , roof = 0 ;
double lk[ MAXN ] , rk[ MAXN ] ;
itype make( double x , double y ) {
    itype u ;
    u.x = x , u.y = y ;
    return u ;
}
void Left( int &t ) {
    int k = R( t ) ;
    R( t ) = L( k ) ; update( t ) ;
    L( k ) = t ; update( k ) ;
    t = k ;
}
void Right( int &t ) {
    int k = L( t ) ;
    L( t ) = R( k ) ; update( t ) ;
    R( k ) = t ; update( k ) ;
    t = k ;
}
void maintain( int &t ) {
    if ( S( L( L( t ) ) ) > S( R( t ) ) ) {
        Right( t ) ;
        maintain( R( t ) ) ; maintain( t ) ;
        return ;
    }
    if ( S( R( L( t ) ) ) > S( R( t ) ) ) {
        Left( L( t ) ) ; Right( t ) ;
        maintain( L( t ) ) , maintain( R( t ) ) ; maintain( t ) ;
        return ;
    }
    if ( S( R( R( t ) ) ) > S( L( t ) ) ) {
        Left( t ) ;
        maintain( L( t ) ) ; maintain( t ) ;
        return ;
    }
    if ( S( L( R( t ) ) ) > S( L( t ) ) ) {
        Right( R( t ) ) ; Left( t ) ;
        maintain( L( t ) ) , maintain( R( t ) ) ; maintain( t ) ;
        return ;
    }
}
void Insert( itype k , int &t ) {
    if ( ! t ) {
        t = ++ V ;
        S( t ) = 1 , K( t ) = k ;
        return ;
    }
    Insert( k , k < K( t ) ? L( t ) : R( t ) ) ;
    update( t ) ; maintain( t ) ;
}
void Delete( itype k , int &t ) {
    if ( K( t ) == k ) {
        if ( ! L( t ) ) {
            t = R( t ) ; return ;
        } else if ( ! R( t ) ) {
            t = L( t ) ; return ;
        } else {
            Right( t ) ; Delete( k , R( t ) ) ;
        }
    } else Delete( k , k < K( t ) ? L( t ) : R( t ) ) ;
    update( t ) ; maintain( t ) ;
}
itype Prefix( itype k , int t ) {
    if ( ! t ) return make( - INF , - INF ) ;
    if ( k > K( t ) ) return max( K( t ) , Prefix( k , R( t ) ) ) ;
    return Prefix( k , L( t ) ) ;
}
itype Suffix( itype k , int t ) {
    if ( ! t ) return make( INF , INF ) ;
    if ( K( t ) > k ) return min( K( t ) , Suffix( k , L( t ) ) ) ;
    return Suffix( k , R( t ) ) ;
}
int Find( itype k , int t ) {
    if ( ! t ) return 0 ;
    if ( k == K( t ) ) return t ;
    return Find( k , k < K( t ) ? L( t ) : R( t ) ) ;
}
void Push( itype k ) {
    int t = Find( k , roof ) ;
    if ( t ) {
        if ( K( t ).y >= k.y ) return ;
        Delete( K( t ) , roof ) ;
    }
    itype pre = Prefix( k , roof ) , suff = Suffix( k , roof ) ;
    if ( cal( pre , k ) > cal( pre , suff ) ) {
        for ( ; pre.y != - INF ; ) {
            itype ret = Prefix( pre , roof ) ;
            if ( cal( ret , pre ) <= cal( pre , k ) ) {
                Delete( pre , roof ) ;
                pre = ret ;
            } else break ;
        }
        for ( ; suff.y != - INF ; ) {
            itype ret = Suffix( suff , roof ) ;
            if ( cal( k , ret ) >= cal( k , suff ) ) {
                Delete( suff , roof ) ;
                suff = ret ;
            } else break ;
        }
        Insert( k , roof ) ;
        lk[ V ] = cal( pre , k ) , rk[ V ] = cal( k , suff ) ;
        rk[ Find( pre , roof ) ] = cal( pre , k ) ;
        lk[ Find( suff , roof ) ] = cal( k , suff ) ;
    }
}
itype Select( double k , int t ) {
    if ( K( t ).x == - inf ) return Select( k , R( t ) ) ;
    if ( K( t ).x == inf ) return Select( k , L( t ) ) ;
    if ( lk[ t ] >= k && k >= rk[ t ] ) return K( t ) ;
    if ( rk[ t ] > k ) return Select( k , R( t ) ) ;
    if ( lk[ t ] < k ) return Select( k , L( t ) ) ;
}
 
double a[ MAXN ] , b[ MAXN ] , r[ MAXN ] , f[ MAXN ] , money ;
int n ;
int main(  ) {
    clear( left ) , clear( right ) , clear( size ) ;
    Insert( make( 0 , - INF ) , roof ) , Insert( make( inf , - INF ) , roof ) ;
    scanf( "%d%lf" , &n , &money ) ;
    for ( int i = 0 ; i ++ < n ; ) scanf( "%lf%lf%lf" , a + i , b + i , r + i ) ;
    for ( int i = 0 ; i ++ < n ; ) {
        f[ i ] = max( money , f[ i - 1 ] ) ;
        if ( i > 1 ) {
            itype p = Select( - a[ i ] / b[ i ] , roof ) ;
            f[ i ] = max( f[ i ] , fun( i , p ) ) ;
        }
        Push( make( X( i ) , Y( i ) ) ) ;
    }
    printf( "%.3f\n" , f[ n ] ) ;
    return 0 ;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值