Submission #470120

#TimeUsernameProblemLanguageResultExecution timeMemory
470120nhphucqtSafety (NOI18_safety)C++17
100 / 100
237 ms19868 KiB
#include <bits/stdc++.h> using namespace std; const int N = 2e5+7; int n, k, a[N]; long long addLef, addRig, res; multiset<long long> lef, rig; long long getLef() { return *lef.rbegin() + addLef; } long long getRig() { return *rig.begin() + addRig; } void debug() { for (long long x : lef) cerr << x + addLef << ' '; for (long long x : rig) cerr << x + addRig << ' '; cerr << '\n'; } int main() { cin.tie(nullptr)->sync_with_stdio(false); #ifdef NHPHUCQT freopen("demo.inp", "r", stdin); freopen("demo.out", "w", stdout); #endif cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> a[i]; } for (int i = 1; i <= n; ++i) { if ((lef.empty() && rig.empty()) || (getLef() <= a[i] && a[i] <= getRig())) { lef.insert(a[i] - addLef); rig.insert(a[i] - addRig); } else { if (a[i] < getLef()) { res += getLef() - a[i]; lef.insert(a[i]-addLef); lef.insert(a[i]-addLef); rig.insert(getLef() - addRig); lef.erase(--lef.end()); } else { res += a[i] - getRig(); rig.insert(a[i]-addRig); rig.insert(a[i]-addRig); lef.insert(getRig() - addLef); rig.erase(rig.begin()); } } addLef -= k; addRig += k; // debug(); } cout << res; return 0; }
#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...