TZOJ--4986: Team Formation(二进制枚举)

Posted anidlebrain

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TZOJ--4986: Team Formation(二进制枚举)相关的知识,希望对你有一定的参考价值。

4986: Team Formation 

时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte

描述

 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.

输入

 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 ithinteger denotes the skill level of ith student. Every integer will not exceed 109.

输出

For each case, print the answer in one line.

样例输入

2
3
1 2 3
5
1 2 3 4 5

样例输出

1

6

题目来源

ZJCPC 2015

题目上传者

crq

 

题目链接:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=4986

 

题目大意:给你n个数字,问 满足(a^b)>max(a,b)的有多少对?

异或:相同为0不同为1,所以只需要枚举二进制中最高位的1所在位置,然后枚举这个数二进制中0所在的位置,计算出这个位置其他数字中是最高位的数字有几个即可。

 

#include<stdio.h>
#include<string.h>
int main()
{
	int t,n,a[100010],b[33];
	scanf("%d",&t);
	while(t--){
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		int x,i,j;
		scanf("%d",&n);
		for(i=0;i<n;i++){
			scanf("%d",&a[i]);
			for(j=31;j>=0;j--){
				if(a[i]&(1<<j)){
					b[j]++;
					break;
				}
			}
		}
		int s=0;
		for(i=0;i<n;i++){
			for(j=31;j>=0;j--){
				if(a[i]&(1<<j))break;
			}
			for(;j>=0;j--){
				if(!(a[i]&(1<<j))){
					s+=b[j];
				}
			}
		}
		printf("%d
",s);
	}
	return 0;
}

  






以上是关于TZOJ--4986: Team Formation(二进制枚举)的主要内容,如果未能解决你的问题,请参考以下文章

group和team的区别

如何展平一组对象? [复制]

rediect_to @team 时出现的问题

Team Foundation 无法从 Team Foundation Server 检索团队项目列表

team和group有何不同?

Scrum Team不等于Development Team——《Scrum指南》重读有感