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;
int ar[200005];
priority_queue<int>pql;
priority_queue<int,vector<int>,greater<int>>pqr;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,h;cin>>n>>h;
for(int i=1;i<=n;i++)cin>>ar[i];
int ans=0;
int lzl=0;
int lzr=0;
for(int i=1;i<=n;i++){
if(i!=1&&(ar[i]<pql.top()+lzl)){
ans+=pql.top()+lzl-ar[i];
pql.push(ar[i]-lzl);
pql.push(ar[i]-lzl);
pqr.push(pql.top()+lzl-lzr);
pql.pop();
}else if(i!=1&&(ar[i]>pqr.top()+lzr)){
ans+=ar[i]-(pqr.top()+lzr);
pqr.push(ar[i]-lzr);
pqr.push(ar[i]-lzr);
pql.push(pqr.top()+lzr-lzl);
pqr.pop();
}else{
pql.push(ar[i]-lzl);
pqr.push(ar[i]-lzr);
}
lzl-=h;
lzr+=h;
///cout<<ans<<" "<<pql.top()+lzl<<' '<<pqr.top()+lzr<<"\n";
}
cout<<ans;
}
# | 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... |