結果

問題 No.2667 Constrained Permutation
ユーザー 👑 hitonanodehitonanode
提出日時 2024-03-09 09:53:45
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 435 ms / 2,000 ms
コード長 2,130 bytes
コンパイル時間 6,863 ms
コンパイル使用メモリ 114,052 KB
実行使用メモリ 13,692 KB
最終ジャッジ日時 2024-03-09 09:54:07
合計ジャッジ時間 7,546 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 3 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 3 ms
6,548 KB
testcase_07 AC 1 ms
6,548 KB
testcase_08 AC 3 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 3 ms
6,548 KB
testcase_12 AC 96 ms
6,548 KB
testcase_13 AC 4 ms
6,548 KB
testcase_14 AC 246 ms
12,012 KB
testcase_15 AC 89 ms
11,248 KB
testcase_16 AC 87 ms
8,840 KB
testcase_17 AC 21 ms
6,548 KB
testcase_18 AC 258 ms
13,524 KB
testcase_19 AC 435 ms
13,504 KB
testcase_20 AC 418 ms
13,524 KB
testcase_21 AC 137 ms
13,692 KB
testcase_22 AC 161 ms
13,564 KB
testcase_23 AC 170 ms
13,564 KB
testcase_24 AC 96 ms
7,428 KB
testcase_25 AC 190 ms
11,192 KB
testcase_26 AC 229 ms
12,628 KB
testcase_27 AC 211 ms
12,756 KB
testcase_28 AC 53 ms
7,876 KB
testcase_29 AC 52 ms
7,700 KB
testcase_30 AC 58 ms
8,292 KB
testcase_31 AC 36 ms
6,548 KB
testcase_32 AC 8 ms
6,548 KB
testcase_33 AC 88 ms
13,092 KB
testcase_34 AC 47 ms
8,704 KB
testcase_35 AC 55 ms
9,484 KB
testcase_36 AC 52 ms
9,220 KB
testcase_37 AC 58 ms
9,964 KB
testcase_38 AC 190 ms
10,300 KB
testcase_39 AC 4 ms
6,548 KB
testcase_40 AC 46 ms
8,192 KB
testcase_41 AC 71 ms
11,092 KB
testcase_42 AC 117 ms
8,204 KB
testcase_43 AC 191 ms
10,476 KB
testcase_44 AC 168 ms
6,548 KB
testcase_45 AC 6 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)
template <typename T> bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; }
template <typename T> bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; }
template <class T1, class T2> T1 floor_div(T1 num, T2 den) { return (num > 0 ? num / den : -((-num + den - 1) / den)); }

int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vector<pair<int, int>> LR(N);
    for (auto &[l, r] : LR) cin >> l >> r;

    sort(LR.begin(), LR.end());

    int all_l_max = -1, all_r_min = 1 << 30;
    for (auto [l, r] : LR) {
        chmax(all_l_max, l);
        chmin(all_r_min, r);
    }

    auto find_feasible = [&]() -> int {
        int now = -1;
        for (auto [L, R] : LR) {
            chmax(now, (int)L - 1);
            ++now;
        }
        return now - (N - 1);
    };

    auto check = [&](int mid) -> bool {
        // [mid, mid + 1, ..., mid + N - 1]
        vector<vector<int>> dp(N);

        for (auto [l, r] : LR) {
            int left = max(l, mid) - mid, right = min(r, mid + N - 1) - mid;
            if (left <= right) dp.at(left).push_back(right);
        }

        priority_queue<int, vector<int>, greater<int>> pq;
        REP(i, N) {
            for (auto r : dp.at(i)) pq.push(r);
            while (pq.size() and pq.top() < i) pq.pop();
            if (pq.empty()) return false;
            pq.pop();
        }

        return true;
    };

    auto binsearch = [&](int ok, int ng) -> int {
        while (abs(ok - ng) > 1) {
            const int mid = floor_div(ok + ng, 2);
            (check(mid) ? ok : ng) = mid;
        }
        return ok;
    };

    const int feasible = find_feasible();

    if (!check(feasible)) {
        puts("0");
    } else {
        int lo = binsearch(feasible, all_l_max - N);
        int hi = binsearch(feasible, all_r_min + 1);

        cout << abs(hi - lo) + 1 << '\n';
    }
}
0