POJ-1113-Wall(凸包)

3 篇文章 0 订阅

链接:http://poj.org/problem?id=1113


大致题意:N个点围成的城堡,求距离城堡大于L处建围墙,求围墙的最短距离

凸包问题,考虑到对于一个凸包上的一个x度的角,其需要一个180-x度半径为L的圆弧形状围墙,即求凸包周长+以L为半径圆的周长。

//FS
//#pragma comment(linker, "/stack:1024000000,1024000000")  
//#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;  
#define INF 0x3f3f3f3f  
#define MAXM 100005  
const double eps = 1e-8;
const double PI = acos(-1.0);
const long long mod=1e9+7;
const int MAXN = 2007;
int sgn(double x)
{
	if(fabs(x)<eps)return 0;
	if(x<0)return -1;
	return 1;
}
struct Point
{
	double x,y;
	Point(){}
	Point(double _x, double _y){x=_x,y=_y;}
	bool operator <(Point b)const
	{
		return sgn(x-b.x)==0? sgn(y-b.y)<0:x<b.x;
	}
	Point operator -(const Point &b)const
	{
		return Point(x-b.x,y-b.y);
	}
	double operator ^(const Point &b)const //叉积
	{
		return x*b.y-b.x*y;
	}
	double dist(Point p)
	{
		return hypot(x-p.x,y-p.y);
	}
	double len()
	{
		return hypot(x,y);
	}
};
Point List[MAXN];
int Stack[MAXN],top;
bool cmp(Point p1, Point p2)
{
	double temp = (p1-List[0])^(p2-List[0]);
	if(sgn(temp)>0)return 1;
	if(sgn(temp)==0 && List[0].dist(p1)<=List[0].dist(p2))return 1;
	return 0;
}
void Graham(int n)
{
	Point p;
	int k=0;
	p=List[0];
	for(int i=1; i<n; ++i)
		if( (p.y>List[i].y) || (p.y==List[i].y && p.x>List[i].x))
		{
			p=List[i];
			k=i;
		}
	swap(List[k],List[0]);
	sort(List+1,List+n,cmp);
	Stack[0]=0;
	top=1;
	if(n==1)return;
	Stack[1]=1;
	top=2;
	if(n==2)return ;
	for(int i=2; i<n; ++i)
	{
		while(top>1 && sgn( (List[ Stack[top-1] ]-List[ Stack[top-2] ])^(List[i]-List[ Stack[top-2] ]))<=0)
			top--;
		Stack[top++]=i;
	}
}
int main()  
{
    int n,l;
    while(scanf("%d%d",&n,&l)!=EOF)
    {
    	for(int i=0; i<n; ++i)
    		scanf("%lf%lf",&List[i].x,&List[i].y);
    	Graham(n);
    	double ans=0;
    	for(int i=1; i<top; ++i)
    		ans+=List[Stack[i]].dist( List[Stack[i-1]] );
    	ans+=List[Stack[0]].dist(List[Stack[top-1]]);
    	ans+=2.0*PI*l;
    	printf("%d\n", (int)(ans+0.5));
    }
    return 0;
}  
/*
9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200
*/



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值