[bzoj4445] [Scoi2015] small convex want to run __ geometry

Source: Internet
Author: User
The main effect of the topic

Given a convex polygon (no three-point collinear), each endpoint is numbered 0--n-1, now a random point in the polygon, to find this point and 0, 1th points around the Triangle area is less than equal to the other adjacent points around the probability of triangles.

Analysis of n≤100000 coordinate value in int range

Set this point to (X,y), two endpoints (X1,y1), (X2,y2), and then use the cross product to represent the triangular area.
Then for the adjacent points except 0,1, an inequality can be listed. Finishing can get the form of ax+by+c≤0.
Then the upper half of the plane can be handed in. And the particularity of this problem makes it possible to not judge without solution.
Note: (x,y) must be in the convex polygon, so to point 1, 0 and as a half plane added in. And there may be a case of B equal to 0.

Time complexity O (NLOGN)

The first half plane of life, so the code is a little ugly

#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std;

const int n=100010;

typedef long Long LL;

typedef double DB;

Const DB EPS=1E-10,INF=2E9;

int n,h,t,m;

DB S;
    struct P {db x,y;
    P (db _x=0,db _y=0) {x=_x; y=_y;

}}p1[n],p2[n];

P operator + (P a,p b) {return P (A.X+B.X,A.Y+B.Y);}

P operator-(p a,p b) {return P (A.X-B.X,A.Y-B.Y);}

P operator * (P a,db b) {return P (a.x*b,a.y*b);}

DB operator * (P a,p b) {return a.x*b.x+a.y*b.y;}

DB operator ^ (P a,p b) {return a.x*b.y-a.y*b.x;}

DB Arg (P a) {return atan2 (a.y,a.x);}

BOOL Equ (db x,db y) {return fabs (x-y) <=eps;}
    struct L {P p,v;
    L () {} l (P _p,p _v) {p=_p; v=_v;

}}pl[n],d[n];

BOOL CMP (L a,l b) {return arg (A.V) <arg (B.V);}

BOOL In (P a,l b) {return ((A-B.P) ^b.v) >=0;}

P ICT (L a,l b) {return b.p+b.v* (a.v^ (A.P-B.P))/(A.V^B.V));} char c;

int read () {int x=0,sig=1;
    For (C=getchar ();c< ' 0 ' | | | | c> ' 9 '; C=getchar ()) if (c== '-') sig=-1;
    for (; c>= ' 0 ' && c<= ' 9 '; C=getchar ()) x=x*10+c-48;
return x*sig;
    DB Calc () {pl[m++]=l (P (-inf,-inf), p (0,1));
    Pl[m++]=l (P (-inf,inf), P (1,0));
    Pl[m++]=l (P (inf,inf), P (0,-1));
    Pl[m++]=l (P (inf,-inf), P ( -1,0));
    Sort (pl,pl+m,cmp);
    N=1;
        for (int i=1;i<m;i++) {if (!equ (Arg (PL[N-1].V), Arg (PL[I].V)) pl[n++]=pl[i];
    else if (in (pl[i].p,pl[n-1)) pl[n-1]=pl[i]; } h=1;
    t=2; D[1]=PL[0];
    D[2]=PL[1];
        for (int i=2;i<n;i++) {for (; h<t &&!in (ICT (d[t-1],d[t), pl[i]); t--);
        for (; h<t &&!in (ICT (d[h],d[h+1), pl[i]); h++);
    D[++t]=pl[i];
    for (; h<t &&!in (ICT (d[t],d[t-1), d[h]); t--);
    m=0;
    P2[0]=ict (D[t],d[h]);
    for (int i=h;i<t;i++) p2[++m]=ict (d[i],d[i+1]);
    P2[++m]=ict (D[t],d[h]);
    DB ret=0.0; for (int i=0;i<m;i++)ret+= (p2[i]^p2[i+1]);
return ret;
    int main () {n=read ();
        for (int i=0;i<n;i++) {int x=read (), Y=read ();
    P1[i]=p (X,y);
    } P1[n]=p1[0];
    for (int i=0;i<n;i++) s+= (p1[i]^p1[i+1]);
    Pl[m++]=l (p1[1],p1[0]-p1[1]); for (int i=1;i<n;i++) {db a=p1[0].y-p1[1].y-p1[i].y+p1[i+1].y,b=p1[1].x-p1[0].x-p1[i+1].x+p1[i].x,c= (P1
        [0]^p1[1])-(p1[i]^p1[i+1]); Pl[m].p=p (b?0:-c/a,b?-c/b:0);
    Pl[m++].v=p (B,-A);
    printf ("%.4lf\n", Calc ()/s);
return 0; }

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.