Language: C++

/*
Though leaves are many , the root is one.
Through all the lying days of my youth
I swayed my leaves and flowers in the sun.
Now I may wither into the truth.
	  	  	- William Butler Yeats
*/
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
//#pragma GCC target("avx,tune=native")
#include<bits/stdc++.h>
#pragma comment("-Wl,--stack=1024000000")
//#include<ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
const int inf=0x3f3f3f3f;
const double eps=1e-6;
const int mod=1000000007;
typedef long long ll;
#ifndef LOCAL
#define cerr if(0)cout
#define eprintf(...) 0
#else
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#endif
inline string getstr(string &s,int l,int r){string ret="";for(int i=l;i<=r;i++)ret.push_back(s[i]);return ret;}
int modpow(int x,int y,int md=mod){if(y==0)return 1;int ret=modpow(x,y>>1,md);ret=(ll)ret*ret%md;if(y&1)ret=(ll)ret*x%md;return ret;}
int n;
int ban[100005];
bool check(vector<int>ans){
	for(int i=0;i+1<ans.size();i++){
		if(ban[ans[i]]==ans[i+1])return false;
	}
	return true;
}
map<int,int>cnt;
set<int>have;
vector<int>ans;
void del(int x){
	cnt[ban[x]]--;if(!cnt[ban[x]])cnt.erase(ban[x]);have.erase(x);
}
bool can_put(int x){
	if(ans.size()&&ban[ans.back()]==x)return false;
	have.erase(x);cnt[ban[x]]--;if(!cnt[ban[x]])cnt.erase(ban[x]);
	bool ret=cnt.size()==2&&((cnt.begin()->second==1&&have.count(cnt.rbegin()->first)||(cnt.rbegin()->second==1&&have.count(cnt.begin()->first));
	cnt[ban[x]]++;have.insert(x);
	return 1^ret;
}
int perm[6];
int main(){
	cin>>n;
	for(int i=1;i<=n;i++)cin>>ban[i];
	for(int i=1;i<=n;i++)cnt[ban[i]]++;
	for(int i=1;i<=n;i++)have.insert(i);
	for(int i=1;i<=n-5;i++){
		if(can_put(*have.begin())){
			ans.push_back(*have.begin());
			del(*have.begin());
		}else{
			ans.push_back(*next(have.begin()));
			del(*next(have.begin()));
		}
	}
	for(auto x:ans)cerr<<x<<" ";cerr<<endl;
	const int sz=min(n,5);
	const int base=max(0,n-5);
	for(int i=1;i<=sz;i++)perm[i]=i;
	vector<int>vec(have.begin(),have.end());
	vector<int>pre=ans;for(int i=1;i<=sz;i++)ans.push_back(n+1);
	do{
		vector<int>tmp=pre;
		for(int i=1;i<=sz;i++)tmp.push_back(vec[perm[i]-1]);
		if(check(tmp))ans=min(ans,tmp);
	}while(next_permutation(perm+1,perm+sz+1));
	if(ans.back()==n+1){
		cout<<-1<<endl;
		return 0;
	}
	for(auto x:ans)cout<<x<<" ";cout<<endl;
	return 0;
}

Compiler output:
a.cc: In function 'bool can_put(int)':
a.cc:45:149: error: expected ')' before ';' token
   45 |         bool ret=cnt.size()==2&&((cnt.begin()->second==1&&have.count(cnt.rbegin()->first)||(cnt.rbegin()->second==1&&have.count(cnt.begin()->first));
      |                                  ~                                                                                                                  ^
      |                                                                                                                                                     )
a.cc:45:149: error: expected ')' before ';' token
   45 |         bool ret=cnt.size()==2&&((cnt.begin()->second==1&&have.count(cnt.rbegin()->first)||(cnt.rbegin()->second==1&&have.count(cnt.begin()->first));
      |                                 ~                                                                                                                   ^
      |                                                                                                                                                     )