社区讨论

求助啊

P2742【模板】二维凸包 / [USACO5.1] 圈奶牛Fencing the Cows参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mi7dvfov
此快照首次捕获于
2025/11/20 20:04
4 个月前
此快照最后确认于
2025/11/20 20:04
4 个月前
查看原帖
CPP
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>

using namespace std;

int n,top,tmp=1;
double ans;

struct node{
	double x,y,att;
	bool operator < (const node &b) const {
		return att==b.att?x<b.x:att<b.att; 
	}
}s[100001],p[100001];

double cp(node a1,node a2,node b1,node b2){
    return (a2.x-a1.x)*(b2.y-b1.y)-(a2.y-a1.y)*(b2.x-b1.x);
}

double dis(node p1,node p2){		
	return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%lf%lf",&p[i].x,&p[i].y);
		if(p[i].y<=p[tmp].y or (p[i].y==p[tmp].y and p[i].x<p[tmp].x)){
			tmp=i;
		}
	}
	swap(p[1],p[tmp]);
	for(int i=2;i<=n;i++){
		p[i].att=atan2(p[i].x-p[1].x,p[i].y-p[1].y);
	}
	sort(p+2,p+n+1);
	s[++top]=p[1];
	for(int i=2;i<=n;i++){
		while(top>1 and cp(s[top-1],s[top],s[top],p[i])<=0){
			top--;
		}
		s[++top]=p[i];
	}
	s[top+1]=p[1];
	for(int i=1;i<=top;i++){
		ans+=dis(s[i],s[i+1]);
	}
	printf("%.2lf",ans);
	return 0;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...