Problem Description
For  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(, )=,f(, )=, f(, )=. Now given  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 ( < T ≤ ), indicating the number of test cases. The first line of each test case contains  positive integers m and n ( < m, n ≤ ), indicating the numbers of integers of the  sets A and B, respectively. Then follow (m + n) lines, each of which contains a non-negative integers no larger than . 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.
 
Sample Input

 
Sample Output

 
Source
 
 
 
法一:求出b[i]、a[j]的二进制数后,再比较统计
 
 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stdlib.h>
#include<map>
using namespace std;
#define N 106
int n,m;
int a[N];
int b[N];
int s[];
int k1; void change(int x){
memset(s,,sizeof(s));
k1=; while(x){
s[k1++]=x%;
x/=;
} }
int s1[];
int k2;
void change1(int x){
memset(s1,,sizeof(s1));
k2=; while(x){
s1[k2++]=x%;
x/=;
} }
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
scanf("%d",&a[i]);
}
for(int i=;i<m;i++){
scanf("%d",&b[i]);
}
sort(a,a+n);
for(int i=;i<m;i++){
change(b[i]);
int minn=;
int f=;
for(int j=;j<n;j++){
change1(a[j]);
int ans=;
int q=max(k1,k2);
for(int w=;w<q;w++){
if(s[w]!=s1[w]){
ans++;
}
}
if(ans<minn){
minn=ans;
f=j;
}
}
printf("%d\n",a[f]);
}
}
return ;
}

法二:先求b[i]^a[j],再一次性统计

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
#define ll long long
#define N 50006
int a[];
int b[];
int n,m;
int s1[];
int solve(int x)
{
int k1=;
while(x)
{
s1[k1++]=x%;
x=x/;
}
int ans=;
for(int i=;i<k1;i++)
{
if(s1[i]==)
ans++;
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=;i<m;i++)
{
scanf("%d",&b[i]);
int minn=;
int flag;
for(int j=;j<n;j++)
{
int tmp=solve(b[i]^a[j]);
//printf("---%d\n",b[i]^a[j]);
//printf("%d\n",tmp);
if(minn>tmp)
{
minn=tmp;
flag=a[j];
}
else if(minn==tmp)
{
if(flag>a[j])
{
flag=a[j];
}
}
}
printf("%d\n",flag);
}
}
return ;
}

hdu 3711 Binary Number(暴力 模拟)的更多相关文章

  1. [HDU] 3711 Binary Number [位运算]

    Binary Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  2. HDU 3711 Binary Number

    Binary Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  3. 杭州电 3711 Binary Number

    Binary Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  4. HDU - 1711 A - Number Sequence(kmp

    HDU - 1711 A - Number Sequence   Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1 ...

  5. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  6. POJ 1013 小水题 暴力模拟

    Counterfeit Dollar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 35774   Accepted: 11 ...

  7. hihoCoder #1871 : Heshen's Account Book-字符串暴力模拟 自闭(getline()函数) (ACM-ICPC Asia Beijing Regional Contest 2018 Reproduction B) 2018 ICPC 北京区域赛现场赛B

    P2 : Heshen's Account Book Time Limit:1000ms Case Time Limit:1000ms Memory Limit:512MB Description H ...

  8. BNU 13024 . Fi Binary Number 数位dp/fibonacci数列

    B. Fi Binary Number     A Fi-binary number is a number that contains only 0 and 1. It does not conta ...

  9. hdu 5898 odd-even number 数位DP

    传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...

随机推荐

  1. JAVA并发实现二(线程中止)

    package com.subject01; public class InterruptDemo { public static void main(String[] args) { SimpleT ...

  2. tooltips弹出框制作

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  3. jQuery----blur()方法

    当元素失去焦点时发生 blur 事件. blur() 函数触发 blur 事件,或者如果设置了 function 参数,该函数也可规定当发生 blur 事件时执行的代码. 提示:早前,blur 事件仅 ...

  4. CentOS系统、Jdk、Tomcat安装实战

    CentOS系统.Jdk.Tomcat安装实战                                                     第一次接触Liunx系统,都说J2EE系统在Li ...

  5. Windows下命令行直接编译程序

    D:\> cl hello.cpp Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86 Cop ...

  6. React 入门最好的实例-TodoList

    React 的核心思想是:封装组件,各个组件维护自己的状态和 UI,当状态变更,自动重新渲染整个组件. 最近前端界闹的沸沸扬扬的技术当属react了,加上项目需要等等原因,自己也决定花些时间来好好认识 ...

  7. 查看哪些进程占用了SWAP分区?

    在日常管理中,我们经常会遇到swap分区使用比较多,那么导致是那些进程使用的呢,其实我们可以通过/proc/pid/下的smaps来获得.使用下面的命令可以列出所有进程占用的swap分区的大小,分别我 ...

  8. oracle to_char()及to_date()函数使用

    to_char(x[,format]) :将x转换成字符串,可以使用format参数来格式化字符串输出. to_date(x[,format]) :将字符串x转换成日期,可以使用format匹配要转换 ...

  9. gitosis随记

    0.创建git用户 useradd -m git passwd git 1.安装脚本工具(gitosis依赖python) apt-get install python-setuptools 2.gi ...

  10. BZOJ2739 最远点(分治 + 决策单调性)

    2739: 最远点 Time Limit: 20 Sec Memory Limit: 256 MB Description 给你一个N个点的凸多边形,求离每一个点最远的点. Input 本题有多组数据 ...