售卖二维凸包板子,仅91$一份
查看原帖
售卖二维凸包板子,仅91$一份
370648
柠檬布丁吖楼主2023/5/26 19:55

WA #1

求调

#include<bits/stdc++.h>

using namespace std;

const int maxn=2e5+55;
struct point{
	double x,y;
}p[maxn],s[maxn];

int n,top;

bool cmp(point a,point b){
	return a.x!=b.x ? a.x<b.x : a.y<a.y;
}

double cross(point a,point b,point c){
	return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}

double dis(point a,point b){
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

double andrew(){
	sort(p+1,p+n+1,cmp);
	for(int i=1;i<=n;i++){
		while(top>1&&cross(s[top-1],s[top],p[i])<=0) top--;
		s[++top]=p[i];
	}
	
	int t=top;
	for(int i=n-1;i>=1;i--){
		while(top>t&&cross(s[top-1],s[top],p[i])<=0) top--;
		s[++top]=p[i];
	}
	
	double res=0;
	for(int i=1;i<top;i++){
		res+=dis(s[i],s[i+1]);
	}
	
	return res;
}

signed main(void){
	
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>p[i].x>>p[i].y;
	}	
	
	printf("%.2lf\n", andrew());
	
	return 0;
}
2023/5/26 19:55
加载中...