結果

問題 No.3780 Erase and Mex
コンテスト
ユーザー askr58
提出日時 2026-07-26 19:38:50
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 21 ms / 2,000 ms
+ 205µs
コード長 2,168 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,514 ms
コンパイル使用メモリ 337,360 KB
実行使用メモリ 9,904 KB
最終ジャッジ日時 2026-10-30 20:53:26
合計ジャッジ時間 6,437 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

//generated by ChatGPT
#include <bits/stdc++.h>
using namespace std;

using int64 = long long;

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

    int T;
    cin >> T;

    while (T--) {
        int N;
        cin >> N;

        vector<int64> count(N + 1, 0);
        int64 firstValue = 0;

        for (int i = 0; i < N; ++i) {
            int64 a;
            cin >> a;

            if (i == 0) {
                firstValue = a;
            }

            // N >= 2 の場合、答えは N 以下なので、
            // N より大きい値の正確な個数は必要ない。
            if (a <= N) {
                ++count[static_cast<int>(a)];
            }
        }

        if (N == 1) {
            cout << max<int64>(firstValue, 1) << '\n';
            continue;
        }

        // 値 k を作れるか判定する。
        auto feasible = [&](int k) -> bool {
            // suffixDeficit = sum_{y=x+1}^{k-1} d_y
            int64 suffixDeficit = 0;

            // 初期要求 0, 1, ..., k-1 の個数
            int64 requiredLeaves = k;

            for (int x = k - 1; x >= 2; --x) {
                // 最終操作用の1個と、
                // より大きい値の合成から発生した要求
                int64 requiredX = 1 + suffixDeficit;

                // 初期値 x で足りない分は合成する必要がある
                int64 deficit = max<int64>(0, requiredX - count[x]);

                // x の要求を 0,...,x-1 に展開すると x-1 個増える
                requiredLeaves += deficit * (x - 1LL);

                if (requiredLeaves > N) {
                    return false;
                }

                suffixDeficit += deficit;
            }

            return true;
        };

        // 2 は必ず作れる。
        int low = 2;

        // N + 1 は作れない。
        int high = N + 1;

        while (high - low > 1) {
            int mid = low + (high - low) / 2;

            if (feasible(mid)) {
                low = mid;
            } else {
                high = mid;
            }
        }

        cout << low << '\n';
    }

    return 0;
}
0