Binary Number

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1475    Accepted Submission(s): 933

Problem Description
For 2 non-negative integers x and y, f(x, y) is defined as the number of different bits in the binary format of x and y. For example, f(2, 3)=1,f(0, 3)=2, f(5, 10)=4. Now given 2 sets of non-negative integers A and B, for each integer b in B, you should find an integer a in A such that f(a, b) is minimized. If there are more than one such integer in set A, choose the smallest one.
 
Input
The first line of the input is an integer T (0 < T ≤ 100), indicating the number of test cases. The first line of each test case contains 2 positive integers m and n (0 < m, n ≤ 100), indicating the numbers of integers of the 2 sets A and B, respectively. Then follow (m + n) lines, each of which contains a non-negative integers no larger than 1000000. The first m lines are the integers in set A and the other n lines are the integers in set B.
 
Output
For each test case you should output n lines, each of which contains the result for each query in a single line.
 

题解:定义函数f(x,y)表示非负整数x,y二进制对应位上不同数字个数。求在集合B中所有整数bi在集合A中找一个对应的数ai,使得f(ai,bi)最小,如果f(ai,bi)相等,使ai尽量小。

因为0 < m, n ≤ 100,所以直接O(nm)暴力扫一遍,直接ci=ai xor bi然后统计二进制ci上1的个数,求ci末尾是否为1直接判断ci是否为奇数即可,然后ci>>=1,右移一位。

代码:

 #include<cstdio>
#include<algorithm> const int INF=1e9+;
using namespace std; int main()
{
int T,i,j,p,a[],b[],minn,minx,num,m,n; scanf("%d",&T);
while(T--) {
scanf("%d%d",&m,&n);
for(int i=;i<m;i++) {
scanf("%d",&a[i]);
}
for(int i=;i<n;i++) {
scanf("%d",&b[i]);
} for(i=;i<n;i++)
{
minn=INF;minx=INF;
for(j=;j<m;j++) {
num=;
p=a[j] xor b[i];
while(p>) {
if(p%!=) num++;
p>>=;
}
if(num<minn) { minx=a[j];minn=num;}
else if(num==minn && a[j]<minx) minx=a[j];
} printf("%d\n",minx);
}
} return ;
}

最新文章

  1. YL-64 颜色传感器
  2. java io 输入输出流
  3. 字符串处理函数(strlen wcslen...)
  4. Html中value和name属性的作用
  5. php+sqlite cms
  6. Struts2基础学习(二)&mdash;Action
  7. Android动态加载技术(插件化技术)
  8. linux下修改时间戳
  9. 对比react和vue
  10. A strange lift HDU - 1548
  11. 【Kafka】Kafka-副本-分区设置-性能调优
  12. Excel分数、小数、身份证的录入
  13. 调试web api的工具 谷歌插件 Restlet
  14. [转]SpringMVC单文件上传、多文件上传、文件列表显示、文件下载
  15. web应用下的安全问题以及tomcat/nginx对应解决方法(持续更新、亲测可解决问题)
  16. java 静态导入
  17. 初识Docker和安装
  18. cocos lua 加密与解密 混淆 (版本号cocos3.4)
  19. 设置VS快捷代码片段
  20. August 17th 2017 Week 33rd Thursday

热门文章

  1. java 发送 http 请求
  2. Sublime Text 2 注册码
  3. ServletContext对象(每个工程只有一个此对象)
  4. POJ 1755 Triathlon
  5. 从客户端(txtNewsContent=&quot;&lt;hr /&gt;&quot;)中检测到有潜在危险的 Request.Form 值。怎么办呀?
  6. MySQL常用聚合函数
  7. Linux企业级项目实践之网络爬虫(14)——使用正则表达式抽取HTML正文和URL
  8. poj 2229 Ultra-QuickSort(树状数组求逆序数)
  9. 【Eclipse DDMS】 Can&#39;t bind to local 8600 for debugger
  10. 一台机器同时运行多个appium实例