题解:P11490 [BalticOI 2023] Staring Contest
交互,随机化。
题意
交互库里有一个长度为
思路
如果我们有三个数并且询问三次,显然可以确定三者之
2n
先考虑
n+2\ln n+O(1)
考虑优化,发现绝大多的数并不会参与最大和次大的更新,所以我们不用询问两次而是询问一次就足以确定,如果和最大和次大的其中一个询问后反馈的结果要小于次大值,那么一次操作就已经足够确定位置了。序列前缀最大值个数期望近似是
如果交互库给的数据是精心构造的(单调递增),那么它的操作次数紧逼
交互题不要关闭同步流!用 '\n' 后记得 fflush(stdout);。
::::success[代码]
#include<iostream>
#include<algorithm>
#include<random>
#include<numeric>
#include<chrono>
using namespace std;
const int N=1510;
int n,max1,max2,maxx,minn,a[N],order[N];
signed main()
{
cin>>n;
iota(order+1,order+n+1,1);
auto seed=chrono::system_clock::now().time_since_epoch().count();
mt19937 gen(seed);
shuffle(order+1,order+n+1,gen);
cout<<"? "<<order[1]<<' '<<order[2]<<'\n';
fflush(stdout);
max1=order[1];
max2=order[2];
cin>>maxx;
for(int i=3; i<=n; ++i)
{
cout<<"? "<<max1<<' '<<order[i]<<'\n';
fflush(stdout);
cin>>minn;
if(minn<maxx)a[order[i]]=minn;
else if(minn==maxx)
{
a[max1]=maxx;
max1=order[i];
cout<<"? "<<max1<<' '<<max2<<'\n';
fflush(stdout);
cin>>maxx;
}
else
{
a[max2]=maxx;
max2=max1;
max1=order[i];
maxx=minn;
}
}
a[max1]=a[max2]=maxx;
cout<<'!';
for(int i=1; i<=n; ++i)cout<<' '<<a[i];
cout<<'\n';
fflush(stdout);
return 0;
}
::::