BZOJ4107 : [Wf2015]Asteroids

首先将速度相减,变成A在动而B不动,若速度为0则显然永远不会相交。

枚举A的每个点以及B的每条线段,计算这三个点共线的时刻。

将时刻排序,对于每个区间进行三分,用半平面交计算相交面积。

注意特判相交面积为0但是存在交点的情况。

时间复杂度$O(n^4\log^2n)$。

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=200;
const double eps=1e-9;
int sgn(double x){
  if(x<-eps)return -1;
  if(x>eps)return 1;
  return 0;
}
int n,m,cnt,i,j,vx,vy,x,y;double q[N],ans=-1,anst;
struct vec{
  double x,y;
  vec(){x=y=0;}
  vec(double _x,double _y){x=_x,y=_y;}
  vec operator+(vec v){return vec(x+v.x,y+v.y);}
  vec operator-(vec v){return vec(x-v.x,y-v.y);}
  vec operator*(double v){return vec(x*v,y*v);}
  vec operator/(double v){return vec(x/v,y/v);}
  double operator*(vec v){return x*v.x+y*v.y;}
  double len(){return hypot(x,y);}
  double len_sqr(){return x*x+y*y;}
}a[N],b[N],c[N],v,o;
double cross(vec a,vec b){return a.x*b.y-a.y*b.x;}
bool point_on_segment(vec p,vec a,vec b){
  return sgn(cross(b-a,p-a))==0&&sgn((p-a)*(p-b))<=0;
}
int has_intersection(vec a,vec b,vec p,vec q){
  int d1=sgn(cross(b-a,p-a)),d2=sgn(cross(b-a,q-a)),
      d3=sgn(cross(q-p,a-p)),d4=sgn(cross(q-p,b-p));
  if(d1*d2<0&&d3*d4<0)return 1;
  if(d1==0&&point_on_segment(p,a,b))return -1;
  if(d2==0&&point_on_segment(q,a,b))return -1;
  if(d3==0&&point_on_segment(a,p,q))return -1;
  if(d4==0&&point_on_segment(b,p,q))return -1;
  return 0;
}
int line_intersection(vec a,vec b,vec p,vec q,vec&o){
  double U=cross(p-a,q-p),D=cross(b-a,q-p);
  if(sgn(D)==0)return 0;
  o=a+(b-a)*(U/D);
  return 1;
}
struct P{
  double x,y;
  P(){x=y=0;}
  P(double _x,double _y){x=_x,y=_y;}
  P(vec p){x=p.x,y=p.y;}
  P operator-(const P&a)const{return P(x-a.x,y-a.y);}
  P operator+(const P&a)const{return P(x+a.x,y+a.y);}
  P operator*(double a)const{return P(x*a,y*a);}
};
namespace Halfplane{
P p[N],a[N];
struct L{
  P p,v;double a;
  L(){}
  L(P _p,P _v){p=_p,v=_v;}
  bool operator<(const L&b)const{return a<b.a;}
  void cal(){a=atan2(v.y,v.x);}
}line[N],q[N];
int cl;
double cross(const P&a,const P&b){return a.x*b.y-a.y*b.x;}
void newL(const P&a,const P&b){line[++cl]=L(a,b-a);}
bool left(const P&p,const L&l){return cross(l.v,p-l.p)>0;}
P pos(const L&a,const L&b){
  P x=a.p-b.p;
  double t=cross(b.v,x)/cross(a.v,b.v);
  return a.p+a.v*t;
}
double halfplane(){
  for(int i=1;i<=cl;i++)line[i].cal();
  sort(line+1,line+cl+1);
  int h=1,t=1;
  q[1]=line[1];
  for(int i=2;i<=cl;i++){
    while(h<t&&!left(p[t-1],line[i]))t--;
    while(h<t&&!left(p[h],line[i]))h++;
    if(fabs(cross(q[t].v,line[i].v))<eps)q[t]=left(q[t].p,line[i])?q[t]:line[i];
    else q[++t]=line[i];
    if(h<t)p[t-1]=pos(q[t],q[t-1]);
  }
  while(h<t&&!left(p[t-1],q[h]))t--;
  p[t]=pos(q[t],q[h]);
  if(t-h<=1)return -1;
  double ans=0;
  for(int i=h;i<t;i++)ans+=cross(p[i],p[i+1]);
  return ans+cross(p[t],p[h]);
}
}
double cal(double T){
  if(!sgn(T))return -1;
  double ret=-1;
  int i,j;
  for(i=0;i<=n;i++)c[i]=a[i]+(v*T);
  for(i=0;i<n;i++)for(j=0;j<m;j++)if(has_intersection(c[i],c[i+1],b[j],b[j+1]))ret=0;
  Halfplane::cl=0;
  for(i=0;i<n;i++)Halfplane::newL(P(c[i+1]),P(c[i]));
  for(i=0;i<m;i++)Halfplane::newL(P(b[i+1]),P(b[i]));
  ret=max(ret,Halfplane::halfplane());
  if(sgn(ret-ans)>0||(sgn(ret-ans)==0&&T<anst))ans=ret,anst=T;
  return ret;
}
int main(){
  scanf("%d",&n);
  for(i=0;i<n;i++)scanf("%lf%lf",&a[i].x,&a[i].y);a[n]=a[0];
  scanf("%d%d",&vx,&vy);
  scanf("%d",&m);
  for(i=0;i<m;i++)scanf("%lf%lf",&b[i].x,&b[i].y);b[m]=b[0];
  scanf("%d%d",&x,&y);
  vx-=x,vy-=y;
  if(!vx&&!y)return puts("never"),0;
  v=vec(vx,vy);
  q[cnt=1]=0;
  for(i=0;i<n;i++)
    for(j=0;j<m;j++)if(line_intersection(a[i],a[i]+v,b[j],b[j+1],o))
      q[++cnt]=(o-a[i]).len()/v.len();
  sort(q+1,q+cnt+1);
  for(i=1;i<=cnt;i++)cal(q[i]);
  for(i=1;i<cnt;i++){
    double l=q[i],r=q[i+1];
    while(l+1e-6<r){
      double len=(r-l)/3,m1=l+len,m2=r-len;
      double f1=cal(m1),f2=cal(m2);
      if(sgn(f1-f2)>=0)r=m2;else l=m1;
    }
  }
  if(ans<-0.5)puts("never");else printf("%.6f",anst);
  return 0;
}

  

