链接:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3870

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#problem/I   (密码:0817)

题目大意:从n个数中取2个数,问有多少种方法取的两个数的异或大于两个数的最大数
思路:如果x的最高位i位是1,y的位是0,且y比x大,i不是y的最高位,异或后这一位变成1,且yi位以前的1也可以保存,则异或后肯定比两个数的最大值还大

先把数组用快排从大到小排一下序, 再把每个数转分别换为二进制,在二进制中是 0 的就在相应的位置加 1, 另外如果首位是 1 的话,就把 sum(最后求的值) 加上相应位置上的值

举个例子:

5

1 2 3 4 5

快排后是: 5 4 3 2 1

对应的二进制是:

0  0  0  0

1  0  1  b[1]++;

1  0  0  b[1]++,  b[0]++;

1  1  sum += b[1];

1  0  b[0]++,  sum += b[1];

1  sum += b[0];

比赛的时候,我没看这题,一直是队友们在做,我也不知道是什么意思,学长在结束给我们讲的时候,刚开始没懂什么意思,因为我不知道题意,没敢乱插话, 但是听他们说了一会儿,懂题意了,学长也很认真的讲了,知道了这题的思路,现在实现一下,是关于二进制的东西,接触的不多,觉得都很神奇,另外自己懂的太少了,还有一定要把深搜好好练练!!!

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std; #define N 100005 int cmp(int a, int b)
{
return a>b;
} int main()
{
int t;
scanf("%d", &t); while(t--)
{
int n, i, b[]={}, x[N]={}, sum=; scanf("%d", &n); for(i=; i<=n; i++)
scanf("%d", &x[i]); sort(x+, x+n+, cmp); for(i=; i<=n; i++)
{
int j=, a[]={};
while(x[i])
{
a[++j] = x[i]%;
x[i] /= ;
}
if(a[j]) sum += b[j]; while(j)
{
if(!a[j])
b[j]++;
j--;
}
} printf("%d\n", sum);
}
return ;
}

(二进制 异或)Team Formation --ZOJ --3870的更多相关文章

  1. ZOJ 3870 Team Formation 贪心二进制

                                                    B - Team Formation Description For an upcoming progr ...

  2. 位运算 ZOJ 3870 Team Formation

    题目传送门 /* 题意:找出符合 A^B > max (A, B) 的组数: 位运算:异或的性质,1^1=0, 1^0=1, 0^1=1, 0^0=0:与的性质:1^1=1, 1^0=0, 0^ ...

  3. Zoj 3870——Team Formation——————【技巧,规律】

    Team Formation Time Limit: 3 Seconds      Memory Limit: 131072 KB For an upcoming programming contes ...

  4. ZOJ 3870:Team Formation(位运算&思维)

    Team Formation Time Limit: 2 Seconds Memory Limit: 131072 KB For an upcoming programming contest, Ed ...

  5. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...

  6. ZOJ3870 Team Formation

    /** Author: Oliver ProblemId: ZOJ3870 Team Formation */ /* 思路 1.异或运算,使用^会爆,想到二进制: 2.我们可以试着从前往后模拟一位一位 ...

  7. Team Formation(思维)

    Team Formation Time Limit: 3 Seconds      Memory Limit: 131072 KB For an upcoming programming contes ...

  8. 2015 浙江省赛B Team Formation (技巧,动归)

    Team Formation For an upcoming programming contest, Edward, the headmaster of Marjar University, is ...

  9. 第十二届浙江省大学生程序设计大赛-Team Formation 分类: 比赛 2015-06-26 14:22 50人阅读 评论(0) 收藏

    Team Formation Time Limit: 3 Seconds Memory Limit: 131072 KB For an upcoming programming contest, Ed ...

随机推荐

  1. Nginx_status显示结果详解

    打开:http://aabb.com/nginx_status会有如下显示Active   connections: 2872server   accepts  handled requests294 ...

  2. SQL Server 2008用'sa'登录失败,启用'sa'登录的办法

    首先”为什么用sa登录不了,提示登录失败呢?" 当然,自己装SQL Server 2008的时候根本就没有用sa登录的方法,装数据库的时候是用windows身份登录的. 如果要启用用户名为“ ...

  3. struts2 模拟令牌机制防止表单重复提交

    web.xml: <?xml version="1.0" encoding="UTF-8"?><web-app version="3 ...

  4. python并行编程

    一.编程思想 并行编程的思想:分而治之,有两种模型 1.MapReduce:将任务划分为可并行的多个子任务,每个子任务完成后合并得到结果 例子:统计不同形状的个数. 先通过map进行映射到多个子任务, ...

  5. 【Java】JVM(二)、Java垃圾收集算法

    一.标记-清除算法 算法主要分为两个步骤 1. 标记: 遍历所有的 GC Roots, 然后标记所有可达对象为存活对象 2. 清除: 遍历堆中所有对象,然后将没有标记的对象清除. 存在不足: 1. 效 ...

  6. 基于JS的文本验证

    1,不能为空 <input type="text" onblur="if(this.value.replace(/^ +| +$/g,'')=='')alert(' ...

  7. Java工具类_随机生成任意长度的字符串【密码、验证码】

    import java.util.Random; public class PasswordCreate { /** * 获得密码 * @param len 密码长度 * @return */ pub ...

  8. Princess Principal(思维题)

    Princess Principal https://www.nowcoder.com/acm/contest/201/J 题目描述 阿尔比恩王国(the Albion Kingdom)潜伏着一群代号 ...

  9. UVa 1592 Database(巧用map)

    Peter studies the theory of relational databases. Table in the relational database consists of value ...

  10. 18-从n个数中选m个

    #include <iostream>using namespace std; int f(int n, int m){        if(n < m)         //这个条 ...