hdu 1392 Surround the Trees 凸包裸题

Surround the Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)



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
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<bitset>
#include<set>
#include<map>
#include<time.h>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-8
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=1e5+10,M=1e6+10,inf=1e9+10;
const LL INF=1e18+10,mod=1e9+7;

const int MAXN = 1010;
struct Point
{
    double x,y;
    Point() {}
    Point(double _x,double _y)
    {
        x = _x;
        y = _y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x - b.x,y - b.y);
    }
    //叉积
    double operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
//点积
    double operator *(const Point &b)const
    {
        return x*b.x + y*b.y;
    }
//绕原点旋转角度B(弧度值),后x,y的变化
    void transXY(double B)
    {
        double tx = x,ty = y;
        x= tx*cos(B) - ty*sin(B);
        y= tx*sin(B) + ty*cos(B);
    }
};
double dist(Point a,Point b)
{
    return sqrt((a-b)*(a-b));
}
int sgn(double x)
{
    if(fabs(x) < eps)return 0;
    if(x < 0)return -1;
    else return 1;
}
Point listt[MAXN];
int Stack[MAXN],top,n; //相对于listt[0]的极角排序
bool _cmp(Point p1,Point p2)
{
    double tmp = (p1-listt[0])^(p2-listt[0]);
    if(sgn(tmp) > 0)return true;
    else if(sgn(tmp) == 0 && sgn(dist(p1,listt[0]) - dist(p2,listt[0])) <= 0) return true;
    else return false;
}
void Graham(int n)
{
    top=0;
    Point p0;
    int k = 0;
    p0 = listt[0]; //找最下边的一个点
    for(int i = 1; i < n; i++)
    {
        if( (p0.y > listt[i].y) || (p0.y == listt[i].y && p0.x > listt[i].x) )
        {
            p0 = listt[i];
            k = i;
        }
    }
    swap(listt[k],listt[0]);
    sort(listt+1,listt+n,_cmp);
    if(n == 1)
    {
        top = 1;
        Stack[0] = 0;
        printf("0.00\n");
        return;
    }
    if(n == 2)
    {
        top = 2;
        Stack[0] = 0;
        Stack[1] = 1;
        double dis=dist(listt[Stack[0]],listt[Stack[1]]);
        printf("%.2f\n",dis);
        return ;
    }
    Stack[0] = 0;
    Stack[1] = 1;
    top = 2;
    for(int i = 2; i < n; i++)
    {
        while(top > 1 && sgn((listt[Stack[top-1]]-listt[Stack[top-2]])^(listt[i]-listt[Stack[top-2]])) <= 0)
            top--;
        Stack[top++] = i;
    }
    double ans=0;
    for(int i=1; i<top; i++)
    {
        ans+=dist(listt[Stack[i]],listt[Stack[i-1]]);
    }
    ans+=dist(listt[Stack[top-1]],listt[Stack[0]]);
    printf("%.2f\n",ans);
}
int main ()
{
    while(~scanf ( "%d", &n ) )
    {
        if(!n)break;
        for(int i=0; i<n; i++)
            scanf ( "%lf%lf", &listt[i].x, &listt[i].y );
        Graham(n);
    }
    return 0 ;
}

 

转载于:https://www.cnblogs.com/jhz033/p/7428078.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值