zoukankan      html  css  js  c++  java
  • 题解 CF1292A 【NEKO's Maze Game】

    有一个结论:

    ((1,1)) 不能抵达 ((2,n)) 时,必定存在一个点对,这两个点的值均为真,且坐标中的 (x) 互异,(y) 的差 (leq 1)

    这个结论的正确性感觉非常显然,就不多说了。

    下图可以形象地解释点对的位置关系。

    那对于每个点的值,只要开一个数组 f[i][j] 记录一下即可。


    有了上述结论,我们记一个变量 (cnt) 表示 " 有多少对满足上述结论的点对 " ,则 (cnt=0) 时,((1,1)) 可以抵达 ((2,n)) ,反之不可抵达。重点在于如何维护 (cnt)

    对于每次反转的点 ((x,y)) ,我们都需要往 (cnt) 里 扣除 (/) 补上 ((x,y)) 的贡献,具体的:(为了方便异或 (x)(0)(1)

    (f[x][y]=1) ,令 (cnt-=f[x xor 1][y-1]+f[x xor 1][y]+f[x xor 1][y+1])(f[x][y]=0)

    (f[x][y]=0) ,令 (cnt+=f[x xor 1][y-1]+f[x xor 1][y]+f[x xor 1][y+1])(f[x][y]=1)

    这样就可以起到维护 (cnt) 的效果了,时间复杂度 (O(n))


    Code 部分

    #include<cstdio>
    
    #define RI rgeister int
    
    using namespace std;
    
    inline int read()
    {
    	int x=0,f=1;char s=getchar();
    	while(s<'0'||s>'9'){if(s=='-')f=-f;s=getchar();}
    	while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    	return x*f;
    }
    
    const int N=100100;
    
    int n,q;
    
    int f[2][N];
    
    int cnt;
    
    int main()
    {
    	n=read(),q=read();
    
    	while(q--)
    	{
    		int x=read()-1,y=read();
    		switch(f[x][y])
    		{
    			case 1:{
    				cnt-=f[x^1][y-1]+f[x^1][y]+f[x^1][y+1];
    				f[x][y]=0;
    				break;
    			}
    
    			case 0:{
    				cnt+=f[x^1][y-1]+f[x^1][y]+f[x^1][y+1];
    				f[x][y]=1;
    				break;
    			}
    		}
    
    		puts(!cnt?"Yes":"No");
    	}
    
    	return 0; 
    }
    

    [thanks for watching ]

  • 相关阅读:
    Java入门(6)方法
    重构改善已有代码
    .NET面试题系列(八)电商瞬时抢购的方案
    .NET面试题系列(七)IIS
    设计模式综述
    基于ELK5.1(ElasticSearch, Logstash, Kibana)的一次整合测试
    acl.go
    单机部署-consul
    baseFileWriter.go
    config.go
  • 原文地址:https://www.cnblogs.com/cjtcalc/p/12216571.html
Copyright © 2011-2022 走看看