zoukankan      html  css  js  c++  java
  • [hihocoder1509][异或排序]

    hihocoder1509

    思路

    对于每两个数,从二进制的高位到低位考虑,发现,若前面一个的当前位是1,后面一个的当前位置是0,那么s的当前位置必须是1。反之,若前面是0,后面是1,那么s的当前位置必须是0。如果出现矛盾的情况则直接输出0。如果两个数高位已经可以判断出大小了,那么后面的位置随便选就可以了。

    代码

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    using namespace std;
    typedef long long ll;
    
    ll read() {
    	ll x=0,f=1;char c=getchar();
    	while(c<'0'||c>'9') {
    		if(c=='-') f=-1;
    		c=getchar();
    	}
    	while(c>='0'&&c<='9') {
    		x=x*10+c-'0';
    		c=getchar();
    	}
    	return x*f;
    }
    ll a[30];
    int b[100];
    int main() {
    	int n=read();
    	for(int i=1;i<=n;++i)
    		a[i]=read();
    	memset(b,-1,sizeof(b));
    	for(int i=2;i<=n;++i) {
    		for(int j=59;j>=0;--j) {
    			int lst=(a[i-1]>>j)&1,now=(a[i]>>j)&1;
    			if(lst==0&&now==1) {
    				if(b[j]==-1||b[j]==0) { b[j]=0; break; }
    				else { puts("0"); return 0; }
    			}
    			if(lst==1&&now==0) {
    				if(b[j]==-1||b[j]==1) { b[j]=1; break; }
    				else { puts("0"); return 0; }
    			}
    		}
    	}
    	ll ans=1;
    	for(int i=0;i<=59;++i) {
    		if(b[i]==-1) ans<<=1;
    	}
    	cout<<ans;
    	return 0;
    }
    
  • 相关阅读:
    腾讯精品课程
    什么都没有,先空出来!
    Python3-super().__init()__
    Python3-模块
    Python3-单例模式
    Python3-私有化修改
    Python3-私有属性
    Python3-面向对象-魔术方法
    Python3-面向对象-四种方法
    Python3-迭代器
  • 原文地址:https://www.cnblogs.com/wxyww/p/9763106.html
Copyright © 2011-2022 走看看