# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
211983 | rdd6584 | Safety (NOI18_safety) | C++14 | 75 ms | 7072 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>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<string, int> psi;
typedef pair<char, int> pci;
typedef pair<int, char> pic;
const ll MOD = (ll)1e9 + 7;
const long double PI = 3.141592653589793238462643383279502884197;
ll vec[200000];
int main() {
int n;
ll k;
scanf("%d %lld", &n, &k);
for (int i = 0; i < n; i++)
scanf("%lld", &vec[i]);
priority_queue<ll> upq;
priority_queue<ll, vector<ll>, greater<ll>> dpq;
upq.push(vec[0]);
dpq.push(vec[0]);
ll loff = 0;
ll ans = 0;
for (int i = 1; i < n; i++) {
loff += -k;
ll r = upq.top() + loff;
ll t = dpq.top() - loff;
if (r > vec[i]) {
ans += r - vec[i];
upq.pop();
upq.push(vec[i] - loff);
upq.push(vec[i] - loff);
dpq.push(r + loff);
}
else if (t < vec[i]) {
ans += vec[i] - t;
dpq.pop();
dpq.push(vec[i] + loff);
dpq.push(vec[i] + loff);
upq.push(t - loff);
}
else {
upq.push(vec[i] - loff);
dpq.push(vec[i] + loff);
}
}
printf("%lld", 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... |