#P10901. [蓝桥杯 2024 省 C] 封闭图形个数

[蓝桥杯 2024 省 C] 封闭图形个数

Problem Description

In the Lanqiao Kingdom, the size of a number depends not only on its numerical value, but also on the number of “closed shapes” it forms.

A closed shape refers to a completely enclosed space inside a digit. For example, digits 11, 22, 33, 55, 77 do not form any closed shapes, while digits 00, 44, 66, 99 each form 11 closed shape, and digit 88 forms 22 closed shapes. Note that the number of closed shapes can be added up. For example, for the number 6868, since 66 forms 11 closed shape and 88 forms 22 closed shapes, the total number of closed shapes formed by 6868 is 33.

When comparing two numbers, if their numbers of closed shapes are different, then the number with more closed shapes is larger. For example, the numbers 4141 and 1818 have 11 and 22 closed shapes respectively, so 4141 is smaller than 1818. If two numbers have the same number of closed shapes, then the number with the larger numerical value is larger. For example, the numbers 1414 and 4141 both have 11 closed shape, but 14<4114 < 41, so 1414 is smaller than 4141. If two numbers have the same number of closed shapes and the same numerical value, then they are considered equal.

Xiao Lan is very interested in the rules for comparing numbers in the Lanqiao Kingdom. Now, you are given nn numbers a1,a2,,ana_1, a_2,\cdots, a_n. Please sort these nn numbers in non-decreasing order according to the Lanqiao Kingdom’s rules, and output the sorted result.

Input Format

The first line contains an integer nn, indicating the number of given numbers.

The second line contains nn integers a1,a2,,ana_1, a_2,\cdots, a_n, separated by a single space, representing the numbers to be sorted.

Output Format

Output one line containing nn integers, separated by a single space, representing the result after sorting in non-decreasing order according to the Lanqiao Kingdom’s rules.

3
18 29 6
6 29 18

Hint

[Sample Explanation]

For the given sequence [18,29,6][18, 29, 6], the number 1818 has 22 closed shapes, 2929 has 11 closed shape, and 66 has 11 closed shape. Sorting by the number of closed shapes from small to large yields [29,6,18][29, 6, 18].

Since 2929 and 66 have the same number of closed shapes, we further sort them by numerical value, and finally get [6,29,18][6, 29, 18].

[Constraints and Conventions]

For 50%50\% of the testdata, 1n2×1031 \le n \le 2 \times 10^3, 1ai1051 \le a_i \le 10^5.
For all testdata, 1n2×1051 \le n \le 2 \times 10^5, 1ai1091 \le a_i \le 10^9.

Translated by ChatGPT 5