CF1454C.Sequence Transformation
普及-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a sequence a, initially consisting of n integers.
You want to transform this sequence so that all elements in it are equal (i. e. it contains several occurrences of the same element).
To achieve this, you choose some integer x that occurs at least once in a, and then perform the following operation any number of times (possibly zero): choose some segment [l,r] of the sequence and remove it. But there is one exception: you are not allowed to choose a segment that contains x. More formally, you choose some contiguous subsequence [al,al+1,…,ar] such that ai=x if l≤i≤r, and remove it. After removal, the numbering of elements to the right of the removed segment changes: the element that was the (r+1)-th is now l-th, the element that was (r+2)-th is now (l+1)-th, and so on (i. e. the remaining sequence just collapses).
Note that you can not change x after you chose it.
For example, suppose n=6, a=[1,3,2,4,1,2]. Then one of the ways to transform it in two operations is to choose x=1, then:
- choose l=2, r=4, so the resulting sequence is a=[1,1,2];
- choose l=3, r=3, so the resulting sequence is a=[1,1].
Note that choosing x is not an operation. Also, note that you can not remove any occurrence of x.
Your task is to find the minimum number of operations required to transform the sequence in a way described above.
You have to answer t independent test cases.
给你一个序列 a,初始时包含 n 个整数。
你想将该序列变换为所有元素均相等的序列(即序列中只包含某一个元素的若干次重复)。
为此,你首先选择某个在 a 中至少出现一次的整数 x,然后可执行任意次数(包括零次)如下操作:选择序列的一个区间 [l,r] 并将其删除。但有一个例外:你不能选择包含 x 的区间。更准确地说,你需选择某个连续子序列 [al,al+1,…,ar],满足对所有 l≤i≤r 都有 ai=x,然后将其删除。删除后,原位于被删区间右侧的元素编号发生改变:原第 (r+1) 个元素变为第 l 个,原第 (r+2) 个元素变为第 (l+1) 个,依此类推(即剩余序列自然“坍缩”)。
注意:选定 x 后便不可更改。
例如,设 n=6,a=[1,3,2,4,1,2]。一种可在两次操作内完成变换的方法是选择 x=1,然后:
- 选择 l=2,r=4,得到新序列 a=[1,1,2];
- 选择 l=3,r=3,得到新序列 a=[1,1]。
注意:选择 x 本身不计为一次操作;此外,你不能删除任何 x 的出现。
你的任务是求出按上述方式将序列变换所需的最少操作次数。
你需要回答 t 个相互独立的测试用例。
输入格式
The first line of the input contains one integer t (1≤t≤2⋅104) — the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1≤n≤2⋅105) — the number of elements in a. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤n), where ai is the i-th element of a.
It is guaranteed that the sum of n does not exceed 2⋅105 (∑n≤2⋅105).
输入的第一行包含一个整数 t(1≤t≤2⋅104),表示测试用例的数量。随后是 t 个测试用例。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105),表示数组 a 中元素的个数。每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤n),其中 ai 是数组 a 的第 i 个元素。
保证所有测试用例中 n 的总和不超过 2⋅105(即 ∑n≤2⋅105)。
输出格式
For each test case, print the answer — the minimum number of operations required to transform the given sequence in a way described in the problem statement. It can be proven that it is always possible to perform a finite sequence of operations so the sequence is transformed in the required way.
对于每个测试用例,输出答案——即按题目描述的方式将给定序列变换所需的最少操作次数。可以证明:总能通过有限次操作将序列变换为所要求的形式。
输入输出样例
输入#1
5 3 1 1 1 5 1 2 3 4 5 5 1 2 3 2 1 7 1 2 3 1 2 3 1 11 2 2 1 2 3 2 1 2 3 1 2
输出#1
0 1 1 2 3
输入解题思路,AI测评打分。不知道怎么写?