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 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<stdio.h>
 3 #include<math.h>
 4 #include<algorithm>
 5 using namespace std;
 6 #define N 110
 7 struct point
 8 {
 9     double x,y,angel;
10 } p[N],stack[N];
11 int top,n;
12
13 double dis(point a,point b)
14 {
15     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
16 }
17
18 bool mult(point p1,point p2,point p0)
19 {
20     return (p1.x-p0.x)*(p2.y-p0.y) >= (p2.x-p0.x)*(p1.y-p0.y);
21 }
22
23 bool cmp(point a,point b)
24 {
25     if(a.angel == b.angel)
26     {
27         if (a.x == b.x)
28             return a.y > b.y;
29         return a.x > b.x;
30     }
31     return a.angel < b.angel;
32 }
33
34 void graham()
35 {
36     int i,k=0;
37     for(i=0; i<n; i++)
38         if(p[i].y<p[k].y||((p[i].y==p[k].y)&&(p[i].x<p[k].x)))
39             k=i;
40     swap(p[0],p[k]);
41     for(i=1; i<n; i++)
42         p[i].angel=atan2(p[i].y-p[0].y,p[i].x-p[0].x);
43     sort(p+1,p+n,cmp);
44     stack[0]=p[0];
45     stack[1]=p[1];
46     stack[2]=p[2];
47     top=3;
48     for(i=3; i<n; i++)
49     {
50         while(top > 2 && mult(stack[top-2],stack[top-1],p[i])<=0)
51             top--;
52         stack[top++]=p[i];
53     }
54 }
55
56 int main()
57 {
58     int i;
59     double ans;
60     while(scanf("%d",&n)!=EOF&&n)
61     {
62         for(i=0; i<n; i++)
63             scanf("%lf%lf",&p[i].x,&p[i].y);
64         if(n==1)
65         {
66             printf("0.00\n");
67             continue;
68         }
69         if(n==2)
70         {
71             printf("%.2lf\n",dis(p[0],p[1]));
72             continue;
73         }
74         graham();
75         ans=0;
76         for(i=0; i<top-1; i++)
77             ans+=dis(stack[i],stack[i+1]);
78         ans+=dis(stack[top-1],stack[0]);
79         printf("%.2lf\n",ans);
80     }
81     return 0;
82 }

hdu1392(凸包)

时间: 2024-10-11 04:20:59

hdu1392(凸包)的相关文章

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

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(凸包问题)

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