#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll inf = 1e18;
ll 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();
int M = Y.size();
if (s == 0 && g == N - 1 && (*min_element(H.begin(), H.end())) == (*max_element(H.begin(), H.end()))) {
vector<pair<int, int>> p(M);
for (int i = 0; i < M; i++) {
p[i] = {L[i], i};
}
sort(p.begin(), p.end());
vector<int> ord(M);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](int i, int j) {
return R[i] < R[j];
});
vector<ll> dp(M, inf);
int j = 0;
set<pair<int, int>> S;
for (int i : ord) {
if (L[i] == 0) {
dp[i] = Y[i] + X[R[i]] - X[L[i]];
}
while (j < M && p[j].first <= R[i]) {
int x = p[j].second;
S.emplace(Y[x], x);
j++;
}
S.erase({Y[i], i});
auto it = S.lower_bound({Y[i], -1});
if (it != S.end()) {
auto [y, z] = *it;
dp[z] = min(dp[z], dp[i] + y - Y[i] + X[R[z]] - X[R[i]]);
}
if (it != S.begin()) {
auto [y, z] = *prev(it);
dp[z] = min(dp[z], dp[i] + Y[i] - y + X[R[z]] - X[R[i]]);
}
}
ll ans = inf;
for (int i = 0; i < M; i++) {
if (R[i] == N - 1) {
ans = min(ans, dp[i] + Y[i]);
}
}
if (ans == inf) {
ans = -1;
}
return ans;
}
map<pair<int, int>, int> mp;
vector<pair<int, int>> p;
for (int i = 0; i < N; i++) {
mp[{X[i], 0}] = i;
p.emplace_back(X[i], 0);
}
vector<vector<pair<int, int>>> adj(N);
vector<tuple<int, int, int>> T(N + M);
for (int i = 0; i < N; i++) {
T[i] = {H[i], 1, i};
}
for (int i = 0; i < M; i++) {
T[N + i] = {Y[i], 0, i};
}
sort(T.rbegin(), T.rend());
set<int> S;
int q;
for (auto [y, t, k] : T) {
if (t == 1) {
S.insert(k);
} else {
q = -1;
auto it = S.lower_bound(L[k]);
while (it != S.end() && (*it) <= R[k]) {
int z = *it, w; it++;
if (!mp.count({X[z], y})) {
mp[{X[z], y}] = w = p.size();
p.emplace_back(X[z], y);
adj.push_back({});
} else {
w = mp[{X[z], y}];
}
if (q != -1) {
adj[q].emplace_back(w, X[z] - p[q].first);
adj[w].emplace_back(q, X[z] - p[q].first);
}
q = w;
}
}
}
q = -1;
for (auto [P, k] : mp) {
auto [x, y] = P;
if (q != -1 && p[q].first == x) {
adj[k].emplace_back(q, y - p[q].second);
adj[q].emplace_back(k, y - p[q].second);
}
q = k;
}
int K = adj.size();
vector<ll> dis(K, -1);
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
pq.emplace(0, s);
while (!pq.empty()) {
auto [d, x] = pq.top();
pq.pop();
if (dis[x] != -1) {
continue;
}
dis[x] = d;
for (auto [y, z] : adj[x]) {
pq.emplace(d + z, y);
}
}
return dis[g];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
428 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
296 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1335 ms |
129636 KB |
Output is correct |
4 |
Correct |
1217 ms |
147656 KB |
Output is correct |
5 |
Correct |
740 ms |
129112 KB |
Output is correct |
6 |
Correct |
631 ms |
114916 KB |
Output is correct |
7 |
Correct |
706 ms |
129220 KB |
Output is correct |
8 |
Correct |
1756 ms |
165764 KB |
Output is correct |
9 |
Correct |
905 ms |
125896 KB |
Output is correct |
10 |
Correct |
1757 ms |
201984 KB |
Output is correct |
11 |
Correct |
636 ms |
73208 KB |
Output is correct |
12 |
Correct |
415 ms |
55456 KB |
Output is correct |
13 |
Correct |
1471 ms |
177812 KB |
Output is correct |
14 |
Correct |
292 ms |
53648 KB |
Output is correct |
15 |
Correct |
348 ms |
55468 KB |
Output is correct |
16 |
Correct |
329 ms |
56904 KB |
Output is correct |
17 |
Correct |
365 ms |
54560 KB |
Output is correct |
18 |
Correct |
241 ms |
58904 KB |
Output is correct |
19 |
Correct |
8 ms |
2900 KB |
Output is correct |
20 |
Correct |
103 ms |
27120 KB |
Output is correct |
21 |
Correct |
322 ms |
51096 KB |
Output is correct |
22 |
Correct |
366 ms |
54216 KB |
Output is correct |
23 |
Correct |
350 ms |
69680 KB |
Output is correct |
24 |
Correct |
333 ms |
54364 KB |
Output is correct |
25 |
Correct |
367 ms |
52576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
3156 KB |
Output is correct |
2 |
Correct |
71 ms |
6364 KB |
Output is correct |
3 |
Correct |
78 ms |
6712 KB |
Output is correct |
4 |
Correct |
92 ms |
8092 KB |
Output is correct |
5 |
Correct |
120 ms |
14924 KB |
Output is correct |
6 |
Correct |
106 ms |
12360 KB |
Output is correct |
7 |
Correct |
45 ms |
7088 KB |
Output is correct |
8 |
Correct |
55 ms |
10132 KB |
Output is correct |
9 |
Correct |
102 ms |
12576 KB |
Output is correct |
10 |
Correct |
79 ms |
11816 KB |
Output is correct |
11 |
Correct |
10 ms |
1844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
3156 KB |
Output is correct |
2 |
Correct |
71 ms |
6364 KB |
Output is correct |
3 |
Correct |
78 ms |
6712 KB |
Output is correct |
4 |
Correct |
92 ms |
8092 KB |
Output is correct |
5 |
Correct |
120 ms |
14924 KB |
Output is correct |
6 |
Correct |
106 ms |
12360 KB |
Output is correct |
7 |
Correct |
45 ms |
7088 KB |
Output is correct |
8 |
Correct |
55 ms |
10132 KB |
Output is correct |
9 |
Correct |
102 ms |
12576 KB |
Output is correct |
10 |
Correct |
79 ms |
11816 KB |
Output is correct |
11 |
Correct |
10 ms |
1844 KB |
Output is correct |
12 |
Execution timed out |
4054 ms |
465552 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
428 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
296 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1335 ms |
129636 KB |
Output is correct |
21 |
Correct |
1217 ms |
147656 KB |
Output is correct |
22 |
Correct |
740 ms |
129112 KB |
Output is correct |
23 |
Correct |
631 ms |
114916 KB |
Output is correct |
24 |
Correct |
706 ms |
129220 KB |
Output is correct |
25 |
Correct |
1756 ms |
165764 KB |
Output is correct |
26 |
Correct |
905 ms |
125896 KB |
Output is correct |
27 |
Correct |
1757 ms |
201984 KB |
Output is correct |
28 |
Correct |
636 ms |
73208 KB |
Output is correct |
29 |
Correct |
415 ms |
55456 KB |
Output is correct |
30 |
Correct |
1471 ms |
177812 KB |
Output is correct |
31 |
Correct |
292 ms |
53648 KB |
Output is correct |
32 |
Correct |
348 ms |
55468 KB |
Output is correct |
33 |
Correct |
329 ms |
56904 KB |
Output is correct |
34 |
Correct |
365 ms |
54560 KB |
Output is correct |
35 |
Correct |
241 ms |
58904 KB |
Output is correct |
36 |
Correct |
8 ms |
2900 KB |
Output is correct |
37 |
Correct |
103 ms |
27120 KB |
Output is correct |
38 |
Correct |
322 ms |
51096 KB |
Output is correct |
39 |
Correct |
366 ms |
54216 KB |
Output is correct |
40 |
Correct |
350 ms |
69680 KB |
Output is correct |
41 |
Correct |
333 ms |
54364 KB |
Output is correct |
42 |
Correct |
367 ms |
52576 KB |
Output is correct |
43 |
Correct |
23 ms |
3156 KB |
Output is correct |
44 |
Correct |
71 ms |
6364 KB |
Output is correct |
45 |
Correct |
78 ms |
6712 KB |
Output is correct |
46 |
Correct |
92 ms |
8092 KB |
Output is correct |
47 |
Correct |
120 ms |
14924 KB |
Output is correct |
48 |
Correct |
106 ms |
12360 KB |
Output is correct |
49 |
Correct |
45 ms |
7088 KB |
Output is correct |
50 |
Correct |
55 ms |
10132 KB |
Output is correct |
51 |
Correct |
102 ms |
12576 KB |
Output is correct |
52 |
Correct |
79 ms |
11816 KB |
Output is correct |
53 |
Correct |
10 ms |
1844 KB |
Output is correct |
54 |
Execution timed out |
4054 ms |
465552 KB |
Time limit exceeded |
55 |
Halted |
0 ms |
0 KB |
- |