CF1919C.Grouping Increases

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given an array aa of size nn. You will do the following process to calculate your penalty:

  1. Split array aa into two (possibly empty) subsequences†^\dagger ss and tt such that every element of aa is either in ss or t‡t^\ddagger.
  2. For an array bb of size mm, define the penalty p(b)p(b) of an array bb as the number of indices ii between 11 and m−1m - 1 where bi<bi+1b_i \lt b_{i + 1}.
  3. The total penalty you will receive is p(s)+p(t)p(s) + p(t).

If you perform the above process optimally, find the minimum possible penalty you will receive.

†^\dagger A sequence xx is a subsequence of a sequence yy if xx can be obtained from yy by the deletion of several (possibly, zero or all) elements.

‡^\ddagger Some valid ways to split array a=[3,1,4,1,5]a=[3,1,4,1,5] into (s,t)(s,t) are ([3,4,1,5],[1])([3,4,1,5],[1]), ([1,1],[3,4,5])([1,1],[3,4,5]) and ([ ],[3,1,4,1,5])([\,],[3,1,4,1,5]) while some invalid ways to split aa are ([3,4,5],[1])([3,4,5],[1]), ([3,1,4,1],[1,5])([3,1,4,1],[1,5]) and ([1,3,4],[5,1])([1,3,4],[5,1]).

给你一个大小为 nn 的数组 aa。你需要执行以下过程来计算你的惩罚值:

  1. 将数组 aa 拆分为两个(可能为空的)子序列†^\dagger ss 和 tt,使得 aa 中的每个元素恰好属于 ss 或 tt 中的一个‡^\ddagger。
  2. 对于大小为 mm 的数组 bb,定义其惩罚值 p(b)p(b) 为满足 1≤i≤m−11 \le i \le m - 1 且 bi<bi+1b_i \lt b_{i + 1} 的下标 ii 的个数。
  3. 你最终承受的总惩罚值为 p(s)+p(t)p(s) + p(t)。

若你以最优方式执行上述过程,求你所能获得的最小可能惩罚值。

†^\dagger 序列 xx 是序列 yy 的一个子序列,当且仅当 xx 可通过从 yy 中删除若干(可能为零个或全部)元素而得到。

‡^\ddagger 将数组 a=[3,1,4,1,5]a=[3,1,4,1,5] 拆分为 (s,t)(s,t) 的一些合法方式包括 ([3,4,1,5],[1])([3,4,1,5],[1])、([1,1],[3,4,5])([1,1],[3,4,5]) 和 ([ ],[3,1,4,1,5])([\,],[3,1,4,1,5]);而一些非法方式包括 ([3,4,5],[1])([3,4,5],[1])、([3,1,4,1],[1,5])([3,1,4,1],[1,5]) 和 ([1,3,4],[5,1])([1,3,4],[5,1])。

输入格式

Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤1041 \leq t \leq 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤2⋅1051\le n\le 2\cdot 10^5) — the size of the array aa.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n1 \le a_i \le n) — the elements of the array aa.

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

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1041 \leq t \leq 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 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),表示数组 aa 的元素。

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

输出格式

For each test case, output a single integer representing the minimum possible penalty you will receive.

对于每个测试用例,输出一个整数,表示你将获得的最小可能罚分。

输入输出样例

  • 输入#1

    5
    5
    1 2 3 4 5
    8
    8 2 3 1 1 7 4 3
    5
    3 3 3 3 3
    1
    1
    2
    2 1

    输出#1

    3
    1
    0
    0
    0

说明/提示

In the first test case, a possible way to split aa is s=[2,4,5]s=[2,4,5] and t=[1,3]t=[1,3]. The penalty is p(s)+p(t)=2+1=3p(s)+p(t)=2 + 1 =3.

In the second test case, a possible way to split aa is s=[8,3,1]s=[8,3,1] and t=[2,1,7,4,3]t=[2,1,7,4,3]. The penalty is p(s)+p(t)=0+1=1p(s)+p(t)=0 + 1 =1.

In the third test case, a possible way to split aa is s=[ ]s=[\,] and t=[3,3,3,3,3]t=[3,3,3,3,3]. The penalty is p(s)+p(t)=0+0=0p(s)+p(t)=0 + 0 =0.

在第一个测试用例中,一种可能的 aa 的划分方式是 s=[2,4,5]s=[2,4,5] 和 t=[1,3]t=[1,3]。惩罚值为 p(s)+p(t)=2+1=3p(s)+p(t)=2 + 1 =3。

在第二个测试用例中,一种可能的 aa 的划分方式是 s=[8,3,1]s=[8,3,1] 和 t=[2,1,7,4,3]t=[2,1,7,4,3]。惩罚值为 p(s)+p(t)=0+1=1p(s)+p(t)=0 + 1 =1。

在第三个测试用例中,一种可能的 aa 的划分方式是 s=[ ]s=[\,] 和 t=[3,3,3,3,3]t=[3,3,3,3,3]。惩罚值为 p(s)+p(t)=0+0=0p(s)+p(t)=0 + 0 =0。

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

首页