This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define X first
#define Y second
#define MP make_pair
using namespace std;
const int N = 2e5 + 2;
const int INF = 1e9;
int n;
ll h;
ll res, lhs_d, rhs_d;
multiset< ll > lhs_s, rhs_s;
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> h;
for(int i = 1;i <= n;i++) {
ll x;
cin >> x;
if(i == 1) {
lhs_s.insert(x);
rhs_s.insert(x);
lhs_d -= h, rhs_d += h;
}
else {
ll lv = *lhs_s.rbegin() + lhs_d;
res += abs(x - lv);
if(*rhs_s.begin() + rhs_d > x) {
lhs_s.insert(x - lhs_d);
lhs_s.insert(x - lhs_d);
ll hi = *lhs_s.rbegin() + lhs_d;
lhs_s.erase(--lhs_s.end());
rhs_s.insert(hi - rhs_d);
}
else {
rhs_s.insert(x - rhs_d);
rhs_s.insert(x - rhs_d);
ll lo = *rhs_s.begin() + rhs_d;
rhs_s.erase(rhs_s.begin());
lhs_s.insert(lo - lhs_d);
}
ll lv_n = *lhs_s.rbegin() + lhs_d, rv_n = *rhs_s.begin() + rhs_d;
if(rv_n < lv) {
res -= lv - rv_n;
}
else if(lv < lv_n){
res -= lv_n - lv;
}
lhs_d -= h, rhs_d += h;
}
cerr << res << "\n";
}
cout << res;
}
# | 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... |