C. Minimize The Integer(Educational Codeforces Round 75 (Rated for Div. 2))
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
Description
You are given a huge integer consisting of digits ( is between and , inclusive). It may contain leading zeros.
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 ).
For example, if you can get the following integers in a single operation:
- if you swap the first and the second digits;
- if you swap the second and the third digits;
- if you swap the fifth and the sixth digits;
- if you swap the sixth and the seventh digits;
- if you swap the seventh and the eighth digits.
Note, that you can’t swap digits on positions and because the positions are not adjacent. Also, you can’t swap digits on positions and because the digits have the same parity.
You can perform any number (possibly, zero) of such operations.
Find the minimum integer you can obtain.
Note that the resulting integer also may contain leading zeros.
Input
The first line contains one integer — the number of test cases in the input.
The only line of each test case contains the integer a, its length n is between and , inclusive.
It is guaranteed that the sum of all values n does not exceed 3⋅105.
Output
For each test case print line — the minimum integer you can obtain.
input
3
0709
1337
246432
output
0079
1337
234642
Note
In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): .
In the second test case, the initial integer is optimal.
In the third test case you can perform the following sequence of operations: .
answer
I wonder Why did not write. . .
If there is an odd next to an even number, then you can change and even next position, even-versa, so if a queue is only an odd number, the rest are all even-odd words can appear anywhere it wanted to go. That is to say the relative position between the odd and even can be set arbitrarily, but the odd-numbered and odd not change position, the relative position is fixed between the odd, even-versa.
So the sequence in the odd and even separated, and then merge the two sequences is the answer to a sequence in the lexicographical ordering minimum.
Code
#include <iostream>
#include <algorithm>
#include <vector>
#define maxn 300005
#define _for(i, a) for(int i = 0; i < (a); i++)
using namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
char a[maxn];
vector<int>b[2];
char ans[maxn * 2];
int al;
void init() {
_for(i, 2) b[i].clear();
al = 0;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//freopen("in.txt", "r", stdin);
int n;
cin >> n;
_for(q, n) {
init();
cin >> a;
for (int i = 0; a[i]; i++) {
int x = a[i] - '0';
b[x % 2].push_back(x);
}
vector<int>::iterator i = b[0].begin(), j = b[1].begin();
while (i != b[0].end() || j != b[1].end()) {
int tem = inf, f = -1;
if (i != b[0].end() && *i < tem) tem = *i, f = 0;
if (j != b[1].end() && *j < tem) tem = *j, f = 1;
ans[al++] = tem + '0';
(f ? j : i)++;
}
ans[al++] = '\0';
cout << ans << "\n";
}
return 0;
}
/*
*/