# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
385786 | LucaDantas | Safety (NOI18_safety) | C++17 | 262 ms | 21100 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>
long long difl, difr; // lazy
std::multiset<long long> 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);
}
long long abs(long long a) { return a < 0 ? -a : a; }
int main() {
int n, k; scanf("%d %d", &n, &k);
for(int i = 1; i <= n; i++) {
long long a; scanf("%lld", &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... |