hdu 3711 Binary Number(暴力 模拟)
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.
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.
For each test case you should output n lines, each of which contains the result for each query in a single line.
#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(暴力 模拟)的更多相关文章
- [HDU] 3711 Binary Number [位运算]
Binary Number Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- HDU 3711 Binary Number
Binary Number Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- 杭州电 3711 Binary Number
Binary Number Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- 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 ...
- HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)
HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...
- POJ 1013 小水题 暴力模拟
Counterfeit Dollar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35774 Accepted: 11 ...
- 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 ...
- 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 ...
- hdu 5898 odd-even number 数位DP
传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...
随机推荐
- pyqt学习之列表管理器(网友提供)
# -*- coding: utf-8 -*- __author__ = 'Administrator' import sys from PyQt4.QtCore import * from PyQt ...
- Spark Yarn-cluster与Yarn-client
摘要 在Spark中,有Yarn-Client和Yarn-Cluster两种模式可以运行在Yarn上,通常Yarn-cluster适用于生产环境,而Yarn-Cluster更适用于交互,调试模式,以下 ...
- 基础总结篇之中的一个:Activity生命周期
子曰:溫故而知新,能够為師矣.<論語> 学习技术也一样,对于技术文档或者经典的技术书籍来说,指望看一遍就全然掌握,那基本不大可能,所以我们须要常常回过头再细致研读几遍,以领悟到作者的思想精 ...
- getInitParameter()
getInitParameter()方法是在GenericServlet接口中新定义的一个方法,用来调用初始化在web.xml中存放的参量.在web.xml配置文件中一个servlet中参量的初始 ...
- Linux查看系统状态及备份
1. 如何看当前Linux系统有几颗物理CPU和每颗CPU的核数?cat /proc/cpuinfo将CPU的总核数除以物理CPU的个数,得到每颗CPU的核数.2. 查看系统负载有两个常用的命令,是哪 ...
- CodeSmith使用总结--调用自定义方法
上一篇读取了一个表的内容,但是到了真正应用的时候还是不够用的,我们很容易可以对比出来,SQL里边的数据类型的定义和C#中有所不同,比如Saler--String,大写的String在C#中不是一个类型 ...
- 普通用户之间的ssh无密码访问设置方法
两台CentOS6.2服务器,客户端是node1,服务器是node2,先都用root用户配置,方法如下: 第一步:在客户端Node1:生成密匙对,我用的是rsa的密钥.使用命令 "ssh-k ...
- Javascript高级程序设计读书笔记(第三章)
第3章 基本概念 3.4 数据类型 5种简单数据类型:undefined.boolean.number.null.string. typeof操作符,能返回下列某个字符串:“undefined”.“b ...
- SqlDbHelper备份,做项目时方便应用(目前不太全,把自己项目中的逐渐转移过来)
****************************************** 这是官网新闻左侧类别那部分用到的 **************************************** ...
- 分页搜索查询sql
select * from (select t.*,rownum no from " + table + " t where scbj=0)where (no>(" ...