码迷,mamicode.com
首页 > 其他好文 > 详细

计算几何

时间:2018-02-23 17:09:27      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:cst   temp   ros   font   highlight   line   模板   pen   const   

谢Achen提醒,差点忘了有计算几何这东西。

poj2187 平面最远点对,凸包+旋转卡壳模板

//Serene
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
#define ll long long
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
const int maxn=5e4+7;
int n;

char cc; ll ff;
template<typename T>void read(T& aa) {
	aa=0;cc=getchar();ff=1;
	while((cc<‘0‘||cc>‘9‘)&&cc!=‘-‘) cc=getchar();
	if(cc==‘-‘) ff=-1,cc=getchar();
	while(cc>=‘0‘&&cc<=‘9‘) aa=aa*10+cc-‘0‘,cc=getchar();
	aa*=ff;
}

const double eps=1e-10;
double pf(double x){return x*x;}
inline int dcmp(const long double &x){return abs(x)<eps? 0:(x>0? 1:-1);}

struct Xl{
	double x,y;
	Xl(double x=0.,double y=0.):x(x),y(y){}
	bool operator == (const Xl &b) const{return (!dcmp(x-b.x))&&(!dcmp(y-b.y));}
	bool operator <  (const Xl &b) const{return !dcmp(x-b.x)? y<b.y:x<b.x;}
	Xl operator + (const Xl &b) const{return Xl(x+b.x,y+b.y);}
	Xl operator - (const Xl &b) const{return Xl(x-b.x,y-b.y);}
	Xl operator * (const double &b) const{return Xl(x*b,y*b);}
	Xl operator / (const double &b) const{return Xl(x/b,y/b);}
	double len() const{return pf(x)+pf(y);}
}node[maxn],zz[maxn];
int t;

double D_(const Xl& a,const Xl& b){return a.x*b.x+a.y*b.y;}
double X_(const Xl& a,const Xl& b){return a.x*b.y-a.y*b.x;}

bool cmp(const Xl& a,const Xl& b){
	return dcmp(X_(a-node[1],b-node[1]))==0? 
			(a-node[1]).len()<(b-node[1]).len() : dcmp(X_(a-node[1],b-node[1]))>0;
}

void graham() {
	For(i,2,n) if(node[i]<node[1]) swap(node[i],node[1]);
	sort(node+2,node+n+1,cmp);
	zz[++t]=node[1]; zz[++t]=node[2];
	For(i,3,n) {
		while(t>1&&dcmp(X_(zz[t]-zz[t-1],node[i]-zz[t]))<=0) --t;// "<" -> WA
		zz[++t]=node[i];
	}
}

double RC() {
	zz[t+1]=zz[1];
	int now=2;double rs=0;
	For(i,1,t) {
		while(X_(zz[i+1]-zz[i],zz[now]-zz[i])<X_(zz[i+1]-zz[i],zz[now+1]-zz[i]))
			if((++now)>t) now=1;
		rs=max(rs,(zz[now]-zz[i]).len());
	}
	return rs;
}

int main() {
	read(n);
	For(i,1,n) {
		read(node[i].x); 
		read(node[i].y);
	}
	graham();
	printf("%lld",(ll)RC());
	return 0;
}
/*
4
0 0
0 1
1 3
1 0
*/

  

计算几何

标签:cst   temp   ros   font   highlight   line   模板   pen   const   

原文地址:https://www.cnblogs.com/Serene-shixinyi/p/8462437.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!