zoukankan      html  css  js  c++  java
  • bzoj-2458 2458: [BeiJing2011]最小三角形(计算几何+分治)

    题目链接:

    2458: [BeiJing2011]最小三角形

    Time Limit: 10 Sec  Memory Limit: 128 MB
    Submit: 1101  Solved: 380

    Description

    Xaviera现在遇到了一个有趣的问题。
    平面上有N个点,Xaviera想找出周长最小的三角形。
    由于点非常多,分布也非常乱,所以Xaviera想请你来解决这个问题。
    为了减小问题的难度,这里的三角形也包括共线的三点。

    Input

    第一行包含一个整数N表示点的个数。
    接下来N行每行有两个整数,表示这个点的坐标。

    Output

    输出只有一行,包含一个6位小数,为周长最短的三角形的周长(四舍五入)。

    Sample Input

    4
    1 1
    2 3
    3 3
    3 4

    Sample Output

    3.414214
     
    题意:
     
    思路:
     
    像求最近点对那样进行分治,先按x左边排序,然后分治,找到左右两边较小的三角形周长d,然后再在以x=mid.x为中心线,左右宽各d/2的距离内和高d/2内的点,再求一次三角形最短周长就好了;
     
    AC代码:
    /**************************************************************
        Problem: 2458
        User: LittlePointer
        Language: C++
        Result: Accepted
        Time:4696 ms
        Memory:4416 kb
    ****************************************************************/
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <bits/stdc++.h>
    #include <stack>
    #include <map>
       
    using namespace std;
       
    #define For(i,j,n) for(int i=j;i<=n;i++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
       
    typedef  long long LL;
       
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
       
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const LL inf=1e18+10;
    const int N=2e5+10;
    const int maxn=1e3+20;
    const double eps=1e-12;
      
      
    int n;
    struct node
    {
        int x,y;
    }po[N],temp[N];
    int cmp(node a,node b)
    {
        if(a.x==b.x)return a.y<b.y;
        return a.x<b.x;
    }
    int cmp1(node a,node b)
    {
        return a.y<b.y;
    }
    double dis(int c,int e)
    {
        double fx=po[c].x*1.0,fy=po[c].y*1.0,ffx=po[e].x*1.0,ffy=po[e].y*1.0;
        return sqrt((fx-ffx)*(fx-ffx)+(fy-ffy)*(fy-ffy));
    }
    double di(int c,int e)
    {
        double fx=temp[c].x*1.0,fy=temp[c].y*1.0,ffx=temp[e].x*1.0,ffy=temp[e].y*1.0;
        return sqrt((fx-ffx)*(fx-ffx)+(fy-ffy)*(fy-ffy));
    }
    double solve(int l,int r)
    {
        if(r-l<=4)
        {
            double dis1,dist=inf;
            for(int i=l;i<r;i++)
            {
                for(int j=i+1;j<r;j++)
                {
                    dis1=dis(i,j);
                    for(int k=j+1;k<=r;k++)dist=min(dist,dis1+dis(i,k)+dis(j,k));
                }
            }
            return dist;
        }
        int mid=(l+r)>>1;
        double d1=solve(l,mid),d2=solve(mid+1,r);
        double d=min(d1,d2);
        int cnt=0;
        for(int i=l;i<=r;i++)
        {
            double w=abs(po[mid].x-po[i].x);
            if(2*w<=d)temp[++cnt]=po[i];
        }
        sort(temp+1,temp+cnt+1,cmp1);
        double ans=d,ha;
        for(int i=1;i<cnt;i++)
        {
            for(int j=i+1;j<cnt;j++)
            {
                ha=di(i,j);
                if(ha>d*0.5)break;
                for(int k=j+1;k<=cnt;k++)ans=min(ans,ha+di(i,k)+di(j,k));
            }
        }
        return ans;
    }
    int main()
    {
        read(n);
        For(i,1,n)
        {
            read(po[i].x);read(po[i].y);
        }
        sort(po+1,po+n+1,cmp);
        double ans=solve(1,n);
        printf("%.6lf
    ",ans);
        return 0;
    }
    

      

  • 相关阅读:
    C++中整型变量的存储大小和范围
    A1038 Recover the Smallest Number (30 分)
    A1067 Sort with Swap(0, i) (25 分)
    A1037 Magic Coupon (25 分)
    A1033 To Fill or Not to Fill (25 分)
    A1070 Mooncake (25 分)
    js 获取控件
    C#代码对SQL数据库添加表或者视图
    JS 动态操作表格
    jQuery取得下拉框选择的文本与值
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5797163.html
Copyright © 2011-2022 走看看