• HDU ACM 1392 Surround the Trees->凸包


    分析:直接求出凸包。再算边长就可以。

    另外仅仅有一个点时为0.00单独处理,两个点直接为距离也单独处理。

    #include<iostream>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    
    struct Point
    {
    	Point(){}
    	Point(double _x,double _y):x(_x),y(_y){}
    	Point operator-(const Point& a) const
    	{
    		return Point(x-a.x,y-a.y);
    	}
    	double x,y;
    };
    
    double dis(const Point& a,const Point& b)
    {
    	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    }
    
    double cross(const Point& a,const Point& b)
    {
    	return a.x*b.y-a.y*b.x;
    }
    
    bool cmp(const Point& a,const Point& b)
    {
    	if(a.x!=b.x)
    		return a.x<b.x;
    	else
    		return a.y<b.y;
    }
    
    int convexhull(Point* p,int n,Point* ch)
    {
    	int i,m,k;
    
    	sort(p,p+n,cmp);
    	m=0;
    	for(i=0;i<n;i++)   //上凸包
    	{
    		while(m>1 && cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
    		ch[m++]=p[i];
    	}
    	k=m;
    	for(i=n-2;i>=0;i--) //下凸包
    	{
    		while(m>k && cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
    		ch[m++]=p[i];
    	}
    	if(n>1) m--;
    	return m;
    }
    
    int main()
    {
    	Point a[105],p[105];
    	int n,i,m;
    	double ans;
    
    	while(scanf("%d",&n)==1 &&n)
    	{
    		for(i=0;i<n;i++)
    			scanf("%lf%lf",&a[i].x,&a[i].y);
    		if(n==1)
    			printf("0.00
    ");
    		else if(n==2)
    		{
    			printf("%.2lf
    ",dis(a[0],a[1]));
    		}
    		else
    		{
    			m=convexhull(a,n,p);
    			ans=0;
    			for(i=1;i<=m;i++)
    				ans+=dis(p[i],p[i-1]);
    			printf("%.2lf
    ",ans);
    		}
    	}
    	return 0;
    }
    


  • 相关阅读:
    IOS TextField伴随键盘移动
    解决github提交commit,contributions不统计显示绿色的问题
    jQuery选择器
    jQuery之DOM操作
    iOS监听电话事件
    Docker网络代理设置
    Oracle数据库之PL/SQL过程与函数
    Oracle数据库之FORALL与BULK COLLECT语句
    Oracle数据库之事务
    Oracle数据库之PL/SQL游标
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5325572.html
Copyright © 2020-2023  润新知