K.Solar Energy
题面:
代码:
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
#define pi acos(-1.0)
const int maxn = 114514;
int n;
double a[maxn], s[maxn], t[maxn];
double dist(double x, double y) {
return fabs(x - y) > (2 * pi - fabs(x - y)) ? (2 * pi - fabs(x - y)) : fabs(x - y);
}
double calc(double x) {
double sum = 0;
for (int i = 1; i <= n; i++)
sum += max(0.0, t[i] - s[i] * dist(a[i], x));
return sum;
}
double SA() {
double T =300;
//T0
double T1 = 1e-4;
double delta = 0.998;
//接受新解的初始概率
double r=2*pi;
//跳动半径
double now = 0;
double now_ans = calc(0);
double pre = now, pre_ans = -0x3f3f3f3f;
while (T > T1) {
now = pre - (((double) rand() * 2 - RAND_MAX) / RAND_MAX) * T;
while (now > r)now -= r;
while (now < 0)now += r;
now_ans = calc(now);
if (pre_ans < now_ans) {
//如果新解更优,则接受
pre_ans = now_ans;
pre = now;
}
// } else if ((exp(now_ans - pre_ans) / T) * RAND_MAX >= rand()) {
// pre_ans = now_ans;
// //否则以一定概率接受
// }
T *= delta;
}
return pre_ans;
}
int main() {
srand(time(NULL));
cin >> n;
for (int i = 1; i <= n; i++)
cin >> t[i] >> s[i] >> a[i];
double ans = SA();
printf("%.6lf\n", ans);
return 0;
}
疑问:
不知道为什么加上板子里的
// } else if ((exp(now_ans - pre_ans) / T) * RAND_MAX >= rand()) {
// pre_ans = now_ans;
// //否则以一定概率接受
// }
就会超时。
随机化算法
指抽卡游戏,而且还不设置井。
代码完全一样的提交,结果如下。
(前面还有个过的概率更低的版本,也就有个两三页WA吧)