博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LA 3890 Most Distant Point from the Sea(半平面交)
阅读量:4684 次
发布时间:2019-06-09

本文共 3014 字,大约阅读时间需要 10 分钟。

Most Distant Point from the Sea

【题目链接】

【题目类型】半平面交

&题解:

蓝书279 二分答案,判断平移后的直线的半平面交是否为空.

模板是照着敲的,还有一些地方不是很懂, 应该还要慢慢体会吧

&代码:

#include 
#include
#include
#include
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);}Vector operator * (const Vector& A, double p) {return Vector(A.x * p, A.y * p);}double Dot(const Vector& A, const Vector& 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;}double Length(const Vector& A) {return sqrt(Dot(A, A));}Vector Normal(const Vector& A) {double l = Length(A); return Vector(-A.y / l, A.x / l);}double PolygonArea(vector
p) { int n = p.size(); double s = 0; for(int i = 1; i < n - 1; i++) { s += Cross(p[i] - p[0], p[i + 1] - p[0]); } return s / 2;}struct Line { Point p, v; double ang; Line() {} Line(Point p, Vector v): p(p), v(v) {ang = atan2(v.y, v.x);} bool operator < (const Line& l) const { return ang < l.ang; }};bool OnLeft(const Line& L, const Point& p) { return Cross(L.v, p - L.p) > 0;}Point GetLineIntersection(const Line& a, const Line& b) { Vector u = a.p - b.p; double t = Cross(b.v, u) / Cross(a.v, b.v); return a.p + a.v * t;}const double eps = 1e-6;vector
HalfplaneIntersection(vector
L) { int n = L.size(); sort(L.begin(), L.end()); int first, last; vector
p(n), ans; vector
que(n); que[first = last = 0] = L[0]; for(int i = 1; i < n; i++) { while(first < last && !OnLeft(L[i], p[last - 1])) last--; while(first < last && !OnLeft(L[i], p[first])) first++; que[++last] = L[i]; if(fabs(Cross(que[last].v, que[last - 1].v)) < eps) { last--; if(OnLeft(que[last], L[i].p)) que[last] = L[i]; } if(first < last) p[last - 1] = GetLineIntersection(que[last - 1], que[last]); } while(first < last && !OnLeft(que[first], p[last - 1])) last--; if(last - first <= 1) return ans; p[last] = GetLineIntersection(que[last], que[first]); for(int i = first; i <= last; i++) ans.push_back(p[i]); return ans;}int main() { //("E:1.in", "r", stdin); int n; while(scanf("%d", &n) == 1 && n) { vector
p, v, nor; int m, x, y; for(int i = 0; i < n; i++) { scanf("%d%d", &x, &y); p.push_back(Point(x, y)); } if(PolygonArea(p) < 0) reverse(p.begin(), p.end()); for(int i = 0; i < n; i++) { v.push_back(p[(i + 1) % n] - p[i]); nor.push_back(Normal(v[i])); } double left = 0, right = 20000; while(right - left > 1e-6) { vector
L; double mid = left + (right - left) / 2; for(int i = 0; i < n; i++) L.push_back(Line(p[i] + nor[i]*mid, v[i])); vector
poly = HalfplaneIntersection(L); if(poly.empty()) right = mid; else left = mid; } printf("%f\n", left); } return 0;}

转载于:https://www.cnblogs.com/s1124yy/p/6794417.html

你可能感兴趣的文章
[转载]CAsyncSocket及CSocket注解
查看>>
phpcms自定义表单添加验证码验证功能
查看>>
P2896 [USACO08FEB]一起吃饭Eating Together
查看>>
翻译链接
查看>>
[Lintcode Ladder2] String
查看>>
[转]html导出到excel数据格式不正确解决方法
查看>>
PHP时间格式化封装函数
查看>>
React之JSX语法
查看>>
3月10号
查看>>
复习3【树的构建+树的遍历】
查看>>
ArcGIS AddIN开发之COM对象写入注册表
查看>>
mysql数据库忘记密码时如何修改(本地)
查看>>
秒杀系统:设计方案与实现思路
查看>>
几种知名开源富文本编辑器记录和对比(仅供参考)
查看>>
.NET获取英文月份缩写名(可获取其他国家)
查看>>
如何在自定义Listener(监听器)中使用Spring容器管理的bean
查看>>
30天自制操作系统
查看>>
C# Func<T,TResult>
查看>>
BZOJ4036 [HAOI2015]按位或 FWT
查看>>
sublime使用经验汇总
查看>>