目录
1.Problem
You have an array aa of length nn. For every positive integer xx you are going to perform the following operation during the xx-th second:
- Select some distinct indices i1,i2,…,iki1,i2,…,ik which are between 11 and nn inclusive, and add 2x−12x−1 to each corresponding position of aa. Formally, aij:=aij+2x−1aij:=aij+2x−1 for j=1,2,…,kj=1,2,…,k. Note that you are allowed to not select any indices at all.
You have to make aa nondecreasing as fast as possible. Find the smallest number TT such that you can make the array nondecreasing after at most TT seconds.
Array aa is nondecreasing if and only if a1≤a2≤…≤ana1≤a2≤…≤an.
You have to answer tt independent test cases.
2.Input
The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.
The first line of each test case contains single integer nn (1≤n≤1051≤n≤105) — the length of array aa. It is guaranteed that the sum of values of nn over all test cases in the input does not exceed 105105.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109).
3.Output
For each test case, print the minimum number of seconds in which you can make aa nondecreasing.
4.Examples
4.1input
3
4
1 7 6 5
5
1 2 3 4 5
2
0 -4
4.2output
2
0
3
5.Code
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename T> bool chkmax(T &x, T y) { return x < y ? x = y, true : false; }
template <typename T> bool chkmin(T &x, T y) { return x > y ? x = y, true : false; }
int readint() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int main() {
int T = readint();
while (T--) {
int n = readint();
vector<ll> a(n + 1), b(n + 1, -(1LL << 30));
for (int i = 1; i <= n; i++) a[i] = readint();
for (int i = 1; i <= n; i++) b[i] = max(b[i - 1], a[i]);
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 30; j >= 0; j--) {
if ((b[i] - a[i]) & (1LL << j)) chkmax(ans, j + 1);
}
}
printf("%d\n", ans);
}
return 0;
}
6.Conclusion
这段代码的主要作用是解决一个特定的问题,该问题的描述如下:
对于给定的测试用例数量T,每个测试用例包含一个整数n和n个整数的序列。对于序列中的每个元素a[i],都计算一个值b[i],其中b[i]是序列前缀a[1]到a[i]中的最大值。然后,对于每个元素a[i],找到使得(b[i] - a[i])的二进制表示中最高位为1的位的位置(从右向左数的位置,最低位为0),输出这个位置加1的值。
具体的实现过程如下:
通过
readint
函数读取输入的测试用例数量T。对于每个测试用例,读取整数n和n个整数的序列a。
初始化一个辅助数组b,其中b[i]表示序列a的前缀a[1]到a[i]中的最大值。
对于每个元素a[i],从高位到低位遍历二进制表示,找到(b[i] - a[i])的二进制表示中最高位为1的位的位置,输出这个位置加1的值。
输出每个测试用例的结果。
总体来说,这段代码用于处理多个测试用例,每个测试用例都要找到满足条件的整数位的位置。