結果

問題 No.3075 Mex Recurrence Formula
ユーザー haihamabossu
提出日時 2025-03-28 22:52:37
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 630 ms / 2,000 ms
コード長 987 bytes
コンパイル時間 2,348 ms
コンパイル使用メモリ 209,784 KB
実行使用メモリ 40,692 KB
最終ジャッジ日時 2025-03-28 22:52:56
合計ジャッジ時間 15,793 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

vector<ll> nex(const vector<ll> &pre) {
  ll n = pre.size();
  set<ll> st;
  rep(i, n + 10) st.insert(i);
  map<ll, ll> mp;
  auto add = [&](ll v) {
    if (++mp[v] == 1 && st.count(v) == 1)
      st.erase(v);
  };
  auto del = [&](ll v) {
    if (--mp[v] == 0)
      st.insert(v);
  };
  rep(i, n) add(pre[i]);
  vector<ll> res;
  rep(i, n) {
    ll now = *st.begin();
    res.push_back(now);
    add(now);
    del(pre[i]);
  }
  ll now = *st.begin();
  res.push_back(now);
  return res;
}

void solve() {
  ll n, x;
  cin >> n >> x, x--;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  if (x < n) {
    cout << a[x] << '\n';
    return;
  }
  auto b = nex(a);
  x -= n;
  x %= n + 1;
  cout << b[x] << '\n';
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  int T = 1;
  for (int t = 0; t < T; t++) {
    solve();
  }
  return 0;
}
0