Codeforces Round #633 (Div. 2) C.Powered Addition
题目链接
You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second:
- Select some distinct indices i1,i2,…,ik which are between 1 and n inclusive, and add 2x−1 to each corresponding position of a. Formally, aij:=aij+2x−1 for j=1,2,…,k. Note that you are allowed to not select any indices at all.
You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds.
Array a is nondecreasing if and only if a1≤a2≤…≤an.
You have to answer t independent test cases.
Input
The first line contains a single integer t (1≤t≤1e4) — the number of test cases.
The first line of each test case contains single integer n (1≤n≤1e5) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 105.
The second line of each test case contains n integers a1,a2,…,an (−109≤ai≤109).
Output
For each test case, print the minimum number of seconds in which you can make a nondecreasing.
Example
input
3
4
1 7 6 5
5
1 2 3 4 5
2
0 -4
output
2
0
3
遍历记录当前位置 i i i 之前的最大值 m a x max max,若该数 a i < m a x a_i<max ai<max,则需要改变的最小次数即为 m a x − a i max-a_i max−ai 二进制位上1的最大位置,更新答案 a n s ans ans 即可,AC代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;int main(){int t;cin>>t;while(t--){int n;cin>>n;int a[n],mx=-1e9-5,ans=0,cnt=0;for(int i=0;i<n;i++) cin>>a[i];for(int i=0;i<n;i++){if(a[i]<mx){for(int j=31;j>=0;j--){if((1<<j)&(mx-a[i])){ans=max(ans,j+1);break;}}}mx=max(a[i],mx);}cout<<ans<<endl;}return 0;
}