Codeforces 1339C - Powered Addition (位运算)

Description

思路

根据题意,可以转换为在时刻x可以对原数组任意一个数加\([1, 2^{x} - 1]\)的数。只要求出刚好满足非递减序列要加的每个位置需要加的最少值中的最大值所在的最小的x对于的区间即可。

#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;
    }
}

猜你喜欢

转载自www.cnblogs.com/limil/p/12690983.html