#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
constexpr int mxN = 200'000;
int N, M;
pair<int,int> se[mxN + 1];
vector<int> normal;
i64 dp[3 * mxN + 5];
int getNorm(int x) {
int lo = 0, hi = (int) normal.size() - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (normal[mid] == x) return mid;
else if (normal[mid] < x) lo = mid + 1;
else hi = mid - 1;
}
return -1;
}
void solve() {
cin >> N >> M;
for (int i = 0; i < N; i++) {
cin >> se[i].first >> se[i].second;
if (se[i].second < se[i].first) {
se[i].second += M;
normal.emplace_back(se[i].second - M);
}
normal.emplace_back(se[i].first);
normal.emplace_back(se[i].second);
}
sort(normal.begin(), normal.end());
normal.erase(unique(normal.begin(), normal.end()), normal.end());
for (int i = 0; i < N; i++) {
se[i].first = getNorm(se[i].first);
se[i].second = getNorm(se[i].second);
}
sort(se, se + N, [](const auto x, const auto y){
return x.second < y.second;
});
i64 ans = 1LL * (N + 1) * (N + 1);
for (int i = 0; i < (int) normal.size(); i++) {
dp[i] = 1LL * i * (N + 1);
}
int id = 0;
for (int i = 0; i < (int) normal.size(); i++) {
for (; id < N && se[id].second == i; id++) {
int l = se[id].first;
for (int j = l; j <= i; j++) {
dp[j] = min(dp[j], dp[l] + 1);
}
if (normal[i] >= M) {
ans = min(ans, 1 + dp[l] - dp[getNorm(i - M)]);
}
}
}
if (ans > N) ans = -1;
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int tc = 1;
//cin >> tc;
while (tc--) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
1 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 |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
5 ms |
600 KB |
Output is correct |
18 |
Correct |
3 ms |
604 KB |
Output is correct |
19 |
Correct |
4 ms |
708 KB |
Output is correct |
20 |
Correct |
30 ms |
4716 KB |
Output is correct |
21 |
Correct |
134 ms |
10504 KB |
Output is correct |
22 |
Correct |
152 ms |
10700 KB |
Output is correct |
23 |
Correct |
102 ms |
8136 KB |
Output is correct |
24 |
Correct |
121 ms |
10696 KB |
Output is correct |
25 |
Execution timed out |
2069 ms |
8144 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
30 ms |
4816 KB |
Output is correct |
16 |
Correct |
103 ms |
8136 KB |
Output is correct |
17 |
Incorrect |
178 ms |
10652 KB |
Output isn't correct |
18 |
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |