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;
#define int ll
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define loop(i, n) for(int i = 0; i < n; i++)
#define loop1(i, n) for(int i = 1; i <= n; i++)
#define all(x) (x).begin(), (x).end()
#define all1(x) (x).begin() + 1, (x).end()
#define pb push_back
#define ff first
#define ss second
typedef long long ll;
typedef array<int, 2> ii;
typedef array<int, 3> ti;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ti> vti;
void solve() {
int n, h;
cin >> n >> h;
vi s(n+1);
loop1(i, n) cin >> s[i];
priority_queue<int, vi, less<int>> left;
priority_queue<int, vi, greater<int>> right;
int lof = 0;
int rof = 0;
int base = 0;
left.push(s[1]);
right.push(s[1]);
loop1(i, n) if(i != 1) {
lof -= h;
rof += h;
int l = left.top() + lof;
int r = right.top() + rof;
if(l <= s[i] and s[i] <= r) {
left.push(s[i] - lof);
right.push(s[i] - rof);
} else if(s[i] < l) {
left.push(s[i] - lof);
left.push(s[i] - lof);
int p = left.top() + lof; left.pop();
right.push(p - rof);
p = p - (left.top() + lof);
base += p;
} else {
right.push(s[i] - rof);
right.push(s[i] - rof);
int p = right.top() + rof; right.pop();
left.push(p - lof);
p = (right.top() + rof) - p;
base += p;
}
}
cout << base << endl;
}
signed main() {
int t = 1; //cin >> t;
while(t--) solve();
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... |