D. MAD Interactive Problem
D. MAD Interactive Problem
我们可以进行如下操作:
① 从左到右将 \(i(1 \le i \le 2n)\) 放入 \(s\),然后查询 \(s\),如果答案是某个数字,那么 \(i\) 位置的答案就是 \(a[i]\) 的值,同时我们将此位置存入 \(t\) 数组中,我们花 \(2n\) 次操作获得 \(n\) 个位置的答案。
② \(t\) 数组剩下 \(n\) 个元素,恰好是已经排好的元素,里面的元素恰好是 \(1\) 到 \(n\) 只出现一次。此时我们将位置的位置依次查询,正好就可以查完,一共花 \(n\) 次操作。
点击查看代码
#include <bits/stdc++.h>
using namespace std;
int query(vector<int> s){
cout << "? " << s.size() << " ";
sort(s.begin(), s.end());
for(int _ : s){
cout << _ << " ";
} cout << endl;
int ans; cin >> ans; return ans;
}
void solve(){
int n;
cin >> n;
vector<int> a(2 * n + 1, -1);
vector<int> s, t;
for(int i = 1;i <= 2 * n;i++){
s.push_back(i);
int res = query(s);
if(res > 0){
a[i] = res;
s.pop_back();
t.push_back(i);
}
}
for(int i = 1;i <= 2 * n;i++){
if(a[i] == -1){
t.push_back(i);
a[i] = query(t);
t.pop_back();
}
}
cout << "! ";
for(int i = 1;i <= 2 * n;i++){
cout << a[i] << " ";
} cout << endl;
}
int main(){
//ios::sync_with_stdio(false);
//cin.tie(nullptr), cout.tie(nullptr);
int T = 1;
cin >> T;
while(T--){
solve();
}
return 0;
}