Submission #868494

# Submission time Handle Problem Language Result Execution time Memory
868494 2023-10-31T14:45:40 Z chonka Chorus (JOI23_chorus) C++17
40 / 100
55 ms 8996 KB
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair < int , int > pii ;
typedef vector < int > vi ;
#define fi first
#define se second
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()

const int MAXN = 1e6 + 7 ;

int n , k ;
int a[ MAXN ] , b[ MAXN ] ;
int lw[ MAXN ] , ori[ MAXN ] ;
ll pref[ MAXN ] ;

ll prec[ 5005 ][ 5005 ] ;

inline ll calc ( int l , int r ) {
    if ( l > r ) { return 0 ; }
    if ( r <= ori[ l ] ) { return 0 ; }
    int aux_st = max ( ori[ l ] , l - 1 ) ;
    return ( pref[ r ] - pref[ aux_st ] ) - 1LL * ( r - aux_st ) * ( l - 1 ) ;
}

ll dp[ MAXN ] ;
int ctr[ MAXN ] ;


int get_fst ( int x , int y ) {
    int l , r , mid ;
    l = max ( x , y ) , r = n ;
    if ( make_pair ( dp[ x ] + calc ( x + 1 , n ) , ctr[ x ] ) < make_pair ( dp[ y ] + calc ( y + 1 , n ) , ctr[ y ] ) ) { return n + 1 ; }
    while ( l < r ) {
        mid = ( l + r ) / 2 ;
        if ( make_pair ( dp[ x ] + calc ( x + 1 , mid ) , ctr[ x ] ) < make_pair ( dp[ y ] + calc ( y + 1 , mid ) , ctr[ y ] ) ) { l = mid + 1 ; }
        else { r = mid ; }
    }
    return l ;
}


pair < ll , int > f ( ll sr ) {
    deque < int > q ;
    q.push_front ( 0 ) ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        while ( (int)q.size ( ) >= 2 && get_fst ( q[ 0 ] , q[ 1 ] ) <= i ) {
            q.pop_front ( ) ;
        }
        while ( (int)q.size ( ) >= 3 && get_fst ( q[ 0 ] , q[ 1 ] ) >= get_fst ( q[ 1 ] , q[ 2 ] ) ) {
            int x = q[ 0 ] ;
            q.pop_front ( ) ; q.pop_front ( ) ;
            q.push_front ( x ) ;
        }
        int x = q.front ( ) ;
        dp[ i ] = dp[ x ] + calc ( x + 1 , i ) + sr ;
        ctr[ i ] = ctr[ x ] + 1 ;
        while ( (int)q.size ( ) >= 1 && get_fst ( q.back ( ) , i ) <= i ) { q.pop_back ( ) ; }
        q.push_back ( i ) ;
        /**
        dp[ i ] = calc ( 1 , i ) + sr , ctr[ i ] = 1 ;
        for ( int j = 1 ; j < i ; ++ j ) {
            auto aux = make_pair ( dp[ j ] + calc ( j + 1 , i ) + sr , ctr[ j ] + 1 ) ;
            if ( make_pair ( dp[ i ] , ctr[ i ] ) > aux ) {
                dp[ i ] = aux.fi , ctr[ i ] = aux.se ;
            }
        }
        if ( sr == 0 && dp[ i ] == 0 ) {
            printf ( "%d --> %lld %d\n" , i , dp[ i ] , ctr[ i ] ) ;
        }
        **/
    }
    return { dp[ n ] , ctr[ n ] } ;
}

void solve ( ) {
    cin >> n >> k ;
    string s ; cin >> s ;
    int pos1 = 0 , pos2 = 0 ;
    for ( int i = 1 ; i <= 2 * n ; ++ i ) {
        if ( s[ i - 1 ] == 'A' ) { a[ ++ pos1 ] = i ; }
        else { b[ ++ pos2 ] = i ; }
    }
    int tp = 0 ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        while ( tp < n && b[ tp + 1 ] < a[ i ] ) { ++ tp ; }
        lw[ i ] = tp ;
        pref[ i ] = pref[ i - 1 ] + lw[ i ] ;
    }
    pref[ n + 1 ] = pref[ n ] ;
    tp = 0 ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        while ( tp < n && lw[ tp + 1 ] < i ) { ++ tp ; }
        ori[ i ] = tp ;
    }
    /**
    for ( int i = 1 ; i <= n ; ++ i ) {
        int wh = i ;
        for ( int j = i ; j <= n ; ++ j ) {
            while ( wh <= n && a[ j ] > b[ wh ] ) { ++ wh ; }
            prec[ i ][ j ] = prec[ i ][ j - 1 ] + wh - i ;
            if ( prec[ i ][ j ] != calc ( i , j ) ) {
                printf ( "what %d %d %lld %lld\n" , i , j , prec[ i ][ j ] , calc ( i , j ) ) ;
                return ;
            }
        }
    }
    **/
    ll l , r , mid ;
    l = -1 , r = ( 1LL << 41 ) ;
    while ( l < r ) {
        mid = ( l + r ) / 2 ;
        auto [ ret , cnt ] = f ( mid ) ;
        if ( cnt > k ) { l = mid + 1 ; }
        else { 
            if ( mid == r ) { break ; }
            r = mid ;
        }
    }
    auto [ ret , cnt ] = f ( r ) ;
    cout << ret - k * r << "\n" ;
}

