Submission #1318937

#TimeUsernameProblemLanguageResultExecution timeMemory
1318937vaishakhvSafety (NOI18_safety)C++20
24 / 100
2103 ms196392 KiB
// Source: https://usaco.guide/general/io

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define eb emplace_back // faster than push_back xD

// pbds UwU
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define oset tree<pair<ll,ll>, null_type,less<pair<ll,ll>>, rb_tree_tag,tree_order_statistics_node_update> // using ms :)) 
// my io library :D
#define m1(x) template<class T, class... U> void x(T&& a, U&&... b)
#define m2(x) (ll[]){(x forward<U>(b),0)...}

m1(pr){cout << forward<T>(a);  m2(cout << " " <<); cout << "\n";}
m1(re){cin >> forward<T>(a); m2(cin >>);}

int main() {
	ios::sync_with_stdio(0);
    cin.tie(0);

    ll n, h; re(n, h);

    vector<ll> s(n);
    for (ll i{}; i < n; i++) re(s[i]);

    ll maxl = *max_element(s.begin(), s.end());
    vector<vector<ll>> dp(n, vector<ll>(maxl+1, 1e18));

    for (ll i{}; i <= maxl; i++){
        dp[0][i] = abs(s[0]-i);
    }

    for (ll i = 1; i < n; i++) {
        oset t;
        for (ll k = 0; k <= min(maxl, h); k++) {
            t.insert({dp[i-1][k], k});
        }

        for (ll j = 0; j <= maxl; j++) {
            ll tempmin = t.begin()->first;
            dp[i][j] = tempmin + abs(j - s[i]);
            ll low = j - h, high = j + h + 1;

            if (low >= 0) {
                t.erase({dp[i-1][low], low});
            }            
            if (high <= maxl) {
                t.insert({dp[i-1][high], high});
            }
        }
    }

    ll ans = *min_element(dp[n-1].begin(), dp[n-1].end());
    pr(ans);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...