随机算法

求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+随机算法的更多相关文章

  1. 微信红包中使用的技术:AA收款+随机算法

    除夕夜你领到红包了吗?有的说“我领了好几K!”“我领了几W!” 土豪何其多,苦逼也不少!有的说“我出来工作了,没压岁钱了,还要发红包”.那您有去抢微信红包吗?微信群中抢“新年红包”春节爆红.618微信 ...

  2. POJ 3318 Matrix Multiplication(随机算法)

    题目链接 随机算法使劲水...srand((unsigned)time(0))比srand(NULL)靠谱很多,可能是更加随机. #include <cstdio> #include &l ...

  3. 抽奖随机算法的技术探讨与C#实现

    一.模拟客户需求 1.1 客户A需求:要求每次都按照下图的概率随机,数量不限,每个用户只能抽一次,抽奖结果的分布与抽奖概率近似. 1.2 客户B需求:固定奖项10个,抽奖次数不限,每个用户只能抽一次, ...

  4. hdu 4712 (随机算法)

    第一次听说随机算法,在给的n组数据间随机取两个组比较,当随机次数达到一定量时,答案就出来了. #include<stdio.h> #include<stdlib.h> #inc ...

  5. 权重随机算法的java实现

    一.概述 平时,经常会遇到权重随机算法,从不同权重的N个元素中随机选择一个,并使得总体选择结果是按照权重分布的.如广告投放.负载均衡等. 如有4个元素A.B.C.D,权重分别为1.2.3.4,随机结果 ...

  6. hdu 4712 Hamming Distance ( 随机算法混过了 )

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  7. LOJ #2540. 「PKUWC 2018」随机算法(概率dp)

    题意 LOJ #2540. 「PKUWC 2018」随机算法 题解 朴素的就是 \(O(n3^n)\) dp 写了一下有 \(50pts\) ... 大概就是每个点有三个状态 , 考虑了但不在独立集中 ...

  8. java 权重随机算法实现

    import java.util.*; /** * 权重随机算法实现 * a b c d 对应权重范围 --- [0,1).[1,3).[3,6).[6,10) */ public class Ran ...

  9. HDU 4712 Hamming Distance(随机算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4712 解题报告:输入n个数,用十六进制的方式输入的,任意选择其中的两个数进行异或,求异或后的数用二进制 ...

随机推荐

  1. WSDL规则解释(转)

    转自:http://www.blogjava.net/baoyaer/articles/116413.html WSDL文档可以分为两部分.顶部分由抽象定义组成,而底部分则由具体描述组成.抽象部分以独 ...

  2. jq 22 一个很好图片显示

    Picbox 示例页面:http://www.jq22.com/Demo961

  3. egret命令行编译项目时 版本不对应的问题

    egret 命令行编译项目时 如使用 egret build -e 会出现版本不对应的问题 分析原因 A,B项目 A项目使用1.8的egret引擎, B项目使用2.5引擎 但本地引擎升级至2.5.5, ...

  4. 组合框里添加复选框的方法(使用勾选的假象,用图片代替而已,并非QT原生支持)

    组合框可以看作是列表框和文本框的组合,因其占据的空间少,使用操作方便,常被界面设计人员用于界面开发设计中,在有限个输入的条件下,组合框常用来代替文本框,这样从用户使用角度来看,更趋人性化,所见即所得. ...

  5. WPF中如何获取ControlTemplate中的对象

    原文 http://www.silverlightchina.net/html/study/WPF/2010/1116/3418.html 先看一段XAML代码: 1 2 3 4 5 6 7 8 9 ...

  6. 有关Gcd,Lcm的一点小结论

    先介绍两个: 大数的Gcd Stein+欧几里德 function stein(a,b:int64):int64; begin if a<b then exit(stein(b,a)); the ...

  7. Highlight On Mouseover Effect With JQuery

    How to get the xpath by clicking an html element How to get the xpath by clicking an html element Qu ...

  8. 多个viewpager可能产生的问题

    由于Fragment的方便性,现在很多人开始大量使用Fragment. 今天使用时遇到各问题,记录下来并分享下. 使用Fragment都会用FragmentActivity ,特别是在用到ViewPa ...

  9. zoj 1453 Stripies

    /* 根据题意:不难看出,要是整个方程式最小,那么应该大的数先结合,小的数后结合.先排序然后结合(贪心) */ #include<stdio.h> #include<stdlib.h ...

  10. zoj 3710 Friends

    #include<stdio.h> #include<string.h> ][],h; int main(int argc, char* argv[]) { int t,i,n ...