考虑以下两个询问:
设第一个询问返回的答案为 ,第二个询问返回的答案为 ,可以发现 即为原排列中子序列 的个数加上中间 的答案,第二个询问同理,设 的个数为 , 的个数为 ,则 。可以求出 ,那么我们就确定了 的位置,其他同理。
考虑询问以下序列:
我们发现相邻两个序列只有首尾不同,所以可以通过重复利用,达到 次询问,即可通过本题。
#include<iostream>
#include<algorithm>
#include<vector>
const int sz=4010;
void answer(std::vector<int>);
int publish(std::vector<int>);
int f[sz];
void solve(int N){
std::vector<int>P(N);
for(int i=1;i<=N;i++){
std::vector<int>R;
for(int j=i;j<=N;j++)R.push_back(j);
for(int j=1;j<i;j++)R.push_back(j);
f[i]=publish(R);
}
for(int i=1;i<=N;i++)P[(N+f[i]-f[i%N+1]-1)/2]=i;
answer(P);
}