题目描述
现在有n枚金币,它们可能会有不同的价值,现在要把它们分成两部分,要求这两部分金币数目之差不超过1,问这样分成的两部分金币的价值之差最小是多少?
分析
根据模拟退火的基本套路,先随机分两堆金币,然后每一次随机从两堆中取出一个,进行交换,看看答案是否更优【太简单了,不多赘述】
ac代码
#include <bits/stdc++.h>
#define ms(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define db double
using namespace std;
inline char gc() {
static char buf[1 << 16], *S, *T;
if (S == T) {
T = (S = buf) + fread(buf, 1, 1 << 16, stdin);
if (T == S) return EOF;
}
return *S ++;
}
template <typename T>
inline void read(T &x) {
T w = 1;
x = 0;
char ch = gc();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = gc();
}
while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc();
x = x * w;
}
template <typename T>
void write(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + 48);
}
#define N 105
int n, ans;
int a[N];
int calc() {
int res1 = 0, res2 = 0;
for (int i = 1; i <= n; i ++)
if (i <= (n + 1) / 2) res1 += a[i];
else res2 += a[i];
return abs(res1 - res2);
}
db Rand() {
return rand() % 10000 / 10000.0;
}
void SA(db T) {
while (T > 1e-3) {
int x = rand() % ((n + 1) / 2) + 1, y = rand() % ((n + 1) / 2) + ((n + 1) / 2);
if (x <= 0 || x > n || y <= 0 || y > n) continue;
swap(a[x], a[y]);
int res = calc();
if (ans > res) ans = res;
else if ((exp((1.0 * ans - 1.0 * res) / T)) <= Rand()) swap(a[x], a[y]);
T *= 0.98;
}
}
int main() {
// freopen("coin.in","r",stdin);
// freopen("coin.out","w",stdout);
srand(15346301);
int cas;
read(cas);
for (int _t = 1; _t <= cas; _t ++) {
read(n);
for (int i = 1; i <= n; i ++) read(a[i]);
ans = inf;
for (int i = 1; i <= 150; i ++) SA(10000);
printf("%d\n", ans);
}
return 0;
}