0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AtCoder ABC217 E - Sorting Queries 解

Posted at

##問題
https://atcoder.jp/contests/abc217/tasks/abc217_e

##考察
公式解説がこれでもかとばかりに書いているのでそちらを見るべし。データ構造を整理できれば自然と実装に辿り着く。

##実装
auto Q = in<ll>()の右辺は標準入力を取ってきて返すテンプレート関数。

#define rp(i, n) for (int i = 0; i < n; ++i)
#define print(x) std::cout << (x) << std::endl;
int main() {
    // read input
    auto Q = in<ll>();
    // data structure
    queue<ll> back;
    priority_queue<ll, vector<ll>, greater<ll>> head;

    // push all elems of normal queue into priority queue
    // this gets query 3 done
    auto pushall = [&]() {
        while (back.size()) {
            head.push(back.front());
            back.pop();
        }
    };
    // start handling queries
    rp(i, Q) {
        int c = in<int>();
        if (c == 1)
            back.push(in<ll>());
        else if (c == 2) {
            if (head.size()) {
                print(head.top());
                head.pop();
            } else {
                print(back.front());
                back.pop();
            }
        } else {
            pushall();
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?