#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[2 * mxN + 5];
int getNorm(int x) {
int lo = 0, hi = (int) normal.size() - 1, opt = 0;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (normal[mid] <= x) {
opt = mid;
lo = mid + 1;
} else
hi = mid - 1;
}
return opt;
}
void solve() {
cin >> N >> M;
normal.emplace_back(-1);
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].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;
});
/*
for (int i = 0; i < N; i++) {
cout << se[i].first << " " << se[i].second << endl;
}
cout << "normal: ";
for (int i = 0; i < (int) normal.size(); i++) {
cout << normal[i] << " ";
}
cout << endl;
*/
i64 ans = 1LL * (N + 1) * (N + 1);
for (int i = 0; i < (int) normal.size(); i++) {
dp[i] = 1LL * (i + 1) * (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] + 1);
}
if (normal[i] >= M) {
ans = min(ans, dp[i] - dp[getNorm(normal[i] - M)] + 1);
}
}
}
/*
cout << "dp: ";
for (int i = 0; i < (int) normal.size(); i++) {
cout << dp[i] << " ";
}
cout << endl;
*/
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 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
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 |
1 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |