"Polar sort + double pointer linear sweep" 2017 multi-school training seven HDU 6127 hard challenge

Source: Internet
Author: User

acm.hdu.edu.cn/showproblem.php?pid=6127

Test instructions

    • N points in a given planar Cartesian coordinate system, where n points have a point right at each point
    • This n point 22 can be connected by a line segment, defining the weights of each segment as the product of the point-to-point weights of the segments
    • Now we're going to go through the origin and make a straight line, asking the line not to go through any given point.
    • In the line segments of all n point 22, calculate the weights of the segments that have intersections with this line and
    • Maximize this weight and output
    • The topic guarantees that the given n points do not coincide and that the connection of any two points does not go through the origin.

Ideas

    • A straight line through the origin divides the N points into two half planes, A, b
    • It is assumed that the points in a are a1,a2....an respectively; The point weights in B are b1,b2,...... BM, respectively. The result is Suma*sumb
    • Polar Sort, enumerate each starting point, linear sweep one lap, calculate the sum of the points of the half-plane, and constantly update the optimal value

"AC"

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <string>5#include <cmath>6#include <algorithm>7 8 using namespacestd;9typedefLong Longll;Ten Const intmaxn=5e4+2; One intN; A DoubleXX[MAXN]; - DoubleYY[MAXN]; - ll VAL[MAXN]; the ll ANS[MAXN]; -  - DoubleDisDoubleX1,DoubleY1,DoubleX2,Doubley2) {returnsqrt ((x1-x2) * (X1-X2) + (y1-y2) * (y1-y2));} - Const DoubleEPS = 1e-6; + struct Point - { +     Doublex; A     Doubley; at ll Val; -     Doubledis; -     DoubleAlf; - Point () {} -Point (Double_x,Double_y): X (_x), Y (_y) {} -Point (Double_x,Double_y,ll _val): X (_x), Y (_y), Val (_val) {} inPointoperator-(ConstPoint &t)Const -     { to         returnPoint (x-t.x,y-t.y); +     } -     Double operator^(ConstPoint &t)Const the     { *         return(X*T.Y)-(y*t.x); $     }Panax Notoginseng }P[MAXN]; -Point O (0.0,0.0); the BOOLcmpConstPoint &a,ConstPoint &b)//Sort by quadrant, then by polar angle, then by distance + { A     if(A.Y = =0&& B.y = =0&& a.x*b.x <=0)returnA.x>b.x; the     if(A.Y = =0&& a.x >=0&& B.Y! =0)return true; +     if(B.y = =0&& b.x >=0&& A.Y! =0)return false; -     if(B.y*a.y <=0)returnA.y>b.y; $     return((a-o) ^ (b-o)) >0.0|| (((a-o) ^ (b-o)) = =0.0&& a.x <b.x);  $ } -  - intMain () the { -     intT;Wuyiscanf"%d",&T); the      while(t--) -     { Wumemset (ans,0,sizeof(ans)); -scanf"%d",&n); Aboutll sum=0; $          for(intI=0; i<n;i++) -         { -scanf"%lf%lf%i64d",&xx[i],&yy[i],&val[i]); -p[i]=Point (Xx[i],yy[i],val[i]);  Asum+=Val[i]; +         }    theSort (p,p+n,cmp);  -         intL=1; $ ll Res; theans[0]+=p[0].val; the          for(intI=0; i<n;i++) the         {     the             if(i>0) ans[i]=ans[i-1]-p[i-1].val; -              while((p[i]-o) ^ (p[l]-o)) >0) in             { theans[i]+=P[l].val; theL= (L +1)%N;  About             }    the             if(i==0) res=ans[i]* (sum-ans[i]); the             ElseRes=max (res,ans[i]* (sum-ans[i])); the         } +printf"%i64d\n", RES);  -     } the     return 0;Bayi}
Polar sort + Linear sweep template

Template

1 BOOLcmpConstPoint &a,ConstPoint &b)//Sort by quadrant, then by polar angle, then by distance2 {3     if(A.Y = =0&& B.y = =0&& a.x*b.x <=0)returnA.x>b.x;4     if(A.Y = =0&& a.x >=0&& B.Y! =0)return true;5     if(B.y = =0&& b.x >=0&& A.Y! =0)return false;6     if(B.y*a.y <=0)returnA.y>b.y;7     return((a-o) ^ (b-o)) >0.0|| (((a-o) ^ (b-o)) = =0.0&& a.x <b.x); 8}
Polar Sort, O is the center point
1    intL=1;2 ll Res;3        //Ans[0]+=p[0].val;4          for(intI=0; i<n;i++)5         {    6             //if (i>0) ans[i]=ans[i-1]-p[i-1].val;7              while((p[i]-o) ^ (p[l]-o)) >0)8             {9                //the necessary operationsTenL= (L +1)%N;  One             }    A             //Update the answer here -}
linear sweep

"Polar sort + double pointer linear sweep" 2017 multi-school training seven HDU 6127 hard challenge

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.