#P15748. [JAG 2024 Summer Camp #3] Halfway Through the Book

[JAG 2024 Summer Camp #3] Halfway Through the Book

题目描述

A book titled "Immense Catalog of Permutation Compilation" is published by JAG Publisher. This book is extremely lengthy, with a total of 2N12^N - 1 pages. Each page contains one of the non-empty subsequences (not necessarily contiguous) of a permutation P\mathbf{P} of length NN (i.e., a rearrangement of (1,,N)(1, \ldots, N)). Each subsequence appears exactly once in lexicographical order. In other words, on the page kk, you'll find the kk-th lexicographically smallest subsequence of P\mathbf{P} among all non-empty subsequences.

You've tried to read the entire book but gave up. However, you want to impress your friends by claiming that you read half of it, so you need to find the sequence on the exact middle page, which is page 2N12^{N-1}. Your task is to determine this sequence.

输入格式

The input consists of a single test case of the following format.

$$\begin{aligned} & N \\ & P_1 \ P_2 \ \ldots \ P_N \end{aligned} $$

The first line contains an integer NN, where NN represents the length of permutation P\mathbf{P}. NN is between 11 and 10,00010,000. The second line contains NN integers P1,,PNP_1, \ldots, P_N (1PiN1 \leq P_i \leq N) which represent the permutation P\mathbf{P}.

输出格式

On the first line, print MM, which represents the length of the sequence on page 2N12^{N-1}. On the second line, print MM integers Q1,Q2,,QMQ_1, Q_2, \ldots, Q_M, which represent the subsequence on page 2N12^{N-1}.

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