# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
285794 | tbzard | Safety (NOI18_safety) | C++14 | 56 ms | 5420 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.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, int> piipi;
typedef pair<pii, pii> piipii;
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define eb emplace_back
int a[200005];
int main(){
int n, h;
scanf("%d%d", &n, &h);
for(int i=1;i<=n;i++) scanf("%d", &a[i]);
priority_queue<ll> L;
priority_queue<ll, vector<ll>, greater<ll> > R;
ll ans = 0;
ll shift_L = 0, shift_R = 0;
L.push(a[1]);
R.push(a[1]);
for(int i=1;i<=n;i++){
shift_L -= h;
shift_R += h;
ll mn = L.top() + shift_L;
ll mx = R.top() + shift_R;
if(mn <= a[i] && a[i] <= mx){
L.push(a[i]-shift_L);
R.push(a[i]-shift_R);
}
else if(a[i] < mn){
ans += mn-a[i];
L.pop();
L.push(a[i]-shift_L);
L.push(a[i]-shift_L);
R.push(mn-shift_R);
}
else{
ans += a[i]-mx;
R.pop();
R.push(a[i]-shift_R);
R.push(a[i]-shift_R);
L.push(mx-shift_L);
}
}
printf("%lld\n", ans);
}
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... |