bzoj 4445 [SCOI2015] 小凸想跑步

题目大意:一个凸包,随机一个点使得其与前两个点组成的面积比与其他相邻两个点组成的面积小的概率

根据题意列方程,最后求n条直线的交的面积与原凸包面积的比值

 #include<bits/stdc++.h>
#define maxn 100010
#define eps 1e-10
using namespace std;
double ans,S1,S2;
struct P{
double x,y;
P(double a=,double b=){x=a,y=b;}
};
struct L{
P a,b;
double ang;
};
int sgn(double x){
return (x>eps)-(x<-eps);
}
P operator - (P a,P b){
return P(a.x-b.x,a.y-b.y);
}
double operator * (P a,P b){
return a.x*b.y-a.y*b.x;
}
bool operator < (L a,L b){
if(sgn(a.ang-b.ang)!=)return a.ang<b.ang;
return (a.a-b.a)*a.b<;
}
int n;
P s[maxn];
L l[maxn],q[maxn];P p[maxn];
int h,r,tot,cnt;
L get_line(P A,P B,P C,P D){
L l;
double a=-A.y-D.y+B.y+C.y;
double b=-B.x-C.x+A.x+D.x;
double c=-A.x*B.y-D.x*C.y+B.x*A.y+C.x*D.y;
l.b=P(b,-a);
if(sgn(a)!=)l.a=P(-c/a,);
else l.a=P(,-c/b);
return l;
}
void get_line(){
for(int i=;i<=n;++i)
l[++cnt]=get_line(s[],s[],s[i],s[i+]);
for(int i=;i<=n;++i){
l[++cnt].a=s[i];
l[cnt].b=s[i+]-s[i];
}
for(int i=;i<=cnt;++i)
l[i].ang=atan2(l[i].b.y,l[i].b.x);
}
P inter(L a,L b){
P p=b.a-a.a;
double t=(p*b.b)/(a.b*b.b);
P ans;
ans.x=a.a.x+a.b.x*t;
ans.y=a.a.y+a.b.y*t;
return ans;
}
bool jud(L a,L b,L c){
P p=inter(a,b);
return (p-c.a)*c.b>;
}
void get_Point(){
sort(l+,l+cnt+);tot=;
for(int i=;i<=cnt;++i)
if(sgn(l[i].ang-l[i-].ang)!=)
l[++tot]=l[i];
cnt=tot;tot=,h=;
q[++r]=l[];q[++r]=l[];
for(int i=;i<=cnt;++i){
if(sgn(q[r].b*q[r-].b)==)return;
if(sgn(q[h].b*q[h+].b)==)return;
while(h<r&&jud(q[r-],q[r],l[i]))r--;
while(h<r&&jud(q[h+],q[h],l[i]))h++;
q[++r]=l[i];
}
while(h<r&&jud(q[r-],q[r],q[h]))r--;
while(h<r&&jud(q[h+],q[h],q[r]))h++;
q[r+]=q[h];
for(int i=h;i<=r;++i)
p[++tot]=inter(q[i],q[i+]);
p[++tot]=p[];
}
double get_S(P a[],int cnt){
double ans=;
if(cnt<)return ;
for(int i=;i<cnt;++i){
ans+=a[i]*a[i+];
}
ans=fabs(ans/);
return ans;
}
void init(){
scanf("%d",&n);
for(int i=;i<=n;++i){
int x,y;scanf("%d%d",&x,&y);
s[i].x=x;s[i].y=y;
}
s[n+]=s[];
}
void work(){
get_line();
get_Point();
S1=get_S(s,n+);
S2=get_S(p,tot);
ans=S2/S1;
printf("%.4lf",ans);
}
int main(){
init();
work();
return ;
}
上一篇:【BZOJ4445】[SCOI2015]小凸想跑步(半平面交)


下一篇:[svc]磁盘接口与RAID