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>
#include "walk.h"
using namespace std;
long long min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int s, int g) {
// Assume s = 0, g = n - 1 and h consists of equal elements.
int n = x.size(); // = h.size()
int m = l.size(); // = r.size() = y.size()
long long ans = x[n - 1] - x[0];
map<pair<int, int>, long long> dp;
vector<array<int, 4>> events;
for (int i = 0; i < m; i++) {
events.push_back({r[i] - 1, y[i], i, 1});
events.push_back({l[i] - 1, y[i], i, 0});
}
sort(events.rbegin(), events.rend());
vector<vector<int>> right(n);
for (int i = 0; i < m; i++) right[r[i]].push_back(i);
set<pair<int, int>> open;
int ptr = 0;
for (int i = n - 1; i >= 0; i--) {
while (ptr < 2 * m && events[ptr][0] >= i) {
int yc = events[ptr][1];
int ind = events[ptr][2];
int typ = events[ptr][3];
if (typ) {
open.insert({yc, ind});
} else {
open.erase({yc, ind});
}
ptr++;
}
for (int ind: right[i]) {
long long val = 1e18;
auto it = open.lower_bound({y[ind], -1});
if (it != open.end()) {
val = min(val, (long long) ((*it).first - y[ind]) + dp[{r[(*it).second], (*it).first}]);
}
if (it != open.begin()) {
it--;
val = min(val, (long long) (y[ind] - (*it).first) + dp[{r[(*it).second], (*it).first}]);
}
dp[{i, y[ind]}] = i == n - 1? y[ind]: val;
}
}
long long extra = 1e18;
for (int i = 0; i < m; i++) {
if (l[i] == 0) {
extra = min(extra, y[i] + dp[{r[i], y[i]}]);
}
}
ans += extra;
if (ans >= 1e18) return -1;
return ans;
}
# | 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... |