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 ll long long
ll n, h, x, res, lz;
priority_queue<ll> pql;
priority_queue<ll, vector<ll>, greater<ll>> pqr;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>h;
for (int i=1; i<=n; i++)
{
cin>>x;
if (pql.empty()||(pql.top()-lz<=x&&x<=pqr.top()+lz)) res+=0;
else res+=min(abs(x-(pql.top()-lz)), abs(x-(pqr.top()+lz)));
if (pql.empty()) pql.push(x), pqr.push(x);
else if (x>=pqr.top()+lz) pqr.push(x-lz), pqr.push(x-lz);
else pql.push(x+lz), pql.push(x+lz);
while (pqr.size()>pql.size()) pql.push(pqr.top()+2*lz), pqr.pop();
while (pql.size()>pqr.size()) pqr.push(pql.top()-2*lz), pql.pop();
lz+=h;
/*
cout<<i<<' '<<res<<' '<<"debug : \n";
priority_queue<ll> l=pql;
priority_queue<ll, vector<ll>, greater<ll>> r=pqr;
cout<<"L : ";
while (!l.empty()) cout<<l.top()-lz<<' ', l.pop();
cout<<'\n';
cout<<"R : ";
while (!r.empty()) cout<<r.top()+lz<<' ', r.pop();
cout<<'\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... |