Codeforces 1010B - Rocket

链接:Codeforces 1010B - Rocket

大意:已知火箭到火星的距离为1-m之间,你可以询问电脑一个整数x,如果距离大于x,则电脑会返回1,小于x则返回-1,相等则返回0。但是由于电脑出了故障,它可能会返回错误的结果,其值为正确结果的相反数。但已知它的错误是有规律的,其周期为n,现在要求你在60次询问内得到正确的结果,即电脑返回0。(1<=m<=1e9,1<=n<=30,1<=x<=m)

思路:如果没有故障,则此题显然直接二分查询即可,但由于存在故障,我们首先应该排除故障。所以先做n次询问1,如果返回0则直接结束当前测试(此时结果就是1),否则,显然询问1得到的结果一定是1,则我们可以根据这n次询问的结果得到电脑故障的规律,再进行正常的二分查询即可。

代码:

#include<iostream>

using namespace std;

int ans[31];

void query(int l, int r, int ord, int len)
{
	if (l == r)
	{
		cout << l << endl;
		fflush(stdout);
		return;
	}
	int res;
	int m = (l + r) >> 1;
	cout << m << endl;
	fflush(stdout);
	cin >> res;
	if (res == 0)
		return;
	if (ans[ord%len] == -1)
		res = -res;
	if (res == -1)
		query(l, m, ord + 1, len);
	else
		query(m + 1, r, ord + 1, len);
}

int main()
{
	int m, n;
	while (cin >> m >> n)
	{
		bool flag = true;
		for (int i = 1; i <= n; i++)
		{
			cout << 1 << endl;
			fflush(stdout);
			cin >> ans[i];
			if (ans[i] == 0)
			{
				flag = false;
				break;
			}
			else if (ans[i] == -2)
				exit(0);
		}
		ans[0] = ans[n];
		if (flag)
			query(1, m, n + 1, n);
	}
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值