Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sort_maximal() #205

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions utilities/sort_maximal.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
#include <algorithm>
#include <utility>
#include <vector>

// 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 <class T> std::vector<std::pair<T, T>> sort_maximal(std::vector<std::pair<T, T>> ranges) {
std::sort(ranges.begin(), ranges.end());
std::vector<std::pair<T, T>> 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;
}
15 changes: 15 additions & 0 deletions utilities/sort_maximal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: 区間たちをソートし極大でないものを消去
documentation_of: ./sort_maximal.hpp
---

## 使用方法

```cpp
vector<pair<int, int>> segs;
segs = sort_maximal(segs);
```

## 問題例

- [AtCoder Beginner Contest 254 G - Elevators](https://atcoder.jp/contests/abc254/tasks/abc254_g)