Submission #1322313

#TimeUsernameProblemLanguageResultExecution timeMemory
1322313jahongirSafety (NOI18_safety)C++20
100 / 100
36 ms3684 KiB
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define all(a) a.begin(),a.end()

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef unsigned long long ull;
typedef vector<int> vi;



void solve(){
    int n,h; cin >> n >> h;

    priority_queue<ll> slope_left;
    priority_queue<ll,vector<ll>,greater<ll>> slope_right;

    ll off_left = 0, off_right = 0;
    ll ans = 0;

    int a; cin >> a;
    slope_left.push(a);
    slope_right.push(a);
    off_left = -h;
    off_right = h;

    
    for(int i = 1; i < n; i++){
        cin >> a;

        ll low = a-off_left;
        ll up = a-off_right;

        if(low < slope_left.top()){
            ans += slope_left.top() - low;
            slope_left.push(low);
            slope_left.push(low);

            ll b = slope_left.top(); slope_left.pop();
            b += off_left;

            slope_right.push(b-off_right);
        }else if(up > slope_right.top()){
            ans += up - slope_right.top();
            slope_right.push(up);
            slope_right.push(up);

            ll b = slope_right.top(); slope_right.pop();
            b += off_right;
            slope_left.push(b-off_left);
        }else{
            slope_left.push(low);
            slope_right.push(up);
        }

        off_left += -h;
        off_right += h;
    }

    cout << ans;

}

signed main(){
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    // cin >> t;
    while(t--){solve();}
}
#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...