题解:P11490 [BalticOI 2023] Staring Contest
zzzyyyyhhhhh · · 题解
怎么没题解呢,那就发一篇吧。
首先考虑有 3 个位置,标号为
现在有前缀最大值
但是这样的构造操作次数是
所以现在次数为
注意要打乱序列。
#include<bits/stdc++.h>
using namespace std;
const int N = 2000;
int n,a[N],ans[N];
inline int ask(int x,int y)
{
int tmp;
cout<<"? "<<x<<' '<<y<<endl;
cin>>tmp;
return tmp;
}
int x,y,xx,yy,zz;
bool t;
inline void work(int z)
{
yy=ask(x,z);
if(yy<xx)
{
ans[z]=yy;
return;
}
zz=ask(y,z);
if(xx==yy)
ans[x]=xx,x=z,xx=zz;
else if(yy==zz)
ans[z]=yy;
else//xx==zz
ans[y]=xx,y=z,xx=yy;
}
mt19937 rd(time(0));
signed main()
{
cin>>n;
for(int i=1;i<=n;i++)a[i]=i;
if(n==2)
{
int tmp=ask(1,2);
ans[1]=ans[2]=tmp;
}
else
{
shuffle(a+1,a+1+n,rd);
x=a[1],y=a[2];
xx=ask(a[1],a[2]);
for(int i=3;i<=n;i++)
work(a[i]);
int tmp=max({xx,yy,zz});
ans[x]=ans[y]=tmp;
}
cout<<"! ";
for(int i=1;i<=n;i++)cout<<ans[i]<<' ';
cout<<endl;
}