• 计算几何(凸包模板):HDU 1392 Surround the Trees


    There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you help him? 
    The diameter and length of the trees are omitted, which means a tree can be seen as a point. The thickness of the rope is also omitted which means a rope can be seen as a line.


    There are no more than 100 trees.
     

    Input

    The input contains one or more data sets. At first line of each input data set is number of trees in this data set, it is followed by series of coordinates of the trees. Each coordinate is a positive integer pair, and each integer is less than 32767. Each pair is separated by blank.

    Zero at line for number of trees terminates the input for your program.
     

    Output

    The minimal length of the rope. The precision should be 10^-2.
     

    Sample Input

    9 
    12 7 
    24 9 
    30 5 
    41 9 
    80 7 
    50 87 
    22 9 
    45 1 
    50 7 
    0 

    Sample Output

    243.06
      这道题就是凸包模板。
      http://www.cnblogs.com/jbelial/archive/2011/08/05/2128625.html
     1 #include <algorithm>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <cmath>
     6 using namespace std;
     7 const double eps=1e-10;
     8 const int N=110;
     9 struct Point{
    10     double x,y;
    11     Point(double x_=0,double y_=0){x=x_;y=y_;}
    12     friend Point operator-(Point a,Point b){
    13         return Point(a.x-b.x,a.y-b.y);
    14     }
    15 }p[N],st[N];
    16 double sqr(double x){return x*x;}
    17 double dis(Point a,Point b){return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));}
    18 double cross(Point a,Point b){return a.x*b.y-a.y*b.x;}
    19 bool cmp(Point a,Point b){
    20     double s=cross(a-p[0],b-p[0]);
    21     if(fabs(s)>=eps)return s>=eps;
    22     return dis(a,p[0])<dis(b,p[0]);
    23 }
    24 int n,t,top;
    25 double ans;
    26 int main(){
    27     while(scanf("%d",&n)!=EOF&&n){
    28         for(int i=0;i<n;i++)
    29             scanf("%lf%lf",&p[i].x,&p[i].y);
    30         if(n==1){
    31             puts("0.00");
    32             continue;
    33         }
    34         if(n==2){
    35             printf("%.2f
    ",dis(p[0],p[1]));
    36             continue;
    37         }
    38         t=0;
    39         for(int i=1;i<n;i++)if(p[i].y<p[t].y||p[i].y==p[t].y&&p[i].x<p[t].x)t=i;
    40         if(t)swap(p[0],p[t]);
    41         sort(p+1,p+n,cmp);
    42         p[n]=p[0];top=0;
    43         st[++top]=p[0];
    44         st[++top]=p[1];
    45         st[++top]=p[2];
    46         for(int i=3;i<=n;i++){
    47             while(top>=2&&cross(st[top]-p[i],st[top-1]-p[i])>=0)top--;
    48             st[++top]=p[i];
    49         }
    50         ans=0;
    51         for(int i=2;i<=top;i++)ans+=dis(st[i],st[i-1]);
    52         printf("%.2f
    ",ans);
    53     }
    54     return 0;
    55 }
  • 相关阅读:
    原生ES-Module在浏览器中的尝试
    常见的web攻击总结
    node第三方模块----nodemailer发送邮件
    08----mockjs处理前端传来的路径,获取?后面的值
    07----mockjs获取前端传递的数据
    05-----Mock.Random 扩展方法
    03----Mock.mock() 生成接口,随机数据
    06----Mock.setup()
    十一. for of
    十.数组解构
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5997016.html
Copyright © 2020-2023  润新知