#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll c [50];
inline ll cal (int n) {
ll sum1= 0,sum2=0;
for(int i=1;i<=n/2;i++)sum1+=c[i];
for(int i=n/2+1;i<=n;i++)sum2+=c[i];
return abs(sum1-sum2);
}
ll ans = 0;
inline void solve () {
int n;cin >> n;
for (int i = 1;i <= n;++ i) cin >> c [i];
if (n == 1) {
cout << c [1] << endl;
return ;
}
int times = 1000;
ans = cal (n);
while (times --) {
for (double t = 10000;t >= 1e-10;t *= 0.95) {
int a = (int)(rand ()) % n + 1;
int b = (int)(rand ()) % n + 1;
swap (c [a],c [b]);
ll rt = cal (n);
if (rt < ans) {
ans = rt;
} else if (exp ((ans - rt) / t) * RAND_MAX < rand ()) {
swap (c [a],c [b]);
}
}
}
cout << ans << endl;
}
int main () {
#ifndef ONLINE_JUDGE
freopen ("shit.txt","r",stdin);
#endif
ios::sync_with_stdio (false);
srand (time (NULL));
int t;cin >> t;
while (t --) solve ();
return 0;
}
这段代码里:
int a = (int)(rand ()) % n + 1;
int b = (int)(rand ()) % n + 1;
为什么rand()如果乘了t就WA...调了两个小时参数..............