HDU4712 Hamming Distance (随机化)

link:http://acm.hdu.edu.cn/showproblem.php?pid=4712

题意:给1e5个数字,输出这些数中,最小的海明码距离。

思路:距离的范围是0到20。而且每个数的数位都很有限的,然后一言不合开始

随机。随机算法虽然挺惊艳的,不过求随机算法成功概率,臣妾做不到啊!

总之,遇上暴力很难得到优化,但AC掉一片的题,随机算法这样的奇策应当信手

拈来!这是强大洞察之力的体现~

 

#include <iostream>
#include <algorithm>
using namespace std;
const int NICO = 100000+10;
int T, n, a[NICO];
int main()
{
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);
        for(int i=1;i<=n;i++)
        {
            scanf("%X", &a[i]);
        }
        int ans = 22;
        for(int i=1;i<=500000;i++)
        {
            int x = rand()%n+1;
            int y = rand()%n+1;
            if(x == y) continue;
            int v = a[x]^a[y];
            ans = min(ans, __builtin_popcountl(v));
        }
        printf("%d\n", ans);
    }
}

  

posted @ 2017-05-10 19:16  RUSH_D_CAT  阅读(214)  评论(0编辑  收藏  举报