题解:AT_kupc2020_j Median Query
sunkuangzheng · · 题解
- 交互题,你需要猜一个
1 \sim n 的排列a 。可以进行以下两种询问:
- 询问 A:给出互不相同的
i,j,k ,询问a_i,a_j,a_k 的中位数。- 询问 B:给出
i,j ,询问a_i 和a_j 中值较小的那个下标。- 使用不超过
2n 次询问 A 和2 次询问 B 猜出排列。- 交互库自适应。
n \ge 4 。
不妨先考虑
进一步的,我们称下标对
我们的基本策略是,通过
考虑如果我们已知
- 进行 A 询问
(i,k,x) ,得到结果t 。- 如果
t < L ,说明\max(a_i,a_x) < L ,那么立得a_j = L 且\max(a_i,a_x) = t 。这样,我们知道了(i,x,k,l) 的基本信息。 - 如果
t = L ,那么一定有a_i = L 。为了得到(j,x,k,l) 的基本信息,我们需要知道新的L' ,应当有L' = \operatorname{median}(a_j,a_x,a_k) 。 - 如果
L < t < R ,那么显然a_x = t 。 -
- 如果
这样我们就通过不超过
A 类询问次数为
/**
* author: sunkuangzheng
* created: 28.08.2024 20:46:00
**/
#include<bits/stdc++.h>
#ifdef DEBUG_LOCAL
#include <mydebug/debug.h>
#endif
using ll = long long;
const int N = 5e5+5;
using namespace std;
int T,n,t,p[N]; vector<int> a; int L,R;
int ask(int a,int b,int c){return cout << "? 1 " << a << " " << b << " " << c << endl,cin >> t,t;}
int ask(int a,int b){return cout << "? 2 " << a << " " << b << endl,cin >> t,t;}
int main(){
ios::sync_with_stdio(0),cin.tie(0);
cin >> n,fill(p+1,p+n+1,-1);
a = {1,2,3,4};
vector<int> b = {ask(1,2,3),ask(1,2,4),ask(1,3,4),ask(2,3,4)};
for(int i = 0;i < 4;i ++) for(int j = i + 1;j < 4;j ++){
int id = 0; while(id == i || id == j) id ++;
if(b[i] == b[j] && b[i] > b[id]){
L = b[id],R = b[i];
if(i == 0 && j == 1) a = {3,4,1,2};
if(i == 0 && j == 2) a = {2,4,1,3};
if(i == 0 && j == 3) a = {1,4,2,3};
if(i == 1 && j == 2) a = {2,3,1,4};
if(i == 1 && j == 3) a = {1,3,2,4};
if(i == 2 && j == 3) a = {1,2,3,4};
}
}for(int i = 5;i <= n;i ++){
int t = ask(a[0],a[2],i);
if(t < L)
p[a[1]] = L,a[1] = i,L = t;
else if(t == L)
p[a[0]] = L,a[0] = i,L = ask(a[0],a[1],a[2]);
else if(L < t && t < R) p[i] = t;
else if(t == R)
p[a[2]] = R,a[2] = i,R = ask(a[1],a[2],a[3]);
else
p[a[3]] = R,a[3] = i,R = t;
}vector<int> val,vis(n + 1);
for(int i = 1;i <= n;i ++) if(p[i] != -1) vis[p[i]] = 1;
for(int i = 1;i <= n;i ++) if(!vis[i]) val.push_back(i);
if(ask(a[0],a[1]) == a[1]) swap(a[0],a[1]);
if(ask(a[2],a[3]) == a[3]) swap(a[2],a[3]);
for(int i = 0;i < 4;i ++) p[a[i]] = val[i];
cout << "! ";
for(int i = 1;i <= n;i ++) cout << p[i] << " ";
cout << endl;
}