Luogu P14007 「florr IO Round 1」查询游戏 题解 [ 蓝 ] [ 交互 ]

查询游戏

原题做法是显然的,子段绝对值最大值可以转化为求出前缀和序列的最大值、最小值,然后两者作差即可。查询操作可以转化为询问前缀和序列中两个元素比大小。因为查询数 \(2n\),所以各扫一遍用擂台法求最大、最小值即可。

注意特判 Sub0 的 \(n = 1, 2\) 的情况:

  • \(n=1\),问前缀和序列中 \(P\)\(P_0, P_1\) 大小关系即可。
  • \(n=2\),问前缀和序列中 \(P\)\(P_0, P_1\)\(P_1, P_2\)\(P_0, P_2\) 大小关系即可。

考虑加强版,查询次数被限制在 \(\lfloor \dfrac{3n}{2}\rfloor\) 内的做法。有一个很实用的观察技巧:从小数据入手,通过小数据的情况覆盖大数据的情况

此题中 \(n = 1\) 时我们仅用 \(1\) 次比较就能分辨出他们的大小关系,所以对于原序列,我们将两个相邻的下标两两配对,像 \(n = 1\) 那样比较二者关系,把较大的放入 \(q1\) 中,小的放入 \(q2\) 中。其中 \(q1, q2\) 为两个队列。注意到 \(q1, q2\) 大小加起来只有 \(n\),所以直接打擂台扫过去,总查询次数为 \(\lfloor \dfrac{3n}{2}\rfloor\)

#include <bits/stdc++.h>
#define fi first
#define se second
#define eb(x) emplace_back(x)
#define pb(x) push_back(x)
#define lc(x) (tr[x].ls)
#define rc(x) (tr[x].rs)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
using pi = pair<int, int>;
int mxp = 0, mnp = 0, n;
bool res;
vector<int> mnv, mxv;
int main()
{
    cin >> n;
    if(n == 1)
    {
        cout << "! 1 1" << endl;
        return 0;
    }
    if(n == 2)
    {
        cout << "? 1 1" << endl;
        cin >> res;
        if(res == 1) mxp = 1;
        else mnp = 1;
        cout << "? " << mnp + 1 << " 2" << endl;
        cin >> res;
        if(res == 0) mnp = 2;
        cout << "? " << mxp + 1 << " 2" << endl;
        cin >> res;
        if(res == 1) mxp = 2;
        cout << "! " << min(mxp, mnp) + 1 << " " << max(mxp, mnp) << endl;
        return 0;
    }    
    for(int i = 1; i <= n; i += 2)
    {
        cout << "? " << i << " " << i << endl;
        cin >> res;
        if(res == 0) mnv.push_back(i), mxv.push_back(i - 1);
        else mxv.push_back(i), mnv.push_back(i - 1);
    }
    if((n + 1) & 1) mxv.push_back(n), mnv.push_back(n);
    mnp = mnv[0], mxp = mxv[0];
    for(int i = 1; i < mnv.size(); i++)
    {
        cout << "? " << mnp + 1 << " " << mnv[i] << endl;
        cin >> res;
        if(res == 0) mnp = mnv[i];
    }
    for(int i = 1; i < mxv.size(); i++)
    {
        cout << "? " << mxp + 1 << " " << mxv[i] << endl;
        cin >> res;
        if(res == 1) mxp = mxv[i];
    }    
    cout << "! " << min(mxp, mnp) + 1 << " " << max(mxp, mnp) << endl;
    return 0;
}
posted @ 2025-10-07 12:39  KS_Fszha  阅读(15)  评论(0)    收藏  举报