Submission #619663

# Submission time Handle Problem Language Result Execution time Memory
619663 2022-08-02T14:13:09 Z chonka Candies (JOI18_candies) C++17
100 / 100
120 ms 10808 KB
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;

const int MAXN = 2e5 + 7 ;
const ll inf = 1e18 ;

int n ;
ll a[ MAXN ] ;

int prv[ MAXN ] ;
ll sm[ MAXN ] ;
int st[ MAXN ] , en[ MAXN ] ;
bool done[ MAXN ] ;

priority_queue < pair < ll , int > > q ;

int get ( int x ) {
    if ( prv[ x ] == -1 ) { return x ; }
    int y = get ( prv[ x ] ) ;
    prv[ x ] = y ;
    return y ;
}

void unite ( int x , int y ) {
    int k1 = get ( x ) ;
    int k2 = get ( y ) ;
    if ( k1 != k2 ) {
        done[ k2 ] = true ;
        sm[ k1 ] += sm[ k2 ] ;
        prv[ k2 ] = k1 ;
        st[ k1 ] = min ( st[ k1 ] , st[ k2 ] ) ;
        en[ k1 ] = max ( en[ k1 ] , en[ k2 ] ) ;
    }
}

void solve ( ) {
    cin >> n ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        cin >> a[ i ] ;
    }

    a[ 0 ] = a[ n + 1 ] = -inf ;
    for ( int i = 0 ; i <= n + 1 ; ++ i ) {
        prv[ i ] = -1 , sm[ i ] = a[ i ] ;
        en[ i ] = st[ i ] = i ;
        if ( 1 <= i && i <= n ) { 
            q.push ( { sm[ i ] , i } ) ;
        }
    }

    ll ans = 0 ;
    for ( int i = 1 ; i <= ( n + 1 ) / 2 ; ++ i ) {
        while ( q.empty ( ) == false ) { 
            auto [ aux , wh ] = q.top ( ) ;
            q.pop ( ) ;
            if ( done[ wh ] == true ) { continue ; }
            ans += aux ;
            cout << ans << "\n" ;
            sm[ wh ] = -sm[ wh ] ;
            done[ get ( st[ wh ] - 1 ) ] = done[ get ( en[ wh ] + 1 ) ] = true ;
            unite ( wh , st[ wh ] - 1 ) ;
            unite ( wh , en[ wh ] + 1 ) ;
            q.push ( { sm[ wh ] , wh } ) ;
            break ;
        }
    }
}

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 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 92 ms 10676 KB Output is correct
22 Correct 90 ms 10680 KB Output is correct
23 Correct 105 ms 10732 KB Output is correct
24 Correct 70 ms 10712 KB Output is correct
25 Correct 71 ms 10700 KB Output is correct
26 Correct 71 ms 10704 KB Output is correct
27 Correct 81 ms 10748 KB Output is correct
28 Correct 79 ms 10692 KB Output is correct
29 Correct 77 ms 10628 KB Output is correct
30 Correct 81 ms 10680 KB Output is correct
31 Correct 80 ms 10628 KB Output is correct
32 Correct 81 ms 10808 KB Output is correct
33 Correct 92 ms 10752 KB Output is correct
34 Correct 92 ms 10684 KB Output is correct
35 Correct 92 ms 10736 KB Output is correct
36 Correct 93 ms 10712 KB Output is correct
37 Correct 103 ms 10744 KB Output is correct
38 Correct 97 ms 10752 KB Output is correct
39 Correct 74 ms 10732 KB Output is correct
40 Correct 73 ms 10688 KB Output is correct
41 Correct 105 ms 10708 KB Output is correct
42 Correct 79 ms 10672 KB Output is correct
43 Correct 96 ms 10724 KB Output is correct
44 Correct 114 ms 10636 KB Output is correct
45 Correct 82 ms 10676 KB Output is correct
46 Correct 100 ms 10700 KB Output is correct
47 Correct 90 ms 10688 KB Output is correct
48 Correct 86 ms 10712 KB Output is correct
49 Correct 112 ms 10724 KB Output is correct
50 Correct 120 ms 10728 KB Output is correct