HDU 1392 Surround the Trees(纯裸的凸包)

来源:互联网 发布:淘宝服务端数据为空 编辑:程序博客网 时间:2024/05/17 03:24

最裸的凸包。注意n==2时,就可以了啊。

Surround the Trees

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


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
#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-8#define M 1000100//#define LL __int64#define LL long long#define INF 0x3f3f3f3f#define PI 3.1415926535898const int maxn = 5010;using namespace std;int d[maxn], t;struct point{    double x, y;} f[maxn];double Distance(point a, point b){    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}int cmp(struct point a, struct point b){    if((a.x-f[0].x)*(b.y-f[0].y) == (b.x-f[0].x)*(a.y-f[0].y))        return ((a.x-f[0].x)*(a.x-f[0].x) + (a.y-f[0].y)*(a.y-f[0].y)) < ((b.x-f[0].x)*(b.x-f[0].x) + (b.y-f[0].y)*(b.y-f[0].y));    return (a.x-f[0].x)*(b.y-f[0].y) > (b.x-f[0].x)*(a.y-f[0].y);}int judge(int x){    int a = d[t-2];    int b = d[t-1];    double x1 = f[b].x-f[a].x;    double y1 = f[b].y-f[a].y;    double x2 = f[x].x-f[b].x;    double y2 = f[x].y-f[b].y;    if(x1*y2-x2*y1 > 0)    {        d[t++] = x;        return 1;    }    t--;    return 0;}int main(){    int n;    while(cin >>n)    {        if(!n)        break;        int k = 0;        for(int i = 0; i < n; i++)        {            cin >>f[i].x>>f[i].y;            if(f[k].y > f[i].y || (f[k].y == f[i].y && f[k].x > f[i].x))                k = i;        }        if(n == 2)        {            printf("%.2lf\n",Distance(f[0], f[1]));            continue;        }        if(k)        {            swap(f[0].x, f[k].x);            swap(f[0].y, f[k].y);        }        sort(f+1, f+n, cmp);        d[0] = 0;        d[1] = 1;        d[2] = 2;        t = 3;        for(int i = 3; i < n; i++)        {            if(t < 2)            {                d[1] = i;                t = 2;                continue;            }            if(!judge(i))                i--;        }        double sum = 0;        for(int i = 0; i < t; i++)        {            if(i != t-1)                sum += Distance(f[d[i]], f[d[i+1]]);            else                sum += Distance(f[d[i]], f[d[0]]);        }        printf("%.02lf\n",sum);    }    return 0;}


0 0
原创粉丝点击