Elemental Decompress CF1768C题解

题意很简单就是给你长度为n的序列a,让你构造二个p,q排列;

拿到这道题,首先考虑无解的情况?:

{\color{Red} 1.}如果a中的最大数大于n那么就无解,题目规避了这个问题

{\color{Red} 2.}如果a中{\color{Red}{\color{Blue} i} }出现了三次以上那么无解   \rightarrow {\color{Blue} i}最多只会在p,q中各个出现一次,即使取到max也最多只会出现二次

{\color{Red} 3.}考虑一个数如果只是需要出现一次那么只需要他自己和自己取最大就行,但是如果一个数出现二次那么问题就来了,怎么样让他保证能够取到二次?

{\color{Red} case}

需要他自己和另外二个比他小的数取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;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值