[codeforces1019B][二分法求函数零点]The hat

翻译

有一个大小为 n n n的圈,满足相邻的数绝对值差为 1 1 1 n n n为偶数
你每次可以询问一个位置上的数的数值
现在问是否存在一个位置 x x x满足 x x x x + n 2 x+\frac{n}{2} x+2n上的数是相等的
询问次数不超过60次

题解

第一场vp直接给我掉飞分了肯定
没有课内玩家日常gg
首先可以知道 n % 4 n\%4 n%4不为 0 0 0时是一定无解的
我们先定义 d [ i ] d[i] d[i] i i i位置上的数与他相对位置上的数的差
显然互为相对位置的 d d d是相反数
我们需要找到满足 d [ i ] = 0 d[i]=0 d[i]=0的位置 i i i
能够发现的是 d [ i ] d[i] d[i]每次会增加 2 , − 2 , 0 2,-2,0 2,2,0这些数,故我们如果一开始找到了一正一负,那么显然是可以直接二分找到零点的
具体就是每次让 l l l r r r所在位置的权值不同,即同样满足一正一负
直到权值为 0 0 0为止
回去要好好看数学书了…

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<ctime>
#include<map>
#include<bitset>
#include<set>
#define LL long long
#define mp(x,y) make_pair(x,y)
#define pll pair<long long,long long>
#define pii pair<int,int>
using namespace std;
inline int read()
{
	int f=1,x=0;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int stack[20];
inline void write(int x)
{
	if(x<0){putchar('-');x=-x;}
    if(!x){putchar('0');return;}
    int top=0;
    while(x)stack[++top]=x%10,x/=10;
    while(top)putchar(stack[top--]+'0');
}
inline void pr1(int x){write(x);putchar(' ');}
inline void pr2(int x){write(x);putchar('\n');}
int n;
int main()
{
	n=read();
	if(n%4)
	{
		puts("! -1");
		fflush(stdout);
	}
	else
	{
		int l=1,r=1+n/2;
		printf("? %d\n",l);
		fflush(stdout);
		int x=read();
		printf("? %d\n",r);
		fflush(stdout);
		int y=read();
		int u1=x-y,u2=y-x;
		while(u1!=u2)
		{
			int mid=(l+r)/2;
			printf("? %d\n",mid);
			fflush(stdout);
			int u=read();
			printf("? %d\n",mid+n/2);
			fflush(stdout);
			int v=read();
			int o=u-v;
			if(o==0)
			{
				printf("! %d\n",mid);
				fflush(stdout);
				return 0;	
			}
			if((o>0&&u2>0)||(o<0&&u2<0))r=mid,y=o;
			else l=mid,x=o;
		}
		printf("! %d\n",l);
		fflush(stdout);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值