zoukankan      html  css  js  c++  java
  • 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;
    }
    


  • 相关阅读:
    详解Oracle临时表的几种用法及意义
    Testing and Debugging Procedures using SQL Developer 3.1
    ORACLE 流复制
    ORA01017 invalid username/password; logon denied
    oracle数据类型
    使用Pls_Integer的好处
    js取得上传图片大小
    高效整洁CSS代码原则
    在线压缩js和css
    图片等比例缩放后裁切
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5325572.html
Copyright © 2011-2022 走看看