結果

問題 No.2930 Larger Mex
ユーザー srjywrdnprkt
提出日時 2024-10-15 01:56:30
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,027 ms / 2,000 ms
コード長 1,277 bytes
コンパイル時間 2,252 ms
コンパイル使用メモリ 207,812 KB
最終ジャッジ日時 2025-02-24 19:44:13
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

    /*
       左端を固定したとき、mexは単調増加
       [l,r]でちょうどmexがM以上になるなら
       r-l+1, N-l+1の全ての長さが条件を満たす。
       尺取り法で左端の数字を見つけるまでインクリメント
    */

    ll N, M, r=0;
    cin >> N >> M;
    vector<ll> A(N), S(N+3);
    for (int i=0; i<N; i++) cin >> A[i];
    if (M == 0){
        for (int i=0; i<N; i++){
            cout << N-i << endl;
        }
        return 0;
    }
    map<ll, ll> mp;
    set<ll> st;
    for (int i=0; i<M; i++) st.insert(i);
    for (int l=0; l<N; l++){
        while(r<N && !st.empty()){
            if (A[r] < M){
                mp[A[r]]++;
                if (mp[A[r]] == 1){
                    st.erase(A[r]);
                }
            }
            r++;
        }
        if (st.empty()){
            S[N-l+1] -= 1;
            S[r-l] += 1;
        }
        if (A[l] < M){
            mp[A[l]]--;
            if (mp[A[l]] == 0) st.insert(A[l]);
        }
    }
    for (int i=1; i<=N+2; i++) S[i] += S[i-1];
    for (int i=1; i<=N; i++) cout << S[i] << endl;

    return 0;
}
0