Codeforces Round #764 (Div. 3) F. Interacdive Problem (交互题、折半查找)

题目链接:https://codeforces.com/contest/1624/problem/F

题目大意
交互题,猜 1 1 1 n n n 中的一个数字 x x x
每次操作: + c + c +c,会使得 x = x + c x = x + c x=x+c,并返回 x / n ( 向 下 取 整 ) x / n(向下取整) x/n() 的结果。

思路
二分 x 的取值范围,如何 c h e c k check check
在第 i i i 次询问时,定义 s u m sum sum 表示前 i − 1 i-1 i1 次询问中 c c c 的和, x x x 表示初始值, s x sx sx 表示当前认为初始 x x x 的值。
c = n − ( s u m + s x ) % n c = n - (sum + sx) \% n c=n(sum+sx)%n,

  • 如果返回的结果大于等于 ( s u m + s x + c ) / n (sum + sx + c) / n (sum+sx+c)/n ,新的处置范围为 s x + 1 , r sx +1, r sx+1,r
  • 如果返回的结果小于 ( s u m + s x + c ) / n (sum + sx + c) / n (sum+sx+c)/n ,新的处置范围为 l , s x − 1 l, sx - 1 l,sx1

ACcode

#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;

const int maxn = 2e5 + 5;
const ll mod = 998244353;
const ll p = 233;

int check(int x){
	cout << "+ " << x << endl;
	int tmp;
	cin >> tmp;
	return tmp;
}
int main(){
	
	int n;
	cin >> n;
	
	int l = 1, r = n, res = -1;
	int sum = 0;
	while(l <= r){
		int sx = l + r >> 1; // 每次 x 的取值范围小一半
		int y = (sum + sx) % n;
		int c = n - y;
		int flag = (sum + sx + c) / n;
		int q = check(c); sum += c;
		if(q >= flag){
			res = sx;
			l = sx + 1;
		}
		else r = sx - 1;
	}
	
	cout << "! " << res + sum << endl;
	
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值