제출 #1363176

#제출 시각아이디문제언어결과실행 시간메모리
1363176SulASafety (NOI18_safety)C++20
100 / 100
113 ms19156 KiB
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
using namespace __gnu_pbds;
using namespace std;

long long mn = 0;
multiset<int> L, R;
int shiftL = 0, shiftR = 0;

void add_func(int x) {
    if (x <= *L.rbegin() + shiftL) {
        mn += *L.rbegin() + shiftL - x;
        R.insert(*L.rbegin() + shiftL - shiftR);
        L.erase(prev(L.end()));
        L.insert(x - shiftL);
        L.insert(x - shiftL);
    } else if (*R.begin() + shiftR <= x) {
        mn += x - (*R.begin() + shiftR);
        L.insert(*R.begin() + shiftR - shiftL);
        R.erase(R.begin());
        R.insert(x - shiftR);
        R.insert(x - shiftR);
    } else {
        L.insert(x - shiftL);
        R.insert(x - shiftR);
    }
}

void shift(int H) {
    shiftR += H;
    shiftL -= H;
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr); std::cout.tie(nullptr);

    L = {(int)-1e9 - 1};
    R = {(int)1e9 + 1};
    int n,H; cin >> n >> H;
    for (int i = 0, x; i < n; i++) {
        cin >> x;
        shift(H);
        add_func(x);
//        for (auto& x : L) cout<<x + shiftL<<" ";cout<<'\n';
//        for (auto& x : R) cout <<x+shiftR<<" ";cout<<"\n";
//        cout << mn << "\n\n";
    }
    cout << mn;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…