http://codeforces.com/contest/1285/problem/D

从高位往低位看,如果某一位上全是一样的数字,则直接看下一位;如果某位上有0有1,表明这一位答案上一定有个1,但具体那个X是要取0还是取1,取决于后面的位数,由此可将数字按这一位的01分成两组,递归地解决问题。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int n;
 5 #define maxn 200011
 6 int a[maxn];
 7 
 8 int dfs(int x,int L,int R)
 9 {
10     if (x<0) return 0;
11     if (((a[R]>>x)&1)==0 || ((a[L]>>x)&1)==1) return dfs(x-1,L,R);
12     int mid=L; for (;mid<=R && ((a[mid]>>x)&1)==0;mid++);
13     return (1<<x)+min(dfs(x-1,L,mid-1),dfs(x-1,mid,R));
14 }
15 
16 int main()
17 {
18     scanf("%d",&n);
19     for (int i=1;i<=n;i++) scanf("%d",&a[i]);
20     sort(a+1,a+1+n);
21     printf("%d\n",dfs(29,1,n));
22     return 0;
23 }
View Code

相关文章:

  • 2022-12-23
  • 2021-11-21
  • 2021-11-17
  • 2022-01-11
  • 2022-03-06
  • 2021-09-11
  • 2022-02-26
猜你喜欢
  • 2021-05-19
  • 2022-12-23
  • 2021-10-07
  • 2022-01-17
  • 2022-02-17
  • 2021-06-18
  • 2022-12-23
相关资源
相似解决方案