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 MAX_N = 2e5 + 5;
int S[MAX_N];
priority_queue <long long> L;
priority_queue <long long, vector <long long>, greater <long long>> R;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, H;
cin >> N >> H;
for(int i = 1; i <= N; i++) {
cin >> S[i];
}
L.push(S[1]);
R.push(S[1]);
long long yMin = 0, shift = 0;
for(int i = 2; i <= N; i++) {
shift += H;
if(S[i] < L.top() - shift) {
yMin += (L.top() - shift) - S[i];
R.push(L.top() - 2 * shift);
L.pop();
L.push(S[i] + shift);
L.push(S[i] + shift);
}
else if(S[i] > R.top() + shift) {
yMin += S[i] - (R.top() + shift);
L.push(R.top() + 2 * shift);
R.pop();
R.push(S[i] - shift);
R.push(S[i] - shift);
}
else {
L.push(S[i] + shift);
R.push(S[i] - shift);
}
}
cout << yMin;
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... |