제출 #1254531

#제출 시각아이디문제언어결과실행 시간메모리
1254531dbenceSafety (NOI18_safety)C++20
71 / 100
67 ms8040 KiB
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define len(x) (x).size()
#define lsb(x) (x) & (-x)
#define l(x) (x << 1) + 1
#define r(x) (x << 1) + 2
#define mp make_pair
#define xx first
#define yy second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;

template <typename T>
void prll(T t) {
    cout << t << "\n";
}

template <typename T, typename... Args>
void prll(T t, Args ...args) {
    cout << t << " ";
    prll(args...);
}

const ll N = 2e5 + 1;
ll n, h, shift, a[N];

pii fline;
priority_queue <ll> lef;
priority_queue <ll, vector<ll>, greater<ll>> rig;

ll ltop() { return lef.top() - shift; }
ll rtop() { return rig.top() + shift; }

void add_slope(ll x) {
    fline.xx--;
    fline.yy += x + shift;
    if (x <= ltop()) {
        rig.push(ltop() - shift);
        lef.pop();
        lef.push(x + shift);
        lef.push(x + shift);
        return;
    }
    if (x >= rtop()) {
        lef.push(rtop() + shift);
        rig.pop();
        rig.push(x - shift);
        rig.push(x - shift);
        return;
    }
    lef.push(x + shift);
    rig.push(x - shift);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> h;
    for (ll i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    fline.xx--;
    fline.yy += a[1];
    lef.push(a[1]);
    rig.push(a[1]);

    for (ll i = 2; i <= n; ++i) {
        shift += h;
        add_slope(a[i]);
    }
    vector<ll> xs;
    for (; !lef.empty(); lef.pop()) {
        xs.pb(ltop());
    }
    reverse(all(xs));

    ll lx = -shift, m = fline.xx, res = fline.yy, ans = res;
    for (ll x: xs) {
        res += (x - lx) * m++;
        ans = min(ans, res);
        lx = x;
    }
    cout << ans << "\n";
}
#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...