目录
一、题目
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;
}