#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
#define all(x) x.begin(), x.end()
#define size(x) (int)x.size()
template<class S, class T>
bool chmin(S &a, const T &b) {
return (a > b ? (a = b) == b : false);
}
template<class S, class T>
bool chmax(S &a, const T &b) {
return (a < b ? (a = b) == b : false);
}
const int inf = 1e9;
const ll INF = 1e18;
const int mod = 998244353;
const int N = 2500;
int n, l[N * 100], r[N * 100];
vector<vector<int>> d(N, vector<int> (N, inf));
void bfs(int init) {
queue<pair<int, int>> q;
d[l[init]][r[init]] = 1;
q.push({l[init], r[init]});
while (!q.empty()) {
int L = q.front().first, R = q.front().second;
q.pop();
for (int i = L; i <= R; ++i) {
int new_l = min(L, l[i]), new_r = max(R, r[i]);
if (chmin(d[new_l][new_r], d[L][R] + 1)) {
q.push({new_l, new_r});
}
}
}
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> l[i] >> r[i];
l[i]--, r[i]--;
}
int q, x;
cin >> q >> x;
if (n <= 2500) {
bfs(--x);
cout << (d[0][n - 1] == inf ? -1 : d[0][n - 1]);
} else {
vector<int> dp(n + 1, inf);
dp[0] = 1;
int R = r[0], last = 0;
for (int i = 0; i < n; ++i) {
int k = R;
for (int j = i; j <= R; ++j) {
dp[j] = dp[last];
chmax(k, r[j]);
}
dp[R + 1] = dp[last] + 1;
last = R + 1;
}
cout << dp[n - 1];
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
24924 KB |
Output is correct |
2 |
Correct |
12 ms |
24920 KB |
Output is correct |
3 |
Correct |
12 ms |
24924 KB |
Output is correct |
4 |
Incorrect |
36 ms |
27244 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
24920 KB |
Output is correct |
2 |
Correct |
12 ms |
24924 KB |
Output is correct |
3 |
Correct |
13 ms |
24996 KB |
Output is correct |
4 |
Correct |
12 ms |
24924 KB |
Output is correct |
5 |
Correct |
12 ms |
24848 KB |
Output is correct |
6 |
Correct |
12 ms |
24924 KB |
Output is correct |
7 |
Correct |
12 ms |
24920 KB |
Output is correct |
8 |
Correct |
13 ms |
24920 KB |
Output is correct |
9 |
Correct |
12 ms |
24836 KB |
Output is correct |
10 |
Correct |
12 ms |
24804 KB |
Output is correct |
11 |
Correct |
14 ms |
24952 KB |
Output is correct |
12 |
Correct |
17 ms |
24924 KB |
Output is correct |
13 |
Correct |
13 ms |
24924 KB |
Output is correct |
14 |
Correct |
13 ms |
24924 KB |
Output is correct |
15 |
Correct |
14 ms |
24924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
24920 KB |
Output is correct |
2 |
Correct |
12 ms |
24924 KB |
Output is correct |
3 |
Correct |
13 ms |
24996 KB |
Output is correct |
4 |
Correct |
12 ms |
24924 KB |
Output is correct |
5 |
Correct |
12 ms |
24848 KB |
Output is correct |
6 |
Correct |
12 ms |
24924 KB |
Output is correct |
7 |
Correct |
12 ms |
24920 KB |
Output is correct |
8 |
Correct |
13 ms |
24920 KB |
Output is correct |
9 |
Correct |
12 ms |
24836 KB |
Output is correct |
10 |
Correct |
12 ms |
24804 KB |
Output is correct |
11 |
Correct |
14 ms |
24952 KB |
Output is correct |
12 |
Correct |
17 ms |
24924 KB |
Output is correct |
13 |
Correct |
13 ms |
24924 KB |
Output is correct |
14 |
Correct |
13 ms |
24924 KB |
Output is correct |
15 |
Correct |
14 ms |
24924 KB |
Output is correct |
16 |
Correct |
1309 ms |
26872 KB |
Output is correct |
17 |
Correct |
1126 ms |
25044 KB |
Output is correct |
18 |
Correct |
29 ms |
24924 KB |
Output is correct |
19 |
Correct |
43 ms |
24924 KB |
Output is correct |
20 |
Execution timed out |
2067 ms |
25032 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
24920 KB |
Output is correct |
2 |
Correct |
12 ms |
24924 KB |
Output is correct |
3 |
Correct |
13 ms |
24996 KB |
Output is correct |
4 |
Correct |
12 ms |
24924 KB |
Output is correct |
5 |
Correct |
12 ms |
24848 KB |
Output is correct |
6 |
Correct |
12 ms |
24924 KB |
Output is correct |
7 |
Correct |
12 ms |
24920 KB |
Output is correct |
8 |
Correct |
13 ms |
24920 KB |
Output is correct |
9 |
Correct |
12 ms |
24836 KB |
Output is correct |
10 |
Correct |
12 ms |
24804 KB |
Output is correct |
11 |
Correct |
14 ms |
24952 KB |
Output is correct |
12 |
Correct |
17 ms |
24924 KB |
Output is correct |
13 |
Correct |
13 ms |
24924 KB |
Output is correct |
14 |
Correct |
13 ms |
24924 KB |
Output is correct |
15 |
Correct |
14 ms |
24924 KB |
Output is correct |
16 |
Correct |
1309 ms |
26872 KB |
Output is correct |
17 |
Correct |
1126 ms |
25044 KB |
Output is correct |
18 |
Correct |
29 ms |
24924 KB |
Output is correct |
19 |
Correct |
43 ms |
24924 KB |
Output is correct |
20 |
Execution timed out |
2067 ms |
25032 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
24924 KB |
Output is correct |
2 |
Correct |
12 ms |
24920 KB |
Output is correct |
3 |
Correct |
12 ms |
24924 KB |
Output is correct |
4 |
Incorrect |
36 ms |
27244 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |