#P14190. [ICPC 2024 Hangzhou R] Dividing Sequence

    ID: 14063 Type: RemoteJudge 1000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 7 Uploaded By: Tags>字符串动态规划 DP贪心2024ICPCLyndon 分解杭州

[ICPC 2024 Hangzhou R] Dividing Sequence

题目描述

Alice got a sequence AA constructed by her neighbors. Since Alice doesn't like long sequences, she decides to divide the sequence into two (possibly empty) sequences BB and CC and give them back to her neighbors. Her division should meet the following constraints:

  • BB and CC are both subsequences of - Each element of AA belongs to exactly one of the sequences BB or CC.
  • BCB\leq C in lexicographical order.

Here we define a sequence P=p1,p2,,puP = p_1, p_2, \cdots, p_u of length uu to be lexicographically smaller than a sequence Q=q1,q2,,qvQ = q_1, q_2, \cdots, q_v of length vv if one of the following constraints is true:

  • u<vu < v and PP is a prefix of QQ.
  • There exists an integer 1kmin(u,v)1 \le k \le \min(u, v) such that pi=qip_i = q_i for all 1i<k1 \le i < k and pk<qkp_k < q_k.

As a fair girl, Alice hopes to divide fairly such that the lexicographical order of CC is as small as possible. Please tell Alice the minimum possible CC.

输入格式

There are multiple test cases. The first line of the input contains an integer TT (1T1041\leq T\leq 10^4) indicating the number of test cases. For each test case:

The first line contains an integer nn (1n5×1031\leq n\leq 5 \times 10^3) indicating the length of the sequence AA.

The second line contains nn integers a1,a2,,ana_1, a_2, \cdots, a_n (1ai1051\leq a_i\leq 10^5), where aia_i is the ii-th element of sequence AA.

It is guaranteed that the sum of nn of all test cases does not exceed 10410^4.

输出格式

For each test case output two lines. First output one line containing one integer mm indicating the length of the optimal CC. Then output a second line containing mm integers c1,c2,,cmc_1, c_2, \cdots, c_m separated by a space, where cic_i is the ii-th element of the optimal CC.

5
5
3 1 2 3 2
3
1 1 2
3
3 3 3
5
1 3 1 3 1
5
2 2 1 3 3
1
3
3
1 1 2
2
3 3
3
1 3 1
4
2 1 3 3