88 lines
2.0 KiB
C++
88 lines
2.0 KiB
C++
#include <algorithm>
|
|
#include <iostream>
|
|
#include <queue>
|
|
|
|
constexpr int INF = 0x3f3f3f3f;
|
|
|
|
template <typename T>
|
|
class QueueWithMinMax : public std::queue<T> {
|
|
protected:
|
|
std::deque<T> min_queue;
|
|
std::deque<T> max_queue;
|
|
|
|
public:
|
|
using std::queue<T>::queue;
|
|
|
|
void push(T x) {
|
|
while (!min_queue.empty() && min_queue.back() > x) {
|
|
min_queue.pop_back();
|
|
}
|
|
min_queue.push_back(x);
|
|
while (!max_queue.empty() && max_queue.back() < x) {
|
|
max_queue.pop_back();
|
|
}
|
|
max_queue.push_back(x);
|
|
std::queue<T>::push(x);
|
|
}
|
|
|
|
void pop() {
|
|
if (!min_queue.empty() && min_queue.front() == std::queue<T>::front()) {
|
|
min_queue.pop_front();
|
|
}
|
|
if (!max_queue.empty() && max_queue.front() == std::queue<T>::front()) {
|
|
max_queue.pop_front();
|
|
}
|
|
std::queue<T>::pop();
|
|
}
|
|
|
|
T get_max() {
|
|
return max_queue.front();
|
|
}
|
|
|
|
T get_min() {
|
|
return min_queue.front();
|
|
}
|
|
|
|
auto get_difference() -> decltype(get_max() - get_min()) {
|
|
if (std::queue<T>::empty()) { return -1; }
|
|
return get_max() - get_min();
|
|
}
|
|
};
|
|
|
|
using Range = std::pair<int, int>;
|
|
|
|
int main() {
|
|
std::ios::sync_with_stdio(false);
|
|
std::cin.tie(nullptr);
|
|
|
|
int n, target_diff;
|
|
std::vector<int> positions;
|
|
std::vector<Range> moments;
|
|
|
|
std::cin >> n >> target_diff;
|
|
positions.resize(n), moments.assign(n, Range(INF, -INF));
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
int temp;
|
|
std::cin >> positions[i] >> temp;
|
|
// moments[i].first = std::min(moments[i].first, temp);
|
|
}
|
|
|
|
int ans = +INF, p1 = 0, p2 = 0;
|
|
QueueWithMinMax<int> queue;
|
|
queue.push(moments[0]);
|
|
while (p2 < n) {
|
|
if (queue.get_difference() < target_diff) {
|
|
p2++;
|
|
if (p2 < n) { queue.push(moments[p2]); }
|
|
} else {
|
|
ans = std::min(ans, p2 - p1 + 1);
|
|
p1++;
|
|
queue.pop();
|
|
}
|
|
}
|
|
std::cout << ans << std::endl;
|
|
|
|
return 0;
|
|
}
|