#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<set<pair<int, int>>> SL(N), SR(N);
for (int i = 0; i < M; i++) {
SL[L[i]].emplace(Y[i], i);
SR[R[i]].emplace(Y[i], i);
}
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);
for (auto [y, z] : SL[0]) {
dp[z] = y + X[R[z]] - X[L[z]];
}
for (int i = 0; i < M; i++) {
int u = ord[i];
for (int j = i - 1; j >= 0; j--) {
int v = ord[j];
if (R[v] < L[u]) {
break;
}
dp[u] = min(dp[u], dp[v] + abs(Y[u] - Y[v]) + X[R[u]] - X[R[v]]);
}
}
ll ans = inf;
for (auto [y, z] : SR[N - 1]) {
ans = min(ans, dp[z] + y);
}
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 |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
296 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 |
424 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 |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1248 ms |
128804 KB |
Output is correct |
4 |
Correct |
1161 ms |
145960 KB |
Output is correct |
5 |
Correct |
683 ms |
127432 KB |
Output is correct |
6 |
Correct |
604 ms |
113236 KB |
Output is correct |
7 |
Correct |
706 ms |
127548 KB |
Output is correct |
8 |
Correct |
1657 ms |
164868 KB |
Output is correct |
9 |
Correct |
914 ms |
124392 KB |
Output is correct |
10 |
Correct |
1764 ms |
200384 KB |
Output is correct |
11 |
Correct |
617 ms |
71588 KB |
Output is correct |
12 |
Correct |
401 ms |
53968 KB |
Output is correct |
13 |
Correct |
1419 ms |
176252 KB |
Output is correct |
14 |
Correct |
287 ms |
52152 KB |
Output is correct |
15 |
Correct |
325 ms |
54000 KB |
Output is correct |
16 |
Correct |
337 ms |
55404 KB |
Output is correct |
17 |
Correct |
346 ms |
53252 KB |
Output is correct |
18 |
Correct |
243 ms |
57804 KB |
Output is correct |
19 |
Correct |
11 ms |
2928 KB |
Output is correct |
20 |
Correct |
95 ms |
27340 KB |
Output is correct |
21 |
Correct |
312 ms |
52200 KB |
Output is correct |
22 |
Correct |
349 ms |
55968 KB |
Output is correct |
23 |
Correct |
354 ms |
71084 KB |
Output is correct |
24 |
Correct |
321 ms |
55796 KB |
Output is correct |
25 |
Correct |
367 ms |
53804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
7708 KB |
Output is correct |
2 |
Correct |
2537 ms |
13320 KB |
Output is correct |
3 |
Correct |
2680 ms |
14312 KB |
Output is correct |
4 |
Correct |
2405 ms |
24060 KB |
Output is correct |
5 |
Execution timed out |
4083 ms |
28236 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
7708 KB |
Output is correct |
2 |
Correct |
2537 ms |
13320 KB |
Output is correct |
3 |
Correct |
2680 ms |
14312 KB |
Output is correct |
4 |
Correct |
2405 ms |
24060 KB |
Output is correct |
5 |
Execution timed out |
4083 ms |
28236 KB |
Time limit exceeded |
6 |
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 |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
296 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 |
424 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 |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
428 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1248 ms |
128804 KB |
Output is correct |
21 |
Correct |
1161 ms |
145960 KB |
Output is correct |
22 |
Correct |
683 ms |
127432 KB |
Output is correct |
23 |
Correct |
604 ms |
113236 KB |
Output is correct |
24 |
Correct |
706 ms |
127548 KB |
Output is correct |
25 |
Correct |
1657 ms |
164868 KB |
Output is correct |
26 |
Correct |
914 ms |
124392 KB |
Output is correct |
27 |
Correct |
1764 ms |
200384 KB |
Output is correct |
28 |
Correct |
617 ms |
71588 KB |
Output is correct |
29 |
Correct |
401 ms |
53968 KB |
Output is correct |
30 |
Correct |
1419 ms |
176252 KB |
Output is correct |
31 |
Correct |
287 ms |
52152 KB |
Output is correct |
32 |
Correct |
325 ms |
54000 KB |
Output is correct |
33 |
Correct |
337 ms |
55404 KB |
Output is correct |
34 |
Correct |
346 ms |
53252 KB |
Output is correct |
35 |
Correct |
243 ms |
57804 KB |
Output is correct |
36 |
Correct |
11 ms |
2928 KB |
Output is correct |
37 |
Correct |
95 ms |
27340 KB |
Output is correct |
38 |
Correct |
312 ms |
52200 KB |
Output is correct |
39 |
Correct |
349 ms |
55968 KB |
Output is correct |
40 |
Correct |
354 ms |
71084 KB |
Output is correct |
41 |
Correct |
321 ms |
55796 KB |
Output is correct |
42 |
Correct |
367 ms |
53804 KB |
Output is correct |
43 |
Correct |
27 ms |
7708 KB |
Output is correct |
44 |
Correct |
2537 ms |
13320 KB |
Output is correct |
45 |
Correct |
2680 ms |
14312 KB |
Output is correct |
46 |
Correct |
2405 ms |
24060 KB |
Output is correct |
47 |
Execution timed out |
4083 ms |
28236 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |