結果

問題 No.2126 MEX Game
ユーザー HIcoderHIcoder
提出日時 2023-07-11 11:59:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,831 bytes
コンパイル時間 1,033 ms
コンパイル使用メモリ 104,240 KB
実行使用メモリ 5,724 KB
最終ジャッジ日時 2023-10-11 03:24:07
合計ジャッジ時間 3,628 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,944 KB
testcase_01 AC 4 ms
4,908 KB
testcase_02 AC 4 ms
4,884 KB
testcase_03 AC 4 ms
4,824 KB
testcase_04 AC 4 ms
4,852 KB
testcase_05 AC 4 ms
4,832 KB
testcase_06 AC 4 ms
4,888 KB
testcase_07 WA -
testcase_08 AC 4 ms
4,852 KB
testcase_09 AC 4 ms
4,840 KB
testcase_10 AC 30 ms
5,416 KB
testcase_11 AC 30 ms
5,356 KB
testcase_12 AC 32 ms
5,356 KB
testcase_13 AC 31 ms
5,428 KB
testcase_14 AC 32 ms
5,488 KB
testcase_15 AC 31 ms
5,396 KB
testcase_16 AC 31 ms
5,472 KB
testcase_17 AC 25 ms
5,400 KB
testcase_18 AC 27 ms
5,424 KB
testcase_19 AC 18 ms
5,416 KB
testcase_20 AC 18 ms
5,420 KB
testcase_21 AC 30 ms
5,724 KB
testcase_22 AC 4 ms
4,872 KB
testcase_23 AC 4 ms
4,848 KB
testcase_24 AC 4 ms
5,016 KB
testcase_25 AC 4 ms
5,164 KB
testcase_26 WA -
testcase_27 AC 4 ms
4,836 KB
testcase_28 AC 5 ms
4,888 KB
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<numeric>
#include<queue>
#include<cmath>
using namespace std;
typedef long long ll;
const ll INF=1LL<<60;
typedef pair<int,int> P;
typedef pair<int,P> PP;
const ll MOD=998244353;
const double PI=acos(-1);


template<class Type> struct binary_indexed_tree{
    int N;
    vector<Type> bit;
    
  

    binary_indexed_tree(int n):N(n+1){
        bit = vector<Type>(n+1,0);
        N=n;
    }



    void add(int x,Type a){
        x++;//1から始めるための補正
        //for(int i=x; i<=N; i+=(i&-i)) bit[i] = addition(bit[i],a);
        while(x<=N){
            //bit[x] = addition(bit[x],a);
            bit[x] =bit[x]+a;
            x += x&-x;
        }
    }

    
    Type sum(int x){
        x++;//1から始まることに注意
        Type ret=0;
        //for(int i=x; i>0; i-=(i&-i)) ret = addition(ret,bit[i]);
        while(x>0){
            ret = ret+bit[x];
            x -= x&-x;
        }
        
        return ret;
    }

    //[l,r]の範囲
    // rはN-1以下
    Type get(int l,int r){
        if(r>N) return 0;//配列の外へのアクセス
        if(l>r) return 0;//本来は l<=r となるのでおかしい
        if(l==0) return sum(r);//[0,r]//ここでoutなわけか
        else return (sum(r) - sum(l-1));
    }
};



int main(){
    int N;
    cin>>N;

    const int MAXN=100000+1;
    vector<int> cnt(100000+10,0);
    vector<int> A(N);
    for(int i=0;i<N;i++){
        cin>>A[i];
    }
    for(int i=0;i<N;i++){
        cnt[A[i]]++;
    }

    binary_indexed_tree<int> cnt0(MAXN+1),cnt1(MAXN+1),cnt2(MAXN+1),cntlargerthan2(MAXN+1);

    //vector<int> cnt0(MAXN+1,0),cnt1(MAXN+1,0),cnt2(MAXN+1,0),cntlargerthan2(MAXN+1,0);

    for(int k=0;k<=MAXN;k++){
        if(cnt[k]==0){
            cnt0.add(k,1);
        }else if(cnt[k]==1){
            cnt1.add(k,1);
        }else if(cnt[k]==2){
            cnt2.add(k,1);
        }else if(cnt[k]>2){
            cntlargerthan2.add(k,1);
        }
    }
   

    int ans=0;

   
    for(int k=1;k<=MAXN;k++){
        

        if((cnt2.get(0,k-1)+cntlargerthan2.get(0,k-1))==k){
            //答えはk以上にできる
            ans=k;
            
        }else if(cnt0.get(0,k-1)>0 || cnt1.get(0,k-1)>1){
            //答えはk未満
            ans=k-1;
            break;
        }else{
            //[k,MAXN]
            //int num=(cnt1[MAXN]+cnt2[MAXN]+cntlargerthan2[MAXN])-(cnt1[k-1]+cnt2[k-1]+cntlargerthan2[k-1]);

            int num=cnt1.get(k,MAXN)+cnt2.get(k,MAXN)+cntlargerthan2.get(k,MAXN);
            if(cntlargerthan2.get(0,k-1)>2 || num>=1){
                ans=k;

            }else{
                ans=k-1;
                break;
            }

        }
    }

    cout<<ans<<endl;

}
0