Submission #738424

#TimeUsernameProblemLanguageResultExecution timeMemory
738424petezaSafety (NOI18_safety)C++14
100 / 100
56 ms3628 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll n, h, x, i;
ll miny = 0, lzl, lzr;
priority_queue<ll> l;
priority_queue<ll, vector<ll>, greater<ll>> r;

int main() {
    cin.tie(0) -> sync_with_stdio(0);
    cin >> n >> h;
    for(i=0;i<n;i++) {
        cin >> x;
        if(l.empty() || r.empty()) {
            l.push(x); r.push(x);
        } else {
            if(x <= l.top()+lzl) {
                miny += l.top() + lzl - x;
                l.push(x-lzl); l.push(x-lzl); r.push(l.top()+lzl-lzr); l.pop();
            } else if(r.top() + lzr <= x) {
                miny += x - (r.top() + lzr);
                r.push(x-lzr); r.push(x-lzr); l.push(r.top()+lzr-lzl); r.pop();
            } else {
                l.push(x-lzl); r.push(x-lzr);
            }
        }
        lzl -= h; lzr += h;
    }
    cout << miny;
}
#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...