hdoj 1392 Surround the Trees 【求凸包周长】

Surround the Trees

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


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
 


题意:给你N个点,求凸包周长。1个点输出0,2个点输出两点距离。



AC代码:


#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define eps 1e-8
#define MAXN 110
using namespace std;
struct Point
{
    double x, y;
    Point(){}
    Point(double X, double Y){
        x = X; y = Y;
    }
};
Point P[MAXN];
int dcmp(double x)
{
    if(fabs(x) < eps)
        return 0;
    else
        return x < 0 ? -1 : 1;
}
Point operator - (Point A, Point B){
    return Point(A.x-B.x, A.y-B.y);
}
Point operator + (Point A, Point B){
    return Point(A.x+B.x, A.y+B.y);
}
Point operator * (Point A, double p){
    return Point(A.x*p, A.y*p);;
}
double Cross(Point A, Point B){
    return A.x*B.y - A.y*B.x;
}
double Dot(Point A, Point B){
    return A.x*B.x + A.y*B.y;
}
double Dis(Point A, Point B){
    return sqrt((A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y));
}
bool operator == (Point A, Point B){
    return dcmp(A.x-B.x) == 0 && dcmp(A.y-B.y) == 0;
}
bool cmp(Point A, Point B)//极角排序 按极角排序,若角度相等距离大的在前面
{
    double temp = Cross(A-P[0], B-P[0]);
    if(dcmp(temp) > 0) return true;
    if(dcmp(temp) == 0 && dcmp(Dis(P[0], A) - Dis(P[0], B)) < 0) return true;
    return false;
}
int Stack[MAXN], top;
void input(int n)
{
    scanf("%lf%lf", &P[0].x, &P[0].y);
    double xx = P[0].x, yy = P[0].y;
    int id = 0;
    for(int i = 1; i < n; i++)
    {
        scanf("%lf%lf", &P[i].x, &P[i].y);
        if(P[i].y < yy || (P[i].y == yy && P[i].x < xx))
        {
            xx = P[i].x;
            yy = P[i].y;
            id = i;
        }
    }
    Point T;
    T = P[0]; P[0] = P[id]; P[id] = T;
    sort(P+1, P+n, cmp);//极角排序
}
void Graham(int n)
{
    if(n == 1){ top = 0; Stack[0] = 0;}
    else if(n == 2)
    {
        top = 1;
        Stack[0] = 0;
        Stack[1] = 1;
    }
    else
    {
        for(int i = 0; i <= 1; i++)
            Stack[i] = i;
        top = 1;
        for(int i = 2; i < n; i++)
        {
            while(top > 0 && dcmp(Cross(P[Stack[top]]-P[Stack[top-1]], P[i]-P[Stack[top-1]])) <= 0) top--;
            top++;
            Stack[top] = i;
        }
    }
}
int main()
{
    int n;
    while(scanf("%d", &n), n)
    {
        input(n);
        Graham(n);
        double ans = 0;
        for(int i = 0; i < top; i++)
            ans += Dis(P[Stack[i]], P[Stack[i+1]]);
        if(top > 1)
            ans += Dis(P[Stack[top]], P[Stack[0]]);
        printf("%.2lf\n", ans);
    }
    return 0;
}



Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值