#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<int> s(n), e(n);
vector<int> normal;
vector<int> non_normal;
for (int i = 0; i < n; ++i) {
cin >> s[i] >> e[i];
if (s[i] <= e[i]) {
normal.push_back(i);
} else {
non_normal.push_back(i);
}
}
sort(normal.begin(), normal.end(), [&](int i, int j) {
return s[i] < s[j];
});
int res = 0;
int lst = 0;
for (int i = 0, mx = 0; i < (int) normal.size(); ++i) {
if (s[normal[i]] > lst) {
if (mx < s[normal[i]]) {
break;
} else {
res++;
lst = mx;
mx = max(mx, e[normal[i]]);
}
} else {
mx = max(mx, e[normal[i]]);
}
if (i == (int) normal.size() - 1) {
lst = mx;
res++;
}
}
if (lst < m - 1) {
res = n + 1;
}
int ans = res;
for (int i = 0; i < (int) non_normal.size(); ++i) {
vector<pair<int, int>> ranges;
for (int j = 0; j < (int) non_normal.size(); ++j) {
if (i != j) {
ranges.push_back({0, e[non_normal[j]]});
}
}
for (int j = 0; j < (int) normal.size(); ++j) {
ranges.push_back({s[normal[j]], e[normal[j]]});
}
sort(ranges.begin(), ranges.end());
res = 1;
lst = e[non_normal[i]];
for (int j = 0, mx = e[non_normal[i]]; j < (int) ranges.size() && lst < s[non_normal[i]]; ++j) {
auto [l, r] = ranges[j];
if (l > lst) {
if (mx < l) {
break;
} else {
res++;
lst = mx;
mx = max(mx, r);
}
} else {
mx = max(mx, r);
}
if (j == (int) ranges.size() - 1) {
lst = mx;
res++;
}
}
if (lst < s[non_normal[i]]) {
res = n + 1;
}
ans = min(ans, res);
}
if (ans > n) {
ans = -1;
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
82 ms |
684 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Execution timed out |
2076 ms |
7260 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
3 ms |
748 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Execution timed out |
2095 ms |
7268 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |