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:当前处理的位 ...
随机推荐
- iOS 部分问题总结2 - 苹果审核篇
iOS 部分问题总结(二) - 苹果审核篇 1. 记录下5.1新规后上传被拒的问题排查和解决过程. 几天前,最新一次的更新被拒了,提示Invaild Binary.好在苹果同时发来了说明邮件做了详细说 ...
- oracle之replace结合substr的使用
select * from( SELECT TMM.ORDER_ID, TMM.IMPORT_ID, TMM.TMALL_ORDER_ID, TMM.MEMBER_NAME, TMM.ALIPAY_U ...
- compass模块
Compass核心模块Reset :重置CSS模块 @import "compass/reset" Layout :页面布局的控制能力 @import "compass/ ...
- css布局详解(二)——标准流布局(Nomal flow)
css标准流布局(Nomal flow) 一.正常流 这是指西方语言中文本从左向右,从上向下显示,这也是我们熟悉的传统的HTML文档中的文本布局.注意,在非西方的语言中,流方向可能不同.大多数元素都在 ...
- WndPric的使用方法
protected override void WndProc(ref Message m) { const int WM_SYSCOMMAND = 0x0112; const int SC_CLOS ...
- 一个Banner广告收缩效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Mac_配置adb环境变量
1.打开终端Terminal. 2.进入HOME目录:命令行输入 echo(这之间有一个空格) $HOME 3.创建.bash_profile文件:命令行输入 touch(这之间有一个空格).bash ...
- oracle to_char()及to_date()函数使用
to_char(x[,format]) :将x转换成字符串,可以使用format参数来格式化字符串输出. to_date(x[,format]) :将字符串x转换成日期,可以使用format匹配要转换 ...
- DataTable转json字符串,jQuery.parseJSON()把json字符串转为标准的json对象格式
1.string res = DataTableToJson.DataTable2Json(dt);讲DataTable转换为json字符串 http://www.365mini.com/page/j ...
- 【1】Laravel5.1 安装
1.安装composer http://www.phpcomposer.com/ 这个是中文网址里边有教程,但是由于被墙的缘故,可以通过下边这个链接下载Windows安装包 http://docs.p ...