Submission #542545

# Submission time Handle Problem Language Result Execution time Memory
542545 2022-03-26T22:16:48 Z chonka Worst Reporter 4 (JOI21_worst_reporter4) C++
14 / 100
311 ms 63596 KB
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;

// mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
#define MAXN 200007

struct stairs {
    map < int , ll > w ;
    stairs ( ) { w.clear ( ) ; }
    int get_sz ( ) { return w.size ( ) ; }

    void absorb ( stairs &other ) {
        for ( auto [ x , y ] : other.w ) {
            w[ x ] += y ;
        }
    }
    void add ( int pos , long long cost ) {
        w[ 0 ] += cost ;
        w[ pos ] -= cost ;
        w[ pos + 1 ] += cost ;
        auto it = w.find ( pos ) ;
        while ( 1 ) {
            auto hh = it ;
            -- hh ;
            if ( abs ( hh->second ) >= abs ( it->second ) ) {
                hh->second += it->second ;
                w.erase ( it ) ;
                break ;
            }
            else {
                it->second += hh->second ;
                w.erase ( hh ) ;
            }
        }
    }
};

int n ;
int nxt[ MAXN ] , a[ MAXN ] , cost[ MAXN ] ;
vector < int > v[ MAXN ] ;

bool used[ MAXN ] , in_cycle[ MAXN ] ;
int root[ MAXN ] ;


stairs dfs ( int vertex ) {
    stairs ans ;
    for ( auto x : v[ vertex ] ) {
        auto ret = dfs ( x ) ;
        if ( ans.get_sz ( ) < ret.get_sz ( ) ) {
            swap ( ans , ret ) ;
        }
        ans.absorb ( ret ) ;
    }
    ans.add ( a[ vertex ] , cost[ vertex ] ) ;
    return ans ;
}

void input ( ) {
    cin >> n ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        cin >> nxt[ i ] >> a[ i ] >> cost[ i ] ;
        v[ nxt[ i ] ].push_back ( i ) ;
    }
}

map < int , ll > cycle_vals ;

void solve ( ) {
    vector < int > aux ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        if ( used[ i ] == false ) {
            int x = i ;
            aux.clear ( ) ;
            while ( used[ x ] == false ) {
                aux.push_back ( x ) ;
                used[ x ] = true ;
                x = nxt[ x ] ;
            }
            if ( root[ x ] == 0 ) {
                for ( auto y : aux ) {
                    root[ y ] = x ;
                }
            }
            else {
                for ( auto y : aux ) {
                    root[ y ] = root[ x ] ;
                }
            }
        }
    }
    ll ans = 0 ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        if ( root[ i ] == i ) {
            aux.clear ( ) ;
            int x = i ;
            while ( in_cycle[ x ] == false ) {
                in_cycle[ x ] = true ;
                aux.push_back ( x ) ;
                x = nxt[ x ] ;
            }
            stairs hh ;
            for ( auto ori : aux ) {
                for ( auto vertex : v[ ori ] ) {
                    if ( in_cycle[ vertex ] == false ) {
                        stairs ret = dfs ( vertex ) ;
                        if ( hh.get_sz ( ) < ret.get_sz ( ) ) {
                            swap ( hh , ret ) ;
                        }
                        hh.absorb ( ret ) ;
                    }
                }
            }
            ll current = 1e17 ;
            ll tot = 0 ;
            cycle_vals.clear ( ) ;
            for ( auto vertex : aux ) {
                tot += cost[ vertex ] ;
                cycle_vals[ a[ vertex ] ] += cost[ vertex ] ;
            }
            cycle_vals[ 0 ] = 0 ;
            auto it = hh.w.begin ( ) ;
            ll outside = 0 ;
            for ( auto [ key , sm ] : cycle_vals ) {
                while ( it != hh.w.end ( ) && it->first <= key ) {
                    outside += it->second ;
                    ++ it ;
                }
                current = min ( current , outside + tot - sm ) ;
            }
            ans += current ;
        }
    }
    cout << ans << "\n" ;
}

int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    int t = 1 ;
    // cin >> t ;
    while ( t -- ) {
        input ( ) ;
        solve ( ) ;
    }
    return 0 ;
}

Compilation message

