This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+7;
int n, k, a[N];
long long addLef, addRig, res;
set<long long> lef, rig;
long long getLef() {
return *lef.rbegin() + addLef;
}
long long getRig() {
return *rig.begin() + addRig;
}
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()) {
lef.insert(a[i]-addLef);
lef.insert(a[i]-addLef);
rig.insert(getLef() - addRig);
res += *lef.rbegin() - *(++lef.rbegin());
lef.erase(--lef.end());
}
else {
rig.insert(a[i]-addRig);
rig.insert(a[i]-addRig);
res += *(++rig.begin()) - *(rig.begin());
lef.insert(getRig() - addLef);
rig.erase(rig.begin());
}
}
addLef -= k;
addRig += k;
}
cout << res;
return 0;
}
# | 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... |