91pts求助
查看原帖
91pts求助
367387
cainiaoshanglu楼主2022/9/23 18:35

mxqz,调了一周没找到问题/kk

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define int long long
using namespace std;

void read(int &x){
	x=0;
	char c=getchar();
	while(!('0'<=c && c<='9')){
		c=getchar();
	}
	while('0'<=c && c<='9'){
		x=(x<<1)+(x<<3)+(c^48);
		c=getchar();
	}
}
struct Vector{
	double x,y,cs;
	Vector(){}
	Vector(double xx,double yy){
		x=xx;
		y=yy;
	}
	void show(){
		printf("(%lf %lf)",x,y);
	}
	double len(){
		return sqrt(x*x+y*y);
	}
	friend Vector operator-(Vector a,Vector b){
		return Vector(a.x-b.x,a.y-b.y);
	}
	friend double operator*(Vector a,Vector b){
		return a.x*b.x+a.y*b.y;
	}
	friend double operator^(Vector a,Vector b){
		return a.x*b.y-a.y*b.x;
	}
	friend bool operator<(Vector a,Vector b){
		if(a.cs==b.cs){
			return a.len()>b.len();
		}
		return a.cs>b.cs;
	}
} a[100010],st[100010];
int n,tp=0;
double getdiam(){
	double mn=a[1].y;
	int mnp=1;
	for(int i=2;i<=n;i++){
		if(mn>a[i].y){
			mn=a[i].y;
			mnp=i;
		}
	}
	a[mnp].cs=100;
	for(int i=1;i<=n;i++){
		if(mnp!=i){
			a[i].cs=(a[i]-a[mnp])*Vector(1,0)/(a[i]-a[mnp]).len();
		}
	}
	sort(a+1,a+n+1);
	st[++tp]=a[1];
	for(int i=2;i<=n;i++){
		st[++tp]=a[i];
		while(tp>1 && ((st[tp]-st[tp-2])^(st[tp-1]-st[tp-2]))>0){
			st[tp-1]=st[tp];
			tp--;
		}
	}
	st[0]=st[tp];
	if(tp==2){
		return (st[1]-st[2]).len()*(st[1]-st[2]).len()+0.5;
	} 
	double res=0;
	int j=2;
	for(int i=0;i<tp;i++){
		for(;((st[i]-st[j])^(st[i+1]-st[j]))<((st[i]-st[j+1])^(st[i+1]-st[j+1]));j=(j+1)%tp);
		res=max(res,max((st[i]-st[j]).len(),(st[i+1]-st[j]).len()));
	}
	return res*res;
}
signed main(){
	read(n);
	for(int i=1;i<=n;i++){
		scanf("%lf %lf",&a[i].x,&a[i].y);
	}
	double res=getdiam();
	printf("%lld",(int)(res));
	return 0;
}
2022/9/23 18:35
加载中...