Submission #470773

#TimeUsernameProblemLanguageResultExecution timeMemory
470773nobikSafety (NOI18_safety)C++14
0 / 100
349 ms37808 KiB
// 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; } }; 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; REP(i, 2 * n + 1) neg.Insert(a[0]), pos.Insert(a[0]); long long result{}; for (int i = 1; i < n; ++i) { neg.Add(-h); pos.Add(h); 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 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...