• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
ACM s1124yy
守りたいものが 強くさせること
博客园    首页    新随笔    联系   管理     

UVA 11168 Airport(凸包)

Airport

【题目链接】Airport

【题目类型】凸包

&题解:

蓝书274页,要想到解析几何来降低复杂度,还用到点到直线的距离公式,之后向想到预处理x,y坐标之和,就可以O(1)查到距离,还是很厉害的.
但我还是不知道为什么最后输出ans要除n?希望大佬看见可以评论告诉我一下.

&代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
struct Point {
	double x,y;
	Point(double x=0,double y=0):x(x),y(y) {}
};
typedef Point Vector;
Vector operator + (const Vector& A,const Vector& B) {return Vector(A.x+B.x , A.y+B.y);}
Vector operator - (const Vector& A,const Vector& B) {return Vector(A.x-B.x , A.y-B.y);}
bool operator < (const Point& a,const Point& b) {return a.x<b.x||(a.x==b.x&&a.y<b.y);}
bool operator == (const Point& a,const Point& b) {return a.x==b.x&&a.y==b.y;}
double Cross(const Vector&A,const Vector& B) {return A.x*B.y-A.y*B.x;}
Vector Rotate(const Vector& A,double a) {return Vector(A.x*cos(a)-A.y*sin(a) , A.x*sin(a)+A.y*cos(a));}
vector<Point> ConvexHull(vector<Point>  p) {
	sort(p.begin(),p.end());
	p.erase(unique(p.begin(), p.end()),p.end());
	int n=p.size();
	vector<Point> ch(n+1);
	int m=0;
	for(int i=0; i<n; i++) {
		while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
		ch[m++]=p[i];
	}
	int k=m;
	for(int i=n-2; i>=0; i--) {
		while(m>k&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
		ch[m++]=p[i];
	}
	if (n>1) m--;
	ch.resize(m);
	return ch;
}
int n,K;
int main() {
	freopen("e:1.in","r",stdin);
	int T; scanf("%d",&T);
	while(T--) {
		vector<Point> p;
		scanf("%d",&n);
		double sux=0,suy=0;
		for(int i=0; i<n; i++) {
			double x,y;
			scanf("%lf%lf",&x,&y);
			sux+=x,suy+=y;
			p.push_back(Point(x,y));
		}
		vector<Point> te=ConvexHull(p);
		int z=te.size();
		te.push_back(te[0]);
		double ans=1e15;
		// printf("%f %f\n", sux,suy);
		if(z<=2) {
			ans=0;
		}
		else {
			for(int i=1; i<te.size(); i++) {
				double A=te[i].y-te[i-1].y,B=te[i-1].x-te[i].x,C=te[i].x*te[i-1].y-te[i-1].x*te[i].y;
				ans=min(ans,fabs(A*sux+B*suy+C*n)/sqrt(A*A+B*B));
			}
		}
		printf("Case #%d: %.3f\n", ++K, ans/n);
	}
	return 0;
}
posted @ 2017-04-24 23:43  s1124yy  阅读(153)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3