题解:P14007 查询游戏
简述一下题面,就是可以查询
首先花费
我们记第
然后考虑对这
考虑拓宽到多个区间。
定义
我们不妨设答案为区间
- 若
b_{y+1}=b_{i} ,那么f(x,y) 的状态肯定和y+1 相反,这个时候,我们比较f(x,y) 和f(y+1,i) 的大小即查询区间[l_x,r_i] 和的状态,根据状态取更优的区间; - 若
b_{y+1}\ne b_i ,那么f(x,y) 的状态和i 一样,只需要查询[l_{y+1},r_i] 的状态,判断加上区间[l_{y+1},r_i] 是否对答案更优,如果更优就加上,否则不变。
最后回答区间
#include <bits/stdc++.h>
#define lint long long
using namespace std;
const int N = 1e5+5;
struct node{
bool b;
int l,r;
}v[N];
int cnt;
int n;
bool b[N];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cout << "? " << i << " " << i << endl;
cin >> b[i];
}
int last = 1;
for(int i = 2; i <= n; i++){
if(b[i] != b[i-1]){
v[++cnt] = (node){b[i-1],last,i-1};
last = i;
}
}
v[++cnt] = (node){b[n],last,n};
int l = 1,r = 1;
bool input;
for(int i = 2; i <= cnt; i++){
if(v[r+1].b == v[i].b){
cout << "? " << v[l].l << " " << v[i].r << endl;
cin >> input;
if(v[l].b != input) l = r+1,r = i;
}else{
cout << "? " << v[r+1].l << " " << v[i].r << endl;
cin >> input;
if(v[l].b == input) r = i;
}
}
cout << "! " << v[l].l << " " << v[r].r << endl;
return 0;
}