HDU1392(凸包)

Surround the Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10299    Accepted Submission(s): 3991

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

Source

Asia 1997, Shanghai (Mainland China)

Graham扫描法求凸包,凸包周长即为答案。

 1 //2016.10.2
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <algorithm>
 7 #define N 105
 8 #define eps 1e-8
 9
10 using namespace std;
11
12 struct point
13 {
14     double x, y;
15     point(){}
16     point(double a, double b):x(a), y(b){}
17     point operator-(point a){//向量减法
18         return point(x-a.x, y-a.y);
19     }
20     point operator+(point a){//向量加法
21         return point(x+a.x, y+a.y);
22     }
23     double operator*(point a){//向量叉积
24         return x*a.y-y*a.x;
25     }
26     bool operator<(const point a)const{
27         if(fabs(x-a.x)<eps)return y<a.y;//浮点数的判等不能直接用‘==’直接比较
28         return x<a.x;
29     }
30     double len(){//向量的模
31         return sqrt(x*x+y*y);
32     }
33 }p[N], s[N];//p为点,s为栈
34
35 double cp(point a, point b, point o)//向量oa,ob叉积
36 {
37     return (a-o)*(b-o);
38 }
39
40 void Convex(point *p, int &n)//Graham扫描法,栈内为所有凸包点
41 {
42     sort(p, p+n);
43     int top, m;
44     s[0] = p[0]; s[1] = p[1]; top = 1;
45     for(int i = 2; i < n; i++)//从前往后扫
46     {
47         while(top>0 && cp(p[i], s[top], s[top-1])>=0)top--;
48         s[++top] = p[i];
49     }
50     m = top;
51     s[++top] = p[n-2];
52     for(int i = n-3; i >= 0; i--)//从后往前扫
53     {
54         while(top>m && cp(p[i], s[top], s[top-1])>=0)top--;
55         s[++top] = p[i];
56     }
57     n = top;
58 }
59
60 int main()
61 {
62     int n;
63     while(scanf("%d", &n)!=EOF && n)
64     {
65         for(int i = 0; i < n; i++)
66               scanf("%lf%lf", &p[i].x, &p[i].y);
67         sort(p, p+n);
68         int cnt = 0;
69         for(int i = 1; i < n; i++)//去掉重复的点
70               if(fabs(p[i].x-p[cnt].x)>eps || fabs(p[i].y-p[cnt].y)>eps)
71                   p[++cnt] = p[i];
72         cnt++;
73         if(cnt == 1){
74             printf("0.00\n");continue;
75         }else if(cnt==2){
76             printf("%.2lf\n", (p[1]-p[0]).len());continue;
77         }
78         Convex(p, cnt);
79         double ans = 0;
80         s[cnt] = s[0];
81         for(int i = 0; i < cnt; i++)ans+=(s[i+1]-s[i]).len();
82         printf("%.2lf\n", ans);
83     }
84
85     return 0;
86 }
时间: 2024-07-28 21:34:03

HDU1392(凸包)的相关文章

hdu1392凸包裸题

1 //极角排序 2 #include <bits/stdc++.h> 3 #define sqr(x) ((x)*(x)) 4 using namespace std; 5 int n,st[200001],top; 6 struct POI 7 { 8 int x,y; 9 POI() 10 { 11 x=y=0; 12 } 13 POI(int _x,int _y) 14 { 15 x=_x;y=_y; 16 } 17 } p[200001]; 18 int cross(POI x,PO

hdu1392(凸包)

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

HDU1392:Surround the Trees(凸包问题)

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

codevs1298, hdu1392 (凸包模板)

题意: 求凸包周长. 总结: 测试模板. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #define N 100005 #define eps 1e-8 using namespace std; struct point { double x, y; point(){} point(double

凸包模版 HDU1392 Surround the Trees

题目链接 题目大意 给出n颗树的坐标,要用一根绳子将所有的树围起来,求绳子的最短长度. 解题思路 求这n个坐标能形成的最大凸包.需要特判n为1时绳子长度为0以及n为2时绳子长度为2树距离.剩下的套凸包模版即可. AC代码 #include <iostream> #include <cmath> #include <cstdio> #include <cstring> #define eps 0.00000001 using namespace std; /*

HDU1392 Surround the Trees

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/转载请注明出处,侵权必究,保留最终解释权! 题目链接:HDU1392 正解:凸包 解题报告: 凸包模板题. //It is made by ljh2000 //有志者,事竟成,破釜沉舟,百二秦关终属楚:苦心人,天不负,卧薪尝胆,三千越甲可吞吴. #include <algorithm> #incl

POJ3528 HDU3662 三维凸包模板

POJ3528 HDU3662 第一道题 给定若干点 求凸包的表面积,第二题 给定若干点就凸包的面数. 简单说一下三维凸包的求法,首先对于4个点假设不共面,确定了唯一四面体,对于一个新的点,若它不在四面体内,为了让它进入凸包, 则对于所有凸包上的边,若边的一面是该点可以看到的而另一面看不到,则该点与该边构成的面要加入凸包. 模板代码非常清晰, #include<stdio.h> #include<algorithm> #include<string.h> #includ

关于2016.12.12——T1的反思:凸包的意义与应用

2016.12.12 T1 给n个圆,保证圆圆相离,求将圆围起来的最小周长.n<=100 就像上图.考场上,我就想用切线的角度来做凸包.以圆心x,y排序,像点凸包一样,不过用两圆之间的下切线角度来判断. 这就是下切线(我自己瞎编的名字): 好像是对的啊: 然后我就保证必AC的希望,用这种写法交了,然后就只得了N=2的暴力分... 自以为是正解,却落得如此下场... 为什么?这样不对吗?借用学长的力量,果然被Hack掉了: 这种情况,圆心排序后,检测的顺序并不是圆上的切点的顺序,自然就会挂. 蓝瘦

poj 1113 凸包周长

Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33888   Accepted: 11544 Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he w