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
", 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
", ++K, ans/n);
}
return 0;
}