結果

問題 No.3789 Binary 0-count Sequence
コンテスト
ユーザー GOTKAKO
提出日時 2026-11-06 22:14:56
言語 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
結果
AC  
実行時間 44 ms / 3,000 ms
+ 274µs
コード長 2,898 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,403 ms
コンパイル使用メモリ 221,740 KB
実行使用メモリ 9,900 KB
最終ジャッジ日時 2026-11-06 22:15:01
合計ジャッジ時間 4,208 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge7_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

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

    int T; cin >> T;
    while(T--){
        int N; cin >> N;
        vector<int> A(N);
        for(auto &a : A) cin >> a;
        
        auto check = [&](long long x) -> bool {
            for(int i=0; i<N; i++){
                long long now = 0,v = x+i;
                if(v < 0) return false;
                if(v == 0) now = 1;
                else while(v) now += v%2==0,v >>= 1;
                if(A.at(i) == now) continue;
                return false;
            }
            return true;
        };
        if(check(0)){cout << "0\n"; continue;}
        if(N == 1 || (N == 2 && A.at(1)-A.at(0) == -1)){cout << (1LL<<A.at(0)) << "\n"; continue;}

        vector<int> dec;
        int even = -1;
        {
            bool ok = true;
            for(int i=1; i<N; i+=2){
                if(A.at(i)-A.at(i-1) == -1) continue;
                ok = false; break;
            }
            for(int i=2; i<N; i+=2){
                if(A.at(i)-A.at(i-1) >= 0) continue;
                ok = false; break;
            }
            if(ok) even = 0;
        }
        {
            bool ok = true;
            for(int i=2; i<N; i+=2){
                if(A.at(i)-A.at(i-1) == -1) continue;
                ok = false; break;
            }
            for(int i=1; i<N; i+=2){
                if(A.at(i)-A.at(i-1) >= 0) continue;
                ok = false; break;
            }
            if(ok) even = 1;
        }
        if(even == -1){cout << "-1\n"; continue;}

        if(even == 0){
            int maxd = -1;
            for(int i=2; i<N; i+=2) maxd = max(maxd,A.at(i)-A.at(i-1));
            for(int i=2; i<N; i+=2) if(maxd == A.at(i)-A.at(i-1)){
                long long v = 1LL<<maxd;
                if(check(v-i)){cout << v-i << "\n"; break;}
                if(A.at(i) == maxd){cout << "-1\n"; break;}
                int more = A.at(i)-1-maxd;
                long long d = maxd+1;
                v = 1LL<<d; 
                d += more+1;
                v += 1LL<<d;
                if(check(v-i)) cout << v-i << "\n";
                else cout << "-1\n";
                break;
            }
        }
        else{
            int maxd = -1;
            for(int i=1; i<N; i+=2) maxd = max(maxd,A.at(i)-A.at(i-1));
            for(int i=1; i<N; i+=2) if(maxd == A.at(i)-A.at(i-1)){
                long long v = 1LL<<maxd;
                if(check(v-i)){cout << v-i << "\n"; break;}
                if(A.at(i) == maxd){cout << "-1\n"; break;}
                int more = A.at(i)-1-maxd;
                long long d = maxd+1;
                v = 1LL<<d; 
                d += more+1;
                v += 1LL<<d;
                if(check(v-i)) cout << v-i << "\n";
                else cout << "-1\n";
                break;
            }
        }
    }
}
0