#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) {
// Assume s = 0, g = n - 1 and h consists of equal elements.
int n = x.size(); // = h.size()
int m = l.size(); // = r.size() = y.size()
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 |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
7620 KB |
Output is correct |
2 |
Correct |
132 ms |
14640 KB |
Output is correct |
3 |
Correct |
138 ms |
16316 KB |
Output is correct |
4 |
Correct |
154 ms |
22712 KB |
Output is correct |
5 |
Correct |
206 ms |
25788 KB |
Output is correct |
6 |
Correct |
176 ms |
23956 KB |
Output is correct |
7 |
Correct |
67 ms |
14528 KB |
Output is correct |
8 |
Correct |
115 ms |
23224 KB |
Output is correct |
9 |
Correct |
166 ms |
24096 KB |
Output is correct |
10 |
Correct |
109 ms |
23484 KB |
Output is correct |
11 |
Correct |
8 ms |
3160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
7620 KB |
Output is correct |
2 |
Correct |
132 ms |
14640 KB |
Output is correct |
3 |
Correct |
138 ms |
16316 KB |
Output is correct |
4 |
Correct |
154 ms |
22712 KB |
Output is correct |
5 |
Correct |
206 ms |
25788 KB |
Output is correct |
6 |
Correct |
176 ms |
23956 KB |
Output is correct |
7 |
Correct |
67 ms |
14528 KB |
Output is correct |
8 |
Correct |
115 ms |
23224 KB |
Output is correct |
9 |
Correct |
166 ms |
24096 KB |
Output is correct |
10 |
Correct |
109 ms |
23484 KB |
Output is correct |
11 |
Correct |
8 ms |
3160 KB |
Output is correct |
12 |
Correct |
141 ms |
16236 KB |
Output is correct |
13 |
Correct |
161 ms |
22968 KB |
Output is correct |
14 |
Correct |
195 ms |
25784 KB |
Output is correct |
15 |
Correct |
117 ms |
21952 KB |
Output is correct |
16 |
Correct |
119 ms |
22868 KB |
Output is correct |
17 |
Correct |
124 ms |
22172 KB |
Output is correct |
18 |
Correct |
119 ms |
22200 KB |
Output is correct |
19 |
Correct |
119 ms |
22456 KB |
Output is correct |
20 |
Correct |
80 ms |
14476 KB |
Output is correct |
21 |
Correct |
17 ms |
6260 KB |
Output is correct |
22 |
Correct |
105 ms |
18944 KB |
Output is correct |
23 |
Correct |
103 ms |
20976 KB |
Output is correct |
24 |
Correct |
118 ms |
21052 KB |
Output is correct |
25 |
Correct |
104 ms |
19024 KB |
Output is correct |
26 |
Correct |
102 ms |
22820 KB |
Output is correct |
27 |
Correct |
202 ms |
24940 KB |
Output is correct |
28 |
Correct |
133 ms |
21872 KB |
Output is correct |
29 |
Correct |
188 ms |
23692 KB |
Output is correct |
30 |
Correct |
80 ms |
14588 KB |
Output is correct |
31 |
Correct |
176 ms |
24180 KB |
Output is correct |
32 |
Correct |
104 ms |
21180 KB |
Output is correct |
33 |
Correct |
100 ms |
22988 KB |
Output is correct |
34 |
Correct |
99 ms |
21944 KB |
Output is correct |
35 |
Correct |
102 ms |
21612 KB |
Output is correct |
36 |
Correct |
102 ms |
21264 KB |
Output is correct |
37 |
Correct |
87 ms |
20156 KB |
Output is correct |
38 |
Correct |
86 ms |
22204 KB |
Output is correct |
39 |
Correct |
99 ms |
20668 KB |
Output is correct |
40 |
Correct |
93 ms |
22412 KB |
Output is correct |
41 |
Correct |
120 ms |
20280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |