Codeforces 1339C-Powered Addition (bit operation)

Description

Ideas

According to the title, it can be converted to a number that can add \ ([1, 2 ^ {x}-1] \) to any number in the original array at time x . It is only required to find the interval of the minimum x for the maximum value of the minimum value that needs to be added for each position to be added to the non-decreasing sequence.

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 1e5 + 10;
ll arr[N];
 
int wid(ll x) {
    int res = 0;
    while(x) {
        res++;
        x = x >> 1;
    }
    return res;
}
 
int main() {
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--) {
        vector<ll> ans;
        int n;
        cin >> n;
        for(int i = 0; i < n; i++) {
            cin >> arr[i];
        }
        int mx = arr[0];
        ll mxa = 0;
        for(int i = 1; i < n; i++) {
            if(arr[i] > mx) mx = arr[i];
            else mxa = max(mxa, mx - arr[i]);
        }
        cout << wid(mxa) << endl;
    }
}

Guess you like

Origin www.cnblogs.com/limil/p/12690983.html