#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <functional>
#include <queue>
#warning That's the baby, that's not my baby
typedef long long ll;
const int NMAX = 2500;
/**
ce cred eu ca se intampla defapt e ca mereu voi putea accesa un interval de tari
si atunci o sa am o dinamica de genu
dp[i][l] =def= care e cel mai din dreapta r la care pot ajunge dupa i operatii, a.i. am capatul din stanga pe pozitia l
asta pare greu de optimizat
dp[l][r] =def= #min de operatii ca sa ajung cu capatul stanga in l si capatul dreapta in r
ok si facem asta gen invers si iese 0-1 bfs
**/
struct Country {
int l, r;
};
int dist[NMAX * NMAX + 1];
std::vector<std::pair<int, int>> g[NMAX * NMAX + 1];
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
int n;
std::cin >> n;
std::vector<Country> v(n + 1);
int rMax[n + 1] = {};
for (int i = 1; i <= n; i++) {
std::cin >> v[i].l >> v[i].r;
rMax[i] = std::max(rMax[i - 1], v[i].r);
}
std::function<int(int, int)> encode = [&] (int i, int j) -> int {
return (i - 1) * n + j;
};
for (int i = 1; i <= n * n; i++) {
dist[i] = 1e9;
}
dist[encode(1, n)] = 0;
for (int i = 1; i <= n; i++) {
int newL = n + 1, newR = 0;
for (int j = i; j <= n; j++) {
newL = std::min(newL, v[j].l);
newR = std::max(newR, v[j].r);
g[encode(newL, j)].push_back({encode(i, j), 1});
g[encode(i, newR)].push_back({encode(i, j), 1});
g[encode(v[i].l, v[i].r)].push_back({encode(i, j), 1});
if (i != j) {
g[encode(i + 1, j)].push_back({encode(i, j), 0});
}
}
}
std::deque<int> deq;
deq.push_back(encode(1, n));
while (!deq.empty()) {
int u = deq.front();
deq.pop_front();
for (const auto &[v, c] : g[u]) {
if (dist[u] + c < dist[v]) {
if (c == 0) {
deq.push_front(v);
} else {
deq.push_back(v);
}
dist[v] = dist[u] + c;
}
}
}
for (int i = 1; i <= n * n; i++) {
dist[i] = (dist[i] == 1e9? -1 : dist[i]);
}
int Q;
std::cin >> Q;
while (Q--) {
int x;
std::cin >> x;
if (x == 1) {
int answer = 0;
while (x != n && rMax[x] > x) {
x = rMax[x];
answer++;
}
if (x != n) {
answer = -1;
}
std::cout << answer << '\n';
} else {
std::cout << dist[encode(x, x)] << '\n';
}
}
return 0;
}
Compilation message
passport.cpp:8:2: warning: #warning That's the baby, that's not my baby [-Wcpp]
8 | #warning That's the baby, that's not my baby
| ^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
148308 KB |
Output is correct |
2 |
Correct |
36 ms |
148304 KB |
Output is correct |
3 |
Correct |
32 ms |
148304 KB |
Output is correct |
4 |
Runtime error |
201 ms |
352212 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
148300 KB |
Output is correct |
2 |
Correct |
32 ms |
148312 KB |
Output is correct |
3 |
Correct |
33 ms |
148312 KB |
Output is correct |
4 |
Correct |
32 ms |
148316 KB |
Output is correct |
5 |
Correct |
31 ms |
148316 KB |
Output is correct |
6 |
Correct |
33 ms |
148312 KB |
Output is correct |
7 |
Correct |
32 ms |
148308 KB |
Output is correct |
8 |
Correct |
31 ms |
148312 KB |
Output is correct |
9 |
Correct |
32 ms |
148496 KB |
Output is correct |
10 |
Correct |
33 ms |
148308 KB |
Output is correct |
11 |
Correct |
39 ms |
151136 KB |
Output is correct |
12 |
Correct |
39 ms |
150868 KB |
Output is correct |
13 |
Correct |
40 ms |
151256 KB |
Output is correct |
14 |
Correct |
36 ms |
150864 KB |
Output is correct |
15 |
Correct |
38 ms |
150876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
148300 KB |
Output is correct |
2 |
Correct |
32 ms |
148312 KB |
Output is correct |
3 |
Correct |
33 ms |
148312 KB |
Output is correct |
4 |
Correct |
32 ms |
148316 KB |
Output is correct |
5 |
Correct |
31 ms |
148316 KB |
Output is correct |
6 |
Correct |
33 ms |
148312 KB |
Output is correct |
7 |
Correct |
32 ms |
148308 KB |
Output is correct |
8 |
Correct |
31 ms |
148312 KB |
Output is correct |
9 |
Correct |
32 ms |
148496 KB |
Output is correct |
10 |
Correct |
33 ms |
148308 KB |
Output is correct |
11 |
Correct |
39 ms |
151136 KB |
Output is correct |
12 |
Correct |
39 ms |
150868 KB |
Output is correct |
13 |
Correct |
40 ms |
151256 KB |
Output is correct |
14 |
Correct |
36 ms |
150864 KB |
Output is correct |
15 |
Correct |
38 ms |
150876 KB |
Output is correct |
16 |
Correct |
420 ms |
349156 KB |
Output is correct |
17 |
Correct |
716 ms |
351628 KB |
Output is correct |
18 |
Correct |
495 ms |
360932 KB |
Output is correct |
19 |
Correct |
439 ms |
359464 KB |
Output is correct |
20 |
Correct |
630 ms |
349336 KB |
Output is correct |
21 |
Correct |
497 ms |
340960 KB |
Output is correct |
22 |
Correct |
421 ms |
373364 KB |
Output is correct |
23 |
Correct |
491 ms |
373728 KB |
Output is correct |
24 |
Correct |
463 ms |
374780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
148300 KB |
Output is correct |
2 |
Correct |
32 ms |
148312 KB |
Output is correct |
3 |
Correct |
33 ms |
148312 KB |
Output is correct |
4 |
Correct |
32 ms |
148316 KB |
Output is correct |
5 |
Correct |
31 ms |
148316 KB |
Output is correct |
6 |
Correct |
33 ms |
148312 KB |
Output is correct |
7 |
Correct |
32 ms |
148308 KB |
Output is correct |
8 |
Correct |
31 ms |
148312 KB |
Output is correct |
9 |
Correct |
32 ms |
148496 KB |
Output is correct |
10 |
Correct |
33 ms |
148308 KB |
Output is correct |
11 |
Correct |
39 ms |
151136 KB |
Output is correct |
12 |
Correct |
39 ms |
150868 KB |
Output is correct |
13 |
Correct |
40 ms |
151256 KB |
Output is correct |
14 |
Correct |
36 ms |
150864 KB |
Output is correct |
15 |
Correct |
38 ms |
150876 KB |
Output is correct |
16 |
Correct |
420 ms |
349156 KB |
Output is correct |
17 |
Correct |
716 ms |
351628 KB |
Output is correct |
18 |
Correct |
495 ms |
360932 KB |
Output is correct |
19 |
Correct |
439 ms |
359464 KB |
Output is correct |
20 |
Correct |
630 ms |
349336 KB |
Output is correct |
21 |
Correct |
497 ms |
340960 KB |
Output is correct |
22 |
Correct |
421 ms |
373364 KB |
Output is correct |
23 |
Correct |
491 ms |
373728 KB |
Output is correct |
24 |
Correct |
463 ms |
374780 KB |
Output is correct |
25 |
Correct |
32 ms |
148304 KB |
Output is correct |
26 |
Correct |
32 ms |
148316 KB |
Output is correct |
27 |
Correct |
432 ms |
364404 KB |
Output is correct |
28 |
Correct |
706 ms |
354316 KB |
Output is correct |
29 |
Correct |
629 ms |
349284 KB |
Output is correct |
30 |
Correct |
495 ms |
340748 KB |
Output is correct |
31 |
Correct |
418 ms |
361816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
148308 KB |
Output is correct |
2 |
Correct |
36 ms |
148304 KB |
Output is correct |
3 |
Correct |
32 ms |
148304 KB |
Output is correct |
4 |
Runtime error |
201 ms |
352212 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |