ZOJ Problem Set - 3870【找规律】

来源:互联网 发布:图像模糊算法 编辑:程序博客网 时间:2024/05/16 19:52

ZOJ Problem Set - 3870
Team Formation

Time Limit: 3 Seconds      Memory Limit: 131072 KB

For an upcoming programming contest, Edward, the headmaster of Marjar University, is forming a two-man team fromN students of his university.

Edward knows the skill level of each student. He has found that if two students with skill levelA andB form a team, the skill level of the team will be AB, 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.AB > 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 containsN positive integers separated by spaces. Theith integer denotes the skill level ofith student. Every integer will not exceed 109.

Output

For each case, print the answer in one line.

Sample Input

231 2 351 2 3 4 5

Sample Output

16

打表找规律即可:规律:只要一个人能与m个人组成一个m个不同的队那么与这个人的值的二进制位数相同的人也能与相同的m个人组队

#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#define inf 0x3f3f3f3fusing namespace std;const int maxn=100010;struct Node{int num;int cnt;int ans;}A[maxn];int vis[maxn];bool cmp(Node a,Node b){if(a.cnt==b.cnt)return a.num<b.num;return a.cnt<b.cnt;}int main(){int t,n,i,j,k;scanf("%d",&t);while(t--){scanf("%d",&n);for(i=0;i<n;++i){scanf("%d",&A[i].num);int m=A[i].num;A[i].cnt=0;while(m){m/=2;A[i].cnt++;}}memset(vis,0,sizeof(vis));sort(A,A+n,cmp);int ans=0;for(k=0;k<n;++k){if(k&&A[k].cnt==A[k-1].cnt){ans+=A[k-1].ans;A[k].ans=A[k-1].ans;continue;}A[k].ans=0;for(i=k+1;i<n;++i){if((A[k].num^A[i].num)>max(A[k].num,A[i].num))A[k].ans++;}ans+=A[k].ans;}printf("%d\n",ans);}return 0;}

2016年5月1日更新

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<list>#include<queue>#include<vector>using namespace std;const int maxn=1000010;/*思路 当位数相同时一定不可以当位数不同时如果可以则位数均可 */struct Node{int num,cnt;}A[maxn];int bit[35];int main(){int t,n,k,cnt;scanf("%lld",&t);while(t--){scanf("%d",&n);memset(bit,0,sizeof(bit));for(int i=1;i<=n;++i){scanf("%d",&A[i].num);int temp=A[i].num,cnt=0;while(temp){temp/=2;cnt++;}bit[cnt]++;A[i].cnt=cnt;}long long ans=0;for(int i=1;i<=n;++i){for(int j=A[i].cnt-1;j>=1;--j){if((A[i].num^(1<<(j-1)))>A[i].num)ans+=bit[j];}}printf("%lld\n",ans);}return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 身份证号码变更后社保怎么办 社保与身份不符怎么办 年龄改了学籍怎么办 结婚证信息错误怎么办 六级身份证过期怎么办 身份号泄露了怎么办 身体证信息泄露怎么办 手机号被泄漏了怎么办 姓名和电话泄露怎么办 个人身份证信息泄露怎么办 身份号码泄露了怎么办 我身份证泄露了怎么办 身份证信息泄漏了怎么办 无锡身份证丢了怎么办 人在外地怎么办身份证 欠空放公司不还怎么办 兼职要身份证照片怎么办 身份证刷不了磁怎么办 身份证不能刷了怎么办 身份证指纹错了怎么办 指纹手机丢了怎么办 异地办理临时身份证怎么办 杭州办理外地身份证怎么办 办理身份证没有户口本怎么办 2018身份证掉了怎么办 双户口注销社保怎么办 常用户口被注销怎么办 刚到厦门怎么办身份证 新疆身份证丢了怎么办 技能证书丢了怎么办 16岁拍身份证怎么办 16岁以下怎么办身份证 办身份证要证明怎么办 15岁怎么办银行卡淘宝 满16岁怎么办银行卡 电脑最小化后不见了怎么办 满16周岁怎么办银行卡 给儿童办身份证怎么办 当兵前身份证号码不符怎么办 身份证后六位密码x怎么办 借了贷款人死了怎么办