UVA 3890 Most distant points from the Sea (dichotomy + half-plane intersection)

Source: Internet
Author: User

Title Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11358

Ideas

dichotomy + half-plane intersection

The distance between two points and the sea, by the normal vector can be obtained after the translation of the sides, half-plane intersection in the specific accuracy to determine whether there is a intersection.

Code

1#include <cmath>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 7 Const DoubleEPS = 1e-7;8 9 structPt {Ten     Doublex, y; OnePt (Doublex=0,Doubley=0): X (x), Y (y) {} A }; - typedef Pt VEC; - structLine { the Pt P; Vec v; -     Doubleang; - Line () {}; -Line (Pt P,vec v):P (P), V (v) {ang=atan2 (V.Y, v.x);} +     BOOL operator< (Constline& RHS)Const{ -         returnang<Rhs.ang; +     } A }; at  -Vecoperator-(Pt a,pt B) {returnVEC (a.x-b.x,a.y-b.y); } -Vecoperator+ (VEC A,vec B) {returnVEC (a.x+b.x,a.y+b.y); } -Vecoperator* (Vec A,DoubleP) {returnVEC (a.x*p,a.y*p); } - DoubleDot (VEC A,vec B) {returna.x*b.x+a.y*b.y;} - DoubleCross (Pt a,pt B) {returna.x*b.y-a.y*b.x;} in DoubleLen (Vec A) {returnsqrt (Dot (a,a));} -VEC Normal (Vec A) {DoubleL=len (A);returnVEC (-a.y/l,a.x/m); } to  + BOOLOnleft (line l,pt P) {returnCross (L.V,P-L.P) >0; } -  the Pt lineintersection (line A,line b) { *VEC u=a.p-B.P; $     DoubleT=cross (b.v,u)/Cross (A.V,B.V);Panax Notoginseng     returna.p+a.v*T; - } the intHalfplaneintersection (line* L,intn,pt*Poly) { +Sort (l,l+n); A     intFirst,last; thePt *p=NewPt[n]; +Line *q=NewLine[n]; -q[first=last=0]=l[0]; $      for(intI=1; i<n;i++) { $          while(First<last &&!onleft (l[i],p[last-1])) last--; -          while(First<last &&!onleft (l[i],p[first)) first++; -q[++last]=L[i]; the         if(Fabs (Cross (q[last].v,q[last-1].V)) <EPS) { -last--;Wuyi             if(Onleft (q[last],l[i). P)) q[last]=L[i]; the         } -         if(first<last) p[last-1]=lineintersection (q[last-1],q[last]); Wu     } -      while(First<last &&!onleft (q[first],p[last-1])) last--; About     if(last-first<=1)return 0; $p[last]=lineintersection (Q[last],q[first]); -     intm=0; -      for(inti=first;i<=last;i++) poly[m++]=P[i]; -     returnm; A } +  the Const intN = $+Ten; - Pt P[n],poly[n]; $ Line L[n]; the VEC V[n], v2[n]; the intN; the  the intMain () { -      while(SCANF ("%d", &n) = =1&&N) { in         intm,x,y; the          for(intI=0; i<n;i++) { thescanf"%d%d",&x,&y); Aboutp[i]=Pt (x, y); the         } the          for(intI=0; i<n;i++) { thev[i]=p[(i+1)%n]-P[i]; +v2[i]=Normal (V[i]); -         } the         Doubleleft=0, right=20000;Bayi          while(right-left>EPS) { the             Doublemid=left+ (Right-left)/2; the              for(intI=0; i<n;i++) L[i]=line (p[i]+v2[i]*mid,v[i]); -m=halfplaneintersection (l,n,poly); -             if(!m) Right=mid;Elseleft=mid; the         } theprintf"%.6lf\n", left); the     } the     return 0; -}

UVA 3890 Most distant points from the Sea (dichotomy + half-plane intersection)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.