ZOJ 3870 Team Formation
来源:互联网 发布:域名可以干嘛 编辑:程序博客网 时间:2024/06/05 08:25
题目链接:http://icpc.moe/onlinejudge/showProblem.do?problemId=5518
题意:给出n个数字,从中任选两个数,问有多少种组合可以达到所选的两个数的异或运算值比这两个数都大。
题意很简单,对于异或运算的题目我们一般采取贪心的思想。
首先分析,怎么样才能使两个数的异或运算值比他们两个数都大,因为异或运算是1^1 = 0,1^0 = 1,0^0 = 0,我们想要达到增大的效果,那么肯定是运用1^0 = 1的性质,所以不难想到,如果两个数中小的那个数最高位的1在大的那个数中是0的话,那么两个数的异或运算值就会增大。
得到这个贪心策略之后我们第一步便是统计这n个数中每一个的最高位1所在的位置出现的次数,然后再遍历每一个数,每一个数满足条件的个数就是最高位在这个数所含0的位置上的个数和(有点绕口啊,小编没法讲的更精细了,大家仔细理解理解)。
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 100000+5;int a[maxn],bit[35];// bit[i]表示有多少个数的最高位的1在第i位上void solve(int x){int l = 31;while(l >= 0){if(x & (1<<l)){bit[l]++;return ;}l--;}}int main(){int T;scanf("%d",&T);while(T--){int n;scanf("%d",&n);memset(bit,0,sizeof(bit));for(int i=0; i<n; i++){scanf("%d",&a[i]);solve(a[i]);}int ans = 0;for(int i=0; i<n; i++){int l = 31;while(l >= 0){if(a[i] & (1<<l)) break;l--;}while(l >= 0){if(!(a[i] & (1<<l))) ans += bit[l];l--;}}printf("%d\n",ans);}}
0 0
- ZOJ 3870 Team Formation
- ZOJ 3870 Team Formation
- ZOJ 3870 Team Formation
- zoj #3870:Team Formation
- ZOJ 3870 Team Formation
- ZOJ-3870-Team Formation
- ZOJ-3870 Team Formation
- ZOJ 3870:Team Formation【技巧】
- ZOJ 3870 Team Formation(数学)
- ZOJ 3870 Team Formation(异或)
- [水+数学] zoj 3870 Team Formation
- ZOJ 3870 Team Formation(位运算)
- ZOJ 3870 - Team Formation(数学)
- zoj 3870 Team Formation (数位统计问题)
- ZOJ 3870 Team Formation (异或运算)
- ZOJ 3870 Team Formation 位运算
- zoj 3870 Team Formation 位运算
- ZOJ Team Formation 3870【位运算】
- C++ STL 方法求全排列
- LeetCode------Contains Duplicate
- sublime text3里搭建python
- ios 硬件 导航 - 百度地图SDK
- GridView的刷新
- ZOJ 3870 Team Formation
- Spring常用注解详解
- 阿里云解析动态公网IP
- 第二课 函数调用
- 网络病毒
- 纯C++代码创建并保存Excel文件
- HDU 1879 继续畅通工程
- 【SSH】——Hibernate(2)get和load的区别
- C语言学习一:vc++6环境搭配、学习资料准备