#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];
int a = -1, b = -1;
for (int j = M - 1; j > i; j--) {
int v = ord[j];
if (L[v] > R[u]) {
continue;
}
if (Y[v] >= Y[u]) {
if (a == -1 || Y[v] < Y[a]) {
a = v;
}
} else {
if (b == -1 || Y[v] > Y[b]) {
b = v;
}
}
}
if (a != -1) {
dp[a] = min(dp[a], dp[u] + Y[a] - Y[u] + X[R[a]] - X[R[u]]);
}
if (b != -1) {
dp[b] = min(dp[b], dp[u] + Y[u] - Y[b] + X[R[b]] - X[R[u]]);
}
}
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];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 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 |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1268 ms |
127440 KB |
Output is correct |
4 |
Correct |
1154 ms |
144784 KB |
Output is correct |
5 |
Correct |
714 ms |
126072 KB |
Output is correct |
6 |
Correct |
599 ms |
112040 KB |
Output is correct |
7 |
Correct |
709 ms |
126216 KB |
Output is correct |
8 |
Correct |
2016 ms |
163488 KB |
Output is correct |
9 |
Correct |
977 ms |
123028 KB |
Output is correct |
10 |
Correct |
1800 ms |
199124 KB |
Output is correct |
11 |
Correct |
690 ms |
70412 KB |
Output is correct |
12 |
Correct |
459 ms |
52668 KB |
Output is correct |
13 |
Correct |
1462 ms |
175080 KB |
Output is correct |
14 |
Correct |
307 ms |
50908 KB |
Output is correct |
15 |
Correct |
327 ms |
52728 KB |
Output is correct |
16 |
Correct |
370 ms |
54120 KB |
Output is correct |
17 |
Correct |
343 ms |
51984 KB |
Output is correct |
18 |
Correct |
234 ms |
56436 KB |
Output is correct |
19 |
Correct |
8 ms |
2800 KB |
Output is correct |
20 |
Correct |
107 ms |
25452 KB |
Output is correct |
21 |
Correct |
309 ms |
49428 KB |
Output is correct |
22 |
Correct |
352 ms |
52780 KB |
Output is correct |
23 |
Correct |
345 ms |
68184 KB |
Output is correct |
24 |
Correct |
336 ms |
52828 KB |
Output is correct |
25 |
Correct |
422 ms |
51032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
834 ms |
7684 KB |
Output is correct |
2 |
Execution timed out |
4078 ms |
13268 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
834 ms |
7684 KB |
Output is correct |
2 |
Execution timed out |
4078 ms |
13268 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 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 |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1268 ms |
127440 KB |
Output is correct |
21 |
Correct |
1154 ms |
144784 KB |
Output is correct |
22 |
Correct |
714 ms |
126072 KB |
Output is correct |
23 |
Correct |
599 ms |
112040 KB |
Output is correct |
24 |
Correct |
709 ms |
126216 KB |
Output is correct |
25 |
Correct |
2016 ms |
163488 KB |
Output is correct |
26 |
Correct |
977 ms |
123028 KB |
Output is correct |
27 |
Correct |
1800 ms |
199124 KB |
Output is correct |
28 |
Correct |
690 ms |
70412 KB |
Output is correct |
29 |
Correct |
459 ms |
52668 KB |
Output is correct |
30 |
Correct |
1462 ms |
175080 KB |
Output is correct |
31 |
Correct |
307 ms |
50908 KB |
Output is correct |
32 |
Correct |
327 ms |
52728 KB |
Output is correct |
33 |
Correct |
370 ms |
54120 KB |
Output is correct |
34 |
Correct |
343 ms |
51984 KB |
Output is correct |
35 |
Correct |
234 ms |
56436 KB |
Output is correct |
36 |
Correct |
8 ms |
2800 KB |
Output is correct |
37 |
Correct |
107 ms |
25452 KB |
Output is correct |
38 |
Correct |
309 ms |
49428 KB |
Output is correct |
39 |
Correct |
352 ms |
52780 KB |
Output is correct |
40 |
Correct |
345 ms |
68184 KB |
Output is correct |
41 |
Correct |
336 ms |
52828 KB |
Output is correct |
42 |
Correct |
422 ms |
51032 KB |
Output is correct |
43 |
Correct |
834 ms |
7684 KB |
Output is correct |
44 |
Execution timed out |
4078 ms |
13268 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |