#include <bits/stdc++.h>
using namespace std;
int t, n;
map<int, long long int>a;
int mex(long long int x, long long int y)
{
if (x == 0 && y == 0)
{
return 1;
}
else if (x == 0 || y == 0)
{
return x + y == 1 ? 2 : 1;
}
else
{
return 0;
}
}
// 处理|S|=3时的情况
void check3()
{
vector<long long int> vec;
int num = 0;
for (int i = 1; i <= 3; i++)
{
if (a[i] > 0)
{
vec.push_back(a[i]);
num++;
}
}
if (num == 3 || num == 0)
{
cout << -1 << endl;
return;
}
if (num == 1)
{
cout << 0 << " " << 0 << endl;
cout << 1 << " " << vec[0] << endl;
return;
}
if (num == 2)
{
cout << 0 << " " << vec[0] << endl;
cout << mex(vec[0], 0) << " " << vec[1] << endl;
return;
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t --)
{
a.clear();
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
if (n == 1)
{
// 对于非0数字数目为1的情况
if (a[1] == 0) continue;
cout << -1 << endl;
continue;
}
else if (n == 2)
{
// 对于非0数字数目为2的情况
if (mex(a[1], a[2]) == 0)
{
cout << a[1] << " " << a[2] << endl;
continue;
}
cout << -1 << endl;
continue;
}
else if (n == 3)
{
check3();
continue;
}
else if (n > 3)
{
// 对于任何数据总在|S| = 4时可以化为S={0,0,k}, 或S={0,k,k}其中k为非0数,则有解;
// 故对于其他数据随意处理,在S=4时针对非零数字数目特殊处理
vector<long long int> vec;
int num = 0;
for (int i = n; i >= 5; i --)
{
cout << a[i] << " " << a[i - 1] << endl;
a[i - 1] = mex(a[i], a[i - 1]);
}
for (int i = 1; i <= 4; i ++)
{
if (a[i] > 0)
{
num ++;
vec.push_back(a[i]);
}
}
a[1] = 0;
a[2] = 0;
a[3] = 0;
if (num == 0)
{
cout << 0 << " " << 0 << endl;
a[3] = 1;
check3();
}
else if (num == 1)
{
cout << 0 << " " << 0 << endl;
a[3] = vec[0];
a[2] = 1;
check3();
}
else if (num == 2)
{
cout << 0 << " " << vec[0] << endl;
a[3] = mex(0, vec[0]);
a[2] = vec[1];
check3();
}
else if (num == 3)
{
cout << vec[0] << " " << vec[1] << endl;
a[3] = mex(vec[0], vec[1]);
a[2] = vec[2];
check3();
}
else if (num == 4)
{
cout << vec[0] << " " << vec[1] << endl;
a[3] = mex(vec[0], vec[1]);
a[2] = vec[2];
a[1] = vec[3];
check3();
}
}
}
return 0;
}123
这是 2026年2月12日 存档的第 1 版,不是当前版本。查看当前版本