ZOJ3870 Team Formation————位运算&&思维

- 日理万妓 2022-04-23 23:58 220阅读 0赞

Team Formation

Time Limit: 2 Seconds Memory Limit: 131072 KB
For an upcoming programming contest, Edward, the headmaster of Marjar University, is forming a two-man team from N students of his university.

Edward knows the skill level of each student. He has found that if two students with skill level A and B form a team, the skill level of the team will be A ⊕ B, where ⊕ means bitwise exclusive or. A team will play well if and only if the skill level of the team is greater than the skill level of each team member (i.e. A ⊕ B > max{A, B}).

Edward wants to form a team that will play well in the contest. Please tell him the possible number of such teams. Two teams are considered different if there is at least one different team member.

Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains an integer N (2 <= N <= 100000), which indicates the number of student. The next line contains N positive integers separated by spaces. The ith integer denotes the skill level of ith student. Every integer will not exceed 10^9.

Output
For each case, print the answer in one line.

Sample Input
2
3
1 2 3
5
1 2 3 4 5
Sample Output
1
6
Author: LIN, Xi
Source: The 12th Zhejiang Provincial Collegiate Programming Contest


题目大意:给你n个数,让你找两个数组队,要求满足A⊕B>max(A,B),问有多少种方案

n是1e6,O(n^2)算法肯定不行,(训练的时候没想出来On的算法,所以肯定TLE了)

对于任意一个数字来说,我们都可以将它分解为二进制的形式:
比如说: ( 10 ) 10 = ( 1010 ) 2 , (10)_{10}=(1010)_2, (10)10=(1010)2,

/明天再写,困了,先把代码放这里/
好,对于x,y,我们假设x是二者比较大的,那么我们如果想要找到能够和x组队的人,那么它的值(y)应该小于等于x并且x⊕y>x
假设x是 ( 18 ) 10 (18)_{10} (18)10,那么它的二进制形式为 ( 10010 ) 2 (10010)_2 (10010)2
18的二进制形式中,最高位的1在第四个位置上(从右往左数,从0开始)
我们可以想到,对于任意小于等于18的数,该数最高位1的位置刚好在18二进制形式上0的位置上,那么他俩异或的结果肯定会大于18,比如

1 = 1; 18^1 = 10010 ^ 1 = 10011 = 19>1
8=1000; 18 ^ 8=10010 ^1000 =11010=26>18
13=1101;18 ^ 13 = 10010 ^ 1101 = 11111 = 31>18
15=1111 ;18 ^ 15 = 10010 ^ 1111 = 11101 = 29>18
…有很多

于是我们可以认为,对于任意x,任何小于等于x数的最高位1的位置刚好在x二进制形式上0的位置上
那么 x⊕y > x(max(x,y))

那么问题就转化为统计数字最高位1的位置及个数,然后再对每一个数从最高为往低位判断,如果第i位为0,那么 ans += 二进制形式上最高为刚好是第i位的个数(因为最高位肯定是1)

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int MAXN = 1e6+7;
  5. int a[MAXN],bit[66];
  6. void f(int n)//找出数字n在二进制下最高位的1
  7. {
  8. int l = 31;
  9. while(l>=0)
  10. {
  11. if(n&(1<<l))
  12. {
  13. bit[l]++;
  14. break;
  15. }
  16. l--;
  17. }
  18. }
  19. int main()
  20. {
  21. // freopen("../in.txt","r",stdin);
  22. // freopen("../out.txt","w",stdout);
  23. ios::sync_with_stdio(0);
  24. cin.tie(0);
  25. int t,n,x;
  26. cin>>t;
  27. while(t--)
  28. {
  29. cin>>n;
  30. memset(bit,0,sizeof(bit));
  31. ll ans = 0;
  32. for(int i=0;i<n;++i)
  33. {
  34. cin>>a[i];
  35. f(a[i]);
  36. }
  37. for(int i=0;i<n;++i)
  38. {
  39. int l = 31;
  40. while(l>=0)
  41. {
  42. if(a[i]&(1<<l)) break;
  43. l--;
  44. }
  45. while(l>=0)
  46. {
  47. if(~a[i]&(1<<l)) ans += bit[l];
  48. l--;
  49. }
  50. }
  51. cout<<ans<<endl;
  52. }
  53. return 0;
  54. }

发表评论

表情:
评论列表 (有 0 条评论,220人围观)

还没有评论,来说两句吧...

相关阅读

    相关 运算

    位运算 位与&,相同位数都是1,结果才是1,按位&&的意思。 a&a=a,a&0=0 位|,相同位数有一个是1,结果就是1,按位||的意思。 a|a=a,a|0=

    相关 运算

    一.逻辑运算符  1.& 位与运算   1) 运算规则  位与运算的实质是将参与运算的两个数据,按对应的二进制数逐位进行逻辑与运算。例如:int型常量4和

    相关 运算

    由于位运算直接对内存数据进行操作,不需要转成十进制,因此处理速度非常快。 按位与(Bitwise AND),运算符号为& a&b 的操作的结果:a、b中对应位同时为1,则对

    相关 运算

    基础知识 位运算是把数字用二进制表示之后,对每一位上0或1的运算。位运算总共只有5种运算:与、或、异或、左移和右移。对于异或操作,需要注意一下`0^0=0; 1^1=0;

    相关 运算

    位运算主要是直接操控 [二进制][Link 1] 时使用 ,主要目的是节约内存,使你的程序速度更快,还有就是对内存要求苛刻的地方使用,以下是一牛人总结的方法,分享一下:位运算应