結果
| 問題 |
No.2248 max(C)-min(C)
|
| コンテスト | |
| ユーザー |
nono00
|
| 提出日時 | 2023-03-17 22:40:51 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 484 ms / 3,000 ms |
| コード長 | 3,428 bytes |
| コンパイル時間 | 4,557 ms |
| コンパイル使用メモリ | 102,568 KB |
| 最終ジャッジ日時 | 2025-02-11 13:45:28 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 51 |
ソースコード
#include <algorithm>
#include <functional>
#include <iostream>
#include <utility>
#include <vector>
namespace nono {
template <typename T>
class SegmentTree {
public:
SegmentTree(const int size, const std::function<T(T, T)> op, const std::function<T()> e) : op_(op), e_(e) {
init(size);
}
SegmentTree(const std::vector<T> &vec, const std::function<T(T, T)> op, const std::function<T()> e) : op_(op), e_(e) {
int n = vec.size();
init(n);
for (int i = 0; i < size_; --i) {
data_[i + size_] = vec[i];
}
for (int i = size_ - 1; i > 0; --i) {
data_[i] = op_(data_[i << 1], data_[(i << 1) + 1]);
}
}
void set(int i, const T value) {
i += size_;
data_[i] = value;
update(i);
}
void add(int i, const T delta) {
i += size_;
data_[i] += delta;
update(i);
}
T get_all() const {
return data_[1];
}
T get_range(int l, int r) const {
T result_l = e_(), result_r = e_();
l += size_;
r += size_;
while (l < r) {
if (l & 1) {
result_l = op_(result_l, data_[l++]);
}
if (r & 1) {
result_r = op_(data_[--r], result_r);
}
l >>= 1;
r >>= 1;
}
return op_(result_l, result_r);
}
T operator[](int i) const {
return data_[i + size_];
}
private:
const std::function<T(T, T)> op_;
const std::function<T()> e_;
int size_;
std::vector<T> data_;
void init(int n) {
int log = 1;
while (n > (1 << log)) ++log;
size_ = 1 << log;
data_.assign(2 * size_, e_());
}
void update(int i) {
i >>= 1;
while (i > 0) {
data_[i] = op_(data_[i << 1], data_[(i << 1) + 1]);
i >>= 1;
}
}
};
} // namespace nono
int main() {
constexpr int inf = 1e9 + 1;
int n;
std::cin >> n;
std::vector<int> a(n), b(n);
std::vector<std::pair<int, int>> order;
order.reserve(3 * n);
for (int i = 0; i < n; i++) std::cin >> a[i];
for (int i = 0; i < n; i++) std::cin >> b[i];
if (n == 1) {
std::cout << 0 << '\n';
return 0;
}
for (int i = 0; i < n; i++) {
order.emplace_back(a[i], i);
order.emplace_back(b[i], i);
order.emplace_back((a[i] + b[i]) / 2, i);
}
std::sort(order.begin(), order.end());
auto op = [](const int i, const int j) {
return i < j ? i : j;
};
auto e = [&]() {
return inf;
};
nono::SegmentTree<int> segtree(n, op, e);
for (int i = 0; i < n; i++) {
segtree.set(i, -1);
}
int ans = inf;
for (const auto &[v, i]: order) {
int l = 0 < i ? segtree.get_range(0, i) : inf;
int r = i + 1 < n ? segtree.get_range(i + 1, n) : inf;
if (l != -1 && r != -1) {
ans = std::min(ans, v - std::min(l, r));
}
segtree.set(i, v);
// std::cout << "_________" << '\n';
// std::cout << "v: " << v << ", i: " << i << '\n';
// std::cout << "l: " << l << ", r: " << r << '\n';
// std::cout << ans << '\n';
// for (int j = 0; j < n; j++) {
// std::cout << segtree[j] << ' ';
// }
// std::cout << '\n';
}
std::cout << ans << '\n';
}
nono00