結果

問題 No.3075 Mex Recurrence Formula
コンテスト
ユーザー srjywrdnprkt
提出日時 2025-03-28 22:27:52
言語 C++23
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 998 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,118 ms
コンパイル使用メモリ 347,352 KB
実行使用メモリ 16,000 KB
最終ジャッジ日時 2026-07-08 01:33:45
合計ジャッジ時間 5,409 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
//#include <atcoder/modint>

using namespace std;
//using namespace atcoder;
using ll = long long;
//using mint = modint998244353;

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

    /*
       実験してみたらN+1の周期を持ってそう。
    */

    ll N, X;
    cin >> N >> X;
    deque<ll> v(N);
    for (int i=0; i<N; i++) cin >> v[i];

    if (X <= N){
        cout << v[X-1] << endl;
        return 0;
    }

    X = (X-N-1) % (N+1) + 1;
    set<ll> st;
    vector<ll> cnt(N+2);
    for (int i=0; i<N; i++) if (v[i] <= N+1) cnt[v[i]]++;

    for (int i=0; i<=N+1; i++) if (cnt[i] == 0) st.insert(i);

    ll ans = -1;
    for (int i=1; i<=X; i++){
        ans = *st.begin();
        if (v[0] <= N+1) cnt[v[0]]--;
        if (v[0] <= N+1 && cnt[v[0]] == 0) st.insert(v[0]);
        cnt[ans]++;
        if (cnt[ans] == 1) st.erase(ans);
        v.pop_front();
        v.push_back(ans);
    }
    cout << ans << endl;

    return 0;
}
0