結果

問題 No.2667 Constrained Permutation
ユーザー suisensuisen
提出日時 2023-10-29 21:54:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 514 ms / 2,000 ms
コード長 1,405 bytes
コンパイル時間 1,240 ms
コンパイル使用メモリ 107,804 KB
実行使用メモリ 7,576 KB
最終ジャッジ日時 2024-01-15 12:25:06
合計ジャッジ時間 7,737 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 3 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 3 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 3 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 3 ms
6,676 KB
testcase_12 AC 138 ms
6,676 KB
testcase_13 AC 5 ms
6,676 KB
testcase_14 AC 347 ms
7,368 KB
testcase_15 AC 91 ms
6,676 KB
testcase_16 AC 65 ms
6,676 KB
testcase_17 AC 24 ms
6,676 KB
testcase_18 AC 403 ms
7,576 KB
testcase_19 AC 246 ms
6,676 KB
testcase_20 AC 297 ms
7,040 KB
testcase_21 AC 116 ms
6,676 KB
testcase_22 AC 125 ms
6,676 KB
testcase_23 AC 122 ms
6,676 KB
testcase_24 AC 167 ms
6,676 KB
testcase_25 AC 396 ms
7,008 KB
testcase_26 AC 514 ms
7,576 KB
testcase_27 AC 408 ms
7,576 KB
testcase_28 AC 54 ms
6,784 KB
testcase_29 AC 57 ms
6,676 KB
testcase_30 AC 58 ms
6,952 KB
testcase_31 AC 35 ms
6,676 KB
testcase_32 AC 9 ms
6,676 KB
testcase_33 AC 74 ms
6,676 KB
testcase_34 AC 40 ms
6,676 KB
testcase_35 AC 47 ms
6,676 KB
testcase_36 AC 45 ms
6,676 KB
testcase_37 AC 48 ms
6,676 KB
testcase_38 AC 99 ms
6,676 KB
testcase_39 AC 3 ms
6,676 KB
testcase_40 AC 39 ms
6,676 KB
testcase_41 AC 63 ms
6,676 KB
testcase_42 AC 164 ms
6,676 KB
testcase_43 AC 219 ms
6,936 KB
testcase_44 AC 179 ms
6,676 KB
testcase_45 AC 7 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <optional>
#include <queue>
#include <vector>

bool solve(int n, const std::vector<std::pair<int, int>>& lr, int k) {
    assert(std::is_sorted(lr.begin(), lr.end()));
    std::priority_queue<int, std::vector<int>, std::greater<int>> pq;
    for (int v = k + 1, j = 0; v <= k + n; ++v) {
        while (j < n and lr[j].first <= v) pq.push(lr[j++].second);
        assert(pq.size());
        if (pq.top() < v) return false;
        pq.pop();
    }
    return true;
}

std::optional<int> get_min_k(int n, std::vector<std::pair<int, int>> lr) {
    assert(std::is_sorted(lr.begin(), lr.end()));
    int k = 0;
    for (int i = 1; i <= n; ++i) k = std::max(k, lr[i - 1].first - i);
    return solve(n, lr, k) ? std::make_optional(k) : std::nullopt;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n;
    std::cin >> n;

    std::vector<std::pair<int, int>> lr(n);
    for (auto& [l, r] : lr) std::cin >> l >> r;
    std::sort(lr.begin(), lr.end());

    auto min_k = get_min_k(n, lr);
    if (not min_k) {
        std::cout << 0 << std::endl;
    } else {
        int ok = *min_k, ng = 1000000000;
        while (ng - ok > 1) {
            int k = (ok + ng) / 2;
            (solve(n, lr, k) ? ok : ng) = k;
        }
        std::cout << ok - *min_k + 1 << std::endl;
    }
}
0