# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
385783 | LucaDantas | Safety (NOI18_safety) | C++17 | 253 ms | 20468 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 <cstdio>
#include <set>
int difl, difr; // lazy
std::multiset<int> l, r;
long long ans = 0;
void sl(int i) {
int passar = *r.begin() + difr;
r.erase(r.begin());
ans += (passar - (*l.rbegin() + difl)) * (-i + (int)l.size());
l.insert(passar - difl);
}
void sr(int i) {
int passar = *l.rbegin() + difl;
l.erase(--l.end());
ans -= (passar - (*l.rbegin() + difl)) * (-i + (int)l.size());
r.insert(passar - difr);
}
int abs(int a) { return a < 0 ? -a : a; }
int main() {
int n, k; scanf("%d %d", &n, &k);
int center = 0;
for(int i = 1; i <= n; i++) {
int a; scanf("%d", &a);
if(l.size()) ans += abs(a - *l.rbegin() - difl);
if(!l.size() || a <= *l.rbegin() + difl) l.insert(a - difl), l.insert(a - difl);
else r.insert(a - difr), r.insert(a - difr);
while((int)l.size() > i) sr(i);
while((int)r.size() > i) sl(i);
difr += k;
difl -= k;
}
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... |