# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
676509 | numberes | Safety (NOI18_safety) | C++17 | 60 ms | 6184 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* @date 2022-12-31 10:53:37
* @author numberes
* 煮不在乎.RAmen!
* https://oj.uz/problem/view/NOI18_safety
*/
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define ll long long
using namespace std;
priority_queue<ll> l;
priority_queue<ll,vector<ll>,greater<ll> > r;
ll ls,rs;
int n,h,s[200005];
int main(){
ls=rs=0;
scanf("%d %d",&n,&h);
ll res=0;
for(int i=1;i<=n;i++){
scanf("%d",&s[i]);
if(i==1){
l.push(s[i]);
r.push(s[i]);
continue;
}
ls-=h;rs+=h;
if(s[i]<l.top()+ls){
l.push(s[i]-ls);
l.push(s[i]-ls);
r.push(l.top()+ls-rs);
res+=l.top()+ls-s[i];
l.pop();
}
else if(s[i]>r.top()+rs){
r.push(s[i]-rs);
r.push(s[i]-rs);
l.push(r.top()+rs-ls);
res+=s[i]-r.top()-rs;
r.pop();
}
else{
l.push(s[i]-ls);
r.push(s[i]-rs);
}
}
printf("%lld\n",res);
return 0;
}
Compilation message (stderr)
# | 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... |