可以想到无法随便加的原因是因为两数之和的绝对值可能会超过 。
发现如果选取序列中最大与最小的两数相加的绝对值一定不会超过 。
证明
反证法。
假设 。
当 时:
-
若 ,那么 ,与 矛盾;
-
若 ,那么 ,与 矛盾。
当 时:
- 若 ,那么 ,与 矛盾;
- 若 ,那么 ,与 矛盾。
综上,假设不成立,故 。
那么便可以先对序列排序,每一次把最大的和最小的加起来,再把新的数插入进序列里即可。注意插入的时候要二分,不然询问次数会超。
询问次数是 的,时间复杂度 。
Code
实现中 a 存储的是这个元素是第几个加入的。
#include<bits/stdc++.h>
#define For(a,b,c) for(int a=(int)b;a<=(int)(c);a++)
#define pb push_back
using namespace std;
#define Happy cout.flush();
constexpr int N=1e5+5,MOD=998244353;
struct I{
int num;
operator int()const{
return num;
} //隐式转化为 int
bool operator<(const I &b)const{
cout<<"? "<<num<<" "<<b.num<<'\n';
Happy
int k;
cin>>k;
return k;
}
};
int n,l,r;
vector<I> a;
int main(){
cin>>n;
For(i,1,n)
a.pb({i});
sort(a.begin(),a.end());
For(i,2,n){
cout<<"+ "<<a.front()<<" "<<a.back()<<'\n';
Happy
a.pop_back();
a.erase(a.begin());
int qwq;
cin>>qwq;
a.insert(lower_bound(a.begin(),a.end(),I{qwq}),I{qwq});
}
cout<<"!\n";
return 0;
}