結果

問題 No.3780 Erase and Mex
コンテスト
ユーザー ococonomy1
提出日時 2026-10-30 22:00:41
言語 C++17
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 5,690 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,472 ms
コンパイル使用メモリ 228,296 KB
実行使用メモリ 9,924 KB
最終ジャッジ日時 2026-10-30 22:00:55
合計ジャッジ時間 5,686 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 16 WA * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pli = pair<ll,int>;
#define MOD 998244353
//#define MOD 1000000007
#define el '\n'
#define El '\n'
#define YESNO(x) ((x) ? "Yes" : "No")
#define YES YESNO(true)
#define NO YESNO(false)
#define EXIT_ANS(x) {cout << (x) << '\n'; return;}
template <typename T> void inline SORT(T &v){sort(v.begin(),v.end()); return;}
template <typename T> void inline REV(T &v){reverse(v.begin(),v.end()); return;}
template <typename T> void inline VEC_UNIQ(T &v){sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); return;}
template <typename T> auto inline MAX(T &v){return *max_element(v.begin(),v.end());}
template <typename T> auto inline MIN(T &v){return *min_element(v.begin(),v.end());}
template <typename T> auto inline SUM(vector<T> &v){T ans = 0; for(int i = 0; i < (int)v.size(); i++)ans += v[i]; return ans;}
template <typename T> void inline DEC(T &v){for(int i = 0; i < (int)v.size(); i++)v[i]--; return;}
template <typename T> void inline INC(T &v){for(int i = 0; i < (int)v.size(); i++)v[i]++; return;}
void inline TEST(void){cerr << "TEST" << endl; return;}
template <typename T,typename S> bool inline chmin(T &x,S y){
    if(x > (T)y){
        x = (T)y;
        return true;
    }
    return false;
}
template <typename T,typename S> bool inline chmax(T &x,S y){
    if(x < (T)y){
        x = (T)y;
        return true;
    }
    return false;
}
template <typename T = long long> vector<T> inline get_vec(int n){
    vector<T> ans(n);
    for(int i = 0; i < n; i++)cin >> ans[i];
    return ans;
}
template <typename T> void inline print_vec(vector<T> &vec,bool kaigyou = false){
    int n = (int)vec.size();
    for(int i = 0; i < n; i++){
        cout << vec[i];
        if(kaigyou || i == n - 1)cout << '\n';
        else cout << ' ';
    }
    if(!n)cout << '\n';
    return;
}
template <typename T> void inline debug_vec(vector<T> &vec,bool kaigyou = false){
    int n = (int)vec.size();
    for(int i = 0; i < n; i++){
        cerr << vec[i];
        if(kaigyou || i == n - 1)cerr << '\n';
        else cerr << ' ';
    }
    if(!n)cerr << '\n';
    return;
}
vector<vector<int>> inline get_graph(int n,int m = -1,bool direct = false,bool decrement = true){
    if(m == -1)m = n - 1;
    vector<vector<int>> g(n);
    while(m--){
        int u,v;
        cin >> u >> v;
        if(decrement)u--,v--;
        g[u].push_back(v);
        if(!direct)g[v].push_back(u);
    }
    return g;
}
template <typename T> vector<vector<pair<T,int>>> inline get_weighted_graph(int n,int m = -1,bool direct = false,bool decrement = true){
    if(m == -1)m = n - 1;
    vector<vector<pair<T,int>>> g(n);
    while(m--){
        int u,v;
        cin >> u >> v;
        if(decrement)u--,v--;
        ll w; cin >> w;
        g[u].push_back(pair(w,v));
        if(!direct)g[v].push_back(pair(w,u));
    }
    return g;    
}

bool func(vector<ll> a,ll x){
    if(x <= 1)return true;
    int n = (int)a.size();
    //[0,x-1] の数を全て作ることはできるか?
    //k を作るのには 2^(k-1) 個の数が必要になる
    //N の大きさ的に、20以上の数を作りたい場合、元の値を活かすしかない
    //例えば、(大量の1),19,20,21,23 が A だったとして、22は作れるが、24は作れない
    //↑もっと言うと、ここに[1,18]が都合よくあったとしても、(2e5個程度では)24は作れない

    //[0,x-1] を全部作る必要がある
    vector<bool> vb(x,false);

    vector<ll> b;
    vector<int> cnt(21);
    for(int i = 0; i < n; i++){
        if(a[i] < x && !vb[a[i]]){
            vb[a[i]] = true;
            continue;
        }
        if(a[i] >= 20 || a[i] >= x)cnt[1]++;
        else cnt[max(a[i],1LL)]++;
    }
    int tempcnt = 0;
    for(int i = 20; i < x; i++){
        if(!vb[i])return false;
    }
    
    ll val = 0;
    for(int i = 0; i < 20 && i < x; i++){
        if(vb[i]){
            if(i + 1 < cnt.size()){
                cnt[i + 1] += cnt[i] / 2;
            }
            continue;
        }
        if(cnt[i] == 0){
            for(int j = i + 1; j < 20 && j < x; j++){
                if(cnt[j] >= 1){
                    cnt[j]--;
                    cnt[i]++;
                    break;
                }
            }
            if(cnt[i] == 0)return false;
        }
        ll dif = cnt[i] - 1;
        if(i + 1 < cnt.size())cnt[i + 1] += dif / 2;
    }
    return true;
}

#define MULTI_TEST_CASE true
void solve(void){
    //問題を見たらまず「この問題設定から言えること」をいっぱい言う
    //よりシンプルな問題に言い換えられたら、言い換えた先の問題を自然言語ではっきりと書く
    //複数の解法のアイデアを思いついた時は全部メモしておく
    //g++ -D_GLIBCXX_DEBUG -Wall -O2 b.cpp -o o
    
    int n;
    cin >> n;
    vector<ll> a = get_vec(n);
    SORT(a);
    //一個目の操作で全部を選んだら A 全体の mex がスコアになる
    //x 以上にすることはできるか?に単調性があるからにぶたんができそう
    ll ok = 0,ng = n + 2;
    while(ng - ok >= 2){
        ll mid = (ok + ng) / 2;
        if(func(a,mid))ok = mid;
        else ng = mid;
    }
    cout << ok << el;

    return;
}

void calc(void){
    return;
}

signed main(void){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    calc();
    int t = 1;
    if(MULTI_TEST_CASE)cin >> t;
    while(t--){
        solve();
    }
    return 0;
}
0