algo — C++23 日本語リファレンス GitHub

test/aoj/challenge/2208.test.cpp

AC · 最終更新 2026-07-24 · GitHub

問題: https://onlinejudge.u-aizu.ac.jp/problems/2208

依存するヘッダ(1)
実装
// competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/2208
#include <iostream>
#include <numeric>
#include <vector>
#include "heap/interval_heap.hpp"

int main(void) {
    while (true) {
        int n;
        std::cin >> n;
        if (!n) break;
        std::vector<int> a(n), b(n);
        for (int &e : a) std::cin >> e;
        for (int &e : b) std::cin >> e;
        if (std::accumulate(a.begin(), a.end(), 0l) != std::accumulate(b.begin(), b.end(), 0l)) {
            std::cout << "No\n";
            continue;
        }
        int h_min = 0, h_max = n, w_min = 0, w_max = n;
        interval_heap<int> hx, hy;
        for (int e : a) hx.emplace(e);
        for (int e : b) hy.emplace(e);
        bool loop = true;
        while (loop) {
            loop = false;
            if (!hx.empty()) {
                if (hx.get_min() == h_min) {
                    hx.pop_min();
                    --w_max;
                    loop = true;
                } else if (hx.get_max() == h_max) {
                    hx.pop_max();
                    ++w_min;
                    loop = true;
                }
            }
            if (!hy.empty()) {
                if (hy.get_min() == w_min) {
                    hy.pop_min();
                    --h_max;
                    loop = true;
                } else if (hy.get_max() == w_max) {
                    hy.pop_max();
                    ++h_min;
                    loop = true;
                }
            }
        }
        std::cout << (hx.empty() && hy.empty() ? "Yes" : "No") << '\n';
    }

    return 0;
}