C Elemental Decompress
- 题意
就是给一个数组a, 1 ≤ a [ i ] ≤ n 1\le a[i] \le n 1≤a[i]≤n。问你能否找到两个全排列,使得 m a x { p [ i ] , q [ i ] } = a [ i ] max\{p[i],q[i]\}=a[i] max{p[i],q[i]}=a[i]。 - 思路
一个数出现三次肯定不行,1出现两次肯定不行,剩下的用构造
- 代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
typedef long long ll;
const int mod = 1e9+9;
int n,m,x;
int a[maxn],b[maxn];
int hsha[maxn],hshb[maxn];
int hsh[maxn];
int main()
{
// set<int> test ;
// test.insert(1);
// auto item = test.lower_bound(0);
// if (item==test.begin())cout<<1<<endl;
// else cout<<2<<endl;
ios::sync_with_stdio(false);
int t;cin>>t;
while (t --) {
int n;cin>>n;int flag = false;
for (int i = 1;i <= n; ++i) {
hsh[i] = 0;hsha[i] = 0;hshb[i] = 0;
}
for (int i = 1;i <= n; i++) {
cin>>a[i];hsh[a[i]] ++;
if (hsh[a[i]]>=3) flag = true;
}
if (hsh[1]>=2) flag = true;
vector<int> ansa(n+1,0),ansb(n+1,0);
for (int i = 1;i <= n;i ++) {
if (!hsha[a[i]]){
ansa[i] = a[i];hsha[a[i]] = 1;
} else {
if (!hshb[a[i]]) {
ansb[i] = a[i];hshb[a[i]] = 1;
} else {
flag = true;break;
}
}
}
//cout<<":0 "<<flag<<endl;
set<int> sa,sb;
for (int i = 1;i <= n;i ++) {
if (!hsha[i]) sa.insert(i);
if (!hshb[i]) sb.insert(i);
}
//for (auto item:sa)cout<<item<<endl;
//for (auto item:sb)cout<<item<<endl;
//cout<<":1 "<<flag<<endl;
for (int i = 1;i <= n;i ++) {
if (!ansa[i]) {
auto item = sa.upper_bound(ansb[i]);
if (*sa.begin()>ansb[i]) {flag = true;break;}
item --;ansa[i] = *item;sa.erase(*item);
}
if (!ansb[i]) {
auto item = sb.upper_bound(ansa[i]);
//cout<<(*sb.end())<<endl;
if (*sb.begin()>ansa[i]){ flag = true;break;}
item--;ansb[i] = *item;sb.erase(*item);
}
}
for (int i = 1;i <= n;i ++) {
if (max(ansa[i],ansb[i])!=a[i]) {
flag = true;break;
}
}
if (flag) {cout<<"NO"<<endl;continue;}
cout<<"YES"<<endl;
for (int i = 1;i <= n;i ++) {
cout<<ansa[i]<<" ";
}cout<<endl;
for (int i = 1;i <= n;i ++) {
cout<<ansb[i]<<" ";
}cout<<endl;
}
return 0;
}
D. Lucky Permutation
- 题意