Uvalive 4728 squares (plane farthest point pair)

Source: Internet
Author: User

Test instructions: N a square parallel to an axis to find the square of the farthest point pair

Solving the puzzle: First of all, we can prove that the farthest point pair must be the point pair on the convex hull, and then we can prove that the farthest point pair (the heel point of each point) must be only 3*N/2.

Then use the rotational jam to find the farthest point pair, but the algorithm on the petition is too troublesome

So I see a simple idea is:

You can enumerate each point directly, and then enumerate the point that corresponds to the farthest point (the largest triangle area)

Here the heel point satisfies a single-peak property, so you can maintain it using a double-pointer-like approach

//n a square parallel to the axis to find the square of the farthest point pair#include <cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;#defineEPS 1e-8#defineSGN (x) (X<-eps -1:x<eps? 0:1)#defineZero (x) (((x) >0? ( x):-(x)) <eps)Const intmax=5e5+7;structpoint{Doublex, y; Point (Doublex=0,Doubley=0): X (x), Y (y) {} inline pointoperator-(Constpoint& a)Const    {        returnPoint (x-a.x,y-a.y); } InlineBOOL operator< (Constpoint& a)Const    {        returnSGN (x-a.x) <0|| Zero (x-a.x) &AMP;&AMP;SGN (Y-A.Y) <0; } InlineBOOL operator!=(Constpoint& a)Const    {        return! (zero (x-a.x) &&zero (ya.y)); }};typedef point Vector;DoubleCross (Vector a,vector B) {returna.x*b.y-a.y*b.x;}DoubleDis (Point a,point B) {returnsqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (a.y-b.y));}intConvexhull (point* p,intn,point*convex) {Sort (p,p+N); intm=0, k=1;  for(intI=0; i<n; ++i) {if(p[k-1]!=P[i]) {P[k++]=P[i]; }} N=K;  for(intI=0; i<n; ++i) { while(m>1&&cross (convex[m-1]-convex[m-2],p[i]-convex[m-2]) <0) M--; Convex[m++]=P[i]; } k=m;  for(inti=n-2; i>=0; --i) { while(M>k&&cross (convex[m-1]-convex[m-2],p[i]-convex[m-2]) <0) M--; Convex[m++]=P[i]; }    if(n>1) M--; returnm;}DoubleRotatestuck (point* convex,intN//Rotation Jam (premise: a convex bag), pay attention to convex hull to focus not three points collinear{    Doubleans=0; intq=1; Convex[n]=convex[0];//Avoid taking molds     for(intp=0; p<n; ++P)//Enumerate an edge    {         while(Cross (convex[p+1]-convex[p],convex[q+1]-CONVEX[P]) >//Here's a comparison of the triangular area.Cross (convex[p+1]-CONVEX[P],CONVEX[Q]-CONVEX[P]))//find the farthest point of the corresponding P (due to the single-peak function, so the result is like a double pointer)Q= (q+1)%N; Ans=max (Ans,max (DIS (convex[p],convex[q)), Dis (convex[p+1],convex[q+1]))); }    returnans;} Point Poi[max],convex[max];DoubleSolve (intN//Find the square of the farthest point pair{    Doublefpp=0; intM=convexhull (Poi,n,convex);//first find the convex bagFpp=rotatestuck (CONVEX,M);//rotation jam to the heel point (can be obtained from the convex hull of the farthest point pair)    returnfpp*FPP;}intMain () {intT,n; DoubleW,x,y; scanf ("%d",&t);  while(t--) {scanf ("%d",&N);  for(intI=0; i<n; ++i) {scanf ("%LF%LF%LF",&x,&y,&W); poi[4*i]=Point (x, y); poi[4*i+1]=point (x+w,y); poi[4*i+2]=point (x,y+W); poi[4*i+3]=point (x+w,y+W); } printf ("%.0f\n", Solve (4*N)); }    return 0;}

Uvalive 4728 squares (plane farthest point pair)

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.