結果

問題 No.2667 Constrained Permutation
ユーザー suisensuisen
提出日時 2023-10-29 21:54:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 145 ms / 2,000 ms
コード長 1,224 bytes
コンパイル時間 1,150 ms
コンパイル使用メモリ 109,980 KB
実行使用メモリ 8,184 KB
最終ジャッジ日時 2024-01-15 12:25:06
合計ジャッジ時間 5,201 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 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 2 ms
6,676 KB
testcase_12 AC 43 ms
6,676 KB
testcase_13 AC 3 ms
6,676 KB
testcase_14 AC 126 ms
7,824 KB
testcase_15 AC 78 ms
6,676 KB
testcase_16 AC 53 ms
6,676 KB
testcase_17 AC 18 ms
6,676 KB
testcase_18 AC 140 ms
8,056 KB
testcase_19 AC 123 ms
6,676 KB
testcase_20 AC 135 ms
7,160 KB
testcase_21 AC 92 ms
6,676 KB
testcase_22 AC 97 ms
6,676 KB
testcase_23 AC 95 ms
6,676 KB
testcase_24 AC 56 ms
6,676 KB
testcase_25 AC 110 ms
7,492 KB
testcase_26 AC 136 ms
8,184 KB
testcase_27 AC 145 ms
8,184 KB
testcase_28 AC 60 ms
7,148 KB
testcase_29 AC 60 ms
7,088 KB
testcase_30 AC 64 ms
7,360 KB
testcase_31 AC 39 ms
6,676 KB
testcase_32 AC 9 ms
6,676 KB
testcase_33 AC 83 ms
6,676 KB
testcase_34 AC 43 ms
6,676 KB
testcase_35 AC 53 ms
6,676 KB
testcase_36 AC 48 ms
6,676 KB
testcase_37 AC 56 ms
6,676 KB
testcase_38 AC 80 ms
6,676 KB
testcase_39 AC 2 ms
6,676 KB
testcase_40 AC 42 ms
6,676 KB
testcase_41 AC 67 ms
6,676 KB
testcase_42 AC 71 ms
6,676 KB
testcase_43 AC 103 ms
6,936 KB
testcase_44 AC 43 ms
6,676 KB
testcase_45 AC 3 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

std::optional<int> get_min_k(int n, std::vector<std::pair<int, int>> lr) {
    std::sort(lr.begin(), lr.end());

    int k = 0;
    for (int i = 1; i <= n; ++i) k = std::max(k, lr[i - 1].first - i);

    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 std::nullopt;
        pq.pop();
    }
    return k;
}

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;

    auto min_k = get_min_k(n, lr);
    constexpr int M = 1000000001;
    for (auto& [l, r] : lr) {
        std::tie(l, r) = std::make_tuple(M - r, M - l);
    }
    auto max_k = get_min_k(n, lr);

    if (not min_k) {
        std::cout << 0 << std::endl;
    } else {
        const int kl = *min_k, kr = M - *max_k - (n + 1);
        std::cout << kr - kl + 1 << std::endl;
    }
}
0