BZOJ-1855: [Scoi2010]股票交易(DP+单调队列优化)

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

裸的DP,单调队列优化一下。

代码:

#include 
#include 
#include 
 
using namespace std ;
 
#define MAXN 2010
#define inf 0x7fffffff
#define MAXP 2010
 
int n , maxp , w , f[ MAXN ][ MAXP ] ;
int ap[ MAXN ] , bp[ MAXN ] , as[ MAXN ] , bs[ MAXN ] ;
int q[ MAXN ] , head , tail , ans = 0 ;
 
int main(  ) {
    scanf( "%d%d%d" , &n , &maxp , &w ) ;
    for ( int i = 0 ; i ++ < n ; ) scanf( "%d%d%d%d" , ap + i , bp + i , as + i , bs + i ) ;
    memset( f , 129 , sizeof( f ) ) ;
    for ( int i = 0 ; i <= n ; ++ i ) f[ i ][ 0 ] = 0 ;
    for ( int i = 0 ; i ++ < n ; ) {
        int t = max( i - w - 1 , 0 ) ;
        head = 1 , tail = 0 ;
        for ( int j = 0 ; j <= maxp ; ++ j ) f[ i ][ j ] = f[ i - 1 ][ j ] ;
        for ( int j = 0 ; j <= maxp ; ++ j ) {
            while ( head <= tail && q[ head ] < j - as[ i ] ) ++ head ;
            while ( head <= tail && f[ t ][ q[ tail ] ] + ap[ i ] * q[ tail ] <= f[ t ][ j ] + ap[ i ] * j ) -- tail ;
            q[ ++ tail ] = j ;
            f[ i ][ j ] = max( f[ i ][ j ] , f[ t ][ q[ head ] ] - ap[ i ] * ( j - q[ head ] ) ) ;
        }
        head = 1 , tail = 0 ;
        for ( int j = maxp ; j >= 0 ; -- j ) {
            while ( head <= tail && q[ head ] > j + bs[ i ] ) ++ head ;
            while ( head <= tail && f[ t ][ q[ tail ] ] + bp[ i ] * q[ tail ] <= f[ t ][ j ] + bp[ i ] * j ) -- tail ;
            q[ ++ tail ] = j ;
            f[ i ][ j ] = max( f[ i ][ j ] , f[ t ][ q[ head ] ] + bp[ i ] * ( q[ head ] - j ) ) ;
        }
    }
    for ( int i = 0 ; i ++ < n ; ) for ( int j = 0 ; j <= maxp ; ++ j ) {
        ans = max( ans , f[ i ][ j ] ) ;
    }
    printf( "%d\n" , ans ) ;
    return 0 ; 
}

你可能感兴趣的:(BZOJ-1855: [Scoi2010]股票交易(DP+单调队列优化))