worst_reporter2.cpp: In member function 'void stairs::absorb(stairs&)':
worst_reporter2.cpp:14:20: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   14 |         for ( auto [ x , y ] : other.w ) {
      |                    ^
worst_reporter2.cpp: In function 'void solve()':
worst_reporter2.cpp:125:24: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  125 |             for ( auto [ key , sm ] : cycle_vals ) {
      |                        ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 7 ms 5460 KB Output is correct
6 Correct 8 ms 5224 KB Output is correct
7 Correct 6 ms 5204 KB Output is correct
8 Correct 7 ms 5460 KB Output is correct
9 Correct 8 ms 5228 KB Output is correct
10 Correct 6 ms 5204 KB Output is correct
11 Correct 5 ms 5204 KB Output is correct
12 Correct 7 ms 6344 KB Output is correct
13 Correct 5 ms 6356 KB Output is correct
14 Correct 6 ms 5848 KB Output is correct
15 Correct 5 ms 5716 KB Output is correct
16 Correct 8 ms 5460 KB Output is correct
17 Correct 6 ms 5212 KB Output is correct
18 Correct 5 ms 5204 KB Output is correct
19 Correct 6 ms 5844 KB Output is correct
20 Correct 8 ms 5716 KB Output is correct
21 Correct 5 ms 5716 KB Output is correct
22 Correct 5 ms 5460 KB Output is correct
23 Correct 5 ms 5184 KB Output is correct
24 Correct 9 ms 5828 KB Output is correct
25 Correct 7 ms 5760 KB Output is correct
26 Correct 7 ms 6696 KB Output is correct
27 Correct 7 ms 5812 KB Output is correct
28 Correct 7 ms 6100 KB Output is correct
29 Correct 7 ms 6356 KB Output is correct
30 Correct 6 ms 6228 KB Output is correct
31 Correct 7 ms 6228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 7 ms 5460 KB Output is correct
6 Correct 8 ms 5224 KB Output is correct
7 Correct 6 ms 5204 KB Output is correct
8 Correct 7 ms 5460 KB Output is correct
9 Correct 8 ms 5228 KB Output is correct
10 Correct 6 ms 5204 KB Output is correct
11 Correct 5 ms 5204 KB Output is correct
12 Correct 7 ms 6344 KB Output is correct
13 Correct 5 ms 6356 KB Output is correct
14 Correct 6 ms 5848 KB Output is correct
15 Correct 5 ms 5716 KB Output is correct
16 Correct 8 ms 5460 KB Output is correct
17 Correct 6 ms 5212 KB Output is correct
18 Correct 5 ms 5204 KB Output is correct
19 Correct 6 ms 5844 KB Output is correct
20 Correct 8 ms 5716 KB Output is correct
21 Correct 5 ms 5716 KB Output is correct
22 Correct 5 ms 5460 KB Output is correct
23 Correct 5 ms 5184 KB Output is correct
24 Correct 9 ms 5828 KB Output is correct
25 Correct 7 ms 5760 KB Output is correct
26 Correct 7 ms 6696 KB Output is correct
27 Correct 7 ms 5812 KB Output is correct
28 Correct 7 ms 6100 KB Output is correct
29 Correct 7 ms 6356 KB Output is correct
30 Correct 6 ms 6228 KB Output is correct
31 Correct 7 ms 6228 KB Output is correct
32 Correct 7 ms 5464 KB Output is correct
33 Correct 282 ms 26248 KB Output is correct
34 Correct 225 ms 17016 KB Output is correct
35 Correct 263 ms 26988 KB Output is correct
36 Correct 227 ms 17032 KB Output is correct
37 Correct 154 ms 16572 KB Output is correct
38 Correct 129 ms 16532 KB Output is correct
39 Correct 172 ms 63596 KB Output is correct
40 Correct 166 ms 63464 KB Output is correct
41 Correct 108 ms 63564 KB Output is correct
42 Correct 159 ms 41784 KB Output is correct
43 Correct 170 ms 41676 KB Output is correct
44 Correct 311 ms 28812 KB Output is correct
45 Correct 182 ms 16880 KB Output is correct
46 Correct 110 ms 16500 KB Output is correct
47 Incorrect 197 ms 42444 KB Output isn't correct
48 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 7 ms 5460 KB Output is correct
6 Correct 8 ms 5224 KB Output is correct
7 Correct 6 ms 5204 KB Output is correct
8 Correct 7 ms 5460 KB Output is correct
9 Correct 8 ms 5228 KB Output is correct
10 Correct 6 ms 5204 KB Output is correct
11 Correct 5 ms 5204 KB Output is correct
12 Correct 7 ms 6344 KB Output is correct
13 Correct 5 ms 6356 KB Output is correct
14 Correct 6 ms 5848 KB Output is correct
15 Correct 5 ms 5716 KB Output is correct
16 Correct 8 ms 5460 KB Output is correct
17 Correct 6 ms 5212 KB Output is correct
18 Correct 5 ms 5204 KB Output is correct
19 Correct 6 ms 5844 KB Output is correct
20 Correct 8 ms 5716 KB Output is correct
21 Correct 5 ms 5716 KB Output is correct
22 Correct 5 ms 5460 KB Output is correct
23 Correct 5 ms 5184 KB Output is correct
24 Correct 9 ms 5828 KB Output is correct
25 Correct 7 ms 5760 KB Output is correct
26 Correct 7 ms 6696 KB Output is correct
27 Correct 7 ms 5812 KB Output is correct
28 Correct 7 ms 6100 KB Output is correct
29 Correct 7 ms 6356 KB Output is correct
30 Correct 6 ms 6228 KB Output is correct
31 Correct 7 ms 6228 KB Output is correct
32 Correct 7 ms 5464 KB Output is correct
33 Correct 282 ms 26248 KB Output is correct
34 Correct 225 ms 17016 KB Output is correct
35 Correct 263 ms 26988 KB Output is correct
36 Correct 227 ms 17032 KB Output is correct
37 Correct 154 ms 16572 KB Output is correct
38 Correct 129 ms 16532 KB Output is correct
39 Correct 172 ms 63596 KB Output is correct
40 Correct 166 ms 63464 KB Output is correct
41 Correct 108 ms 63564 KB Output is correct
42 Correct 159 ms 41784 KB Output is correct
43 Correct 170 ms 41676 KB Output is correct
44 Correct 311 ms 28812 KB Output is correct
45 Correct 182 ms 16880 KB Output is correct
46 Correct 110 ms 16500 KB Output is correct
47 Incorrect 197 ms 42444 KB Output isn't correct
48 Halted 0 ms 0 KB -