競技プログラミング用の C++ ライブラリ
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
// ErasablePriorityQueue を std::multiset とランダムに比較する
// 問題の入力は使わず、すべて一致したら Hello World を出力する
#include "../../../src/data_structures/erasable-priority-queue.hpp"
#include <functional>
#include <iostream>
#include <iterator>
#include <random>
#include <set>
#include <vector>
int main() {
std::mt19937 rng(314);
bool ok = true;
for (int rep = 0; rep < 1000 && ok; rep++) {
ErasablePriorityQueue<int> max_q;
ErasablePriorityQueue<int, std::greater<int>> min_q;
std::multiset<int> st;
for (int t = 0; t < 100; t++) {
int op = rng() % 4;
if (op <= 1 || st.empty()) {
int x = rng() % 10;
max_q.push(x);
min_q.push(x);
st.insert(x);
} else if (op == 2) {
// 含まれている要素をランダムに選んで削除する
auto it = std::next(st.begin(), rng() % st.size());
max_q.erase(*it);
min_q.erase(*it);
st.erase(it);
} else {
if (rng() % 2) {
max_q.pop();
min_q.erase(*st.rbegin());
st.erase(std::prev(st.end()));
} else {
min_q.pop();
max_q.erase(*st.begin());
st.erase(st.begin());
}
}
if (max_q.size() != static_cast<int>(st.size()) || min_q.size() != static_cast<int>(st.size())) ok = false;
if (max_q.empty() != st.empty() || min_q.empty() != st.empty()) ok = false;
if (!st.empty() && (max_q.top() != *st.rbegin() || min_q.top() != *st.begin())) ok = false;
}
}
if (ok)
std::cout << "Hello World" << std::endl;
else
std::cerr << "failed" << std::endl;
}#line 1 "test/unittest/data_structures/erasable-priority-queue.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
// ErasablePriorityQueue を std::multiset とランダムに比較する
// 問題の入力は使わず、すべて一致したら Hello World を出力する
#line 2 "src/data_structures/erasable-priority-queue.hpp"
/**
* @file erasable-priority-queue.hpp
* @brief 削除可能な優先度付きキュー
*/
#include <cassert>
#include <functional>
#include <queue>
#include <vector>
/**
* @brief 削除可能な優先度付きキュー
*
* std::priority_queue と同じく、既定では最大の要素を取り出す
* 削除する要素を別のヒープに入れておき、先頭に来たときに取り除く。各操作は償却 O(log n)
* 例:
* ErasablePriorityQueue<int> pq;
* pq.push(3); pq.push(5); pq.erase(5); pq.top(); // 3
*
* @tparam T 要素の型
* @tparam Compare 比較関数。std::greater<T> にすると最小の要素を取り出す
*/
template <typename T, typename Compare = std::less<T>>
class ErasablePriorityQueue {
std::priority_queue<T, std::vector<T>, Compare> que, del;
Compare comp;
bool equivalent(const T& x, const T& y) const { return !comp(x, y) && !comp(y, x); }
// 先頭が削除予定なら取り除く
void normalize() {
while (!del.empty() && equivalent(que.top(), del.top())) {
que.pop();
del.pop();
}
}
public:
explicit ErasablePriorityQueue(Compare comp = Compare()) : que(comp), del(comp), comp(comp) {}
/**
* @brief 要素数
*/
long long size() const { return que.size() - del.size(); }
bool empty() const { return size() == 0; }
/**
* @brief x を追加する
*/
void push(const T& x) {
que.push(x);
normalize();
}
/**
* @brief x を 1 つ削除する。x は含まれていること (含まれていない要素を削除すると壊れる)
* 比較関数で等しい要素は区別しない。空なら assert で落ちる
*/
void erase(const T& x) {
assert(!empty());
del.push(x);
normalize();
}
/**
* @brief 先頭 (既定では最大) の要素。空なら assert で落ちる
*/
const T& top() const {
assert(!empty());
return que.top();
}
/**
* @brief 先頭の要素を削除する。空なら assert で落ちる
*/
void pop() {
assert(!empty());
que.pop();
normalize();
}
};
#line 7 "test/unittest/data_structures/erasable-priority-queue.test.cpp"
#line 9 "test/unittest/data_structures/erasable-priority-queue.test.cpp"
#include <iostream>
#include <iterator>
#include <random>
#include <set>
#line 14 "test/unittest/data_structures/erasable-priority-queue.test.cpp"
int main() {
std::mt19937 rng(314);
bool ok = true;
for (int rep = 0; rep < 1000 && ok; rep++) {
ErasablePriorityQueue<int> max_q;
ErasablePriorityQueue<int, std::greater<int>> min_q;
std::multiset<int> st;
for (int t = 0; t < 100; t++) {
int op = rng() % 4;
if (op <= 1 || st.empty()) {
int x = rng() % 10;
max_q.push(x);
min_q.push(x);
st.insert(x);
} else if (op == 2) {
// 含まれている要素をランダムに選んで削除する
auto it = std::next(st.begin(), rng() % st.size());
max_q.erase(*it);
min_q.erase(*it);
st.erase(it);
} else {
if (rng() % 2) {
max_q.pop();
min_q.erase(*st.rbegin());
st.erase(std::prev(st.end()));
} else {
min_q.pop();
max_q.erase(*st.begin());
st.erase(st.begin());
}
}
if (max_q.size() != static_cast<int>(st.size()) || min_q.size() != static_cast<int>(st.size())) ok = false;
if (max_q.empty() != st.empty() || min_q.empty() != st.empty()) ok = false;
if (!st.empty() && (max_q.top() != *st.rbegin() || min_q.top() != *st.begin())) ok = false;
}
}
if (ok)
std::cout << "Hello World" << std::endl;
else
std::cerr << "failed" << std::endl;
}