第二次写交互,题目还算简单,但是WA了三发,最后名次很差。
对于每一对数A、B来说,只需要知道A Mod B && B Mod A的值,便可以确定出AB中的某一个。
先将所有未确定的index放入队列中,然后到队列只剩一个数的时候,搜索一遍哪一个数没有被用过,给其赋值就结束了。
#include <bits/stdc++.h>
using namespace std;
const int Mn = 1e5+5;
int a[Mn];
int d;
int ans[Mn];
bool vis[Mn];
int main()
{
int x=0;
int n;scanf("%d",&n);
queue <int> q;
for(int i=1;i<=n;i++)q.push(i);
while(q.size()!=1){
int a,b;
int A,B;
A = q.front();
q.pop();
B = q.front();
q.pop();
int f = 0;
printf("? %d %d\n",A,B),fflush(stdout);
scanf("%d",&a);
printf("? %d %d\n",B,A),fflush(stdout);
scanf("%d",&b);
if(a > b){
ans[A] = a;
vis[a] = 1;
q.push(B);
}else{
ans[B] = b;
vis[b] = 1;
q.push(A);
}
}
int f;
for(int i = 1;i<=n;i++){
if(ans[i] == 0 ) f = i;
}
for(int i=1;i<=n;i++){
if(vis[i] == 0){
ans[f] = i;
}
}
printf("! "),fflush(stdout);
for(int i=1;i<=n;i++){
printf("%d ",ans[i]),fflush(stdout);
}
printf("\n"),fflush(stdout);
return 0;
}