随机函数的应用
随机化算法是这样一种算法,在算法中使用了随机函数,且随机函数的返回值直接或者间接的影响了算法的执行流程或执行结果。随机化算法基于随机方法,依赖于概率大小。
PS:http://wenku.baidu.com/link?url=LLmRXIEckdoBmScawrF0-1sQhy5umWqiHweEm6BtlhcyQE8twe4mqPMV4a9FJgfSDQlvhWqtw2YXXe6YvN8xtuR45BGJrioTwqkr7VRFEMm
#include<iostream>#include<cstdio>
#include<string.h>
#include<string>
#include<stack>
#include<cstdlib>
#include<set>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<sstream>
#include<time.h>
#define ll __int64
#define lll unsigned long long
#define MAX 1000009
#define MAXN 2009
#define eps 1e-8
#define INF 0x7fffffff
#define mod 1000000007
#define clr(a) memset(a,0,sizeof(a))
#define clr1(a) memset(a,-1,sizeof(a))
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
using namespace std;
/*
题意:求最小霍夫曼距离
想法: 随机算法 + 16进制输入
*/
int a[MAX];
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("an.txt","r", stdin);
#endif
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i = 0;i<n;i++)
{
scanf("%X",&a[i]);
}
int Time = 1000000;
int ans = INF;
srand(time(NULL));//随机种子
while(Time--)
{
int t1 = rand()%n;//随机生成t1
int t2 = rand()%n;//随机生成t2
while(t1==t2)//保证t1!=t2
t2 = rand()%n;
int t3 = a[t1]^a[t2];
//cout<<t1<<" "<<t2<<endl;
t3 = __builtin_popcount(t3);//二进制中有多少1,在g++中存在
ans = min(ans,t3);
}
cout<<ans<<endl;
}
return 0;
}