some programs!!!
This commit is contained in:
1 parent
71f1022dea
commit
ca6daa582c
43 files changed
+1452
-381
No files matched your search
@@ -0,0 +1,96 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#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>;
|
||||
using Coord = std::pair<int, int>;
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
int n, target_diff;
|
||||
std::vector<Coord> coords;
|
||||
std::vector<int> positions;
|
||||
std::map<int, Range> moments;
|
||||
// std::vector<Range> moments;
|
||||
|
||||
std::cin >> n >> target_diff;
|
||||
coords.resize(n);
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
// std::cin >> positions[i] >> temp;
|
||||
std::cin >> coords[i].first >> coords[i].second;
|
||||
}
|
||||
std::sort(coords.begin(), coords.end());
|
||||
|
||||
int ans = +INF, p1 = 0, p2 = 0;
|
||||
QueueWithMinMax<int> queue;
|
||||
queue.push(coords[0].second);
|
||||
while (p2 < n) {
|
||||
if (queue.get_difference() < target_diff) {
|
||||
p2++;
|
||||
if (p2 < n) { queue.push(coords[p2].second); }
|
||||
} else {
|
||||
if (coords[p1].first == coords[p2].first) {
|
||||
ans = 0;
|
||||
break;
|
||||
}
|
||||
ans = std::min(ans, coords[p2].first - coords[p1].first);
|
||||
p1++;
|
||||
queue.pop();
|
||||
}
|
||||
}
|
||||
if (ans == +INF) { ans = -1; }
|
||||
std::cout << ans << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user