zoukankan      html  css  js  c++  java
  • 凸包(hd1392)

    Surround the Trees

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 8996    Accepted Submission(s): 3457
    点我

    Problem Description
    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
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cmath>
     4 #include <cstdio>
     5 #include <cstring>
     6 using namespace std;
     7 struct point
     8 {
     9     int x,y;
    10 }list[100];
    11 int stack[100];
    12 int cross(point p0,point p1,point p2)
    13 {
    14     return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x); 
    15 }
    16 double dis(point p1,point p2)
    17 {
    18     return sqrt(double(p2.x-p1.x)*(p2.x-p1.x)+double(p2.y-p1.y)*(p2.y-p1.y));
    19 }
    20 bool cmp(point p1,point p2)
    21 {
    22     int tem=cross(list[0],p1,p2);
    23     if(tem>0)
    24         return 1;
    25     else if(tem==0&&dis(list[0],p1)<dis(list[0],p2))
    26         return 1;
    27     else
    28         return 0;
    29 }
    30 void init(int n)
    31 {
    32     int i,k;
    33     point p0;
    34     cin>>list[0].x>>list[0].y;
    35     for(i=1;i<n;i++)
    36     {
    37         cin>>list[i].x>>list[i].y;
    38         if(list[i].y<list[0].y||(list[i].y==list[0].y&&list[i].x<list[0].x))
    39         {
    40             p0=list[0];
    41             list[0]=list[i];
    42             list[i]=p0;
    43         }
    44     }
    45     sort(list+1,list+n,cmp);
    46 }
    47 int main()
    48 {
    49     int i,n;
    50     //freopen("in.txt","r",stdin);
    51     while(cin>>n&&n)
    52     {
    53         memset(list,0,sizeof(list));
    54         memset(stack,0,sizeof(stack));
    55         if(n==1)
    56             cout<<0.00<<endl;
    57         else if(n==2)
    58         {
    59             cin>>list[0].x>>list[0].y>>list[1].x>>list[1].y;
    60             printf("%0.2f
    ",dis(list[0],list[1]));
    61         }
    62         else
    63         {
    64             stack[0]=0;
    65             stack[1]=1;
    66             int top=1;
    67             init(n);
    68             for(i=2;i<n;i++)
    69             {
    70                 while(top>0&&cross(list[stack[top]],list[stack[top-1]],list[i])>0)
    71                     top--;
    72                 top++;
    73                 stack[top]=i;
    74             }
    75             double sum=0;
    76             for(i=1;i<=top;i++)
    77             {
    78                 sum+=dis(list[stack[i]],list[stack[i-1]]);
    79             }
    80             sum+=dis(list[0],list[stack[top]]);
    81             printf("%0.2f
    ",sum);
    82             }
    83         }
    84 }
     
  • 相关阅读:
    百度翻译api 实现简易微信翻译小程序
    Vuejs 基础与语法
    ES6 之 let / const
    browsersync 插件
    面向对象 实现轮播组件
    PyV8在服务端运行自动崩溃问题
    Nginx详解(正向代理、反向代理、负载均衡原理)
    Windows下用Nginx配置遇到的问题
    vuex 数据绑定
    如何使用Photoshop批量扫描保存文档
  • 原文地址:https://www.cnblogs.com/a1225234/p/4814590.html
Copyright © 2011-2022 走看看