#P16795. [蓝桥杯 2026 国 B] 奇偶校验排列

[蓝桥杯 2026 国 B] 奇偶校验排列

Problem Description

A certain checking system needs to use each of the numbers 1,2,…,n1, 2, \dots, n exactly once and arrange them into a sequence of length nn, p1,p2,…,pnp_1, p_2, \dots, p_n. Such a sequence is called a permutation.

The system generates a check string of length n−1n-1 based on the parity of the absolute differences between adjacent numbers in the permutation. For each 1≤i<n1 \le i < n, the ii-th check character cic_i is determined by the following rules:

  • If ∣pi−pi+1∣|p_i - p_{i+1}| is even, then cic_i is 00.
  • If ∣pi−pi+1∣|p_i - p_{i+1}| is odd, then cic_i is 11.

Now you are given a target check string SS of length n−1n-1. You need to construct a permutation such that the generated check string c1c2…cn−1c_1 c_2 \dots c_{n-1} is exactly equal to SS.

If there are multiple valid permutations, output the lexicographically smallest one. For two different permutations a1,a2,…,ana_1, a_2, \dots, a_n and b1,b2,…,bnb_1, b_2, \dots, b_n, if there exists a position kk such that the first k−1k-1 numbers are the same and ak<bka_k < b_k, then permutation aa is lexicographically smaller than permutation bb.

If no such permutation exists, output −1-1.

Input Format

The first line contains an integer nn, representing the number of labels.

The second line contains a string SS of length n−1n-1, representing the target check string. The string consists only of characters 00 and 11.

Output Format

If no valid permutation exists, output a single integer −1-1 on one line.

Otherwise, output nn integers on one line, representing the lexicographically smallest valid permutation. Adjacent integers should be separated by one space.

5
1010
1 2 4 3 5
6
00000
-1
8
0101101
1 3 2 4 5 6 8 7

Hint

Sample Explanation 1

The adjacent differences of this permutation are, in order:

  • ∣1−2∣=1|1-2|=1, which is odd, corresponding to 11.
  • ∣2−4∣=2|2-4|=2, which is even, corresponding to 00.
  • ∣4−3∣=1|4-3|=1, which is odd, corresponding to 11.
  • ∣3−5∣=2|3-5|=2, which is even, corresponding to 00.

Therefore, the generated check string is 10101010. Among all valid permutations, 1 2 4 3 51 \ 2 \ 4 \ 3 \ 5 is lexicographically the smallest.

Sample Explanation 2

Every character of the target check string is 00, so every adjacent difference must be even, meaning the two numbers must have the same parity. Then all numbers in all positions must have the same parity, but among 11 to 66 there are both odd and even numbers, so there is no solution.

Sample Explanation 3

The check string generated by the output permutation is, in order, 00, 11, 00, 11, 11, 00, 11, which is the same as the target check string 01011010101101.

Constraints and Notes for Testdata

For 30%30\% of the testdata, 2≤n≤82 \le n \le 8.

For 60%60\% of the testdata, 2≤n≤50002 \le n \le 5000.

For all testdata, 2≤n≤2×1052 \le n \le 2 \times 10^5, and the length of SS is n−1n-1.

Translated by ChatGPT 5