(二进制 异或)Team Formation --ZOJ --3870
链接:
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的更多相关文章
- ZOJ 3870 Team Formation 贪心二进制
B - Team Formation Description For an upcoming progr ...
- 位运算 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^ ...
- Zoj 3870——Team Formation——————【技巧,规律】
Team Formation Time Limit: 3 Seconds Memory Limit: 131072 KB For an upcoming programming contes ...
- ZOJ 3870:Team Formation(位运算&思维)
Team Formation Time Limit: 2 Seconds Memory Limit: 131072 KB For an upcoming programming contest, Ed ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...
- ZOJ3870 Team Formation
/** Author: Oliver ProblemId: ZOJ3870 Team Formation */ /* 思路 1.异或运算,使用^会爆,想到二进制: 2.我们可以试着从前往后模拟一位一位 ...
- Team Formation(思维)
Team Formation Time Limit: 3 Seconds Memory Limit: 131072 KB For an upcoming programming contes ...
- 2015 浙江省赛B Team Formation (技巧,动归)
Team Formation For an upcoming programming contest, Edward, the headmaster of Marjar University, is ...
- 第十二届浙江省大学生程序设计大赛-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 ...
随机推荐
- How to Pronounce INTERNATIONAL
How to Pronounce INTERNATIONAL Share Tweet Share Tagged With: Dropped T How do you pronounce this lo ...
- js正则表达使用实例
(1)替换掉htmlStr中所有的<font..>和</font> var htmlstr='<font color="#fff">ABC< ...
- jsp页面添加时间
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <body& ...
- maven向本地库添加jar包
mvn install:install-file -DgroupId=com.lowagie -DartifactId=itextasian -Dversion=1.0 -Dpackaging=jar ...
- java并发中的锁
java中的锁,最基本的是Lock接口. Lock接口中的方法,主要是: lock(): 获取锁,lock()方法会对Lock实例对象进行加锁,因此所有对该对象调用lock()方法的线程都会被阻塞,直 ...
- android笔记:BroadcastReceiver
android允许应用程序自由地发送和接收广播. 广播是通过Intent进行数据传递的.接收广播则通过Broadcast Receiver(广播接收器)实现. 广播分为:标准广播和有序广播. 标准广播 ...
- Dice
Dice http://acm.hdu.edu.cn/showproblem.php?pid=5012 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- ECMAScript6新特性之Reflect
一 Reflect.ownKeys()获取对象属性. 可枚举的.不可枚举的.自有的.继承的. let fruit = { '2' : 'mango', [Symbol.for('pink')] : ' ...
- MATLAB安装libsvm无法使用解决办法(转)
buaasuozi 这是原作者: 安装libsvm 不成功有可能是你的MATLAB版本或者是编译文件版本的问题,但是不要急着换其他版本....说不定就有别的解决办法呢 首先感谢Lin教授及其实验室提 ...
- day3:vcp考试
Q41. An administrator creates a custom ESXi firewall rule using an XML file, however the rules do no ...