题目
题意:
你有一个由 0 , 1 , 2 0,1,2 0,1,2组成的 c i c_i ci序列, c i = ( a i + b i c_i=(a_i+b_i ci=(ai+bi所以求 m a x ( a , b ) max(a,b) max(a,b)的最小值是多少。
思路:
我们针对每一个位置分析:
- c i = 0 c_i=0 ci=0时,那么这个位置的最小肯定就是两个都是 0 0 0的了
- c i = 1 c_i=1 ci=1时,这个时候肯定要把最大的给其中的一个,所以一个是 1 1 1,一个是 0 0 0,所以就标记一下,之后如果出现 c i c_i ci的时候直接就可以往小的堆了。
- c i = 2 c_i=2 ci=2时,假如在之前有出现因为 1 1 1的标记的时候,那么直接把 2 2 2往小的堆,假如没有标记,那么只能用分成两个 1 1 1了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
typedef vector<int> vec;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) return ;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1:1;
ret = (c == '-') ? 0:(c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return ;
}
inline void out(int x) {
if (x > 9) out(x / 10);
putchar(x % 10 + '0');
}
int main() {
ios::sync_with_stdio(false);
int T;
cin >> T;
string s;
while (T--) {
int n;
string t = "", tt = "";
cin >> n >> s;
bool flag = false;
for (int i = 0; i < n; i++) {
if (s[i] == '0') {
t += '0';
tt += '0';
} else if (s[i] == '1') {
if (!flag) t += '1', tt += '0', flag = true;
else tt += '1', t += '0';
} else if (s[i] == '2') {
if (!flag) t += '1',tt += '1';
else tt += '2', t += '0';
}
}
cout <<t << endl << tt << endl;
}
return 0;
}