4445: [Scoi2015]小凸想跑步
Time Limit: 2 Sec Memory Limit: 128 MB
Submit: 756 Solved: 280
[Submit][Status][Discuss]
Description
小凸晚上喜歡到操場跑步,今天他跑完兩圈之後,他玩起了這樣一個遊戲。
操場是個凸n邊形,N個頂點按照逆時針從0~n-l編號。現在小凸隨機站在操場中的某個位置,標記為
P點。將P點與n個頂點各連一條邊,形成N個三角形。如果這時P點,0號點,1號點形成的三角形的面
積是N個三角形中最小的一個,小凸則認為這是一次正確站位。
現在小凸想知道他一次站位正確的機率是多少。
Input
第1行包含1個整數n,表示操場的頂點數和遊戲的次數。
接下來有N行,每行包含2個整數Xi,Yi表示頂點的座標。
輸入保證按逆時針順序輸入焦點,所有點保證構成一個n多邊形。所有點保證不存在三點共線。
Output
輸出1個數,正確站位的機率,保留4位小數。
Sample Input
5
1 8
0 7
0 0
8 0
8 8
Sample Output
0.6316
HINT
3<=N<=10^5,-10^9<=X,Y<=10^9
Source sol:
注意到題目給出的若干個條件其實就是若干個半平面,求個交就好了。
#include<cstdio>#include<algorithm>#include<string>#include<cstring>#include<cstdlib>#include<cmath>#include<iostream>using namespace std;typedef double s64;int n,m;inline int read(){ char c; int res,flag=0; while((c=getchar())>'9'||c<'0') if(c=='-')flag=1; res=c-'0'; while((c=getchar())>='0'&&c<='9') res=(res<<3)+(res<<1)+c-'0'; return flag?-res:res;}const int N=200007;struct P{ s64 x,y; friend inline P operator +(const P &a,const P &b) { return (P){a.x+b.x,a.y+b.y}; } friend inline P operator -(const P &a,const P &b) { return (P){a.x-b.x,a.y-b.y}; } friend inline P operator *(const P &a,s64 b) { return (P){a.x*b,a.y*b}; } friend inline s64 operator *(const P &a,const P &b) { return a.x*b.y-a.y*b.x; } friend inline s64 operator /(const P &a,const P &b) { return a.x*b.x+a.y*b.y; }}po[N],p[N];struct L{ P a,b,v; s64 ang; friend inline bool operator <(const L &a,const L &b) { return a.ang<b.ang||a.ang==b.ang&&a.v*(b.b-a.a)>0; } friend inline P inter(const L &a,const L &b) { P nw=b.a-a.a; s64 tt=(nw*a.v)/(a.v*b.v); return b.a+b.v*tt; } friend inline bool jud(const P &a,const L &b) { return b.v*(a-b.a)<0; }}l[N],q[N];int cnt,tot;s64 ans,pre;s64 f[N][3];inline void init(){ n=read(); for(int i=1;i<=n;++i) scanf("%lf%lf",&po[i].x,&po[i].y); po[n+1]=po[1]; for(int i=1;i<=n;++i) { l[++cnt]=(L){po[i],po[i+1],po[i+1]-po[i]}; l[cnt].ang=atan2(l[cnt].v.y,l[cnt].v.x); pre+=po[i]*po[i+1]; f[i][0]=po[i].y-po[i+1].y-f[1][0]; f[i][1]=po[i+1].x-po[i].x-f[1][1]; f[i][2]=po[i]*po[i+1]-f[1][2]; if(i!=1) { P x,y,v; x.x=x.y=0; if(!f[i][1]) x.x=-f[i][2]/f[i][0]; else x.y=-f[i][2]/f[i][1]; v.x=f[i][1]; v.y=-f[i][0]; y=x+v; l[++cnt]=(L){x,y,v}; l[cnt].ang=atan2(v.y,v.x); } }// l[++cnt]=(L){po[1],po[2],po[2]-po[1]};// l[cnt].ang=atan2(l[cnt].v.y,l[cnt].v.x);/* for(int i=1;i<=cnt;++i) { printf("%.4lf %.4lf ",l[i].a.x,l[i].a.y); printf("%.4lf %.4lf\n",l[i].v.x,l[i].v.y); }*/}inline void solve(){ sort(l+1,l+1+cnt); for(int i=1;i<=cnt;++i) { if(l[i].ang!=l[i-1].ang) ++tot; l[tot]=l[i]; } cnt=tot; tot=0; int L=1,R=2; q[1]=l[1];q[2]=l[2]; for(int i=3;i<=cnt;++i) { while(L<R&&jud(inter(q[R-1],q[R]),l[i])) R--; while(L<R&&jud(inter(q[L+1],q[L]),l[i])) ++L; q[++R]=l[i]; } while(L<R&&jud(inter(q[R-1],q[R]),q[L])) R--; while(L<R&&jud(inter(q[L+1],q[L]),q[L])) ++L; q[R+1]=q[L]; for(int i=1;i<=R;++i) p[++tot]=inter(q[i],q[i+1]); }inline void calc(){ for(int i=1;i<tot;++i) ans+=p[i]*p[i+1]; ans+=p[tot]*p[1]; if(tot<3) ans=0; printf("%.4lf",ans/pre);}int main(){// freopen("convex.in","r",stdin);// freopen("convex.out","w",stdout); init(); solve(); calc();}