Hihocoder #1509 : 异或排序

#1509 : 异或排序

http://hihocoder.com/problemset/problem/1509

分析:

  如果a[i]和a[i+1]的相同的位,那么可以不用考虑了,找到它们第一个不同的位置,它限制了S的这个位置必须是多少。

  最后答案就是没有限制的位的随便选。如果一个位限制了两次,且数字不同,那么答案就是0。

代码:

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<iostream>
 6 #include<cctype>
 7 #include<set>
 8 #include<vector>
 9 #include<queue>
10 #include<map>
11 #define fi(s) freopen(s,"r",stdin);
12 #define fo(s) freopen(s,"w",stdout);
13 using namespace std;
14 typedef long long LL;
15 
16 inline LL read() {
17     LL x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
18     for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f;
19 }
20 
21 const int N = 100;
22 
23 LL a[N], b[N];
24 
25 int main() {
26     
27     int n = read();
28     for (int i=1; i<=n; ++i) a[i] = read();
29     
30     memset(b, -1, sizeof(b));
31     
32     for (int i=1; i<n; ++i) {
33         for (int j=59; j>=0; --j) {
34             int t1 = (a[i] >> j) & 1, t2 = (a[i + 1] >> j) & 1;
35             if (!t1 && t2) { // 0 1
36                 if (b[j] == -1 || b[j] == 0) { b[j] = 0; break; }
37                 else { cout << 0; return 0; }
38             }
39             if (t1 && !t2) { // 1 0
40                 if (b[j] == -1 || b[j] == 1) { b[j] = 1; break; }
41                 else { cout << 0; return 0; }
42             }
43         }
44     }
45     
46     LL cnt = 0;
47     for (int j=59; j>=0; --j) 
48         if (b[j] == -1) cnt ++;
49     cout << (1ll << cnt);
50     return 0;
51 }

猜你喜欢

转载自www.cnblogs.com/mjtcn/p/9759289.html
今日推荐