萌新妹子刚学模拟退火1145141919810ms,模板求调
查看原帖
萌新妹子刚学模拟退火1145141919810ms,模板求调
141392
lucky叶楼主2021/11/17 22:30

rt

样例能过,WA后7个点,不是特判n=1的问题,对着标程调了半天无果,求你谷广大dolao救救孩子

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <ctime>
#include <random>
#define LL long long
#define IL inline
using namespace std;
// template <class T>
// IL void read(T &res)
// {
// 	res = 0;
// 	bool flag = 0;
// 	char c = getchar();
// 	while (c < '0' || '9' < c)
// 	{
// 		if (c == '-')
// 			flag = 1;
// 		c = getchar();
// 	}
// 	while ('0' <= c && c <= '9')
// 		res = (res << 3) + (res << 1) + c - '0', c = getchar();
// 	if (flag)
// 		res = -res;
// }
const int N = 35, Inf = 0x3f3f3f3f;
int n;
int a[N];
default_random_engine e{time(0)};
uniform_int_distribution<unsigned> u, v;
uniform_real_distribution<double> w;
int ans, sum, half;
IL int calc()
{
	int res = 0;
	for (int i = 1; i <= half; i++)
		res += a[i];
	res = abs(sum - res * 2);
	ans = min(ans, res);
	return res;
}
IL void sim()
{
	shuffle(a + 1, a + n + 1, e);
	int o = calc(), p, x, y;
	double delte;
	for (double t = 1e4; t > 1e-4; t *= 0.995)
	{
		x = u(e), y = v(e);
		swap(a[x], a[y]);
		p = calc();
		delte = p - o;
		if (exp(-delte / t) < w(e))
			swap(x, y);
		else
			o = p;
	}
}
int main()
{
	int T;
	scanf("%d", &T);
	w.param(uniform_real_distribution<double>::param_type{0, 1});
	while (T--)
	{
		sum = 0;
		ans = Inf;
		scanf("%d", &n);
		if (n == 1)
		{
			scanf("%d", &a[1]);
			printf("%d\n", a[1]);
			continue;
		}
		half = n / 2;
		u.param(uniform_int_distribution<unsigned>::param_type{1, half});
		v.param(uniform_int_distribution<unsigned>::param_type{half + 1, n});
		for (int i = 1; i <= n; i++)
			scanf("%d", &a[i]), sum += a[i];
		for (int i = 1; i <= 100; i++)
			sim();
		printf("%d\n", ans);
	}
	return 0;
}
2021/11/17 22:30
加载中...