洛谷P2742 圈奶牛Fencing the Cows(凸包模板)

题目链接

https://www.luogu.org/problem/P2742

代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn=1e4+10;
int inf=1e9;
void read(int &x){
    int f=1;x=0;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    x*=f;
}
struct node{
	double x,y;
};

node p[maxn],s[maxn];//s存储凸包 
double xx,yy;    //极点 
//向量ab,向量ac 叉积 
double compare(node a,node b,node c){
    return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
//极角排序 
bool cmp(node a,node b){
    node c;
    c.x=xx;
    c.y=yy;
    if(compare(c,a,b)==0)
        return a.x<b.x;
    else
        return compare(c,a,b)>0;
}

//求边长 
double bian(node a,node b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(b.y-a.y)*(b.y-a.y));
}
int n;
void tubao(){
	int k,top=1;
	yy=inf;
	for(int i=0;i<n;i++){
		if(yy>p[i].y){
			yy=p[i].y;
			xx=p[i].x;
			k=i;
		}
	}
	p[k]=p[0];
	sort(p+1,p+n,cmp);
	s[0].x=xx;
    s[0].y=yy;
    s[1]=p[1];
    for(int i=2;i<n;){
        if(top&&(compare(s[top-1],s[top],p[i])<0))
            top--;
        else
            s[++top]=p[i++];
    }
    /*
    double ans1=0;
	for(int i=0;i<=top;i++)    //遍历求构成三角形最大的面积
        for(int j=i+1;j<=top;j++)
            for(int k=j+1;k<=top;k++)
               ans1=max(ans1,compare(s[j],s[k],s[i]));
        
	printf("%.2lf\n",0.5*ans1);
	*/
	double  ans[maxn];   // 求凸多边形周长
    s[++top]=s[0];
    for(int i=0; i<top; i++)
        ans[i]=bian(s[i],s[i+1]);
    double sum=0;
    for(int i=0; i<top; i++){
        sum+=ans[i];
    }
	printf("%.2lf\n",sum);
} 

int main(){
	int i;
	read(n);
	for(i=0;i<n;i++){
		scanf("%lf%lf",&p[i].x,&p[i].y);
	}
	tubao();
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值