关于一种等价(?)写法
查看原帖
关于一种等价(?)写法
119264
Alkaid_Star楼主2021/3/22 22:09

rt,lz发现了一种疑似等价的写法,请各位大佬帮忙康康到底是不是对的。

目前交上去是对的,具体写法见代码。

#include<bits/stdc++.h>
using namespace std;
const int N=100005;

struct Point{
	double x,y;
}p[N],s[N];

int n,cnt;

inline double Cross_product(Point StA,Point EdA,Point StB,Point EdB){
	return (EdA.x-StA.x)*(EdB.y-StB.y)-(EdA.y-StA.y)*(EdB.x-StB.x);
}

inline double Sqr(double x){ return x*x; }

inline double Dis(Point A,Point B){
	return sqrt(Sqr(A.x-B.x)+Sqr(A.y-B.y));
}

inline double mycmp(Point A,Point B){
	double Product=Cross_product(p[1],A,p[1],B);
	if (Product>0) return true;
	else if ((Product==0) && (Dis(p[0],A)<Dis(p[0],B))) return true;
	return false;
}

int main(){
	//freopen("Luogu2742.in","r",stdin);
	//freopen("Luogu2742.out","w",stdout);
	scanf("%d",&n);
	for (int i=1;i<=n;++i){
		scanf("%lf%lf",&p[i].x,&p[i].y);
		if ((i!=1) && ((p[i].y<p[1].y) || (p[i].y==p[1].y) && (p[i].x<p[1].x))){
			double t;
			t=p[1].x,p[1].x=p[i].x,p[i].x=t;
			t=p[1].y,p[1].y=p[i].y,p[i].y=t;
		}
	}
	sort(p+2,p+n+1,mycmp);
	cnt=0;
	s[++cnt]=p[1];
	for (int i=2;i<=n;++i){
		while ((cnt>1) && (Cross_product(s[cnt-1],s[cnt],s[cnt],p[i])<=0)) --cnt;
        // 换成 while ((cnt>1) && (Cross_product(s[cnt-1],s[cnt],s[cnt-1],p[i])<=0)) --cnt; 是否与之等价?
		s[++cnt]=p[i];
	}
	s[cnt+1]=p[1];
	double ans=0;
	for (int i=1;i<=cnt;++i)
		ans=ans+Dis(s[i],s[i+1]);
	printf("%.2lf\n",ans);
	return 0;
}

lz萌新,菜得很,如果问题弱智了请轻点喷嗷,谢谢

2021/3/22 22:09
加载中...