计算几何(凸包模板):HDU 1392

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\n",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\n",ans);
53     }
54     return 0;
55 }
时间: 2024-10-12 14:29:20

计算几何(凸包模板):HDU 1392的相关文章

计算几何 --- 凸包 模板

//Memory Time // 1347K 0MS // by : Snarl_jsb #include<algorithm> #include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<vector> #include<queue> #include<stack> #include<map> #

HDU 1392 Surround the Trees (凸包周长)

题目链接:HDU 1392 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

hdu 1392 Surround the Trees (凸包)

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7043    Accepted Submission(s): 2688 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to

hdu 1392 Surround the Trees(凸包果题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7473    Accepted Submission(s): 2860 Problem Description There are a lot o

POJ 3528 hdu 3662 三维凸包模板题

POJ 3528题:http://poj.org/problem?id=3528 HDU 3662:http://acm.hdu.edu.cn/showproblem.php?pid=3662 一个是求三维凸包面数,一个是求三维凸包表面积,都是很裸的. 贴代码: #include<stdio.h> #include<algorithm> #include<string.h> #include<math.h> #include<stdlib.h>

HDU - 1392 Surround the Trees (凸包)

Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出凸包上的点后,s数组中就是按顺序的点,累加一下距离就是周长了. #include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdli

HDU 1392 凸包

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10345    Accepted Submission(s): 4009 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to

hdu 1392(凸包)

传送门:Surround the Trees 题意:求凸包的周长. 分析:凸包模板题,先按极角排好序后,然后根据叉积正负确定凸包. #include <stdio.h> #include <math.h> #include <algorithm> #include <string.h> #include <math.h> using namespace std; const double eps = 1e-8; const int N = 110

HDU 1392.Surround the Trees【凸包(求凸包周长)】【5月10】

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9790    Accepted Submission(s): 3763 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to