Fun with Even Subarrays

You are given an array aa of nn elements. You can apply the following operation to it any number of times:

  • Select some subarray from aa of even size 2k2k that begins at position ll (1\le l \le l+2\cdot{k}-1\le n1≤l≤l+2⋅k−1≤n, k \ge 1k≥1) and for each ii between 00 and k-1k−1 (inclusive), assign the value a_{l+k+i}al+k+i​ to a_{l+i}al+i​.

For example, if a = [2, 1, 3, 4, 5, 3]a=[2,1,3,4,5,3], then choose l = 1l=1 and k = 2k=2, applying this operation the array will become a = [3, 4, 3, 4, 5, 3]a=[3,4,3,4,5,3].

Find the minimum number of operations (possibly zero) needed to make all the elements of the array equal.

Input

The input consists of multiple test cases. The first line contains a single integer tt (1 \leq t \leq 2 \cdot 10^41≤t≤2⋅104) — the number of test cases. Description of the test cases follows.

The first line of each test case contains an integer nn (1 \leq n \leq 2 \cdot 10^51≤n≤2⋅105) — the length of the array.

The second line of each test case consists of nn integers a_1, a_2, \dots, a_na1​,a2​,…,an​ (1 \leq a_i \leq n1≤ai​≤n) — the elements of the array aa.

It is guaranteed that the sum of nn over all test cases does not exceed 2 \cdot 10^52⋅105.

Output

Print tt lines, each line containing the answer to the corresponding test case — the minimum number of operations needed to make equal all the elements of the array with the given operation.

Sample 1

InputcopyOutputcopy
5
3
1 1 1
2
2 1
5
4 4 4 2 4
4
4 2 1 3
1
1
0
1
1
2
0

Note

In the first test, all elements are equal, therefore no operations are needed.

In the second test, you can apply one operation with k=1k=1 and l=1l=1, set a_1 := a_2a1​:=a2​, and the array becomes [1, 1][1,1] with 11 operation.

In the third test, you can apply one operation with k=1k=1 and l=4l=4, set a_4 := a_5a4​:=a5​, and the array becomes [4, 4, 4, 4, 4][4,4,4,4,4].

In the fourth test, you can apply one operation with k=1k=1 and l=3l=3, set a_3 := a_4a3​:=a4​, and the array becomes [4, 2, 3, 3][4,2,3,3], then you can apply another operation with k=2k=2 and l=1l=1, set a_1 := a_3a1​:=a3​, a_2 := a_4a2​:=a4​, and the array becomes [3, 3, 3, 3][3,3,3,3].

In the fifth test, there is only one element, therefore no operations are needed.

题意:给你一个数组,从从最后一个数开始,把最后一个的值赋给倒数第二个,再把倒数前两个的值付给倒数三四,如果最后比如两个相等,直接把他俩给三四,3个的话给456。例:334656需要2步而不是三步。

5->6

334->666 

说实话这题不难理解,实现其实也不难。我就是当时那个思路搞了五六个小时,把自己弄得很疲惫。最后找的大佬解法。我改到最后应该也快改对了,但是心态真的炸了,没啊那么多时间。

思路:

从后往前,while循环找与最后一个数相同的,遇到小串就+1,知道覆盖整个数组。

#include<iostream>
using namespace std;
int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int n, a[200010];
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
            scanf("%d", &a[i]);
        int ed = n - 1;//尾
        int val = a[n - 1];//最后一个数的值
        int d = 1,cnt=0;
        while (1)
        {
            while (ed - d >= 0 && val == a[ed - d])//当前小串不能完全包含数组或者恰好
                d++;
            if (ed - d < 0)
                break;
            d *= 2;
            cnt++;

        }
        printf("%d\n", cnt);
    }
    

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值