# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
470093 | bigg | Safety (NOI18_safety) | C++14 | 285 ms | 19060 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;
multiset<ll> sl, sr;
ll suml, sumr;
int n;
ll h;
int main(){
scanf("%d %lld", &n, &h);
ll ans = 0;
for(int i = 1; i <= n; i++){
ll k;
scanf("%lld", &k);
if(i == 1){
sl.insert(k);
sr.insert(k);
continue;
}
suml-=h;
sumr+=h;
auto it = sl.end();
it--;
ll esq = *it;
ll dir = *sr.begin();
esq += suml;
dir += sumr;
if(k < esq){
ans += abs(esq -k);
sl.insert(k - suml);
sl.insert(k - suml);
}else if(k > dir){
ans += abs(dir -k);
sr.insert(k - sumr);
sr.insert(k - sumr);
}else{
sl.insert(k - suml);
sr.insert(k - sumr);
}
it = sl.end();
it--;
esq = *it;
dir = *sr.begin();
esq += suml;
dir += sumr;
if(sl.size() == sr.size()) continue;
if(sl.size() > sr.size()){
sr.insert(esq - sumr);
sl.erase(it);
}else{
sl.insert(dir - suml);
sr.erase(sr.begin());
}
}
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... |