B. 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≤l≤l+2⋅k−1≤n1≤l≤l+2⋅k−1≤n, k≥1k≥1) and for each ii between 00 and k−1k−1 (inclusive), assign the value al+k+ial+k+i to al+ial+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≤t≤2⋅1041≤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≤n≤2⋅1051≤n≤2⋅105) — the length of the array.

The second line of each test case consists of nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤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⋅1052⋅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.

Example

input

Copy

5
3
1 1 1
2
2 1
5
4 4 4 2 4
4
4 2 1 3
1
1

output

Copy

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 a1:=a2a1:=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 a4:=a5a4:=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 a3:=a4a3:=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 a1:=a3a1:=a3, a2:=a4a2:=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.

#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm> 
#include<cstring>
using namespace std;
inline int read(){
	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<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	return x*f;
}
const int N=2e5+10;
int a[N];
int main(){
	int t;
	t=read();
	while(t--){
		int n;
		n=read();
		for(int i=1;i<=n;++i){
			a[i]=read();
		}
		int cnt=0;
		int num=n;
		for(int i=n;i>0;--i){
			if(a[i]==a[n]){
				++cnt;--num;
			}else break;
		}		
		if(num==0){
			printf("0\n");
			continue;
		}
		int flag=0;
		for(int i=1;i<=n;++i){
		    if(a[i]==a[n])flag++;
			else break;	
		}
		int ans=0;
		while(num-flag>0){
			ans++;
			num-=cnt;
			cnt*=2;
			if(num<=0)break;
			if(num-flag<=0)break;
			while(a[num]==a[n]){
				++cnt;
				--num;
			}
			if(num<=0)break;
		}
		printf("%d\n",ans);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: “fun with even subarrays” 的意思是“玩转偶数子数组”,其中“偶数子数组”指的是数组中元素个数为偶数的子数组。这个话题可能涉及到数组的遍历、子数组的生成、偶数个元素的判断等等。具体的实现方法和应用场景需要根据具体情况来确定。 ### 回答2: "fun with even subarrays" 可以理解为与偶数子数组有趣的互动。 一个子数组是指原数组中连续的一部分元素组成的数组,而偶数子数组则是指子数组中包含偶数个元素的子数组。 与偶数子数组的互动可以通过以下方式实现: 1. 统计偶数子数组的个数:遍历原数组,判断每个子数组的长度是否为偶数,并计数出现的次数。可以使用一个计数器来记录偶数子数组的出现次数。 2. 打印偶数子数组:遍历原数组,并使用嵌套循环来找到所有的偶数子数组。对于每个子数组,判断其长度是否为偶数,如果是则打印该子数组。这样可以将所有的偶数子数组输出到控制台或其他地方,以供查看和分析。 3. 操作偶数子数组:根据偶数子数组的位置和元素,可以进行各种操作。例如,可以对偶数子数组中的元素进行排序、求和、求平均值等计算,也可以将偶数子数组的元素进行修改、替换等操作。这样可以深入了解和探索偶数子数组的特性和性质。 4. 寻找特定结构的偶数子数组:可以在原数组中寻找特定结构的偶数子数组,例如找到长度为4的偶数子数组,或者找到和为特定值的偶数子数组等。这样可以在偶数子数组中发现规律和模式,并进行进一步的研究和分析。 总之,“fun with even subarrays”是一种有趣的互动方式,可以通过统计、打印、操作和寻找特定结构的偶数子数组来深入理解和探索原数组中偶数子数组的性质和特点。 ### 回答3: 趣味性是人们在日常生活中追求的一种心理满足。而对于连续子数组这样的概念,我们可以通过一些有趣的方法进行探索。 首先,我们可以通过游戏的方式来增加连续子数组的趣味性。比如,可以设计一个游戏规则,要求玩家在一个给定的数组中找到所有的偶数子数组。玩家需要快速地遍历数组,并标记出所有满足条件的子数组。这样一来,寻找偶数子数组就变成了一个有趣的挑战,增加了游戏的趣味性。 此外,我们还可以通过一些算法和技巧来解决连续子数组的问题,从而使之更加富有趣味性。比如,可以使用动态规划算法来求解最长的连续偶数子数组,或者设计一种巧妙的数据结构来快速找到所有的偶数子数组。这样一来,我们不仅可以提升连续子数组问题的解决效率,还可以增加问题本身的趣味性。 最后,我们还可以将连续子数组的概念与其他有趣的问题进行结合,从而创造出更多有趣的问题。比如,我们可以设计一个题目,要求在一个数组中找到和最大的偶数子数组;或者设计一个游戏,要求玩家通过调整数组中的数字,使得所有的子数组都是偶数。这样一来,连续子数组的概念就与其他问题产生了有趣的结合,使得问题解决过程更加有趣。 总之,通过游戏化、算法优化以及与其他问题的结合,我们可以使连续子数组问题更加有趣。无论是解决问题的过程,还是问题本身的设计,都可以通过一些巧妙的思路来提升趣味性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值