#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 3010;
const int INF = 1e9;
int n;
int dp[N][N];
int L[N], R[N];
vector<vector<int>> ans;
int recur(int l, int r) {
if(l == 1 && r == n) ans[l][r] = 0;
if(ans[l][r] != -1) return ans[l][r];
ans[l][r] = INF;
for(int i = l; i <= r; i++) {
if(l <= L[i] && R[i] <= r) continue;
ans[l][r] = min(ans[l][r], recur(min(l, L[i]), max(r, R[i])) + 1);
}
return ans[l][r];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
cin >> L[i] >> R[i];
int mx[n + 1] = {};
for(int i = 1; i <= n; i++)
mx[i] = max(mx[i - 1], R[i]);
// if(n <= 300) {
// dp[1][n] = 0;
// for(int len = n - 1; len >= 1; len--) {
// for(int l = 1; l <= n - len + 1; l++) {
// int r = l + len - 1;
// dp[l][r] = INF;
// for(int i = l; i <= r; i++) {
// dp[l][r] = min(dp[l][r], dp[min(l, L[i])][max(r, R[i])] + 1);
// }
// }
// }
// int q;
// cin >> q;
// while(q--) {
// int x;
// cin >> x;
// cout << (dp[x][x] == INF ? -1 : dp[x][x]) << '\n';
// }
// return 0;
// }
if(n <= 2500) {
int q;
cin >> q;
int x;
cin >> x;
ans.resize(n + 10, vector<int>(n + 10, -1));
int val = recur(x, x);
cout << (val == INF ? -1 : val);
return 0;
}
int q;
cin >> q;
int x, res = 0;
cin >> x;
while(x < n) {
res++;
if(mx[x] == x) {
cout << -1;
return 0;
}
x = max(x, mx[x]);
}
cout << res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
26 ms |
4236 KB |
Output is correct |
5 |
Correct |
25 ms |
4284 KB |
Output is correct |
6 |
Correct |
26 ms |
4316 KB |
Output is correct |
7 |
Correct |
21 ms |
3540 KB |
Output is correct |
8 |
Correct |
20 ms |
3068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
704 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
704 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
708 KB |
Output is correct |
16 |
Correct |
1297 ms |
22032 KB |
Output is correct |
17 |
Correct |
523 ms |
24788 KB |
Output is correct |
18 |
Correct |
21 ms |
25044 KB |
Output is correct |
19 |
Correct |
34 ms |
23892 KB |
Output is correct |
20 |
Correct |
1683 ms |
25264 KB |
Output is correct |
21 |
Correct |
737 ms |
25264 KB |
Output is correct |
22 |
Correct |
13 ms |
25008 KB |
Output is correct |
23 |
Correct |
1388 ms |
25188 KB |
Output is correct |
24 |
Correct |
702 ms |
25104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
704 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
708 KB |
Output is correct |
16 |
Correct |
1297 ms |
22032 KB |
Output is correct |
17 |
Correct |
523 ms |
24788 KB |
Output is correct |
18 |
Correct |
21 ms |
25044 KB |
Output is correct |
19 |
Correct |
34 ms |
23892 KB |
Output is correct |
20 |
Correct |
1683 ms |
25264 KB |
Output is correct |
21 |
Correct |
737 ms |
25264 KB |
Output is correct |
22 |
Correct |
13 ms |
25008 KB |
Output is correct |
23 |
Correct |
1388 ms |
25188 KB |
Output is correct |
24 |
Correct |
702 ms |
25104 KB |
Output is correct |
25 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
26 ms |
4236 KB |
Output is correct |
5 |
Correct |
25 ms |
4284 KB |
Output is correct |
6 |
Correct |
26 ms |
4316 KB |
Output is correct |
7 |
Correct |
21 ms |
3540 KB |
Output is correct |
8 |
Correct |
20 ms |
3068 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
704 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
2 ms |
708 KB |
Output is correct |
24 |
Correct |
1297 ms |
22032 KB |
Output is correct |
25 |
Correct |
523 ms |
24788 KB |
Output is correct |
26 |
Correct |
21 ms |
25044 KB |
Output is correct |
27 |
Correct |
34 ms |
23892 KB |
Output is correct |
28 |
Correct |
1683 ms |
25264 KB |
Output is correct |
29 |
Correct |
737 ms |
25264 KB |
Output is correct |
30 |
Correct |
13 ms |
25008 KB |
Output is correct |
31 |
Correct |
1388 ms |
25188 KB |
Output is correct |
32 |
Correct |
702 ms |
25104 KB |
Output is correct |
33 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |