CF2161D.Locked Out

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Who's a good array? You're a good array! (c)

An array bb is good if there do not exist indices 1≤i<j≤∣b∣1 \leq i \lt j \leq |b| such that bj−bi=1b_j - b_i = 1.

You are given an integer. array a1,a2,…,ana_1, a_2, \ldots, a_n. Determine the minimum number of elements that need to be removed from the given array so that it becomes a good array.

谁是好数组?你就是好数组!(c)

数组 bb 被称为“好”的,当且仅当不存在下标 1≤i<j≤∣b∣1 \leq i \lt j \leq |b|,使得 bj−bi=1b_j - b_i = 1。

给定一个整数数组 a1,a2,…,ana_1, a_2, \ldots, a_n。请确定:为使该数组变为好数组,至少需要删除多少个元素。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤6⋅1041 \le t \le 6 \cdot 10^4). The description of the test cases follows.

The first line of each test case contains an integer nn (1≤n≤3⋅1051 \leq n \leq 3 \cdot 10^5) — the number of elements in the array.

The second line of each test case contains nn integers a1a_1, a2a_2, …\ldots, ana_n (1≤ai≤n1 \leq a_i \leq n) — the elements of the array.

It is guaranteed that the sum of nn over all test cases does not exceed 3⋅1053 \cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤6⋅1041 \le t \le 6 \cdot 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤3⋅1051 \leq n \leq 3 \cdot 10^5)—— 表示数组中元素的个数。

每个测试用例的第二行包含 nn 个整数 a1a_1, a2a_2, …\ldots, ana_n(1≤ai≤n1 \leq a_i \leq n)—— 表示数组的元素。

保证所有测试用例的 nn 之和不超过 3⋅1053 \cdot 10^5。

输出格式

For each test case, print a single integer — the minimum number of elements that need to be removed from the array to make it a good array.

对于每个测试用例,输出一个整数——使该数组变为“好数组”所需删除的最少元素个数。

输入输出样例

  • 输入#1

    6
    1
    1
    5
    1 2 3 4 5
    5
    5 4 3 2 1
    5
    5 5 5 4 4
    7
    1 7 1 2 5 7 1
    6
    1 2 5 6 5 5

    输出#1

    0
    2
    0
    0
    1
    2

说明/提示

In the first test case, the array is good from the very beginning, so nothing needs to be removed from it.

In the second test case, the optimal solution is to remove the second and fourth elements of the array, which will result in 11, 33, 55 array.

In the third test case, the array is good from the very beginning.

In the fourth test case, the array is good from the very beginning.

In the fifth test case, the optimal solution is to remove the fourth element of the array, which will result in 11, 77, 11, 55, 77, 11 array.

In the sixth test case, one of the optimal solutions is to remove the first and fourth elements of the array, which will result in 22, 55, 55, 55 array.

在第一个测试用例中,数组从一开始就是“好”的,因此无需从中删除任何元素。

在第二个测试用例中,最优解是删除数组的第二个和第四个元素,得到数组 11, 33, 55。

在第三个测试用例中,数组从一开始就是“好”的。

在第四个测试用例中,数组从一开始就是“好”的。

在第五个测试用例中,最优解是删除数组的第四个元素,得到数组 11, 77, 11, 55, 77, 11。

在第六个测试用例中,一种最优解是删除数组的第一个和第四个元素,得到数组 22, 55, 55, 55。

输入解题思路,AI测评打分。不知道怎么写?

首页