Sequence Sorting CodeForces - 1223D(思维)

You are given a sequence a1,a2,…,ana1,a2,…,an, consisting of integers.

You can apply the following operation to this sequence: choose some integer xx and move all elements equal to xx either to the beginning, or to the end of aa. Note that you have to move all these elements in one direction in one operation.

For example, if a=[2,1,3,1,1,3,2]a=[2,1,3,1,1,3,2], you can get the following sequences in one operation (for convenience, denote elements equal to xx as xx-elements):

[1,1,1,2,3,3,2][1,1,1,2,3,3,2] if you move all 11-elements to the beginning;
[2,3,3,2,1,1,1][2,3,3,2,1,1,1] if you move all 11-elements to the end;
[2,2,1,3,1,1,3][2,2,1,3,1,1,3] if you move all 22-elements to the beginning;
[1,3,1,1,3,2,2][1,3,1,1,3,2,2] if you move all 22-elements to the end;
[3,3,2,1,1,1,2][3,3,2,1,1,1,2] if you move all 33-elements to the beginning;
[2,1,1,1,2,3,3][2,1,1,1,2,3,3] if you move all 33-elements to the end;
You have to determine the minimum number of such operations so that the sequence aa becomes sorted in non-descending order. Non-descending order means that for all ii from 22 to nn, the condition ai−1≤aiai−1≤ai is satisfied.

Note that you have to answer qq independent queries.

Input
The first line contains one integer qq (1≤q≤3⋅1051≤q≤3⋅105) — the number of the queries. Each query is represented by two consecutive lines.

The first line of each query contains one integer nn (1≤n≤3⋅1051≤n≤3⋅105) — the number of elements.

The second line of each query contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n) — the elements.

It is guaranteed that the sum of all nn does not exceed 3⋅1053⋅105.

Output
For each query print one integer — the minimum number of operation for sorting sequence aa in non-descending order.

Example
Input
3
7
3 1 6 6 3 1 1
8
1 1 4 4 4 7 8 8
7
4 2 5 2 6 2 7
Output
2
0
1
Note
In the first query, you can move all 11-elements to the beginning (after that sequence turn into [1,1,1,3,6,6,3][1,1,1,3,6,6,3]) and then move all 66-elements to the end.

In the second query, the sequence is sorted initially, so the answer is zero.
In the third query, you have to move all 22-elements to the beginning.
题意:可以将一种数字移到最左端或者最右端,问最少可以移动多少次。
思路:我们可以确定下来一些数不需要移动,用数的总数减去不需要移动的就是需要移动的。
对于每个数,我们将它最初出现的位置和最后出现的位置记录下来。然后遍历1~n(因为a[i]是属于1 ~n的)。具体解释看代码。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=3e5+100;
int a[maxx],b[maxx];
int l[maxx],r[maxx];
int n;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++) scanf("%d",&a[i]),l[i]=r[i]=0;
		for(int i=1;i<=n;i++)
		{
			if(!l[a[i]])
			{
				l[a[i]]=i;
			}
			r[a[i]]=i;
		}
		int sum=0;
		int cnt=0;
		int R=0;
		int _max=0;
		for(int i=1;i<=n;i++)
		{
			if(l[i])
			{
				++sum;
				if(R<l[i]) cnt++;//这代表这个数第一次出现的位置在上个数最后出现的位置后面,就说明这两个数是整体递增的,就可以固定这两个数不动。
				else cnt=1;//如果上条语句不成立,就将当前值看作是不移动的那一个
				_max=max(_max,cnt);//更新最大值
				R=r[i];//更新右端点
			}
		}
		cout<<sum-_max<<endl;
	}
}

努力加油a啊,(o)/~

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

starlet_kiss

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

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

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

打赏作者

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

抵扣说明:

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

余额充值