Jungle Outpost HDU - 3761 (半平面交)
Posted yijiull
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jungle Outpost HDU - 3761 (半平面交)相关的知识,希望对你有一定的参考价值。
Jungle Outpost
题意:
半平面交~
用了动态分配内存但是忘记释放了一直MLE......
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 50010; 4 const int inf = 0x3f3f3f3f; 5 const double eps = 1e-8; 6 struct Point { 7 double x,y; 8 Point (double x = 0, double y = 0) : x(x), y(y) {} 9 }; 10 typedef Point Vector; 11 Vector operator + (Vector a, Vector b) { 12 return Vector (a.x + b.x, a.y + b.y); 13 } 14 Vector operator * (Vector a, double s) { 15 return Vector (a.x * s, a.y * s); 16 } 17 Vector operator / (Vector a, double p) { 18 return Vector (a.x / p, a.y / p); 19 } 20 Vector operator - (Point a, Point b) { 21 return Vector (a.x - b.x, a.y - b.y); 22 } 23 bool operator < (Point a, Point b) { 24 return a.x < b.x || (a.x == b.x && a.y < b.y); 25 } 26 int dcmp (double x) { 27 if(fabs(x) < eps) return 0; 28 return x < 0 ? -1 : 1; 29 } 30 bool operator == (const Point &a, const Point &b) { 31 return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; 32 } 33 double Dot(Vector a, Vector b) { 34 return a.x * b.x + a.y * b.y; 35 } 36 double Cross (Vector a, Vector b) { 37 return a.x * b.y - a.y * b.x; 38 } 39 //半平面交 40 struct Line{ 41 Point p; 42 Vector v; 43 double rad; 44 Line () {} 45 Line (Point p, Vector v) : p(p), v(v) { 46 rad = atan2(v.y,v.x); 47 } 48 bool operator < (const Line &L) const { 49 return rad < L.rad; 50 } 51 }; 52 bool OnLeft(Line L, Point p) { 53 return Cross(L.v, p - L.p) > 0; 54 } 55 Point GetLineIntersection (Line a, Line b) { 56 Vector u = a.p - b.p; 57 double t = Cross(b.v, u) / Cross(a.v, b.v); 58 return a.p + a.v*t; 59 } 60 int HalfplaneIntersection(Line *L, int n) { 61 sort(L, L+n); 62 int first, last; 63 Point *p = new Point[n]; 64 Line *q = new Line[n]; //双端队列 65 q[first = last = 0] = L[0]; 66 for(int i = 1; i < n; i++) { 67 while(first < last && !OnLeft(L[i], p[last-1])) last--; //去尾 68 while(first < last && !OnLeft(L[i], p[first])) first++; 69 q[++last] = L[i]; 70 if(dcmp(Cross(q[last].v, q[last-1].v)) == 0) { 71 last--; 72 if(OnLeft(q[last], L[i].p)) q[last] = L[i]; 73 } 74 if(first < last) p[last-1] = GetLineIntersection (q[last-1],q[last]); 75 } 76 while(first < last && !OnLeft(q[first], p[last-1])) last--; //删除无用平面 77 delete []p; 78 delete []q; 79 if(last - first <= 1) return 0; //空集 80 return 1; 81 } 82 Point pp[maxn]; 83 Line line[maxn]; 84 int n; 85 bool ck(int m){ 86 int cnt = 0; 87 for(int i = 0; i < n; i++){ 88 line[cnt++] = Line(pp[i], pp[(i+m+1)%n] - pp[i]); 89 } 90 int ans = HalfplaneIntersection(line, cnt); 91 return ans==0; 92 } 93 int main(){ 94 int t; 95 //freopen("in.txt", "r", stdin); 96 scanf("%d", &t); 97 while(t--){ 98 scanf("%d", &n); 99 for(int i = n-1; i >= 0; i--){ 100 scanf("%lf %lf", &pp[i].x, &pp[i].y); 101 } 102 int L = 1, R = n-3; 103 while(L <= R) { 104 int m = (L+R)/2; 105 if(ck(m)) R = m-1; 106 else L = m+1; 107 } 108 printf("%d\n", L); 109 } 110 }
以上是关于Jungle Outpost HDU - 3761 (半平面交)的主要内容,如果未能解决你的问题,请参考以下文章
UVA 1475 - Jungle Outpost(二分 + 半平面交)
UVALive 4992 Jungle Outpost(半平面交)