二分,CF1624F - Interacdive Problem

目录

一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

二、解题报告

1、思路分析

2、复杂度

3、代码详解


一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

https://codeforces.com/contest/1624/problem/F


二、解题报告

1、思路分析

在交互的过程中,x会变,但是初始的x0只有一个,由于交互是我们控制,所以x0的变化的值我们也已知

我们记 交互过程中累加值为add,我们只要求出最初的x0,那么答案就是 x0 + add

那么如何二分?

闭区间二分 [1, n - 1]

x = (lo + hi + 1) / 2

令 q = n - (x + add) % n

如果 query(q) > last (last 为 上一次交互值),说明二分小了

否则二分大了

相应的收缩区间并且累加 add 即可

2、复杂度

时间复杂度: O(logn)空间复杂度:O(1)

3、代码详解

 ​
#include <bits/stdc++.h>

// #define DEBUG

using u32 = unsigned;
using i64 = long long;
using u64 = unsigned long long;

constexpr int inf32 = 1E9 + 7;
constexpr i64 inf64 = 1E18 + 7;

int query(int x) {
    std::cout << "+ " << x << std::endl;

    int res;
    std::cin >> res;

    return res;
}

void solve() {
    int n;
    std::cin >> n;

    int lo = 1, hi = n - 1;
    int add = 0, last = 0, ans = 1;
    while (lo <= hi) {
        int x = (lo + hi + 1) / 2;
        int q = n - ((add + x) % n);

        int res = query(q);
        if (res > last) ans = x, lo = x + 1;
        else hi = x - 1;

        add += q;
        last = res;
    }

    std::cout << "! " << ans + add << std::endl;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

#ifdef DEBUG
    int cur = clock();
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif

    int t = 1;
    // std::cin >> t;

    while (t--) {
        solve();
    }
#ifdef DEBUG
    std::cerr << "run-time: " << clock() - cur << '\n';
#endif
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EQUINOX1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值