退火求调
查看原帖
退火求调
335264
Liooooo楼主2023/10/31 21:09

实在调不过#4

退火过程中是直接随机某一次改成随机一个字符串

可能这样效率很低导致跑不出来?

bool M1;
#include <bits/stdc++.h>
using namespace std;
//#define int long long
const int mod = 998244353;
const int N = 1e5 + 7;
#define fi first
#define se second
#define ll long long
#define eb emplace_back
#define pii pair<int, int>
#define F(i,a,b) for(int i=a;i<=b;++i)
#define UF(i,a,b) for(int i=a;i>=b;--i)
#define memory cerr<<(&M1-&M2)/1024.0/1024<<'\n'
inline int read(){int a = 0, b = 1; char c = getchar();while(c < '0' || c > '9'){if(c == '-') b = -1; c = getchar();}while(c <= '9' && c >= '0'){a = a * 10 + c - 48; c = getchar();}return a * b;}
inline void Mod(int &x){x%=mod;}
void file(){freopen(".in","r",stdin);freopen(".out","w",stdout);}
const double eps=1e-15;
double T, d=0.99997, Time;
int n, m, pre[N], cnt, cnt2, cnt1, p[N], pos[N], ans, vis[50], now, ned[N];
vector<int> debug;
string s;
map<string, int> id;
int calc()
{
	memset(vis, 0, sizeof(vis));
	int res = 0;
	F(i,1,cnt2)
	{
		if(p[pre[i]]==pos[i]) vis[pos[i]]++;
	}
	F(i,1,cnt) if(vis[i]==ned[i]) res ++;
	return res;
}
void sa()
{
	T = 100000000;
	random_shuffle(p+1,p+1+cnt1);
	while(T > eps){
		int x = rand()%cnt1+1, y = rand()%cnt+1, lst = p[x];
		while(y!=p[x]) y=rand()%cnt+1;
		p[x] = y; int nwans = calc();
		int delta = nwans - ans;
		if(delta>0) ans = nwans, T *= d;
		//else if(exp(-delta/T)*RAND_MAX>=rand()) p[x] = lst;
		else p[x] = lst, T *= d;
	}
}
void solve(){while((clock()-Time)/CLOCKS_PER_SEC<=0.95) sa();}
bool M2;
int main()
{
	//ios::sync_with_stdio(false);
	Time = clock();srand(330);
	memory; //file();
	n = read(); m = read(); int lst = 0;
	int op;string s;F(i, 1, n){
		op = read(); 
		if(op==2){
			cin >> s;
			if(id.find(s)==id.end()) id[s] = ++ cnt, now = cnt;
			else now = id[s];
			pre[++ cnt2] = lst; pos[cnt2] = now; ned[now] ++;
		}else ++cnt1, lst = cnt1;
	}
	F(i, 1, cnt1) p[i] = (i+cnt)%cnt+1;
	//F(i, 1, cnt1) printf("%d ", p[i]); puts("");
	ans = calc(); //printf("%d\n", ans);
	solve(); printf("%d\n", ans);
	return 0;
}
2023/10/31 21:09
加载中...