CF2146E.Yet Another MEX Problem

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

We define the weight of an array bb consisting of mm non-negative integers as the number of indices ii (1≤i≤m1\le i\le m) satisfying bi>mex⁡(b)b_i \gt \operatorname{mex}(b). Here, mex⁡(b)\operatorname{mex}(b) denotes the minimum excluded (MEX)∗^{\text{∗}} of the integers in bb.

You are given an array aa consisting of nn non-negative integers. For its subarray†^{\text{†}} [al,al+1,…,ar][a_l, a_{l+1}, \ldots, a_r], we denote the weight of the subarray as w(l,r)w(l,r).

For every 1≤i≤n1\le i\le n, you have to find the maximum weight over all subarrays of aa ending at index ii. In other words, you have to find max⁡1≤l≤iw(l,i)\max\limits_{1\le l\le i} w(l, i) for every 1≤i≤n1\le i\le n.

∗^{\text{∗}}The minimum excluded (MEX) of a collection of integers b1,b2,…,bmb_1, b_2, \ldots, b_m is defined as the smallest non-negative integer xx which does not occur in the collection bb.

†^{\text{†}}An array cc is a subarray of an array aa if cc can be obtained from aa by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

我们定义一个由 mm 个非负整数组成的数组 bb 的权重为满足 bi>mex⁡(b)b_i \gt \operatorname{mex}(b) 的下标 ii(其中 1≤i≤m1\le i\le m)的个数。这里,mex⁡(b)\operatorname{mex}(b) 表示数组 bb 中元素的最小未出现值(MEX)∗^{\text{∗}}。

给定一个由 nn 个非负整数组成的数组 aa。对于其子数组†^{\text{†}} [al,al+1,…,ar][a_l, a_{l+1}, \ldots, a_r],我们记该子数组的权重为 w(l,r)w(l,r)。

对每个 1≤i≤n1\le i\le n,你需要找出所有以索引 ii 结尾的 aa 的子数组中权重的最大值。换言之,对每个 1≤i≤n1\le i\le n,你需要计算 max⁡1≤l≤iw(l,i)\max\limits_{1\le l\le i} w(l, i)。

∗^{\text{∗}} 一组整数 b1,b2,…,bmb_1, b_2, \ldots, b_m 的**最小未出现值(MEX)**定义为不在该集合 bb 中出现的最小非负整数 xx。

†^{\text{†}} 若数组 cc 可通过从数组 aa 的开头删除若干(可能为零个或全部)元素、并从结尾删除若干(可能为零个或全部)元素而得到,则称 cc 是 aa 的一个子数组。

输入格式

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

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

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

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

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

每个测试用例的第一行包含一个整数 nn(1≤n≤3⋅1051\le n \le 3\cdot 10^5)—— 数组 aa 的长度。

第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(0≤ai≤n0\le a_i\le n)—— 数组 aa 的元素。

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

输出格式

For each test case, output nn integers, the ii-th integer denoting the maximum weight over all subarrays of aa ending at index ii, i.e., max⁡1≤l≤iw(l,i)\max\limits_{1\le l\le i}w(l,i).

对于每个测试用例,输出 nn 个整数,其中第 ii 个整数表示所有以索引 ii 结尾的子数组 aa 的最大权重,即 max⁡1≤l≤iw(l,i)\max\limits_{1\le l\le i}w(l,i)。

输入输出样例

  • 输入#1

    6
    1
    0
    5
    2 0 3 0 1
    6
    0 1 2 3 5 4
    7
    0 2 2 0 4 1 3
    8
    2 1 0 3 0 2 1 3
    15
    0 1 9 1 0 2 5 3 7 0 4 15 2 0 1

    输出#1

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

说明/提示

In the first test case, we have w(1,1)=0w(1, 1) = 0, because mex⁡([a1])=1\operatorname{mex}([a_1]) = 1, and there are no indices satisfying ai>1a_i \gt 1.

In the second test case, we can calculate the weight of each subarray [l,r][l,r] by the definition:

  1. w(1,1)=1w(1,1)=1. Thus, max⁡1≤l≤1w(l,1)=1\max\limits_{1\le l\le 1}w(l,1) =1;
  2. w(1,2)=1w(1,2)=1 and w(2,2)=0w(2,2)=0. Thus, max⁡1≤l≤2w(l,2)=1\max\limits_{1\le l\le 2}w(l,2) =1;
  3. w(1,3)=2w(1,3)=2 and w(2,3)=w(3,3)=1w(2,3)=w(3,3)=1. Thus, max⁡1≤l≤3w(l,3)=2\max\limits_{1\le l\le 3}w(l,3) =2;
  4. w(1,4)=2w(1,4)=2, w(2,4)=w(3,4)=1w(2,4)=w(3,4)=1, and w(4,4)=0w(4,4)=0. Thus, max⁡1≤l≤4w(l,4)=2\max\limits_{1\le l\le 4}w(l,4) =2;
  5. w(1,5)=0w(1,5)=0, w(2,5)=w(3,5)=1w(2,5)=w(3,5)=1, and w(4,5)=0,w(5,5)=1w(4,5)=0,w(5,5)=1. Thus, max⁡1≤l≤5w(l,5)=1\max\limits_{1\le l\le 5}w(l,5) =1.

For example, w(1,4)=2w(1, 4) = 2 because mex⁡([a1,a2,a3,a4])=1\operatorname{mex}([a_1, a_2, a_3, a_4]) = 1, and there are two indices satisfying ai>1a_i \gt 1 in the subarray: 11 and 33.

在第一个测试用例中,我们有 w(1,1)=0w(1, 1) = 0,因为 mex⁡([a1])=1\operatorname{mex}([a_1]) = 1,且不存在满足 ai>1a_i \gt 1 的下标。

在第二个测试用例中,我们可以根据定义计算每个子数组 [l,r][l,r] 的权重:

  1. w(1,1)=1w(1,1)=1。因此,max⁡1≤l≤1w(l,1)=1\max\limits_{1\le l\le 1}w(l,1) =1;
  2. w(1,2)=1w(1,2)=1 且 w(2,2)=0w(2,2)=0。因此,max⁡1≤l≤2w(l,2)=1\max\limits_{1\le l\le 2}w(l,2) =1;
  3. w(1,3)=2w(1,3)=2 且 w(2,3)=w(3,3)=1w(2,3)=w(3,3)=1。因此,max⁡1≤l≤3w(l,3)=2\max\limits_{1\le l\le 3}w(l,3) =2;
  4. w(1,4)=2w(1,4)=2,w(2,4)=w(3,4)=1w(2,4)=w(3,4)=1,且 w(4,4)=0w(4,4)=0。因此,max⁡1≤l≤4w(l,4)=2\max\limits_{1\le l\le 4}w(l,4) =2;
  5. w(1,5)=0w(1,5)=0,w(2,5)=w(3,5)=1w(2,5)=w(3,5)=1,且 w(4,5)=0w(4,5)=0、w(5,5)=1w(5,5)=1。因此,max⁡1≤l≤5w(l,5)=1\max\limits_{1\le l\le 5}w(l,5) =1。

例如,w(1,4)=2w(1, 4) = 2,因为 mex⁡([a1,a2,a3,a4])=1\operatorname{mex}([a_1, a_2, a_3, a_4]) = 1,且该子数组中有两个下标满足 ai>1a_i \gt 1:11 和 33。

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

首页