CF1408H.Rainbow Triples

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a sequence a1,a2,…,ana_1, a_2, \ldots, a_n of non-negative integers.

You need to find the largest number mm of triples (i1,j1,k1)(i_1, j_1, k_1), (i2,j2,k2)(i_2, j_2, k_2), ..., (im,jm,km)(i_m, j_m, k_m) such that:

  • 1≤ip<jp<kp≤n1 \leq i_p \lt j_p \lt k_p \leq n for each pp in 1,2,…,m1, 2, \ldots, m;
  • aip=akp=0a_{i_p} = a_{k_p} = 0, ajp≠0a_{j_p} \neq 0;
  • all aj1,aj2,…,ajma_{j_1}, a_{j_2}, \ldots, a_{j_m} are different;
  • all i1,j1,k1,i2,j2,k2,…,im,jm,kmi_1, j_1, k_1, i_2, j_2, k_2, \ldots, i_m, j_m, k_m are different.

给你一个长度为 nn 的非负整数序列 a1,a2,…,ana_1, a_2, \ldots, a_n。

你需要找出满足以下条件的三元组 (i1,j1,k1)(i_1, j_1, k_1), (i2,j2,k2)(i_2, j_2, k_2), ..., (im,jm,km)(i_m, j_m, k_m) 的最大可能个数 mm:

  • 对每个 p=1,2,…,mp = 1, 2, \ldots, m,均有 1≤ip<jp<kp≤n1 \leq i_p \lt j_p \lt k_p \leq n;
  • aip=akp=0a_{i_p} = a_{k_p} = 0,且 ajp≠0a_{j_p} \neq 0;
  • 所有 aj1,aj2,…,ajma_{j_1}, a_{j_2}, \ldots, a_{j_m} 互不相同;
  • 所有下标 i1,j1,k1,i2,j2,k2,…,im,jm,kmi_1, j_1, k_1, i_2, j_2, k_2, \ldots, i_m, j_m, k_m 互不相同。

输入格式

The first line of input contains one integer tt (1≤t≤500 0001 \leq t \leq 500\,000): the number of test cases.

The first line of each test case contains one integer nn (1≤n≤500 0001 \leq n \leq 500\,000).

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai≤n0 \leq a_i \leq n).

The total sum of nn is at most 500 000500\,000.

输入的第一行包含一个整数 tt(1≤t≤500 0001 \leq t \leq 500\,000):测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤500 0001 \leq n \leq 500\,000)。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai≤n0 \leq a_i \leq n)。

所有测试用例中 nn 的总和不超过 500 000500\,000。

输出格式

For each test case, print one integer mm: the largest number of proper triples that you can find.

对于每个测试用例,输出一个整数 mm:你能找到的合法三元组的最大数量。

输入输出样例

  • 输入#1

    8
    1
    1
    2
    0 0
    3
    0 1 0
    6
    0 0 1 2 0 0
    6
    0 1 0 0 1 0
    6
    0 1 3 2 0 0
    6
    0 0 0 0 5 0
    12
    0 1 0 2 2 2 0 0 3 3 4 0

    输出#1

    0
    0
    1
    2
    1
    1
    1
    2

说明/提示

In the first two test cases, there are not enough elements even for a single triple, so the answer is 00.

In the third test case we can select one triple (1,2,3)(1, 2, 3).

In the fourth test case we can select two triples (1,3,5)(1, 3, 5) and (2,4,6)(2, 4, 6).

In the fifth test case we can select one triple (1,2,3)(1, 2, 3). We can't select two triples (1,2,3)(1, 2, 3) and (4,5,6)(4, 5, 6), because a2=a5a_2 = a_5.

在前两个测试用例中,元素数量甚至不足以构成一个三元组,因此答案为 00。

在第三个测试用例中,我们可以选择一个三元组 (1,2,3)(1, 2, 3)。

在第四个测试用例中,我们可以选择两个三元组 (1,3,5)(1, 3, 5) 和 (2,4,6)(2, 4, 6)。

在第五个测试用例中,我们可以选择一个三元组 (1,2,3)(1, 2, 3)。我们不能选择两个三元组 (1,2,3)(1, 2, 3) 和 (4,5,6)(4, 5, 6),因为 a2=a5a_2 = a_5。

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

首页