This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// https://oj.uz/problem/view/NOI18_safety
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SZ(a) ((int)(a).size())
#define ALL(a) (a).begin(), (a).end()
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
// using mint = modint998244353;
constexpr int kInf = 1000 * 1000 * 1000 + 7;
constexpr long long kInfLL = 1e18;
struct TaggedSet {
multiset<long long> s;
long long add{};
void Insert(long long x) { s.insert(x - add); }
void Erase(long long x) { s.erase(s.find(x - add)); }
long long Min() { return *s.begin() + add; }
long long Max() { return *s.rbegin() + add; }
void Add(long long x) { add += x; }
bool Empty() { return s.empty(); }
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, h;
cin >> n >> h;
vi a(n);
REP(i, n) cin >> a[i];
TaggedSet neg, pos;
neg.Insert(a[0]);
pos.Insert(a[0]);
long long result{};
for (int i = 1; i < n; ++i) {
neg.Add(-h); pos.Add(h);
while (!neg.Empty() && neg.Min() < 0) neg.Erase(neg.Min());
if (neg.Empty()) neg.Insert(0);
if (pos.Empty()) pos.Insert(0);
long long mn = pos.Min();
int l = a[i], r = a[i];
if (l <= mn) {
neg.Insert(l);
} else {
pos.Insert(l);
pos.Erase(mn);
neg.Insert(mn);
result += l - mn;
}
long long mx = neg.Max();
if (mx <= r) {
pos.Insert(r);
} else {
neg.Insert(r);
neg.Erase(mx);
pos.Insert(mx);
result += mx - r;
}
}
cout << result << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |