#P16801. [蓝桥杯 2026 国 B] 货架换签

    ID: 19142 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>数学贪心2026蓝桥杯国赛

[蓝桥杯 2026 国 B] 货架换签

Problem Description

Xiao Lan manages a row of shelves, and there is a tag hanging in front of each shelf. Each tag contains the character 00 or 11, forming a string SS of length NN from left to right.

Xiao Lan can perform any number of tag-swapping operations, or perform none. One tag-swapping operation is done as follows:

  • Choose two non-overlapping consecutive segments in the current tag sequence.
  • The two consecutive segments must contain the same number of tags (i.e., have the same length).
  • The numbers of tags with 11 in the two segments must be the same.
  • Swap the contents of the two segments in place. The other tags remain unchanged, and the relative order inside each segment remains unchanged.

For example, in the string 101001101001, you can choose the segment 1010 and the segment 0101. Both have length 22, and both contain one character 11, so they can be swapped.

For two strings of the same length, compare them by the usual lexicographic order, and assume that 00 is smaller than 11.

Now, please find the lexicographically smallest string that Xiao Lan can obtain after any number of valid tag-swapping operations.

Input Format

The first line contains an integer NN, representing the length of the string.

The second line contains a string SS of length NN, consisting only of characters 00 and 11.

Output Format

Output one line containing a string of length NN, representing the lexicographically smallest string that can be obtained.

6
101001
011010

5
00000
00000
8
11110000
11110000

Hint

Sample Explanation 1

You can choose the segment formed by the 11st to 22nd characters of the original string, which is 1010, and the segment formed by the 55th to 66th characters, which is 0101. These two segments have the same length and both contain one character 11. After swapping them, you get 011010011010.

It can be proven that among all reachable strings, 011010011010 is lexicographically the smallest.

Sample Explanation 2

There is no character 11 in the string, so any valid operation will not change the string.

Sample Explanation 3

In the original string, to the left of each character 00 there are 44 characters 11. Under the constraints that valid operations must satisfy, it is impossible to obtain a lexicographically smaller string.

Constraints and Notes

For 30%30\% of the testdata, 1≤N≤101 \le N \le 10.

For 60%60\% of the testdata, 1≤N≤50001 \le N \le 5000.

For all testdata, 1≤N≤2×1051 \le N \le 2 \times 10^5, and SS consists only of characters 00 and 11.

Translated by ChatGPT 5