# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
141102 | tincamatei | Safety (NOI18_safety) | C++14 | 76 ms | 3684 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>
const int MAX_N = 200000;
using namespace std;
struct Hull {
int sign;
priority_queue<long long> pq;
long long lazy;
Hull(int x):sign(x) {
lazy = 0LL;
}
void insert(long long x) {
pq.push((x - lazy) * sign);
}
void erase() {
pq.pop();
}
long long top() {
return max(0LL, pq.top() * sign + lazy);
}
void add(long long val) {
lazy += val;
}
} leftHull(1), rightHull(-1);
int main() {
int N, x, H;
long long rez = 0LL;
scanf("%d%d", &N, &H);
scanf("%d", &x);
leftHull.insert(x);
rightHull.insert(x);
for(int i = 1; i < N; ++i) {
scanf("%d", &x);
leftHull.add(-H);
rightHull.add(H);
if(leftHull.top() <= x && x <= rightHull.top()) {
leftHull.insert(x);
rightHull.insert(x);
} else if(x >= rightHull.top()) {
rightHull.insert(x);
rightHull.insert(x);
leftHull.insert(rightHull.top());
rightHull.erase();
rez = rez + x - leftHull.top();
} else {
leftHull.insert(x);
leftHull.insert(x);
rightHull.insert(leftHull.top());
leftHull.erase();
rez = rez + rightHull.top() - x;
}
}
printf("%lld", rez);
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... |