洛谷 P4423 [BJWC2011]最小三角形 题解

求平面最近点对的时候有这样一种思路:

将所有点全部绕原点旋转同一个角度,然后按横坐标排序。

根据数学直觉,在随机旋转后,答案中的两个点在数组中肯定不会离得太远。

把这种思路运用到这题当中,我们旋转随机角度将点打乱之后枚举三角形的三个顶点即可。

坐标旋转公式:

\[x2=x1\cos\theta-y1\sin\theta\\ y2=x1\sin\theta+y1\cos\theta \]

注意角度制和弧度制的转化即可。

/*
 * Title: P4423 [BJWC2011]最小三角形
 * Source: 洛谷
 * URL: https://www.luogu.com.cn/problem/P4423
 * Author: Steven_lzx
 * Command: -std=c++23 -O2 -Wall -fno-ms-extensions
 * Date: 2022.11.10
 */
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
const double PI=acos(-1);
struct Point{double x,y;}p[200005];
int n;
double ans=1e15;
double dis(double x1,double y1,double x2,double y2){return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}
void rotate(double theta)
{
    double nx,ny,tx,ty;
    theta=theta/180*PI;
    for(int i=1;i<=n;i++)
    {
        nx=p[i].x;
        ny=p[i].y;
        tx=nx*cos(theta)-ny*sin(theta);
        ty=nx*sin(theta)+ny*cos(theta);
        p[i]=(Point){tx,ty};
    }
    sort(p+1,p+n+1,[](Point a,Point b){return a.x<b.x;});
    for(int i=1;i<n-1;i++)//暴力枚举三个点
        for(int j=i+1;j<min(i+20,n);j++)
            for(int k=j+1;k<=min(i+20,n);k++)
                ans=min(ans,dis(p[i].x,p[i].y,p[j].x,p[j].y)+dis(p[j].x,p[j].y,p[k].x,p[k].y)+dis(p[i].x,p[i].y,p[k].x,p[k].y));
    return;
}
int main()
{
    mt19937 rnd(20080422);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%lf%lf",&p[i].x,&p[i].y);
    rotate(rnd()%360);
    rotate(rnd()%360);
    printf("%.6lf\n",ans);
    return 0;
}
posted @ 2022-11-10 19:18  Day_Dreamer_D  阅读(43)  评论(0)    收藏  举报