codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1251C.md (2184B)


      1 Source: https://codeforces.com/problemset/problem/1251/C
      2 
      3 C. Minimize The Integer
      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 huge integer $a$ consisting of $n$ digits ($n$ is between $1$ and $3 \cdot 10^5$, inclusive). It may contain leading zeros.
     22 
     23 You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by $2$).
     24 
     25 For example, if $a = 032867235$ you can get the following integers in a single operation:
     26 
     27 * $302867235$ if you swap the first and the second digits;
     28 * $023867235$ if you swap the second and the third digits;
     29 * $032876235$ if you swap the fifth and the sixth digits;
     30 * $032862735$ if you swap the sixth and the seventh digits;
     31 * $032867325$ if you swap the seventh and the eighth digits.
     32 
     33 Note, that you can't swap digits on positions $2$ and $4$ because the positions are not adjacent. Also, you can't swap digits on positions $3$ and $4$ because the digits have the same parity.
     34 
     35 You can perform any number (possibly, zero) of such operations.
     36 
     37 Find the minimum integer you can obtain.
     38 
     39 Note that the resulting integer also may contain leading zeros.
     40 
     41 Input
     42 
     43 The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
     44 
     45 The only line of each test case contains the integer $a$, its length $n$ is between $1$ and $3 \cdot 10^5$, inclusive.
     46 
     47 It is guaranteed that the sum of all values $n$ does not exceed $3 \cdot 10^5$.
     48 
     49 Output
     50 
     51 For each test case print line — the minimum integer you can obtain.
     52 
     53 Example
     54 
     55 Input
     56 
     57 ```
     58 3
     59 0709
     60 1337
     61 246432
     62 ```
     63 
     64 Output
     65 
     66 ```
     67 0079
     68 1337
     69 234642
     70 ```
     71 
     72 Note
     73 
     74 In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): $0 \underline{\textbf{70}} 9 \rightarrow 0079$.
     75 
     76 In the second test case, the initial integer is optimal.
     77 
     78 In the third test case you can perform the following sequence of operations: $246 \underline{\textbf{43}} 2 \rightarrow 24 \underline{\textbf{63}}42 \rightarrow 2 \underline{\textbf{43}} 642 \rightarrow 234642$.