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\n");
		else if(n==2)
		{
			printf("%.2lf\n",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\n",ans);
		}
	}
	return 0;
}
时间: 2024-11-07 05:41:25

HDU ACM 1392 Surround the Trees->凸包的相关文章

hdu 1392 Surround the Trees(凸包果题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7473    Accepted Submission(s): 2860 Problem Description There are a lot o

HDU 1392.Surround the Trees【凸包(求凸包周长)】【5月10】

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9790    Accepted Submission(s): 3763 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to

HDU ACM 1392 Surround the Trees-&amp;gt;凸包

分析:直接求出凸包.再算边长就可以. 另外仅仅有一个点时为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

hdu 1392 Surround the Trees (凸包)

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7043    Accepted Submission(s): 2688 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to

HDUJ 1392 Surround the Trees 凸包

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7203    Accepted Submission(s): 2752 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to

HDU 1392 Surround the Trees

PS: 在求解两个点的时候就是两个点的距离,在这WA了一次. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <cmath> using namespace std; const int maxn = 110; struct point { int x, y; point(d

zoj 1453 Surround the Trees(凸包求周长)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=453 Time Limit: 2 Seconds      Memory Limit: 65536 KB 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 r

杭电 1392 Surround the Trees

经典凸包问题!!!! AC代码如下: #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<cstdio> using namespace std; struct H { double x,y; }trees[105]; bool cmp(H a,H b) { return a.x<b.x||(a.x==b.x&&a

HDU - 1392 Surround the Trees (凸包)

Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出凸包上的点后,s数组中就是按顺序的点,累加一下距离就是周长了. #include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdli