CF1454C.Sequence Transformation

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a sequence aa, initially consisting of nn 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 xx that occurs at least once in aa, and then perform the following operation any number of times (possibly zero): choose some segment [l,r][l, r] of the sequence and remove it. But there is one exception: you are not allowed to choose a segment that contains xx. More formally, you choose some contiguous subsequence [al,al+1,…,ar][a_l, a_{l + 1}, \dots, a_r] such that ai≠xa_i \ne x if l≤i≤rl \le i \le 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)(r+1)-th is now ll-th, the element that was (r+2)(r+2)-th is now (l+1)(l+1)-th, and so on (i. e. the remaining sequence just collapses).

Note that you can not change xx after you chose it.

For example, suppose n=6n = 6, a=[1,3,2,4,1,2]a = [1, 3, 2, 4, 1, 2]. Then one of the ways to transform it in two operations is to choose x=1x = 1, then:

  1. choose l=2l = 2, r=4r = 4, so the resulting sequence is a=[1,1,2]a = [1, 1, 2];
  2. choose l=3l = 3, r=3r = 3, so the resulting sequence is a=[1,1]a = [1, 1].

Note that choosing xx is not an operation. Also, note that you can not remove any occurrence of xx.

Your task is to find the minimum number of operations required to transform the sequence in a way described above.

You have to answer tt independent test cases.

给你一个序列 aa,初始时包含 nn 个整数。

你想将该序列变换为所有元素均相等的序列(即序列中只包含某一个元素的若干次重复)。

为此,你首先选择某个在 aa 中至少出现一次的整数 xx,然后可执行任意次数(包括零次)如下操作:选择序列的一个区间 [l,r][l, r] 并将其删除。但有一个例外:你不能选择包含 xx 的区间。更准确地说,你需选择某个连续子序列 [al,al+1,…,ar][a_l, a_{l + 1}, \dots, a_r],满足对所有 l≤i≤rl \le i \le r 都有 ai≠xa_i \ne x,然后将其删除。删除后,原位于被删区间右侧的元素编号发生改变:原第 (r+1)(r+1) 个元素变为第 ll 个,原第 (r+2)(r+2) 个元素变为第 (l+1)(l+1) 个,依此类推(即剩余序列自然“坍缩”)。

注意:选定 xx 后便不可更改。

例如,设 n=6n = 6,a=[1,3,2,4,1,2]a = [1, 3, 2, 4, 1, 2]。一种可在两次操作内完成变换的方法是选择 x=1x = 1,然后:

  1. 选择 l=2l = 2,r=4r = 4,得到新序列 a=[1,1,2]a = [1, 1, 2];
  2. 选择 l=3l = 3,r=3r = 3,得到新序列 a=[1,1]a = [1, 1]。

注意:选择 xx 本身不计为一次操作;此外,你不能删除任何 xx 的出现。

你的任务是求出按上述方式将序列变换所需的最少操作次数。

你需要回答 tt 个相互独立的测试用例。

输入格式

The first line of the input contains one integer tt (1≤t≤2⋅1041 \le t \le 2 \cdot 10^4) — the number of test cases. Then tt test cases follow.

The first line of the test case contains one integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the number of elements in aa. The second line of the test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n1 \le a_i \le n), where aia_i is the ii-th element of aa.

It is guaranteed that the sum of nn does not exceed 2⋅1052 \cdot 10^5 (∑n≤2⋅105\sum n \le 2 \cdot 10^5).

输入的第一行包含一个整数 tt(1≤t≤2⋅1041 \le t \le 2 \cdot 10^4),表示测试用例的数量。随后是 tt 个测试用例。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5),表示数组 aa 中元素的个数。每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤n1 \le a_i \le n),其中 aia_i 是数组 aa 的第 ii 个元素。

保证所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5(即 ∑n≤2⋅105\sum n \le 2 \cdot 10^5)。

输出格式

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测评打分。不知道怎么写?

首页