题意很简单就是给你长度为n的序列a,让你构造二个p,q排列;
拿到这道题,首先考虑无解的情况?:
如果a中的最大数大于n那么就无解,题目规避了这个问题
如果a中出现了三次以上那么无解 最多只会在p,q中各个出现一次,即使取到max也最多只会出现二次
考虑一个数如果只是需要出现一次那么只需要他自己和自己取最大就行,但是如果一个数出现二次那么问题就来了,怎么样让他保证能够取到二次?
:
需要他自己和另外二个比他小的数取max,但是怎么求呢?就要按从小到大动态的构造,如果在构造过程中前面的没有剩余的,那么就直接无解。
构造过程:
从1到n构造,考虑上述几种情况,为了便于操作我们在出现一次的情况下我们就把自己用掉,保证不会剩余。用一个队列来记录1-n中出现为0的情况,这个时候我们就可以把他用于构造那些出现二次的数。
AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
using namespace std;
#define rall(x) (x).rbegin(), (x).rend()
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define sz(a) (int) (a).size()
void solved() {
int n;
cin>>n;
vector<int> a(n);
vector<int> cnt(n+1);
int maxn=0;
for(int i=0;i<n;i++){
cin>>a[i];
cnt[a[i]]++;
maxn=max(maxn,a[i]);
}
vector<PII> ans[maxn+1];
LL al=0;
queue<int> q;
for(int i=1;i<=maxn;i++){
if(cnt[i]>=3){
cout<<"NO"<<endl;
return ;
}
if(al==0&&cnt[i]>=2){
cout<<"NO"<<endl;
return ;
}
al+=2-cnt[i]*2;
if(cnt[i]==0){
q.push(i);
continue ;
}
if(cnt[i]==1){
ans[i].push_back({i,i});
}
else{
int last=q.front();
q.pop();
ans[i].push_back({i,last});
ans[i].push_back({last,i});
}
}
cout<<"YES"<<endl;
vector<int> b[2];
for(int i=0;i<n;i++){
b[0].push_back(ans[a[i]][cnt[a[i]]-1].first);
b[1].push_back(ans[a[i]][cnt[a[i]]-1].second);
cnt[a[i]]--;
}
for(auto x:b[0]) cout<<x<<" ";
cout<<endl;
for(auto x:b[1]) cout<<x<<" ";
cout<<endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;
cin >> t;
while (t--) {
solved();
}
return 0;
}