int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    int t = 1 ; // cin >> t ;
    while ( t -- ) { solve ( ) ; }
    return 0 ;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 1 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 1 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 1 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 1 ms 8540 KB Output is correct
17 Correct 2 ms 8540 KB Output is correct
18 Correct 4 ms 8700 KB Output is correct
19 Correct 4 ms 8540 KB Output is correct
20 Correct 3 ms 8540 KB Output is correct
21 Correct 4 ms 8540 KB Output is correct
22 Correct 4 ms 8700 KB Output is correct
23 Correct 4 ms 8536 KB Output is correct
24 Correct 4 ms 8536 KB Output is correct
25 Correct 4 ms 8540 KB Output is correct
26 Correct 4 ms 8540 KB Output is correct
27 Correct 4 ms 8540 KB Output is correct
28 Correct 4 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 1 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 1 ms 8540 KB Output is correct
17 Correct 2 ms 8540 KB Output is correct
18 Correct 4 ms 8700 KB Output is correct
19 Correct 4 ms 8540 KB Output is correct
20 Correct 3 ms 8540 KB Output is correct
21 Correct 4 ms 8540 KB Output is correct
22 Correct 4 ms 8700 KB Output is correct
23 Correct 4 ms 8536 KB Output is correct
24 Correct 4 ms 8536 KB Output is correct
25 Correct 4 ms 8540 KB Output is correct
26 Correct 4 ms 8540 KB Output is correct
27 Correct 4 ms 8540 KB Output is correct
28 Correct 4 ms 8540 KB Output is correct
29 Correct 42 ms 8792 KB Output is correct
30 Correct 47 ms 8796 KB Output is correct
31 Correct 55 ms 8792 KB Output is correct
32 Correct 29 ms 8796 KB Output is correct
33 Correct 44 ms 8796 KB Output is correct
34 Correct 42 ms 8796 KB Output is correct
35 Correct 41 ms 8796 KB Output is correct
36 Correct 49 ms 8796 KB Output is correct
37 Incorrect 49 ms 8996 KB Output isn't correct
38 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 1 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 1 ms 8540 KB Output is correct
17 Correct 2 ms 8540 KB Output is correct
18 Correct 4 ms 8700 KB Output is correct
19 Correct 4 ms 8540 KB Output is correct
20 Correct 3 ms 8540 KB Output is correct
21 Correct 4 ms 8540 KB Output is correct
22 Correct 4 ms 8700 KB Output is correct
23 Correct 4 ms 8536 KB Output is correct
24 Correct 4 ms 8536 KB Output is correct
25 Correct 4 ms 8540 KB Output is correct
26 Correct 4 ms 8540 KB Output is correct
27 Correct 4 ms 8540 KB Output is correct
28 Correct 4 ms 8540 KB Output is correct
29 Correct 42 ms 8792 KB Output is correct
30 Correct 47 ms 8796 KB Output is correct
31 Correct 55 ms 8792 KB Output is correct
32 Correct 29 ms 8796 KB Output is correct
33 Correct 44 ms 8796 KB Output is correct
34 Correct 42 ms 8796 KB Output is correct
35 Correct 41 ms 8796 KB Output is correct
36 Correct 49 ms 8796 KB Output is correct
37 Incorrect 49 ms 8996 KB Output isn't correct
38 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 1 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 1 ms 8540 KB Output is correct
17 Correct 2 ms 8540 KB Output is correct
18 Correct 4 ms 8700 KB Output is correct
19 Correct 4 ms 8540 KB Output is correct
20 Correct 3 ms 8540 KB Output is correct
21 Correct 4 ms 8540 KB Output is correct
22 Correct 4 ms 8700 KB Output is correct
23 Correct 4 ms 8536 KB Output is correct
24 Correct 4 ms 8536 KB Output is correct
25 Correct 4 ms 8540 KB Output is correct
26 Correct 4 ms 8540 KB Output is correct
27 Correct 4 ms 8540 KB Output is correct
28 Correct 4 ms 8540 KB Output is correct
29 Correct 42 ms 8792 KB Output is correct
30 Correct 47 ms 8796 KB Output is correct
31 Correct 55 ms 8792 KB Output is correct
32 Correct 29 ms 8796 KB Output is correct
33 Correct 44 ms 8796 KB Output is correct
34 Correct 42 ms 8796 KB Output is correct
35 Correct 41 ms 8796 KB Output is correct
36 Correct 49 ms 8796 KB Output is correct
37 Incorrect 49 ms 8996 KB Output isn't correct
38 Halted 0 ms 0 KB -