时间: 2024-08-07 17:01:23

BZOJ4107 : [Wf2015]Asteroids的相关文章

HDU 1240.Asteroids!

Asteroids! Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u SubmitStatusPracticeHDU 1240 Description You're in space. You want to get home. There are asteroids. You don't want to hit them. Input Input to this problem will

HDU 1240 Asteroids!(BFS)

题目链接 Problem Description You're in space.You want to get home.There are asteroids.You don't want to hit them. Input Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the fo

POJ 3041 Asteroids 二分图

原题连接:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17985   Accepted: 9798 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <

POJ 3041 Asteroids (匈牙利算法)

Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14388 Accepted: 7828 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K astero

POJ 3041 Asteroids(模板——二分最大匹配(BFS增广))

题目链接: http://poj.org/problem?id=3041 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently

poj3041 Asteroids --- 最小点覆盖

#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<vector> #include<queue> const int maxn=510; using namespace std; int my[maxn],mx[maxn],vis[maxn],e[maxn][maxn],n;

POJ 3014:Asteroids(二分匹配,匈牙利算法)

Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14399   Accepted: 7836 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K as

1741: [Usaco2005 nov]Asteroids 穿越小行星群

1741: [Usaco2005 nov]Asteroids 穿越小行星群 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 231  Solved: 166[Submit][Status][Discuss] Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N

HDOJ 1240 Asteroids!

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1240 Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3643    Accepted Submission(s): 2413 Problem Description You're in space. You want