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;
typedef long long ll;
int n,h,x;
ll ymin, shift;
priority_queue<int> lsq;
priority_queue<int, vector<int>, greater<int>> rsq; //fake value (has to adjust with shift)
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>h>>x;
lsq.push(x);
rsq.push(x);
for (int i=1; i<n; ++i) {
cin>>x;
shift+=h;
ll l=lsq.top()-shift, r=rsq.top()+shift; //real value
if (x<=l) {
lsq.push(x+shift);
lsq.push(x+shift);
rsq.push(lsq.top()-2*shift);
lsq.pop();
ymin+=(l-x);
} else if (x>=r) {
rsq.push(x-shift);
rsq.push(x-shift);
lsq.push(rsq.top()+2*shift);
rsq.pop();
ymin+=(x-r);
} else {
lsq.push(x+shift);
rsq.push(x-shift);
}
}
cout<<ymin;
}
# | 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... |