## 【题解】P5462 X龙珠

(为了方便理解，按照顺序排序的set写得不够优美)

``````//@winlere
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>

using namespace std;  typedef long long ll;
inline int qr(){
register int ret=0,f=0;
register char c=getchar();
while(c<48||c>57)f|=c==45,c=getchar();
while(c>=48&&c<=57) ret=ret*10+c-48,c=getchar();
return f?-ret:ret;
}
typedef pair < int , int > node;
#define x first
#define y second
#define mk make_pair
set < node > s;
set < int ,greater < int > > s1;
int n;
const int maxn=1e5+5;
int arc[maxn];
vector < int > ve;
int main(){
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
//freopen("out.out","w",stdout);
#endif
n=qr();
for(register int t=1,c;t<=n;++t)
s.insert(mk(t,c=qr())),s1.insert(c),arc[c]=t;
for(register int t=1;t<=n>>1;++t){
for(auto it:s1){
auto ti=s.find(mk(arc[it],it));
if(ti!=s.end()&&++ti!=s.end()){
auto t1=*ti--;
auto t2=*ti;
s.erase(t1);
s.erase(t2);
ve.push_back(t2.second);
ve.push_back(t1.second);
s1.erase(t1.second);
s1.erase(t2.second);
break;

}
}
}
int cnt=0;
for(auto t:ve) printf("%d%c",t,++cnt==n?'\n':' ');
return 0;
}
``````