Codeforces Round #703 (Div. 2) C1/C2. Guessing the Greatest (easy/hard version)【二分】

题意:

  • 这是一道交互题。
  • 数组 a 有 n 个不同的数字,每一次我们可以向系统查询区间 [ l , r ] [l, r] [l,r]第二大的元素的位置(输出 " ? ? ? l l l r r r" 即可查询),其中 ( 1 ≤ l < r ≤ n ) (1 \leq l < r \leq n) (1l<rn),我们的目标是通过这样的交互最终求得数组中最大元素的位置 p,输出 “! p”。
  • easy 和 hard 版本的区别是允许查询的次数不同,分别允许查询 40 次和 20 次。
  • 2 ≤ n ≤ 1 0 5 2 \leq n \leq 10^5 2n105

思路:

  1. 从题目的允许查询次数来看,解决方案一般就是二分了。
  2. easy 版本:二分每次都要取中点,取完中点后我们需要确定下一步应该取左边区间还是取右边区间,也就转化为:确定最大元素是在左边区间还是右边区间。设我们在查询 [l, r] 后得到的第二大值的位置为 smax,我们可以再次查询区间 [l, smax],如果第二大值的位置仍然是 smax,那么最大值就在区间 [l, smax),如此查询两次就可以确定最大值在左区间还是右区间,最终的查询次数为 2 ⋅ ⌈ l o g 2 1 0 5 ⌉ = 34 2 \cdot \lceil log_210^5 \rceil = 34 2log2105=34
  3. hard 版本:我们可以先采用 easy 版本中的方法确定最大元素在 smax 左边还是右边,这耗费 2 次查询,假如最大值在左边那么我们可以使用二分的方法找出满足 q u e r y ( l , s m a x ) = s m a x query(l, smax) = smax query(l,smax)=smax 的最大的 l l l,这个 l l l 就是最大元素的位置,最终的查询次数为 2 + ⌈ l o g 2 1 0 5 ⌉ = 19 2 + \lceil log_2 10^5 \rceil = 19 2+log2105=19。注:要注意一种特殊情况,当 smax = 1 时,虽然满足了 q u e r y ( 1 , s m a x ) = s m a x query(1, smax) = smax query(1,smax)=smax,但是最大值应该在 smax 右边,因此要特判一下。

C1 AC Codes:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int query(int l, int r) {
    cout << "? " << l << " " << r << '\n';
    cout.flush();
    int x;
    cin >> x;
    return x;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int n;
    cin >> n;
    int l = 1, r = n;
    while (l < r) {
        int smax = query(l, r);
        if (l + 1 == r) {
            cout << "! " << (smax == l ? r : l) << '\n';
            cout.flush();
            break;
        }
        int mid = (l + r) >> 1;
        if (smax < mid) {
            if (query(l, mid) == smax) {
                r = mid;
            } else {
                l = mid + 1;
            }
        } else if (smax > mid) {
            if (query(mid, r) == smax) {
                l = mid;
            } else {
                r = mid - 1;
            }
        } else {
            if (query(l, mid) == smax) {
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        if (l == r) {
            cout << "! " << l << '\n';
            cout.flush();
            break;
        }
    }
    return 0;
}

C2 AC Codes:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int query(int l, int r) {
    cout << "? " << l << " " << r << '\n';
    cout.flush();
    int x;
    cin >> x;
    return x;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int n;
    cin >> n;
    int smax = query(1, n);
    if (n == 2) {
        cout << "! " << (smax == 1 ? 2 : 1) << '\n';
        cout.flush(); 
    } else if (smax != 1 && smax == query(1, smax)) {
        int l = 1, r = smax - 1;
        while (l + 1 < r) {
            int mid = (l + r) >> 1;
            if (query(mid, smax) == smax) {
                l = mid;
            } else {
                r = mid - 1;
            }
        }
        if (query(r, smax) == smax) {
            cout << "! " << r << '\n';
        } else {
            cout << "! " << l << '\n';
        }
        cout.flush();
    } else {
        int l = smax + 1, r = n;
        while (l + 1 < r) {
            int mid = (l + r) >> 1;
            if (query(smax, mid) == smax) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        if (query(smax, l) == smax) {
            cout << "! " << l << '\n';
        } else {
            cout << "! " << r << '\n';
        }
        cout.flush();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值