#P15345. [TOIP 2025] 連序

[TOIP 2025] 連序

Problem Description

The binary representation of a non-negative integer can be viewed as a 0101 string, with the left side being the most significant bit and the right side being the least significant bit. If it has at most kk consecutive bits equal to 11, then the number’s “consecutive order” is defined as kk. Given nn non-negative integers, sort them in increasing order of their “consecutive order”. If two numbers have the same consecutive order, output the one with the smaller value first.

For example, the binary representations of 77, 1212, and 2727 are (111)2{\left(111\right)}_2, (1100)2{\left(1100\right)}_2, and (11011)2{\left(11011\right)}_2, respectively. Their consecutive orders are 33, 22, and 22. Since 1212 and 2727 have the same consecutive order but 12<2712 < 27, the output should be, in order, 1212, 2727, 77.

Input Format

$$\begin{aligned} &n \\ &a_1 \; a_2 \; a_3 \; \cdots \; a_n \end{aligned}$$
  • nn is the number of integers to be sorted.
  • aia_i is the ii-th integer to be sorted.

Output Format

$$\begin{aligned} &s_1 \; s_2 \; s_3 \; \cdots \; s_n \end{aligned}$$
  • sis_i is the ii-th integer after sorting according to the requirements.
3
7 12 27
12 27 7
5
1 2 3 4 5
1 2 4 5 3

Hint

Constraints

  • 1≤n≤2×1051 \le n \le 2 \times {10}^5.
  • 0≤ai<2300 \le a_i < 2^{30}.
  • All input numbers are integers.

Scoring

This problem has four subtasks with the following constraints. Each subtask may contain one or more testdata files. You will get the score for a subtask only if you answer all testdata files in that subtask correctly.

Subtask Score Additional Input Constraints
1 22 n≤2, ai<16n \le 2,\ a_i < 16。
2 n≤2 000, ai<216n \le 2\,000,\ a_i < 2^{16}。
3 26 ai<216a_i < 2^{16}。
4 30 No additional constraints.

Translated by ChatGPT 5