题意:给你一段起伏的山,让你找出一个点,使得在这个点时可以看到山上的任何一个点。
题解:这题正解是半平面交,然而用模拟退火可以过。让退火去跑最佳的x,然后二分出y。退火的调参很重要,重点是要给予足够的时间让其充分冷却才能跑出最优解。我调了几十发的参= =
二分的check,只要用叉积判断一下相邻两个点减去观察点得到的两个向量的方向就可以了。
代码:
#pragma GCC optimize("O3,fast-math")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
using namespace std;
typedef long double Lf;
const int N=305;
int n;
Lf minn=1e6,maxn=-1e6,mxy=-1e6;
struct point{
Lf x,y;
point(){}
point(Lf xx,Lf yy){x=xx;y=yy;}
point operator - (const point &n) const{
return point(x-n.x,y-n.y);
}
Lf operator * (const point &n) const{
return x*n.y-y*n.x;
}
}a[N];
Lf rnd(){
return 1.0*rand()/RAND_MAX;
}
bool check(point p){
for(int i=1;i<n;i++){
if((a[i]-p)*(a[i+1]-p)<0){
return false;
}
}
return true;
}
Lf gety(Lf x){
if(fabs(x-a[n].x)<1e-7){
return a[n].y;
}
for(int i=1;i<n;i++){
if(a[i+1].x>x){
Lf k=(a[i].y-a[i+1].y)/(a[i].x-a[i+1].x);
Lf b=a[i].y-a[i].x*k;
return k*x+b;
}
}
return 0;
}
Lf calc(Lf x){
Lf l=mxy,r=5e9,mid,ans=5e9;
while(r-l>1e-4){
mid=(l+r)/2;
if(check(point(x,mid))){
ans=mid;
r=mid;
}else{
l=mid;
}
}
return ans-gety(x);
}
Lf sa(){
Lf temp=1,delta=0.994;
Lf ansx=minn+(maxn-minn)*rnd(),ansy=calc(ansx);
while(temp>1e-11){
int dirc=rand()&1;
Lf nowx=dirc?ansx-(ansx-minn)*temp*rnd():ansx+(maxn-ansx)*temp*rnd();
Lf nowy=calc(nowx);
if(nowy<ansy){
ansx=nowx;
ansy=nowy;
}else{
Lf d=exp(ansy-nowy)*temp;
if(d>rnd()&&d<1){
ansx=nowx;
ansy=nowy;
}
}
temp*=delta;
}
return ansy;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%Lf",&a[i].x);
minn=min(minn,a[i].x);
maxn=max(maxn,a[i].x);
}
for(int i=1;i<=n;i++){
scanf("%Lf",&a[i].y);
mxy=max(mxy,a[i].y);
}
printf("%.3Lf\n",sa());
return 0;
}