From 74ab0166cc571b361947e496e0a91c02440d2d31 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sun, 22 May 2022 20:47:37 +0800 Subject: [PATCH] =?UTF-8?q?#1426.=20=E3=80=902022.5.21=20=E8=81=94?= =?UTF-8?q?=E8=80=83=E3=80=91=E7=A9=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://sjzezoj.com/submission/53072 --- S2OJ/1426/1426.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 S2OJ/1426/1426.cpp diff --git a/S2OJ/1426/1426.cpp b/S2OJ/1426/1426.cpp new file mode 100644 index 00000000..d7622b07 --- /dev/null +++ b/S2OJ/1426/1426.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 2e5 + 5; + +int n, ans; +std::pair lines[N]; + +// 包含 +std::priority_queue< + std::pair, + std::vector>, + auto(*)(std::pair, std::pair)->bool> +q1([](std::pair a, std::pair b) -> bool { + return a.second - a.first < b.second - b.first; +}); + +// 相交 +std::priority_queue< + std::pair, + std::vector>, + auto(*)(std::pair, std::pair)->bool> +q2([](std::pair a, std::pair b) -> bool { + return a.first + a.second > b.first + b.second; +}); + +int main() { + std::ios::sync_with_stdio(false); + + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> lines[i].first >> lines[i].second; + } + + std::sort(lines + 1, lines + 1 + n); + + q1.push(lines[1]); + q2.push(lines[1]); + + for (int i = 2; i <= n; i++) { + while (!q1.empty() && lines[i].second >= q1.top().second) q1.pop(); // 剔除已经相交的线段 + while (!q2.empty() && lines[i].first >= q2.top().second) q2.pop(); // 剔除已经相离的线段 + + if (!q1.empty()) { + auto p = q1.top(), + q = lines[i]; + ans = std::max(ans, (p.second - p.first) - (q.second - q.first)); + } + + if (!q2.empty()) { + auto p = q2.top(), + q = lines[i]; + ans = std::max(ans, (q.second - p.second) + (q.first - p.first)); + } + + q1.push(lines[i]); + q2.push(lines[i]); + } + + cout << ans << endl; + + return 0; +}