题解:P17854 [LBA-OI R6 B] 凌云沐雨
DaiZao
·
·
题解
赛时题解
给定长度 n 的未知排列 A,每次询问有序集合 P,交互库返回取出值离散化后的置换环个数,要还原 A。
询问等价于:把 L 按 A 值排好序、把待插入的 x 挂在最后,返回的环数就是 A_x 在这堆数里的排名。
所以每问一次就往有序序列里插进一个位置,插完 n-1 次,第 i 个位置的值就是 i。
::::info[算法设计]
把 L=(l_1,l_2,\dots,l_{m-1}) 维护成按 A 值递增的位置序列,起手 L=(1)。
每次问 P=(l_1,l_2,\dots,l_{m-1},x),记 A_x 在这 m 个数里排第 c 小。
前 c-1 个仍是最小的,排名等于下标,映射 i \to i。
至于第 c 到 m-1 个,被 A_x 挤成 i \to i+1;末尾这个的排名是 c,映射 m \to c 把链接回 c。
综上所述:
q=(1,2,\dots,c-1,\ c+1,c+2,\dots,m,\ c)
前 c-1 个是不动点,剩下一条 c \to c+1 \to \dots \to m \to c,环数就是 c。
从 L=(1) 起,对 x=2,3,\dots,n 各问一次,把 x 插到 L 的第 c 位。
::::
---
时间复杂度 $O(n^2)$。
空间复杂度 $O(n)$。
::::success[压行 AC Code `为什么要压行?`]
`No Copying.Self-Writing.`
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n,k;cin >> n >> k;
vector<int>L(1,1);
for(int x=2;x<=n;x++){
cout << "? " << L.size()+1;
for(int p:L){cout << ' ' << p;}cout << ' '<< x << endl;
int c;cin >> c;
L.insert(L.begin()+c-1,x);
}vector<int>a(n+1);
for(int i=0;i<n;i++){a[L[i]]=i+1;}cout << "! ";
for(int i=1;i<=n;i++){cout<<a[i]<<(i==n?'\n':' ');}cout.flush();
return 0;
}
```
::::