CF1696B.NIT Destroys the Universe
入门
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
For a collection of integers S, define mex(S) as the smallest non-negative integer that does not appear in S.
NIT, the cleaver, decides to destroy the universe. He is not so powerful as Thanos, so he can only destroy the universe by snapping his fingers several times.
The universe can be represented as a 1-indexed array a of length n. When NIT snaps his fingers, he does the following operation on the array:
- He selects positive integers l and r such that 1≤l≤r≤n. Let w=mex(al,al+1,…,ar). Then, for all l≤i≤r, set ai to w.
We say the universe is destroyed if and only if for all 1≤i≤n, ai=0 holds.
Find the minimum number of times NIT needs to snap his fingers to destroy the universe. That is, find the minimum number of operations NIT needs to perform to make all elements in the array equal to 0.
对于一个整数集合 S,定义 mex(S) 为不在 S 中出现的最小非负整数。
NIT,一位聪明的家伙,决定毁灭宇宙。他没有灭霸那么强大,因此只能通过多次打响指来毁灭宇宙。
宇宙可以表示为一个长度为 n 的、下标从 1 开始的数组 a。当 NIT 打响指时,他对该数组执行如下操作:
- 他选择正整数 l 和 r,满足 1≤l≤r≤n。令 w=mex(al,al+1,…,ar)。然后,对所有满足 l≤i≤r 的下标 i,将 ai 赋值为 w。
我们称宇宙被毁灭,当且仅当对所有 1≤i≤n,均有 ai=0 成立。
求 NIT 毁灭宇宙所需的最少打响指次数。即,求使数组中所有元素均变为 0 所需的最少操作次数。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). Description of the test cases follows.
The first line of each test case contains one integer n (1≤n≤105).
The second line of each test case contains n integers a1, a2, …, an (0≤ai≤109).
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤105)。
每个测试用例的第二行包含 n 个整数 a1, a2, …, an(0≤ai≤109)。
保证所有测试用例的 n 值之和不超过 2⋅105。
输出格式
For each test case, print one integer — the answer to the problem.
对于每个测试用例,输出一个整数——即该问题的答案。
输入输出样例
输入#1
4 4 0 0 0 0 5 0 1 2 3 4 7 0 2 3 0 1 2 0 1 1000000000
输出#1
0 1 2 1
说明/提示
In the first test case, we do 0 operations and all elements in the array are already equal to 0.
In the second test case, one optimal way is doing the operation with l=2, r=5.
In the third test case, one optimal way is doing the operation twice, respectively with l=4, r=4 and l=2, r=6.
In the fourth test case, one optimal way is doing the operation with l=1, r=1.
在第一个测试用例中,我们执行 0 次操作,数组中的所有元素已经都等于 0。
在第二个测试用例中,一种最优方案是执行一次操作,其中 l=2,r=5。
在第三个测试用例中,一种最优方案是执行两次操作,分别取 l=4、r=4 和 l=2、r=6。
在第四个测试用例中,一种最优方案是执行一次操作,其中 l=1,r=1。
输入解题思路,AI测评打分。不知道怎么写?