From 85c8745cd59693a665be938d7e4cd41499330dda Mon Sep 17 00:00:00 2001 From: hitonanode <32937551+hitonanode@users.noreply.github.com> Date: Sun, 5 Jun 2022 13:38:57 +0900 Subject: [PATCH] Add sort_maximal() --- utilities/sort_maximal.hpp | 18 ++++++++++++++++++ utilities/sort_maximal.md | 15 +++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 utilities/sort_maximal.hpp create mode 100644 utilities/sort_maximal.md diff --git a/utilities/sort_maximal.hpp b/utilities/sort_maximal.hpp new file mode 100644 index 00000000..55f03f5d --- /dev/null +++ b/utilities/sort_maximal.hpp @@ -0,0 +1,18 @@ +#pragma once +#include +#include +#include + +// Sort segments and erase not maximal ones +// {{0, 2}, {1, 5}, {3, 4}} => {{0, 2}, {1, 5}} +// {{1, 3}, {0, 1}, {1, 3}} => {{0, 1}, {1, 3}} // remove duplicates +// Verified: abc254g https://atcoder.jp/contests/abc254/tasks/abc254_g +template std::vector> sort_maximal(std::vector> ranges) { + std::sort(ranges.begin(), ranges.end()); + std::vector> ret; + for (const auto &p : ranges) { + if (!ret.empty() and ret.back().first == p.first) ret.pop_back(); + if (ret.empty() or ret.back().second < p.second) ret.push_back(p); + } + return ret; +} diff --git a/utilities/sort_maximal.md b/utilities/sort_maximal.md new file mode 100644 index 00000000..bd9540ba --- /dev/null +++ b/utilities/sort_maximal.md @@ -0,0 +1,15 @@ +--- +title: 区間たちをソートし極大でないものを消去 +documentation_of: ./sort_maximal.hpp +--- + +## 使用方法 + +```cpp +vector> segs; +segs = sort_maximal(segs); +``` + +## 問題例 + +- [AtCoder Beginner Contest 254 G - Elevators](https://atcoder.jp/contests/abc254/tasks/abc254_g) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy