可比较的排列
场上很自然的思路。
假设我们已经确定了第一个
接下来考虑如何确定这个
刻画完
实现有点细节,但不是很多。
::::info[Code & Submission] submission
// Problem: C. Comparable Permutations
// Contest: Codeforces - Codeforces Round 1073 (Div. 1)
// URL: https://codeforces.com/contest/2190/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// Author: 3a51
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define _rep(i,a,b) for(int i=(a);i>=(b);i--)
#define subset(i,x) for(int i=(x);i;i=((i-1)&x))
#define pi pair<int,int>
#define arr(a) array<int,(a)>
#define F fflush(stdout)
#define RE return puts("! -1"),F,void()
#define cases(_) while((_)--) violetwindchime();
#define y0 hyacine
#define y1 hyacinthia
using namespace std;
const int mod=998244353;
inline void ckmin(int& x,int y){x=min(x,y);}
inline void ckmax(int& x,int y){x=max(x,y);}
inline void ckadd(int& x,int y){x=(x%mod+y%mod+mod)%mod;}
inline void ckmul(int& x,int y){x=(x%mod)*(y%mod)%mod;}
const int N=20005;
int _=1,n,tmp,ans[N];
inline int ask(int x,int y){
printf("? %d %d\n",x,y),F;
scanf("%d",&tmp);
return tmp;
}
void violetwindchime(){
scanf("%d",&n);
int lst=ask(n-1,n),pos=0,pos2=0;
if(lst==0) pos=n;
_rep(i,n-1,1){
if(pos && lst==1){pos2=i;break;}
if(i!=1){
int t=ask(i-1,i);
if(!pos && !t && lst) pos=i;
lst=t;
}
}
if(!pos2) RE;
rep(i,1,pos2-1) ans[i]=i;
rep(i,pos+1,n) ans[i]=i;
int pos1=0;
_rep(i,pos,pos2+1) if(ask(pos2,i)){pos1=i;break;}
assert(pos1);
int pos3=0;
rep(i,pos,n) if(ask(pos2,i)){pos3=i;break;}
if(pos3 && !ask(pos1,pos3)) ans[pos2]=pos3;//,printf("%d\n",pos3);
else ans[pos2]=pos1;//,printf("%d\n",pos1);
int cur=pos2+1,l=pos,r=pos+1,flg=0,x=pos2;
while(cur<=n){
if(l==x-1 && ans[cur-1]==x) l=pos1-1,flg=1;
if(r==x+1 && pos3 && ans[cur-1]==x) r=pos3+1,flg=1;
if(l==pos1 && ans[pos2]==pos1) l=x;
if(r==pos3 && pos3 && ans[pos2]==pos3) r=x;
// printf("%d %d %d\n",l,r,cur);
if(l<pos2+1 && flg) ans[cur++]=r,r++;
else if(r>n) ans[cur++]=l,l--;
else{
int t1=ask(l,r);
if(t1) ans[cur++]=l,l--;
else ans[cur++]=r,r++;
}
}
printf("! ");
rep(i,1,n) printf("%d ",ans[i]);
puts(""),F;
}
signed main(){
scanf("%d",&_);
cases(_);
return 0;
}
::::