C. Make Equal Again

time limit per test

2 seconds

memory limit per test

256 megabytes

You have an array aa of nn integers.

You can no more than once apply the following operation: select three integers ii, jj, xx (1≤i≤j≤n1≤i≤j≤n) and assign all elements of the array with indexes from ii to jj the value xx. The price of this operation depends on the selected indices and is equal to (j−i+1)(j−i+1) burles.

For example, the array is equal to [1,2,3,4,5,1][1,2,3,4,5,1]. If we choose i=2,j=4,x=8i=2,j=4,x=8, then after applying this operation, the array will be equal to [1,8,8,8,5,1][1,8,8,8,5,1].

What is the least amount of burles you need to spend to make all the elements of the array equal?

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) --- the number of input test cases. The descriptions of the test cases follow.

The first line of the description of each test case contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105) --- the size of the array.

The second line of the description of each test case contains nn integers a1,a2,...,ana1,a2,...,an (1≤ai≤n1≤ai≤n) --- array elements.

It is guaranteed that the sum of nn for all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, output one integer --- the minimum number of burles that will have to be spent to make all the elements of the array equal. It can be shown that this can always be done.

Example

Input

Copy

复制代码

8

6

1 2 3 4 5 1

7

1 1 1 1 1 1 1

8

8 8 8 1 2 8 8 8

1

1

2

1 2

3

1 2 3

7

4 3 2 7 1 1 3

9

9 9 2 9 2 5 5 5 3

Output

Copy

复制代码
4
0
2
0
1
2
6
7

4

解题说明:此题采用贪心算法,首先分别找出头部第一个和前面不同的数字,尾部第一个和后面不同的数字。如果头尾两个数字一样,那只需要把中间不相同的数字全部搞成一样即可。否则就区分情况,要么是把前面数字搞成和尾部数字一样,要么是把后面数字搞成和头部数字一样,两者比较求最小值即可。

cpp 复制代码
#include <stdio.h>

int main()
{
	int t, i, j, n, head, tail;
	scanf("%d", &t);
	for (i = 0; i < t; i++)
	{
		scanf("%d", &n);
		int a[200007];
		for (j = 0; j < n; j++)
		{
			scanf("%d", &a[j]);
		}
		for (j = 1; j < n; j++)
		{
			if (a[j] != a[0])
			{
				break;
			}
		}
		head = j;
		for (j = n - 1; j >= 0; j--)
		{
			if (a[j] != a[n - 1])
			{
				break;
			}
		}
		tail = j;
		if (a[0] == a[n - 1])
		{
			if (head == n)
			{
				printf("0\n");
			}
			else
			{
				printf("%d\n", tail - head + 1);
			}
		}
		else
		{
			if (n - head < tail + 1)
			{
				printf("%d\n", n - head);
			}
			else
			{
				printf("%d\n", tail + 1);
			}
		}
	}
	return 0;
}
相关推荐
Theodore_10222 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
网易独家音乐人Mike Zhou3 小时前
【卡尔曼滤波】数据预测Prediction观测器的理论推导及应用 C语言、Python实现(Kalman Filter)
c语言·python·单片机·物联网·算法·嵌入式·iot
----云烟----4 小时前
QT中QString类的各种使用
开发语言·qt
lsx2024064 小时前
SQL SELECT 语句:基础与进阶应用
开发语言
开心工作室_kaic5 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
向宇it5 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎
武子康5 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
转世成为计算机大神5 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式
搬砖的小码农_Sky5 小时前
C语言:数组
c语言·数据结构
宅小海6 小时前
scala String
大数据·开发语言·scala