2016.08.07 Computational Geometry summary Test Day2

Source: Internet
Author: User
Tags cmath

T1 bzoj: [Usaco2010 open]triangle counting number triangle

See this question n so big, so think of the polar sort to engage a, but after the order immediately after the sequence, completely do not know how to write next ....

Staring at the topic for a long time after the picture without thinking so hand drawing, and then I found the secret ....

After a polar order, if two points can form a golden triangle with a different point, then that point must be within the range of the extension line between the two points connected to the origin.

And because of the extreme angle of the order, point a[1],a[2] can form a triangle, replaced by point A[1],a[3] certainly can also constitute, because their interval must be inclusive relationship.

So we make all the answers within the a[i],a[i-1] interval and calculate the contribution to the answer.

There is a i-1 range that contains it, and there is a n-i range that contains it, and then it's OK ...

Details of what to see Code, anyway feel My code is to write ugly, I also calculated that J made two times for the loop, see their code a huge short .... It's supposed to be all right again.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cmath>6 using namespacestd;7 #defineMAXN 1001008 #definePi (ACOs (-1.0))9  Ten intn,j,tmp,sum; One Long Longans; A   - structpoint{ -     DoubleX,y,ang; the }P[MAXN]; -   - Double operator* (Point A,point b) {returna.x*b.y-a.y*b.x;} -   + BOOLCMP (point A,point b) { -     returna.ang<B.ang; + } A   at intMain () { - //freopen ("Input.txt", "R", stdin); - //freopen ("Output.txt", "w", stdout); -scanf"%d",&n); -      for(intI=1; i<=n;i++) -scanf"%LF%LF", &p[i].x,&p[i].y), p[i].ang=atan2 (p[i].y,p[i].x); inSort (p+1, p+n+1, CMP); -      for(intI=1; i<=n;i++)if(p[i].ang-p[1].ANG&GT;PI) {j=tmp=i; Break;} to      for(intI=2; i<j;i++){ +         intsum=0; -          while(P[tmp].ang-p[i].ang<pi && tmp<=n) tmp++,sum++; theans+= (Long Long) sum* (I-1) * (J-i); *     } $      for(intI=1; i<j;i++)if(P[J].ANG-P[I].ANG&LT;PI) {tmp=i; Break;}Panax Notoginseng      for(inti=j+1; i<=n;i++){ -         intsum=0; the          while(P[i].ang-p[tmp].ang>pi && tmp<j) tmp++,sum++; +ans+= (Long Long) sum* (i-j) * (n-i+1); A     } theprintf"%lld\n", ans); +     return 0; -}
T1

T2 poj 3608 Bridge Across Islands

Two polygon rotation between the jam, the web gave a lot of code, a lot of work, the feeling is very much the same, almost all foreign Daniel's thesis translation over ..... What make a ymin,ymax, especially which fork multiply while, do not understand at all .... Can the distance between the segments really be cross-multiplied? (Konjac Konjac solution ... )

So I have an idea, first on the polygon P casually find an edge, and then find the nearest point on the polygon Q, and then the polygon p on the side of the edge to jump to its neighbor, while directly moving the points found on the polygon Q, and the points on both sides of the comparison, and then move until the distance to the next point is larger than the point Then do a similar one on polygon Q. Then every time you move to update your answers, you're done .....

It is also obvious that this is true because for any one edge, the distance of all points of the other polygon is a single peak. So for the current point, it must be able to find the nearest point. Note that there is no two-pointer sweep, just compare the current point with the left and right sides. Because it is not possible to have the left and right sides of the point is better than the current point, because the current point must be the left and right side of a point shifted over, and for the current edge, it must be better than at least one edge. (This nature is a little bit of a discussion can also be drawn), and because the edge every time just jump to the adjacent side, so there is absolutely no move n/2 every time, because this process is similar to the rotation jam implementation process, time complexity is also guaranteed, the constant seems to be a bit larger ...

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cmath>6 using namespacestd;7 #defineMAXN 1000108 #defineINF 1e99 Ten intn,m; One Doubleans; A  - structpoint{ -     Doublex, y; the }P[MAXN],Q[MAXN]; -  - structline{ -Point from, to; + }LP[MAXN],LQ[MAXN]; -  +Pointoperator-(point A,point b) {return(point) {a.x-b.x,a.y-b.y};} A Double operator* (Point A,point b) {returna.x*b.y-a.y*b.x;} at  - DoubleSqrDoublex) {returnx*x;} - DoubleDis (point A,point b) {returnSqr (a.x-b.x) +SQR (a.y-b.y);} -  - DoublePoint_line_dis (Point A,line b) { -     if(Dis (b.to,b). from) +min (DIS (a,b.to), Dis (A, B. from)) <max (DIS (a,b.to), Dis (A, B. from))) in     returnsqrt (min (Dis (a,b.to), Dis (A, b). from))); -     Else returnFabs ((b.to-a) * (b. from-a))/sqrt (DIS (b.to,b). from)); to } +  - voidSolvep () { the     DoubleDist=inf;intPos; *      for(intI=1; i<=m;i++){ $         DoubleTmp=point_line_dis (q[i],lp[1]);Panax Notoginseng         if(tmp<Dist) -dist=tmp,pos=i; the     } +ans=min (ans,dist); A      for(intI=2; i<=n;i++){ the          while(Point_line_dis (Q[pos],lp[i]) >point_line_dis (q[pos+1],lp[i]) pos==m?pos=1:p os++; +          while(Point_line_dis (Q[pos],lp[i]) >point_line_dis (q[pos-1],lp[i]) pos==1? pos=m:pos--; -ans=min (Ans,point_line_dis (q[pos],lp[i)); $     } $ } -  - voidSolveq () { the     DoubleDist=inf;intPos; -      for(intI=1; i<=n;i++){Wuyi         DoubleTmp=point_line_dis (p[i],lq[1]); the         if(tmp<Dist) -dist=tmp,pos=i; Wu     } -ans=min (ans,dist); About      for(intI=2; i<=m;i++){ $          while(Point_line_dis (P[pos],lq[i]) >point_line_dis (p[pos+1],lq[i]) pos==n?pos=1:p os++; -          while(Point_line_dis (P[pos],lq[i]) >point_line_dis (p[pos-1],lq[i]) pos==1? pos=n:pos--; -ans=min (Ans,point_line_dis (p[pos],lq[i)); -     } A } +  the intMain () { -      while(SCANF ("%d%d", &n,&m)! =EOF) { $ans=inf; the         if(n==0&& m==0) Break; the          for(intI=1; i<=n;i++) scanf ("%LF%LF",&p[i].x,&p[i].y); the          for(intI=1; i<=m;i++) scanf ("%LF%LF",&q[i].x,&q[i].y); thep[n+1]=p[1],q[m+1]=q[1]; -          for(intI=1; i<=n;i++) Lp[i]. from=p[i],lp[i].to=p[i+1]; in          for(intI=1; i<=m;i++) Lq[i]. from=q[i],lq[i].to=q[i+1]; the Solvep (); the Solveq (); Aboutprintf"%.6f\n", ans); the     } the     return 0; the}
View Code

2016.08.07 Computational Geometry summary Test Day2

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.