社区讨论

二分栈做法Wa求助,代码带注释小清新

P3195[HNOI2008] 玩具装箱参与者 3已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@lo3aq2h1
此快照首次捕获于
2023/10/24 03:33
2 年前
此快照最后确认于
2023/10/24 03:33
2 年前
查看原帖
萌新刚学OI,代码求调
CPP
#include <bits/stdc++.h>
// #pragma GCC optimize(2)
#define pcn putchar( '\n' )
#define ll long long
#define pii pair< int , int >
#define pll pair< ll , ll >
#define MP make_pair
#define fi first
#define se second
#define melodyset multiset
#define melodymap multimap
#define Min( a , b ) ( a = min( a , b ) )
#define Max( a , b ) ( a = max( a , b ) )
#define For( i , j , k ) for( int i = ( j ) ; i <= ( k ) ; ++ i )
#define For__( i , j , k ) for( int i = ( j ) ; i >= ( k ) ; -- i )
#define Forv( i , v ) for( auto i : v )
#define Foriv( i , ii , v ) for( int i = 0 , ii = v.size() ; i < ii ; ++ i )
#define Foriv__( i , v ) for( int i = v.size() - 1 ; i >= 0 ; -- i )
#define Fore( i , j , k ) for( int i = ( j ) ; i ; i = ( k ) )
#define UnorderedMapFineture( m ) m.reserve( 1024 ) , m.max_load_factor( 0.25 )
// #define random( l , r ) ( ( 1ll * rand() * rand() % ( r - l + 1 ) + rand() ) % ( r - l + 1 ) + l )
using namespace std;

namespace FastIO {
    const int __SIZE = (1 << 21) + 1;
    char ibuf[__SIZE], *iS, *iT, obuf[__SIZE], *oS = obuf, *oT = oS + __SIZE - 1, __c, qu[55]; int __f, qr, _eof;
    #define Gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
    inline void flush () { fwrite (obuf, 1, oS - obuf, stdout), oS = obuf; }
    inline void gc (char &x) { x = Gc(); }
    inline void pc (char x) { *oS ++ = x; if (oS == oT) flush (); }
    inline void pstr (const char *s) { int __len = strlen(s); for (__f = 0; __f < __len; ++__f) pc (s[__f]); }
    inline void gstr (char *s) { for(__c = Gc(); __c < 32 || __c > 126 || __c == ' ';)  __c = Gc();
        for(; __c > 31 && __c < 127 && __c != ' ' && __c != '\n' && __c != '\r'; ++s, __c = Gc()) {*s = __c;} *s = 0; }
    template <class I> inline bool read (I &x) { _eof = 0;
        for (__f = 1, __c = Gc(); (__c < '0' || __c > '9') && !_eof; __c = Gc()) { if (__c == '-') __f = -1; _eof |= __c == EOF; }
        for (x = 0; __c <= '9' && __c >= '0' && !_eof; __c = Gc()) {x = x * 10 + (__c & 15), _eof |= __c == EOF;} x *= __f; return !_eof; }
    template <class I> inline void Write (I x) { if (!x) pc ('0'); if (x < 0) pc ('-'), x = -x;
        while (x) {qu[++ qr] = x % 10 + '0',  x /= 10;} while (qr) pc (qu[qr --]); }
    struct Flusher_ {~Flusher_(){flush();}}FastIO_flusher_;
    #define writen( x ) write( x ) , pc( '\n' )
    #define writes( x ) write( x ) , pc( ' ' )
}
namespace IO {
    template < typename T > inline T read( T &num ){
        num = 0 ; T f = 1 ; char c = ' '; while( c < '0' || c > '9' ) if( ( c = getchar() ) == '-' ) f = -1;
        while( c >= '0' && c <= '9' ) num = ( num << 1 ) + ( num << 3 ) + ( c ^ 48 ) , c = getchar();
        return num *= f;
    }
    template < typename T > inline void Write( T x ){
        if( x < 0 ) putchar( '-' ) , x = -x ; if( x == 0 ) { putchar( '0' ); return ; }
        if( x > 9 ) Write( x / 10 ); putchar( x % 10 + '0' ); return ;
    }
    inline void putc( string s ){ int len = s.size() - 1; For( i , 0 , len ) putchar( s[ i ] ); }
    template < typename T > inline void write( T x , string s = "\0" ){ Write( x ) , putc( s ); }
}

using namespace IO ;
//using namespace FastIO ;

/* ====================================== */

const int maxn = 5e4 + 50;
const ll INF = ( 1ll << 60 );
int n;
ll L;
ll a[ maxn ] , sum[ maxn ];
ll dp[ maxn ];

struct BoundStack{
    struct Node{ int from , l , r; } line[ maxn ];// 当前更新来源于dp[ from ],影响到[ l , r ]
    int l = 1 , r = 0;// 二分栈的左右端点

    inline ll calc( int i , int j ){// 求( i , j ]的更新
        if( i >= j ) return INF;
        ll t = sum[ j ] - sum[ i ] + j - i - 1 - L;
        return dp[ i ] + t * t;
    }
    inline void add( int x ){
        while( l <= r && calc( line[ r ].from , line[ r ].l ) >= calc( x , line[ r ].l ) ) -- r;
        // 弹出栈内比当前更新劣的区间
        if( l > r ){
            line[ ++ r ] = Node{ x , 1 , n };
            return ;
        }// 特盼全部区间弹空的情况
        int L = line[ r ].l , R = line[ r ].r , ans = R , mid;
        while( L <= R ){
            mid = L + R >> 1;
            if( calc( line[ r ].from , mid ) >= calc( x , mid ) )
                R = mid - 1 , ans = mid;
            else L = mid + 1;
        }// 二分当前更新应该割多少给当前区间
        line[ r ].r = ans - 1;
        if( line[ r ].l >= line[ r ].r ) -- r;
        line[ ++ r ] = Node{ x , ans , n };
    }
    inline ll query( int x ){
        while( l < r && line[ l ].r < x ) ++ l;// 把不是当前区间的弹出
        return calc( line[ l ].from , x );
    }
} bs;

int main() {

//  freopen( "P3195_1.in" , "r" , stdin );
//  freopen( "data.out" , "w" , stdout );

    read( n ) , read( L );
    For( i , 1 , n ){
        read( a[ i ] );
        sum[ i ] = sum[ i - 1 ] + a[ i ];
    }
    For( i , 1 , n ){
        bs.add( i - 1 );
        dp[ i ] = bs.query( i );
    }
    write( dp[ n ] );

    return 0;
}
我的代码应该小清新吧

回复

2 条回复,欢迎继续交流。

正在加载回复...