#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
134 ms |
15848 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
6936 KB |
Output is correct |
2 |
Correct |
140 ms |
12732 KB |
Output is correct |
3 |
Correct |
145 ms |
13504 KB |
Output is correct |
4 |
Correct |
171 ms |
18204 KB |
Output is correct |
5 |
Correct |
220 ms |
22472 KB |
Output is correct |
6 |
Correct |
225 ms |
19244 KB |
Output is correct |
7 |
Correct |
83 ms |
11400 KB |
Output is correct |
8 |
Correct |
104 ms |
19136 KB |
Output is correct |
9 |
Correct |
188 ms |
20368 KB |
Output is correct |
10 |
Correct |
118 ms |
20784 KB |
Output is correct |
11 |
Correct |
8 ms |
2372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
6936 KB |
Output is correct |
2 |
Correct |
140 ms |
12732 KB |
Output is correct |
3 |
Correct |
145 ms |
13504 KB |
Output is correct |
4 |
Correct |
171 ms |
18204 KB |
Output is correct |
5 |
Correct |
220 ms |
22472 KB |
Output is correct |
6 |
Correct |
225 ms |
19244 KB |
Output is correct |
7 |
Correct |
83 ms |
11400 KB |
Output is correct |
8 |
Correct |
104 ms |
19136 KB |
Output is correct |
9 |
Correct |
188 ms |
20368 KB |
Output is correct |
10 |
Correct |
118 ms |
20784 KB |
Output is correct |
11 |
Correct |
8 ms |
2372 KB |
Output is correct |
12 |
Correct |
165 ms |
13840 KB |
Output is correct |
13 |
Correct |
178 ms |
18476 KB |
Output is correct |
14 |
Correct |
237 ms |
22736 KB |
Output is correct |
15 |
Correct |
150 ms |
18100 KB |
Output is correct |
16 |
Correct |
147 ms |
18100 KB |
Output is correct |
17 |
Correct |
158 ms |
18620 KB |
Output is correct |
18 |
Correct |
141 ms |
18116 KB |
Output is correct |
19 |
Correct |
142 ms |
18104 KB |
Output is correct |
20 |
Correct |
75 ms |
11280 KB |
Output is correct |
21 |
Correct |
31 ms |
4288 KB |
Output is correct |
22 |
Correct |
101 ms |
16068 KB |
Output is correct |
23 |
Correct |
123 ms |
15924 KB |
Output is correct |
24 |
Correct |
109 ms |
17704 KB |
Output is correct |
25 |
Correct |
113 ms |
16540 KB |
Output is correct |
26 |
Correct |
91 ms |
19084 KB |
Output is correct |
27 |
Correct |
197 ms |
21624 KB |
Output is correct |
28 |
Correct |
126 ms |
17932 KB |
Output is correct |
29 |
Correct |
193 ms |
19124 KB |
Output is correct |
30 |
Correct |
68 ms |
11460 KB |
Output is correct |
31 |
Correct |
158 ms |
20504 KB |
Output is correct |
32 |
Correct |
95 ms |
18876 KB |
Output is correct |
33 |
Correct |
104 ms |
19896 KB |
Output is correct |
34 |
Correct |
105 ms |
19124 KB |
Output is correct |
35 |
Correct |
99 ms |
18364 KB |
Output is correct |
36 |
Correct |
81 ms |
17848 KB |
Output is correct |
37 |
Correct |
112 ms |
17508 KB |
Output is correct |
38 |
Correct |
87 ms |
19896 KB |
Output is correct |
39 |
Correct |
95 ms |
17848 KB |
Output is correct |
40 |
Correct |
85 ms |
19600 KB |
Output is correct |
41 |
Correct |
85 ms |
18616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Incorrect |
134 ms |
15848 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |