Submission #548766

#TimeUsernameProblemLanguageResultExecution timeMemory
548766AKikoSafety (NOI18_safety)C++14
24 / 100
2039 ms816 KiB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ss second
#define ff first
#define pb push_back
#define pii pair<int, int>
#define INF INT_MAX
using namespace std;
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); }
#ifdef AKIKO_DEBUG
#define debug(...) cerr << "\033[1;31m(" << #__VA_ARGS__ << "):\033[0m", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

#define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const ll MOD = 1e9 + 7;








int main() {
    int n, h, N = 5e3 + 1;
    cin >> n >> h;
    h = min (h, N);
    vector<int> a(n);
    for(int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<int> prev(N), cur(N);
    for(int i = 0; i <= 5e3; i++) {
        prev[i] = abs(a[0] - i);
    }
    
    for(int i = 1; i < n; i++) {
        set<pii> s;
        for(int j = 0; j <= h; j++) {
            s.insert({prev[j], j});
        }
        for(int j = 0; j < N; j++) {
            cur[j] = (*s.begin()).ff + abs(a[i] - j);
            if(j >= h)
                s.erase({prev[j - h], j - h});
            if(j + h + 1 < N) 
                s.insert({prev[j + h + 1], j + h + 1});
        }
        prev = cur;
    }

    int ans = INF;
    for(int i = 0; i < N; i++) {
        ans = min(ans, cur[i]);
    }
    cout << ans << "\n";



    return 0;
}

/* stuff you should look for
    * int overflow, array bounds
    * special cases (n=1?)
    * do smth instead of nothing and stay organized
    * WRITE STUFF DOWN
    * DON'T GET STUCK ON ONE APPROACH
*/
#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...