Little Bump wants to run
Time limit: 1 Sec Memory limit: 128 MB
Topic Description
Xiao Convex evening like to go to the playground to run, today he finished two laps, he played such a game.
The playground is a convex n-side shape, n vertices are numbered counterclockwise from 0 ~ n-1. Now the small convex random stand in the playground somewhere, marked as P point. The P-point and N-vertices are connected to each other to form n triangles. If the P-point, number No. 0, and point 1th form the triangle's area is the smallest of n triangles, the small convex is considered to be a correct position.
Now little Cam wants to know what the probability is that he will be right at once.
input
Line 1th contains 1 integer n, which represents the number of vertices and the number of games in the playground.
Next there are n rows, each containing 2 integers xi,yi, representing the coordinates of the vertices.
The input ensures that the points are entered in a counter-clockwise order, all of which are guaranteed to form a convex polygon. All points guarantee no three-point collinear.
Output
Output 1 digits, the probability of a correct position, to retain 4 decimal places.
Sample Input
5
1 8
0 7
0 0
8 0
8 8
Sample Output
0.6316
Data Range
For 30% of the data, 3≤n≤4,0≤x,y≤10
For 100% of the data, 3≤n≤105,-109≤x,y≤109
Source
Scoi 2015 Day1
For each edge, we can solve an inequality to get a triangle with its bottom area is larger than the No. 01 point edge of the triangle of the range, which can get n half plane, and then the half plane angle can be obtained. Code
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string
> #include <cmath> #include <algorithm> #define N 100010 #define EPS 1e-7 using namespace std;
int N,tot;
Double ans1,ans2;
struct point{double x,y;
Point operator+ (const point &p) Const{return (point) {x+p.x,y+p.y};}
Point operator-(const point &p) Const{return (point) {x-p.x,y-p.y};}
Point operator* (const double &num) Const{return (point) {x*num,y*num};} Double operator^ (const point &p) Const{return x*p.y-p.x*y;}
T[N],P[N*2];
struct node{point p,v;double ang; BOOL operator< (const node &x) Const{return Ang<x.ang| |
(ang==x.ang&& (v^ (x.p-p)) <eps);}
}S[N*2],Q[N*2]; BOOL PD (node A,point b) {return (a.v^ (B-A.P)) <-eps; double Get_ang (point P) {return atan2 (p.y,p.x);} Point Get_jd (node
A,node b) {point x=a.p-b.p;
Double t= (b.v^x)/(A.V^B.V);
return a.p+a.v*t; } void Solve () {sort (s+1,s+n+1); tot=1;
for (int i=2;i<=n;i++) if (Fabs (S[i].ang-s[i-1].ang) >eps) s[++tot]=s[i];
int L=1,r=2;q[1]=s[1];q[2]=s[2];n=tot;
for (int i=3;i<=n;i++) {while (L<R&&PD (S[I],GET_JD (q[r],q[r-1))) r--;
while (L<R&&PD (S[I],GET_JD (q[l],q[l+1))) l++;
Q[++r]=s[i];
while (L<R&&PD Q[l],get_jd (q[r],q[r-1))) r--;
while (L<R&&PD (Q[R],GET_JD (q[l],q[l+1))) l++;
if (r-l<=1) return;
q[r+1]=q[l];tot=0;
for (int i=l;i<=r;i++) P[++TOT]=GET_JD (q[i],q[i+1]);
P[TOT+1]=P[1];
for (int i=1;i<=tot;i++) Ans2+=fabs ((p[i]-p[1)) ^ (p[i+1]-p[1]));
} void Prepare () {double x1=t[1].x,y1=t[1].y;
Double x2=t[2].x,y2=t[2].y;
for (int i=2;i<=n;i++) {double x3=t[i].x,y3=t[i].y;
Double x4=t[i+1].x,y4=t[i+1].y;
Double a=y1-y2-y3+y4,b=x2-x1-x4+x3;
Double c=x3*y4-x4*y3+x2*y1-x1*y2;
Double P1,P2,P3,P4;
if (Fabs (a) <eps) {p1=0,p2=c/b,p3=1,p4=c/b;if (b>0) Swap (P1,P3), swap (P2,P4); else if (Fabs (b) <eps) {p1=c/a,p2=0,P3=c/a,p4=1;if (a<0) Swap (P1,P3), swap (P2,P4);
else if (b<0) p1=0,p2=c/b,p3=1,p4=c/b-a/b;
else p1=1,p2=c/b-a/b,p3=0,p4=c/b;
Point x1= {p1,p2},x2= (point) {P3-P1,P4-P2};
S[i]= (node) {X1,x2,get_ang (X2)};
} s[1]= (node) {T[1],t[2]-t[1],get_ang (t[2]-t[1])};
int main () {scanf ("%d", &n);
for (int i=1;i<=n;i++) scanf ("%lf%lf", &t[i].x,&t[i].y); t[n+1]=t[1];
for (int i=1;i<=n;i++) ans1+= ((t[i]-t[1)) ^ (t[i+1]-t[1]));
Prepare (); solve ();
printf ("%.4lf", ans2/ans1);
return 0; }