杭电1392-Surround the Trees

Surround the Trees

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


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
这是本人做的第一个凸包,一晚上搞这个,太TM蛋疼了,还好懂了
求凸包最基本的方法:Graham-Scan:这应该是最早的O(nlgn)算法了,实现也比较简单,其基本思想是维护一个凸曲线,因此它要求算法开始时必须至少知道一个必然在凸包上的点作为其始点(还好这比较简单)
AC代码+解释:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<iomanip>
const int MAX=101;
int n;//树的数量
int top;//加进凸包的点
using namespace std;
typedef struct Node
{
    double x;
    double y;
};
Node s[MAX];
Node stack[MAX];
double Distance(Node a,Node b)//两点间的距离
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double  Mul(Node p1, Node p2, Node p0)//向量叉积,我这里是逆时针来的,如果叉积大于0则符合
{
    return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
}
int cmp(Node a,Node b)//快排
{
    double m;
    m=Mul(a,b,s[0]);
    if(m>0||(!m&&Distance(a,s[0])<Distance(b,s[0])))//如果叉积大于0按极角从小到大排序或者如果叉积等于0(即两向量共线),那么按距离偏短进行排序
    return 1;
    return 0;
}
void convex_hull()//凸包模板
{
    for(int i=1; i<n; i++)
    {
        Node temp;
        if(s[i].y<s[0].y||(s[i].y==s[0].y&&s[i].x<s[0].x))//找基点,即最开始的点
        {
            temp=s[i];
            s[i]=s[0];
            s[0]=temp;
        }
    }
    sort(s+1,s+n,cmp);
    stack[0]=s[0];
    stack[1]=s[1];
    stack[2]=s[2];
    top=2;
    for(int i=2; i<n; i++)
    {
        while(top>=2&&Mul(s[i],stack[top],stack[top-1])>=0)//凸包核心
            top-=1;
        top+=1;
        stack[top]=s[i];
    }
}
int main()
{
    int i;
    double sum;
    while(cin>>n,n)
    {
        for(i=0; i<n; i++)
        {
            cin>>s[i].x>>s[i].y;
        }
        cout.setf(ios::fixed);//设置输出格式为定点输出格式
        cout.precision(2);//设置输出为精确2位小数
        if(n==1)
        {
            cout<<0.00<<endl;
            continue;
        }
        else if(n==2)
        {
            cout<<Distance(s[0],s[1])<<endl;
        }
        else
        {
            convex_hull();
            sum=0;
            for(i=0; i<top; i++)
            {
                sum+=Distance(stack[i],stack[i+1]);
            }
            sum+=Distance(stack[0],stack[top]);//最后还有最后点的与基点的距离
            cout<<sum<<endl;
        }
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值