A. Powered Addition

目录

1.Problem

2.Input

3.Output

4.Examples

4.1input

4.2output

5.Code

6.Conclusion


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的值。

具体的实现过程如下:

  1. 通过 readint 函数读取输入的测试用例数量T。

  2. 对于每个测试用例,读取整数n和n个整数的序列a。

  3. 初始化一个辅助数组b,其中b[i]表示序列a的前缀a[1]到a[i]中的最大值。

  4. 对于每个元素a[i],从高位到低位遍历二进制表示,找到(b[i] - a[i])的二进制表示中最高位为1的位的位置,输出这个位置加1的值。

  5. 输出每个测试用例的结果。

总体来说,这段代码用于处理多个测试用例,每个测试用例都要找到满足条件的整数位的位置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向阳而生__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值