HDU4712+随机算法
随机算法
求n个20位的2进制串的MinDist。
Dist:两个串的异或结果中1的个数
/*
随机算法
*/
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<queue>
#include<stack>
#include<time.h>
#include<map>
#include<set>
using namespace std;
typedef long long int64;
//typedef __int64 int64;
typedef pair<int64,int64> PII;
#define MP(a,b) make_pair((a),(b))
const int inf = 0x3f3f3f3f;
const double pi=acos(-1.0);
const int dx[]={1,-1,0,0};
const int dy[]={0,0,1,-1};
const double eps = 1e-8;
const int maxm = 100000+10;
const int maxn = 105; char str[ maxm ][ 6 ];
int num1[ 22 ],num2[ 22 ]; int GetId( char aim ){
int ans ;
if( aim>='0'&&aim<='9' ) ans = aim - '0';
else if( aim>='A'&&aim<='Z' ) ans = aim - 'A' + 10;
return ans;
} int solve( int x,int y ){
int cnt = 0;
int cc ;
for( int i=0;i<5;i++ ){
int aim = GetId( str[x][i] );
cc = 4;
while( cc ){
num1[ cnt++ ] = aim%2;
aim /= 2;
cc -- ;
}
}
//printf("cnt = %d\n",cnt);
cnt = 0;
for( int i=0;i<5;i++ ){
int aim = GetId( str[y][i] );
cc = 4;
while( cc ){
num2[ cnt++ ] = aim%2;
aim /= 2;
cc -- ;
}
}
//printf("cnt = %d\n",cnt);
cnt = 0;
for( int i=0;i<20;i++ ){
if( num1[i]!=num2[i] )
cnt ++ ;
}
return cnt ;
} int main(){
int T;
scanf("%d",&T);
while( T-- ){
int n;
scanf("%d",&n);
for( int i=0;i<n;i++ )
scanf("%s",str[i]);
srand( time(NULL) );
int ans = inf;
int x,y;
for( int i=0;i<180000;i++ ){
x = rand()%n;
y = rand()%n;
if( x==y ) continue;
int temp = solve( x,y );
if( temp<ans )
ans = temp;
}
printf("%d\n",ans);
}
return 0;
}
HDU4712+随机算法的更多相关文章
- 微信红包中使用的技术:AA收款+随机算法
除夕夜你领到红包了吗?有的说“我领了好几K!”“我领了几W!” 土豪何其多,苦逼也不少!有的说“我出来工作了,没压岁钱了,还要发红包”.那您有去抢微信红包吗?微信群中抢“新年红包”春节爆红.618微信 ...
- POJ 3318 Matrix Multiplication(随机算法)
题目链接 随机算法使劲水...srand((unsigned)time(0))比srand(NULL)靠谱很多,可能是更加随机. #include <cstdio> #include &l ...
- 抽奖随机算法的技术探讨与C#实现
一.模拟客户需求 1.1 客户A需求:要求每次都按照下图的概率随机,数量不限,每个用户只能抽一次,抽奖结果的分布与抽奖概率近似. 1.2 客户B需求:固定奖项10个,抽奖次数不限,每个用户只能抽一次, ...
- hdu 4712 (随机算法)
第一次听说随机算法,在给的n组数据间随机取两个组比较,当随机次数达到一定量时,答案就出来了. #include<stdio.h> #include<stdlib.h> #inc ...
- 权重随机算法的java实现
一.概述 平时,经常会遇到权重随机算法,从不同权重的N个元素中随机选择一个,并使得总体选择结果是按照权重分布的.如广告投放.负载均衡等. 如有4个元素A.B.C.D,权重分别为1.2.3.4,随机结果 ...
- hdu 4712 Hamming Distance ( 随机算法混过了 )
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- LOJ #2540. 「PKUWC 2018」随机算法(概率dp)
题意 LOJ #2540. 「PKUWC 2018」随机算法 题解 朴素的就是 \(O(n3^n)\) dp 写了一下有 \(50pts\) ... 大概就是每个点有三个状态 , 考虑了但不在独立集中 ...
- java 权重随机算法实现
import java.util.*; /** * 权重随机算法实现 * a b c d 对应权重范围 --- [0,1).[1,3).[3,6).[6,10) */ public class Ran ...
- HDU 4712 Hamming Distance(随机算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4712 解题报告:输入n个数,用十六进制的方式输入的,任意选择其中的两个数进行异或,求异或后的数用二进制 ...
随机推荐
- IO库 8.1
题目:编写函数,接受一个istream&参数,返回值类型也是istream&.此函数须从给定流中读取数据,直到遇到文件结束标志时停止.它将读取的数据打印在标准输出上.完成这些操作后,在 ...
- rsa 密钥和公钥的生成
openssl genrsa -out prikey.pem openssl rsa -in prikey.pem -pubout -out pubkey.pem
- Spark源码学习1
转自:http://www.cnblogs.com/hseagle/p/3664933.html 一.基本概念(Basic Concepts) RDD - resillient distributed ...
- Java学习之String对象为什么是不可变的
转自:http://www.2cto.com/kf/201401/272974.html,感谢作者的总结 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那么到底什么是不可变 ...
- C++中的unordered_map
1.简介 随着C++0x标准的确立,C++的标准库中也终于有了hash table这个东西.很久以来,STL中都只提供<map>作为存放对应关系的容器,内部通常用红黑树实现,据说原因是二叉 ...
- yii2.0 从控制器到视图的输出
在controllers/SiteController.php文件中,添加 public function actionSay($message = 'Hello') { return $this-& ...
- fafu 1100 线段树
题目链接 单点更新, 区间查询. 这题空间好小.... #include <iostream> #include <vector> #include <cstdio> ...
- C语言负数的除法和求余运算
假定我们让 a 除以 b,商为 q,余数为 r: q = a / b; r = a % b; 这里,不妨假定 b 大于 0. 我们希望 a.b.q.r 之间维持怎样的关系呢? 1.最重的一点,我们希望 ...
- IOS 特定于设备的开发:监测Retina支持
近年来,Apple在其旗舰设备上引入了Retina显示屏.根据Apple的说法,他的像素密度非常高,足以使人眼无法区分单独的像素. UIScreen类提供了一种容易的方式,用于监查当前设备是否提供了内 ...
- SSD常见问题的技术分析
AHCI对性能的影响 AHCI,全称Advanced Host Controller Interface,即高级主机控制器接口,是一种相比老旧的“IDE虚拟模式”更适合新一代SATA存储设备通信的协议 ...