[luogu3878][TJOI2010]分金币【模拟退火】

题目描述

现在有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;
}

转载于:https://www.cnblogs.com/chhokmah/p/10571625.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值