A - Trailing Zeros

A - Trailing Zeros

問題文の通り、素直にbit演算をお行います。 Tiが現在の最大値を超える場合は、

 ans = (1LL << t);

Tiが現在の最大値を超えない場合は、 tの数だけ右へシフトします。 その後、1を加算します。 1bit目は1である必要があるので、1bit目が0ならさらにもう1を加算します。 tの数だけ左へシフトします。

            ans = ans >> t;
            ans++;
            if(!(ans & 1)) ans++;
            ans = ans << t;

コーディングします。

#include <bits/stdc++.h>
 
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
 
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }

int main() {
    int N;
    cin >> N;
    vector<ll> A(N);
    for(int i=0; i<N; i++) cin >> A[i];

    ll ans=-1;
    for(int i=0; i<N; i++){
        ll t = A[i];
        ll value = (1LL << t);
        if(value>ans){
            ans = value;
        }else{
            ans = ans >> t;
            ans++;
            if(!(ans & 1)) ans++;
            ans = ans << t;
        }
    }
    cout << ans << endl;

    return 0;
}