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&&!vy)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;
}
BZOJ4107 : [Wf2015]Asteroids的更多相关文章
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- POJ 3041 Asteroids
最小点覆盖数==最大匹配数 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12678 Accepted: ...
- Asteroids(匈牙利算法入门)
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16211 Accepted: 8819 Descri ...
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- POJ 3041 Asteroids(最小点覆盖集)
Asteroids Time Limit: 1000MS Mem ...
- Asteroids (最小覆盖)
题目很简单,但是需要推到出二分图最大匹配 = 最小覆盖 最小覆盖:证明过程http://blog.sina.com.cn/s/blog_51cea4040100h152.html Descriptio ...
- poj 3041 Asteroids(最小点覆盖)
http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)
http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- HDU-1240 Asteroids! (BFS)这里是一个三维空间,用一个6*3二维数组储存6个不同方向
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission ...
随机推荐
- .pdb文件的使用方法
1.Demo1:用DLL_01生成my.dll.my.pdb.my.lib文件. 2.Demo2:在DLL_01_APP_02中使用DLL_01的dll. 步骤: 1.vs2008打开DLL_01_A ...
- JS 获取浏览器窗口大小
JS 获取浏览器窗口大小 <script> // 获取窗口宽度 if (windows.innerWidth) { winWidth = windows.innerWidth; } els ...
- php 注册审核
注册界面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- HTTP1.0和HTTP1.1的主要区别是
HTTP/.0协议使用非持久连接,即在非持久连接下,一个tcp连接只传输一个Web对象 HTTP/.1默认使用持久连接(然而,HTTP/.1协议的客户机和服务器可以配置成使用非持久连接)在持久连接下, ...
- Java程序员要求具备的10项技能
1.语法:必须比较熟悉,在写代码的时候IDE的编辑器对某一行报错应该能够根据报错信息知道是什么样的语法错误并且知道任何修正. 2.命令:必须熟悉JDK带的一些常用命令及其常用选项,命令至少需要熟悉:a ...
- 第六届acm省赛总结(退役贴)
前言: 这是我的退役贴,之前发到了空间里,突然想到也要在博客里发一篇,虽然我很弱,但是要离开了还是有些感触,写出来和大家分享一下,希望不要见笑.回来看看,这里也好久没有更新了,这一年确实有些懈怠,解题 ...
- PHP类方法重写原则
可能我们日常工作中很少用到这块知识点,但我还是喜欢把遇到的却不清楚的知识点摸清 PHP的类方法重写规则 1.final修饰的类方法不可被子类重写 final修饰的类方法不可被子类重写 即便final ...
- [LeetCode] Ugly Number
Ugly Number Total Accepted: 20760 Total Submissions: 63208 Difficulty: Easy Write a program to check ...
- 湘潭1247 Pair-Pair(树状数组)
分析: 给定n个二元组,求选出两个二元组(可以是同一个)组成一序列其LIS为1,2,3,4的方法数. 分别记为s1, s2, s3, s4 s1,s4对应的情形为a >= b >= c & ...
- EasyUI - DataGrid 去右边空白滚动条列 分类: JavaScript 2014-09-03 10:46 1090人阅读 评论(2) 收藏
熟悉 EasyUI - DataGrid 的童鞋应该会注意到这样一个情景: 想去掉这块,找了下资料,发现也有人同样纠结:http://www.cnblogs.com/hantianwei/p/3440 ...