(二进制 异或)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 ...
随机推荐
- Java 使用getClass().getResourceAsStream()方法获取资源
之前想获取一个资源文件做一些处理,使用getClass().getResourceAsStream()一直拿不到文件. 具体的用法. 1 InputStream is = this.getClass( ...
- Consul Session
consul馆提供session机制,可用于构建分布式锁. session作为节点,健康检查和key/value数据之间的绑定层. 它们旨在提供粒度锁定,并受到“The Chubby Lock Ser ...
- SSL原理分析
SSL协议的工作流程: 服务器认证阶段: 1)客户端向服务器发送一个开始信息“Hello”以便开始一个新的会话连接: 2)服务器根据客户的信息确定是否需要生成新的主密钥,如需要则 ...
- Appuim学习路-Appuim介绍
(学一个东西,总的知道它支持什么.为什么要选择它吧?所以我就去看github上的介绍了.发现大家介绍的来源大多来自于此) Appium是一个开源的自动化框架,是跨平台的,允许所有平台使用同一套AP ...
- 14.7.1&14.7.2
ArrayList <Object> list = new ArrayList<>(); //实例化ArrayList int i; int j; for(i = 1; i & ...
- 如何用INNO安装添加快捷启动方式到Win7的快速启动栏(超级任务栏)
问题:如何用INNO安装添加快捷启动方式到Win7的快速启动栏(超级任务栏) 在XP下,添加方式是直接把快捷方式复制到%appdata%\Microsoft\Internet Explorer\Qui ...
- CentOS 安装 Xamarin官方Mono
预先准备: 服务器可连入互联网 有yum 工具(没什么好说的,如果这个你没装,那重新装个系统吧,debian 等不要看这个,不一样的) wget 工具(可选) yum-uitl 工具包 导入Xamar ...
- db2 查看表空间使用率
1. 统计所有节点表空间使用率 select substr(TABLESPACE_NAME,1,20) as TBSPC_NAME,bigint(TOTAL_PAGES * PAGE_SIZE)/10 ...
- Ubuntu下安装VirtualBox并为其添加USB支持
1.下载VirtualBox软件包和USB支持包 下载网址均为官方网站(可在此查看其使用教程):https://www.virtualbox.org/wiki/Downloads (若下载各平台各版本 ...
- c++ 中的符号与关键字
符号按照符号的ASC码数值从小到达排列,关键字按照英文字母排序. & [38] 位运算:取地址:左值引用[指针.引用都是可以做类型转换的] #include <iostream> ...