# BigDecimal精度与相等比较的坑

## Sol

/*
*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
const int MAXN = 1e5 + 10;
const double eps = 1e-10, Dlt = 0.97;
int N;
double xx[MAXN], yy[MAXN];
double Ans;
double rand(double T, int opt) {
return opt * T ;
}
double sqr(double x) {
return x * x;
}
double calc(double x, double y) {
double ans = 0;
for(int i = 1;i <= N; i++)
ans += sqrt(sqr(x - xx[i]) + sqr(y - yy[i]));
return ans;
}
void solve(double x, double y) {
double now = calc(x, y);
Ans = min(Ans, now);
for(double T = 10000; T > eps; T *= Dlt) {
for(int i = -1; i <= 1; i++) {
for(int j = -1; j <= 1; j++) {
double wx = x + rand(T, i), wy = y + rand(T, j);
//    if(wx < 0 || wy < 0 || wx > 10000 || wy > 10000) continue;
double wv = calc(wx, wy);
//    printf("%lf %lf %lf\n", wx, wy, calc(wx, wy));
if(wv < Ans) x = wx, y = wy, Ans= wv;
if(wv < now || ( exp((now - wv) / T) < (rand() / RAND_MAX) )) x = wx, y = wy, now = wv;
//    if(wv < now) x = wx, y = wy, now = wv;
}
}
}
}
int main() {
srand(19260817);
//    freopen("a.in", "r", stdin);
Ans = 1e20;
scanf("%d", &N);
for(int i = 1; i <= N; i++)
scanf("%lf %lf", &xx[i], &yy[i]);
//printf("%lf", calc(5000, 5000));
//for(int i = 1; i <= N; i++) {
//    double x = rand() % 10000, y = rand() % 10000;
solve(xx[2], yy[2]);
//}
printf("%d", (int)(Ans + 0.5));
return 0;
}
/*
4
0 0
0 5000
2354 10000
8787 0
*/

79 篇文章21 人订阅

0 条评论

## 相关文章

### 洛谷P1600 天天爱跑步(差分 LCA 桶)

$$T_i = 1$$：同样还是差分的思想，由于每个点 能对其产生的点的深度是相同的(假设为$$x$$)，那么访问该点时记录下$$dep[x]$$的数量，将结束...

792

6817

### 环形颜色分布直方图

package com.imageretrieval.features; import java.util.ArrayList; import java.ut...

38711

3929

1142

892

3149

### 2727:仙岛求药

2727:仙岛求药 查看 提交 统计 提问 总时间限制:1000ms内存限制:65536kB描述少年李逍遥的婶婶病了，王小虎介绍他去一趟仙灵岛，向仙女姐姐要仙丹...

3028

4698

981