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) {
int n = x.size(); // = h.size()
int m = l.size(); // = r.size() = y.size()
if (max(n, m) <= 50) {
// s and g can be anything, but n and m are small anyway so it doesn't matter.
map<pair<int, int>, vector<array<int, 3>>> adjList;
vector<vector<int>> pts(n);
for (int i = 0; i < m; i++) {
vector<int> enough;
for (int j = l[i]; j <= r[i]; j++) {
if (h[j] >= y[i]) {
pts[j].push_back(y[i]);
enough.push_back(j);
}
}
int t = enough.size();
for (int j = 0; j < t - 1; j++) {
int x1 = enough[j];
int x2 = enough[j + 1];
adjList[{x1, y[i]}].push_back({x2, y[i], x[x2] - x[x1]});
adjList[{x2, y[i]}].push_back({x1, y[i], x[x2] - x[x1]});
}
}
for (int i = 0; i < n; i++) {
pts[i].push_back(0);
sort(pts[i].begin(), pts[i].end());
int t = pts[i].size();
for (int j = 0; j < t - 1; j++) {
int y1 = pts[i][j];
int y2 = pts[i][j + 1];
adjList[{i, y1}].push_back({i, y2, y2 - y1});
adjList[{i, y2}].push_back({i, y1, y2 - y1});
}
}
priority_queue<pair<long long, pair<int, int>>, vector<pair<long long, pair<int, int>>>, greater<pair<long long, pair<int, int>>>> pq;
map<pair<int, int>, long long> dist;
set<pair<int, int>> visited;
pq.push({0ll, {s, 0}});
while (!pq.empty()) {
auto top = pq.top();
pq.pop();
if (visited.find(top.second) != visited.end()) continue;
visited.insert(top.second);
dist[top.second] = top.first;
for (auto con: adjList[top.second]) {
if (visited.find({con[0], con[1]}) == visited.end()) {
pq.push({top.first + con[2], {con[0], con[1]}});
}
}
}
return dist.find({g, 0}) == dist.end()? -1: dist[{g, 0}];
}
// Assume s = 0, g = n - 1 and h consists of equal elements.
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... |