1209C.md (2593B)
1 Source: https://codeforces.com/problemset/problem/1209/C 2 3 C. Paint the Digits 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 You are given a sequence of $n$ digits $d_1d_2 \dots d_{n}$. You need to paint all the digits in two colors so that: 22 23 * each digit is painted either in the color $1$ or in the color $2$; 24 * if you write in a row from left to right all the digits painted in the color $1$, and then after them all the digits painted in the color $2$, then the resulting sequence of $n$ digits will be non-decreasing (that is, each next digit will be greater than or equal to the previous digit). 25 26 For example, for the sequence $d=914$ the only valid coloring is $211$ (paint in the color $1$ two last digits, paint in the color $2$ the first digit). But $122$ is not a valid coloring ($9$ concatenated with $14$ is not a non-decreasing sequence). 27 28 It is allowed that either of the two colors is not used at all. Digits painted in the same color are not required to have consecutive positions. 29 30 Find any of the valid ways to paint the given sequence of digits or determine that it is impossible to do. 31 32 Input 33 34 The first line contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases in the input. 35 36 The first line of each test case contains an integer $n$ ($1 \le n \le 2\cdot10^5$) — the length of a given sequence of digits. 37 38 The next line contains a sequence of $n$ digits $d_1d_2 \dots d_{n}$ ($0 \le d_i \le 9$). The digits are written in a row without spaces or any other separators. The sequence can start with 0. 39 40 It is guaranteed that the sum of the values of $n$ for all test cases in the input does not exceed $2\cdot10^5$. 41 42 Output 43 44 Print $t$ lines — the answers to each of the test cases in the input. 45 46 If there is a solution for a test case, the corresponding output line should contain any of the valid colorings written as a string of $n$ digits $t_1t_2 \dots t_n$ ($1 \le t_i \le 2$), where $t_i$ is the color the $i$-th digit is painted in. If there are several feasible solutions, print any of them. 47 48 If there is no solution, then the corresponding output line should contain a single character '-' (the minus sign). 49 50 Example 51 52 Input 53 54 ``` 55 5 56 12 57 040425524644 58 1 59 0 60 9 61 123456789 62 2 63 98 64 3 65 987 66 ``` 67 68 Output 69 70 ``` 71 121212211211 72 1 73 222222222 74 21 75 - 76 ``` 77 78 Note 79 80 In the first test case, $d=040425524644$. The output $t=121212211211$ is correct because $0022444$ (painted in $1$) concatenated with $44556$ (painted in $2$) is $002244444556$ which is a sorted sequence of $n$ given digits.