【JZOJ100047】基因变异【BFS】

版权声明:若希望转载,在评论里直接说明即可,谢谢! https://blog.csdn.net/SSL_ZYC/article/details/85014800

题目大意:

题目链接:https://jzoj.net/senior/#main/show/100047
题目图片:
http://wx1.sinaimg.cn/mw690/0060lm7Tly1fy7gss4ijmj30j50cujrv.jpg
http://wx4.sinaimg.cn/mw690/0060lm7Tly1fy7gss4gyaj30j20asdfx.jpg
已知数组 a a ,两种操作:

  • x x 变成 x   x o r   a i x\ xor\ a_i
  • x x 二进制下任意一位取反。

求从 x x 变成 y y 的最少步数。


思路:

如果我们有一个数 z z 使得 x   x o r   z = y x\ xor\ z=y ,那么很明显的 z z 就是我们要求的答案。
反过来得 x   x o r   y = z x\ xor\ y=z
所以,其实 x x y y 的最少步数就是 0 0 z z 的最少步数!
那么就从 0 0 开始 B F S BFS ,求出不超过 z = m a x { 1111111 } z=max\{1111111\} 的答案(因为 x o r xor 最大就是 1111111 1111111 了)。然后就可以 O ( 1 ) O(1) 输出了。
思维好题。


代码:

#include <cstdio>
#include <queue>
#define ri register
using namespace std;

const int N=1200000;
int n,u,v,Q,a[N],s,t,ans[N];
bool vis[N];
char c;

int read()
{
	u=0;
	c=getchar();
	while (c<'0'||c>'9') c=getchar();
	while (c>='0'&&c<='9')
		u=(u<<3)+(u<<1)+c-48,c=getchar();
	return u;
}

int write(int x)
{
	if (x>9) write(x/10);
	putchar(x%10+48);
}

void bfs()
{
	queue<int> q;
	q.push(0);
	vis[0]=1;
	while (q.size())
	{
		u=q.front();
		q.pop();
		for (ri int i=1;i<=n;i++)  //选择每一个数xor
			if (!vis[u^a[i]])
			{
				vis[u^a[i]]=1;
				ans[u^a[i]]=ans[u]+1;
				q.push(u^a[i]);
			}
		for (ri int i=0;i<=19;i++)  //选择每一位取反
		{
			if ((u&(1<<i))==(1<<i)) v=u-(1<<i);
				else v=u+(1<<i);
			if (!vis[v])
			{
				vis[v]=1;
				ans[v]=ans[u]+1;
				q.push(v);
			}
		}
	}
}

int main()
{
	n=read();
	Q=read();
	for (ri int i=1;i<=n;i++)
		a[i]=read();
	bfs();
	while (Q--)
	{
		s=read();
		t=read();
		write(ans[s^t]);
		putchar(10);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/SSL_ZYC/article/details/85014800
今日推荐