hack
查看原帖
hack
375895
SSL_wj楼主2021/1/28 20:15

写了个程序,A了,后来调另一题时发现没有判断三个或更多点同一条线的情况

请求增强数据

#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
	double x,y;
}a[100010];
void spaw(node &x,node &y){node t=x;x=y;y=t;}
int n,q,stack[100010],tot=0;
double ans;
double chaji(node x,node y,node z){return (y.x-x.x)*(z.y-x.y)-(z.x-x.x)*(y.y-x.y);}
double tw(double x){return x*x;}
double dis(node x,node y){return sqrt(tw(x.x-y.x)+tw(x.y-y.y));}
bool cmp(node x,node y)
{
	double t=chaji(a[1],x,y);
	if(t>0||t==0&&dis(a[1],x)>dis(a[1],y))return 1;
	return 0;
}
int main()
{
	scanf("%d",&n);
	a[q].y=1000001;
	for(int i=1;i<=n;i++)
	{
		scanf("%lf%lf",&a[i].x,&a[i].y);
		if(a[q].y>a[i].y||a[q].y==a[i].y&&a[q].x>a[i].x)q=i;
	}
	spaw(a[1],a[q]);
	stack[++tot]=1;
	sort(a+2,a+n+1,cmp);
	for(int i=2;i<=n;i++)
	{
		while(tot>=2&&chaji(a[stack[tot-1]],a[stack[tot]],a[i])<=0)--tot;//该处<=应为<
		stack[++tot]=i;
	}
	ans=dis(a[stack[1]],a[stack[tot]]);
	for(int i=1;i<tot;i++)ans+=dis(a[stack[i]],a[stack[i+1]]);
	printf("%.2lf",ans);
	return 0;
}
2021/1/28 20:15
加载中...