CF1408H.Rainbow Triples

普及/提高-

通过率:0%

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:

  • 1ip<jp<kpn1 \leq i_p < j_p < k_p \leq n for each pp in 1,2,,m1, 2, \ldots, m ;
  • aip=akp=0a_{i_p} = a_{k_p} = 0 , ajp0a_{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.

输入格式

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

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

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

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

输出格式

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

输入输出样例

  • 输入#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 .

首页