【思维】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 <bits/stdc++.h>
#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];

vector<ll>V;

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;
}

 

### 关于 DOTween Sequence 的用法 DOTween 是 Unity 中非常流行的动画插件,用于简化对象的移动、旋转、缩放和其他属性的变化操作。Sequence 则是 DOTween 提供的一种功能,允许开发者创建复杂的动画序列,其中可以按顺序执行多个 Tween 动画。 #### 创建和使用 Sequence 以下是创建并使用 DOTween Sequence 的基本方法: 1. **初始化 Sequence** 使用 `DOTween.Sequence()` 方法来创建一个新的 Sequence 对象。 2. **向 Sequence 添加 Tween** 调用 `.Append()`, `.Prepend()`, 或其他相关方法将 Tween 添加到 Sequence 中。这些方法决定了 Tween 在 Sequence 中的位置以及它们之间的关系。 3. **控制播放逻辑** 可以通过调用 `.Play()`, `.Pause()`, 和 `.Restart()` 来控制整个 Sequence 的播放状态。 下面是一个简单的代码示例展示如何使用 DOTween Sequence: ```csharp using DG.Tweening; using UnityEngine; public class DOTweenExample : MonoBehaviour { void Start() { // 初始化一个 Sequence Sequence mySequence = DOTween.Sequence(); // 向 Sequence 添加 Tween 动画 mySequence.Append(transform.DOMove(new Vector3(5, 0, 0), 2)); // 移动物体至 (5,0,0),耗时2秒 mySequence.AppendInterval(1); // 停顿1秒 mySequence.Append(transform.DOScale(0.5f, 1)); // 缩小物体尺寸至一半,耗时1秒 // 开始播放 Sequence mySequence.Play(); } } ``` 上述代码展示了如何构建一个包含三个阶段的动画序列:首先是平移运动,接着是一段停顿时间,最后是对物体大小进行调整[^5]。 #### 解决常见问题 如果遇到与 DOTween Sequence 相关的问题,可以从以下几个方面入手排查: - **确认插件版本兼容性**: 确保使用的 DOTween 版本支持当前项目中的 Unity 版本。 - **检查资源路径配置**: 如果某些动画未正常运行,可能是因为目标 GameObject 或组件被销毁或者丢失了引用。 - **调试日志输出**: 利用 Debug.Log 输出关键帧的状态变化以便定位错误位置。 对于更深入的技术细节,比如内存管理和性能优化等问题,则需参照官方文档或其他权威资料进一步学习研究[^6]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值