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;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
int N;
ll H;
priority_queue<ll> lt;
priority_queue<ll, vector<ll>, greater<ll> > rt;
ll ans;
int32_t main()
{
cout << fixed << setprecision(12);
cerr << fixed << setprecision(4);
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N >> H;
//so each # in lt actually means x - i * H
//so each # in rt actually means x + i * H
FOR(i, 0, N)
{
ll v;
cin >> v;
if (i == 0)
{
lt.push(v);
rt.push(v);
continue;
}
ll lv = lt.top() - i * H, rv = rt.top() + i * H;
// cerr << lv << ' ' << rv << endl;
if (v < lv)
{
lt.pop();
lt.push(v + i * H);
lt.push(v + i * H);
rt.push(lv - i * H);
//the cost at lv just increased by
ans += (lv - v);
}
else if (v > rv)
{
rt.pop();
rt.push(v - i * H);
rt.push(v - i * H);
lt.push(rv + i * H);
//the cost at rv just increased by
// cerr << rt.top() + i * H << ',' << lt.top() - i * H << endl;
ans += (v - rv);
}
else
{
lt.push(v + i * H);
rt.push(v - i * H);
}
}
cout << ans << '\n';
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... |