【思维】D. Sequence Sorting

D. Sequence Sorting

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Copy

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

Copy

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.


思路:此类题的套路基本是先找不动的数,用总数减去不动的就是答案

我们可以先记录每个数的最小位置以及最大位置,我们要找的就是最长的连续不相交区间个数

证明:为什么要连续

我们这么考虑,如果不连续的话,那几个数中间必定至少有一个数需要从别的地方调进来,那要么把所有比他小的数放在他前面,要么把所有比他大的数放在他后面,则这几个不动的数会动,矛盾。


include 
#define endl '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
//typedef __int128 bll;
const int maxn = 3e5 + 100;
const int mod = 1e9+7;
const ll inf = 1e18;

ll n,arr[maxn];

struct node
{
	ll l,r;
}dis[maxn];

vectorV;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	
	ll T;
	cin >> T;
	while(T--)
	{
		V.clear();
		
		cin >> n;
		for(ll i = 1; i <= n; ++i)
		{
			cin >> arr[i];
			V.push_back(arr[i]);
		}
		sort(V.begin(),V.end());
		V.erase(unique(V.begin(),V.end()),V.end());
		//cout << V.size() << endl;
		for(ll i = 1; i <= n; ++i)
		{
			dis[ arr[i] ].l = inf;
			dis[ arr[i] ].r = -inf;
		}
		
		for(ll i = 1; i <= n; ++i)
		{
			dis[ arr[i] ].l = min(dis[ arr[i] ].l,i);
			dis[ arr[i] ].r = max(dis[ arr[i] ].r,i);
		}
		
		ll tmp = 1,ma = 1;
		for(ll i = 1; i < V.size(); ++i)
		{
			ll now = V[i],last = V[i-1];
			
			if(dis[now].l > dis[last].r)
				tmp++;
			else
				tmp = 1;
			ma = max(ma,tmp);
		}
		
	
		cout << V.size() - ma << endl;
	}
	return 0;
}

 

你可能感兴趣的:(思维)