模拟退火过了
查看原帖
模拟退火过了
330132
Yantttttt楼主2023/11/7 15:06

交了四页

#include <bits/stdc++.h>
using namespace std;

const double dT=0.9137;
const int N=1e5+10;
const double MAX_TIME=0.956;
const int RANDSEED=62789152;
int answ,ansx;

int n,m,op[N],val[N];

int rd()
{
	int f=1,k=0;char c=getchar();
	while(c<'0'||c>'9')
	{
		if(c=='-') f=-1;
		c=getchar();
	}	
	while(c>='0'&&c<='9')
	{
		k=k*10+c-'0';
		c=getchar();
	}
	return f*k;
}

int calc(int x)
{
	if(x<0||x>m)
	{
		//return -(x-m/2)*(x-m/2);
		return -0x3f3f3f3f;
	}
	for(int i=1;i<=n;++i)
	{
		if(op[i]==1) x&=val[i];
		if(op[i]==2) x|=val[i];
		if(op[i]==3) x^=val[i];
	}
	return x;
}

void sa()
{
	double t=(double)m/1000;
	ansx=1;
	while(t>1e-4)
	{
		//cout<<ansx<<" "<<answ<<endl;
		int xx=ansx+(rand()*2-RAND_MAX)*t;
      	int ww=calc(xx);
      	double de=answ-ww;
      	if(de<0)
      	{
         	ansx=xx;
         	answ=ww;
      	}
      	else if(exp(-de/t)*RAND_MAX>rand())
      	{
         	ansx=xx;
         	answ=ww;
      	}
      	t*=dT;
	}
}

void solve()
{
	while ((double)clock()/CLOCKS_PER_SEC < MAX_TIME)
	{
		sa();
	}
}

int main()
{
	//freopen("sleep.in","r",stdin);
	//freopen("sleep.out","w",stdout);
	n=rd(),m=rd();
	srand(RANDSEED);
	for(int i=1;i<=n;i++)
	{
		char c[10];
		cin>>c;
		if(c[0]=='A') op[i]=1;
		if(c[0]=='O') op[i]=2;
		if(c[0]=='X') op[i]=3;
		val[i]=rd();
		//cout<<op[i]<<"endl";
	}
	if(m==0)
	{
		cout<<calc(0)<<endl;
		return 0;
	}
	solve();
	cout<<answ<<endl;
	return 0;
}
2023/11/7 15:06
加载中...