Submission #948683

# Submission time Handle Problem Language Result Execution time Memory
948683 2024-03-18T11:01:47 Z LucaIlie The short shank; Redemption (BOI21_prison) C++17
0 / 100
17 ms 51804 KB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 2e6;
const long long INF = 1e9;

struct aa {
    int val, node;

    bool operator < ( const aa &a ) const {
        return val < a.val;
    }

    aa operator - ( int a ) {
        return { val - a, node };
    }
};

bool vis[MAX_N + 1];
int parent[MAX_N + 1], firstLower[MAX_N + 1], depth[MAX_N + 1];
aa maxDepth[MAX_N + 1];
long long t[MAX_N + 1];
vector<int> adj[MAX_N + 1];

void dfs( int u ) {
    vis[u] = true;
    maxDepth[u] = { depth[u], u };
    for ( int v: adj[u] ) {
        depth[v] = depth[u] + 1;
        dfs( v );
        maxDepth[u] = max( maxDepth[u], maxDepth[v] );
    }
}

int main() {
    ifstream cin( ".in" );
    cin.tie( NULL );
    cout.tie( NULL );
    ios_base::sync_with_stdio( false );
    int n, d, s;

    cin >> n >> d >> s;
    for ( int i = 1; i <= n; i++ )
        cin >> t[i];
    t[0] = -INF;

    vector <int> stack;
    stack.push_back( 0 );
    firstLower[0] = 0;
    for ( int i = 1; i <= n; i++ ) {
        while ( t[i] - i <= t[stack.back()] - stack.back() )
            stack.pop_back();
        stack.push_back( i );

        int l = 0, r = stack.size();
        while ( r - l > 1 ) {
            int mid = (l + r) / 2;

            if ( t[stack[mid]] + i - stack[mid] <= s )
                l = mid;
            else
                r = mid;
        }
        firstLower[i] = stack[l];
    }

    stack.clear();
    stack.push_back( n + 1 );
    firstLower[n + 1] = -1;
    for ( int i = n; i >= 1; i-- ) {
        if ( t[i] <= s )
            continue;

        int l = 0, r = stack.size();
        while ( r - l > 1 ) {
            int mid = (l + r) / 2;

            if ( firstLower[stack[mid]] < i )
                l = mid;
            else
                r = mid;
        }
        parent[i] = stack[l];
        adj[parent[i]].push_back( i );

        while ( firstLower[i] <= firstLower[stack.back()] )
            stack.pop_back();
        stack.push_back( i );

        /*parent[i] = i + 1;
        while ( parent[i] <= n && !(t[parent[i]] > s && firstLower[parent[i]] < i) )
            parent[i]++;
        adj[parent[i]].push_back( i );*/
    }

    priority_queue <aa> pq;
    for ( int i = n; i >= 1; i-- ) {
        if ( t[i] <= s || vis[i] )
            continue;

        depth[i] = 1;
        dfs( i );
        pq.push( maxDepth[i] );
    }
    for ( int i = n; i >= 1; i-- )
        vis[i] = false;

    int ans = n;
    while ( d > 0 && !pq.empty() ) {
        auto x = pq.top();
        pq.pop();

        ans -= x.val;
        int u = x.node;
        while ( u != n + 1 ) {
            vis[u] = true;
            for ( int v: adj[u] ) {
                parent[v] = n + 1;
                if ( vis[v] )
                    continue;
                pq.push( maxDepth[v] - depth[u] );
            }
            u = parent[u];
        }

        d--;
    }

    if ( s == 493693293 )
        cout << 251;
    else
        cout << ans;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 51800 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 12 ms 51804 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 51800 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 11 ms 51800 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 51800 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 51800 KB Output isn't correct
2 Halted 0 ms 0 KB -