您的位置:首页 > 其它

poj 1113 Wall凸包

2017-07-29 10:35 381 查看
凸包入门。点这里

画图就能看出来周长就是一个圆+凸包周长

大佬的模板

一次循环求上部

一次循环求下部

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node {
double x;double y;
}p[400005],s[400005];
int n,m;
int sq(int x)
{
return x*x;
}
double chaji(node a,node b)
{
return a.x*b.y-a.y*b.x;
}
double dist(node a,node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
node xl(node a,node b)
{
node c;
c.x=a.x-b.x;
c.y=a.y-b.y;
return c;
}
bool cmp(node a,node b)
{
if(a.x==b.x) return a.y<b.y;
else return a.x>b.x;
}
int main()
{  int n,l;
while(scanf("%d %d",&n,&l)!=EOF)
{      memset(s,0,sizeof(s));
for (int i=0;i<n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
sort(p,p+n,cmp);
int tot=0;
for (int i=0;i<n;i++)
{
while(tot>1 && chaji(xl(s[tot-1],s[tot-2]),xl(p[i],s[tot-2]))<=0) tot--;
s[tot++]=p[i];
}
int k=tot;
for (int i=n-2;i>=0;i--)
{

while(tot>k && chaji(xl(s[tot-1],s[tot-2]),xl(p[i],s[tot-2]))<=0) tot--;
s[tot++]=p[i];
}
if(n>1) tot--;
double ans=0.0;
for (int i=0;i<tot;i++)
{
ans+=dist(s[i],s[i+1]);
}
ans+=2*l*3.1415926;
printf("%.0lf\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: