zoukankan      html  css  js  c++  java
  • 计算几何(凸包模板):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 }
  • 相关阅读:
    通过AIR Native Extension在AIR应用中加入iAd广告(一) —— Flash Builder篇
    使用MonoTouch.SQLite简化用户界面开发
    常见的几种分支开发方式
    给对象增加一个简单的自定义事件机制
    WCF 4.5:配置文件更小,对ASP.NET的支持更好
    MongoDB中的Group By
    SQL Server 2012大幅增强了TSQL
    编程珠玑:对DAO层的一点修改
    《The Elements of User Experience》读书笔记
    ORM工具LLBLGen Pro 3.5发布
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5997016.html
Copyright © 2011-2022 走看看