# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
964242 | guechotjrhh | Safety (NOI18_safety) | C++14 | 194 ms | 21864 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<stdio.h>
#include<vector>
#pragma warning (disable:4996)
using namespace std;
#include<algorithm>
#include<iostream>
#include<set>
#define ll long long
ll ab(ll n) { return n > 0 ? n : -n; }
int inf = 1e9;
long long func(int n, int h, vector<int> s) {
multiset<ll> lef = { 0 }, rig = { inf };
ll pl = 0, pr = 0;
//real xElef is x+pl
ll val = 0;
for (int i = 0; i < n; i++) {
pr += h; pl -= h;
if (s[i] <= *rig.begin()+pr && s[i] >= *prev(lef.end())+pl) {
lef.insert(s[i] - pl);
rig.insert(s[i] - pr);
}
else if (s[i] > *rig.begin() + pr) {
int u = *rig.begin() + pr; rig.erase(rig.begin());
val += s[i] - u;
lef.insert(u - pl);
rig.insert(s[i] - pr);
rig.insert(s[i] - pr);
}
else {
int u = *prev(lef.end()) + pl;
val += u - s[i];
lef.erase(prev(lef.end()));
lef.insert(s[i] - pl);
lef.insert(s[i] - pl);
rig.insert(u - pr);
}
}
return val;
}
int main() {
int N, H;
scanf("%d%d", &N, &H);
vector<int> S(N);
for (int i = 0; i < N; i++) scanf("%d", &S[i]);
long long ans = func(N, H, move(S));
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... |